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

command-line arguments

parent 7daa6ce2
...@@ -51,7 +51,7 @@ else() ...@@ -51,7 +51,7 @@ else()
endif() endif()
unset(_AMDIS_LIB CACHE) 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) if(Boost_FOUND)
list(APPEND AMDIS_LIBRARIES ${Boost_LIBRARIES}) list(APPEND AMDIS_LIBRARIES ${Boost_LIBRARIES})
list(APPEND AMDIS_LIBRARY_DIRS ${Boost_LIBRARY_DIRS}) list(APPEND AMDIS_LIBRARY_DIRS ${Boost_LIBRARY_DIRS})
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#ifdef HAVE_ZOLTAN #ifdef HAVE_ZOLTAN
#include <zoltan_cpp.h> #include <zoltan_cpp.h>
#endif #endif
#include "boost/program_options.hpp"
namespace AMDiS { namespace AMDiS {
...@@ -43,16 +44,58 @@ namespace AMDiS { ...@@ -43,16 +44,58 @@ namespace AMDiS {
#endif #endif
Parameters::clearData(); 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 == "") { if (initFileName == "") {
TEST_EXIT(argc >= 2)("No init file!\n"); if (vm.count("init-file"))
Parameters::init(string(argv[1])); Parameters::init(vm["init-file"].as<std::string>());
else
throw(std::runtime_error("No init file specified!"));
} else { } else {
Parameters::init(initFileName); 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 { ...@@ -52,7 +52,7 @@ namespace AMDiS {
if (w) { if (w) {
char line[10]; char line[10];
MSG("wait for <enter> ..."); MSG("wait for <enter> ...");
fgets(line, 9, stdin); char* result = fgets(line, 9, stdin);
} }
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include<boost/tokenizer.hpp>
using namespace std; using namespace std;
namespace AMDiS { namespace AMDiS {
...@@ -42,9 +43,6 @@ namespace AMDiS { ...@@ -42,9 +43,6 @@ namespace AMDiS {
fn_include_list.clear(); fn_include_list.clear();
singlett->read(in); singlett->read(in);
singlett->getInternalParameters(); singlett->getInternalParameters();
// initialize global strcutures using parameters
Global::init();
} }
...@@ -139,7 +137,7 @@ namespace AMDiS { ...@@ -139,7 +137,7 @@ namespace AMDiS {
size_t posVar = inputSwap.find_first_of('$'); size_t posVar = inputSwap.find_first_of('$');
while (posVar != string::npos) { while (posVar != string::npos) {
size_t posVarBegin, posVarEnd; size_t posVarBegin, posVarEnd;
if(inputSwap[posVar+1] == '{') { // ${var_name} if (inputSwap[posVar+1] == '{') { // ${var_name}
posVarEnd = inputSwap.find_first_of('}',posVar + 2); posVarEnd = inputSwap.find_first_of('}',posVar + 2);
posVarBegin = posVar + 1; posVarBegin = posVar + 1;
} else if (inputSwap[posVar+1] != '(' && inputSwap[posVar+1] != '[') { } else if (inputSwap[posVar+1] != '(' && inputSwap[posVar+1] != '[') {
...@@ -168,6 +166,7 @@ namespace AMDiS { ...@@ -168,6 +166,7 @@ namespace AMDiS {
return inputSwap; return inputSwap;
} }
std::string Initfile::variableEvaluation(const std::string& input) const std::string Initfile::variableEvaluation(const std::string& input) const
{ {
...@@ -181,8 +180,8 @@ namespace AMDiS { ...@@ -181,8 +180,8 @@ namespace AMDiS {
std::string varName = inputSwap.substr(posVarBegin + 1 , posVarEnd - posVarBegin - 1); std::string varName = inputSwap.substr(posVarBegin + 1 , posVarEnd - posVarBegin - 1);
double value = 0.0; double value = 0.0;
InitfileInternal::convert(varName, value); // string -> double (using muparser) details::convert(varName, value); // string -> double (using muparser)
InitfileInternal::convert(value, varName); // double -> string details::convert(value, varName); // double -> string
std::string replaceName = inputSwap.substr(posVar , posVarEnd - posVar + (posVarBegin - posVar)); std::string replaceName = inputSwap.substr(posVar , posVarEnd - posVar + (posVarBegin - posVar));
inputSwap.replace(inputSwap.find(replaceName), replaceName.length(), varName); inputSwap.replace(inputSwap.find(replaceName), replaceName.length(), varName);
...@@ -193,37 +192,29 @@ namespace AMDiS { ...@@ -193,37 +192,29 @@ namespace AMDiS {
return inputSwap; return inputSwap;
} }
void Initfile::readArgv(int argc, char **argv) void Initfile::readArgv(std::string parameters, int debugInfo)
{ {
initIntern(); initIntern();
for (int i = 0; i < argc; i++) {
if (strcmp("-rs", argv[i]) == 0) { char seperator = ';';
std::string input(argv[i + 1]); typedef boost::escaped_list_separator<char> TokenizerFunc;
add("argv->rs", input, 0); typedef boost::tokenizer<TokenizerFunc> Tokenizer;
} TokenizerFunc tokenizerFunc('\\', seperator, '\"');
else if (strcmp("-parameters", argv[i]) == 0) { Tokenizer tok(parameters, tokenizerFunc);
std::string input(argv[i + 1]);
int found = input.find_first_of(';'); // split parameterstring by seperator ";"
std::vector<std::string> val;
std::vector<std::string> parameters; for (Tokenizer::iterator cell = tok.begin(); cell != tok.end(); ++cell) {
while (found != static_cast<int>(std::string::npos)) { val.push_back(trim(*cell));
if (found > 2) { }
parameters.push_back(input.substr(0, found).c_str());
} // split each parameter by ":"
input = input.substr(found + 1); for (size_t i = 0; i < val.size(); i++) {
found = input.find_first_of(';'); int found = val[i].find_first_of(':');
} if (found != static_cast<int>(std::string::npos)) {
if (input.length() > 2) std::string value = trim(val[i].substr(found+1));
parameters.push_back(input.c_str()); std::string key = trim(val[i].substr(0, found));
set(key, value, debugInfo);
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);
}
}
} }
} }
} }
......
...@@ -34,56 +34,39 @@ ...@@ -34,56 +34,39 @@
namespace AMDiS { namespace AMDiS {
namespace InitfileInternal { namespace details {
/// Exceptions /// Exceptions
///_________________________________________________________________________________________
struct WrongVectorSize : std::runtime_error { struct WrongVectorSize : std::runtime_error {
WrongVectorSize(std::string m) WrongVectorSize(std::string m) : std::runtime_error(m) {}
: std::runtime_error(m)
{}
}; };
struct NoDelim : std::runtime_error { struct NoDelim : std::runtime_error {
NoDelim(std::string m) NoDelim(std::string m) : std::runtime_error(m) {}
: std::runtime_error(m)
{}
}; };
struct WrongVectorFormat : std::runtime_error { struct WrongVectorFormat : std::runtime_error {
WrongVectorFormat(std::string m) WrongVectorFormat(std::string m) : std::runtime_error(m) {}
: std::runtime_error(m)
{}
}; };
struct GetTagError : std::runtime_error { struct GetTagError : std::runtime_error {
GetTagError(std::string m) GetTagError(std::string m) : std::runtime_error(m) {}
: std::runtime_error(m)
{}
}; };
template<typename T> template<typename T>
struct WrongValueFormat : std::runtime_error { struct WrongValueFormat : std::runtime_error {
static std::string name(int)
{ static std::string name(bool) { return "bool"; }
return "int"; 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(bool) static std::string name(unsigned int) { return "unsigned int"; }
{
return "bool";
}
static std::string name(double)
{
return "double";
}
static std::string name(unsigned int)
{
return "unsigned int";
}
template<typename G> template<typename G>
static std::string name(G) static std::string name(G)
...@@ -92,32 +75,19 @@ namespace AMDiS { ...@@ -92,32 +75,19 @@ namespace AMDiS {
} }
WrongValueFormat(std::string value) WrongValueFormat(std::string value)
: std::runtime_error(std::string("cannot convert '") + : std::runtime_error("cannot convert '" + value + "' into <" + name(T()) + ">")
value + std::string("' into <") + name(T()) + ">")
{} {}
}; };
template<typename T> template<typename T>
struct BadArithmeticExpression : std::runtime_error { struct BadArithmeticExpression : std::runtime_error {
static std::string name(int)
{
return "int";
}
static std::string name(bool)
{
return "bool";
}
static std::string name(double) static std::string name(bool) { return "bool"; }
{ static std::string name(double) { return "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"; }
static std::string name(unsigned int)
{
return "unsigned int";
}
template<typename G> template<typename G>
static std::string name(G) static std::string name(G)
...@@ -126,19 +96,19 @@ namespace AMDiS { ...@@ -126,19 +96,19 @@ namespace AMDiS {
} }
BadArithmeticExpression(std::string m, std::string value) BadArithmeticExpression(std::string m, std::string value)
: std::runtime_error(std::string("cannot evaluate expression '") + : std::runtime_error("cannot evaluate expression '" + value + "' into <" + name(T()) + ">\n"
value + std::string("' into <") + name(T()) + "Parser message: '" + m + "'")
">\nParser message: '" + m + "'")
{} {}
}; };
///_________________________________________________________________________________________
/// return the delimiter or throw an exception if there is no known /// return the delimiter or throw an exception if there is no known
/// delimiter in value /// delimiter in value
inline size_t checkDelim(const std::string& value, const std::string& delims) inline size_t checkDelim(const std::string& value, const std::string& delims)
{ {
size_t pos(std::string::npos); 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]); pos = value.find(delims[i]);
if (pos != std::string::npos) if (pos != std::string::npos)
return i; return i;
...@@ -258,7 +228,7 @@ namespace AMDiS { ...@@ -258,7 +228,7 @@ namespace AMDiS {
else if (swapStr == "L2_NORM") else if (swapStr == "L2_NORM")
value = static_cast< Norm >(L2_NORM); value = static_cast< Norm >(L2_NORM);
else { else {
int swap= 0; int swap = 0;
convert(valStr, swap); convert(valStr, swap);
value = static_cast< Norm >(swap); value = static_cast< Norm >(swap);
} }
...@@ -274,7 +244,7 @@ namespace AMDiS { ...@@ -274,7 +244,7 @@ namespace AMDiS {
if (static_cast<int>(temp_vec.size()) != c.getSize()) if (static_cast<int>(temp_vec.size()) != c.getSize())
throw WrongVectorSize("wrong number of entries for WorldVector"); 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]; c[i] = temp_vec[i];
} }
...@@ -316,8 +286,9 @@ namespace AMDiS { ...@@ -316,8 +286,9 @@ namespace AMDiS {
temp_vec[i] = c[i]; temp_vec[i] = c[i];
convert(temp_vec, valStr); convert(temp_vec, valStr);
} }
} // end namespace InitfileInternal } // end namespace details
///_________________________________________________________________________________________
/** The entry in an initfile. This helper class was constructed to allow calls /** 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 * like val = data.get(tag) for arbitrary types of val. At current stage, only
...@@ -372,6 +343,7 @@ namespace AMDiS { ...@@ -372,6 +343,7 @@ namespace AMDiS {
return o; return o;
} }
///_________________________________________________________________________________________
/** Basis data container as a map of tag on a value as strings. The container /** Basis data container as a map of tag on a value as strings. The container
* throws an exception, if the tag was not found. * throws an exception, if the tag was not found.
...@@ -424,7 +396,6 @@ namespace AMDiS { ...@@ -424,7 +396,6 @@ namespace AMDiS {
template<typename T> template<typename T>
static void get(const std::string tag, T& value, int debugInfo = -1) static void get(const std::string tag, T& value, int debugInfo = -1)
{ {
using namespace InitfileInternal;
initIntern(); initIntern();
if (debugInfo == -1) if (debugInfo == -1)
debugInfo = singlett->getMsgInfo(); debugInfo = singlett->getMsgInfo();
...@@ -438,14 +409,14 @@ namespace AMDiS { ...@@ -438,14 +409,14 @@ namespace AMDiS {
int error_code = singlett->checkedGet(tag, valStr); int error_code = singlett->checkedGet(tag, valStr);
if (error_code == 0) { if (error_code == 0) {
valStr = trim(valStr); valStr = trim(valStr);
convert(valStr, value); details::convert(valStr, value);
} else if(error_code == TAG_NOT_FOUND_BREAK) } else if(error_code == TAG_NOT_FOUND_BREAK)
throw TagNotFoundBreak("required tag '" + tag + "' not found"); throw TagNotFoundBreak("required tag '" + tag + "' not found");
else if (error_code == TAG_NOT_FOUND) { else if (error_code == TAG_NOT_FOUND) {
if (debugInfo == 2) if (debugInfo == 2)
std::cout << "there is no tag '" + tag + "'" << std::endl; std::cout << "there is no tag '" + tag + "'" << std::endl;
} else } 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) { if (debugInfo == 2) {
std::cout << "Parameter '" << tag << "'" std::cout << "Parameter '" << tag << "'"
...@@ -458,7 +429,6 @@ namespace AMDiS { ...@@ -458,7 +429,6 @@ namespace AMDiS {
/// return InitEntry object for tag tag /// return InitEntry object for tag tag
static InitEntry get(const std::string tag) static InitEntry get(const std::string tag)
{ {
using namespace InitfileInternal;
InitEntry result; InitEntry result;
std::string valStr; std::string valStr;
...@@ -467,11 +437,12 @@ namespace AMDiS { ...@@ -467,11 +437,12 @@ namespace AMDiS {
valStr = trim(valStr); valStr = trim(valStr);
result = InitEntry(valStr); result = InitEntry(valStr);
} else if(error_code == TAG_NOT_FOUND_BREAK) } 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) 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 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; return result;
} }
...@@ -481,13 +452,12 @@ namespace AMDiS { ...@@ -481,13 +452,12 @@ namespace AMDiS {
template<typename T> template<typename T>
static void set(const std::string tag, T& value, int debugInfo= -1) static void set(const std::string tag, T& value, int debugInfo= -1)
{ {
using namespace InitfileInternal;
initIntern(); initIntern();
if (debugInfo == -1) if (debugInfo == -1)
debugInfo = singlett->getMsgInfo(); debugInfo = singlett->getMsgInfo();
std::string swap = ""; std::string swap = "";
convert(value, swap); details::convert(value, swap);
(*singlett)[trim(tag)] = swap; (*singlett)[trim(tag)] = swap;
// update msg parameters msgInfo, msgWait, paramInfo // update msg parameters msgInfo, msgWait, paramInfo
singlett->getInternalParameters(); singlett->getInternalParameters();
...@@ -506,7 +476,7 @@ namespace AMDiS { ...@@ -506,7 +476,7 @@ namespace AMDiS {
/// rescheduling parameter /// rescheduling parameter
static void readArgv(int argc, char **argv); static void readArgv(std::string parameters, int debugInfo = 2);
/// Returns specified info level /// Returns specified info level
...@@ -568,7 +538,7 @@ protected: ...@@ -568,7 +538,7 @@ protected:
static void initIntern() static void initIntern()
{ {
if (singlett == NULL) 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