Skip to content

Commit

Permalink
- Moved where CFLAGS for generated code are defined (in configure ins…
Browse files Browse the repository at this point in the history
…tead of logic in the code generation)

- Added configure env.vars RUNTIMECC and RUNTIMECFLAGS if the user wants different compilers for the generated code and omc itself (typically to use clang for generated code)
- Always include delay.h in generated simulation code. Revert previous change that made Counter and Counter3 work (relied on the wrong delayImpl being used)
- Moved performSimulation into the callback interface


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18262 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Nov 24, 2013
1 parent 1367106 commit 8ad3639
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 59 deletions.
4 changes: 2 additions & 2 deletions Compiler/BackEnd/SimCodeMain.mo
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ algorithm
(daeElements,literals) = SimCodeUtil.findLiterals(daeMainFunction::daeElements);
(mainFunction::fns, extraRecordDecls, includes, includeDirs, libs) = SimCodeUtil.elaborateFunctions(program, daeElements, metarecordTypes, literals, includes);
SimCodeUtil.checkValidMainFunction(name, mainFunction);
makefileParams = SimCodeUtil.createMakefileParams(includeDirs, libs);
makefileParams = SimCodeUtil.createMakefileParams(includeDirs, libs, true);
fnCode = SimCode.FUNCTIONCODE(name, SOME(mainFunction), fns, literals, includes, makefileParams, extraRecordDecls);
// Generate code
_ = Tpl.tplString(CodegenC.translateFunctions, fnCode);
Expand All @@ -615,7 +615,7 @@ algorithm
// Create SimCode.FunctionCode
(daeElements,literals) = SimCodeUtil.findLiterals(daeElements);
(fns, extraRecordDecls, includes, includeDirs, libs) = SimCodeUtil.elaborateFunctions(program, daeElements, metarecordTypes, literals, includes);
makefileParams = SimCodeUtil.createMakefileParams(includeDirs, libs);
makefileParams = SimCodeUtil.createMakefileParams(includeDirs, libs, true);
// remove OpenModelica.threadData.ThreadData
fns = removeThreadDataFunction(fns, {});
extraRecordDecls = removeThreadDataRecord(extraRecordDecls, {});
Expand Down
5 changes: 3 additions & 2 deletions Compiler/BackEnd/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,7 @@ algorithm

((uniqueEqIndex, algorithmAndEquationAsserts)) = BackendDAEUtil.foldEqSystem(dlow, createAlgorithmAndEquationAsserts, (uniqueEqIndex, {}));
discreteModelVars = BackendDAEUtil.foldEqSystem(dlow, extractDiscreteModelVars, {});
makefileParams = createMakefileParams(includeDirs, libs);
makefileParams = createMakefileParams(includeDirs, libs, false);
(delayedExps, maxDelayedExpIndex) = extractDelayedExpressions(dlow);

// append removed equation to all equations, since these are actually
Expand Down Expand Up @@ -5510,6 +5510,7 @@ end extractIdAndExpFromDelayExp;
public function createMakefileParams
input list<String> includes;
input list<String> libs;
input Boolean isFunction;
output SimCode.MakefileParams makefileParams;
protected
String omhome, ccompiler, cxxcompiler, linker, exeext, dllext, cflags, ldflags, rtlibs, platform, fopenmp;
Expand All @@ -5526,7 +5527,7 @@ algorithm
cflags := System.getCFlags();
cflags := Util.if_(stringEq(Config.simCodeTarget(),"JavaScript"),"-Os -Wno-warn-absolute-paths",cflags);
ldflags := System.getLDFlags();
rtlibs := System.getRTLibs();
rtlibs := Util.if_(isFunction, System.getRTLibs(), System.getRTLibsSim());
platform := System.modelicaPlatform();
makefileParams := SimCode.MAKEFILE_PARAMS(ccompiler, cxxcompiler, linker, exeext, dllext,
omhome, cflags, ldflags, rtlibs, includes, libs, platform);
Expand Down
13 changes: 6 additions & 7 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ template simulationFile_nls(SimCode simCode, String guid)
/* Non Linear Systems */
<%simulationFileHeader(simCode)%>
#include "<%simCode.fileNamePrefix%>_12jac.h"
#include "delay.h"
<%functionNonLinearResiduals(initialEquations)%>
<%functionNonLinearResiduals(inlineEquations)%>
<%functionNonLinearResiduals(parameterEquations)%>
Expand Down Expand Up @@ -303,7 +302,6 @@ template simulationFile_dly(SimCode simCode, String guid)
<<
/* Delay */
<%simulationFileHeader(simCode)%>
#include <delay.h>

