Skip to content

Commit

Permalink
+ Added built-in ParModelica functions.
Browse files Browse the repository at this point in the history
+ fix parallelism propagation.
+ temporary fix on simulation settings for ParModelica case.
+ initialize OpenCL env only when needed.
+ sum fixes.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12639 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
mahge committed Aug 22, 2012
1 parent f6b3e26 commit 680facc
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 49 deletions.
32 changes: 24 additions & 8 deletions Compiler/FrontEnd/Builtin.mo
Expand Up @@ -646,7 +646,7 @@ public function getInitialFunctions
algorithm
initialProgram := matchcontinue ()
local
String fileModelica,fileMetaModelica,initialFunctionStr,initialFunctionStrMM;
String fileModelica,fileMetaModelica,fileParModelica,initialFunctionStr,initialFunctionStrMM;
list<tuple<Boolean,Absyn.Program>> assocLst;
list<Absyn.Class> classes,classes1,classes2;
case ()
Expand All @@ -660,27 +660,43 @@ algorithm
then Util.assoc(Config.acceptMetaModelicaGrammar(), assocLst);
case ()
equation
false = Config.acceptMetaModelicaGrammar();
true = Config.acceptMetaModelicaGrammar();
fileModelica = Settings.getInstallationDirectoryPath() +& "/lib/omc/ModelicaBuiltin.mo";
fileMetaModelica = Settings.getInstallationDirectoryPath() +& "/lib/omc/MetaModelicaBuiltin.mo";
initialFunctionStr = System.readFile(fileModelica);
initialProgram = Parser.parsebuiltinstring(initialFunctionStr, fileModelica);
initialFunctionStrMM = System.readFile(fileMetaModelica);
Absyn.PROGRAM(classes=classes1,within_=Absyn.TOP()) = Parser.parsebuiltinstring(initialFunctionStr, fileModelica);
Absyn.PROGRAM(classes=classes2,within_=Absyn.TOP()) = Parser.parsebuiltinstring(initialFunctionStrMM, fileMetaModelica);
classes = listAppend(classes1,classes2);
initialProgram = Absyn.PROGRAM(classes,Absyn.TOP(),Absyn.dummyTimeStamp);
assocLst = getGlobalRoot(Global.builtinIndex);
setGlobalRoot(Global.builtinIndex, (false,initialProgram)::assocLst);
setGlobalRoot(Global.builtinIndex, (true,initialProgram)::assocLst);
then initialProgram;
case ()
equation
true = Config.acceptMetaModelicaGrammar();
true = Config.acceptParModelicaGrammar();
fileModelica = Settings.getInstallationDirectoryPath() +& "/lib/omc/ModelicaBuiltin.mo";
fileMetaModelica = Settings.getInstallationDirectoryPath() +& "/lib/omc/MetaModelicaBuiltin.mo";
fileParModelica = Settings.getInstallationDirectoryPath() +& "/lib/omc/ParModelicaBuiltin.mo";
initialFunctionStr = System.readFile(fileModelica);
initialFunctionStrMM = System.readFile(fileMetaModelica);
initialFunctionStrMM = System.readFile(fileParModelica);
Absyn.PROGRAM(classes=classes1,within_=Absyn.TOP()) = Parser.parsebuiltinstring(initialFunctionStr, fileModelica);
Absyn.PROGRAM(classes=classes2,within_=Absyn.TOP()) = Parser.parsebuiltinstring(initialFunctionStrMM, fileMetaModelica);
Absyn.PROGRAM(classes=classes2,within_=Absyn.TOP()) = Parser.parsebuiltinstring(initialFunctionStrMM, fileParModelica);
classes = listAppend(classes1,classes2);
initialProgram = Absyn.PROGRAM(classes,Absyn.TOP(),Absyn.dummyTimeStamp);
assocLst = getGlobalRoot(Global.builtinIndex);
setGlobalRoot(Global.builtinIndex, (true,initialProgram)::assocLst);
then initialProgram;
case ()
equation
false = Config.acceptMetaModelicaGrammar();
false = Config.acceptParModelicaGrammar();

