Skip to content

Commit

Permalink
fix for daeExpReduction in cpp template
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Nov 17, 2015
1 parent 13983f6 commit e344cf7
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 11 deletions.
13 changes: 7 additions & 6 deletions Compiler/Template/CodegenCppCommon.tpl
Expand Up @@ -1121,7 +1121,7 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
<%tmp_shape%>.push_back(0);<%\n%>
<%tmp_idx%>.push_back(<%arrIndex%>++);<%\n%>
<%tmp_indeces%>.push_back(<%tmp_idx%>);<%\n%>
create_array_from_shape(make_pair(<%tmp_shape%>,<%tmp_indeces%>),<%reductionBodyExpr%>,<%res%>);
fill_array_from_shape(make_pair(<%tmp_shape%>,<%tmp_indeces%>),<%reductionBodyExpr%>,<%res%>);
>>
else
'<%res%>(<%arrIndex%>++) = <%reductionBodyExpr%>;'
Expand Down Expand Up @@ -1187,7 +1187,7 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
"")
<<
<%arrIndex%> = 1;
/* Note: skip dimensioning of <%res%> because create_array_from_shape does it
/* Note: skip dimensioning of <%res%> because create_array_from_shape does it*/
<% match typeof(r.expr)
case T_COMPLEX(complexClassType = record_state) then
let rec_name = '<%underscorePath(ClassInf.getStateName(record_state))%>'
Expand All @@ -1200,12 +1200,13 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
case DIM_ENUM(__) then '<%dim_vec%>.push_back(<%size%>);'
else error(sourceInfo(), 'array reduction unable to generate code for element of unknown dimension sizes; type <%unparseType(typeof(r.expr))%>: <%ExpressionDump.printExpStr(r.expr)%>')
; separator = ", "
'<%dimSizes%>
'<%dim_vec%>.push_back(<%length%>);
<%dimSizes%>
<%res%>.setDims(<%dim_vec%>);'

else
'<%res%>.setDims(<%length%>);'%>
*/

>>
else if ri.defaultValue then
<<
Expand Down Expand Up @@ -1440,7 +1441,7 @@ case ARRAY(array=_::_, ty = arraytype) then
let params = daeExpArray2(array,arrayVar,ArrayType,arrayTypeStr,context,preExp,varDecls,simCode, &extraFuncs,&extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
""
else
let &varDecls += '<%ArrayType%> <%arrayVar%>;'
let &varDecls += '<%ArrayType%> <%arrayVar%>;<%\n%>'
daeExpArray3(array, arrayVar, ArrayType, context, preExp, varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
arrayVar
case ARRAY(__) then
Expand All @@ -1464,7 +1465,7 @@ template daeExpArray3(list<Exp> array, String arrayVar, String ArrayType, Conte
let arraycreate = (array |> e hasindex i0 fromindex 1 =>
let subArraycall = daeExp(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
<<
<%arrayVar%>.append(<%i0%>, <%subArraycall%>);
<%arrayVar%>.append(<%i0%>, <%subArraycall%>,<%listLength(array)%>);
>> ;separator="\n")
let &preExp +=
<<
Expand Down
59 changes: 58 additions & 1 deletion SimulationRuntime/cpp/Core/Math/ArrayOperations.cpp
Expand Up @@ -115,7 +115,7 @@ void create_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d
shape.push_back(*iter);

}
d.setDims(shape);
d.setDims(shape);

//Check if the dimension of passed indices match the dimension of target array
if(sp.second.size()!=s.getNumDims())
Expand Down Expand Up @@ -160,6 +160,58 @@ void create_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d
}




/*
fills an array (d) with passed multi array shape (sp) and initialized it with elements from passed source array (s)
s source array
d destination array
sp (shape,indices) of source array
*/
template < typename T >
void fill_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d)
{


T* data = new T[d.getNumElems()];

idx_type::const_iterator spec_iter;
//calc number of indeces
size_t n =1;
for(spec_iter = sp.second.begin();spec_iter!=sp.second.end();++spec_iter)
{

n*=spec_iter->size();
}
size_t k =0;
size_t index=0;
vector<size_t>::const_iterator indeces_iter;

//initialize target array with elements of source array using passed indices
vector<size_t> idx;
for(int i=0;i<n;i++)
{
spec_iter = sp.second.begin();
for(int dim=0;dim<s.getNumDims();dim++)
{
size_t idx1 = getNextIndex(*spec_iter,i);
idx.push_back(idx1);
spec_iter++;
}
if(index>(d.getNumElems()-1))
{
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,"Erro in create array from shape, number of dimensions does not match");
}
data[index] = s(idx);
idx.clear();
index++;
}
//assign elemets to target array
d.assign( data );
delete [] data;
}