<%functionStoreDelayed(delayedExps, modelNamePrefix(simCode))%>

Expand Down Expand Up @@ -484,8 +482,8 @@ template simulationFile(SimCode simCode, String guid)
/* Main Simulation File */
<%simulationFileHeader(simCode)%>

#define prefixedName_performSimulation <%symbolName(modelNamePrefixStr,"performSimulation")%>
#include <perform_simulation.c>
#include <delay.h>

/* dummy VARINFO and FILEINFO */
const FILE_INFO dummyFILE_INFO = omc_dummyFileInfo;
Expand Down Expand Up @@ -544,6 +542,7 @@ template simulationFile(SimCode simCode, String guid)
extern const char* <%symbolName(modelNamePrefixStr,"linear_model_frame")%>(void);
struct OpenModelicaGeneratedFunctionCallbacks <%symbolName(modelNamePrefixStr,"callback")%> = {
<%symbolName(modelNamePrefixStr,"performSimulation")%>,
<%symbolName(modelNamePrefixStr,"callExternalObjectConstructors")%>,
<%symbolName(modelNamePrefixStr,"callExternalObjectDestructors")%>,
<%symbolName(modelNamePrefixStr,"initialNonLinearSystem")%>,
Expand Down Expand Up @@ -632,6 +631,7 @@ template simulationFileHeader(SimCode simCode)
#include "simulation_runtime.h"
#include "omc_error.h"
#include "model_help.h"
#include "delay.h"

#include <assert.h>
#include <string.h>
Expand Down Expand Up @@ -4070,11 +4070,10 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
CFLAGS_BASED_ON_INIT_FILE=<%extraCflags%>
CFLAGS=$(CFLAGS_BASED_ON_INIT_FILE) <%makefileParams.cflags%> <%match sopt case SOME(s as SIMULATION_SETTINGS(__)) then '<%s.cflags%> ' /* From the simulate() command */%>
CPPFLAGS=-I"<%makefileParams.omhome%>/include/omc" -I. <%makefileParams.includes ; separator=" "%> -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME
LIBSIMULATIONRUNTIMEC=<% if boolAnd(boolNot(stringEq(os(), "OSX")), boolOr(acceptMetaModelicaGrammar(), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS))) then "-Wl,-whole-archive "%>-lSimulationRuntimeC <% if boolAnd(boolNot(stringEq(os(), "OSX")), boolOr(acceptMetaModelicaGrammar(), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS))) then " -Wl,-no-whole-archive"%> <% if stringEq(makefileParams.platform, "win32") then "" else " -ldl"%>
LDFLAGS=<%dirExtra%> <%
if stringEq(Config.simCodeTarget(),"JavaScript") then <<-L'<%makefileParams.omhome%>/lib/omc/emcc' -lblas -llapack -lexpat -lSimulationRuntimeC -lf2c -s TOTAL_MEMORY=536870912 -s MAX_SETJMPS=20000 -s OUTLINING_LIMIT=20000 --pre-js <%makefileParams.omhome%>/lib/omc/emcc/pre.js>>
else <<-L"<%makefileParams.omhome%>/lib/omc" -L"<%makefileParams.omhome%>/lib" -Wl,<% if stringEq(makefileParams.platform, "win32") then "--stack,0x2000000,"%>-rpath,"<%makefileParams.omhome%>/lib/omc" -Wl,-rpath,"<%makefileParams.omhome%>/lib" $(LIBSIMULATIONRUNTIMEC) -linteractive <%ParModelicaLibs%> <%makefileParams.ldflags%> <%makefileParams.runtimelibs%> <%match System.os() case "OSX" then "-lf2c -llis" else "-Wl,-Bstatic -lf2c -Wl,-Bdynamic -llis"%>>>
%> -lstdc++ -lm
else <<-L"<%makefileParams.omhome%>/lib/omc" -L"<%makefileParams.omhome%>/lib" -Wl,<% if stringEq(makefileParams.platform, "win32") then "--stack,0x2000000,"%>-rpath,"<%makefileParams.omhome%>/lib/omc" -Wl,-rpath,"<%makefileParams.omhome%>/lib" <%ParModelicaLibs%> <%makefileParams.ldflags%> <%makefileParams.runtimelibs%>>>
%>
MAINFILE=<%fileNamePrefix%>.c
MAINOBJ=<%fileNamePrefix%>.o
CFILES=<%fileNamePrefix%>_functions.c <%fileNamePrefix%>_records.c \
Expand Down Expand Up @@ -4309,7 +4308,7 @@ case FUNCTIONCODE(makefileParams=MAKEFILE_PARAMS(__)) then
EXEEXT=<%makefileParams.exeext%>
DLLEXT=<%makefileParams.dllext%>
CFLAGS= -I"<%makefileParams.omhome%>/include/omc" <%makefileParams.includes ; separator=" "%> <%makefileParams.cflags%>
LDFLAGS= -L"<%makefileParams.omhome%>/lib/omc" -Wl,-rpath,'<%makefileParams.omhome%>/lib/omc' -lOpenModelicaRuntimeC <%ParModelicaLibs%> <%makefileParams.ldflags%> <%makefileParams.runtimelibs%>
LDFLAGS= -L"<%makefileParams.omhome%>/lib/omc" -Wl,-rpath,'<%makefileParams.omhome%>/lib/omc' <%ParModelicaLibs%> <%makefileParams.ldflags%> <%makefileParams.runtimelibs%>
PERL=perl
MAINFILE=<%name%>.c

