Add algorithm selection

This commit is contained in:
Patrick Niklaus 2017-03-01 23:34:36 +00:00 committed by Patrick Niklaus
parent 3f485ac09b
commit ed00965d18
3 changed files with 57 additions and 4 deletions

View File

@ -55,12 +55,27 @@ namespace engine
*
* In addition, shared memory can be used for datasets loaded with osrm-datastore.
*
* You can chose between three algorithms:
* - Algorithm::CH
* Contraction Hierarchies, extremely fast queries but slow pre-processing. The default right now.
* - Algorithm::CoreCH
* Contractoin Hierachies with partial contraction for faster pre-processing but slower queries.
*
* Algorithm::CH is specified we will automatically upgrade to CoreCH if we find the data for it.
* If Algorithm::CoreCH is specified and we don't find the speedup data, we fail hard.
*
* \see OSRM, StorageConfig
*/
struct EngineConfig final
{
bool IsValid() const;
enum class Algorithm
{
CH, // will upgrade to CoreCH if it finds core data
CoreCH // will fail hard if there is no core data
};
storage::StorageConfig storage_config;
int max_locations_trip = -1;
int max_locations_viaroute = -1;
@ -68,6 +83,7 @@ struct EngineConfig final
int max_locations_map_matching = -1;
int max_results_nearest = -1;
bool use_shared_memory = true;
Algorithm algorithm = Algorithm::CH;
};
}
}

View File

@ -18,14 +18,34 @@ namespace osrm
OSRM::OSRM(engine::EngineConfig &config)
{
if (engine::Engine<engine::algorithm::CoreCH>::CheckCompability(config))
if (config.algorithm == EngineConfig::Algorithm::CoreCH ||
config.algorithm == EngineConfig::Algorithm::CH)
{
engine_ = std::make_unique<engine::Engine<engine::algorithm::CoreCH>>(config);
bool corech_compatible = engine::Engine<engine::algorithm::CoreCH>::CheckCompability(config);
// Activate CoreCH if we can because it is faster
if (config.algorithm == EngineConfig::Algorithm::CH && corech_compatible)
{
config.algorithm = EngineConfig::Algorithm::CoreCH;
}
// throw error if dataset is not usable with CoreCH
if (config.algorithm == EngineConfig::Algorithm::CoreCH && !corech_compatible)
{
throw util::exception("Dataset is not compatible with CoreCH.");
}
}
else
switch (config.algorithm)
{
// Will fail hard if there is no CH data
case EngineConfig::Algorithm::CH:
engine_ = std::make_unique<engine::Engine<engine::algorithm::CH>>(config);
break;
case EngineConfig::Algorithm::CoreCH:
engine_ = std::make_unique<engine::Engine<engine::algorithm::CoreCH>>(config);
break;
default:
util::exception("Algorithm not implemented!");
}
}
OSRM::~OSRM() = default;

View File

@ -1,5 +1,6 @@
#include "server/server.hpp"
#include "util/log.hpp"
#include "util/exception.hpp"
#include "util/version.hpp"
#include "osrm/engine_config.hpp"
@ -48,6 +49,15 @@ const static unsigned INIT_OK_START_ENGINE = 0;
const static unsigned INIT_OK_DO_NOT_START_ENGINE = 1;
const static unsigned INIT_FAILED = -1;
EngineConfig::Algorithm stringToAlgorithm(const std::string &algorithm)
{
if (algorithm == "CH")
return EngineConfig::Algorithm::CH;
if (algorithm == "CoreCH")
return EngineConfig::Algorithm::CoreCH;
throw util::exception("Invalid algorithm name: " + algorithm);
}
// generate boost::program_options object for the routing part
inline unsigned generateServerProgramOptions(const int argc,
const char *argv[],
@ -56,6 +66,7 @@ inline unsigned generateServerProgramOptions(const int argc,
int &ip_port,
int &requested_num_threads,
bool &use_shared_memory,
std::string &algorithm,
bool &trial,
int &max_locations_trip,
int &max_locations_viaroute,
@ -87,6 +98,9 @@ inline unsigned generateServerProgramOptions(const int argc,
("shared-memory,s",
value<bool>(&use_shared_memory)->implicit_value(true)->default_value(false),
"Load data from shared memory") //
("algorithm,a",
value<std::string>(&algorithm)->default_value("CH"),
"Algorithm to use for the data. Can be CH, CoreCH") //
("max-viaroute-size",
value<int>(&max_locations_viaroute)->default_value(500),
"Max. locations supported in viaroute query") //
@ -178,6 +192,7 @@ int main(int argc, const char *argv[]) try
EngineConfig config;
boost::filesystem::path base_path;
std::string algorithm;
const unsigned init_result = generateServerProgramOptions(argc,
argv,
base_path,
@ -185,6 +200,7 @@ int main(int argc, const char *argv[]) try
ip_port,
requested_thread_num,
config.use_shared_memory,
algorithm,
trial_run,
config.max_locations_trip,
config.max_locations_viaroute,
@ -264,6 +280,7 @@ int main(int argc, const char *argv[]) try
}
return EXIT_FAILURE;
}
config.algorithm = stringToAlgorithm(algorithm);
util::Log() << "starting up engines, " << OSRM_VERSION;