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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1796 - (hide annotations)
Wed Sep 17 01:45:46 2008 UTC (11 years ago) by jfenwick
Original Path: trunk/escript/src/DataVector.cpp
File size: 5421 byte(s)
Merged noarrayview branch onto trunk.


1 jgs 117
2 ksteube 1312 /* $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 jgs 474 #include "DataVector.h"
17 jgs 117
18 jgs 477 #include "Taipan.h"
19     #include "DataException.h"
20 jfenwick 1796 #include <boost/python/extract.hpp>
21     #include "DataTypes.h"
22 jgs 477
23     #include <cassert>
24    
25 jgs 117 using namespace std;
26 jgs 121 using namespace escript;
27 jfenwick 1796 using namespace boost::python;
28 jgs 117
29     namespace escript {
30    
31 jgs 121 Taipan arrayManager;
32    
33 gross 797 void releaseUnusedMemory()
34     {
35     arrayManager.release_unused_arrays();
36     }
37    
38    
39 jgs 121 DataVector::DataVector() :
40     m_array_data(0),
41     m_size(0),
42     m_dim(0),
43     m_N(0)
44     {
45 jgs 117 }
46    
47 jgs 121 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 jgs 122 int i;
55     #pragma omp parallel for private(i) schedule(static)
56     for (i=0; i<m_size; i++) {
57 jgs 121 m_array_data[i] = other.m_array_data[i];
58     }
59 jgs 117 }
60    
61 jgs 121 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 jgs 117 }
70    
71 jgs 121 DataVector::~DataVector()
72     {
73     // dispose of data array
74 jgs 151 if (m_array_data!=0) {
75     arrayManager.delete_array(m_array_data);
76     }
77 jgs 121
78     // clear data members
79     m_size = -1;
80     m_dim = -1;
81     m_N = -1;
82     m_array_data = 0;
83 jgs 117 }
84    
85 jgs 121 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 jgs 151 if ( newBlockSize == 0) {
93 ksteube 1250 throw DataException("DataVector: invalid blockSize specified (newBlockSize)");
94 jgs 151 }
95    
96 jgs 121 if ( (newSize % newBlockSize) != 0) {
97     throw DataException("DataVector: invalid blockSize specified");
98     }
99    
100 jgs 151 if (m_array_data!=0) {
101     arrayManager.delete_array(m_array_data);
102     }
103 jgs 121
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 jgs 122 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 jgs 121 }
114     }
115    
116     DataVector&
117     DataVector::operator=(const DataVector& other)
118     {
119     assert(m_size >= 0);
120    
121 jgs 151 if (m_array_data!=0) {
122     arrayManager.delete_array(m_array_data);
123     }
124 jgs 121
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 jgs 122 int i;
131     #pragma omp parallel for private(i) schedule(static)
132     for (i=0; i<m_size; i++) {
133 jgs 121 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 jfenwick 1796
168     void
169     DataVector::copyFromNumArray(const boost::python::numeric::array& value)
170 jgs 123 {
171 jfenwick 1796 using DataTypes::ValueType;
172     if (m_array_data!=0) {
173     arrayManager.delete_array(m_array_data);
174 jgs 123 }
175    
176    
177 jfenwick 1796 m_array_data = arrayManager.new_array(1,value.nelements());
178    
179     int si=0,sj=0,sk=0,sl=0; // bounds for each dimension of the shape
180     DataTypes::ShapeType tempShape;
181     for (int i=0; i<value.getrank(); i++) {
182     tempShape.push_back(extract<int>(value.getshape()[i]));
183 jgs 123 }
184    
185 jfenwick 1796 if (value.getrank()==0) {
186     m_array_data[0]=extract<double>(value[value.getshape()]);
187     } else if (value.getrank()==1) {
188     si=tempShape[0];
189     for (ValueType::size_type i=0;i<si;i++) {
190     m_array_data[i]=extract<double>(value[i]);
191     }
192     } else if (value.getrank()==2) {
193     si=tempShape[0];
194     sj=tempShape[1];
195     for (ValueType::size_type i=0;i<si;i++) {
196     for (ValueType::size_type j=0;j<sj;j++) {
197     m_array_data[DataTypes::getRelIndex(tempShape,i,j)]=extract<double>(value[i][j]);
198     }
199     }
200     } else if (value.getrank()==3) {
201     si=tempShape[0];
202     sj=tempShape[1];
203     sk=tempShape[2];
204     for (ValueType::size_type i=0;i<si;i++) {
205     for (ValueType::size_type j=0;j<sj;j++) {
206     for (ValueType::size_type k=0;k<sk;k++) {
207     m_array_data[DataTypes::getRelIndex(tempShape,i,j,k)]=extract<double>(value[i][j][k]);
208     }
209     }
210     }
211     } else if (value.getrank()==4) {
212     si=tempShape[0];
213     sj=tempShape[1];
214     sk=tempShape[2];
215     sl=tempShape[3];
216     for (ValueType::size_type i=0;i<si;i++) {
217     for (ValueType::size_type j=0;j<sj;j++) {
218     for (ValueType::size_type k=0;k<sk;k++) {
219     for (ValueType::size_type l=0;l<sl;l++) {
220     m_array_data[DataTypes::getRelIndex(tempShape,i,j,k,l)]=extract<double>(value[i][j][k][l]);
221     }
222     }
223     }
224     }
225     }
226     m_size=value.nelements(); // total amount of elements
227     m_dim=m_size; // elements per sample
228     m_N=1; // number of samples
229 jgs 123 }
230 jfenwick 1796
231 jgs 123
232    
233 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