Expand Down
6 changes: 6 additions & 0 deletions Compiler/Util/System.mo
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,12 @@ public function getRTLibs
external "C" libs=System_getRTLibs() annotation(Library = "omcruntime");
end getRTLibs;

public function getRTLibsSim
"Returns a string containing the compiler flags used for simulation real-time libraries"
output String libs;
external "C" libs=System_getRTLibsSim() annotation(Library = "omcruntime");
end getRTLibsSim;

public function getCorbaLibs
"Returns a string containing the compiler flags used for Corba libraries.
Needed for annotation(Library=\"OpenModelicaCorba\"), a library with special
Expand Down
5 changes: 5 additions & 0 deletions Compiler/runtime/System_omc.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ extern const char* System_getRTLibs()
return LDFLAGS_RT;
}

extern const char* System_getRTLibsSim()
{
return LDFLAGS_RT_SIM;
}

extern const char* System_getCCompiler()
{
return GC_strdup(cc);
Expand Down
7 changes: 7 additions & 0 deletions Compiler/runtime/System_rml.c
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,13 @@ RML_BEGIN_LABEL(System__getRTLibs)
}
RML_END_LABEL

RML_BEGIN_LABEL(System__getRTLibsSim)
{
rmlA0 = (void*) mk_scon(LDFLAGS_RT_SIM);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(System__getCorbaLibs)
{
rmlA0 = (void*) mk_scon(CONFIG_CORBALIBS);
Expand Down
6 changes: 4 additions & 2 deletions Compiler/runtime/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
#endif

/* adrpo: add -loleaut32 as is used by ExternalMedia */
#define LDFLAGS_RT " -static-libgcc -luuid -loleaut32 -lole32 -lws2_32 -lsundials_kinsol -lsundials_nvecserial -lipopt -lcoinmumps -lcoinmetis -lpthread -lm -lgfortranbegin -lgfortran -lmingw32 -lgcc_eh -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -llapack-mingw -ltmglib-mingw -lblas-mingw -lf2c"
#define BASIC_LDFLAGS_RT " -static-libgcc -luuid -loleaut32 -lole32 -lws2_32 -lsundials_kinsol -lsundials_nvecserial -lipopt -lcoinmumps -lcoinmetis -lpthread -lm -lgfortranbegin -lgfortran -lmingw32 -lgcc_eh -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -llapack-mingw -ltmglib-mingw -lblas-mingw -lf2c"
#define LDFLAGS_RT " -lOpenModelicaRuntimeC" BASIC_LDFLAGS_RT
#define LDFLAGS_RT_SIM " -lSimulationRuntimeC" BASIC_LDFLAGS_RT " -llis -lstdc++"
#define CONFIG_EXE_EXT ".exe"
#define CONFIG_DLL_EXT ".dll"
#define CONFIG_OS "Windows_NT"
Expand Down Expand Up @@ -100,7 +102,7 @@

#else /* Unix */

#define DEFAULT_LDFLAGS "-lexpat"
#define DEFAULT_LDFLAGS ""

#include "config.unix.h"

Expand Down
20 changes: 8 additions & 12 deletions Compiler/runtime/config.unix.h.in
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
/* @configure_input@ */
#define CONFIGURE_COMMANDLINE "Configured @date@ using arguments: '@CONFIGURE_ARGS@'"
#define LDFLAGS_RT "@LDFLAGS@ @RT_LDFLAGS@ @SUNDIALS_LDFLAGS@ @IPOPT_LDFLAGS@ -lpthread"
#define LDFLAGS_RT "@LDFLAGS@ @RT_LDFLAGS_GENERATED_CODE@"
#define LDFLAGS_RT_SIM "@LDFLAGS@ @RT_LDFLAGS_GENERATED_CODE_SIM@"
#define CONFIG_EXE_EXT ""
#define CONFIG_DLL_EXT "@SHREXT@"
#define CONFIG_PLATFORM "Unix"
#define CONFIG_MODELICA_SPEC_PLATFORM "@MODELICA_SPEC_PLATFORM@"
#define CONFIG_OPENMODELICA_SPEC_PLATFORM "@OPENMODELICA_SPEC_PLATFORM@"

#define DEFAULT_CC "@CC@"
#define DEFAULT_CC "@RUNTIMECC@"
#define DEFAULT_CXX "@CXX@"
#define DEFAULT_OMPCC "@OMPCC@"
#define DEFAULT_MAKE "@MAKE@"

#if defined(__sparc__)
#define DEFAULT_LINKER "@CC@ -G"
#define DEFAULT_LINKER "@RUNTIMECC@ -G"
#elif defined(__APPLE_CC__)
#define DEFAULT_LINKER "@CC@ -single_module -dynamiclib -flat_namespace"
#define DEFAULT_LINKER "@RUNTIMECC@ -single_module -dynamiclib -flat_namespace"
#elif defined(__x86_64__)
/* -fPIC needed on x86_64! */
#define DEFAULT_LINKER "@CC@ -shared"
#define DEFAULT_LINKER "@RUNTIMECC@ -shared"
#else
#define DEFAULT_LINKER "@CC@ -shared"
#define DEFAULT_LINKER "@RUNTIMECC@ -shared"
#endif

#define DEFAULT_CFLAGS "@RUNTIMECFLAGS@ ${MODELICAUSERCFLAGS}"
#ifdef __APPLE_CC__
#define DEFAULT_CFLAGS "${SIM_OR_DYNLOAD_OPT_LEVEL} -falign-functions -msse2 -mfpmath=sse ${MODELICAUSERCFLAGS}"
#define CONFIG_OS "OSX"
#else
#if defined(__x86_64__)
#define DEFAULT_CFLAGS "${SIM_OR_DYNLOAD_OPT_LEVEL} -falign-functions -march=native -mfpmath=sse -fPIC ${MODELICAUSERCFLAGS}"
#else
#define DEFAULT_CFLAGS "${SIM_OR_DYNLOAD_OPT_LEVEL} -falign-functions -march=native -mfpmath=sse ${MODELICAUSERCFLAGS}"
#endif
#define CONFIG_OS "linux"
#endif

Expand Down
12 changes: 8 additions & 4 deletions SimulationRuntime/c/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ libSimulationRuntimeC.a: $(ALL_OBJS)

libSimulationRuntimeC.so: $(ALL_OBJS)
@rm -f $@
$(CC) -shared -o $@ $(ALL_OBJS) $(LDFLAGS)
$(CC) -shared -o $@ $(ALL_OBJS) $(LDFLAGS_SIM)

libSimulationRuntimeC.dylib: $(ALL_OBJS) install-f2c
@rm -f $@
$(CC) -shared -o $@ $(ALL_OBJS) $(LDFLAGS) -lf2c -undefined dynamic_lookup -install_name '@rpath/$@'
$(CC) -shared -o $@ $(ALL_OBJS) $(LDFLAGS_SIM) -undefined dynamic_lookup -install_name '@rpath/$@'

libSimulationRuntimeC.bc: $(ALL_OBJS)
@rm -f $@
Expand All @@ -112,7 +112,7 @@ libOpenModelicaRuntimeC.a: $(BASE_OBJS)

libOpenModelicaRuntimeC.so: $(BASE_OBJS)
@rm -f $@
$(CC) -shared -o $@ $(BASE_OBJS)
$(CC) -shared -o $@ $(BASE_OBJS) $(LDFLAGS)

libOpenModelicaFMIRuntimeC.a: $(FMIOBJSPATH)
@rm -f $@
Expand All @@ -121,7 +121,11 @@ libOpenModelicaFMIRuntimeC.a: $(FMIOBJSPATH)

libOpenModelicaFMIRuntimeC.so: $(FMIOBJSPATH)
@rm -f $@
$(CC) -shared -o $@ $(FMIOBJSPATH)
$(CC) -shared -o $@ $(FMIOBJSPATH) $(LDFLAGS)

libOpenModelicaFMIRuntimeC.dylib: $(FMIOBJSPATH)
@rm -f $@
$(CC) -shared -o $@ $(FMIOBJSPATH) $(LDFLAGS_SIM) -undefined dynamic_lookup -install_name '@rpath/$@'

$(METAOBJSPATH):%$(OBJ_EXT): %.c $(METAHFILESPATH) $(COMMON_HEADERS)
$(CC) -c $(CFLAGS) -o $@ $< -I$(METAPATH) -I$(UTILPATH)
Expand Down
3 changes: 2 additions & 1 deletion SimulationRuntime/c/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ CXX = @CXX@
CONFIG_CPPFLAGS = @CPPFLAGS@
CONFIG_CFLAGS = @CFLAGS@ @IPOPT_CFLAGS@
FPMATHFORTRAN = @FPMATHFORTRAN@
LDFLAGS=-L$(top_builddir)/build/lib/omc -L../interactive @LDFLAGS@ @RT_LDFLAGS@ @SUNDIALS_LDFLAGS@ @IPOPT_LDFLAGS@ -linteractive -llis -lpthread -lstdc++
LDFLAGS=-L$(top_builddir)/build/lib/omc @LDFLAGS@ @RT_LDFLAGS@
LDFLAGS_SIM=-L$(top_builddir)/build/lib/omc -L../interactive@RT_LDFLAGS_SIM@

LIBMAKEFILE = Makefile
INTERACTIVELIBS = "-lpthread"
Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/c/openmodelica_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ extern "C" {
#include "utility.h"

struct OpenModelicaGeneratedFunctionCallbacks {
int (*performSimulation)(DATA* data, void* solverInfo);
/* Function for calling external object constructors */
void (*callExternalObjectConstructors)(DATA *data);
/* Function for calling external object deconstructors */
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/solver/perform_simulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void updateContinuousSystem(DATA *data)
*
* This function performs the simulation controlled by solverInfo.
*/
int performSimulation(DATA* data, SOLVER_INFO* solverInfo)
int prefixedName_performSimulation(DATA* data, SOLVER_INFO* solverInfo)
{

int retValIntegrator = 0;
Expand Down
18 changes: 1 addition & 17 deletions SimulationRuntime/c/simulation/solver/solver_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,22 +388,6 @@ int initializeModel(DATA* data, const char* init_initMethod,
}


/*! \fn performSimulation(DATA* data, SOLVER_INFO* solverInfo)
*
* \param [ref] [data]
* \param [ref] [solverInfo]
*
* This function performs the simulation controlled by solverInfo.
*/

/*!
* Moved to perform_simulation.c and omp_perform_simulation.c
* and included in the generrated code. The things we do for
* OPENMP.
*/
/* int performSimulation(DATA* data, SOLVER_INFO* solverInfo) */


/*! \fn finishSimulation(DATA* data, SOLVER_INFO* solverInfo)
*
* \param [ref] [data]
Expand Down Expand Up @@ -591,7 +575,7 @@ int solver_main(DATA* data, const char* init_initMethod,
}

INFO2(LOG_SOLVER, "Start numerical solver from %g to %g", simInfo->startTime, simInfo->stopTime);
retVal = performSimulation(data, &solverInfo);
retVal = data->callback->performSimulation(data, &solverInfo);
omc_alloc_interface.collect_a_little();
/* terminate the simulation */
finishSimulation(data, &solverInfo, outputVariablesAtEnd);
Expand Down
3 changes: 0 additions & 3 deletions SimulationRuntime/c/simulation/solver/solver_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ extern int initializeModel(DATA* data, const char* init_initMethod,
const char* init_optiMethod, const char* init_file, double init_time,
int lambda_steps);

/* Defined in perform_simulation.c and omp_perform_simulation.c */
extern int performSimulation(DATA* data, SOLVER_INFO* solverInfo);

extern int finishSimulation(DATA* data, SOLVER_INFO* solverInfo, const char* outputVariablesAtEnd);

extern int solver_main_step(DATA* data, SOLVER_INFO* solverInfo);
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/interactive/omi_ServiceInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ int performSolverStepFromOM(double start, double stop, double stepSize)
simInfo->stopTime = stop;

/* starts the simulation main loop */
retVal = performSimulation(globalData, solverInfo);
retVal = globalData->callback->performSimulation(globalData, solverInfo);

gdMutex.Unlock();
return retVal;
Expand Down

0 comments on commit 8ad3639

Please sign in to comment.