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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2881 - (show annotations)
Thu Jan 28 02:03:15 2010 UTC (9 years, 6 months ago) by jfenwick
File size: 6577 byte(s)
Don't panic.
Updating copyright stamps

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26