fileModelica = Settings.getInstallationDirectoryPath() +& "/lib/omc/ModelicaBuiltin.mo";
initialFunctionStr = System.readFile(fileModelica);
initialProgram = Parser.parsebuiltinstring(initialFunctionStr, fileModelica);
assocLst = getGlobalRoot(Global.builtinIndex);
setGlobalRoot(Global.builtinIndex, (false,initialProgram)::assocLst);
then initialProgram;
else
equation
Error.addMessage(Error.INTERNAL_ERROR, {"Builtin.getInitialFunctions failed."});
Expand Down
2 changes: 2 additions & 0 deletions Compiler/FrontEnd/SCode.mo
Expand Up @@ -3474,6 +3474,8 @@ protected function propagateParallelism
output Parallelism p;
algorithm
p := matchcontinue(p1,p2)
case(p1,NON_PARALLEL())
then p1;
case(p1,p2)
equation
true = parallelismEqual(p1,p2);
Expand Down
18 changes: 18 additions & 0 deletions Compiler/Main/Main.mo
Expand Up @@ -819,6 +819,7 @@ algorithm
case (dlow,classname,ap,dae) /* classname ass1 ass2 blocks */
equation
true = Config.simulationCg();
false = Config.acceptParModelicaGrammar();
Print.clearErrorBuf();
Print.clearBuf();
cname_str = Absyn.pathString(classname);
Expand All @@ -827,6 +828,23 @@ algorithm
Debug.execStat("Codegen Done",CevalScript.RT_CLOCK_EXECSTAT_MAIN);
then
();

// If accepting parModelica create a slightly different default settings.
// Temporary solution for now since Intel OpenCL dll calls hang.
// So use simple Models and call the needed functions.
case (dlow,classname,ap,dae) /* classname ass1 ass2 blocks */
equation
true = Config.simulationCg();
true = Config.acceptParModelicaGrammar();
Print.clearErrorBuf();
Print.clearBuf();
cname_str = Absyn.pathString(classname);
simSettings = SimCode.createSimulationSettings(0.0, 1.0, 1, 1e-6,"dassl","","plt",".*",false,"");
(_,_,_,_,_,_) = SimCode.generateModelCode(dlow,ap,dae,classname,cname_str,SOME(simSettings),Absyn.FUNCTIONARGS({},{}));
Debug.execStat("Codegen Done",CevalScript.RT_CLOCK_EXECSTAT_MAIN);
then
();

