/[escript]/branches/complex/escriptcore/src/DataVector.cpp
ViewVC logotype

Contents of /branches/complex/escriptcore/src/DataVector.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5868 - (show annotations)
Wed Jan 13 07:23:44 2016 UTC (2 years, 11 months ago) by jfenwick
File size: 12502 byte(s)
Adding an alternate DataVector which does not use Taipan
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 #define ESNEEDPYTHON
18 #include "esysUtils/first.h"
19
20
21 #include "DataVector.h"
22
23 #include "Taipan.h"
24 #include "DataException.h"
25 #include <boost/python/extract.hpp>
26 #include "DataTypes.h"
27 #include "WrappedArray.h"
28
29 #include <cassert>
30
31 using namespace std;
32 using namespace escript;
33 using namespace boost::python;
34
35 namespace escript {
36
37 Taipan arrayManager;
38
39 void releaseUnusedMemory()
40 {
41 arrayManager.release_unused_arrays();
42 }
43
44
45 DataVectorTaipan::DataVectorTaipan() :
46 m_size(0),
47 m_dim(0),
48 m_N(0),
49 m_array_data(0)
50 {
51 }
52
53 DataVectorTaipan::DataVectorTaipan(const DataVectorTaipan& other) :
54 m_size(other.m_size),
55 m_dim(other.m_dim),
56 m_N(other.m_N),
57 m_array_data(0)
58 {
59 m_array_data = arrayManager.new_array(m_dim,m_N);
60 int i;
61 #pragma omp parallel for private(i) schedule(static)
62 for (i=0; i<m_size; i++) {
63 m_array_data[i] = other.m_array_data[i];
64 }
65 }
66
67 DataVectorTaipan::DataVectorTaipan(const DataVectorTaipan::size_type size,
68 const DataVectorTaipan::value_type val,
69 const DataVectorTaipan::size_type blockSize) :
70 m_size(size),
71 m_dim(blockSize),
72 m_array_data(0)
73 {
74 resize(size, val, blockSize);
75 }
76
77 DataVectorTaipan::~DataVectorTaipan()
78 {
79 // dispose of data array
80 if (m_array_data!=0) {
81 arrayManager.delete_array(m_array_data);
82 }
83
84 // clear data members
85 m_size = -1;
86 m_dim = -1;
87 m_N = -1;
88 m_array_data = 0;
89 }
90
91 void
92 DataVectorTaipan::resize(const DataVectorTaipan::size_type newSize,
93 const DataVectorTaipan::value_type newValue,
94 const DataVectorTaipan::size_type newBlockSize)
95 {
96 assert(m_size >= 0);
97
98 // The < 1 is to catch both ==0 and negatives
99 if ( newBlockSize < 1) {
100 ostringstream oss;
101 oss << "DataVectorTaipan: invalid blockSize specified (" << newBlockSize << ')';
102 throw DataException(oss.str());
103 }
104
105 if ( newSize < 0 ) {
106 ostringstream oss;
107 oss << "DataVectorTaipan: invalid new size specified (" << newSize << ')';
108 throw DataException(oss.str());
109 }
110 if ( (newSize % newBlockSize) != 0) {
111 ostringstream oss;
112 oss << "DataVectorTaipan: newSize is not a multiple of blockSize: (" << newSize << ", " << newBlockSize<< ')';
113 throw DataException(oss.str());
114 }
115
116 if (m_array_data!=0) {
117 arrayManager.delete_array(m_array_data);
118 }
119
120 m_size = newSize;
121 m_dim = newBlockSize;
122 m_N = newSize / newBlockSize;
123 m_array_data = arrayManager.new_array(m_dim,m_N);
124
125 int i;
126 #pragma omp parallel for private(i) schedule(static)
127 for (i=0; i<m_size; i++) {
128 m_array_data[i] = newValue;
129 }
130 }
131
132 DataVectorTaipan&
133 DataVectorTaipan::operator=(const DataVectorTaipan& other)
134 {
135 assert(m_size >= 0);
136
137 if (m_array_data!=0) {
138 arrayManager.delete_array(m_array_data);
139 }
140
141 m_size = other.m_size;
142 m_dim = other.m_dim;
143 m_N = other.m_N;
144
145 m_array_data = arrayManager.new_array(m_dim,m_N);
146 int i;
147 #pragma omp parallel for private(i) schedule(static)
148 for (i=0; i<m_size; i++) {
149 m_array_data[i] = other.m_array_data[i];
150 }
151
152 return *this;
153 }
154
155 bool
156 DataVectorTaipan::operator==(const DataVectorTaipan& other) const
157 {
158 assert(m_size >= 0);
159
160 if (m_size!=other.m_size) {
161 return false;
162 }
163 if (m_dim!=other.m_dim) {
164 return false;
165 }
166 if (m_N!=other.m_N) {
167 return false;
168 }
169 for (int i=0; i<m_size; i++) {
170 if (m_array_data[i] != other.m_array_data[i]) {
171 return false;
172 }
173 }
174 return true;
175 }
176
177 bool
178 DataVectorTaipan::operator!=(const DataVectorTaipan& other) const
179 {
180 return !(*this==other);
181 }
182
183 void
184 DataVectorTaipan::copyFromArrayToOffset(const WrappedArray& value, size_type offset, size_type copies)
185 {
186 using DataTypes::FloatVectorType;
187 const DataTypes::ShapeType& tempShape=value.getShape();
188 size_type len=DataTypes::noValues(tempShape);
189 if (offset+len*copies>size())
190 {
191 ostringstream ss;
192 ss << "Error - not enough room for that DataPoint at that offset. (";
193 ss << "offset=" << offset << " + " << " len=" << len << " >= " << size();
194 throw DataException(ss.str());
195 }
196 size_type si=0,sj=0,sk=0,sl=0;
197 switch (value.getRank())
198 {
199 case 0:
200 for (size_type z=0;z<copies;++z)
201 {
202 m_array_data[offset+z]=value.getElt();
203 }
204 break;
205 case 1:
206 for (size_type z=0;z<copies;++z)
207 {
208 for (size_t i=0;i<tempShape[0];++i)
209 {
210 m_array_data[offset+i]=value.getElt(i);
211 }
212 offset+=len;
213 }
214 break;
215 case 2:
216 si=tempShape[0];
217 sj=tempShape[1];
218 for (size_type z=0;z<copies;++z)
219 {
220 for (size_type i=0;i<si;i++)
221 {
222 for (size_type j=0;j<sj;j++)
223 {
224 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j)]=value.getElt(i,j);
225 }
226 }
227 offset+=len;
228 }
229 break;
230 case 3:
231 si=tempShape[0];
232 sj=tempShape[1];
233 sk=tempShape[2];
234 for (size_type z=0;z<copies;++z)
235 {
236 for (size_type i=0;i<si;i++)
237 {
238 for (size_type j=0;j<sj;j++)
239 {
240 for (size_type k=0;k<sk;k++)
241 {
242 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k)]=value.getElt(i,j,k);
243 }
244 }
245 }
246 offset+=len;
247 }
248 break;
249 case 4:
250 si=tempShape[0];
251 sj=tempShape[1];
252 sk=tempShape[2];
253 sl=tempShape[3];
254 for (size_type z=0;z<copies;++z)
255 {
256 for (size_type i=0;i<si;i++)
257 {
258 for (size_type j=0;j<sj;j++)
259 {
260 for (size_type k=0;k<sk;k++)
261 {
262 for (size_type l=0;l<sl;l++)
263 {
264 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k,l)]=value.getElt(i,j,k,l);
265 }
266 }
267 }
268 }
269 offset+=len;
270 }
271 break;
272 default:
273 ostringstream oss;
274 oss << "Error - unknown rank. Rank=" << value.getRank();
275 throw DataException(oss.str());
276 }
277 }
278
279
280 void
281 DataVectorTaipan::copyFromArray(const WrappedArray& value, size_type copies)
282 {
283 using DataTypes::FloatVectorType;
284 if (m_array_data!=0) {
285 arrayManager.delete_array(m_array_data);
286 }
287 DataTypes::ShapeType tempShape=value.getShape();
288 DataVectorTaipan::size_type nelements=DataTypes::noValues(tempShape)*copies;
289 m_array_data = arrayManager.new_array(1,nelements);
290 m_size=nelements; // total amount of elements
291 m_dim=m_size; // elements per sample
292 m_N=1; // number of samples
293 copyFromArrayToOffset(value,0,copies);
294 }
295
296
297
298
299
300 ////////////////////////////////////////////////////////////////////////////////
301
302
303 DataVectorAlt::DataVectorAlt() :
304 m_size(0),
305 m_dim(0),
306 m_N(0),
307 m_array_data(0)
308 {
309 }
310
311 DataVectorAlt::DataVectorAlt(const DataVectorAlt& other) :
312 m_size(other.m_size),
313 m_dim(other.m_dim),
314 m_N(other.m_N),
315 m_array_data(0)
316 {
317
318 m_array_data.reserve(other.m_size);
319 int i;
320 #pragma omp parallel for private(i) schedule(static)
321 for (i=0; i<m_size; i++) {
322 m_array_data[i] = other.m_array_data[i];
323 }
324 }
325
326 DataVectorAlt::DataVectorAlt(const DataVectorAlt::size_type size,
327 const DataVectorAlt::value_type val,
328 const DataVectorAlt::size_type blockSize) :
329 m_size(size),
330 m_dim(blockSize),
331 m_array_data(0)
332 {
333 resize(size, val, blockSize);
334 }
335
336 DataVectorAlt::~DataVectorAlt()
337 {
338 // clear data members
339 m_size = -1;
340 m_dim = -1;
341 m_N = -1;
342 }
343
344 void
345 DataVectorAlt::resize(const DataVectorAlt::size_type newSize,
346 const DataVectorAlt::value_type newValue,
347 const DataVectorAlt::size_type newBlockSize)
348 {
349 assert(m_size >= 0);
350
351 // The < 1 is to catch both ==0 and negatives
352 if ( newBlockSize < 1) {
353 ostringstream oss;
354 oss << "DataVectorAlt: invalid blockSize specified (" << newBlockSize << ')';
355 throw DataException(oss.str());
356 }
357
358 if ( newSize < 0 ) {
359 ostringstream oss;
360 oss << "DataVectorAlt: invalid new size specified (" << newSize << ')';
361 throw DataException(oss.str());
362 }
363 if ( (newSize % newBlockSize) != 0) {
364 ostringstream oss;
365 oss << "DataVectorAlt: newSize is not a multiple of blockSize: (" << newSize << ", " << newBlockSize<< ')';
366 throw DataException(oss.str());
367 }
368
369 m_size = newSize;
370 m_dim = newBlockSize;
371 m_N = newSize / newBlockSize;
372 m_array_data.reserve(newSize); // unlike the taipan version resize does not allocate a new array
373 // I'm hoping this won't lead to different first touch properties
374 int i;
375 #pragma omp parallel for private(i) schedule(static)
376 for (i=0; i<m_size; i++) {
377 m_array_data[i] = newValue;
378 }
379 }
380
381 DataVectorAlt&
382 DataVectorAlt::operator=(const DataVectorAlt& other)
383 {
384 assert(m_size >= 0);
385
386
387 m_size = other.m_size;
388 m_dim = other.m_dim;
389 m_N = other.m_N;
390
391 m_array_data.reserve(m_size);
392 int i;
393 #pragma omp parallel for private(i) schedule(static)
394 for (i=0; i<m_size; i++) {
395 m_array_data[i] = other.m_array_data[i];
396 }
397
398 return *this;
399 }
400
401 bool
402 DataVectorAlt::operator==(const DataVectorAlt& other) const
403 {
404 assert(m_size >= 0);
405
406 if (m_size!=other.m_size) {
407 return false;
408 }
409 if (m_dim!=other.m_dim) {
410 return false;
411 }
412 if (m_N!=other.m_N) {
413 return false;
414 }
415 for (int i=0; i<m_size; i++) {
416 if (m_array_data[i] != other.m_array_data[i]) {
417 return false;
418 }
419 }
420 return true;
421 }
422
423 bool
424 DataVectorAlt::operator!=(const DataVectorAlt& other) const
425 {
426 return !(*this==other);
427 }
428
429 void
430 DataVectorAlt::copyFromArrayToOffset(const WrappedArray& value, size_type offset, size_type copies)
431 {
432 using DataTypes::FloatVectorType;
433 const DataTypes::ShapeType& tempShape=value.getShape();
434 size_type len=DataTypes::noValues(tempShape);
435 if (offset+len*copies>size())
436 {
437 ostringstream ss;
438 ss << "Error - not enough room for that DataPoint at that offset. (";
439 ss << "offset=" << offset << " + " << " len=" << len << " >= " << size();
440 throw DataException(ss.str());
441 }
442 size_type si=0,sj=0,sk=0,sl=0;
443 switch (value.getRank())
444 {
445 case 0:
446 for (size_type z=0;z<copies;++z)
447 {
448 m_array_data[offset+z]=value.getElt();
449 }
450 break;
451 case 1:
452 for (size_type z=0;z<copies;++z)
453 {
454 for (size_t i=0;i<tempShape[0];++i)
455 {
456 m_array_data[offset+i]=value.getElt(i);
457 }
458 offset+=len;
459 }
460 break;
461 case 2:
462 si=tempShape[0];
463 sj=tempShape[1];
464 for (size_type z=0;z<copies;++z)
465 {
466 for (size_type i=0;i<si;i++)
467 {
468 for (size_type j=0;j<sj;j++)
469 {
470 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j)]=value.getElt(i,j);
471 }
472 }
473 offset+=len;
474 }
475 break;
476 case 3:
477 si=tempShape[0];
478 sj=tempShape[1];
479 sk=tempShape[2];
480 for (size_type z=0;z<copies;++z)
481 {
482 for (size_type i=0;i<si;i++)
483 {
484 for (size_type j=0;j<sj;j++)
485 {
486 for (size_type k=0;k<sk;k++)
487 {
488 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k)]=value.getElt(i,j,k);
489 }
490 }
491 }
492 offset+=len;
493 }
494 break;
495 case 4:
496 si=tempShape[0];
497 sj=tempShape[1];
498 sk=tempShape[2];
499 sl=tempShape[3];
500 for (size_type z=0;z<copies;++z)
501 {
502 for (size_type i=0;i<si;i++)
503 {
504 for (size_type j=0;j<sj;j++)
505 {
506 for (size_type k=0;k<sk;k++)
507 {
508 for (size_type l=0;l<sl;l++)
509 {
510 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k,l)]=value.getElt(i,j,k,l);
511 }
512 }
513 }
514 }
515 offset+=len;
516 }
517 break;
518 default:
519 ostringstream oss;
520 oss << "Error - unknown rank. Rank=" << value.getRank();
521 throw DataException(oss.str());
522 }
523 }
524
525
526 void
527 DataVectorAlt::copyFromArray(const WrappedArray& value, size_type copies)
528 {
529 using DataTypes::FloatVectorType;
530 DataTypes::ShapeType tempShape=value.getShape();
531 DataVectorAlt::size_type nelements=DataTypes::noValues(tempShape)*copies;
532 m_array_data.reserve(nelements);
533 m_size=nelements; // total amount of elements
534 m_dim=m_size; // elements per sample
535 m_N=1; // number of samples
536 copyFromArrayToOffset(value,0,copies);
537 }
538
539
540
541
542
543 } // end of namespace

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26