From 250d80e54b5501cc5d810c9c42fbca8064927cf7 Mon Sep 17 00:00:00 2001 From: Yannick Brosseau Date: Mon, 20 Nov 2023 11:03:23 -0500 Subject: [PATCH] Add an program option to specify the number of threads. With the latest changes, we can do calculation in parallel in multiple different threads. The updateCache API call is not thread-safe yet. We need to add locks around the transitData object and the updateCache call. But in the majority of cases, we do not use this api call, so it's safe to use as is for the moment. --- connection_scan_algorithm/include/program_options.hpp | 1 + connection_scan_algorithm/src/program_options.cpp | 7 +++++++ .../src/transit_routing_http_server.cpp | 6 ++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/connection_scan_algorithm/include/program_options.hpp b/connection_scan_algorithm/include/program_options.hpp index 64ff834..f68418a 100644 --- a/connection_scan_algorithm/include/program_options.hpp +++ b/connection_scan_algorithm/include/program_options.hpp @@ -18,6 +18,7 @@ namespace TrRouting bool debug; bool cacheAllConnectionSets; bool useEuclideanDistance; + int numberOfThreads; std::string algorithm; std::string dataFetcherShortname; std::string osrmWalkingPort; diff --git a/connection_scan_algorithm/src/program_options.cpp b/connection_scan_algorithm/src/program_options.cpp index 9d18a75..d715d37 100644 --- a/connection_scan_algorithm/src/program_options.cpp +++ b/connection_scan_algorithm/src/program_options.cpp @@ -20,6 +20,8 @@ namespace TrRouting { ("cacheAllConnectionSets", boost::program_options::value() ->default_value(false), "cache all connections set instead of the ones from the last used scenario"); options.add_options() ("useEuclideanDistance", boost::program_options::value() ->default_value(false), "Use euclidean distance calculation instead of OSRM for access and egress calculations"); + options.add_options() + ("threads", boost::program_options::value() ->default_value(1), "Number of threads to use to serve requests"); options.add_options() ("osrmPort,osrmWalkPort,osrmWalkingPort", boost::program_options::value()->default_value("5000"), "osrm walking port"); options.add_options() @@ -46,6 +48,7 @@ namespace TrRouting { cachePath = "cache"; cacheAllConnectionSets = false; useEuclideanDistance = false; + numberOfThreads = 1; osrmWalkingPort = "5000"; osrmCyclingPort = "8000"; osrmDrivingPort = "7000"; @@ -85,6 +88,10 @@ namespace TrRouting { { useEuclideanDistance = variablesMap["useEuclideanDistance"].as(); } + if(variablesMap.count("threads") == 1) + { + numberOfThreads = variablesMap["threads"].as(); + } if(variablesMap.count("osrmWalkPort") == 1) { diff --git a/connection_scan_algorithm/src/transit_routing_http_server.cpp b/connection_scan_algorithm/src/transit_routing_http_server.cpp index 6ad6cc6..67cc566 100644 --- a/connection_scan_algorithm/src/transit_routing_http_server.cpp +++ b/connection_scan_algorithm/src/transit_routing_http_server.cpp @@ -136,13 +136,11 @@ int main(int argc, char** argv) { spdlog::info("Using OSRM for access/egress node time/distance"); } - spdlog::info("preparing server..."); + spdlog::info("preparing server with {} threads...", programOptions.numberOfThreads); - //HTTP-server using 1 thread - //Unless you do more heavy non-threaded processing in the resources, - //1 thread is usually faster than several threads HttpServer server; server.config.port = programOptions.port; + server.config.thread_pool_size = programOptions.numberOfThreads; // updateCache: server.resource["^/updateCache[/]?$"]["GET"]=[&server, &transitData](std::shared_ptr serverResponse, std::shared_ptr request) {