escript Revision_
DataVectorAlt.h
Go to the documentation of this file.
1
2/*****************************************************************************
3*
4* Copyright (c) 2003-2020 by The University of Queensland
5* http://www.uq.edu.au
6*
7* Primary Business: Queensland, Australia
8* Licensed under the Apache License, version 2.0
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12* Development 2012-2013 by School of Earth Sciences
13* Development from 2014-2017 by Centre for Geoscience Computing (GeoComp)
14* Development from 2019 by School of Earth and Environmental Sciences
15**
16*****************************************************************************/
17
18
19#ifndef __ESCRIPT_DATAVECTORALT_H__
20#define __ESCRIPT_DATAVECTORALT_H__
21
22#include "DataTypes.h"
23#include "system_dep.h"
24#include "Assert.h"
25#include "DataException.h"
26#include "WrappedArray.h"
27
28#include <sstream>
29
30namespace escript
31{
32
33namespace DataTypes
34{
35
36template <class T>
37class /* ESCRIPT_DLL_API */ DataVectorAlt {
38
39 public:
40
41 //
42 // The type of the elements stored in the vector.
43 typedef T ElementType;
44
45 //
46 // Various types exported to clients of this class.
47
48 typedef const ElementType * const_pointer;
53
62
72
92 const value_type val=0.0,
93 const size_type blockSize=1);
94
103
114 void
115 resize(const size_type newSize,
116 const value_type newVal=0.0,
117 const size_type newBlockSize=1);
118
125 void
126 copyFromArray(const WrappedArray& value, size_type copies);
127
128
129 // Please make sure that any implementation changes here are reflected in the specialised
130 // version in the .cpp file
131 void
133
134
139 inline
141 size() const;
142
150
156 bool
157 operator==(const DataVectorAlt<T>& other) const;
158
164 bool
165 operator!=(const DataVectorAlt<T>& other) const;
166
175 inline
178
179 inline
181 operator[](const size_type i) const;
182
183 // for compatibility with std::vector
186
187 protected:
188
189 private:
190
194
196};
197
198template <class T>
199inline
201{
202 return m_array_data;
203}
204
205template <class T>
206inline
208{
209 return m_array_data;
210}
211
212template <class T>
213inline
216{
217 return m_size;
218}
219
220template <class T>
221inline
224{
225 ESYS_ASSERT(i<size(), "DataVectorAlt: invalid index specified, " << i << " of " << size());
226 return m_array_data[i];
227}
228
229template <class T>
230inline
233{
234 ESYS_ASSERT(i<size(), "DataVectorAlt: invalid index specified. " << i << " of " << size());
235 return m_array_data[i];
236}
237
238
239
240template <class T>
242 m_size(0),
243 m_dim(0),
244 m_N(0),
245 m_array_data(0)
246{
247}
248
249template <class T>
251 m_size(other.m_size),
252 m_dim(other.m_dim),
253 m_N(other.m_N),
254 m_array_data(0)
255{
256 m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*m_size));
257 int i;
258 #pragma omp parallel for private(i) schedule(static)
259 for (i=0; i<m_size; i++) {
260 m_array_data[i] = other.m_array_data[i];
261 }
262}
263
264template <class T>
267 const DataVectorAlt<T>::size_type blockSize) :
268 m_size(size),
269 m_dim(blockSize),
270 m_array_data(0)
271{
272 resize(size, val, blockSize);
273}
274
275template <class T>
277{
278 // clear data members
279 m_size = -1;
280 m_dim = -1;
281 m_N = -1;
282 if (m_array_data!=0)
283 {
284 free(m_array_data);
285 }
286 m_array_data=0;
287}
288
289template <class T>
290void
292 const DataVectorAlt<T>::value_type newValue,
293 const DataVectorAlt<T>::size_type newBlockSize)
294{
295 // The < 1 is to catch both ==0 and negatives
296 if ( newBlockSize < 1) {
297 std::ostringstream oss;
298 oss << "DataVectorAlt: invalid blockSize specified (" << newBlockSize << ')';
299 throw DataException(oss.str());
300 }
301
302 if ( newSize < 0 ) {
303 std::ostringstream oss;
304 oss << "DataVectorAlt: invalid new size specified (" << newSize << ')';
305 throw DataException(oss.str());
306 }
307 if ( (newSize % newBlockSize) != 0) {
308 std::ostringstream oss;
309 oss << "DataVectorAlt: newSize is not a multiple of blockSize: (" << newSize << ", " << newBlockSize<< ')';
310 throw DataException(oss.str());
311 }
312
313 m_size = newSize;
314 m_dim = newBlockSize;
315 m_N = newSize / newBlockSize;
316
317 if (m_array_data!=0)
318 {
319 free(m_array_data);
320 }
321 m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*m_size));
322 int i;
323 #pragma omp parallel for private(i) schedule(static)
324 for (i=0; i<m_size; i++) {
325 m_array_data[i] = newValue;
326 }
327}
328
329template <class T>
332{
333 assert(m_size >= 0);
334
335
336 m_size = other.m_size;
337 m_dim = other.m_dim;
338 m_N = other.m_N;
339
340 if (m_array_data!=0)
341 {
342 free(m_array_data);
343 }
344 m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*m_size));
345 int i;
346 #pragma omp parallel for private(i) schedule(static)
347 for (i=0; i<m_size; i++) {
348 m_array_data[i] = other.m_array_data[i];
349 }
350
351 return *this;
352}
353
354template <class T>
355bool
357{
358 assert(m_size >= 0);
359
360 if (m_size!=other.m_size) {
361 return false;
362 }
363 if (m_dim!=other.m_dim) {
364 return false;
365 }
366 if (m_N!=other.m_N) {
367 return false;
368 }
369 for (int i=0; i<m_size; i++) {
370 if (m_array_data[i] != other.m_array_data[i]) {
371 return false;
372 }
373 }
374 return true;
375}
376
377template <class T>
378bool
380{
381 return !(*this==other);
382}
383
384template <class T>
385void
387{
388 const DataTypes::ShapeType& tempShape=value.getShape();
389 size_type len=DataTypes::noValues(tempShape);
390 if (offset+len*copies>size())
391 {
392 std::ostringstream ss;
393 ss << "Error - not enough room for that DataPoint at that offset. (";
394 ss << "offset=" << offset << " + " << " len=" << len << " >= " << size();
395 throw DataException(ss.str());
396 }
397 size_type si=0,sj=0,sk=0,sl=0;
398 switch (value.getRank())
399 {
400 case 0:
401 for (size_type z=0;z<copies;++z)
402 {
403 m_array_data[offset+z]=value.getElt();
404 }
405 break;
406 case 1:
407 for (size_type z=0;z<copies;++z)
408 {
409 for (size_t i=0;i<tempShape[0];++i)
410 {
411 m_array_data[offset+i]=value.getElt(i);
412 }
413 offset+=len;
414 }
415 break;
416 case 2:
417 si=tempShape[0];
418 sj=tempShape[1];
419 for (size_type z=0;z<copies;++z)
420 {
421 for (size_type i=0;i<si;i++)
422 {
423 for (size_type j=0;j<sj;j++)
424 {
425 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j)]=value.getElt(i,j);
426 }
427 }
428 offset+=len;
429 }
430 break;
431 case 3:
432 si=tempShape[0];
433 sj=tempShape[1];
434 sk=tempShape[2];
435 for (size_type z=0;z<copies;++z)
436 {
437 for (size_type i=0;i<si;i++)
438 {
439 for (size_type j=0;j<sj;j++)
440 {
441 for (size_type k=0;k<sk;k++)
442 {
443 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k)]=value.getElt(i,j,k);
444 }
445 }
446 }
447 offset+=len;
448 }
449 break;
450 case 4:
451 si=tempShape[0];
452 sj=tempShape[1];
453 sk=tempShape[2];
454 sl=tempShape[3];
455 for (size_type z=0;z<copies;++z)
456 {
457 for (size_type i=0;i<si;i++)
458 {
459 for (size_type j=0;j<sj;j++)
460 {
461 for (size_type k=0;k<sk;k++)
462 {
463 for (size_type l=0;l<sl;l++)
464 {
465 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k,l)]=value.getElt(i,j,k,l);
466 }
467 }
468 }
469 }
470 offset+=len;
471 }
472 break;
473 default:
474 std::ostringstream oss;
475 oss << "Error - unknown rank. Rank=" << value.getRank();
476 throw DataException(oss.str());
477 }
478}
479
480template <class T>
481void
483{
484 DataTypes::ShapeType tempShape=value.getShape();
485 DataVectorAlt<T>::size_type nelements=DataTypes::noValues(tempShape)*copies;
486 if (m_array_data!=0)
487 {
488 free(m_array_data);
489 }
490 m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*nelements));
491 m_size=nelements; // total amount of elements
492 m_dim=m_size; // elements per sample
493 m_N=1; // number of samples
494 copyFromArrayToOffset(value,0,copies);
495}
496
497
498
499} // end of namespace
500} // end of namespace
501
502#endif // __ESCRIPT_DATAVECTORALT_H__
#define ESYS_ASSERT(a, b)
EsysAssert is a MACRO that will throw an exception if the boolean condition specified is false.
Definition: Assert.h:79
Definition: DataException.h:28
Definition: DataVectorAlt.h:37
bool operator!=(const DataVectorAlt< T > &other) const
DataVectorAlt inequality comparison operator "!=". Return true if the given DataVectorAlt is not equa...
Definition: DataVectorAlt.h:379
ElementType * data()
Definition: DataVectorAlt.h:200
bool operator==(const DataVectorAlt< T > &other) const
DataVectorAlt equality comparison operator "==". Return true if the given DataVectorAlt is equal to t...
Definition: DataVectorAlt.h:356
ElementType value_type
Definition: DataVectorAlt.h:49
size_type m_N
Definition: DataVectorAlt.h:193
~DataVectorAlt()
Default destructor for DataVectorAlt.
Definition: DataVectorAlt.h:276
void copyFromArrayToOffset(const WrappedArray &value, size_type offset, size_type copies)
Definition: DataVectorAlt.h:386
void copyFromArray(const WrappedArray &value, size_type copies)
Populates the vector with the data from value. This method currently throws an exception if the speci...
Definition: DataVectorAlt.h:482
DataVectorAlt & operator=(const DataVectorAlt< T > &other)
DataVectorAlt assignment operator "=". Assign the given DataVectorAlt object to this.
Definition: DataVectorAlt.h:331
const_reference operator[](const size_type i) const
Definition: DataVectorAlt.h:232
void resize(const size_type newSize, const value_type newVal=0.0, const size_type newBlockSize=1)
Resize the DataVectorAlt to the given length "newSize". All current data is lost. All elements in the...
Definition: DataVectorAlt.h:291
size_type size() const
Return the number of elements in this DataVectorAlt.
Definition: DataVectorAlt.h:215
size_type m_dim
Definition: DataVectorAlt.h:192
reference operator[](const size_type i)
Return a reference to the element at position i in this DataVectorAlt. Will throw an exception if an ...
Definition: DataVectorAlt.h:223
const ElementType * const_pointer
Definition: DataVectorAlt.h:48
DataVectorAlt()
Default constructor for DataVectorAlt.
Definition: DataVectorAlt.h:241
T ElementType
Definition: DataVectorAlt.h:43
const ElementType & const_reference
Definition: DataVectorAlt.h:52
DataVectorAlt(const DataVectorAlt< T > &other)
Copy constructor for DataVectorAlt.
Definition: DataVectorAlt.h:250
DataVectorAlt(const size_type size, const value_type val=0.0, const size_type blockSize=1)
Constructor for DataVectorAlt.
ElementType & reference
Definition: DataVectorAlt.h:51
DataTypes::vec_size_type size_type
Definition: DataVectorAlt.h:50
const ElementType * data() const
Definition: DataVectorAlt.h:207
ElementType * m_array_data
Definition: DataVectorAlt.h:195
size_type m_size
Definition: DataVectorAlt.h:191
Definition: WrappedArray.h:33
DataTypes::real_t getElt() const
Definition: WrappedArray.h:89
unsigned int getRank() const
Definition: WrappedArray.h:77
const DataTypes::ShapeType & getShape() const
Definition: WrappedArray.h:83
#define ESCRIPT_INLINE_DLL_API
Definition: escriptcore/src/system_dep.h:31
int noValues(const ShapeType &shape)
Calculate the number of values in a datapoint with the given shape.
Definition: DataTypes.cpp:91
std::vector< int > ShapeType
The shape of a single datapoint.
Definition: DataTypes.h:44
vec_size_type getRelIndex(const DataTypes::ShapeType &shape, vec_size_type i)
Compute the offset (in 1D vector) of a given subscript with a shape.
Definition: DataTypes.h:240
long vec_size_type
Definition: DataTypes.h:49
Definition: AbstractContinuousDomain.cpp:23