47#include "boost/smart_ptr.hpp"
99 ,
class SolPropagationGraph_t
124 setup(known_expr_funcs);
132 std::vector<double> hypervolRef = {},
133 bool isOptimizerRun =
true,
134 const std::map<std::string, std::string> &userVariables = {})
144 setup(known_expr_funcs, userVariables);
175 typedef MasterNode<
typename Opt_t::SolutionState_t,
197 typedef std::map<size_t, std::pair<Param_t, reqVarContainer_t> >
Jobs_t;
207 const std::map<std::string, std::string> &userVariables) {
211 std::cout <<
"\033[01;35m";
213 std::cout <<
" | | (_) | | | " <<
std::endl;
214 std::cout <<
" ___ _ __ | |_ ______ _ __ _| | ___ | |_ " <<
std::endl;
215 std::cout <<
" / _ \\| '_ \\| __|______| '_ \\| | |/ _ \\| __|" <<
std::endl;
216 std::cout <<
"| (_) | |_) | |_ | |_) | | | (_) | |_ " <<
std::endl;
217 std::cout <<
" \\___/| .__/ \\__| | .__/|_|_|\\___/ \\__|" <<
std::endl;
223 std::cout <<
"\e[0m";
227 MPI_Barrier(MPI_COMM_WORLD);
243 std::cout <<
"Could not find 'inputfile' in arguments.. Aborting."
250 "No objectives or dvars specified");
254 std::ostringstream os;
258 if (isOptimizationRun) {
262 os <<
" ✔ " <<
dvars_.size()
266 std::cout << os.str() << std::flush;
269 MPI_Barrier(MPI_COMM_WORLD);
275 std::ostringstream os;
276 os <<
"\033[01;35m" <<
" " <<
global_rank_ <<
" (PID: " << getpid() <<
") ▶ Opt"
278 std::cout << os.str() << std::flush;
280 boost::scoped_ptr<Opt_t> opt(
289 void startWorker(
const std::map<std::string, std::string> &userVariables) {
291 std::ostringstream os;
292 os <<
"\033[01;35m" <<
" " <<
global_rank_ <<
" (PID: " << getpid() <<
") ▶ Worker"
294 std::cout << os.str() << std::flush;
298 if(pos != std::string::npos)
300 pos = tmplfile.find(
".");
301 std::string simName = tmplfile.substr(0,pos);
303 boost::scoped_ptr< Worker<Sim_t> > w(
307 std::cout <<
"Stop Worker.." <<
std::endl;
313 std::ostringstream os;
314 os <<
"\033[01;35m" <<
" " <<
global_rank_ <<
" (PID: " << getpid() <<
") ▶ Pilot"
316 std::cout << os.str() << std::flush;
319 std::ostringstream trace_filename;
320 trace_filename <<
"pilot.trace." <<
comm_->getBundle().island_id;
323 boost::shared_ptr<TraceComponent>(
new FileSink(trace_filename.str())));
347 size_t upperbound_buffer_size =
348 sizeof(double) *
alpha * (1 + opt_size) * 1000
351 new MasterNode<
typename Opt_t::SolutionState_t,
352 SolPropagationGraph_t >(
354 comm_->getBundle().island_id));
361 std::cout <<
"Stop Pilot.." <<
std::endl;
381 bool all_worker_idle =
true;
401 MPI_Isend(&dummy, 1, MPI_INT, worker,
415 size_t jid = job->first;
417 Param_t job_params = job->second.first;
425 std::pair<Param_t, reqVarContainer_t> >(job->first, job->second));
429 std::ostringstream dump;
430 dump <<
"sent job with ID " << jid <<
" to worker " << worker
445 size_t job_id = recv_value;
448 MPI_Send(&dummy, 1, MPI_UNSIGNED_LONG, status.MPI_SOURCE,
457 std::ostringstream dump;
458 dump <<
"worker finished job with ID " << job_id <<
std::endl;
465 int opt_master_rank =
comm_->getLeader();
466 MPI_Send(&job_id, 1, MPI_UNSIGNED_LONG, opt_master_rank,
480 size_t job_id = recv_value;
481 int opt_master_rank =
comm_->getLeader();
489 std::pair<Param_t, reqVarContainer_t> job =
490 std::pair<Param_t, reqVarContainer_t>(job_params, reqVars);
492 std::pair<
size_t, std::pair<Param_t, reqVarContainer_t> >(
495 std::ostringstream dump;
496 dump <<
"new opt job with ID " << job_id <<
std::endl;
506 std::ostringstream dump;
507 dump <<
"starting solution exchange.. " << status.MPI_SOURCE <<
std::endl;
511 size_t buffer_size = recv_value;
512 int opt_master_rank = status.MPI_SOURCE;
514 char *buffer =
new char[buffer_size];
515 MPI_Recv(buffer, buffer_size, MPI_CHAR, opt_master_rank,
521 dump.str(std::string());
522 dump <<
"getting " << buffer_size <<
" bytes from OPT "
527 std::ostringstream states;
529 buffer_size = states.str().length();
532 dump.str(std::string());
533 dump <<
"collected solution states of other PILOTS: "
538 MPI_Send(&buffer_size, 1, MPI_UNSIGNED_LONG, opt_master_rank,
541 buffer =
new char[buffer_size];
542 memcpy(buffer, states.str().c_str(), buffer_size);
543 MPI_Send(buffer, buffer_size, MPI_CHAR, opt_master_rank,
547 dump.str(std::string());
548 dump <<
"sent set of new solutions to OPT" <<
std::endl;
566 std::string msg =
"(Pilot) Error: unexpected MPI_TAG: ";
567 msg += status.MPI_TAG;
583 if(! isOpt)
return true;
586 if(!
cmd_args_->getArg<
bool>(
"one-pilot-converge",
false,
false))
594 if(i == my_rank)
continue;
608 MPI_Request opt_request;
609 MPI_Request worker_request;
612 size_t recv_value_worker = 0;
613 size_t recv_value_opt = 0;
617 MPI_Irecv(&recv_value_opt, 1, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE,
619 MPI_Irecv(&recv_value_worker, 1, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE,
622 bool pending_opt_request =
true;
623 bool pending_worker_request =
true;
624 bool pending_pilot_request =
false;
626 MPI_Request pilot_request;
627 size_t recv_value_pilot = 0;
628 if(
cmd_args_->getArg<
bool>(
"one-pilot-converge",
false,
false)) {
629 MPI_Irecv(&recv_value_pilot, 1, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE,
631 pending_pilot_request =
true;
638 if(opt_request != MPI_REQUEST_NULL) {
639 MPI_Test(&opt_request, &flag, &status);
641 pending_opt_request =
false;
646 MPI_Irecv(&recv_value_opt, 1, MPI_UNSIGNED_LONG,
649 pending_opt_request =
true;
656 if(worker_request != MPI_REQUEST_NULL) {
657 MPI_Test(&worker_request, &flag, &status);
659 pending_worker_request =
false;
663 if(
onMessage(status, recv_value_worker)) {
664 MPI_Irecv(&recv_value_worker, 1,
665 MPI_UNSIGNED_LONG, MPI_ANY_SOURCE, MPI_ANY_TAG,
667 pending_worker_request =
true;
674 if(
cmd_args_->getArg<
bool>(
"one-pilot-converge",
false,
false)) {
675 if(pilot_request != MPI_REQUEST_NULL) {
676 MPI_Test(&pilot_request, &flag, &status);
678 pending_pilot_request =
false;
682 MPI_Irecv(&recv_value_pilot, 1,
683 MPI_UNSIGNED_LONG, MPI_ANY_SOURCE, MPI_ANY_TAG,
685 pending_pilot_request =
true;
694 if(pending_opt_request) MPI_Cancel( &opt_request );
695 if(pending_worker_request) MPI_Cancel( &worker_request );
696 if(pending_pilot_request) MPI_Cancel( &pilot_request );
Inform & endl(Inform &inf)
#define MPI_WORK_JOBID_TAG
unique id of the job
#define MPI_EXCHANGE_SOL_STATE_DATA_TAG
#define MPI_OPT_JOB_FINISHED_TAG
pilot tells optimizer that results are ready to collect
#define MPI_EXCHANGE_SOL_STATE_RES_SIZE_TAG
#define MPI_EXCHANGE_SOL_STATE_RES_TAG
@ WORKER_STATUSUPDATE_TAG
#define MPI_WORKER_FINISHED_ACK_TAG
pilot notifies worker that he is ready to collect the results
#define MPI_STOP_TAG
global stop tag to exit poll loop (
void MPI_Send_reqvars(reqVarContainer_t reqvars, size_t pid, MPI_Comm comm)
void MPI_Send_params(Param_t params, size_t pid, MPI_Comm comm)
void MPI_Recv_reqvars(reqVarContainer_t &reqvars, size_t pid, MPI_Comm comm)
void MPI_Recv_params(Param_t ¶ms, size_t pid, MPI_Comm comm)
std::map< std::string, DVar_t > DVarContainer_t
std::map< std::string, reqVarInfo_t > reqVarContainer_t
namedVariableCollection_t Param_t
boost::shared_ptr< CmdArguments > CmdArguments_t
std::map< std::string, client::function::type > functionDictionary_t
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
constexpr double alpha
The fine structure constant, no dimension.
constexpr double e
The value of.
std::string::iterator iterator
int total_available_workers_
boost::shared_ptr< Comm_t > comm_
MPI_Comm coworker_comm_
MPI communicator used for messages between all pilots.
void setup(functionDictionary_t known_expr_funcs, const std::map< std::string, std::string > &userVariables)
std::string input_file_
input file for simulation with embedded optimization problem
std::map< size_t, std::pair< Param_t, reqVarContainer_t > > Jobs_t
keep track of requests and running jobs
virtual void setupPoll()
executed before starting polling loop
std::vector< bool > is_worker_idle_
virtual void startPilot()
virtual void startWorker(const std::map< std::string, std::string > &userVariables)
virtual void startOptimizer()
bool stop(bool isOpt=true)
virtual void prePoll()
executed before checking for new request
Jobs_t::iterator JobIter_t
int my_rank_in_worker_comm_
Expressions::Named_t constraints_
constraints
virtual void onStop()
enable implementation to react to STOP tag
virtual void sendNewJobToWorker(int worker)
MasterNode< typename Opt_t::SolutionState_t, SolPropagationGraph_t > MasterNode_t
std::vector< double > hypervolRef_
hypervolume reference point
Pilot(CmdArguments_t args, boost::shared_ptr< Comm_t > comm, functionDictionary_t known_expr_funcs, const DVarContainer_t &dvar, const Expressions::Named_t &obj, const Expressions::Named_t &cons, std::vector< double > hypervolRef={}, bool isOptimizerRun=true, const std::map< std::string, std::string > &userVariables={})
MPI_Comm opt_comm_
MPI communicator used for messages to/from optimizer.
boost::scoped_ptr< Trace > job_trace_
Expressions::Named_t objectives_
objectives
Pilot(CmdArguments_t args, boost::shared_ptr< Comm_t > comm, functionDictionary_t known_expr_funcs)
Pilot(CmdArguments_t args, boost::shared_ptr< Comm_t > comm, const DVarContainer_t &dvar)
MPI_Comm worker_comm_
MPI communicator used for messages to/from worker.
virtual void postPoll()
executed after handling (if any) new request
DVarContainer_t dvars_
design variables
boost::scoped_ptr< MasterNode_t > master_node_
virtual bool onMessage(MPI_Status status, size_t recv_value)
void parseInputFile(functionDictionary_t, bool isOptimizationRun)
Poller(MPI_Comm comm, double delay=0.1)
MPI_Comm comm_m
communicator the poller listens to requests