Skip to content

Commit

Permalink
+ Added a flag and API to specify default OpenCL device.
Browse files Browse the repository at this point in the history
+ Free dangling array after returning from a function. (y)
+ Show device selection only if default device is not set.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12682 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
mahge committed Aug 27, 2012
1 parent 801a933 commit 35938d9
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 50 deletions.
17 changes: 17 additions & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,23 @@ algorithm
annotation(preferredView="text");
end setVectorizationLimit;

public function getDefaultOpenCLDevice
"Returns the id for the default OpenCL device to be used."
output Integer defdevid;
external "builtin";
annotation(preferredView="text");
end getDefaultOpenCLDevice;

public function setDefaultOpenCLDevice
"Sets the default OpenCL device to be used."
input Integer defdevid;
output Boolean success;
annotation(__OpenModelica_EarlyInline = true);
algorithm
success := setCommandLineOptions("+o=" + String(defdevid));
annotation(preferredView="text");
end setDefaultOpenCLDevice;

function setShowAnnotations
input Boolean show;
output Boolean success;
Expand Down
35 changes: 34 additions & 1 deletion Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2512,7 +2512,8 @@ template commonHeader(String filePrefix)
const char* omc_ocl_kernels_source = "<%filePrefix%>_kernels.cl";
/* the OpenCL program. Made global to avoid repeated builds */
extern cl_program omc_ocl_program;

/* The default OpenCL device. If not set (=0) show the selection option.*/
unsigned int default_ocl_device = <%getDefaultOpenCLDevice()%>;
>>
%>

Expand Down Expand Up @@ -4906,6 +4907,20 @@ case STMT_ASSIGN_ARR(exp=RANGE(__), componentRef=cr, type_=t) then
<<
<%fillArrayFromRange(t,exp,cr,context,&varDecls)%>
>>
case STMT_ASSIGN_ARR(exp=e as CALL(__), componentRef=cr, type_=t) then
let &preExp = buffer "" /*BUFD*/
let expPart = daeExp(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let ispec = indexSpecFromCref(cr, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
if ispec then
<<
<%preExp%>
<%indexedAssign(t, expPart, cr, ispec, context, &varDecls)%>
>>
else
<<
<%preExp%>
<%copyArrayDataAndFreeMemAfterCall(t, expPart, cr, context)%>
>>
case STMT_ASSIGN_ARR(exp=e, componentRef=cr, type_=t) then
let &preExp = buffer "" /*BUFD*/
let expPart = daeExp(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
Expand Down Expand Up @@ -4973,6 +4988,24 @@ template copyArrayData(DAE.Type ty, String exp, DAE.ComponentRef cr,
else
'copy_<%type%>_data_mem(&<%exp%>, &<%cref%>);'
end copyArrayData;

template copyArrayDataAndFreeMemAfterCall(DAE.Type ty, String exp, DAE.ComponentRef cr,

Context context)
::=
let type = expTypeArray(ty)
let cref = contextArrayCref(cr, context)
match context
case FUNCTION_CONTEXT(__) then
<<
copy_<%type%>_data(&<%exp%>, &<%cref%>);
<%if acceptParModelicaGrammar() then 'free_device_array(&<%exp%>);'%>
>>
case PARALLEL_FUNCTION_CONTEXT(__) then
'copy_<%type%>_data(&<%exp%>, &<%cref%>);'
else
'copy_<%type%>_data_mem(&<%exp%>, &<%cref%>);'
end copyArrayDataAndFreeMemAfterCall;

template algStmtTupleAssign(DAE.Statement stmt, Context context,
Text &varDecls /*BUFP*/)
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2284,6 +2284,12 @@ package Config
function getRunningTestsuite
output Boolean runningTestsuite;
end getRunningTestsuite;

function getDefaultOpenCLDevice
"Returns the id for the default OpenCL device to be used."
output Integer defdevid;
end getDefaultOpenCLDevice;

end Config;

package Flags
Expand Down
14 changes: 14 additions & 0 deletions Compiler/Util/Config.mo
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ algorithm
Flags.setConfigInt(Flags.VECTORIZATION_LIMIT, limit);
end setVectorizationLimit;

public function getDefaultOpenCLDevice
"Returns the id for the default OpenCL device to be used."
output Integer defdevid;
algorithm
defdevid := Flags.getConfigInt(Flags.DEFAULT_OPENCL_DEVICE);
end getDefaultOpenCLDevice;

public function setDefaultOpenCLDevice
"Sets the default OpenCL device to be used."
input Integer defdevid;
algorithm
Flags.setConfigInt(Flags.DEFAULT_OPENCL_DEVICE, defdevid);
end setDefaultOpenCLDevice;

public function showAnnotations
output Boolean show;
algorithm
Expand Down
6 changes: 5 additions & 1 deletion Compiler/Util/Flags.mo
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,9 @@ constant ConfigFlag PLOT_SILENT = CONFIG_FLAG(40, "plotSilent",
constant ConfigFlag LOCALE_FLAG = CONFIG_FLAG(41, "locale",
NONE(), EXTERNAL(), STRING_FLAG(""), NONE(),
Util.gettext("Override the locale from the environment."));
constant ConfigFlag DEFAULT_OPENCL_DEVICE = CONFIG_FLAG(42, "defaultOCLDevice",
SOME("o"), EXTERNAL(), INT_FLAG(0), NONE(),
Util.gettext("Sets the default OpenCL device to be used for parallel execution."));

// 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 initialisation so that all flags are
Expand Down Expand Up @@ -734,7 +737,8 @@ constant list<ConfigFlag> allConfigFlags = {
REDUCE_TERMS,
REDUCTION_METHOD,
PLOT_SILENT,
LOCALE_FLAG
LOCALE_FLAG,
DEFAULT_OPENCL_DEVICE
};

public function new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,19 @@ void alloc_real_array(device_real_array *dest, int ndims, ...){
}


void free_device_array(device_array *dest){
void free_device_array(device_array* dest){
clReleaseMemObject(dest->data);
clReleaseMemObject(dest->info_dev);
free(dest->info);
}

// This is just overloaded to allow the device arrays
// be freed properly.
void free_device_array(base_array_t* dest){
}




void copy_real_array_data(device_real_array* dev_array_ptr, real_array_t* host_array_ptr){

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ modelica_integer size_of_dimension_integer_array(device_integer_array dev_arr, m
//Free a device array memory.
void free_device_array(device_array *dest);

// This is just overloaded to allow the device arrays
// be freed properly.
void free_device_array(base_array_t* dest);

//prints information about a device array. useful for debugging.
void print_array_info(device_real_array* arr);
Expand Down

0 comments on commit 35938d9

Please sign in to comment.