Skip to content

Commit a4250b3

Browse files
committed
- Converted options.cpp to C89 code, and made it more memory-efficient
- Added option -clock=CPU (or -clock=RT) for the simulation executable (in case the user wants a different clock on Linux) git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15253 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent a307afc commit a4250b3

File tree

7 files changed

+105
-65
lines changed

7 files changed

+105
-65
lines changed

SimulationRuntime/c/simulation/options.cpp renamed to SimulationRuntime/c/simulation/options.c

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,25 @@
3131
#include "options.h"
3232
#include "omc_error.h"
3333

34-
#include <string>
35-
36-
using namespace std;
34+
#include <string.h>
35+
#include <stdio.h>
3736

3837
int checkCommandLineArguments(int argc, char **argv)
3938
{
40-
for(int i=1; i<argc; ++i)
39+
int i,j;
40+
for(i=1; i<argc; ++i)
4141
{
42-
int error = 1; /* first, suggest an error anyway */
43-
string tmpStr = string(argv[i]);
44-
45-
for(int j=1; j<FLAG_MAX; ++j)
42+
int found=0;
43+
for(j=1; j<FLAG_MAX; ++j)
4644
{
47-
if(tmpStr == ("-" + string(FLAG_NAME[j])))
48-
{
49-
if(FLAG_TYPE[j] == FLAG_TYPE_FLAG)
50-
error = 0;
51-
else if((FLAG_TYPE[j] == FLAG_TYPE_FLAG_VALUE) && (++i < argc))
52-
error = 0;
45+
if (((FLAG_TYPE[j] == FLAG_TYPE_FLAG) && flagSet(FLAG_NAME[j],1,argv+i)) ||
46+
((FLAG_TYPE[j] == FLAG_TYPE_FLAG_VALUE) && flagSet(FLAG_NAME[j],1,argv+i) && (++i < argc)) ||
47+
((FLAG_TYPE[j] == FLAG_TYPE_OPTION) && optionSet(FLAG_NAME[j],1,argv+i))) {
48+
found=1;
49+
break;
5350
}
54-
else if(tmpStr.substr(0,tmpStr.find("=")) == ("-" + string(FLAG_NAME[j])))
55-
error = 0;
5651
}
57-
58-
if(error)
59-
{
52+
if (!found) {
6053
WARNING1(LOG_STDOUT, "invalid command line option: %s", argv[i]);
6154
return 1;
6255
}
@@ -67,46 +60,40 @@ int checkCommandLineArguments(int argc, char **argv)
6760

6861
int flagSet(const char *option, int argc, char** argv)
6962
{
70-
for(int i=0; i<argc;i++)
63+
int i;
64+
for (i=0; i<argc;i++)
7165
{
72-
if(("-"+string(option)) == string(argv[i]))
66+
if (argv[i][0] == '-' && 0==strcmp(option,argv[i]+1))
7367
return 1;
7468
}
7569
return 0;
7670
}
7771

7872
int optionSet(const char *option, int argc, char** argv)
7973
{
80-
for(int i=0; i<argc;i++)
81-
{
82-
string tmpStr=string(argv[i]);
83-
if(("-"+string(option)) == (tmpStr.substr(0,tmpStr.find("="))))
84-
return 1;
85-
}
86-
return 0;
74+
return getOption(option,argc,argv) != NULL;
8775
}
8876

8977
/* returns the value of a flag on the form -flagname=value */
90-
const string* getOption(const char *option, int argc, char **argv)
78+
const char* getOption(const char *option, int argc, char **argv)
9179
{
92-
for(int i=0; i<argc;i++)
93-
{
94-
string tmpStr=string(argv[i]);
95-
if(("-"+string(option)) == (tmpStr.substr(0,tmpStr.find("="))))
96-
return new string(tmpStr.substr(tmpStr.find("=")+1));
80+
int optLen = strlen(option), i;
81+
for (i=0; i<argc;i++) {
82+
if (argv[i][0] == '-' && 0==strncmp(option,argv[i]+1,optLen) && argv[i][optLen+1]=='=') {
83+
return argv[i]+optLen+2;
84+
}
9785
}
9886
return NULL;
9987
}
10088

10189
/* returns the value of a flag on the form -flagname value */
102-
const string* getFlagValue(const char *option, int argc, char **argv)
90+
const char* getFlagValue(const char *option, int argc, char **argv)
10391
{
104-
for(int i=0; i<argc;i++)
92+
int i;
93+
for(i=0; i<argc-1;i++)
10594
{
106-
string tmpStr=string(argv[i]);
107-
if(("-"+string(option)) == string(argv[i]))
108-
if(argc > i+1)
109-
return new string(argv[i+1]);
95+
if (argv[i][0] == '-' && 0==strcmp(option,argv[i]+1))
96+
return argv[i+1];
11097
}
11198
return NULL;
11299
}

SimulationRuntime/c/simulation/options.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@
3232
#ifndef OPTIONS_H
3333
#define OPTIONS_H
3434

35-
#include <string>
3635
#include "simulation_options.h"
3736

37+
#ifdef __cplusplus
38+
extern "C" {
39+
#endif
3840
int checkCommandLineArguments(int argc, char **argv);
3941

4042
int flagSet(const char*, int, char**); /* -f */
4143
int optionSet(const char *option, int argc, char** argv); /* -f=value */
42-
const std::string* getOption(const char*, int, char **); /* -f=value; returns NULL if not found */
43-
const std::string* getFlagValue(const char *, int , char **); /* -f value; returns NULL if not found */
44+
const char* getOption(const char*, int, char **); /* -f=value; returns NULL if not found */
45+
const char* getFlagValue(const char *, int , char **); /* -f value; returns NULL if not found */
46+
#ifdef __cplusplus
47+
}
48+
#endif
4449

4550
#endif

SimulationRuntime/c/simulation/simulation_runtime.cpp

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ void setTermMsg(const char *msg)
155155
*/
156156
void setGlobalVerboseLevel(int argc, char**argv)
157157
{
158-
const string *flags = getOption("lv", argc, argv);
158+
const char *cflags = getOption("lv", argc, argv);
159+
const string *flags = cflags ? new string(cflags) : NULL;
159160
int i;
160161
int error;
161162

@@ -249,7 +250,8 @@ void setGlobalVerboseLevel(int argc, char**argv)
249250

250251
int getNonlinearSolverMethod(int argc, char**argv)
251252
{
252-
const string *method = getOption("nls", argc, argv);
253+
const char *cflags = getOption("nls", argc, argv);
254+
const string *method = cflags ? new string(cflags) : NULL;
253255

254256
if(!method)
255257
return NS_HYBRID; /* default method */
@@ -273,7 +275,8 @@ int getNonlinearSolverMethod(int argc, char**argv)
273275

274276
int getlinearSolverMethod(int argc, char**argv)
275277
{
276-
const string *method = getOption("ls", argc, argv);
278+
const char *cflags = getOption("ls", argc, argv);
279+
const string *method = cflags ? new string(cflags) : NULL;
277280

278281
if(!method)
279282
return LS_LAPACK; /* default method */
@@ -382,7 +385,7 @@ int startNonInteractiveSimulation(int argc, char**argv, DATA* data)
382385

383386
/* linear model option is set : <-l lintime> */
384387
int create_linearmodel = optionSet("l", argc, argv);
385-
string* lintime = (string*) getOption("l", argc, argv);
388+
const char* lintime = getOption("l", argc, argv);
386389

387390
/* activated measure time option with LOG_STATS */
388391
if((ACTIVE_STREAM(LOG_STATS) || flagSet("cpu", argc, argv)) && !measure_time_flag)
@@ -394,6 +397,23 @@ int startNonInteractiveSimulation(int argc, char**argv, DATA* data)
394397
/* calc numStep */
395398
data->simulationInfo.numSteps = static_cast<modelica_integer>((data->simulationInfo.stopTime - data->simulationInfo.startTime)/data->simulationInfo.stepSize);
396399

400+
{ /* Setup the clock */
401+
enum omc_rt_clock_t clock = OMC_CLOCK_REALTIME;
402+
const char *clockName;
403+
if (clockName=getOption("clock",argc,argv)) {
404+
if (0==strcmp(clockName,"CPU")) {
405+
clock = OMC_CLOCK_CPUTIME;
406+
} else if (0==strcmp(clockName,"RT")) {
407+
clock = OMC_CLOCK_REALTIME;
408+
} else {
409+
WARNING1(LOG_STDOUT, "[unknown clock-type] got %s, expected CPU|RT. Defaulting to RT.", clockName);
410+
}
411+
}
412+
if (rt_set_clock(clock)) {
413+
WARNING1(LOG_STDOUT, "Chosen clock-type not available for the current platform. Defaulting to real-time.", clockName);
414+
}
415+
}
416+
397417
if(measure_time_flag)
398418
{
399419
modelInfoXmlInit(&data->modelData.modelDataXml);
@@ -410,13 +430,13 @@ int startNonInteractiveSimulation(int argc, char**argv, DATA* data)
410430
if(lintime == NULL)
411431
data->simulationInfo.stopTime = data->simulationInfo.startTime;
412432
else
413-
data->simulationInfo.stopTime = atof(lintime->c_str());
433+
data->simulationInfo.stopTime = atof(lintime);
414434
INFO1(LOG_STDOUT, "Linearization will performed at point of time: %f", data->simulationInfo.stopTime);
415435
}
416436

417437
if(optionSet("s", argc, argv))
418438
{
419-
const string *method = getOption("s", argc, argv);
439+
const string *method = new string(getOption("s", argc, argv));
420440
if(method)
421441
{
422442
data->simulationInfo.solverMethod = method->c_str();
@@ -442,25 +462,29 @@ int startNonInteractiveSimulation(int argc, char**argv, DATA* data)
442462
string outputVariablesAtEnd = "";
443463
int cpuTime = flagSet("cpu", argc, argv);
444464

445-
if(optionSet("iim", argc, argv))
446-
init_initMethod = *getOption("iim", argc, argv);
447-
if(optionSet("iom", argc, argv))
448-
init_optiMethod = *getOption("iom", argc, argv);
449-
if(optionSet("iif", argc, argv))
450-
init_file = *getOption("iif", argc, argv);
465+
if(optionSet("iim", argc, argv)) {
466+
init_initMethod = getOption("iim", argc, argv);
467+
}
468+
if(optionSet("iom", argc, argv)) {
469+
init_optiMethod = getOption("iom", argc, argv);
470+
}
471+
if(optionSet("iif", argc, argv)) {
472+
init_file = getOption("iif", argc, argv);
473+
}
451474
if(optionSet("iit", argc, argv))
452475
{
453-
init_time_string = *getOption("iit", argc, argv);
476+
init_time_string = getOption("iit", argc, argv);
454477
init_time = atof(init_time_string.c_str());
455478
}
456479
if(optionSet("ils", argc, argv))
457480
{
458-
init_lambda_steps_string = *getOption("ils", argc, argv);
481+
init_lambda_steps_string = getOption("ils", argc, argv);
459482
init_lambda_steps = atoi(init_lambda_steps_string.c_str());
460483
}
461484

462-
if(flagSet("output", argc, argv))
463-
outputVariablesAtEnd = *getFlagValue("output", argc, argv);
485+
if(flagSet("output", argc, argv)) {
486+
outputVariablesAtEnd = getFlagValue("output", argc, argv);
487+
}
464488

465489
retVal = callSolver(data, result_file_cstr, init_initMethod, init_optiMethod, init_file, init_time, init_lambda_steps, outputVariablesAtEnd, cpuTime);
466490

@@ -644,7 +668,7 @@ int initRuntimeAndSimulation(int argc, char**argv, DATA *data)
644668

645669
if(optionSet("help", argc, argv))
646670
{
647-
std::string option = *getOption("help", argc, argv);
671+
std::string option = getOption("help", argc, argv);
648672

649673
for(i=1; i<FLAG_MAX; ++i)
650674
{

SimulationRuntime/c/util/rtclock.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ double rt_total(int ix) {
124124

125125
#if defined(__MINGW32__) || defined(_MSC_VER)
126126

127+
int rt_set_clock(omc_rt_clock_t newClock) {
128+
return newClock != OMC_CLOCK_REALTIME;
129+
}
130+
127131
static LARGE_INTEGER performance_frequency;
128132

129133
void rt_tick(int ix) {
@@ -181,6 +185,10 @@ double rtclock_value (LARGE_INTEGER tp) {
181185

182186
#elif defined(__APPLE_CC__)
183187

188+
int rt_set_clock(omc_rt_clock_t newClock) {
189+
return newClock != OMC_CLOCK_REALTIME;
190+
}
191+
184192
void rt_tick(int ix) {
185193
tick_tp[ix] = mach_absolute_time();
186194
rt_clock_ncall[ix]++;
@@ -233,14 +241,21 @@ int rtclock_compare(uint64_t t1, uint64_t t2) {
233241

234242
#else
235243

244+
static clockid_t omc_clock = CLOCK_MONOTONIC_RAW;
245+
246+
int rt_set_clock(enum omc_rt_clock_t newClock) {
247+
omc_clock = newClock == OMC_CLOCK_REALTIME ? CLOCK_MONOTONIC_RAW : CLOCK_PROCESS_CPUTIME_ID;
248+
return 0;
249+
}
250+
236251
void rt_tick(int ix) {
237-
clock_gettime(CLOCK_MONOTONIC, &tick_tp[ix]);
252+
clock_gettime(omc_clock, &tick_tp[ix]);
238253
rt_clock_ncall[ix]++;
239254
}
240255

241256
double rt_tock(int ix) {
242257
struct timespec tock_tp = {0,0};
243-
clock_gettime(CLOCK_MONOTONIC, &tock_tp);
258+
clock_gettime(omc_clock, &tock_tp);
244259
return (tock_tp.tv_sec - tick_tp[ix].tv_sec) + (tock_tp.tv_nsec - tick_tp[ix].tv_nsec)*1e-9;
245260
}
246261

@@ -270,7 +285,7 @@ void rt_clear_total(int ix)
270285

271286
void rt_accumulate(int ix) {
272287
struct timespec tock_tp = {0,0};
273-
clock_gettime(CLOCK_MONOTONIC, &tock_tp);
288+
clock_gettime(omc_clock, &tock_tp);
274289
acc_tp[ix].tv_sec += tock_tp.tv_sec -tick_tp[ix].tv_sec;
275290
acc_tp[ix].tv_nsec += tock_tp.tv_nsec-tick_tp[ix].tv_nsec;
276291
if(acc_tp[ix].tv_nsec >= 1e9) {

SimulationRuntime/c/util/rtclock.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ extern "C" {
5757
#define SIM_PROF_ACC_EQ(ix) rt_accumulate(ix+SIM_TIMER_FIRST_FUNCTION+data->modelData.modelDataXml.nFunctions)
5858
#define SIM_PROF_ADD_NCALL_EQ(ix,num) rt_add_ncall(ix+SIM_TIMER_FIRST_FUNCTION+data->modelData.modelDataXml.nFunctions,num)
5959

60+
enum omc_rt_clock_t {
61+
OMC_CLOCK_REALTIME, /* CLOCK_MONOTONIC_RAW if available; else CLOCK_MONOTONIC */
62+
OMC_CLOCK_CPUTIME /* Per-process CPU-time */
63+
};
64+
65+
int rt_set_clock(enum omc_rt_clock_t clockType); /* non-zero on failure */
6066
void rt_init(int numTimer);
6167

6268
void rt_tick(int ix);

SimulationRuntime/c/util/simulation_options.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
const char *FLAG_NAME[FLAG_MAX] = {
3434
"LOG_UNKNOWN",
35-
35+
/* FLAG_CLOCK */ "clock",
3636
/* FLAG_CPU */ "cpu",
3737
/* FLAG_F */ "f",
3838
/* FLAG_HELP */ "help",
@@ -58,7 +58,7 @@ const char *FLAG_NAME[FLAG_MAX] = {
5858

5959
const char *FLAG_DESC[FLAG_MAX] = {
6060
"unknown",
61-
61+
/* FLAG_CLOCK */ "selects the type of clock to use -clock=RT or -clock=CPU",
6262
/* FLAG_CPU */ "dumps the cpu-time into the results-file",
6363
/* FLAG_F */ "value specifies a new setup XML file to the generated simulation code",
6464
/* FLAG_HELP */ "get deteiled information the specifies the command-line flag",
@@ -85,6 +85,7 @@ const char *FLAG_DESC[FLAG_MAX] = {
8585
const char *FLAG_DETAILED_DESC[FLAG_MAX] = {
8686
"unknown",
8787

88+
/* FLAG_CLOCK */ "selects the type of clock to use -clock=RT or -clock=CPU\n RT=monotonic real-time clock, CPU=process-based CPU-time",
8889
/* FLAG_CPU */ " - dumps the cpu-time into the result-file\n - $cpuTime is the variable name inside the result-file",
8990
/* FLAG_F */ "value specifies a new setup XML file to the generated simulation code",
9091
/* FLAG_HELP */ "get deteiled information the specifies the command-line flag\n e.g. -help=f prints detaild information for command-line flag f",
@@ -111,6 +112,7 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX] = {
111112
const int FLAG_TYPE[FLAG_MAX] = {
112113
FLAG_TYPE_UNKNOWN,
113114

115+
/* FLAG_CLOCK */ FLAG_TYPE_OPTION,
114116
/* FLAG_CPU */ FLAG_TYPE_FLAG,
115117
/* FLAG_F */ FLAG_TYPE_OPTION,
116118
/* FLAG_HELP */ FLAG_TYPE_OPTION,

SimulationRuntime/c/util/simulation_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ enum _FLAG
4040
{
4141
FLAG_UNKNOWN = 0,
4242

43+
FLAG_CLOCK,
4344
FLAG_CPU,
4445
FLAG_F,
4546
FLAG_HELP,

0 commit comments

Comments
 (0)