Skip to content

Commit

Permalink
Distinguish array slice of size 1 from reduction (#7675)
Browse files Browse the repository at this point in the history
See PowerSystems.Examples.AC3ph.Precalculation.TransDatFromEqCirc
  • Loading branch information
rfranke committed Jul 8, 2021
1 parent 3c70220 commit 7cc57c1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
16 changes: 11 additions & 5 deletions OMCompiler/SimulationRuntime/cpp/Include/Core/Math/ArraySlice.h
Expand Up @@ -78,7 +78,7 @@ class Slice {
iset = NULL;
}

// index set, reduction if size(indices) == 1
// index set
Slice(const BaseArray<int> &indices) {
start = 0;
step = 0;
Expand Down Expand Up @@ -109,6 +109,7 @@ class ArraySliceConst: public BaseArray<T> {
, _baseArray(baseArray)
, _isets(baseArray.getNumDims())
, _idxs(baseArray.getNumDims())
, _baseReduction(baseArray.getNumDims())
, _baseIdx(baseArray.getNumDims())
, _tmp_data(NULL) {

Expand Down Expand Up @@ -141,10 +142,13 @@ class ArraySliceConst: public BaseArray<T> {
dit->push_back(start + i * step);
}
}
if (sit->iset == NULL && size == 1 && sit->step == 0)
if (sit->iset == NULL && size == 1 && sit->step == 0) {
_baseReduction[dim - 1] = true;
// preset constant _baseIdx in case of reduction
_baseIdx[dim - 1] = sit->iset != NULL? (*_isets[dim - 1])(1): (*dit)[0];
}
else {
_baseReduction[dim - 1] = false;
if (size == 0)
_baseIdx[dim - 1] = 0; // mark empty dimension to distinguish it from WHOLEDIM
else
Expand All @@ -157,6 +161,7 @@ class ArraySliceConst: public BaseArray<T> {
// use all indices of remaining dims
for (; dim <= baseArray.getNumDims(); dim++) {
_isets[dim - 1] = NULL;
_baseReduction[dim - 1] = false;
_baseIdx[dim - 1] = 1; // mark regular case with positive value
_dims.push_back(_baseArray.getDim(dim));
}
Expand Down Expand Up @@ -278,6 +283,7 @@ class ArraySliceConst: public BaseArray<T> {
vector<const BaseArray<int>*> _isets; // given index sets per dimension
vector< vector<size_t> > _idxs; // created index sets per dimension
vector<size_t> _dims; // dimensions of array slice
vector<bool> _baseReduction; // mark reduced dimensions to distinguish them from size == 1
mutable vector<size_t> _baseIdx; // idx into underlying array
mutable T *_tmp_data; // storage for const T* getData()

Expand All @@ -292,6 +298,9 @@ class ArraySliceConst: public BaseArray<T> {
const BaseArray<int> *iset;
vector< vector<size_t> >::const_iterator dit;
for (dim = 1, dit = _idxs.begin(); dit != _idxs.end(); dim++, dit++) {
if (_baseReduction[dim - 1])
// preset base index in case of reduction
continue;
iset = _isets[dim - 1];
size = iset? iset->getNumElems(): dit->size();
switch (size) {
Expand All @@ -303,9 +312,6 @@ class ArraySliceConst: public BaseArray<T> {
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,
"Access to empty ArraySlice");
break;
case 1:
// reduction
break;
default:
// regular index mapping
_baseIdx[dim - 1] = iset? (*iset)(*idx++): (*dit)[*idx++ - 1];
Expand Down
18 changes: 15 additions & 3 deletions testsuite/openmodelica/cppruntime/arraySliceTest.mos
Expand Up @@ -10,8 +10,9 @@ package ArraySlice
model Test
Real[:,:] m = [1; 2; 3; 4; 5];
Real[:] w = reverse(m);
Real y = catEmpty(w);
Real z = reduction(w);
Real x = catEmpty(w);
Real y = reduction(w);
Real[:,:] z = sliceTranspose(2, [1, 2, 3; 4, 5, 6; 7, 8, 9]);
annotation(experiment(StopTime = 0));
end Test;
function reverse
Expand Down Expand Up @@ -40,6 +41,13 @@ protected
algorithm
y := product(sum(v[i*idx] for i in 1:n)); // index set with one element
end reduction;
function sliceTranspose
input Integer n;
input Real[n+1,n+1] M;
output Real[n,1] z;
algorithm
z := transpose(M[n+1:n+1,1:n]); // test access to slice of slice and size 1
end sliceTranspose;
end ArraySlice;
");
getErrorString();
Expand All @@ -49,8 +57,10 @@ getErrorString();

val(w[1], 0);
val(w[4], 0);
val(x, 0);
val(y, 0);
val(z, 0);
val(z[1,1], 0);
val(z[2,1], 0);

// Result:
// true
Expand All @@ -66,4 +76,6 @@ val(z, 0);
// 1.0
// 20.0
// 10.0
// 7.0
// 8.0
// endResult

0 comments on commit 7cc57c1

Please sign in to comment.