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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 797 - (show annotations)
Thu Aug 3 02:03:18 2006 UTC (13 years, 1 month ago) by gross
File size: 4483 byte(s)
function added to manually free unused memory in the memory manager
1 /*
2 ************************************************************
3 * Copyright 2006 by ACcESS MNRF *
4 * *
5 * http://www.access.edu.au *
6 * Primary Business: Queensland, Australia *
7 * Licensed under the Open Software License version 3.0 *
8 * http://www.opensource.org/licenses/osl-3.0.php *
9 * *
10 ************************************************************
11 */
12
13 #include "DataVector.h"
14
15 #include "Taipan.h"
16 #include "DataException.h"
17
18 #include <cassert>
19
20 using namespace std;
21 using namespace escript;
22
23 namespace escript {
24
25 Taipan arrayManager;
26
27 void releaseUnusedMemory()
28 {
29 arrayManager.release_unused_arrays();
30 }
31
32
33 DataVector::DataVector() :
34 m_array_data(0),
35 m_size(0),
36 m_dim(0),
37 m_N(0)
38 {
39 }
40
41 DataVector::DataVector(const DataVector& other) :
42 m_array_data(0),
43 m_size(other.m_size),
44 m_dim(other.m_dim),
45 m_N(other.m_N)
46 {
47 m_array_data = arrayManager.new_array(m_dim,m_N);
48 int i;
49 #pragma omp parallel for private(i) schedule(static)
50 for (i=0; i<m_size; i++) {
51 m_array_data[i] = other.m_array_data[i];
52 }
53 }
54
55 DataVector::DataVector(const DataVector::size_type size,
56 const DataVector::value_type val,
57 const DataVector::size_type blockSize) :
58 m_array_data(0),
59 m_size(size),
60 m_dim(blockSize)
61 {
62 resize(size, val, blockSize);
63 }
64
65 DataVector::~DataVector()
66 {
67 // dispose of data array
68 if (m_array_data!=0) {
69 arrayManager.delete_array(m_array_data);
70 }
71
72 // clear data members
73 m_size = -1;
74 m_dim = -1;
75 m_N = -1;
76 m_array_data = 0;
77 }
78
79 void
80 DataVector::resize(const DataVector::size_type newSize,
81 const DataVector::value_type newValue,
82 const DataVector::size_type newBlockSize)
83 {
84 assert(m_size >= 0);
85
86 if ( newBlockSize == 0) {
87 throw DataException("DataVector: invalid blockSize specified");
88 }
89
90 if ( (newSize % newBlockSize) != 0) {
91 throw DataException("DataVector: invalid blockSize specified");
92 }
93
94 if (m_array_data!=0) {
95 arrayManager.delete_array(m_array_data);
96 }
97
98 m_size = newSize;
99 m_dim = newBlockSize;
100 m_N = newSize / newBlockSize;
101 m_array_data = arrayManager.new_array(m_dim,m_N);
102
103 int i;
104 #pragma omp parallel for private(i) schedule(static)
105 for (i=0; i<m_size; i++) {
106 m_array_data[i] = newValue;
107 }
108 }
109
110 DataVector&
111 DataVector::operator=(const DataVector& other)
112 {
113 assert(m_size >= 0);
114
115 if (m_array_data!=0) {
116 arrayManager.delete_array(m_array_data);
117 }
118
119 m_size = other.m_size;
120 m_dim = other.m_dim;
121 m_N = other.m_N;
122
123 m_array_data = arrayManager.new_array(m_dim,m_N);
124 int i;
125 #pragma omp parallel for private(i) schedule(static)
126 for (i=0; i<m_size; i++) {
127 m_array_data[i] = other.m_array_data[i];
128 }
129
130 return *this;
131 }
132
133 bool
134 DataVector::operator==(const DataVector& other) const
135 {
136 assert(m_size >= 0);
137
138 if (m_size!=other.m_size) {
139 return false;
140 }
141 if (m_dim!=other.m_dim) {
142 return false;
143 }
144 if (m_N!=other.m_N) {
145 return false;
146 }
147 for (int i=0; i<m_size; i++) {
148 if (m_array_data[i] != other.m_array_data[i]) {
149 return false;
150 }
151 }
152 return true;
153 }
154
155 bool
156 DataVector::operator!=(const DataVector& other) const
157 {
158 return !(*this==other);
159 }
160
161 int
162 DataVector::archiveData(ofstream& archiveFile,
163 const size_type noValues) const
164 {
165 //
166 // Check number of values expected to be written matches number in this object
167 if (noValues != size()) {
168 return 2;
169 }
170
171 //
172 // Write all values in this object out to archiveFile
173 for (int i=0; i<size(); i++) {
174 archiveFile.write(reinterpret_cast<char *>(&m_array_data[i]),sizeof(double));
175 }
176
177 //
178 // Check no errors were encountered before returning
179 if (!archiveFile.good()) {
180 return 1;
181 }
182
183 return 0;
184 }
185
186 int
187 DataVector::extractData(ifstream& archiveFile,
188 const size_type noValues)
189 {
190 //
191 // Check number of values expected to be read matches number in this object
192 if (noValues != size()) {
193 return 2;
194 }
195
196 //
197 // Read all values in archiveFile back to this object
198 for (int i=0; i<size(); i++) {
199 archiveFile.read(reinterpret_cast<char *>(&m_array_data[i]),sizeof(double));
200 }
201
202 //
203 // Check no errors were encountered before returning
204 if (!archiveFile.good()) {
205 return 1;
206 }
207
208 return 0;
209 }
210
211 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26