Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move argparse boilerplate code to private header #4169

Merged
merged 24 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ec6b9b6
refactor valhalla_add_elevation a bit and move the mjolnir util to baldr
nilsnolde Jun 17, 2023
1132e0c
add -j flag to all multithreaded executables
nilsnolde Jun 17, 2023
71918ab
changelog
nilsnolde Jun 17, 2023
95f299c
remove deprecated heder
nilsnolde Jun 17, 2023
918ecfa
oops
nilsnolde Jun 17, 2023
6b8f931
moved the argparse boilerplate code to a private header which all pro…
nilsnolde Jun 17, 2023
2ff4f01
cxxopts entry not needed in cmake
nilsnolde Jun 17, 2023
1c78a88
add missing midgard header
nilsnolde Jun 17, 2023
28b1d89
some fixes
nilsnolde Jun 17, 2023
c4a5e50
huch, quite a logical mistake..
nilsnolde Jun 17, 2023
a22ec55
Merge branch 'nn-concurrency2' into nn--exe-boilerplate
nilsnolde Jun 17, 2023
2e7e735
few nits
nilsnolde Jun 17, 2023
81785e4
more fixes
nilsnolde Jun 17, 2023
57a76d7
remove left-overs
nilsnolde Jun 17, 2023
30ff914
keep deduplication in the executable valhalla_add_elevation
nilsnolde Jun 21, 2023
1ca6677
merge nn-concurrency2
nilsnolde Jun 21, 2023
509e3b2
keep deduplication in the executable valhalla_add_elevation
nilsnolde Jun 21, 2023
a919eb6
Merge branch 'nn-concurrency2' into nn--exe-boilerplate
nilsnolde Jun 21, 2023
9d81545
use __FILE__ to find the executable name; overhaul src/CMakeLists.txt…
nilsnolde Jun 21, 2023
d0b33c3
Merge branch 'master' into nn--exe-boilerplate
nilsnolde Jun 23, 2023
3460c08
move get_tile_ids function into the executable
nilsnolde Jul 4, 2023
f042793
Merge branch 'master' into nn--exe-boilerplate
nilsnolde Jul 4, 2023
f4575c9
add filesystem::stem() to return the file name without extension and …
nilsnolde Jul 4, 2023
79fbea3
Merge branch 'master' into nn--exe-boilerplate
nilsnolde Jul 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
huch, quite a logical mistake..
  • Loading branch information
nilsnolde committed Jun 17, 2023
commit c4a5e50d155c99e370d8aae210ee162d69f4b252
11 changes: 6 additions & 5 deletions src/mjolnir/valhalla_add_elevation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ using namespace valhalla::mjolnir;

boost::property_tree::ptree config;
std::vector<std::string> tiles;
uint32_t num_threads = 0;

