config file: support GlobalInitFile option

issue#70
Houtan Bastani 2012-06-06 16:07:59 +02:00
parent 35bf1d5ec2
commit dc2e5e9226
3 changed files with 310 additions and 200 deletions

View File

@ -30,6 +30,16 @@
using namespace std;
Hook::Hook(string &global_init_file_arg)
{
if (global_init_file_arg.empty())
{
cerr << "ERROR: The Hook must have a Global Initialization File argument." << endl;
exit(EXIT_FAILURE);
}
hooks["global_init_file"] = global_init_file_arg;
}
SlaveNode::SlaveNode(string &computerName_arg, string port_arg, int minCpuNbr_arg, int maxCpuNbr_arg, string &userName_arg,
string &password_arg, string &remoteDrive_arg, string &remoteDirectory_arg,
string &dynarePath_arg, string &matlabOctavePath_arg, bool singleCompThread_arg,
@ -74,56 +84,78 @@ ConfigFile::~ConfigFile()
}
void
ConfigFile::getConfigFileInfo(const string &parallel_config_file)
ConfigFile::getConfigFileInfo(const string &config_file)
{
using namespace boost;
if (!parallel && !parallel_test)
return;
ifstream *configFile;
if (parallel_config_file.empty())
if (config_file.empty())
{
string defaultConfigFile ("");
// Test OS and try to open default file
#if defined(_WIN32) || defined(__CYGWIN32__)
if (getenv("APPDATA") == NULL)
{
cerr << "ERROR: APPDATA environment variable not found." << endl;
if (parallel || parallel_test)
cerr << "ERROR: ";
else
cerr << "WARNING: ";
cerr << "APPDATA environment variable not found." << endl;
if (parallel || parallel_test)
exit(EXIT_FAILURE);
}
string defaultConfigFile(getenv("APPDATA"));
else
{
defaultConfigFile += getenv("APPDATA");
defaultConfigFile += "\\dynare.ini";
}
#else
if (getenv("HOME") == NULL)
{
cerr << "ERROR: HOME environment variable not found." << endl;
if (parallel || parallel_test)
cerr << "ERROR: ";
else
cerr << "WARNING: ";
cerr << "HOME environment variable not found." << endl;
if (parallel || parallel_test)
exit(EXIT_FAILURE);
}
string defaultConfigFile(getenv("HOME"));
else
{
defaultConfigFile += getenv("HOME");
defaultConfigFile += "/.dynare";
}
#endif
configFile = new ifstream(defaultConfigFile.c_str(), fstream::in);
if (!configFile->is_open())
if (parallel || parallel_test)
{
cerr << "ERROR: Could not open the default config file (" << defaultConfigFile << ")" << endl;
exit(EXIT_FAILURE);
}
else
return;
}
else
{
configFile = new ifstream(parallel_config_file.c_str(), fstream::in);
configFile = new ifstream(config_file.c_str(), fstream::in);
if (!configFile->is_open())
{
cerr << "ERROR: Couldn't open file " << parallel_config_file << endl;;
cerr << "ERROR: Couldn't open file " << config_file << endl;;
exit(EXIT_FAILURE);
}
}
string name, computerName, port, userName, password, remoteDrive,
remoteDirectory, dynarePath, matlabOctavePath, operatingSystem;
remoteDirectory, dynarePath, matlabOctavePath, operatingSystem,
global_init_file;
int minCpuNbr = 0, maxCpuNbr = 0;
bool singleCompThread = true;
member_nodes_t member_nodes;
bool inHooks = false;
bool inNode = false;
bool inCluster = false;
while (configFile->good())
@ -134,29 +166,43 @@ ConfigFile::getConfigFileInfo(const string &parallel_config_file)
if (line.empty())
continue;
if (!line.compare("[node]") || !line.compare("[cluster]"))
if (!line.compare("[node]") || !line.compare("[cluster]") || !line.compare("[hooks]"))
{
addConfFileElement(inNode, inCluster, member_nodes, name,
if (!global_init_file.empty())
// we were just in [hooks]
addHooksConfFileElement(global_init_file);
else
// we were just in [node] or [cluster]
addParallelConfFileElement(inNode, inCluster, member_nodes, name,
computerName, port, minCpuNbr, maxCpuNbr, userName,
password, remoteDrive, remoteDirectory,
dynarePath, matlabOctavePath, singleCompThread,
operatingSystem);
//! Reset communication vars / option defaults
if (!line.compare("[hooks]"))
{
inHooks = true;
inNode = false;
inCluster = false;
}
else
if (!line.compare("[node]"))
{
inHooks = false;
inNode = true;
inCluster = false;
}
else
{
inHooks = false;
inNode = false;
inCluster = true;
}
name = userName = computerName = port = password = remoteDrive
= remoteDirectory = dynarePath = matlabOctavePath
= operatingSystem = "";
= operatingSystem = global_init_file = "";
minCpuNbr = maxCpuNbr = 0;
singleCompThread = true;
member_nodes.clear();
@ -173,6 +219,12 @@ ConfigFile::getConfigFileInfo(const string &parallel_config_file)
trim(tokenizedLine.front());
trim(tokenizedLine.back());
if (inHooks)
{
if (!tokenizedLine.front().compare("GlobalInitFile"))
global_init_file = tokenizedLine.back();
}
else
if (!tokenizedLine.front().compare("Name"))
name = tokenizedLine.back();
else if (!tokenizedLine.front().compare("CPUnbr"))
@ -316,7 +368,10 @@ ConfigFile::getConfigFileInfo(const string &parallel_config_file)
}
}
addConfFileElement(inNode, inCluster, member_nodes, name,
if (!global_init_file.empty())
addHooksConfFileElement(global_init_file);
else
addParallelConfFileElement(inNode, inCluster, member_nodes, name,
computerName, port, minCpuNbr, maxCpuNbr, userName,
password, remoteDrive, remoteDirectory,
dynarePath, matlabOctavePath, singleCompThread,
@ -326,7 +381,19 @@ ConfigFile::getConfigFileInfo(const string &parallel_config_file)
}
void
ConfigFile::addConfFileElement(bool inNode, bool inCluster, member_nodes_t member_nodes,
ConfigFile::addHooksConfFileElement(string &global_init_file)
{
if (global_init_file.empty())
{
cerr << "ERROR: The global initialization file must be passed to the GlobalInitFile option." << endl;
exit(EXIT_FAILURE);
}
else
hooks.push_back(new Hook(global_init_file));
}
void
ConfigFile::addParallelConfFileElement(bool inNode, bool inCluster, member_nodes_t member_nodes,
string &name, string &computerName, string port, int minCpuNbr, int maxCpuNbr, string &userName,
string &password, string &remoteDrive, string &remoteDirectory,
string &dynarePath, string &matlabOctavePath, bool singleCompThread,
@ -375,6 +442,21 @@ ConfigFile::addConfFileElement(bool inNode, bool inCluster, member_nodes_t membe
void
ConfigFile::checkPass(WarningConsolidation &warnings) const
{
bool global_init_file_declared = false;
for (vector<Hook *>::const_iterator it = hooks.begin() ; it != hooks.end(); it++)
{
const map <string, string> hookmap = (*it)->get_hooks();
for (map <string, string>::const_iterator mapit = hookmap.begin() ; mapit != hookmap.end(); mapit++)
if (mapit->first.compare("global_init_file") == 0)
if (global_init_file_declared == true)
{
cerr << "ERROR: Only one global initialization file may be provided." << endl;
exit(EXIT_FAILURE);
}
else
global_init_file_declared = true;
}
if (!parallel && !parallel_test)
return;
@ -502,6 +584,17 @@ ConfigFile::transformPass()
it->second /= weight_denominator;
}
void
ConfigFile::writeHooks(ostream &output) const
{
for (vector<Hook *>::const_iterator it = hooks.begin() ; it != hooks.end(); it++)
{
map <string, string> hookmap = (*it)->get_hooks();
for (map <string, string>::const_iterator mapit = hookmap.begin() ; mapit != hookmap.end(); mapit++)
output << "options_." << mapit->first << " = '" << mapit->second << "';" << endl;
}
}
void
ConfigFile::writeCluster(ostream &output) const
{

View File

@ -29,6 +29,16 @@ using namespace std;
typedef map<string, double> member_nodes_t;
class Hook
{
public:
Hook(string &global_init_file_arg);
~Hook();
private:
map<string, string> hooks;
public:
inline map<string, string>get_hooks() { return hooks; };
};
class SlaveNode
{
@ -79,12 +89,16 @@ private:
const bool parallel_slave_open_mode;
const string cluster_name;
string firstClusterName;
//! Hooks
vector<Hook *> hooks;
//! Cluster Table
map<string, Cluster *> clusters;
//! Node Map
map<string, SlaveNode *> slave_nodes;
//! Add Hooks
void addHooksConfFileElement(string &global_init_file);
//! Add a SlaveNode or a Cluster object
void addConfFileElement(bool inNode, bool inCluster, member_nodes_t member_nodes, string &name,
void addParallelConfFileElement(bool inNode, bool inCluster, member_nodes_t member_nodes, string &name,
string &computerName, string port, int minCpuNbr, int maxCpuNbr, string &userName,
string &password, string &remoteDrive, string &remoteDirectory,
string &dynarePath, string &matlabOctavePath, bool singleCompThread,
@ -96,6 +110,8 @@ public:
void checkPass(WarningConsolidation &warnings) const;
//! Check Pass
void transformPass();
//! Write any hooks
void writeHooks(ostream &output) const;
//! Create options_.parallel structure, write options
void writeCluster(ostream &output) const;
//! Close slave nodes if needed

View File

@ -462,8 +462,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b
<< "M_.fname = '" << basename << "';" << endl
<< "%" << endl
<< "% Some global variables initialization" << endl
<< "%" << endl
<< "global_initialization;" << endl
<< "%" << endl;
config_file.writeHooks(mOutputFile);
mOutputFile << "global_initialization;" << endl
<< "diary off;" << endl;
if (!no_log)
mOutputFile << "logname_ = '" << basename << ".log';" << endl