escript  Revision_
DataVectorAlt.h
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2016 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 
183  protected:
184 
185  private:
186 
187  size_type m_size;
188  size_type m_dim;
189  size_type m_N;
190 
191  ElementType* m_array_data;
192 };
193 
194 
195 template <class T>
196 inline
199 {
200  return m_size;
201 }
202 
203 template <class T>
204 inline
207 {
208  ESYS_ASSERT(i<size(), "DataVectorAlt: invalid index specified, " << i << " of " << size());
209  return m_array_data[i];
210 }
211 
212 template <class T>
213 inline
216 {
217  ESYS_ASSERT(i<size(), "DataVectorAlt: invalid index specified. " << i << " of " << size());
218  return m_array_data[i];
219 }
220 
221 
222 
223 template <class T>
225  m_size(0),
226  m_dim(0),
227  m_N(0),
228  m_array_data(0)
229 {
230 }
231 
232 template <class T>
234  m_size(other.m_size),
235  m_dim(other.m_dim),
236  m_N(other.m_N),
237  m_array_data(0)
238 {
239  m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*m_size));
240  int i;
241  #pragma omp parallel for private(i) schedule(static)
242  for (i=0; i<m_size; i++) {
243  m_array_data[i] = other.m_array_data[i];
244  }
245 }
246 
247 template <class T>
250  const DataVectorAlt<T>::size_type blockSize) :
251  m_size(size),
252  m_dim(blockSize),
253  m_array_data(0)
254 {
255  resize(size, val, blockSize);
256 }
257 
258 template <class T>
260 {
261  // clear data members
262  m_size = -1;
263  m_dim = -1;
264  m_N = -1;
265  if (m_array_data!=0)
266  {
267  free(m_array_data);
268  }
269  m_array_data=0;
270 }
271 
272 template <class T>
273 void
275  const DataVectorAlt<T>::value_type newValue,
276  const DataVectorAlt<T>::size_type newBlockSize)
277 {
278  // The < 1 is to catch both ==0 and negatives
279  if ( newBlockSize < 1) {
280  std::ostringstream oss;
281  oss << "DataVectorAlt: invalid blockSize specified (" << newBlockSize << ')';
282  throw DataException(oss.str());
283  }
284 
285  if ( newSize < 0 ) {
286  std::ostringstream oss;
287  oss << "DataVectorAlt: invalid new size specified (" << newSize << ')';
288  throw DataException(oss.str());
289  }
290  if ( (newSize % newBlockSize) != 0) {
291  std::ostringstream oss;
292  oss << "DataVectorAlt: newSize is not a multiple of blockSize: (" << newSize << ", " << newBlockSize<< ')';
293  throw DataException(oss.str());
294  }
295 
296  m_size = newSize;
297  m_dim = newBlockSize;
298  m_N = newSize / newBlockSize;
299 
300  if (m_array_data!=0)
301  {
302  free(m_array_data);
303  }
304  m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*m_size));
305  int i;
306  #pragma omp parallel for private(i) schedule(static)
307  for (i=0; i<m_size; i++) {
308  m_array_data[i] = newValue;
309  }
310 }
311 
312 template <class T>
315 {
316  assert(m_size >= 0);
317 
318 
319  m_size = other.m_size;
320  m_dim = other.m_dim;
321  m_N = other.m_N;
322 
323  if (m_array_data!=0)
324  {
325  free(m_array_data);
326  }
327  m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*m_size));
328  int i;
329  #pragma omp parallel for private(i) schedule(static)
330  for (i=0; i<m_size; i++) {
331  m_array_data[i] = other.m_array_data[i];
332  }
333 
334  return *this;
335 }
336 
337 template <class T>
338 bool
340 {
341  assert(m_size >= 0);
342 
343  if (m_size!=other.m_size) {
344  return false;
345  }
346  if (m_dim!=other.m_dim) {
347  return false;
348  }
349  if (m_N!=other.m_N) {
350  return false;
351  }
352  for (int i=0; i<m_size; i++) {
353  if (m_array_data[i] != other.m_array_data[i]) {
354  return false;
355  }
356  }
357  return true;
358 }
359 
360 template <class T>
361 bool
363 {
364  return !(*this==other);
365 }
366 
367 template <class T>
368 void
370 {
371  const DataTypes::ShapeType& tempShape=value.getShape();
372  size_type len=DataTypes::noValues(tempShape);
373  if (offset+len*copies>size())
374  {
375  std::ostringstream ss;
376  ss << "Error - not enough room for that DataPoint at that offset. (";
377  ss << "offset=" << offset << " + " << " len=" << len << " >= " << size();
378  throw DataException(ss.str());
379  }
380  size_type si=0,sj=0,sk=0,sl=0;
381  switch (value.getRank())
382  {
383  case 0:
384  for (size_type z=0;z<copies;++z)
385  {
386  m_array_data[offset+z]=value.getElt();
387  }
388  break;
389  case 1:
390  for (size_type z=0;z<copies;++z)
391  {
392  for (size_t i=0;i<tempShape[0];++i)
393  {
394  m_array_data[offset+i]=value.getElt(i);
395  }
396  offset+=len;
397  }
398  break;
399  case 2:
400  si=tempShape[0];
401  sj=tempShape[1];
402  for (size_type z=0;z<copies;++z)
403  {
404  for (size_type i=0;i<si;i++)
405  {
406  for (size_type j=0;j<sj;j++)
407  {
408  m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j)]=value.getElt(i,j);
409  }
410  }
411  offset+=len;
412  }
413  break;
414  case 3:
415  si=tempShape[0];
416  sj=tempShape[1];
417  sk=tempShape[2];
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  for (size_type k=0;k<sk;k++)
425  {
426  m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k)]=value.getElt(i,j,k);
427  }
428  }
429  }
430  offset+=len;
431  }
432  break;
433  case 4:
434  si=tempShape[0];
435  sj=tempShape[1];
436  sk=tempShape[2];
437  sl=tempShape[3];
438  for (size_type z=0;z<copies;++z)
439  {
440  for (size_type i=0;i<si;i++)
441  {
442  for (size_type j=0;j<sj;j++)
443  {
444  for (size_type k=0;k<sk;k++)
445  {
446  for (size_type l=0;l<sl;l++)
447  {
448  m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k,l)]=value.getElt(i,j,k,l);
449  }
450  }
451  }
452  }
453  offset+=len;
454  }
455  break;
456  default:
457  std::ostringstream oss;
458  oss << "Error - unknown rank. Rank=" << value.getRank();
459  throw DataException(oss.str());
460  }
461 }
462 
463 template <class T>
464 void
466 {
467  DataTypes::ShapeType tempShape=value.getShape();
468  DataVectorAlt<T>::size_type nelements=DataTypes::noValues(tempShape)*copies;
469  if (m_array_data!=0)
470  {
471  free(m_array_data);
472  }
473  m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*nelements));
474  m_size=nelements; // total amount of elements
475  m_dim=m_size; // elements per sample
476  m_N=1; // number of samples
477  copyFromArrayToOffset(value,0,copies);
478 }
479 
480 
481 
482 } // end of namespace
483 } // end of namespace
484 
485 #endif // __ESCRIPT_DATAVECTORALT_H__
ElementType * m_array_data
Definition: DataVectorAlt.h:191
const DataTypes::ShapeType & getShape() const
Definition: WrappedArray.h:82
DataVectorAlt()
Default constructor for DataVectorAlt.
Definition: DataVectorAlt.h:224
ElementType & reference
Definition: DataVectorAlt.h:50
DataTypes::vec_size_type size_type
Definition: DataVectorAlt.h:49
size_type m_dim
Definition: DataVectorAlt.h:188
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:198
ElementType value_type
Definition: DataVectorAlt.h:48
size_type m_size
Definition: DataVectorAlt.h:187
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:206
size_type m_N
Definition: DataVectorAlt.h:189
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