1 |
|
2 |
/* $Id$ */ |
3 |
|
4 |
/******************************************************* |
5 |
* |
6 |
* Copyright 2003-2007 by ACceSS MNRF |
7 |
* Copyright 2007 by University of Queensland |
8 |
* |
9 |
* http://esscc.uq.edu.au |
10 |
* Primary Business: Queensland, Australia |
11 |
* Licensed under the Open Software License version 3.0 |
12 |
* http://www.opensource.org/licenses/osl-3.0.php |
13 |
* |
14 |
*******************************************************/ |
15 |
|
16 |
#include "DataVector.h" |
17 |
|
18 |
#include "Taipan.h" |
19 |
#include "DataException.h" |
20 |
#include <boost/python/extract.hpp> |
21 |
#include "DataTypes.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_array_data(0), |
41 |
m_size(0), |
42 |
m_dim(0), |
43 |
m_N(0) |
44 |
{ |
45 |
} |
46 |
|
47 |
DataVector::DataVector(const DataVector& other) : |
48 |
m_array_data(0), |
49 |
m_size(other.m_size), |
50 |
m_dim(other.m_dim), |
51 |
m_N(other.m_N) |
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_array_data(0), |
65 |
m_size(size), |
66 |
m_dim(blockSize) |
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 |
if ( newBlockSize == 0) { |
93 |
throw DataException("DataVector: invalid blockSize specified (newBlockSize)"); |
94 |
} |
95 |
|
96 |
if ( (newSize % newBlockSize) != 0) { |
97 |
throw DataException("DataVector: invalid blockSize specified"); |
98 |
} |
99 |
|
100 |
if (m_array_data!=0) { |
101 |
arrayManager.delete_array(m_array_data); |
102 |
} |
103 |
|
104 |
m_size = newSize; |
105 |
m_dim = newBlockSize; |
106 |
m_N = newSize / newBlockSize; |
107 |
m_array_data = arrayManager.new_array(m_dim,m_N); |
108 |
|
109 |
int i; |
110 |
#pragma omp parallel for private(i) schedule(static) |
111 |
for (i=0; i<m_size; i++) { |
112 |
m_array_data[i] = newValue; |
113 |
} |
114 |
} |
115 |
|
116 |
DataVector& |
117 |
DataVector::operator=(const DataVector& other) |
118 |
{ |
119 |
assert(m_size >= 0); |
120 |
|
121 |
if (m_array_data!=0) { |
122 |
arrayManager.delete_array(m_array_data); |
123 |
} |
124 |
|
125 |
m_size = other.m_size; |
126 |
m_dim = other.m_dim; |
127 |
m_N = other.m_N; |
128 |
|
129 |
m_array_data = arrayManager.new_array(m_dim,m_N); |
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] = other.m_array_data[i]; |
134 |
} |
135 |
|
136 |
return *this; |
137 |
} |
138 |
|
139 |
bool |
140 |
DataVector::operator==(const DataVector& other) const |
141 |
{ |
142 |
assert(m_size >= 0); |
143 |
|
144 |
if (m_size!=other.m_size) { |
145 |
return false; |
146 |
} |
147 |
if (m_dim!=other.m_dim) { |
148 |
return false; |
149 |
} |
150 |
if (m_N!=other.m_N) { |
151 |
return false; |
152 |
} |
153 |
for (int i=0; i<m_size; i++) { |
154 |
if (m_array_data[i] != other.m_array_data[i]) { |
155 |
return false; |
156 |
} |
157 |
} |
158 |
return true; |
159 |
} |
160 |
|
161 |
bool |
162 |
DataVector::operator!=(const DataVector& other) const |
163 |
{ |
164 |
return !(*this==other); |
165 |
} |
166 |
|
167 |
int |
168 |
DataVector::archiveData(ofstream& archiveFile, |
169 |
const size_type noValues) const |
170 |
{ |
171 |
// |
172 |
// Check number of values expected to be written matches number in this object |
173 |
if (noValues != size()) { |
174 |
return 2; |
175 |
} |
176 |
|
177 |
// |
178 |
// Write all values in this object out to archiveFile |
179 |
for (int i=0; i<size(); i++) { |
180 |
archiveFile.write(reinterpret_cast<char *>(&m_array_data[i]),sizeof(double)); |
181 |
} |
182 |
|
183 |
// |
184 |
// Check no errors were encountered before returning |
185 |
if (!archiveFile.good()) { |
186 |
return 1; |
187 |
} |
188 |
|
189 |
return 0; |
190 |
} |
191 |
|
192 |
int |
193 |
DataVector::extractData(ifstream& archiveFile, |
194 |
const size_type noValues) |
195 |
{ |
196 |
// |
197 |
// Check number of values expected to be read matches number in this object |
198 |
if (noValues != size()) { |
199 |
return 2; |
200 |
} |
201 |
|
202 |
// |
203 |
// Read all values in archiveFile back to this object |
204 |
for (int i=0; i<size(); i++) { |
205 |
archiveFile.read(reinterpret_cast<char *>(&m_array_data[i]),sizeof(double)); |
206 |
} |
207 |
|
208 |
// |
209 |
// Check no errors were encountered before returning |
210 |
if (!archiveFile.good()) { |
211 |
return 1; |
212 |
} |
213 |
|
214 |
return 0; |
215 |
} |
216 |
|
217 |
|
218 |
void |
219 |
DataVector::copyFromNumArray(const boost::python::numeric::array& value) |
220 |
{ |
221 |
using DataTypes::ValueType; |
222 |
if (m_array_data!=0) { |
223 |
arrayManager.delete_array(m_array_data); |
224 |
} |
225 |
|
226 |
|
227 |
m_array_data = arrayManager.new_array(1,value.nelements()); |
228 |
|
229 |
int si=0,sj=0,sk=0,sl=0; // bounds for each dimension of the shape |
230 |
DataTypes::ShapeType tempShape; |
231 |
for (int i=0; i<value.getrank(); i++) { |
232 |
tempShape.push_back(extract<int>(value.getshape()[i])); |
233 |
} |
234 |
|
235 |
if (value.getrank()==0) { |
236 |
m_array_data[0]=extract<double>(value[value.getshape()]); |
237 |
} else if (value.getrank()==1) { |
238 |
si=tempShape[0]; |
239 |
for (ValueType::size_type i=0;i<si;i++) { |
240 |
m_array_data[i]=extract<double>(value[i]); |
241 |
} |
242 |
} else if (value.getrank()==2) { |
243 |
si=tempShape[0]; |
244 |
sj=tempShape[1]; |
245 |
for (ValueType::size_type i=0;i<si;i++) { |
246 |
for (ValueType::size_type j=0;j<sj;j++) { |
247 |
m_array_data[DataTypes::getRelIndex(tempShape,i,j)]=extract<double>(value[i][j]); |
248 |
} |
249 |
} |
250 |
} else if (value.getrank()==3) { |
251 |
si=tempShape[0]; |
252 |
sj=tempShape[1]; |
253 |
sk=tempShape[2]; |
254 |
for (ValueType::size_type i=0;i<si;i++) { |
255 |
for (ValueType::size_type j=0;j<sj;j++) { |
256 |
for (ValueType::size_type k=0;k<sk;k++) { |
257 |
m_array_data[DataTypes::getRelIndex(tempShape,i,j,k)]=extract<double>(value[i][j][k]); |
258 |
} |
259 |
} |
260 |
} |
261 |
} else if (value.getrank()==4) { |
262 |
si=tempShape[0]; |
263 |
sj=tempShape[1]; |
264 |
sk=tempShape[2]; |
265 |
sl=tempShape[3]; |
266 |
for (ValueType::size_type i=0;i<si;i++) { |
267 |
for (ValueType::size_type j=0;j<sj;j++) { |
268 |
for (ValueType::size_type k=0;k<sk;k++) { |
269 |
for (ValueType::size_type l=0;l<sl;l++) { |
270 |
m_array_data[DataTypes::getRelIndex(tempShape,i,j,k,l)]=extract<double>(value[i][j][k][l]); |
271 |
} |
272 |
} |
273 |
} |
274 |
} |
275 |
} |
276 |
} |
277 |
|
278 |
|
279 |
|
280 |
} // end of namespace |