Skip to content

Commit

Permalink
- Added scripting call system_parallel
Browse files Browse the repository at this point in the history
- Made BuildModelRecursive.mos capable of generating a report while simulating models in parallel


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12994 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Sep 20, 2012
1 parent a702cff commit a7e56f7
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -989,6 +989,13 @@ external "builtin" annotation(__OpenModelica_Impure=true);
annotation(preferredView="text");
end system;

function system_parallel "Similar to system(3). Executes the given commands in the system shell, in parallel if omc was compiled using OpenMP."
input String callStr[:] "String to call: bash -c $callStr";
output Integer retval[:] "Return value of the system call; usually 0 on success";
external "builtin" annotation(__OpenModelica_Impure=true);
annotation(preferredView="text");
end system_parallel;

function saveAll "save the entire loaded AST to file."
input String fileName;
output Boolean success;
Expand Down
7 changes: 7 additions & 0 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -1568,6 +1568,13 @@ algorithm
then
(cache,Values.INTEGER(resI),st);

case (cache,env,"system_parallel",{Values.ARRAY(valueLst=vals)},st,msg)
equation
strs = List.map(vals, ValuesUtil.extractValueString);
v = ValuesUtil.makeIntArray(System.systemCallParallel(strs));
then
(cache,v,st);

case (cache,env,"timerClear",{Values.INTEGER(i)},st,msg)
equation
System.realtimeClear(i);
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Util/System.mo
Expand Up @@ -308,6 +308,12 @@ public function systemCall
external "C" outInteger=SystemImpl__systemCall(inString) annotation(Library = "omcruntime");
end systemCall;

public function systemCallParallel
input list<String> inStrings;
output list<Integer> outIntegers;
external "C" outIntegers=SystemImpl__systemCallParallel(inStrings) annotation(Library = "omcruntime");
end systemCallParallel;

public function spawnCall
input String path "The absolute path to the executable";
input String str "The list of arguments with executable";
Expand Down
6 changes: 4 additions & 2 deletions Compiler/runtime/Makefile.common
Expand Up @@ -62,8 +62,10 @@ Print_rml.o : printimpl.c
Print_omc.o : printimpl.c
Error_rml.o : errorext.cpp ErrorMessage.hpp
Error_omc.o : errorext.cpp ErrorMessage.hpp ../OpenModelicaBootstrappingHeader.h
System_rml.o : systemimpl.c config.h errorext.h $(configUnix)
System_omc.o : systemimpl.c config.h errorext.h $(configUnix) $(RML_COMPAT)
System_rml.o : System_rml.c systemimpl.c config.h errorext.h $(configUnix)
$(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) -fopenmp $<
System_omc.o : System_omc.c systemimpl.c config.h errorext.h $(configUnix) $(RML_COMPAT)
$(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) -fopenmp $<
Lapack_rml.o : lapackimpl.c config.h $(configUnix)
Lapack_omc.o : lapackimpl.c config.h $(configUnix) $(RML_COMPAT)
IOStreamExt_rml.o : IOStreamExt.c
Expand Down
Expand Up @@ -28,8 +28,6 @@
*
*/

extern "C" {

#if defined(_MSC_VER)
#include <Windows.h>
#endif
Expand Down Expand Up @@ -666,5 +664,3 @@ extern const char* System_snprintff(const char *fmt, int len, double d)
buf[1023] = 0;
return buf;
}

}
7 changes: 7 additions & 0 deletions Compiler/runtime/System_rml.c
Expand Up @@ -792,6 +792,13 @@ RML_BEGIN_LABEL(System__systemCall)
}
RML_END_LABEL

