/[escript]/branches/trilinos_from_5897/escriptcore/src/DataVectorTaipan.cpp
ViewVC logotype

Contents of /branches/trilinos_from_5897/escriptcore/src/DataVectorTaipan.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5963 - (show annotations)
Mon Feb 22 06:59:27 2016 UTC (3 years, 3 months ago) by caltinay
File size: 6940 byte(s)
sync and fix.

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 using namespace DataTypes;
35
36 namespace escript
37 {
38
39 namespace DataTypes
40 {
41
42 Taipan arrayManager;
43
44 void releaseUnusedMemory()
45 {
46 arrayManager.release_unused_arrays();
47 }
48
49
50 DataVectorTaipan::DataVectorTaipan() :
51 m_size(0),
52 m_dim(0),
53 m_N(0),
54 m_array_data(0)
55 {
56 }
57
58 DataVectorTaipan::DataVectorTaipan(const DataVectorTaipan& other) :
59 m_size(other.m_size),
60 m_dim(other.m_dim),
61 m_N(other.m_N),
62 m_array_data(0)
63 {
64 m_array_data = arrayManager.new_array(m_dim,m_N);
65 int i;
66 #pragma omp parallel for private(i) schedule(static)
67 for (i=0; i<m_size; i++) {
68 m_array_data[i] = other.m_array_data[i];
69 }
70 }
71
72 DataVectorTaipan::DataVectorTaipan(const DataVectorTaipan::size_type size,
73 const DataVectorTaipan::value_type val,
74 const DataVectorTaipan::size_type blockSize) :
75 m_size(size),
76 m_dim(blockSize),
77 m_array_data(0)
78 {
79 resize(size, val, blockSize);
80 }
81
82 DataVectorTaipan::~DataVectorTaipan()
83 {
84 // dispose of data array
85 if (m_array_data!=0) {
86 arrayManager.delete_array(m_array_data);
87 }
88
89 // clear data members
90 m_size = -1;
91 m_dim = -1;
92 m_N = -1;
93 m_array_data = 0;
94 }
95
96 void
97 DataVectorTaipan::resize(const DataVectorTaipan::size_type newSize,
98 const DataVectorTaipan::value_type newValue,
99 const DataVectorTaipan::size_type newBlockSize)
100 {
101 assert(m_size >= 0);
102
103 // The < 1 is to catch both ==0 and negatives
104 if ( newBlockSize < 1) {
105 ostringstream oss;
106 oss << "DataVectorTaipan: invalid blockSize specified (" << newBlockSize << ')';
107 throw DataException(oss.str());
108 }
109
110 if ( newSize < 0 ) {
111 ostringstream oss;
112 oss << "DataVectorTaipan: invalid new size specified (" << newSize << ')';
113 throw DataException(oss.str());
114 }
115 if ( (newSize % newBlockSize) != 0) {
116 ostringstream oss;
117 oss << "DataVectorTaipan: newSize is not a multiple of blockSize: (" << newSize << ", " << newBlockSize<< ')';
118 throw DataException(oss.str());
119 }
120
121 if (m_array_data!=0) {
122 arrayManager.delete_array(m_array_data);
123 }
124
125 m_size = newSize;
126 m_dim = newBlockSize;
127 m_N = newSize / newBlockSize;
128 m_array_data = arrayManager.new_array(m_dim,m_N);
129
130 int i;
131 #pragma omp parallel for private(i) schedule(static)
132 for (i=0; i<m_size; i++) {
133 m_array_data[i] = newValue;
134 }
135 }
136
137 DataVectorTaipan&
138 DataVectorTaipan::operator=(const DataVectorTaipan& other)
139 {
140 assert(m_size >= 0);
141
142 if (m_array_data!=0) {
143 arrayManager.delete_array(m_array_data);
144 }
145
146 m_size = other.m_size;
147 m_dim = other.m_dim;
148 m_N = other.m_N;
149
150 m_array_data = arrayManager.new_array(m_dim,m_N);
151 int i;
152 #pragma omp parallel for private(i) schedule(static)
153 for (i=0; i<m_size; i++) {
154 m_array_data[i] = other.m_array_data[i];
155 }
156
157 return *this;
158 }
159
160 bool
161 DataVectorTaipan::operator==(const DataVectorTaipan& other) const
162 {
163 assert(m_size >= 0);
164
165 if (m_size!=other.m_size) {
166 return false;
167 }
168 if (m_dim!=other.m_dim) {
169 return false;
170 }
171 if (m_N!=other.m_N) {
172 return false;
173 }
174 for (int i=0; i<m_size; i++) {
175 if (m_array_data[i] != other.m_array_data[i]) {
176 return false;
177 }
178 }
179 return true;
180 }
181
182 bool
183 DataVectorTaipan::operator!=(const DataVectorTaipan& other) const
184 {
185 return !(*this==other);
186 }
187
188 void
189 DataVectorTaipan::copyFromArrayToOffset(const WrappedArray& value, size_type offset, size_type copies)
190 {
191 using DataTypes::RealVectorType;
192 const DataTypes::ShapeType& tempShape=value.getShape();
193 size_type len=DataTypes::noValues(tempShape);
194 if (offset+len*copies>size())
195 {
196 ostringstream ss;
197 ss << "Error - not enough room for that DataPoint at that offset. (";
198 ss << "offset=" << offset << " + " << " len=" << len << " >= " << size();
199 throw DataException(ss.str());
200 }
201 size_type si=0,sj=0,sk=0,sl=0;
202 switch (value.getRank())
203 {
204 case 0:
205 for (size_type z=0;z<copies;++z)
206 {
207 m_array_data[offset+z]=value.getElt();
208 }
209 break;
210 case 1:
211 for (size_type z=0;z<copies;++z)
212 {
213 for (size_t i=0;i<tempShape[0];++i)
214 {
215 m_array_data[offset+i]=value.getElt(i);
216 }
217 offset+=len;
218 }
219 break;
220 case 2:
221 si=tempShape[0];
222 sj=tempShape[1];
223 for (size_type z=0;z<copies;++z)
224 {
225 for (size_type i=0;i<si;i++)
226 {
227 for (size_type j=0;j<sj;j++)
228 {
229 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j)]=value.getElt(i,j);
230 }
231 }
232 offset+=len;
233 }
234 break;
235 case 3:
236 si=tempShape[0];
237 sj=tempShape[1];
238 sk=tempShape[2];
239 for (size_type z=0;z<copies;++z)
240 {
241 for (size_type i=0;i<si;i++)
242 {
243 for (size_type j=0;j<sj;j++)
244 {
245 for (size_type k=0;k<sk;k++)
246 {
247 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k)]=value.getElt(i,j,k);
248 }
249 }
250 }
251 offset+=len;
252 }
253 break;
254 case 4:
255 si=tempShape[0];
256 sj=tempShape[1];
257 sk=tempShape[2];
258 sl=tempShape[3];
259 for (size_type z=0;z<copies;++z)
260 {
261 for (size_type i=0;i<si;i++)
262 {
263 for (size_type j=0;j<sj;j++)
264 {
265 for (size_type k=0;k<sk;k++)
266 {
267 for (size_type l=0;l<sl;l++)
268 {
269 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k,l)]=value.getElt(i,j,k,l);
270 }
271 }
272 }
273 }
274 offset+=len;
275 }
276 break;
277 default:
278 ostringstream oss;
279 oss << "Error - unknown rank. Rank=" << value.getRank();
280 throw DataException(oss.str());
281 }
282 }
283
284
285 void
286 DataVectorTaipan::copyFromArray(const WrappedArray& value, size_type copies)
287 {
288 using DataTypes::RealVectorType;
289 if (m_array_data!=0) {
290 arrayManager.delete_array(m_array_data);
291 }
292 DataTypes::ShapeType tempShape=value.getShape();
293 DataVectorTaipan::size_type nelements=DataTypes::noValues(tempShape)*copies;
294 m_array_data = arrayManager.new_array(1,nelements);
295 m_size=nelements; // total amount of elements
296 m_dim=m_size; // elements per sample
297 m_N=1; // number of samples
298 copyFromArrayToOffset(value,0,copies);
299 }
300
301
302 }
303 }

  ViewVC Help
Powered by ViewVC 1.1.26