1 |
/* |
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 |
#include "DataVector.h" |
16 |
|
17 |
#include "Taipan.h" |
18 |
#include "DataException.h" |
19 |
|
20 |
#include <cassert> |
21 |
|
22 |
using namespace std; |
23 |
using namespace escript; |
24 |
|
25 |
namespace escript { |
26 |
|
27 |
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 |
} |
36 |
|
37 |
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 |
int i; |
45 |
#pragma omp parallel for private(i) schedule(static) |
46 |
for (i=0; i<m_size; i++) { |
47 |
m_array_data[i] = other.m_array_data[i]; |
48 |
} |
49 |
} |
50 |
|
51 |
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 |
} |
60 |
|
61 |
DataVector::~DataVector() |
62 |
{ |
63 |
// dispose of data array |
64 |
if (m_array_data!=0) { |
65 |
arrayManager.delete_array(m_array_data); |
66 |
} |
67 |
|
68 |
// clear data members |
69 |
m_size = -1; |
70 |
m_dim = -1; |
71 |
m_N = -1; |
72 |
m_array_data = 0; |
73 |
} |
74 |
|
75 |
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 |
if ( newBlockSize == 0) { |
83 |
throw DataException("DataVector: invalid blockSize specified"); |
84 |
} |
85 |
|
86 |
if ( (newSize % newBlockSize) != 0) { |
87 |
throw DataException("DataVector: invalid blockSize specified"); |
88 |
} |
89 |
|
90 |
if (m_array_data!=0) { |
91 |
arrayManager.delete_array(m_array_data); |
92 |
} |
93 |
|
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 |
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 |
} |
104 |
} |
105 |
|
106 |
DataVector& |
107 |
DataVector::operator=(const DataVector& other) |
108 |
{ |
109 |
assert(m_size >= 0); |
110 |
|
111 |
if (m_array_data!=0) { |
112 |
arrayManager.delete_array(m_array_data); |
113 |
} |
114 |
|
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 |
int i; |
121 |
#pragma omp parallel for private(i) schedule(static) |
122 |
for (i=0; i<m_size; i++) { |
123 |
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 |
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 |
} // end of namespace |