Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
[Cpp] Fix reduce_array, usub_array and in-situ access to array slice
Browse files Browse the repository at this point in the history
See Modelica.Blocks.Nonlinear.PadeDelay

Belonging to [master]:
  - #2779
  • Loading branch information
rfranke authored and OpenModelica-Hudson committed Nov 9, 2018
1 parent 4125b19 commit 144493f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 9 additions & 7 deletions SimulationRuntime/cpp/Core/Math/ArrayOperations.cpp
Expand Up @@ -224,7 +224,7 @@ template <typename T>
void promote_array(size_t n, const BaseArray<T>& s, BaseArray<T>& d)
{
vector<size_t> ex = s.getDims();
for (int i=0; i<n; i++)
for (size_t i = ex.size(); i < n; i++)
ex.push_back(1);
d.setDims(ex);
d.assign(s.getData());
Expand Down Expand Up @@ -447,12 +447,14 @@ void add_array_scalar(const BaseArray<T>& inputArray, T b, BaseArray<T>& outputA
template <typename T>
void usub_array(const BaseArray<T>& a, BaseArray<T>& b)
{
const T* src_data = a.getData();
T* dst_data = b.getData();
size_t numElems = a.getNumElems();
b.setDims(a.getDims());
for (size_t i = 0; i < numElems; i++)
dst_data[i] = -src_data[i];
size_t nelems = a.getNumElems();
if (nelems > 0) {
b.setDims(a.getDims());
const T* data = a.getData();
T* result = b.getData();
for (size_t i = 0; i < nelems; i++)
result[i] = -data[i];
}
}

template <typename T>
Expand Down
13 changes: 12 additions & 1 deletion SimulationRuntime/cpp/Include/Core/Math/ArraySlice.h
Expand Up @@ -193,7 +193,18 @@ class ArraySliceConst: public BaseArray<T> {
if (n != getNumElems())
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,
"Wrong number of elements in getDataCopy");
getDataDim(_idxs.size(), data);
if (n > 0) {
const T* base_data = _baseArray.getData();
if (base_data <= data && data < base_data + n) {
// in-situ access requires an internal copy to avoid side effects,
// e.g. v = v[n:-1:1]
const T* slice_data = getData();
std::copy(slice_data, slice_data + n, data);
}
else
// direct access
getDataDim(_idxs.size(), data);
}
}

virtual const T* getData() const {
Expand Down

0 comments on commit 144493f

Please sign in to comment.