Skip to content

Commit

Permalink
(OpenMP) Use the memory pool of the current thread
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15885 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Apr 23, 2013
1 parent ab7eb56 commit 7d2713c
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 138 deletions.
24 changes: 11 additions & 13 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -1508,9 +1508,12 @@ template functionXXX_system(list<SimEqSystem> derivativEquations, String name, I
::=
let odeEqs = derivativEquations |> eq => equationNames_(eq,contextSimulationNonDiscrete); separator="\n"
<<
static void function<%name%>_system<%n%>(DATA *data,int omc_thread_number)
static void function<%name%>_system<%n%>(DATA *data)
{
state mem_state;
mem_state = get_memory_state();
<%odeEqs%>
restore_memory_state(mem_state);
}
>>
end functionXXX_system;
Expand All @@ -1520,25 +1523,24 @@ template functionXXX_systems(list<list<SimEqSystem>> eqs, String name, Text &loo
let funcs = eqs |> eq hasindex i1 fromindex 0 => functionXXX_system(eq,name,i1) ; separator="\n"
let nFuncs = listLength(eqs)
let funcNames = eqs |> e hasindex i1 fromindex 0 => 'function<%name%>_system<%i1%>' ; separator=",\n"
let head = if Flags.isSet(Flags.OPENMP) then '#pragma omp parallel for private(id,th_id) schedule(<%match noProc() case 0 then "dynamic" else "static"%>)'
let &varDecls += 'int id, th_id;<%\n%>'
let head = if Flags.isSet(Flags.OPENMP) then '#pragma omp parallel for private(id) schedule(<%match noProc() case 0 then "dynamic" else "static"%>)'
let &varDecls += 'int id;<%\n%>'
let &loop +=
/* Text for the loop body that calls the equations */
match listLength(eqs)
case 0 then ""
case 1 then 'function<%name%>_systems[0](data,omp_get_thread_num());'
case 1 then 'function<%name%>_systems[0](data);'
else
<<
<%head%>
for(id=0; id<<%nFuncs%>; id++) {
th_id = omp_get_thread_num();
function<%name%>_systems[id](data,th_id);
function<%name%>_systems[id](data);
}
>>
/* Text before the function head */
<<
<%funcs%>
static void (*function<%name%>_systems[<%nFuncs%>])(DATA *, int) = {
static void (*function<%name%>_systems[<%nFuncs%>])(DATA *) = {
<%funcNames%>
};
>>
Expand All @@ -1564,6 +1566,7 @@ template functionODE(list<list<SimEqSystem>> derivativEquations, Text method)
void function_initMemoryState()
{
push_memory_states(<% if Flags.isSet(Flags.OPENMP) then (match noProc() case 0 then "omp_get_max_threads()" else noProc()) else 1 %>);
<% if Flags.isSet(Flags.OPENMP) then "get_thread_index = omp_get_thread_num;" %>
}

int functionODE(DATA *data)
Expand Down Expand Up @@ -1626,13 +1629,9 @@ template functionAlgebraic(list<list<SimEqSystem>> algebraicEquations)
/* for continuous time variables */
int functionAlgebraics(DATA *data)
{
state mem_state;
<%varDecls%>
data->simulationInfo.discreteCall = 0;
mem_state = get_memory_state();
<%loop%>
restore_memory_state(mem_state);
return 0;
}
>>
Expand Down Expand Up @@ -3117,10 +3116,9 @@ template commonHeader(String filePrefix)
<<
<% if acceptMetaModelicaGrammar() then "#define __OPENMODELICA__METAMODELICA"%>
<% if acceptMetaModelicaGrammar() then "#include \"meta_modelica.h\"" %>
#ifdef _OPENMP_
#ifdef _OPENMP
#include <omp.h>
#else
#define omp_get_thread_num() 0
#define omp_get_max_threads() 1
#endif
#include "modelica.h"
Expand Down
12 changes: 6 additions & 6 deletions SimulationRuntime/c/math-support/hybrj.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ static logical c_false = FALSE_;
{
/* Initialized data */

static doublereal one = 1.;
static doublereal p1 = .1;
static doublereal p5 = .5;
static doublereal p001 = .001;
static doublereal p0001 = 1e-4;
static doublereal zero = 0.;
const doublereal one = 1.;
const doublereal p1 = .1;
const doublereal p5 = .5;
const doublereal p001 = .001;
const doublereal p0001 = 1e-4;
const doublereal zero = 0.;

/* System generated locals */
integer fjac_dim1, fjac_offset, i__1, i__2;
Expand Down
2 changes: 0 additions & 2 deletions SimulationRuntime/c/simulation/solver/linearSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ int solve_linear_system(DATA *data, int sysNumber)
int success;
LINEAR_SYSTEM_DATA* linsys = data->simulationInfo.linearSystemData;

data->simulationInfo.currentLinearSystemIndex = sysNumber;

/* for now just use lapack solver as before */
switch(data->simulationInfo.lsMethod)
{
Expand Down
12 changes: 6 additions & 6 deletions SimulationRuntime/c/util/base_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void base_array_create(base_array_t *dest, void *data, int ndims, va_list ap)
dest->data = data;
dest->ndims = ndims;

dest->dim_size = size_alloc(0,ndims);
dest->dim_size = size_alloc(ndims);

for(i = 0; i < ndims; ++i) {
dest->dim_size[i] = va_arg(ap, int);
Expand Down Expand Up @@ -217,15 +217,15 @@ size_t base_array_nr_of_elements(const base_array_t *a)
void simple_alloc_1d_base_array(base_array_t *dest, int n, void *data)
{
dest->ndims = 1;
dest->dim_size = size_alloc(0,1);
dest->dim_size = size_alloc(1);
dest->dim_size[0] = n;
dest->data = data;
}

void simple_alloc_2d_base_array(base_array_t *dest, int r, int c, void *data)
{
dest->ndims = 2;
dest->dim_size = size_alloc(0,2);
dest->dim_size = size_alloc(2);
dest->dim_size[0] = r;
dest->dim_size[1] = c;
dest->data = data;
Expand All @@ -237,7 +237,7 @@ size_t alloc_base_array(base_array_t *dest, int ndims, va_list ap)
size_t nr_of_elements = 1;

dest->ndims = ndims;
dest->dim_size = size_alloc(0,ndims);
dest->dim_size = size_alloc(ndims);

for(i = 0; i < ndims; ++i) {
dest->dim_size[i] = va_arg(ap, _index_t);
Expand All @@ -261,7 +261,7 @@ void clone_base_array_spec(const base_array_t *source, base_array_t *dest)
assert(base_array_ok(source));

dest->ndims = source->ndims;
dest->dim_size = size_alloc(0,dest->ndims);
dest->dim_size = size_alloc(dest->ndims);
assert(dest->dim_size);

for(i = 0; i < dest->ndims; ++i) {
Expand Down Expand Up @@ -351,7 +351,7 @@ void clone_reverse_base_array_spec(const base_array_t* source, base_array_t* des
assert(base_array_ok(source));

dest->ndims = source->ndims;
dest->dim_size = size_alloc(0,dest->ndims);
dest->dim_size = size_alloc(dest->ndims);
assert(dest->dim_size);

for(i = 0; i < dest->ndims; ++i) {
Expand Down
36 changes: 18 additions & 18 deletions SimulationRuntime/c/util/boolean_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ void boolean_array_create(boolean_array_t *dest, modelica_boolean *data,

void simple_alloc_1d_boolean_array(boolean_array_t* dest, int n)
{
simple_alloc_1d_base_array(dest, n, boolean_alloc(0,n));
simple_alloc_1d_base_array(dest, n, boolean_alloc(n));
}

void simple_alloc_2d_boolean_array(boolean_array_t* dest, int r, int c)
{
simple_alloc_2d_base_array(dest, r, c, boolean_alloc(0,r * c));
simple_alloc_2d_base_array(dest, r, c, boolean_alloc(r * c));
}

void alloc_boolean_array(boolean_array_t *dest, int ndims, ...)
Expand All @@ -108,12 +108,12 @@ void alloc_boolean_array(boolean_array_t *dest, int ndims, ...)
va_start(ap, ndims);
elements = alloc_base_array(dest, ndims, ap);
va_end(ap);
dest->data = boolean_alloc(0,elements);
dest->data = boolean_alloc(elements);
}

void alloc_boolean_array_data(boolean_array_t* a)
{
a->data = boolean_alloc(0,base_array_nr_of_elements(a));
a->data = boolean_alloc(base_array_nr_of_elements(a));
}

void copy_boolean_array_data(const boolean_array_t *source, boolean_array_t *dest)
Expand Down Expand Up @@ -296,9 +296,9 @@ void indexed_assign_boolean_array(const boolean_array_t* source,
assert(j == source->ndims);

mem_state = get_memory_state();
idx_vec1 = size_alloc(0,dest->ndims);
idx_vec2 = size_alloc(0,source->ndims);
idx_size = size_alloc(0,dest_spec->ndims);
idx_vec1 = size_alloc(dest->ndims);
idx_vec2 = size_alloc(source->ndims);
idx_size = size_alloc(dest_spec->ndims);

for(i = 0; i < dest_spec->ndims; ++i) {
idx_vec1[i] = 0;
Expand Down Expand Up @@ -363,9 +363,9 @@ void index_boolean_array(const boolean_array_t* source,
assert(j == dest->ndims);

mem_state = get_memory_state();
idx_vec1 = size_alloc(0,source->ndims); /*indices in the source array*/
idx_vec2 = size_alloc(0,dest->ndims); /* indices in the destination array*/
idx_size = size_alloc(0,source_spec->ndims);
idx_vec1 = size_alloc(source->ndims); /*indices in the source array*/
idx_vec2 = size_alloc(dest->ndims); /* indices in the destination array*/
idx_size = size_alloc(source_spec->ndims);

for(i = 0; i < source->ndims; ++i) {
idx_vec1[i] = 0;
Expand Down Expand Up @@ -431,7 +431,7 @@ void index_alloc_boolean_array(const boolean_array_t* source,
}

dest->ndims = source->ndims + ndimsdiff;
dest->dim_size = size_alloc(0,dest->ndims);
dest->dim_size = size_alloc(dest->ndims);

for(i = 0,j = 0; i < dest->ndims; ++i) {
while(source_spec->index_type[i+j] == 'S') { /* Skip scalars */
Expand All @@ -456,7 +456,7 @@ void simple_index_alloc_boolean_array1(const boolean_array_t* source, int i1,
assert(base_array_ok(source));

dest->ndims = source->ndims - 1;
dest->dim_size = size_alloc(0,dest->ndims);
dest->dim_size = size_alloc(dest->ndims);

for(i = 0; i < dest->ndims; ++i) {
dest->dim_size[i] = source->dim_size[i+1];
Expand Down Expand Up @@ -728,9 +728,9 @@ void cat_alloc_boolean_array(int k, boolean_array_t* dest, int n,
n_sub *= elts[0]->dim_size[i];
}
/* allocate dest structure */
dest->data = boolean_alloc(0, n_super * new_k_dim_size * n_sub);
dest->data = boolean_alloc( n_super * new_k_dim_size * n_sub);
dest->ndims = elts[0]->ndims;
dest->dim_size = size_alloc(0,dest->ndims);
dest->dim_size = size_alloc(dest->ndims);
for(j = 0; j < dest->ndims; j++) {
dest->dim_size[j] = elts[0]->dim_size[j];
}
Expand Down Expand Up @@ -775,7 +775,7 @@ void promote_boolean_array(const boolean_array_t* a, int n,boolean_array_t* dest
{
int i;

dest->dim_size = size_alloc(0,n+a->ndims);
dest->dim_size = size_alloc(n+a->ndims);
dest->data = a->data;
/* Assert a->ndims>=n */
for(i = 0; i < a->ndims; ++i) {
Expand All @@ -801,10 +801,10 @@ void promote_scalar_boolean_array(modelica_boolean s,int n,
/* Assert that dest is of correct dimension */

/* Alloc size */
dest->dim_size = size_alloc(0,n);
dest->dim_size = size_alloc(n);

/* Alloc data */
dest->data = boolean_alloc(0,1);
dest->data = boolean_alloc(1);

dest->ndims = n;
boolean_set(dest, 0, s);
Expand Down Expand Up @@ -972,7 +972,7 @@ void fill_alloc_boolean_array(boolean_array_t* dest, modelica_boolean value, int
va_start(ap, ndims);
elements = alloc_base_array(dest, ndims, ap);
va_end(ap);
dest->data = boolean_alloc(0,elements);
dest->data = boolean_alloc(elements);

for(i = 0; i < elements; ++i) {
boolean_set(dest, i, value);
Expand Down
12 changes: 6 additions & 6 deletions SimulationRuntime/c/util/index_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ int index_spec_ok(const index_spec_t* s)
void alloc_index_spec(index_spec_t* s)
{
int i;
s->index = index_alloc(0,s->ndims);
s->index = index_alloc(s->ndims);
for(i = 0; i < s->ndims; ++i) {
if(s->dim_size[i] > 0) {
s->index[i] = size_alloc(0,s->dim_size[i]);
s->index[i] = size_alloc(s->dim_size[i]);
} else {
s->index[i] = 0;
}
Expand All @@ -100,9 +100,9 @@ void create_index_spec(index_spec_t* dest, int nridx, ...)
va_start(ap,nridx);

dest->ndims = nridx;
dest->dim_size = size_alloc(0,nridx);
dest->index = index_alloc(0,nridx);
dest->index_type = char_alloc(0,nridx);
dest->dim_size = size_alloc(nridx);
dest->index = index_alloc(nridx);
dest->index_type = char_alloc(nridx);
for(i = 0; i < nridx; ++i) {
dest->dim_size[i] = va_arg(ap, _index_t);
dest->index[i] = va_arg(ap, _index_t*);
Expand All @@ -123,7 +123,7 @@ _index_t* make_index_array(int nridx, ...)
va_list ap;
va_start(ap,nridx);

res = size_alloc(0,nridx);
res = size_alloc(nridx);
for(i = 0; i < nridx; ++i) {
res[i] = va_arg(ap,_index_t);
}
Expand Down

0 comments on commit 7d2713c

Please sign in to comment.