1 |
jgs |
117 |
/* |
2 |
|
|
****************************************************************************** |
3 |
|
|
* * |
4 |
|
|
* COPYRIGHT ACcESS 2004 - All Rights Reserved * |
5 |
|
|
* * |
6 |
|
|
* This software is the property of ACcESS. No part of this code * |
7 |
|
|
* may be copied in any form or by any means without the expressed written * |
8 |
|
|
* consent of ACcESS. Copying, use or modification of this software * |
9 |
|
|
* by any unauthorised person is illegal unless that person has a software * |
10 |
|
|
* license agreement with ACcESS. * |
11 |
|
|
* * |
12 |
|
|
****************************************************************************** |
13 |
|
|
*/ |
14 |
|
|
|
15 |
jgs |
474 |
#include "DataVector.h" |
16 |
jgs |
117 |
|
17 |
jgs |
477 |
#include "Taipan.h" |
18 |
|
|
#include "DataException.h" |
19 |
|
|
|
20 |
|
|
#include <cassert> |
21 |
|
|
|
22 |
jgs |
117 |
using namespace std; |
23 |
jgs |
121 |
using namespace escript; |
24 |
jgs |
117 |
|
25 |
|
|
namespace escript { |
26 |
|
|
|
27 |
jgs |
121 |
Taipan arrayManager; |
28 |
|
|
|
29 |
|
|
DataVector::DataVector() : |
30 |
|
|
m_array_data(0), |
31 |
|
|
m_size(0), |
32 |
|
|
m_dim(0), |
33 |
|
|
m_N(0) |
34 |
|
|
{ |
35 |
jgs |
117 |
} |
36 |
|
|
|
37 |
jgs |
121 |
DataVector::DataVector(const DataVector& other) : |
38 |
|
|
m_array_data(0), |
39 |
|
|
m_size(other.m_size), |
40 |
|
|
m_dim(other.m_dim), |
41 |
|
|
m_N(other.m_N) |
42 |
|
|
{ |
43 |
|
|
m_array_data = arrayManager.new_array(m_dim,m_N); |
44 |
jgs |
122 |
int i; |
45 |
|
|
#pragma omp parallel for private(i) schedule(static) |
46 |
|
|
for (i=0; i<m_size; i++) { |
47 |
jgs |
121 |
m_array_data[i] = other.m_array_data[i]; |
48 |
|
|
} |
49 |
jgs |
117 |
} |
50 |
|
|
|
51 |
jgs |
121 |
DataVector::DataVector(const DataVector::size_type size, |
52 |
|
|
const DataVector::value_type val, |
53 |
|
|
const DataVector::size_type blockSize) : |
54 |
|
|
m_array_data(0), |
55 |
|
|
m_size(size), |
56 |
|
|
m_dim(blockSize) |
57 |
|
|
{ |
58 |
|
|
resize(size, val, blockSize); |
59 |
jgs |
117 |
} |
60 |
|
|
|
61 |
jgs |
121 |
DataVector::~DataVector() |
62 |
|
|
{ |
63 |
|
|
// dispose of data array |
64 |
jgs |
151 |
if (m_array_data!=0) { |
65 |
|
|
arrayManager.delete_array(m_array_data); |
66 |
|
|
} |
67 |
jgs |
121 |
|
68 |
|
|
// clear data members |
69 |
|
|
m_size = -1; |
70 |
|
|
m_dim = -1; |
71 |
|
|
m_N = -1; |
72 |
|
|
m_array_data = 0; |
73 |
jgs |
117 |
} |
74 |
|
|
|
75 |
jgs |
121 |
void |
76 |
|
|
DataVector::resize(const DataVector::size_type newSize, |
77 |
|
|
const DataVector::value_type newValue, |
78 |
|
|
const DataVector::size_type newBlockSize) |
79 |
|
|
{ |
80 |
|
|
assert(m_size >= 0); |
81 |
|
|
|
82 |
jgs |
151 |
if ( newBlockSize == 0) { |
83 |
|
|
throw DataException("DataVector: invalid blockSize specified"); |
84 |
|
|
} |
85 |
|
|
|
86 |
jgs |
121 |
if ( (newSize % newBlockSize) != 0) { |
87 |
|
|
throw DataException("DataVector: invalid blockSize specified"); |
88 |
|
|
} |
89 |
|
|
|
90 |
jgs |
151 |
if (m_array_data!=0) { |
91 |
|
|
arrayManager.delete_array(m_array_data); |
92 |
|
|
} |
93 |
jgs |
121 |
|
94 |
|
|
m_size = newSize; |
95 |
|
|
m_dim = newBlockSize; |
96 |
|
|
m_N = newSize / newBlockSize; |
97 |
|
|
m_array_data = arrayManager.new_array(m_dim,m_N); |
98 |
|
|
|
99 |
jgs |
122 |
int i; |
100 |
|
|
#pragma omp parallel for private(i) schedule(static) |
101 |
|
|
for (i=0; i<m_size; i++) { |
102 |
|
|
m_array_data[i] = newValue; |
103 |
jgs |
121 |
} |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
DataVector& |
107 |
|
|
DataVector::operator=(const DataVector& other) |
108 |
|
|
{ |
109 |
|
|
assert(m_size >= 0); |
110 |
|
|
|
111 |
jgs |
151 |
if (m_array_data!=0) { |
112 |
|
|
arrayManager.delete_array(m_array_data); |
113 |
|
|
} |
114 |
jgs |
121 |
|
115 |
|
|
m_size = other.m_size; |
116 |
|
|
m_dim = other.m_dim; |
117 |
|
|
m_N = other.m_N; |
118 |
|
|
|
119 |
|
|
m_array_data = arrayManager.new_array(m_dim,m_N); |
120 |
jgs |
122 |
int i; |
121 |
|
|
#pragma omp parallel for private(i) schedule(static) |
122 |
|
|
for (i=0; i<m_size; i++) { |
123 |
jgs |
121 |
m_array_data[i] = other.m_array_data[i]; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
return *this; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
bool |
130 |
|
|
DataVector::operator==(const DataVector& other) const |
131 |
|
|
{ |
132 |
|
|
assert(m_size >= 0); |
133 |
|
|
|
134 |
|
|
if (m_size!=other.m_size) { |
135 |
|
|
return false; |
136 |
|
|
} |
137 |
|
|
if (m_dim!=other.m_dim) { |
138 |
|
|
return false; |
139 |
|
|
} |
140 |
|
|
if (m_N!=other.m_N) { |
141 |
|
|
return false; |
142 |
|
|
} |
143 |
|
|
for (int i=0; i<m_size; i++) { |
144 |
|
|
if (m_array_data[i] != other.m_array_data[i]) { |
145 |
|
|
return false; |
146 |
|
|
} |
147 |
|
|
} |
148 |
|
|
return true; |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
bool |
152 |
|
|
DataVector::operator!=(const DataVector& other) const |
153 |
|
|
{ |
154 |
|
|
return !(*this==other); |
155 |
|
|
} |
156 |
|
|
|
157 |
jgs |
123 |
int |
158 |
|
|
DataVector::archiveData(ofstream& archiveFile, |
159 |
|
|
const size_type noValues) const |
160 |
|
|
{ |
161 |
|
|
// |
162 |
|
|
// Check number of values expected to be written matches number in this object |
163 |
|
|
if (noValues != size()) { |
164 |
|
|
return 2; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
// |
168 |
|
|
// Write all values in this object out to archiveFile |
169 |
|
|
for (int i=0; i<size(); i++) { |
170 |
|
|
archiveFile.write(reinterpret_cast<char *>(&m_array_data[i]),sizeof(double)); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
// |
174 |
|
|
// Check no errors were encountered before returning |
175 |
|
|
if (!archiveFile.good()) { |
176 |
|
|
return 1; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
return 0; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
int |
183 |
|
|
DataVector::extractData(ifstream& archiveFile, |
184 |
|
|
const size_type noValues) |
185 |
|
|
{ |
186 |
|
|
// |
187 |
|
|
// Check number of values expected to be read matches number in this object |
188 |
|
|
if (noValues != size()) { |
189 |
|
|
return 2; |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
// |
193 |
|
|
// Read all values in archiveFile back to this object |
194 |
|
|
for (int i=0; i<size(); i++) { |
195 |
|
|
archiveFile.read(reinterpret_cast<char *>(&m_array_data[i]),sizeof(double)); |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
// |
199 |
|
|
// Check no errors were encountered before returning |
200 |
|
|
if (!archiveFile.good()) { |
201 |
|
|
return 1; |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
return 0; |
205 |
|
|
} |
206 |
|
|
|
207 |
jgs |
117 |
} // end of namespace |