Commit 71abbc3e authored by frey_m's avatar frey_m
Browse files

user defined variables for optimizer and sampler

parent 9182dcb2
......@@ -113,7 +113,8 @@ public:
const Expressions::Named_t &obj,
const Expressions::Named_t &cons,
std::vector<double> hypervolRef = {},
bool isOptimizerRun = true)
bool isOptimizerRun = true,
const std::map<std::string, std::string> &userVariables = {})
: Poller(comm->mpiComm())
, comm_(comm)
, cmd_args_(args)
......@@ -123,7 +124,7 @@ public:
, hypervolRef_(hypervolRef)
{
if (isOptimizerRun)
setup(known_expr_funcs);
setup(known_expr_funcs, userVariables);
}
virtual ~Pilot()
......@@ -185,7 +186,8 @@ protected:
boost::scoped_ptr<Trace> job_trace_;
private:
void setup(functionDictionary_t known_expr_funcs) {
void setup(functionDictionary_t known_expr_funcs,
const std::map<std::string, std::string> &userVariables) {
global_rank_ = comm_->globalRank();
if(global_rank_ == 0) {
......@@ -210,7 +212,7 @@ private:
// here the control flow starts to diverge
if ( comm_->isOptimizer() ) { startOptimizer(); }
else if ( comm_->isWorker() ) { startWorker(); }
else if ( comm_->isWorker() ) { startWorker(userVariables); }
else if ( comm_->isPilot() ) { startPilot(); }
}
......@@ -267,7 +269,7 @@ protected:
}
virtual
void startWorker() {
void startWorker(const std::map<std::string, std::string> &userVariables) {
std::ostringstream os;
os << "\033[01;35m" << " " << global_rank_ << " (PID: " << getpid() << ") ▶ Worker"
......@@ -283,7 +285,7 @@ protected:
boost::scoped_ptr< Worker<Sim_t> > w(
new Worker<Sim_t>(objectives_, constraints_, simName,
comm_->getBundle(), cmd_args_));
comm_->getBundle(), cmd_args_, userVariables));
std::cout << "Stop Worker.." << std::endl;
}
......
......@@ -51,9 +51,11 @@ public:
std::string simName,
Comm::Bundle_t comms,
CmdArguments_t args,
const std::map<std::string, std::string> &userVariables,
bool isOptimizer = true)
: Poller(comms.worker)
, cmd_args_(args)
, userVariables_(userVariables)
{
objectives_ = objectives;
constraints_ = constraints;
......@@ -121,8 +123,8 @@ protected:
try {
SimPtr_t sim(new Sim_t(objectives_, constraints_,
params, simulation_name_, coworker_comm_,
cmd_args_));
params, simulation_name_, coworker_comm_,
cmd_args_, userVariables_));
sim->run();
} catch(OptPilotException &ex) {
......@@ -151,6 +153,8 @@ protected:
std::string simulation_name_;
CmdArguments_t cmd_args_;
const std::map<std::string, std::string> userVariables_;
/// notify coworkers of incoming broadcast
void notifyCoWorkers(int tag) {
......@@ -203,7 +207,8 @@ protected:
reqVarContainer_t requested_results;
try {
SimPtr_t sim(new Sim_t(objectives_, constraints_,
params, simulation_name_, coworker_comm_, cmd_args_));
params, simulation_name_, coworker_comm_, cmd_args_,
userVariables_));
// run simulation in a "blocking" fashion
sim->run();
......
......@@ -746,6 +746,17 @@ void OpalData::update() {
}
}
std::map<std::string, std::string> OpalData::getVariableData() {
std::map<std::string, std::string> udata;
std::vector<std::string> uvars = this->getVariableNames();
for (auto& uvar : uvars) {
RealVariable &variable = *dynamic_cast<RealVariable *>(OpalData::getInstance()->find(uvar));
udata.insert({uvar, variable.value().getBase().getImage()});
}
return udata;
}
std::vector<std::string> OpalData::getVariableNames() {
std::vector<std::string> result;
......@@ -777,8 +788,8 @@ std::vector<std::string> OpalData::getAllNames() {
// DTA
std::cout << "\nUser-defined variables:\n";
std::vector<std::string> userVars = this->getVariableNames();
for (auto& uvar : userVars) {
std::vector<std::string> uvars = this->getVariableNames();
for (auto& uvar : uvars) {
std::cout << uvar;
RealVariable &variable = *dynamic_cast<RealVariable *>(OpalData::getInstance()->find(uvar));
std::cout << "\te= " << variable.value().getBase().getImage();
......
......@@ -151,7 +151,7 @@ public:
virtual void operator()(Object *) const;
};
std::map<std::string, std::string> getVariableData();
std::vector<std::string> getVariableNames();
std::vector<std::string> getAllNames();
......
......@@ -40,7 +40,8 @@
OpalSimulation::OpalSimulation(Expressions::Named_t objectives,
Expressions::Named_t constraints,
Param_t params, std::string name,
MPI_Comm comm, CmdArguments_t args)
MPI_Comm comm, CmdArguments_t args,
std::map<std::string, std::string> uvars)
: Simulation(args)
, objectives_(objectives)
, constraints_(constraints)
......@@ -130,7 +131,11 @@ OpalSimulation::OpalSimulation(Expressions::Named_t objectives,
throw OptPilotException("OpalSimulation::OpalSimulation",
"The template file '" + tmplFile + "' doesn't exit");
gs_.reset(new GenerateOpalSimulation(tmplFile, dataFile, userVariables_));
for (const auto& uvar : userVariables_) {
uvars.insert({uvar.first, uvar.second});
}
gs_.reset(new GenerateOpalSimulation(tmplFile, dataFile, uvars));
}
......
......@@ -40,11 +40,13 @@ public:
* @param[in] name of the simulation
* @param[in] comm MPI communicator used for running the simulation
* @param[in] args command line arguments passed to the framework
* @param[in] uvars defined in the input file (e.g. REAL a = 1.0)
*/
OpalSimulation(Expressions::Named_t objectives,
Expressions::Named_t constraints,
Param_t params, std::string name, MPI_Comm comm,
CmdArguments_t args);
CmdArguments_t args,
std::map<std::string, std::string> uvars);
virtual ~OpalSimulation();
......
......@@ -438,6 +438,8 @@ void SampleCmd::execute() {
}
*gmsg << endl;
std::map<std::string, std::string> userVariables = opal->getVariableData();
Inform *origGmsg = gmsg;
gmsg = 0;
try {
......@@ -457,7 +459,7 @@ void SampleCmd::execute() {
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm, funcs, dvars,
objectives, sampleMethods,
storeobjstr, filesToKeep));
storeobjstr, filesToKeep, userVariables));
if (comm->isWorker())
popEnvironment();
......
......@@ -46,7 +46,8 @@ public:
std::shared_ptr<SamplingMethod>
>& sampleMethods,
const std::vector<std::string> &storeobjstr,
const std::vector<std::string> &filesToKeep)
const std::vector<std::string> &filesToKeep,
const std::map<std::string, std::string> &userVariables)
: Pilot<Opt_t,
Sim_t,
SolPropagationGraph_t,
......@@ -57,7 +58,8 @@ public:
obj,
Expressions::Named_t(),
{},
false)
false,
{})
, sampleMethods_m(sampleMethods)
{
if (obj.size() == 0) {
......@@ -67,7 +69,7 @@ public:
};
}
this->setup(known_expr_funcs, storeobjstr, filesToKeep);
this->setup(known_expr_funcs, storeobjstr, filesToKeep, userVariables);
}
virtual ~SamplePilot()
......@@ -86,7 +88,8 @@ protected:
virtual
void setup(functionDictionary_t known_expr_funcs,
const std::vector<std::string> &storeobjstr,
const std::vector<std::string> &filesToKeep)
const std::vector<std::string> &filesToKeep,
const std::map<std::string, std::string> &userVariables)
{
this->global_rank_ = this->comm_->globalRank();
......@@ -96,7 +99,7 @@ protected:
// here the control flow starts to diverge
if ( this->comm_->isOptimizer() ) { startSampler(); }
else if ( this->comm_->isWorker() ) { startWorker(storeobjstr, filesToKeep); }
else if ( this->comm_->isWorker() ) { startWorker(storeobjstr, filesToKeep, userVariables); }
else if ( this->comm_->isPilot() ) { this->startPilot(); }
}
......@@ -118,7 +121,8 @@ protected:
using Pilot<Opt_t, Sim_t, SolPropagationGraph_t, Comm_t>::startWorker;
void startWorker(const std::vector<std::string> &storeobjstr,
const std::vector<std::string> &filesToKeep)
const std::vector<std::string> &filesToKeep,
const std::map<std::string, std::string> &userVariables)
{
std::ostringstream os;
os << "\033[01;35m" << " " << this->global_rank_ << " (PID: " << getpid() << ") ▶ Worker"
......@@ -135,7 +139,7 @@ protected:
boost::scoped_ptr< SampleWorker<Sim_t> > w(
new SampleWorker<Sim_t>(this->objectives_, this->constraints_, simName,
this->comm_->getBundle(), this->cmd_args_,
storeobjstr, filesToKeep));
storeobjstr, filesToKeep, userVariables));
std::cout << "Stop Worker.." << std::endl;
}
......
......@@ -27,8 +27,9 @@ public:
Comm::Bundle_t comms,
CmdArguments_t args,
const std::vector<std::string> &storeobjstr,
const std::vector<std::string> &filesToKeep)
: Worker<Sim_t>(objectives, constraints, simName, comms, args, false)
const std::vector<std::string> &filesToKeep,
const std::map<std::string, std::string> &userVariables)
: Worker<Sim_t>(objectives, constraints, simName, comms, args, userVariables, false)
, statVariablesToStore_m(storeobjstr)
, filesToKeep_m(filesToKeep)
{
......@@ -94,9 +95,10 @@ protected:
MPI_Bcast_params(params, this->leader_pid_, this->coworker_comm_);
try {
typename Worker<Sim_t>::SimPtr_t sim(new Sim_t(this->objectives_, this->constraints_,
params, this->simulation_name_, this->coworker_comm_,
this->cmd_args_));
typename Worker<Sim_t>::SimPtr_t sim(
new Sim_t(this->objectives_, this->constraints_,
params, this->simulation_name_, this->coworker_comm_,
this->cmd_args_, this->userVariables_));
sim->setFilename(job_id);
......@@ -138,11 +140,12 @@ protected:
reqVarContainer_t requested_results;
try {
typename Worker<Sim_t>::SimPtr_t sim(new Sim_t(this->objectives_,
this->constraints_,
params,
this->simulation_name_,
this->coworker_comm_,
this->cmd_args_));
this->constraints_,
params,
this->simulation_name_,
this->coworker_comm_,
this->cmd_args_,
this->userVariables_));
sim->setFilename(job_id);
......
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