//template < typename T , size_t NumDims, size_t NumDims2 >
template <typename T>
void promote_array(size_t n, const BaseArray<T>& s, BaseArray<T>& d)
Expand Down Expand Up @@ -528,6 +580,11 @@ template void BOOST_EXTENSION_EXPORT_DECL create_array_from_shape(const spec_typ
template void BOOST_EXTENSION_EXPORT_DECL create_array_from_shape(const spec_type& sp, BaseArray<int>& s, BaseArray<int>& d);
template void BOOST_EXTENSION_EXPORT_DECL create_array_from_shape(const spec_type& sp, BaseArray<bool>& s, BaseArray<bool>& d);


template void BOOST_EXTENSION_EXPORT_DECL fill_array_from_shape(const spec_type& sp, BaseArray<double>& s, BaseArray<double>& d);
template void BOOST_EXTENSION_EXPORT_DECL fill_array_from_shape(const spec_type& sp, BaseArray<int>& s, BaseArray<int>& d);
template void BOOST_EXTENSION_EXPORT_DECL fill_array_from_shape(const spec_type& sp, BaseArray<bool>& s, BaseArray<bool>& d);

template void BOOST_EXTENSION_EXPORT_DECL
multiply_array(const BaseArray<double>& inputArray, const double &b, BaseArray<double>& outputArray);
template void BOOST_EXTENSION_EXPORT_DECL
Expand Down
19 changes: 16 additions & 3 deletions SimulationRuntime/cpp/Include/Core/Math/Array.h
Expand Up @@ -1089,8 +1089,9 @@ class StatArrayDim2 : public StatArray<T, size1*size2, external>
* Copies one dimensional array to row i
* @param b array of type StatArrayDim1
* @param i row number
* @param n optional number of rows not needed for static arrays
*/
void append(size_t i, const StatArrayDim1<T, size2, external>& b)
void append(size_t i, const StatArrayDim1<T, size2, external>& b, size_t n = 0)
{
const T* data = b.getData();
T *array_data = StatArray<T, size1*size2, external>::getData() + i-1;
Expand Down Expand Up @@ -1273,8 +1274,9 @@ class StatArrayDim3 : public StatArray<T, size1*size2*size3, external>
* Copies two dimensional array to row i
* @param b array of type StatArrayDim2
* @param i row number
* @param n optional number of rows not needed for static arrays
*/
void append(size_t i, const StatArrayDim2<T,size2,size3>& b)
void append(size_t i, const StatArrayDim2<T,size2,size3>& b, size_t n = 0)
{
const T* data = b.getData();
T *array_data = StatArray<T, size1*size2*size3, external>::getData() + i-1;
Expand Down Expand Up @@ -1631,9 +1633,20 @@ class DynArrayDim2 : public DynArray<T, 2>
* Copies one dimensional array to row i
* @param b array of type DynArrayDim1
* @param i row number
* @param n number of rows
*/
void append(size_t i, const DynArrayDim1<T>& b)
void append(size_t i, const DynArrayDim1<T>& b, size_t n)
{
//if the dynamic array was not allocate before
if(this->_dims[0]==0 )
{
size_t m = b.getDim(1);
if(n > 0 && m > 0)
setDims(n,m);
else
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Could not append array, wrong array dimensions");

}
const T* data = b.getData();
T *array_data = this->_array_data + i-1;
size_t size1 = this->_dims[0];
Expand Down
3 changes: 3 additions & 0 deletions SimulationRuntime/cpp/Include/Core/Math/ArrayOperations.h
Expand Up @@ -46,6 +46,9 @@ sp (shape,indices) of source array
template < typename T >
void create_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d);

template < typename T >
void fill_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d);

template <typename T>
void promote_array(size_t n, const BaseArray<T>& s, BaseArray<T>& d);

Expand Down

0 comments on commit e344cf7

Please sign in to comment.