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