/[escript]/branches/arrexp_2137_win_merge/escript/src/DataVector.cpp
ViewVC logotype

Contents of /branches/arrexp_2137_win_merge/escript/src/DataVector.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2219 - (show annotations)
Wed Jan 14 04:31:34 2009 UTC (10 years, 8 months ago) by jfenwick
File size: 6213 byte(s)
Merging changes from trunk 2098:2218
1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 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 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 void
168 DataVector::copyFromArrayToOffset(const WrappedArray& value, size_type offset, size_type copies)
169 {
170 using DataTypes::ValueType;
171 const DataTypes::ShapeType& tempShape=value.getShape();
172 size_type len=DataTypes::noValues(tempShape);
173 if (offset+len*copies>size())
174 {
175 ostringstream ss;
176 ss << "Error - not enough room for that DataPoint at that offset. (";
177 ss << "offset=" << offset << " + " << " len=" << len << " >= " << size();
178 throw DataException(ss.str());
179 }
180 size_t si=0,sj=0,sk=0,sl=0;
181 switch (value.getRank())
182 {
183 case 0:
184 for (size_type z=0;z<copies;++z)
185 {
186 m_array_data[offset]=value.getElt();
187 }
188 break;
189 case 1:
190 for (size_type z=0;z<copies;++z)
191 {
192 for (size_t i=0;i<tempShape[0];++i)
193 {
194 m_array_data[offset+i]=value.getElt(i);
195 }
196 offset+=z*len;
197 }
198 break;
199 case 2:
200 si=tempShape[0];
201 sj=tempShape[1];
202 for (size_type z=0;z<copies;++z)
203 {
204 for (ValueType::size_type i=0;i<si;i++)
205 {
206 for (ValueType::size_type j=0;j<sj;j++)
207 {
208 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j)]=value.getElt(i,j);
209 }
210 }
211 offset+=z*len;
212 }
213 break;
214 case 3:
215 si=tempShape[0];
216 sj=tempShape[1];
217 sk=tempShape[2];
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 for (ValueType::size_type k=0;k<sk;k++)
225 {
226 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k)]=value.getElt(i,j,k);
227 }
228 }
229 }
230 offset+=len;
231 }
232 break;
233 case 4:
234 si=tempShape[0];
235 sj=tempShape[1];
236 sk=tempShape[2];
237 sl=tempShape[3];
238 for (size_type z=0;z<copies;++z)
239 {
240 for (ValueType::size_type i=0;i<si;i++)
241 {
242 for (ValueType::size_type j=0;j<sj;j++)
243 {
244 for (ValueType::size_type k=0;k<sk;k++)
245 {
246 for (ValueType::size_type l=0;l<sl;l++)
247 {
248 m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k,l)]=value.getElt(i,j,k,l);
249 }
250 }
251 }
252 }
253 offset+=len;
254 }
255 break;
256 default:
257 ostringstream oss;
258 oss << "Error - unknown rank. Rank=" << value.getRank();
259 throw DataException(oss.str());
260 }
261 }
262
263
264 void
265 DataVector::copyFromArray(const WrappedArray& value, size_type copies)
266 {
267 using DataTypes::ValueType;
268 if (m_array_data!=0) {
269 arrayManager.delete_array(m_array_data);
270 }
271 DataTypes::ShapeType tempShape=value.getShape();
272 DataVector::size_type nelements=DataTypes::noValues(tempShape)*copies;
273 m_array_data = arrayManager.new_array(1,nelements);
274 m_size=nelements; // total amount of elements
275 m_dim=m_size; // elements per sample
276 m_N=1; // number of samples
277 copyFromArrayToOffset(value,0,copies);
278 }
279
280 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26