RML_BEGIN_LABEL(System__systemCallParallel)
{
rmlA0 = SystemImpl__systemCallParallel(rmlA0);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(System__spawnCall)
{
const char* path = RML_STRINGDATA(rmlA0);
Expand Down
36 changes: 36 additions & 0 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -565,6 +565,42 @@ int SystemImpl__systemCall(const char* str)
return ret_val;
}

void* SystemImpl__systemCallParallel(void *lst)
{
void *tmp = lst;
int sz = 0, i;
char **calls;
int *results;
while (RML_NILHDR != RML_GETHDR(tmp)) {
sz++;
tmp = RML_CDR(tmp);
}
if (sz == 0) return mk_nil();
calls = (char**) malloc(sz*sizeof(char*));
assert(calls);
results = (int*) malloc(sz*sizeof(int));
assert(results);
tmp = lst;
sz = 0;
while (RML_NILHDR != RML_GETHDR(tmp)) {
calls[sz++] = RML_STRINGDATA(RML_CAR(tmp));
tmp = RML_CDR(tmp);
}
#pragma omp parallel for private(i) schedule(dynamic)
for (i=0; i<sz; i++) {
/* fprintf(stderr, "Starting call %s\n", calls[i]); */
results[i] = system(calls[i]);
/* fprintf(stderr, "Finished call %s=%d\n", calls[i], results[i]); */
}
free(calls);
free(results);
tmp = mk_nil();
for (i=sz-1; i>=0; i--) {
tmp = mk_cons(mk_icon(results[i]),tmp);
}
return tmp;
}

int SystemImpl__spawnCall(const char* path, const char* str)
{
int ret_val = -1;
Expand Down
9 changes: 7 additions & 2 deletions Examples/BuildModelRecursive.mos
@@ -1,6 +1,6 @@
// Note: Run with +g=MetaModelica

setCommandLineOptions("+g=Modelica");
setCommandLineOptions({"+g=Modelica","+d=nogen"});
OpenModelica.Scripting.Internal.Time.timerTick(OpenModelica.Scripting.Internal.Time.RT_CLOCK_USER_RESERVED);
log:="BuildModelRecursive.html";
MSLVersion:="3.1";
Expand Down Expand Up @@ -36,18 +36,23 @@ deleteClass(Modelica.Utilities);
system("rm -f Modelica");
system("ln -fs '"+getInstallationDirectoryPath()+"/lib/omlibrary/Modelica "+MSLVersion+"/' Modelica");
omc:=getInstallationDirectoryPath()+"/bin/omc";

a:={typeNameString(x) for x guard isExperiment(x) in getClassNames(recursive=true,sort=true)};
getErrorString();

// writeFile("x",sum(s + "\n" for s in a));

print("Number of classes to build: " + String(size(a,1)));
system("rm -f Modelica.* " + log);

/*** This is the stuff executed for each model ***/

min(writeFile(s + ".mos","
writeFile(\""+log+"\",\""+s+"\n\",append=true);
statFile := \""+s+".stat\";
writeFile(statFile,\"<tr><td bgcolor=\\\"#ff0000\\\">"+s+"</td></tr>\");getErrorString();
loadModel(Modelica,{\"3.1\"});"+loadModelicaTest+"
setCommandLineOptions(\"+d=nogen\");
res:=buildModel("+s+");
errFile:=\""+s+".err\";
simFile:=\""+s+".sim\";
Expand Down Expand Up @@ -98,7 +103,7 @@ str:=\"<tr><td>\" + (if err <> \"\" then \"<a href=\"+errFile+\">"+s+"</a>\" els
writeFile(statFile,str);getErrorString();
") for s in a);
getErrorString();
min(0==system("ulimit -t "+ulimitOmc+" ; " + omc + " " + s + ".mos") for s in a);
results := system_parallel({"ulimit -t "+ulimitOmc+" ; " + omc + " " + s + ".mos" for s in a});
getErrorString();

echo(false);
Expand Down
6 changes: 4 additions & 2 deletions configure.in
Expand Up @@ -578,9 +578,9 @@ else
fi

if test "Darwin" != `uname`; then
RT_LDFLAGS="$RT_LDFLAGS -lrt"
RT_LDFLAGS="$RT_LDFLAGS -lrt -fopenmp"
else
RT_LDFLAGS="$RT_LDFLAGS -liconv"
RT_LDFLAGS="$RT_LDFLAGS -liconv -fopenmp"
fi

dnl should we compile modpar
Expand Down Expand Up @@ -629,6 +629,8 @@ else
fi
AC_MSG_RESULT([$MODELICA_SPEC_PLATFORM])

CFLAGS="$CFLAGS"

date=`date "+%Y-%m-%d %H:%M:%S"`

GENERATED_AUTOCONF_FILES="Makefile \
Expand Down

0 comments on commit a7e56f7

Please sign in to comment.