/[escript]/trunk/escriptcore/src/DataVectorAlt.h
ViewVC logotype

Contents of /trunk/escriptcore/src/DataVectorAlt.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6000 - (show annotations)
Tue Mar 1 00:24:43 2016 UTC (3 years, 1 month ago) by caltinay
File MIME type: text/plain
File size: 12154 byte(s)
a few more include rearrangements.

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 Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
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>
36 class ESCRIPT_DLL_API DataVectorAlt {
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;
49 typedef DataTypes::vec_size_type size_type;
50 typedef ElementType & reference;
51 typedef const ElementType & const_reference;
52
53 /**
54 \brief
55 Default constructor for DataVectorAlt.
56
57 Description:
58 Constructs an empty DataVectorAlt object.
59 */
60 DataVectorAlt();
61
62 /**
63 \brief
64 Copy constructor for DataVectorAlt.
65
66 Description:
67 Constructs a DataVectorAlt object which is a copy of the
68 given DataVectorAlt object.
69 */
70 DataVectorAlt(const DataVectorAlt<T>& other);
71
72 /**
73 \brief
74 Constructor for DataVectorAlt.
75
76 Description:
77 Constructs a DataVectorAlt object of length "size" with all elements
78 initilised to "val".
79
80 \param size - Input - Number of elements in the vector.
81 \param val - Input - Initial value for all elements in the vector. Default is 0.0.
82 \param blockSize - Input - size of blocks within the vector, overall vector
83 size must be a precise multiple of the block size. Default is 1.
84
85 In escript::Data, blocksize corresponds to the number of elements required to hold all
86 the data-points for a sample, ie: the product of the dimensions of a data-point and the
87 number of data-points per sample. Size is the total number of elements required to hold
88 all elements for all data-points in the given object, ie: number of samples * blocksize.
89 */
90 DataVectorAlt(const size_type size,
91 const value_type val=0.0,
92 const size_type blockSize=1);
93
94 /**
95 \brief
96 Default destructor for DataVectorAlt.
97
98 Description:
99 Destroys the current DataVectorAlt object.
100 */
101 ~DataVectorAlt();
102
103 /**
104 \brief
105 Resize the DataVectorAlt to the given length "newSize".
106 All current data is lost. All elements in the new DataVectorAlt are
107 initialised to "newVal".
108
109 \param newSize - Input - New size for the vector.
110 \param newVal - Input - New initial value for all elements in the vector.
111 \param newBlockSize - Input - New block size for the vector.
112 */
113 void
114 resize(const size_type newSize,
115 const value_type newVal=0.0,
116 const size_type newBlockSize=1);
117
118 /**
119 \brief
120 Populates the vector with the data from value.
121 This method currently throws an exception if the specified number of copies won't fit.
122 \warning This function does not attempt to perform shape checking.
123 */
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
134 /**
135 \brief
136 Return the number of elements in this DataVectorAlt.
137 */
138 inline
139 size_type
140 size() const;
141
142 /**
143 \brief
144 DataVectorAlt assignment operator "=".
145 Assign the given DataVectorAlt object to this.
146 */
147 DataVectorAlt&
148 operator=(const DataVectorAlt<T>& other);
149
150 /**
151 \brief
152 DataVectorAlt equality comparison operator "==".
153 Return true if the given DataVectorAlt is equal to this.
154 */
155 bool
156 operator==(const DataVectorAlt<T>& other) const;
157
158 /**
159 \brief
160 DataVectorAlt inequality comparison operator "!=".
161 Return true if the given DataVectorAlt is not equal to this.
162 */
163 bool
164 operator!=(const DataVectorAlt<T>& other) const;
165
166 /**
167 \brief
168 Return a reference to the element at position i in this DataVectorAlt.
169 Will throw an exception if an invalid index "i" is given.
170
171 NB: access to the element one past the end of the vector is permitted
172 in order to provide a facility equivalent to an end() pointer.
173 */
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
197 typename DataVectorAlt<T>::size_type
198 DataVectorAlt<T>::size() const
199 {
200 return m_size;
201 }
202
203 template <class T>
204 inline
205 typename DataVectorAlt<T>::reference
206 DataVectorAlt<T>::operator[](const DataVectorAlt::size_type i)
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
214 typename DataVectorAlt<T>::const_reference
215 DataVectorAlt<T>::operator[](const DataVectorAlt::size_type i) const
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>
224 DataTypes::DataVectorAlt<T>::DataVectorAlt() :
225 m_size(0),
226 m_dim(0),
227 m_N(0),
228 m_array_data(0)
229 {
230 }
231
232 template <class T>
233 DataTypes::DataVectorAlt<T>::DataVectorAlt(const DataVectorAlt& other) :
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>
248 DataTypes::DataVectorAlt<T>::DataVectorAlt(const DataVectorAlt<T>::size_type size,
249 const DataVectorAlt<T>::value_type val,
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>
259 DataTypes::DataVectorAlt<T>::~DataVectorAlt()
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
274 DataVectorAlt<T>::resize(const DataVectorAlt<T>::size_type newSize,
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>
313 DataVectorAlt<T>&
314 DataVectorAlt<T>::operator=(const DataVectorAlt& other)
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
339 DataVectorAlt<T>::operator==(const DataVectorAlt& other) const
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
362 DataVectorAlt<T>::operator!=(const DataVectorAlt& other) const
363 {
364 return !(*this==other);
365 }
366
367 template <class T>
368 void
369 DataVectorAlt<T>::copyFromArrayToOffset(const WrappedArray& value, size_type offset, size_type copies)
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
465 DataVectorAlt<T>::copyFromArray(const WrappedArray& value, size_type copies)
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__

  ViewVC Help
Powered by ViewVC 1.1.26