Commit c774d111 authored by Praetorius, Simon's avatar Praetorius, Simon

command-line arguments

parent 7daa6ce2
......@@ -51,7 +51,7 @@ else()
endif()
unset(_AMDIS_LIB CACHE)
find_package(Boost 1.42 REQUIRED system iostreams filesystem)
find_package(Boost 1.42 REQUIRED system iostreams filesystem program_options)
if(Boost_FOUND)
list(APPEND AMDIS_LIBRARIES ${Boost_LIBRARIES})
list(APPEND AMDIS_LIBRARY_DIRS ${Boost_LIBRARY_DIRS})
......
......@@ -15,6 +15,7 @@
#ifdef HAVE_ZOLTAN
#include <zoltan_cpp.h>
#endif
#include "boost/program_options.hpp"
namespace AMDiS {
......@@ -43,16 +44,58 @@ namespace AMDiS {
#endif
Parameters::clearData();
Parameters::readArgv(argc, argv);
// read commandline arguments
namespace po = boost::program_options;
// Declare the supported options.
po::options_description desc("Usage: " + std::string(argv[0]) + " init-file [options]\nAllowed options");
desc.add_options()
("help", "produce help message")
("init-file", po::value<std::string>(), "set init file")
("rs", po::value<int>(), "set restart parameter")
("parameters", po::value<std::string>(), "set parameter in init file\nsyntax: \"key1: value1; key2: value2...\"");
// first argument is init-filename
po::positional_options_description p;
p.add("init-file", 1);
// parse comandline
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
po::notify(vm);
// print help message
if (vm.count("help")) {
cout << desc << "\n";
exit(1);
}
// add parameter for restart simulations
if (vm.count("rs"))
Parameters::add("argv->rs", vm["rs"].as<int>(), 0);
// set parameters before reading the initfile
if (vm.count("parameters"))
Parameters::readArgv(vm["parameters"].as<std::string>());
if (initFileName == "") {
TEST_EXIT(argc >= 2)("No init file!\n");
Parameters::init(string(argv[1]));
if (vm.count("init-file"))
Parameters::init(vm["init-file"].as<std::string>());
else
throw(std::runtime_error("No init file specified!"));
} else {
Parameters::init(initFileName);
}
Parameters::readArgv(argc, argv);
// reset parameters from command line
bool ignoreCommandline = false;
Parameters::get("ignore commandline options", ignoreCommandline);
if (vm.count("parameters") && !ignoreCommandline)
Parameters::readArgv(vm["parameters"].as<std::string>(),0);
// initialize global strcutures using parameters
Global::init();
}
......
......@@ -52,7 +52,7 @@ namespace AMDiS {
if (w) {
char line[10];
MSG("wait for <enter> ...");
fgets(line, 9, stdin);
char* result = fgets(line, 9, stdin);
}
}
......
......@@ -4,6 +4,7 @@
#include <stdexcept>
#include <iostream>
#include <sstream>
#include<boost/tokenizer.hpp>
using namespace std;
namespace AMDiS {
......@@ -42,9 +43,6 @@ namespace AMDiS {
fn_include_list.clear();
singlett->read(in);
singlett->getInternalParameters();
// initialize global strcutures using parameters
Global::init();
}
......@@ -139,7 +137,7 @@ namespace AMDiS {
size_t posVar = inputSwap.find_first_of('$');
while (posVar != string::npos) {
size_t posVarBegin, posVarEnd;
if(inputSwap[posVar+1] == '{') { // ${var_name}
if (inputSwap[posVar+1] == '{') { // ${var_name}
posVarEnd = inputSwap.find_first_of('}',posVar + 2);
posVarBegin = posVar + 1;
} else if (inputSwap[posVar+1] != '(' && inputSwap[posVar+1] != '[') {
......@@ -168,6 +166,7 @@ namespace AMDiS {
return inputSwap;
}
std::string Initfile::variableEvaluation(const std::string& input) const
{
......@@ -181,8 +180,8 @@ namespace AMDiS {
std::string varName = inputSwap.substr(posVarBegin + 1 , posVarEnd - posVarBegin - 1);
double value = 0.0;
InitfileInternal::convert(varName, value); // string -> double (using muparser)
InitfileInternal::convert(value, varName); // double -> string
details::convert(varName, value); // string -> double (using muparser)
details::convert(value, varName); // double -> string
std::string replaceName = inputSwap.substr(posVar , posVarEnd - posVar + (posVarBegin - posVar));
inputSwap.replace(inputSwap.find(replaceName), replaceName.length(), varName);
......@@ -193,37 +192,29 @@ namespace AMDiS {
return inputSwap;
}
void Initfile::readArgv(int argc, char **argv)
void Initfile::readArgv(std::string parameters, int debugInfo)
{
initIntern();
for (int i = 0; i < argc; i++) {
if (strcmp("-rs", argv[i]) == 0) {
std::string input(argv[i + 1]);
add("argv->rs", input, 0);
}
else if (strcmp("-parameters", argv[i]) == 0) {
std::string input(argv[i + 1]);
int found = input.find_first_of(';');
std::vector<std::string> parameters;
while (found != static_cast<int>(std::string::npos)) {
if (found > 2) {
parameters.push_back(input.substr(0, found).c_str());
}
input = input.substr(found + 1);
found = input.find_first_of(';');
}
if (input.length() > 2)
parameters.push_back(input.c_str());
for (size_t i = 0; i < parameters.size(); i++) {
int found = input.find_first_of(':');
if (found != static_cast<int>(std::string::npos)) {
std::string value = input.substr(found+1);
set(input.substr(0, found), value, 0);
}
}
char seperator = ';';
typedef boost::escaped_list_separator<char> TokenizerFunc;
typedef boost::tokenizer<TokenizerFunc> Tokenizer;
TokenizerFunc tokenizerFunc('\\', seperator, '\"');
Tokenizer tok(parameters, tokenizerFunc);
// split parameterstring by seperator ";"
std::vector<std::string> val;
for (Tokenizer::iterator cell = tok.begin(); cell != tok.end(); ++cell) {
val.push_back(trim(*cell));
}
// split each parameter by ":"
for (size_t i = 0; i < val.size(); i++) {
int found = val[i].find_first_of(':');
if (found != static_cast<int>(std::string::npos)) {
std::string value = trim(val[i].substr(found+1));
std::string key = trim(val[i].substr(0, found));
set(key, value, debugInfo);
}
}
}
......
......@@ -34,56 +34,39 @@
namespace AMDiS {
namespace InitfileInternal {
namespace details {
/// Exceptions
///_________________________________________________________________________________________
struct WrongVectorSize : std::runtime_error {
WrongVectorSize(std::string m)
: std::runtime_error(m)
{}
WrongVectorSize(std::string m) : std::runtime_error(m) {}
};
struct NoDelim : std::runtime_error {
NoDelim(std::string m)
: std::runtime_error(m)
{}
NoDelim(std::string m) : std::runtime_error(m) {}
};
struct WrongVectorFormat : std::runtime_error {
WrongVectorFormat(std::string m)
: std::runtime_error(m)
{}
WrongVectorFormat(std::string m) : std::runtime_error(m) {}
};
struct GetTagError : std::runtime_error {
GetTagError(std::string m)
: std::runtime_error(m)
{}
GetTagError(std::string m) : std::runtime_error(m) {}
};
template<typename T>
struct WrongValueFormat : std::runtime_error {
static std::string name(int)
{
return "int";
}
static std::string name(bool)
{
return "bool";
}
static std::string name(double)
{
return "double";
}
static std::string name(unsigned int)
{
return "unsigned int";
}
static std::string name(bool) { return "bool"; }
static std::string name(double) { return "double"; }
static std::string name(float) { return "float"; }
static std::string name(int) { return "int"; }
static std::string name(unsigned int) { return "unsigned int"; }
template<typename G>
static std::string name(G)
......@@ -92,32 +75,19 @@ namespace AMDiS {
}
WrongValueFormat(std::string value)
: std::runtime_error(std::string("cannot convert '") +
value + std::string("' into <") + name(T()) + ">")
: std::runtime_error("cannot convert '" + value + "' into <" + name(T()) + ">")
{}
};
template<typename T>
struct BadArithmeticExpression : std::runtime_error {
static std::string name(int)
{
return "int";
}
static std::string name(bool)
{
return "bool";
}
static std::string name(double)
{
return "double";
}
static std::string name(unsigned int)
{
return "unsigned int";
}
static std::string name(bool) { return "bool"; }
static std::string name(double) { return "double"; }
static std::string name(float) { return "float"; }
static std::string name(int) { return "int"; }
static std::string name(unsigned int) { return "unsigned int"; }
template<typename G>
static std::string name(G)
......@@ -126,19 +96,19 @@ namespace AMDiS {
}
BadArithmeticExpression(std::string m, std::string value)
: std::runtime_error(std::string("cannot evaluate expression '") +
value + std::string("' into <") + name(T()) +
">\nParser message: '" + m + "'")
: std::runtime_error("cannot evaluate expression '" + value + "' into <" + name(T()) + ">\n"
"Parser message: '" + m + "'")
{}
};
///_________________________________________________________________________________________
/// return the delimiter or throw an exception if there is no known
/// delimiter in value
inline size_t checkDelim(const std::string& value, const std::string& delims)
{
size_t pos(std::string::npos);
for (unsigned i = 0; i < delims.length(); i++) {
for (size_t i = 0; i < delims.length(); i++) {
pos = value.find(delims[i]);
if (pos != std::string::npos)
return i;
......@@ -258,7 +228,7 @@ namespace AMDiS {
else if (swapStr == "L2_NORM")
value = static_cast< Norm >(L2_NORM);
else {
int swap= 0;
int swap = 0;
convert(valStr, swap);
value = static_cast< Norm >(swap);
}
......@@ -274,7 +244,7 @@ namespace AMDiS {
if (static_cast<int>(temp_vec.size()) != c.getSize())
throw WrongVectorSize("wrong number of entries for WorldVector");
for (unsigned i = 0; i < temp_vec.size(); i++)
for (size_t i = 0; i < temp_vec.size(); i++)
c[i] = temp_vec[i];
}
......@@ -316,8 +286,9 @@ namespace AMDiS {
temp_vec[i] = c[i];
convert(temp_vec, valStr);
}
} // end namespace InitfileInternal
} // end namespace details
///_________________________________________________________________________________________
/** The entry in an initfile. This helper class was constructed to allow calls
* like val = data.get(tag) for arbitrary types of val. At current stage, only
......@@ -372,6 +343,7 @@ namespace AMDiS {
return o;
}
///_________________________________________________________________________________________
/** Basis data container as a map of tag on a value as strings. The container
* throws an exception, if the tag was not found.
......@@ -424,7 +396,6 @@ namespace AMDiS {
template<typename T>
static void get(const std::string tag, T& value, int debugInfo = -1)
{
using namespace InitfileInternal;
initIntern();
if (debugInfo == -1)
debugInfo = singlett->getMsgInfo();
......@@ -438,14 +409,14 @@ namespace AMDiS {
int error_code = singlett->checkedGet(tag, valStr);
if (error_code == 0) {
valStr = trim(valStr);
convert(valStr, value);
details::convert(valStr, value);
} else if(error_code == TAG_NOT_FOUND_BREAK)
throw TagNotFoundBreak("required tag '" + tag + "' not found");
else if (error_code == TAG_NOT_FOUND) {
if (debugInfo == 2)
std::cout << "there is no tag '" + tag + "'" << std::endl;
} else
throw std::runtime_error("unknown error_code (" + boost::lexical_cast<std::string>(error_code) + ") in checkedGet(...) returned for tag '" + tag + "'");
throw std::runtime_error("unknown error_code (" + boost::lexical_cast<std::string>(error_code) + ") returned for tag '" + tag + "'");
if (debugInfo == 2) {
std::cout << "Parameter '" << tag << "'"
......@@ -458,7 +429,6 @@ namespace AMDiS {
/// return InitEntry object for tag tag
static InitEntry get(const std::string tag)
{
using namespace InitfileInternal;
InitEntry result;
std::string valStr;
......@@ -467,11 +437,12 @@ namespace AMDiS {
valStr = trim(valStr);
result = InitEntry(valStr);
} else if(error_code == TAG_NOT_FOUND_BREAK)
throw TagNotFoundBreak("required tag '" + tag + "' not found");
throw TagNotFoundBreak("get(): required tag '" + tag + "' not found");
else if (error_code == TAG_NOT_FOUND)
throw TagNotFound("there is no tag '" + tag + "'"); // exception must be thrown, because an empty object would be return otherwise
throw TagNotFound("get(): there is no tag '" + tag + "'");
// exception must be thrown, because an empty object would be return otherwise
else
throw std::runtime_error("unknown error_code in checkedGet(...) returned for tag '" + tag + "'");
throw std::runtime_error("get(): unknown error_code returned for tag '" + tag + "'");
return result;
}
......@@ -481,13 +452,12 @@ namespace AMDiS {
template<typename T>
static void set(const std::string tag, T& value, int debugInfo= -1)
{
using namespace InitfileInternal;
initIntern();
if (debugInfo == -1)
debugInfo = singlett->getMsgInfo();
std::string swap = "";
convert(value, swap);
details::convert(value, swap);
(*singlett)[trim(tag)] = swap;
// update msg parameters msgInfo, msgWait, paramInfo
singlett->getInternalParameters();
......@@ -506,7 +476,7 @@ namespace AMDiS {
/// rescheduling parameter
static void readArgv(int argc, char **argv);
static void readArgv(std::string parameters, int debugInfo = 2);
/// Returns specified info level
......@@ -568,7 +538,7 @@ protected:
static void initIntern()
{
if (singlett == NULL)
singlett = new Initfile;
singlett = new Initfile;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment