Skip to content

Commit

Permalink
[cppRuntime] Added config flags to configure use of zeromq communicat…
Browse files Browse the repository at this point in the history
…ion in

simulation runtime.The ports for publish and subscribe can be configured. The
simulation runtime can then communicate over these ports with other applications via
zeroMQ.
  • Loading branch information
niklwors authored and adrpo committed Nov 13, 2019
1 parent e9fc5e1 commit 5d013b7
Show file tree
Hide file tree
Showing 23 changed files with 204 additions and 82 deletions.
6 changes: 3 additions & 3 deletions OMCompiler/Compiler/Template/CodegenCpp.tpl
Expand Up @@ -1529,14 +1529,14 @@ 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 ''
match makefileParams.platform
case "linux32"
case "linux64" then
<<
#!/bin/sh
<%preRunCommandLinux%>
<%execCommandLinux%> ./<%fileNamePrefixx%> <%execParameters%> <%outputParameter%> $*
<%execCommandLinux%> ./<%fileNamePrefixx%> <%execParameters%> <%zermMQParams%> <%outputParameter%> $*
>>
case "win32"
case "win64" then
Expand All @@ -1545,7 +1545,7 @@ 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%> <%outputParameter%>
<%moLib%>/<%fileNamePrefixx%>.exe <%execParameters%> <%zermMQParams%> <%outputParameter%>
>>
end match
end match
Expand Down
4 changes: 4 additions & 0 deletions OMCompiler/Compiler/Template/SimCodeTV.mo
Expand Up @@ -3758,6 +3758,10 @@ package Flags
constant ConfigFlag Load_PACKAGE_FILE;
constant ConfigFlag SINGLE_INSTANCE_AGLSOLVER;
constant ConfigFlag LINEARIZATION_DUMP_LANGUAGE;
constant ConfigFlag USE_ZEROMQ_IN_SIM;
constant ConfigFlag ZEROMQ_PUB_PORT;
constant ConfigFlag ZEROMQ_SUB_PORT;
constant ConfigFlag ZEROMQ_SIM_ID;
function set
input DebugFlag inFlag;
input Boolean inValue;
Expand Down
22 changes: 21 additions & 1 deletion OMCompiler/Compiler/Util/Flags.mo
Expand Up @@ -1518,6 +1518,22 @@ constant ConfigFlag FULL_ASSC = CONFIG_FLAG(133, "fullASSC",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Enables full equation replacement for BLT transformation from the ASSC algorithm."));

constant ConfigFlag USE_ZEROMQ_IN_SIM = CONFIG_FLAG(134, "useZeroMQInSim",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Configures to use zeroMQ in simulation runtime to exchange information via ZeroMQ with other applications"));

constant ConfigFlag ZEROMQ_PUB_PORT = CONFIG_FLAG(135, "zeroMQPubPort",
NONE(), EXTERNAL(), INT_FLAG(3203), NONE(),
Util.gettext("Configures port number for simulation runtime to send information via ZeroMQ"));

constant ConfigFlag ZEROMQ_SUB_PORT = CONFIG_FLAG(136, "zeroMQSubPort",
NONE(), EXTERNAL(), INT_FLAG(3204), NONE(),
Util.gettext("Configures port number for simulation runtime to receive information via ZeroMQ"));

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

protected
// This is a list of all configuration flags. A flag can not be used unless it's
// in this list, and the list is checked at initialization so that all flags are
Expand Down Expand Up @@ -1655,7 +1671,11 @@ constant list<ConfigFlag> allConfigFlags = {
STRICT,
LINEARIZATION_DUMP_LANGUAGE,
NO_ASSC,
FULL_ASSC
FULL_ASSC,
USE_ZEROMQ_IN_SIM,
ZEROMQ_PUB_PORT,
ZEROMQ_SUB_PORT,
ZEROMQ_SIM_ID
};

public function new
Expand Down
Expand Up @@ -33,7 +33,7 @@ _model(model)
std::cout << "Intialize OMC, use gcc" << std::endl;


status = InitOMC(&_omcPtr,"gcc",omhome);
status = InitOMC(&_omcPtr,"gcc",omhome,"");
if(status > 0)
{
std::cout << "..ok" << std::endl;
Expand Down
Expand Up @@ -4,38 +4,8 @@
*/
#include <Core/ModelicaDefine.h>
#include <Core/Modelica.h>
#if defined(__vxworks) || defined(__TRICORE__)



#include <Core/SimController/ISimController.h>
#include <Core/SimController/SimController.h>
#include <Core/SimController/SimObjects.h>

extern "C" ISimController* createSimController(PATH library_path, PATH modelicasystem_path)
{
return new SimController(library_path, modelicasystem_path);
}

shared_ptr<ISimObjects> createSimObjects(PATH library_path, PATH modelicasystem_path,IGlobalSettings* settings)
{
return shared_ptr<ISimObjects>(new SimObjects(library_path, modelicasystem_path,settings));
}

#elif defined(SIMSTER_BUILD)



#include <Core/SimController/ISimController.h>
#include <Core/SimController/SimController.h>
/*Simster factory*/
extern "C" void BOOST_EXTENSION_EXPORT_DECL extension_export_simcontroller(boost::extensions::factory_map & fm)
{
fm.get<ISimController,int,PATH,PATH>()[1].set<SimController>();
// fm.get<ISimData,int>()[1].set<SimData>();
}

#elif defined(OMC_BUILD) && !defined(RUNTIME_STATIC_LINKING)
#if defined(OMC_BUILD) && !defined(RUNTIME_STATIC_LINKING)

#include <Core/SimController/ISimController.h>
#include <Core/SimController/SimController.h>
Expand All @@ -45,6 +15,8 @@ using boost::extensions::factory;
BOOST_EXTENSION_TYPE_MAP_FUNCTION {
types.get<std::map<std::string, factory<ISimController,PATH,PATH> > >()
["SimController"].set<SimController>();
types.get<std::map<std::string, factory<ISimController, PATH, PATH,bool> > >()
["SimController"].set<SimController>();
types.get<std::map<std::string, factory<ISimObjects,PATH,PATH,IGlobalSettings* > > >()
["SimObjects"].set<SimObjects>();
}
Expand Down
Expand Up @@ -179,6 +179,9 @@ try
global_settings->setSolverThreads(simsettings.solverThreads);
global_settings->setInputPath(simsettings.inputPath);
global_settings->setOutputPath(simsettings.outputPath);
global_settings->setZeroMQPubPort(simsettings.zeroMQPubPort);
global_settings->setZeroMQSubPort(simsettings.zeroMQSubPort);


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

Expand Down Expand Up @@ -210,6 +213,7 @@ catch (ModelicaSimulationError& ex)
if(_startZeroMQ)
{
#if defined(USE_ZEROMQ)
_communicator->initialize(global_settings->getZeroMQPubPort(), global_settings->getZeroMQSubPort(), global_settings->getSimulationID());
_communicator->startThreads(_simMgr, global_settings, mixedsystem, _sim_objects, modelKey);
_communicator->waitForAllThreads(120);
#elif defined(USE_ZEROMQ)
Expand Down
Expand Up @@ -13,21 +13,9 @@ Communicator::Communicator()
, _simstopped(true)
,_guistopped(true)
, _stop(false)
,_isInitialized(false)
{
try
{


_notify = shared_ptr<INotify>(new ToZeroMQEvent());


}
catch (std::exception& ex)
{

std::string error(ex.what());

}

}


Expand All @@ -40,6 +28,25 @@ Communicator::~Communicator()



}

void Communicator::initialize(int pubPort, int subPort, int simulationID)
{
try
{


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


}
catch (std::exception & ex)
{

std::string error(ex.what());

}
_isInitialized = true;
}
/**
Waits for all threads to end.
Expand Down
Expand Up @@ -49,7 +49,7 @@ void SimulationThread::Run(shared_ptr<SimManager> simManager, shared_ptr<IGlobal
if (starting)
{
_communicator->setSimStarted();
simManager->initialize();


#ifdef RUNTIME_PROFILING
if (MeasureTime::getInstance() != NULL)
Expand Down
Expand Up @@ -8,14 +8,14 @@
// Short alias for this namespace
namespace pt = boost::property_tree;

ToZeroMQEvent::ToZeroMQEvent()
ToZeroMQEvent::ToZeroMQEvent(int pubPort, int subPort, int simulationID)
:ctx_(1),
publisher_(ctx_, ZMQ_PUB),
subscriber_(ctx_, ZMQ_SUB),
_simulation_id("")
_simulation_id(std::to_string(simulationID))
{
publisher_.connect("tcp://127.0.0.1:3203");
subscriber_.connect("tcp://127.0.0.1:3204");
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);
//Needed to establish connection
std::this_thread::sleep_for(std::chrono::milliseconds(500));
Expand Down
Expand Up @@ -23,6 +23,9 @@ GlobalSettings::GlobalSettings()
, _outputPointType(OPT_ALL)
, _alarm_time(0)
, _outputFormat(MAT)
,_zeroMQ_pub_port(3203)
,_zeroMQ_sub_port(3204)
,_simulation_id(-1)
{
}

Expand Down Expand Up @@ -229,4 +232,32 @@ int GlobalSettings::getSolverThreads()
{
_outputFormat = outputFormat;
}


void GlobalSettings::setZeroMQPubPort(int pubPort)
{
_zeroMQ_pub_port = pubPort;
}
int GlobalSettings::getZeroMQPubPort()
{
return _zeroMQ_pub_port;
}

void GlobalSettings::setZeroMQSubPort(int subPort)
{
_zeroMQ_sub_port = subPort;
}
int GlobalSettings::getZeroMQSubPort()
{
return _zeroMQ_sub_port;
}

void GlobalSettings::setSimulationID(int id)
{
_simulation_id = id;
}
int GlobalSettings::getSimulationID()
{
return _simulation_id;
}
/** @} */ // end of coreSimulationSettings
Expand Up @@ -25,6 +25,10 @@ struct SimSettings
EmitResults emitResults;
string inputPath;
string outputPath;
bool useZeroMQ;
int zeroMQPubPort;
int zeroMQSubPort;
int simulationID;
};

/**
Expand Down
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);

void notifyResults(double time);

Expand Down Expand Up @@ -61,7 +61,7 @@ class Communicator
bool _paused;
double _end_time;
shared_ptr<Runnable> _sim_thread;

bool _isInitialized;

};

Expand Up @@ -9,7 +9,7 @@
class ToZeroMQEvent: public INotify
{
public:
ToZeroMQEvent( );
ToZeroMQEvent(int pubPort, int subPort, int simulationID);

~ToZeroMQEvent();
virtual void NotifyStarted();
Expand Down
Expand Up @@ -64,6 +64,14 @@ class GlobalSettings : public IGlobalSettings
virtual void setSolverThreads(int);
virtual int getSolverThreads();

virtual void setZeroMQPubPort(int);
virtual int getZeroMQPubPort();

virtual void setZeroMQSubPort(int);
virtual int getZeroMQSubPort() ;

virtual void setSimulationID(int);
virtual int getSimulationID();
private:
double
_startTime, ///< Start time of integration (default: 0.0)
Expand All @@ -89,5 +97,8 @@ class GlobalSettings : public IGlobalSettings

int _solverThreads;
OutputFormat _outputFormat;
int _zeroMQ_pub_port;
int _zeroMQ_sub_port;
int _simulation_id;
};
/** @} */ // end of coreSimulationSettings
Expand Up @@ -103,5 +103,15 @@ class IGlobalSettings

virtual void setSolverThreads(int) = 0;
virtual int getSolverThreads() = 0;

virtual void setZeroMQPubPort(int) = 0;
virtual int getZeroMQPubPort() = 0;

virtual void setZeroMQSubPort(int) = 0;
virtual int getZeroMQSubPort() = 0;


virtual void setSimulationID(int) = 0;
virtual int getSimulationID() = 0;
};
/** @} */ // end of coreSimulationSettings
Expand Up @@ -59,5 +59,11 @@ class FMUGlobalSettings : public IGlobalSettings
virtual int getSolverThreads() { return 1; };
virtual OutputFormat getOutputFormat() {return EMPTY;};
virtual void setOutputFormat(OutputFormat) {};
virtual void setZeroMQPubPort(int) {};
virtual int getZeroMQPubPort() { return -1; };
virtual void setZeroMQSubPort(int) {};
virtual int getZeroMQSubPort() { return -1; };
virtual void setSimulationID(int) {};
virtual int getSimulationID() { return -1; };
private:
};
Expand Up @@ -91,5 +91,11 @@ class FMU2GlobalSettings : public IGlobalSettings
virtual int getSolverThreads() { return 1; };
virtual OutputFormat getOutputFormat() {return EMPTY;};
virtual void setOutputFormat(OutputFormat) {};
virtual void setZeroMQPubPort(int) {};
virtual int getZeroMQPubPort() { return -1; };
virtual void setZeroMQSubPort(int) {};
virtual int getZeroMQSubPort() { return -1; };
virtual void setSimulationID(int) {};
virtual int getSimulationID() { return -1; };
};
/** @} */ // end of fmu2
Expand Up @@ -97,5 +97,6 @@ class OMCFactory
unordered_set<string> _argumentsToIgnore; //a set of arguments that should be ignored
std::map<string, string> _argumentsToReplace; //a mapping to replace arguments (e.g. -r=... -> -F=...)
std::string _overrideOMEdit; // unrecognized options if called from OMEdit
bool _use_zeroMQ;
};
/** @} */ // end of simcorefactoryOMCFactory

0 comments on commit 5d013b7

Please sign in to comment.