Skip to content
Configuration.cpp 1.29 KiB
Newer Older
#include "Configuration.hpp"

#include <iostream>
#include <stdexcept>

#include "MpiEnvironment.hpp"
#include <boost/format.hpp>
#include <boost/program_options.hpp>

auto Configuration::parseArgs(int argc, char* argv[], const MpiEnvironment& env)
    -> Configuration {
	namespace po = boost::program_options;
	Configuration cfg;
	po::options_description desc{"Allowed options"};
	desc.add_options()("help,h", "produce help message") //
	    ("gridrows,r", po::value<std::size_t>(&cfg.GridRows)->required(),
	     "number of rows in the grid") //
	    ("gridcols,c", po::value<std::size_t>(&cfg.GridColumns)->required(),
	     "number of columns in the grid") //
	    ("file,f", po::value<std::string>(&cfg.InputFilePath)->required(),
	     "path to wireworld file"); //

	po::variables_map vm;
	po::store(po::parse_command_line(argc, argv, desc), vm);

	if (vm.count("help") > 0 && env.isMaster()) { std::cout << desc << "\n"; }
	po::notify(vm);

	const auto& totalCellCount = cfg.GridColumns * cfg.GridRows;
	const auto& worldSize = env.worldSize();
	if (totalCellCount != worldSize) {
		throw std::invalid_argument(
		    boost::str(boost::format("Total number of cells (%d) does not "
		                             "match the MPI World size (%d)") %
		               totalCellCount % worldSize));
	}

	return cfg;
}