Skip to content

Commit 1577216

Browse files
committed
store dimensions of array slice to reduce code size and speed up getDim
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25611 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 25860e3 commit 1577216

File tree

1 file changed

+13
-55
lines changed

1 file changed

+13
-55
lines changed

SimulationRuntime/cpp/Include/Core/Math/ArraySlice.h

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ class ArraySlice: public BaseArray<T> {
102102
"Wrong dimensions for ArraySlice");
103103
// create an explicit index set per dimension,
104104
// except for all indices that are indicated with an empty index set
105-
_ndims = 0;
106-
_nelems = 1;
107105
size_t dim;
108106
vector<Slice>::const_iterator sit;
109107
vector< vector<size_t> >::iterator dit = _idxs.begin();
@@ -125,8 +123,9 @@ class ArraySlice: public BaseArray<T> {
125123
if (dit->size() == 1)
126124
// prefill constant _baseIdx in case of reduction
127125
_baseIdx[dim - 1] = (*dit)[0];
128-
_ndims += dit->size() == 1? 0: 1;
129-
_nelems *= dit->size() != 0? dit->size(): _baseArray.getDim(dim);
126+
else
127+
// store dimension of array slice
128+
_dims.push_back(dit->size() != 0? dit->size(): _baseArray.getDim(dim));
130129
dit++;
131130
}
132131
}
@@ -164,52 +163,11 @@ class ArraySlice: public BaseArray<T> {
164163
}
165164

166165
virtual std::vector<size_t> getDims() const {
167-
vector<size_t> dims;
168-
size_t dim, size;
169-
const BaseArray<int> *iset;
170-
vector< vector<size_t> >::const_iterator dit;
171-
for (dim = 1, dit = _idxs.begin(); dit != _idxs.end(); dim++, dit++) {
172-
iset = _isets[dim - 1];
173-
size = iset? iset->getNumElems(): dit->size();
174-
switch (size) {
175-
case 0:
176-
// all indices
177-
dims.push_back(_baseArray.getDim(dim));
178-
break;
179-
case 1:
180-
// reduction
181-
break;
182-
default:
183-
// regular index mapping
184-
dims.push_back(size);
185-
}
186-
}
187-
return dims;
166+
return _dims;
188167
}
189168

190-
virtual int getDim(size_t reducedDim) const {
191-
size_t dim, size, rdim = 1;
192-
const BaseArray<int> *iset;
193-
vector< vector<size_t> >::const_iterator dit;
194-
for (dim = 1, dit = _idxs.begin(); dit != _idxs.end(); dim++, dit++) {
195-
iset = _isets[dim - 1];
196-
size = iset? iset->getNumElems(): dit->size();
197-
switch (size) {
198-
case 0:
199-
// all indices
200-
if (reducedDim == rdim++)
201-
return (int)_baseArray.getDim(dim);
202-
break;
203-
case 1:
204-
// reduction
205-
break;
206-
default:
207-
// regular index mapping
208-
if (reducedDim == rdim++)
209-
return (int)size;
210-
}
211-
}
212-
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "getDim out of range");
169+
virtual int getDim(size_t sliceDim) const {
170+
return (int)_dims[sliceDim];
213171
}
214172

215173
virtual T* getData() {
@@ -218,7 +176,7 @@ class ArraySlice: public BaseArray<T> {
218176
}
219177

220178
virtual void getDataCopy(T data[], size_t n) const {
221-
if (n != _nelems)
179+
if (n != getNumElems())
222180
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,
223181
"Wrong number of elements in getDataCopy");
224182
getDataDim(1, data);
@@ -227,17 +185,18 @@ class ArraySlice: public BaseArray<T> {
227185
virtual const T* getData() const {
228186
if (_tmp_data.size() == 0)
229187
// allocate on first use
230-
_tmp_data.resize(_nelems);
188+
_tmp_data.resize(getNumElems());
231189
getDataDim(1, &_tmp_data[0]);
232190
return &_tmp_data[0];
233191
}
234192

235193
virtual size_t getNumElems() const {
236-
return _nelems;
194+
return std::accumulate(_dims.begin(), _dims.end(),
195+
1, std::multiplies<size_t>());
237196
}
238197

239198
virtual size_t getNumDims() const {
240-
return _ndims;
199+
return _dims.size();
241200
}
242201

243202
virtual void setDims(const std::vector<size_t> &v) {
@@ -282,8 +241,7 @@ class ArraySlice: public BaseArray<T> {
282241
BaseArray<T> &_baseArray; // underlying array
283242
vector<const BaseArray<int>*> _isets; // given index sets per dimension
284243
vector< vector<size_t> > _idxs; // created index sets per dimension
285-
size_t _ndims; // number of reduced dimensions
286-
size_t _nelems; // number of elements
244+
vector<size_t> _dims; // dimensions of array slice
287245
mutable vector<size_t> _baseIdx; // idx into underlying array
288246
mutable vector<T> _tmp_data; // contiguous storage for const T* getData()
289247

@@ -328,7 +286,7 @@ class ArraySlice: public BaseArray<T> {
328286
}
329287

330288
T& accessElement(size_t ndims, ...) const {
331-
if (ndims != _ndims)
289+
if (ndims != _dims.size())
332290
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,
333291
"Wrong dimensions accessing ArraySlice");
334292
size_t dim, size, i;

0 commit comments

Comments
 (0)