/* If not generating simulation code: Succeed so no error messages are printed */
else
equation
Expand Down
34 changes: 16 additions & 18 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -2227,12 +2227,6 @@ template simulationFunctionsFile(String filePrefix, list<Function> functions, li
extern "C" {
#endif
<%if acceptParModelicaGrammar() then
<< /* the OpenCL program. Made global to avoid repeated builds */
cl_program ocl_kernels_program = ocl_build_p_from_src("<%filePrefix%>_kernels.cl", true);
>>
%>
<%literals |> literal hasindex i0 fromindex 0 => literalExpConst(literal,i0) ; separator="\n";empty%>
<%functionBodies(functions)%>
Expand Down Expand Up @@ -2308,7 +2302,7 @@ template simulationFunctionsHeaderFile(String filePrefix, list<Function> functio
<<
#ifndef <%stringReplace(filePrefix,".","_")%>__H
#define <%stringReplace(filePrefix,".","_")%>__H
<%commonHeader()%>
<%commonHeader(filePrefix)%>
#include "simulation_runtime.h"
#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -2501,7 +2495,7 @@ template initVal(Exp initialValue)
else error(sourceInfo(), 'initial value of unknown type: <%printExpStr(initialValue)%>')
end initVal;

template commonHeader()
template commonHeader(String filePrefix)
::=
<<
<% if acceptMetaModelicaGrammar() then "#define __OPENMODELICA__METAMODELICA"%>
Expand All @@ -2511,7 +2505,16 @@ template commonHeader()
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
<%if acceptParModelicaGrammar() then '#include <omc_ocl_interface.h>'%>
<%if acceptParModelicaGrammar() then
<<
#include <omc_ocl_interface.h>
/* the OpenCL Kernels file name needed in libOMOCLRuntime.a */
const char* omc_ocl_kernels_source = "<%filePrefix%>_kernels.cl";
/* the OpenCL program. Made global to avoid repeated builds */
extern cl_program omc_ocl_program;

>>
%>

>>
end commonHeader;
Expand All @@ -2534,11 +2537,6 @@ template functionsFile(String filePrefix,
#define MODELICA_ASSERT(info,msg) { printInfo(stderr,info); fprintf(stderr,"Modelica Assert: %s!\n", msg); fflush(NULL); }
#define MODELICA_TERMINATE(msg) { fprintf(stderr,"Modelica Terminate: %s!\n", msg); fflush(NULL); }

<%if acceptParModelicaGrammar() then
<< /* the OpenCL program. Made global to avoid repeated builds */
cl_program ocl_kernels_program = ocl_build_p_from_src("<%filePrefix%>_kernels.cl", true);
>>
%>

<%match mainFunction case SOME(fn) then functionBody(fn,true)%>
<%functionBodies(functions)%>
Expand All @@ -2557,7 +2555,7 @@ template functionsHeaderFile(String filePrefix,
<<
#ifndef <%stringReplace(filePrefix,".","_")%>__H
#define <%stringReplace(filePrefix,".","_")%>__H
<%commonHeader()%>
<%commonHeader(filePrefix)%>
#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -3113,7 +3111,7 @@ case tupleVar as ((cref as CREF_IDENT(identType = T_ARRAY(__)),_)) then
case identType as T_ARRAY(ty = T_INTEGER(__)) then
'__global modelica_integer* data_<%varName%>,<%\n%>__global modelica_integer* info_<%varName%>'
case identType as T_ARRAY(ty = T_REAL(__)) then
'__global modelica_integer* data_<%varName%>,<%\n%>__global modelica_integer* info_<%varName%>'
'__global modelica_real* data_<%varName%>,<%\n%>__global modelica_integer* info_<%varName%>'

else 'Tempalte error in parFunArgDefinitionFromLooptupleVar'

Expand Down Expand Up @@ -3682,7 +3680,7 @@ case KERNEL_FUNCTION(__) then
/* functionBodyKernelFunctionInterface : <%fname%> Kernel creation and execution */
int <%kernel_arg_number%> = 0;
<%cl_kernelVar%> = ocl_create_kernel(ocl_kernels_program, "_<%fname%>");
<%cl_kernelVar%> = ocl_create_kernel(omc_ocl_program, "_<%fname%>");
<%kernelArgSets%>
ocl_execute_kernel(<%cl_kernelVar%>);
clReleaseKernel(<%cl_kernelVar%>);
Expand Down Expand Up @@ -5199,7 +5197,7 @@ case RANGE(__) then
<<
<%preExp%>
<%startVar%> = <%startValue%>; <%stepVar%> = <%stepValue%>; <%stopVar%> = <%stopValue%>;
<%cl_kernelVar%> = ocl_create_kernel(ocl_kernels_program, "<%parforKernelName%>");
<%cl_kernelVar%> = ocl_create_kernel(omc_ocl_program, "<%parforKernelName%>");
int <%kerArgNr%> = 0;

ocl_set_kernel_arg(<%cl_kernelVar%>, <%kerArgNr%>, <%startVar%>); ++<%kerArgNr%>; <%\n%>
Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/ParModelica/OpenCLRuntime/Makefile
Expand Up @@ -28,6 +28,7 @@ transfer: libOMOCLRuntime.a
$(COPY) omc_ocl_memory_ops.h $(OPENMODELICA_INC)
$(COPY) libOMOCLRuntime.a $(OPENMODELICA_LIB)
$(COPY) $(OPENLC_LIB) $(OPENMODELICA_LIB)
$(COPY) ParModelicaBuiltin.mo $(OPENMODELICA_LIB)
mkdir -p $(OPENMODELICA_INC)/CL/
$(COPY) ./CL/* $(OPENMODELICA_INC)/CL/
$(COPY) OCLRuntimeUtil_new.cl $(OPENMODELICAHOME)/include/omc/
Expand Down
Expand Up @@ -112,7 +112,7 @@ modelica_integer oclGetGlobalId(modelica_integer dim) {
}



#define oclGlobalBarrier() barrier(CLK_GLOBAL_MEM_FENCE)



Expand Down Expand Up @@ -198,6 +198,8 @@ void copy_real_array_data(real_array *source, real_array *dest)
#define integer_array_element_addr_c99_1(L, d, i) (((L)->data) + i - 1)
#define integer_array_element_addr_c99_2(L, d, i, j) ((((L)->data)) + (i - 1)*((L)->dim_size[1]) + (j - 1))

#define real_array_element_addr_c99_1(L, d, i) (((L)->data) + i - 1)
#define real_array_element_addr_c99_2(L, d, i, j) ((((L)->data)) + (i - 1)*((L)->dim_size[1]) + (j - 1))



Expand Down
112 changes: 112 additions & 0 deletions SimulationRuntime/ParModelica/OpenCLRuntime/ParModelicaBuiltin.mo
@@ -0,0 +1,112 @@


// package ParModelica

function oclGetWorkDim
output Integer dim;
external "builtin";
end oclGetWorkDim;

function oclGetGlobalSize
input Integer dim;
output Integer size;
external "builtin";
end oclGetGlobalSize;

function oclGetGlobalId
input Integer dim;
output Integer id;
external "builtin";
end oclGetGlobalId;

function oclGetLocalSize
input Integer dim;
output Integer size;
external "builtin";
end oclGetLocalSize;

function oclGetLocalId
input Integer dim;
output Integer id;
external "builtin";
end oclGetLocalId;

function oclGetNumGroups
input Integer dim;
output Integer num;
external "builtin";
end oclGetNumGroups;

function oclGetGroupId
input Integer dim;
output Integer id;
external "builtin";
end oclGetGroupId;

function oclGlobalBarrier
external "builtin";
end oclGlobalBarrier;

function oclSetNumThreadsOnlyGlobal
input Integer num_threads;
external "builtin";
end oclSetNumThreadsOnlyGlobal;

function oclSetNumThreadsGlobalLocal
input Integer global_num_threads;
input Integer local_num_threads;
external "builtin";
end oclSetNumThreadsGlobalLocal;

function oclSetNumThreadsGlobalLocal1D
input Integer[1] global_num_threads;
input Integer[1] local_num_threads;
external "builtin";
end oclSetNumThreadsGlobalLocal1D;

function oclSetNumThreadsGlobalLocal2D
input Integer[2] global_num_threads;
input Integer[2] local_num_threads;
external "builtin";
end oclSetNumThreadsGlobalLocal2D;

function oclSetNumThreadsGlobalLocal3D
input Integer[3] global_num_threads;
input Integer[3] local_num_threads;
external "builtin";
end oclSetNumThreadsGlobalLocal3D;

function oclSetNumThreadsGlobalLocalError
input Integer[:] global_num_threads;
input Integer[:] local_num_threads;
external "builtin";
end oclSetNumThreadsGlobalLocalError;

function oclSetNumThreads = overload(
oclSetNumThreadsOnlyGlobal,
oclSetNumThreadsGlobalLocal1D,
oclSetNumThreadsGlobalLocal2D,
oclSetNumThreadsGlobalLocal3D
);




encapsulated package OpenCL

function matrixMult
parglobal input Integer A[:,:];
parglobal input Integer B[:,:];
parglobal output Integer C[:,:];
external "builtin";
end matrixMult;

function matrixTranspose
parglobal input Integer A[:,:];
parglobal output Integer B[:,:];
external "builtin";
end matrixTranspose;

end OpenCL;

// end ParModelica;
Expand Up @@ -67,7 +67,9 @@ size_t device_array_nr_of_elements(device_array *a);
// Just to stick to OpenModelica's function naming pattern
#define oclSetNumThreadsOnlyGlobal(...) ocl_set_num_threads( __VA_ARGS__ )
#define oclSetNumThreadsGlobalLocal(...) ocl_set_num_threads( __VA_ARGS__ )
#define oclSetNumThreadsGlobalLocalArrays(...) ocl_set_num_threads( __VA_ARGS__ )
#define oclSetNumThreadsGlobalLocal1D(...) ocl_set_num_threads( __VA_ARGS__ )
#define oclSetNumThreadsGlobalLocal2D(...) ocl_set_num_threads( __VA_ARGS__ )
#define oclSetNumThreadsGlobalLocal3D(...) ocl_set_num_threads( __VA_ARGS__ )


// sets the number of threads for subsequent parallel operations
Expand Down

0 comments on commit 680facc

Please sign in to comment.