/[escript]/trunk/escript/src/DataVector.cpp
ViewVC logotype

Contents of /trunk/escript/src/DataVector.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26