Skip to content

Commit

Permalink
Implement DIV_ARR and ADD_ARRAY_SCALAR in Cpp runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranke committed Aug 25, 2016
1 parent 0b58b42 commit 7ce61b8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Compiler/Template/CodegenCppCommon.tpl
Expand Up @@ -2319,8 +2319,16 @@ template daeExpBinary(Operator it, Exp exp1, Exp exp2, Context context, Text &pr
let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/)
let &preExp += 'multiply_array_elem_wise<<%type%>>(<%e1%>, <%e2%>, <%tvar%>);<%\n%>'
'<%tvar%>'
case DIV_ARR(__) then "daeExpBinary:ERR DIV_ARR not supported"
case ADD_ARRAY_SCALAR(__) then "daeExpBinary:ERR ADD_ARRAY_SCALAR not supported"
case DIV_ARR(ty=T_ARRAY(dims=dims)) then
let type = expTypeShort(ty.ty)
let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/)
let &preExp += 'divide_array_elem_wise<<%type%>>(<%e1%>, <%e2%>, <%tvar%>);<%\n%>'
'<%tvar%>'
case ADD_ARRAY_SCALAR(ty=T_ARRAY(dims=dims)) then
let type = expTypeShort(ty.ty)
let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/)
let &preExp += 'add_array_scalar<<%type%>>(<%e2%>, <%e1%>, <%tvar%>);<%\n%>'
'<%tvar%>'
case SUB_SCALAR_ARRAY(ty=T_ARRAY(dims=dims)) then
let type = expTypeShort(ty.ty)
let tvar = tempDecl(expTypeArrayDims(ty.ty, dims), &varDecls /*BUFD*/)
Expand Down
45 changes: 45 additions & 0 deletions SimulationRuntime/cpp/Core/Math/ArrayOperations.cpp
Expand Up @@ -342,6 +342,24 @@ void divide_array(const BaseArray<T>& inputArray, const T &b, BaseArray<T>& outp
std::transform(data, data + nelems, aim, std::bind2nd(std::divides<T>(), b));
}

template <typename T>
void divide_array_elem_wise(const BaseArray<T> &leftArray, const BaseArray<T> &rightArray, BaseArray<T> &resultArray)
{
size_t dimLeft = leftArray.getNumElems();
size_t dimRight = rightArray.getNumElems();

if(dimLeft != dimRight)
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,
"Right and left array must have the same size for element wise division");

resultArray.setDims(leftArray.getDims());
const T* leftData = leftArray.getData();
const T* rightData = rightArray.getData();
T* result = resultArray.getData();

std::transform (leftData, leftData + leftArray.getNumElems(), rightData, result, std::divides<T>());
}

template <typename T>
void fill_array(BaseArray<T>& inputArray, T b)
{
Expand Down Expand Up @@ -413,6 +431,19 @@ void add_array(const BaseArray<T>& leftArray, const BaseArray<T>& rightArray, Ba
std::transform(data1, data1 + leftArray.getNumElems(), data2, aim, std::plus<T>());
}

template <typename T>
void add_array_scalar(const BaseArray<T>& inputArray, T b, BaseArray<T>& outputArray)
{
size_t dim = inputArray.getNumElems();
if (dim > 0) {
outputArray.setDims(inputArray.getDims());
const T* data = inputArray.getData();
T* result = outputArray.getData();
std::transform (data, data + inputArray.getNumElems(),
result, std::bind2nd(std::plus<T>(), b));
}
}

template <typename T>
void usub_array(const BaseArray<T>& a, BaseArray<T>& b)
{
Expand Down Expand Up @@ -604,6 +635,13 @@ divide_array(const BaseArray<int>& inputArray, const int &b, BaseArray<int>& out
template void BOOST_EXTENSION_EXPORT_DECL
divide_array(const BaseArray<bool>& inputArray, const bool &b, BaseArray<bool>& outputArray);

template void BOOST_EXTENSION_EXPORT_DECL
divide_array_elem_wise(const BaseArray<double> &leftArray, const BaseArray<double> &rightArray, BaseArray<double> &resultArray);
template void BOOST_EXTENSION_EXPORT_DECL
divide_array_elem_wise(const BaseArray<int> &leftArray, const BaseArray<int> &rightArray, BaseArray<int> &resultArray);
template void BOOST_EXTENSION_EXPORT_DECL
divide_array_elem_wise(const BaseArray<bool> &leftArray, const BaseArray<bool> &rightArray, BaseArray<bool> &resultArray);

template void BOOST_EXTENSION_EXPORT_DECL
fill_array(BaseArray<double>& inputArray, double b);
template void BOOST_EXTENSION_EXPORT_DECL
Expand Down Expand Up @@ -637,6 +675,13 @@ add_array(const BaseArray<int>& leftArray, const BaseArray<int>& rightArray, Bas
template void BOOST_EXTENSION_EXPORT_DECL
add_array(const BaseArray<bool>& leftArray, const BaseArray<bool>& rightArray, BaseArray<bool>& resultArray);

template void BOOST_EXTENSION_EXPORT_DECL
add_array_scalar(const BaseArray<double>& inputArray, double b, BaseArray<double>& outputArray);
template void BOOST_EXTENSION_EXPORT_DECL
add_array_scalar(const BaseArray<int>& inputArray, int b, BaseArray<int>& outputArray);
template void BOOST_EXTENSION_EXPORT_DECL
add_array_scalar(const BaseArray<bool>& inputArray, bool b, BaseArray<bool>& outputArray);

template void BOOST_EXTENSION_EXPORT_DECL
usub_array(const BaseArray<double>& a, BaseArray<double>& b);
template void BOOST_EXTENSION_EXPORT_DECL
Expand Down

0 comments on commit 7ce61b8

Please sign in to comment.