Skip to content

Commit

Permalink
#1550 document and test order of parsing arguments passed from cli, f…
Browse files Browse the repository at this point in the history
…ile and appConfig
  • Loading branch information
Jakub Strzebonski committed Mar 1, 2022
1 parent b19b24c commit e151dd3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/md/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ int main(int argc, char** argv) {

It is worth noting that if you run your application with any of vt's command-line arguments and at the same time you define and pass `AppConfig` to `vt::initialize`, CLI arguments have a higher priority. In other words, if you predefine in source code and give from the command line the same vt's argument, but with a different value, the program will use the CLI one.

There is also an option to use configuration file. Refer to CLI11 documentation for details https://cliutils.github.io/CLI11/book/chapters/config.html. Important thing to remember - CLI11 processes configuration file before command line arguments, so in the end command line arguments might overwrite values defined in configuration file.

\section tutorial-walkthrough Tutorial Code Snippets

This page walks through the tutorial that exists in the source code. See
Expand Down
2 changes: 1 addition & 1 deletion src/vt/configs/arguments/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ std::tuple<int, std::string> ArgConfig::parse(
return parseToConfig(argc, argv, config_);
}

// if user defines appConfig, parse into temporary config for later comparison.
// If user defines appConfig, parse into temporary config for later comparison.
AppConfig config{*appConfig};
auto const parse_result = parseToConfig(argc, argv, config);

Expand Down
91 changes: 91 additions & 0 deletions tests/unit/runtime/test_initialization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

#include <vt/collective/startup.h>

#include <fstream>

namespace vt { namespace tests { namespace unit {

struct TestInitialization : TestParallelHarness { };
Expand Down Expand Up @@ -161,4 +163,93 @@ TEST_F(TestInitialization, test_initialize_with_args_and_appconfig) {
EXPECT_EQ(custom_argv[2], nullptr);
}

TEST_F(TestInitialization, test_initialize_with_file_and_args) {
MPI_Comm comm = MPISingletonMultiTest::Get()->getComm();

static char prog_name[]{"vt_program"};
static char cli_argument[]{"--cli_argument=100"};
static char vt_no_terminate[]{"--vt_no_terminate"};
static char vt_lb_name[]{"--vt_lb_name=RotateLB"};
static char vt_input_config[]{"--vt_input_config=test_cfg.toml"};

std::vector<char *> custom_args;
custom_args.emplace_back(prog_name);
custom_args.emplace_back(cli_argument);
custom_args.emplace_back(vt_no_terminate);
custom_args.emplace_back(vt_input_config);
custom_args.emplace_back(vt_lb_name);
custom_args.emplace_back(nullptr);

int custom_argc = custom_args.size() - 1;
char **custom_argv = custom_args.data();

EXPECT_EQ(custom_argc, 5);

int this_rank;
MPI_Comm_rank(comm, &this_rank);
if (this_rank == 0) {
std::ofstream cfg_file_{"test_cfg.toml", std::ofstream::out | std::ofstream::trunc};
cfg_file_ << "vt_lb_name = RandomLB\n";
cfg_file_.close();
}
MPI_Barrier(comm);

vt::initialize(custom_argc, custom_argv, no_workers, true, &comm);

EXPECT_EQ(theConfig()->prog_name, "vt_program");
EXPECT_EQ(theConfig()->vt_no_terminate, true);
EXPECT_EQ(theConfig()->vt_lb_name, "RotateLB");

EXPECT_EQ(custom_argc, 2);
EXPECT_STREQ(custom_argv[0], "vt_program");
EXPECT_STREQ(custom_argv[1], "--cli_argument=100");
EXPECT_EQ(custom_argv[2], nullptr);
}

TEST_F(TestInitialization, test_initialize_with_file_args_and_appconfig) {
MPI_Comm comm = MPISingletonMultiTest::Get()->getComm();

static char prog_name[]{"vt_program"};
static char cli_argument[]{"--cli_argument=100"};
static char vt_no_terminate[]{"--vt_no_terminate"};
static char vt_lb_name[]{"--vt_lb_name=RotateLB"};
static char vt_input_config[]{"--vt_input_config=test_cfg.toml"};

std::vector<char*> custom_args;
custom_args.emplace_back(prog_name);
custom_args.emplace_back(cli_argument);
custom_args.emplace_back(vt_no_terminate);
custom_args.emplace_back(vt_input_config);
custom_args.emplace_back(vt_lb_name);
custom_args.emplace_back(nullptr);

int custom_argc = custom_args.size() - 1;
char** custom_argv = custom_args.data();

EXPECT_EQ(custom_argc, 5);

arguments::AppConfig appConfig{};
appConfig.vt_lb_name = "GreedyLB";

int this_rank;
MPI_Comm_rank(comm, &this_rank);
if (this_rank == 0) {
std::ofstream cfg_file_{"test_cfg.toml", std::ofstream::out | std::ofstream::trunc};
cfg_file_ << "vt_lb_name = RandomLB\n";
cfg_file_.close();
}
MPI_Barrier(comm);

vt::initialize(custom_argc, custom_argv, no_workers, true, &comm, &appConfig);

EXPECT_EQ(theConfig()->prog_name, "vt_program");
EXPECT_EQ(theConfig()->vt_no_terminate, true);
EXPECT_EQ(theConfig()->vt_lb_name, "RotateLB");

EXPECT_EQ(custom_argc, 2);
EXPECT_STREQ(custom_argv[0], "vt_program");
EXPECT_STREQ(custom_argv[1], "--cli_argument=100");
EXPECT_EQ(custom_argv[2], nullptr);
}

}}} // end namespace vt::tests::unit

0 comments on commit e151dd3

Please sign in to comment.