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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1977 - (show annotations)
Thu Nov 6 03:54:35 2008 UTC (10 years, 9 months ago) by jfenwick
File size: 5386 byte(s)
More warning removal.
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
22 #include <cassert>
23
24 using namespace std;
25 using namespace escript;
26 using namespace boost::python;
27
28 namespace escript {
29
30 Taipan arrayManager;
31
32 void releaseUnusedMemory()
33 {
34 arrayManager.release_unused_arrays();
35 }
36
37
38 DataVector::DataVector() :
39 m_size(0),
40 m_dim(0),
41 m_N(0),
42 m_array_data(0)
43 {
44 }
45
46 DataVector::DataVector(const DataVector& other) :
47 m_size(other.m_size),
48 m_dim(other.m_dim),
49 m_N(other.m_N),
50 m_array_data(0)
51 {
52 m_array_data = arrayManager.new_array(m_dim,m_N);
53 int i;
54 #pragma omp parallel for private(i) schedule(static)
55 for (i=0; i<m_size; i++) {
56 m_array_data[i] = other.m_array_data[i];
57 }
58 }
59
60 DataVector::DataVector(const DataVector::size_type size,
61 const DataVector::value_type val,
62 const DataVector::size_type blockSize) :
63 m_size(size),
64 m_dim(blockSize),
65 m_array_data(0)
66 {
67 resize(size, val, blockSize);
68 }
69
70 DataVector::~DataVector()
71 {
72 // dispose of data array
73 if (m_array_data!=0) {
74 arrayManager.delete_array(m_array_data);
75 }
76
77 // clear data members
78 m_size = -1;
79 m_dim = -1;
80 m_N = -1;
81 m_array_data = 0;
82 }
83
84 void
85 DataVector::resize(const DataVector::size_type newSize,
86 const DataVector::value_type newValue,
87 const DataVector::size_type newBlockSize)
88 {
89 assert(m_size >= 0);
90
91 if ( newBlockSize == 0) {
92 throw DataException("DataVector: invalid blockSize specified (newBlockSize)");
93 }
94
95 if ( (newSize % newBlockSize) != 0) {
96 throw DataException("DataVector: invalid blockSize specified");
97 }
98
99 if (m_array_data!=0) {
100 arrayManager.delete_array(m_array_data);
101 }
102
103 m_size = newSize;
104 m_dim = newBlockSize;
105 m_N = newSize / newBlockSize;
106 m_array_data = arrayManager.new_array(m_dim,m_N);
107
108 int i;
109 #pragma omp parallel for private(i) schedule(static)
110 for (i=0; i<m_size; i++) {
111 m_array_data[i] = newValue;
112 }
113 }
114
115 DataVector&
116 DataVector::operator=(const DataVector& other)
117 {
118 assert(m_size >= 0);
119
120 if (m_array_data!=0) {
121 arrayManager.delete_array(m_array_data);
122 }
123
124 m_size = other.m_size;
125 m_dim = other.m_dim;
126 m_N = other.m_N;
127
128 m_array_data = arrayManager.new_array(m_dim,m_N);
129 int i;
130 #pragma omp parallel for private(i) schedule(static)
131 for (i=0; i<m_size; i++) {
132 m_array_data[i] = other.m_array_data[i];
133 }
134
135 return *this;
136 }
137
138 bool
139 DataVector::operator==(const DataVector& other) const
140 {
141 assert(m_size >= 0);
142
143 if (m_size!=other.m_size) {
144 return false;
145 }
146 if (m_dim!=other.m_dim) {
147 return false;
148 }
149 if (m_N!=other.m_N) {
150 return false;
151 }
152 for (int i=0; i<m_size; i++) {
153 if (m_array_data[i] != other.m_array_data[i]) {
154 return false;
155 }
156 }
157 return true;
158 }
159
160 bool
161 DataVector::operator!=(const DataVector& other) const
162 {
163 return !(*this==other);
164 }
165
166
167 void
168 DataVector::copyFromNumArray(const boost::python::numeric::array& value)
169 {
170 using DataTypes::ValueType;
171 if (m_array_data!=0) {
172 arrayManager.delete_array(m_array_data);
173 }
174
175
176 m_array_data = arrayManager.new_array(1,value.nelements());
177
178 int si=0,sj=0,sk=0,sl=0; // bounds for each dimension of the shape
179 DataTypes::ShapeType tempShape;
180 for (int i=0; i<value.getrank(); i++) {
181 tempShape.push_back(extract<int>(value.getshape()[i]));
182 }
183
184 if (value.getrank()==0) {
185 m_array_data[0]=extract<double>(value[value.getshape()]);
186 } else if (value.getrank()==1) {
187 si=tempShape[0];
188 for (ValueType::size_type i=0;i<si;i++) {
189 m_array_data[i]=extract<double>(value[i]);
190 }
191 } else if (value.getrank()==2) {
192 si=tempShape[0];
193 sj=tempShape[1];
194 for (ValueType::size_type i=0;i<si;i++) {
195 for (ValueType::size_type j=0;j<sj;j++) {
196 m_array_data[DataTypes::getRelIndex(tempShape,i,j)]=extract<double>(value[i][j]);
197 }
198 }
199 } else if (value.getrank()==3) {
200 si=tempShape[0];
201 sj=tempShape[1];
202 sk=tempShape[2];
203 for (ValueType::size_type i=0;i<si;i++) {
204 for (ValueType::size_type j=0;j<sj;j++) {
205 for (ValueType::size_type k=0;k<sk;k++) {
206 m_array_data[DataTypes::getRelIndex(tempShape,i,j,k)]=extract<double>(value[i][j][k]);
207 }
208 }
209 }
210 } else if (value.getrank()==4) {
211 si=tempShape[0];
212 sj=tempShape[1];
213 sk=tempShape[2];
214 sl=tempShape[3];
215 for (ValueType::size_type i=0;i<si;i++) {
216 for (ValueType::size_type j=0;j<sj;j++) {
217 for (ValueType::size_type k=0;k<sk;k++) {
218 for (ValueType::size_type l=0;l<sl;l++) {
219 m_array_data[DataTypes::getRelIndex(tempShape,i,j,k,l)]=extract<double>(value[i][j][k][l]);
220 }
221 }
222 }
223 }
224 }
225 m_size=value.nelements(); // total amount of elements
226 m_dim=m_size; // elements per sample
227 m_N=1; // number of samples
228 }
229
230
231
232 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26