Skip to content

Commit

Permalink
[cppRuntime] start simulation asynchronly and Send solver settings vi…
Browse files Browse the repository at this point in the history
…a zeromq
  • Loading branch information
niklwors authored and adrpo committed Dec 16, 2019
1 parent a96be8d commit bfa7423
Show file tree
Hide file tree
Showing 24 changed files with 652 additions and 158 deletions.
4 changes: 3 additions & 1 deletion OMCompiler/Compiler/Template/CodegenCpp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@ template simulationMainRunScript(SimCode simCode ,Text& extraFuncs,Text& extraFu

let libFolder =simulationLibDir(simulationCodeTarget(),simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)
let libPaths = makefileParams.libPaths |> path => path; separator=";"
let zermMQParams = if getConfigBool(USE_ZEROMQ_IN_SIM) then '-u true -p <%getConfigInt(ZEROMQ_PUB_PORT)%> -s <%getConfigInt(ZEROMQ_SUB_PORT)%> -i <%getConfigInt(ZEROMQ_SIM_ID)%>' else ''
let zermMQParams = if getConfigBool(USE_ZEROMQ_IN_SIM) then '-u true -p <%getConfigInt(ZEROMQ_PUB_PORT)%> -s <%getConfigInt(ZEROMQ_SUB_PORT)%> -v <%getConfigString(ZEROMQ_SERVER_ID)%> -c <%getConfigString(ZEROMQ_CLIENT_ID)%> -i <%getConfigString(ZEROMQ_JOB_ID)%>' else ''
match makefileParams.platform
case "linux32"
case "linux64" then
Expand All @@ -1545,7 +1545,9 @@ template simulationMainRunScript(SimCode simCode ,Text& extraFuncs,Text& extraFu
<%preRunCommandWindows%>
REM ::export PATH=<%libFolder%>:$PATH REPLACE C: with /C/
SET PATH=<%home%>/bin;<%libFolder%>;<%libPaths%>;%PATH%

"<%moLib%>/<%fileNamePrefixx%>.exe" <%execParameters%> <%zermMQParams%> <%outputParameter%>

>>
end match
end match
Expand Down
5 changes: 4 additions & 1 deletion OMCompiler/Compiler/Template/SimCodeTV.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3809,7 +3809,10 @@ package Flags
constant ConfigFlag USE_ZEROMQ_IN_SIM;
constant ConfigFlag ZEROMQ_PUB_PORT;
constant ConfigFlag ZEROMQ_SUB_PORT;
constant ConfigFlag ZEROMQ_SIM_ID;
constant ConfigFlag ZEROMQ_JOB_ID;
constant ConfigFlag ZEROMQ_SERVER_ID;
constant ConfigFlag ZEROMQ_CLIENT_ID;

function isSet
input DebugFlag inFlag;
output Boolean outValue;
Expand Down
17 changes: 11 additions & 6 deletions OMCompiler/Compiler/Util/Flags.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,7 @@ constant ConfigFlag NO_ASSC = CONFIG_FLAG(131, "noASSC",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Gettext.gettext("Disables analytical to structural singularity conversion."));


constant ConfigFlag FULL_ASSC = CONFIG_FLAG(132, "fullASSC",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Gettext.gettext("Enables full equation replacement for BLT transformation from the ASSC algorithm."));
Expand All @@ -1324,14 +1325,18 @@ constant ConfigFlag ZEROMQ_SUB_PORT = CONFIG_FLAG(135, "zeroMQSubPort",
NONE(), EXTERNAL(), INT_FLAG(3204), NONE(),
Gettext.gettext("Configures port number for simulation runtime to receive information via ZeroMQ"));

constant ConfigFlag ZEROMQ_SIM_ID = CONFIG_FLAG(136, "zeroMQSimID",
NONE(), EXTERNAL(), INT_FLAG(-1), NONE(),
Gettext.gettext("Configures the ID with which the simulation is labelled for zeroMQ communication."));

constant ConfigFlag FMI_VERSION = CONFIG_FLAG(137,
constant ConfigFlag ZEROMQ_JOB_ID = CONFIG_FLAG(136, "zeroMQJOBID",
NONE(), EXTERNAL(), STRING_FLAG("empty"), NONE(),
Gettext.gettext("Configures the ID with which the omc api call is labelled for zeroMQ communication."));
constant ConfigFlag ZEROMQ_SERVER_ID = CONFIG_FLAG(137, "zeroMQServerID",
NONE(), EXTERNAL(), STRING_FLAG("empty"), NONE(),
Gettext.gettext("Configures the ID with which server application is labelled for zeroMQ communication."));
constant ConfigFlag ZEROMQ_CLIENT_ID = CONFIG_FLAG(138, "zeroMQClientID",
NONE(), EXTERNAL(), STRING_FLAG("empty"), NONE(),
Gettext.gettext("Configures the ID with which the client application is labelled for zeroMQ communication."));
constant ConfigFlag FMI_VERSION = CONFIG_FLAG(139,
"", NONE(), INTERNAL(), STRING_FLAG(""), NONE(),
Gettext.gettext("returns the FMI Version either 1.0 or 2.0."));

function getFlags
"Loads the flags with getGlobalRoot. Assumes flags have been loaded."
input Boolean initialize = true;
Expand Down
5 changes: 4 additions & 1 deletion OMCompiler/Compiler/Util/FlagsUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,11 @@ constant list<Flags.ConfigFlag> allConfigFlags = {
Flags.USE_ZEROMQ_IN_SIM,
Flags.ZEROMQ_PUB_PORT,
Flags.ZEROMQ_SUB_PORT,
Flags.ZEROMQ_SIM_ID,
Flags.ZEROMQ_JOB_ID,
Flags.ZEROMQ_SERVER_ID,
Flags.ZEROMQ_CLIENT_ID,
Flags.FMI_VERSION

};

public function new
Expand Down
4 changes: 4 additions & 0 deletions OMCompiler/SimulationRuntime/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,10 @@ include_directories (${Boost_INCLUDE_DIR} ${cppzmq_INCLUDE_DIR} ${ZeroMQ_INCLUDE
include_directories ("${PROJECT_SOURCE_DIR}")
include_directories ("${PROJECT_BINARY_DIR}")





message(STATUS "Boost dirs '${Boost_LIBRARY_DIRS}', '${Boost_INCLUDE_DIR}'")

if(UNIX AND Boost_INCLUDE_DIR STREQUAL "/usr/include")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ try
global_settings->setOutputPath(simsettings.outputPath);
global_settings->setZeroMQPubPort(simsettings.zeroMQPubPort);
global_settings->setZeroMQSubPort(simsettings.zeroMQSubPort);

global_settings->setZeroMQJobiID(simsettings.zeromqJobID);
global_settings->setZeroMQServerID(simsettings.zeromqServerID);
global_settings->setZeroMQClientID(simsettings.zeromqClientID);

_simMgr = shared_ptr<SimManager>(new SimManager(mixedsystem, _config.get()));

Expand Down Expand Up @@ -213,7 +215,7 @@ catch (ModelicaSimulationError& ex)
if(_startZeroMQ)
{
#if defined(USE_ZEROMQ)
_communicator->initialize(global_settings->getZeroMQPubPort(), global_settings->getZeroMQSubPort(), global_settings->getSimulationID());
_communicator->initialize(global_settings->getZeroMQPubPort(), global_settings->getZeroMQSubPort(), global_settings->getZeroMQJobiID(), global_settings->getZeroMQServerID(),global_settings->getZeroMQClientID());
_communicator->startThreads(_simMgr, global_settings, mixedsystem, _sim_objects, modelKey);
_communicator->waitForAllThreads(120);
#elif defined(USE_ZEROMQ)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ Communicator::~Communicator()

}

void Communicator::initialize(int pubPort, int subPort, int simulationID)
void Communicator::initialize(int pubPort, int subPort, string zeroMQJobiID, string zeroMQServerID, string zeroMQClientID)
{
try
{


_notify = shared_ptr<INotify>(new ToZeroMQEvent( pubPort, subPort, simulationID));
_notify = shared_ptr<INotify>(new ToZeroMQEvent( pubPort, subPort, zeroMQJobiID, zeroMQServerID, zeroMQClientID));


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@
// Short alias for this namespace
namespace pt = boost::property_tree;

ToZeroMQEvent::ToZeroMQEvent(int pubPort, int subPort, int simulationID)
ToZeroMQEvent::ToZeroMQEvent(int pubPort, int subPort, string zeroMQJobiID, string zeroMQServerID, string zeroMQClientID)
:ctx_(1),
publisher_(ctx_, ZMQ_PUB),
subscriber_(ctx_, ZMQ_SUB),
_simulation_id(std::to_string(simulationID))
_zeromq_job_id(zeroMQJobiID),
_zeromq_server_id(zeroMQServerID),
_zeromq_client_id(zeroMQClientID)

{
publisher_.connect("tcp://127.0.0.1:" + to_string(pubPort));
subscriber_.connect("tcp://127.0.0.1:" + to_string(subPort));
subscriber_.setsockopt(ZMQ_SUBSCRIBE, "OMCSimultionThread", 18);
string zeromq_simultaion_thread_id = _zeromq_server_id + string("Thread");
subscriber_.setsockopt(ZMQ_SUBSCRIBE, zeromq_simultaion_thread_id.c_str(), 18);
//Needed to establish connection
std::this_thread::sleep_for(std::chrono::milliseconds(500));
//std::cout << "Connection to ZeroMQ 1 " << std::endl;

//std::cout << "pub port: " << to_string(pubPort)<< " sub port: " << to_string(subPort) << " job id " << zeroMQJobiID << " server thread id " << zeromq_simultaion_thread_id << std::endl;
}
ToZeroMQEvent::~ToZeroMQEvent()
{
Expand All @@ -32,17 +37,17 @@ void ToZeroMQEvent::NotifyResults(double progress)
boost::property_tree::ptree progress_tree;
std::stringstream progress_stream;
int p = (int)progress;
if ((_progress != p)&& (!_simulation_id.empty()))
if ((_progress != p)&& (!_zeromq_job_id.empty()))
{

_progress = p;
progress_tree.put("JobId", _simulation_id);
progress_tree.put("JobId", _zeromq_job_id);
progress_tree.put("Progress", (int)progress);
pt::write_json(progress_stream, progress_tree);

s_sendmore(publisher_, "Client",false);
s_sendmore(publisher_, _zeromq_client_id,false);
s_sendmore(publisher_, "SimulationProgressChanged",false);
s_send(publisher_, "{\"jobId\":\"" + _simulation_id + "\",\"progress\":"+ std::to_string(p) +"}",false);
s_send(publisher_, "{\"jobId\":\"" + _zeromq_job_id + "\",\"progress\":"+ std::to_string(p) +"}",false);



Expand All @@ -54,11 +59,11 @@ void ToZeroMQEvent::NotifyResults(double progress)
void ToZeroMQEvent::NotifyWaitForStarting()
{
//std::cout << "Wating for ID" << std::endl;
s_sendmore(publisher_, "OMCSimulator");
s_sendmore(publisher_, _zeromq_server_id);
s_sendmore(publisher_, "SimulationThreadWatingForID");
s_send(publisher_, "{\"jobId\":\"BBBBBBB\"}");
s_send(publisher_, "{\"jobId\":\"" + _zeromq_job_id + "\"}");

//std::cout << "test wait for message start simulation thread" << std::endl;

// Read envelope with address
std::string topic = s_recv(subscriber_);
// Read message contents
Expand All @@ -69,9 +74,9 @@ void ToZeroMQEvent::NotifyWaitForStarting()
// Create a root
pt::ptree root;
pt::read_json(ss, root);
_simulation_id = root.get < std::string >("jobId");

// std::cout << "topic: " << topic << ", Type: " << type << ", Message:" << message << std::endl;
_zeromq_job_id = root.get < std::string >("jobId");

}
bool ToZeroMQEvent::AskForStop()
{
Expand All @@ -97,11 +102,11 @@ bool ToZeroMQEvent::AskForStop()

void ToZeroMQEvent::NotifyFinish()
{
if (!_simulation_id.empty())
if (!_zeromq_job_id.empty())
{
s_sendmore(publisher_, "Client");
s_sendmore(publisher_, _zeromq_client_id);
s_sendmore(publisher_, "SimulationFinished");
s_send(publisher_, "{\"Succeeded\":true,\"JobId\":\"" + _simulation_id + "\",\"ResultFile\":\"\",\"Error\":\"\"}");
s_send(publisher_, "{\"Succeeded\":true,\"JobId\":\"" + _zeromq_job_id + "\",\"ResultFile\":\"\",\"Error\":\"\"}");
}
else
throw ModelicaSimulationError(SIMMANAGER, "No simulation id received");
Expand All @@ -115,11 +120,11 @@ void ToZeroMQEvent::NotifyException(std::string message)

void ToZeroMQEvent::NotifyStarted()
{
if (!_simulation_id.empty())
if (!_zeromq_job_id.empty())
{
s_sendmore(publisher_, "Client");
s_sendmore(publisher_, _zeromq_client_id);
s_sendmore(publisher_, "SimulationStarted");
s_send(publisher_, "{\"JobId\":\"" + _simulation_id + "\"}");
s_send(publisher_, "{\"JobId\":\"" + _zeromq_job_id + "\"}");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@
#include <Core/SimulationSettings/GlobalSettings.h>

GlobalSettings::GlobalSettings()
: _startTime(0.0)
, _endTime(5.0)
, _hOutput(0.001)
, _emitResults(EMIT_ALL)
, _infoOutput(true)
, _selected_solver("Euler")
, _selected_lin_solver("linearSolver")
, _selected_nonlin_solver("Newton")
, _resultsfile_name("results.csv")
, _endless_sim(false)
, _nonLinSolverContinueOnError(false)
, _outputPointType(OPT_ALL)
, _alarm_time(0)
, _outputFormat(MAT)
,_zeroMQ_pub_port(3203)
,_zeroMQ_sub_port(3204)
,_simulation_id(-1)
: _startTime(0.0)
, _endTime(5.0)
, _hOutput(0.001)
, _emitResults(EMIT_ALL)
, _infoOutput(true)
, _selected_solver("Euler")
, _selected_lin_solver("linearSolver")
, _selected_nonlin_solver("Newton")
, _resultsfile_name("results.csv")
, _endless_sim(false)
, _nonLinSolverContinueOnError(false)
, _outputPointType(OPT_ALL)
, _alarm_time(0)
, _outputFormat(MAT)
,_zeroMQ_pub_port(3203)
,_zeroMQ_sub_port(3204)
,_zeromq_job_id("empty")
,_zeromq_server_id("empty")
,_zeromq_client_id("empty")
{
}

Expand Down Expand Up @@ -252,12 +254,35 @@ int GlobalSettings::getSolverThreads()
return _zeroMQ_sub_port;
}

void GlobalSettings::setSimulationID(int id)




void GlobalSettings::setZeroMQServerID(string id)
{
_simulation_id = id;
_zeromq_server_id = id;
}
int GlobalSettings::getSimulationID()
string GlobalSettings::getZeroMQServerID()
{
return _simulation_id;
return _zeromq_server_id;
}

void GlobalSettings::setZeroMQClientID(string id)
{
_zeromq_client_id = id;
}
string GlobalSettings::getZeroMQClientID()
{
return _zeromq_client_id;
}
void GlobalSettings::setZeroMQJobiID(string id)
{
_zeromq_job_id = id;
}
string GlobalSettings::getZeroMQJobiID()
{
return _zeromq_job_id;
}


/** @} */ // end of coreSimulationSettings
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ struct SimSettings
bool useZeroMQ;
int zeroMQPubPort;
int zeroMQSubPort;
int simulationID;
string zeromqJobID;
string zeromqServerID;
string zeromqClientID;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Communicator
virtual void startPause();
virtual void stopPause();
virtual bool shouldStop();
virtual void initialize(int pubPort, int subPort, int simulationID);
virtual void initialize(int pubPort, int subPort, string zeroMQJobiID,string zeroMQServerID, string zeroMQClientID);

void notifyResults(double time);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class ToZeroMQEvent: public INotify
{
public:
ToZeroMQEvent(int pubPort, int subPort, int simulationID);
ToZeroMQEvent(int pubPort, int subPort, string zeroMQJobiID, string zeroMQServerID, string zeroMQClientID);

~ToZeroMQEvent();
virtual void NotifyStarted();
Expand All @@ -23,7 +23,9 @@
zmq::socket_t publisher_;
zmq::socket_t subscriber_;

std::string _simulation_id;
string _zeromq_job_id;
string _zeromq_server_id;
string _zeromq_client_id;
int _progress;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ class GlobalSettings : public IGlobalSettings
virtual void setZeroMQSubPort(int);
virtual int getZeroMQSubPort() ;

virtual void setSimulationID(int);
virtual int getSimulationID();
virtual void setZeroMQJobiID(string);
virtual string getZeroMQJobiID();
virtual void setZeroMQServerID(string);
virtual string getZeroMQServerID();
virtual void setZeroMQClientID(string);
virtual string getZeroMQClientID();
private:
double
_startTime, ///< Start time of integration (default: 0.0)
Expand Down Expand Up @@ -99,6 +103,8 @@ class GlobalSettings : public IGlobalSettings
OutputFormat _outputFormat;
int _zeroMQ_pub_port;
int _zeroMQ_sub_port;
int _simulation_id;
string _zeromq_job_id;
string _zeromq_server_id;
string _zeromq_client_id;
};
/** @} */ // end of coreSimulationSettings
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ class IGlobalSettings
virtual int getZeroMQSubPort() = 0;


virtual void setSimulationID(int) = 0;
virtual int getSimulationID() = 0;
virtual void setZeroMQJobiID(string) = 0;
virtual string getZeroMQJobiID() = 0;
virtual void setZeroMQServerID(string) = 0;
virtual string getZeroMQServerID() = 0;
virtual void setZeroMQClientID(string) = 0;
virtual string getZeroMQClientID() = 0;
};
/** @} */ // end of coreSimulationSettings

0 comments on commit bfa7423

Please sign in to comment.