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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1811 - (hide annotations)
Thu Sep 25 23:11:13 2008 UTC (11 years ago) by ksteube
File size: 5386 byte(s)
Copyright updated in all files

1 jgs 117
2 ksteube 1312 /*******************************************************
3 ksteube 1811 *
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 ksteube 1312
14 ksteube 1811
15 jgs 474 #include "DataVector.h"
16 jgs 117
17 jgs 477 #include "Taipan.h"
18     #include "DataException.h"
19 jfenwick 1796 #include <boost/python/extract.hpp>
20     #include "DataTypes.h"
21 jgs 477
22     #include <cassert>
23    
24 jgs 117 using namespace std;
25 jgs 121 using namespace escript;
26 jfenwick 1796 using namespace boost::python;
27 jgs 117
28     namespace escript {
29    
30 jgs 121 Taipan arrayManager;
31    
32 gross 797 void releaseUnusedMemory()
33     {
34     arrayManager.release_unused_arrays();
35     }
36    
37    
38 jgs 121 DataVector::DataVector() :
39     m_array_data(0),
40     m_size(0),
41     m_dim(0),
42     m_N(0)
43     {
44 jgs 117 }
45    
46 jgs 121 DataVector::DataVector(const DataVector& other) :
47     m_array_data(0),
48     m_size(other.m_size),
49     m_dim(other.m_dim),
50     m_N(other.m_N)
51     {
52     m_array_data = arrayManager.new_array(m_dim,m_N);
53 jgs 122 int i;
54     #pragma omp parallel for private(i) schedule(static)
55     for (i=0; i<m_size; i++) {
56 jgs 121 m_array_data[i] = other.m_array_data[i];
57     }
58 jgs 117 }
59    
60 jgs 121 DataVector::DataVector(const DataVector::size_type size,
61     const DataVector::value_type val,
62     const DataVector::size_type blockSize) :
63     m_array_data(0),
64     m_size(size),
65     m_dim(blockSize)
66     {
67     resize(size, val, blockSize);
68 jgs 117 }
69    
70 jgs 121 DataVector::~DataVector()
71     {
72     // dispose of data array
73 jgs 151 if (m_array_data!=0) {
74     arrayManager.delete_array(m_array_data);
75     }
76 jgs 121
77     // clear data members
78     m_size = -1;
79     m_dim = -1;
80     m_N = -1;
81     m_array_data = 0;
82 jgs 117 }
83    
84 jgs 121 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 jgs 151 if ( newBlockSize == 0) {
92 ksteube 1250 throw DataException("DataVector: invalid blockSize specified (newBlockSize)");
93 jgs 151 }
94    
95 jgs 121 if ( (newSize % newBlockSize) != 0) {
96     throw DataException("DataVector: invalid blockSize specified");
97     }
98    
99 jgs 151 if (m_array_data!=0) {
100     arrayManager.delete_array(m_array_data);
101     }
102 jgs 121
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 jgs 122 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 jgs 121 }
113     }
114    
115     DataVector&
116     DataVector::operator=(const DataVector& other)
117     {
118     assert(m_size >= 0);
119    
120 jgs 151 if (m_array_data!=0) {
121     arrayManager.delete_array(m_array_data);
122     }
123 jgs 121
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 jgs 122 int i;
130     #pragma omp parallel for private(i) schedule(static)
131     for (i=0; i<m_size; i++) {
132 jgs 121 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 jfenwick 1796
167     void
168     DataVector::copyFromNumArray(const boost::python::numeric::array& value)
169 jgs 123 {
170 jfenwick 1796 using DataTypes::ValueType;
171     if (m_array_data!=0) {
172     arrayManager.delete_array(m_array_data);
173 jgs 123 }
174    
175    
176 jfenwick 1796 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 jgs 123 }
183    
184 jfenwick 1796 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 jgs 123 }
229 jfenwick 1796
230 jgs 123
231    
232 jgs 117 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26