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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5504 - (show annotations)
Wed Mar 4 22:58:13 2015 UTC (4 years, 1 month ago) by jfenwick
File size: 6815 byte(s)
Again with a more up to date copy


1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2015 by 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 DataVector::DataVector() :
46 m_size(0),
47 m_dim(0),
48 m_N(0),
49 m_array_data(0)
50 {
51 }
52
53 DataVector::DataVector(const DataVector& 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 DataVector::DataVector(const DataVector::size_type size,
68 const DataVector::value_type val,
69 const DataVector::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 DataVector::~DataVector()
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 DataVector::resize(const DataVector::size_type newSize,
93 const DataVector::value_type newValue,
94 const DataVector::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 << "DataVector: invalid blockSize specified (" << newBlockSize << ')';
102 throw DataException(oss.str());
103 }
104
105 if ( newSize < 0 ) {
106 ostringstream oss;
107 oss << "DataVector: invalid new size specified (" << newSize << ')';
108 throw DataException(oss.str());
109 }
110 if ( (newSize % newBlockSize) != 0) {
111 ostringstream oss;
112 oss << "DataVector: 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 DataVector&
133 DataVector::operator=(const DataVector& 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 DataVector::operator==(const DataVector& 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 DataVector::operator!=(const DataVector& other) const
179 {
180 return !(*this==other);
181 }
182
183 void
184 DataVector::copyFromArrayToOffset(const WrappedArray& value, size_type offset, size_type copies)
185 {
186 using DataTypes::ValueType;
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_t 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 (ValueType::size_type i=0;i<si;i++)
221 {
222 for (ValueType::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 (ValueType::size_type i=0;i<si;i++)
237 {
238 for (ValueType::size_type j=0;j<sj;j++)
239 {
240 for (ValueType::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 (ValueType::size_type i=0;i<si;i++)
257 {
258 for (ValueType::size_type j=0;j<sj;j++)
259 {
260 for (ValueType::size_type k=0;k<sk;k++)
261 {
262 for (ValueType::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 DataVector::copyFromArray(const WrappedArray& value, size_type copies)
282 {
283 using DataTypes::ValueType;
284 if (m_array_data!=0) {
285 arrayManager.delete_array(m_array_data);
286 }
287 DataTypes::ShapeType tempShape=value.getShape();
288 DataVector::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 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26