/*
* This tool downloads elevations from remote storage for each provided tile.
Expand Down Expand Up @@ -81,7 +80,7 @@ bool parse_arguments(int argc, char** argv) {
("v,version", "Print the version of this software.")
("c,config", "Path to the configuration file.", opt::value<std::string>())
("t,tiles", "Tiles to add elevations to", opt::value<std::vector<std::string>>(tiles))
("j,concurrency", "Number of threads to use. Defaults to all threads.", opt::value<uint32_t>(num_threads));
("j,concurrency", "Number of threads to use. Defaults to all threads.", opt::value<uint32_t>());
// clang-format on

auto result = options.parse(argc, argv);
Expand All @@ -94,9 +93,11 @@ bool parse_arguments(int argc, char** argv) {
return false;
}

if (num_threads) {
config.put<unsigned int>("mjolnir.concurrency", num_threads);
}
config.put<uint32_t>("mjolnir.concurrency",
result.count("concurrency")
? result["concurrency"].as<uint32_t>()
: config.get<uint32_t>("mjolnir.concurrency",
std::thread::hardware_concurrency()));

if (!result.count("tiles")) {
std::cerr << "Tile file is required\n\n" << options.help() << "\n\n";
Expand Down
20 changes: 10 additions & 10 deletions src/mjolnir/valhalla_add_predicted_traffic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ namespace bpt = boost::property_tree;
// args
boost::property_tree::ptree config;
filesystem::path traffic_tile_dir;
unsigned int num_threads;
bool summary = false;

namespace {
Expand Down Expand Up @@ -253,11 +252,11 @@ bool ParseArguments(int argc, char* argv[]) {
options.add_options()
("h,help", "Print this help message.")
("v,version", "Print the version of this software.")
("j,concurrency", "Number of threads to use.", cxxopts::value<unsigned int>(num_threads))
("j,concurrency", "Number of threads to use.", cxxopts::value<unsigned int>())
("c,config", "Path to the json configuration file.", cxxopts::value<std::string>())
("i,inline-config", "Inline json config.", cxxopts::value<std::string>())
("s,summary", "Output summary information about traffic coverage for the tile set", cxxopts::value<bool>(summary))
("t,traffic_tile_dir", "positional argument", cxxopts::value<std::string>());
("t,traffic-tile-dir", "positional argument", cxxopts::value<std::string>());
// clang-format on

options.parse_positional({"traffic-tile-dir"});
Expand All @@ -274,11 +273,11 @@ bool ParseArguments(int argc, char* argv[]) {
exit(0);
}

if (!result.count("traffic_tile_dir")) {
if (!result.count("traffic-tile-dir")) {
std::cout << "You must provide a tile directory to read the csv tiles from.\n";
return false;
}
traffic_tile_dir = filesystem::path(result["traffic_tile_dir"].as<std::string>());
traffic_tile_dir = filesystem::path(result["traffic-tile-dir"].as<std::string>());

// Read the config file
if (result.count("inline-config")) {
Expand All @@ -293,9 +292,11 @@ bool ParseArguments(int argc, char* argv[]) {
return false;
}

if (num_threads) {
config.put<unsigned int>("mjolnir.concurrency", num_threads);
}
config.put<uint32_t>("mjolnir.concurrency",
result.count("concurrency")
? result["concurrency"].as<uint32_t>()
: config.get<uint32_t>("mjolnir.concurrency",
std::thread::hardware_concurrency()));

return true;
} catch (cxxopts::OptionException& e) {
Expand Down Expand Up @@ -341,11 +342,10 @@ int main(int argc, char** argv) {
std::random_device rd;
std::shuffle(traffic_tiles.begin(), traffic_tiles.end(), std::mt19937(rd()));

auto num_threads = std::max(1U, config.get<uint32_t>("mjolnir.concurrency"));
LOG_INFO("Adding predicted traffic with " + std::to_string(num_threads) + " threads");
std::vector<std::shared_ptr<std::thread>> threads(num_threads);

std::cout << traffic_tile_dir << std::endl;

LOG_INFO("Parsing speeds from " + std::to_string(traffic_tiles.size()) + " tiles.");
size_t floor = traffic_tiles.size() / threads.size();
size_t at_ceiling = traffic_tiles.size() - (threads.size() * floor);
Expand Down
15 changes: 7 additions & 8 deletions src/mjolnir/valhalla_assign_speeds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ int main(int argc, char** argv) {
// args
filesystem::path config_file_path;
bpt::ptree config;
uint32_t num_threads = 0;

try {
// clang-format off
Expand All @@ -93,7 +92,7 @@ int main(int argc, char** argv) {
("v,version", "Print the version of this software.")
("c,config", "Path to the json configuration file.", cxxopts::value<std::string>())
("i,inline-config", "Inline JSON config", cxxopts::value<std::string>())
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>(num_threads));
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>());
// clang-format on

auto result = options.parse(argc, argv);
Expand Down Expand Up @@ -122,9 +121,11 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

if (num_threads) {
config.put<unsigned int>("mjolnir.concurrency", num_threads);
}
config.put<uint32_t>("mjolnir.concurrency",
result.count("concurrency")
? result["concurrency"].as<uint32_t>()
: config.get<uint32_t>("mjolnir.concurrency",
std::thread::hardware_concurrency()));

if (!result.count("config")) {
std::cout << "You must provide a config for loading and modifying tiles.\n";
Expand Down Expand Up @@ -158,9 +159,7 @@ int main(int argc, char** argv) {
std::shuffle(tilequeue.begin(), tilequeue.end(), std::mt19937(3));

// spawn threads to modify the tiles
auto concurrency =
std::max(static_cast<unsigned int>(1),
config.get<unsigned int>("mjolnir.concurrency", std::thread::hardware_concurrency()));
auto concurrency = std::max(1U, config.get<uint32_t>("mjolnir.concurrency"));
std::vector<std::shared_ptr<std::thread>> threads(concurrency);
std::list<std::promise<std::pair<size_t, size_t>>> results;
std::mutex lock;
Expand Down
22 changes: 11 additions & 11 deletions src/mjolnir/valhalla_build_statistics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ using namespace valhalla::baldr;
using namespace valhalla::mjolnir;

filesystem::path config_file_path;
uint32_t num_threads = 0;
boost::property_tree::ptree pt;

namespace {

Expand Down Expand Up @@ -583,7 +583,7 @@ bool ParseArguments(int argc, char* argv[]) {
("h,help", "Print this help message")
("v,version", "Print the version of this software.")
("c,config", "Path to the json configuration file.", cxxopts::value<std::string>())
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>(num_threads));
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>());
// clang-format on

auto result = options.parse(argc, argv);
Expand All @@ -601,10 +601,18 @@ bool ParseArguments(int argc, char* argv[]) {
if (result.count("config") &&
filesystem::is_regular_file(config_file_path =
filesystem::path(result["config"].as<std::string>()))) {
return true;
rapidjson::read_json(config_file_path.string(), pt);
} else {
std::cerr << "Configuration file is required\n\n" << options.help() << "\n\n";
return false;
}

pt.put<uint32_t>("mjolnir.concurrency",
result.count("concurrency")
? result["concurrency"].as<uint32_t>()
: pt.get<uint32_t>("mjolnir.concurrency",
std::thread::hardware_concurrency()));
return true;
} catch (const cxxopts::OptionException& e) {
std::cout << "Unable to parse command line options because: " << e.what() << std::endl;
}
Expand All @@ -617,14 +625,6 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

// check the type of input
boost::property_tree::ptree pt;
rapidjson::read_json(config_file_path.string(), pt);

if (num_threads) {
pt.put<unsigned int>("mjolnir.concurrency", num_threads);
}

// configure logging
auto logging_subtree = pt.get_child_optional("mjolnir.logging");
if (logging_subtree) {
Expand Down
11 changes: 6 additions & 5 deletions src/mjolnir/valhalla_build_tiles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ int main(int argc, char** argv) {
BuildStage start_stage = BuildStage::kInitialize;
BuildStage end_stage = BuildStage::kCleanup;
boost::property_tree::ptree pt;
uint32_t num_threads = 0;

try {

Expand All @@ -50,7 +49,7 @@ int main(int argc, char** argv) {
("s,start", "Starting stage of the build pipeline", cxxopts::value<std::string>()->default_value("initialize"))
("e,end", "End stage of the build pipeline", cxxopts::value<std::string>()->default_value("cleanup"))
("input_files", "positional arguments", cxxopts::value<std::vector<std::string>>(input_files))
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>(num_threads));
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>());
// clang-format on

options.parse_positional({"input_files"});
Expand Down Expand Up @@ -82,9 +81,11 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

if (num_threads) {
pt.put<unsigned int>("mjolnir.concurrency", num_threads);
}
pt.put<uint32_t>("mjolnir.concurrency",
result.count("concurrency")
? result["concurrency"].as<uint32_t>()
: pt.get<uint32_t>("mjolnir.concurrency",
std::thread::hardware_concurrency()));

// configure logging
auto logging_subtree = pt.get_child_optional("mjolnir.logging");
Expand Down
16 changes: 9 additions & 7 deletions src/mjolnir/valhalla_convert_transit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
filesystem::path config_file_path;
boost::property_tree::ptree pt;
std::vector<valhalla::mjolnir::OneStopTest> onestoptests;
uint32_t num_threads = 0;

bool ParseArguments(int argc, char* argv[]) {
try {
Expand All @@ -24,7 +23,7 @@ bool ParseArguments(int argc, char* argv[]) {
("h,help", "Print this help message.")
("v,version", "Print the version of this software.")
("c,config", "Path to the json configuration file.", cxxopts::value<std::string>())
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>(num_threads))
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>())
("target_directory", "Path to write transit tiles", cxxopts::value<std::string>())
("test_file", "file where tests are written", cxxopts::value<std::string>());

Expand All @@ -47,14 +46,16 @@ bool ParseArguments(int argc, char* argv[]) {
filesystem::is_regular_file(config_file_path =
filesystem::path(result["config"].as<std::string>()))) {
rapidjson::read_json(config_file_path.string(), pt);
return true;
} else {
std::cerr << "Configuration file is required\n" << options.help() << "\n\n";
return false;
}

if (num_threads) {
pt.put<unsigned int>("mjolnir.concurrency", num_threads);
}
pt.put<uint32_t>("mjolnir.concurrency",
result.count("concurrency")
? result["concurrency"].as<uint32_t>()
: pt.get<uint32_t>("mjolnir.concurrency",
std::thread::hardware_concurrency()));

if (result.count("target_directory")) {
pt.get_child("mjolnir").erase("transit_dir");
Expand All @@ -69,6 +70,7 @@ bool ParseArguments(int argc, char* argv[]) {
std::sort(onestoptests.begin(), onestoptests.end());
}

return true;
} catch (cxxopts::OptionException& e) {
std::cerr << "Unable to parse command line options because: " << e.what() << "\n"
<< "This is a bug, please report it at " PACKAGE_BUGREPORT << "\n";
Expand All @@ -79,7 +81,7 @@ bool ParseArguments(int argc, char* argv[]) {
<< std::endl;
}

return EXIT_FAILURE;
return false;
}

int main(int argc, char** argv) {
Expand Down
25 changes: 13 additions & 12 deletions src/mjolnir/valhalla_ingest_transit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <cxxopts.hpp>

filesystem::path config_file_path;
uint32_t num_threads = 0;
boost::property_tree::ptree pt;

bool ParseArguments(int argc, char* argv[]) {
try {
Expand All @@ -20,7 +20,7 @@ bool ParseArguments(int argc, char* argv[]) {
("h,help", "Print this help message.")
("v,version", "Print the version of this software.")
("c,config", "Path to the json configuration file.", cxxopts::value<std::string>())
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>(num_threads));
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>());
// clang-format on

auto result = options.parse(argc, argv);
Expand All @@ -38,16 +38,25 @@ bool ParseArguments(int argc, char* argv[]) {
if (result.count("config") &&
filesystem::is_regular_file(config_file_path =
filesystem::path(result["config"].as<std::string>()))) {
return true;
rapidjson::read_json(config_file_path.string(), pt);
} else {
std::cerr << "Configuration file is required\n" << options.help() << "\n\n";
return false;
}

pt.put<uint32_t>("mjolnir.concurrency",
result.count("concurrency")
? result["concurrency"].as<uint32_t>()
: pt.get<uint32_t>("mjolnir.concurrency",
std::thread::hardware_concurrency()));

return true;
} catch (cxxopts::OptionException& e) {
std::cerr << "Unable to parse command line options because: " << e.what() << "\n"
<< "This is a bug, please report it at " PACKAGE_BUGREPORT << "\n";
}

return EXIT_FAILURE;
return false;
}

int main(int argc, char** argv) {
Expand All @@ -56,14 +65,6 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

// args and config file loading
boost::property_tree::ptree pt;
rapidjson::read_json(config_file_path.string(), pt);

if (num_threads) {
pt.put<unsigned int>("mjolnir.concurrency", num_threads);
}

// spawn threads to download all the tiles returning a list of
// tiles that ended up having dangling stop pairs
auto dangling_tiles = valhalla::mjolnir::ingest_transit(pt);
Expand Down
24 changes: 11 additions & 13 deletions src/mjolnir/valhalla_validate_transit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using namespace valhalla::mjolnir;

filesystem::path config_file_path;
uint32_t num_threads = 0;
boost::property_tree::ptree pt;

bool ParseArguments(int argc, char* argv[]) {
try {
Expand All @@ -35,7 +35,7 @@ bool ParseArguments(int argc, char* argv[]) {
("h,help", "Print this help message.")
("v,version", "Print the version of this software.")
("c,config", "Path to the json configuration file.", cxxopts::value<std::string>())
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>(num_threads));
("j,concurrency", "Number of threads to use. Defaults to all threads.", cxxopts::value<uint32_t>());
// clang-format on

auto result = options.parse(argc, argv);
Expand All @@ -53,17 +53,23 @@ bool ParseArguments(int argc, char* argv[]) {
if (result.count("config") &&
filesystem::is_regular_file(config_file_path =
filesystem::path(result["config"].as<std::string>()))) {
return true;
} else {
std::cerr << "Configuration file is required\n\n" << options.help() << "\n\n";
return false;
}

return false;
pt.put<uint32_t>("mjolnir.concurrency",
result.count("concurrency")
? result["concurrency"].as<uint32_t>()
: pt.get<uint32_t>("mjolnir.concurrency",
std::thread::hardware_concurrency()));

return true;
} catch (const cxxopts::OptionException& e) {
std::cout << "Unable to parse command line options because: " << e.what() << std::endl;
}

return EXIT_FAILURE;
return false;
}

int main(int argc, char** argv) {
Expand All @@ -72,14 +78,6 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

// check what type of input we are getting
boost::property_tree::ptree pt;
rapidjson::read_json(config_file_path.string(), pt);

if (num_threads) {
pt.put<unsigned int>("mjolnir.concurrency", num_threads);
}

// configure logging
auto logging_subtree = pt.get_child_optional("mjolnir.logging");
if (logging_subtree) {
Expand Down