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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 149 - (show annotations)
Thu Sep 1 03:31:39 2005 UTC (14 years, 1 month ago) by jgs
Original Path: trunk/esys2/escript/src/Data/Taipan.cpp
File size: 6831 byte(s)
Merge of development branch dev-02 back to main trunk on 2005-09-01

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 "escript/Data/Taipan.h"
16
17 #include <iostream>
18 #include <cassert>
19 #ifdef _OPENMP
20 #include <omp.h>
21 #endif
22
23 using namespace std;
24
25 namespace escript {
26
27 Taipan::Taipan() :
28 memTable_Root(0),
29 totalElements(0)
30 {
31 // create and initialise a new StatTable
32 statTable = new Taipan_StatTable;
33 clear_stats();
34 }
35
36 Taipan::~Taipan() {
37
38 Taipan_MemTable *tab;
39 Taipan_MemTable *tab_next;
40
41 // dump memory usage statistics
42 dump_stats();
43
44 // deallocate StatTable object
45 delete statTable;
46
47 // deallocate all managed arrays and the memTable
48 tab = memTable_Root;
49 while (tab != 0) {
50 tab_next = tab->next;
51 delete[] tab->array;
52 delete tab;
53 tab = tab_next;
54 }
55
56 // clear the MemTable root node
57 memTable_Root = 0;
58
59 // clear the totalElements counter
60 totalElements = -1;
61 }
62
63 double*
64 Taipan::new_array(int dim, int N) {
65
66 assert(totalElements >= 0);
67
68 int len = 0;
69 #ifdef _OPENMP
70 int numThreads = omp_get_num_threads();
71 #else
72 int numThreads = 1;
73 #endif
74
75 Taipan_MemTable *tab;
76 Taipan_MemTable *new_tab;
77 Taipan_MemTable *tab_prev;
78
79 // increment count of alloc opperations called
80 statTable->allocs++;
81
82 // numThreads = omp_get_max_threads();
83
84 // is a suitable array already available?
85 if (memTable_Root != 0) {
86 tab = memTable_Root;
87 while (tab != 0) {
88 if (tab->dim == dim &&
89 tab->N == N &&
90 tab->free &&
91 tab->numThreads == numThreads) {
92 tab->free = false;
93 return tab->array;
94 }
95 tab_prev = tab;
96 tab = tab->next;
97 }
98 }
99
100 //
101 // otherwise a new array must be allocated
102
103 // create the corresponding memTable entry
104 len = dim * N;
105 new_tab = new Taipan_MemTable;
106 new_tab->dim = dim;
107 new_tab->N = N;
108 new_tab->numThreads = numThreads;
109 new_tab->free = false;
110 new_tab->next = 0;
111 if (memTable_Root == 0) {
112 memTable_Root = new_tab;
113 } else {
114 tab_prev->next = new_tab;
115 }
116
117 // allocate and initialise the new array
118 new_tab->array = new double[len];
119 int i,j;
120 #pragma omp parallel for private(i,j) schedule(static)
121 for (i=0; i<N; i++) {
122 for (j=0; j<dim; j++) {
123 new_tab->array[j+dim*i]=0.0;
124 }
125 }
126 totalElements += len;
127
128 // increment count of arrays allocated
129 statTable->arrays++;
130
131 // increment count of elements allocated
132 statTable->elements+=len;
133
134 return new_tab->array;
135 }
136
137 void
138 Taipan::delete_array(double* array) {
139
140 assert(totalElements >= 0);
141
142 int N;
143 int len = 0;
144 bool found = false;
145
146 Taipan_MemTable *tab;
147 Taipan_MemTable *tab_next;
148 Taipan_MemTable *tab_prev = 0;
149
150 if (memTable_Root != 0) {
151
152 // find the table entry for this array and mark it as free
153 tab = memTable_Root;
154 while (tab != 0) {
155 if (tab->array == array) {
156 N = tab->N;
157 tab->free = true;
158 found = true;
159 break;
160 }
161 tab = tab->next;
162 }
163 if (!found) {
164 // this wasn't an array under management, so quit now
165 return;
166 }
167
168 // increment count of dealloc opperations called
169 statTable->deallocs++;
170
171 // are there any N block arrays still in use?
172 tab = memTable_Root;
173 while (tab != 0) {
174 if (tab->N==N && !tab->free) {
175 return;
176 }
177 tab = tab->next;
178 }
179
180 // if not, all N block arrays are deallocated
181 tab = memTable_Root;
182 while (tab != 0) {
183 tab_next = tab->next;
184 if (tab->N == N) {
185 delete[] tab->array;
186 len += tab->dim * N;
187 if (tab_prev != 0) {
188 tab_prev->next = tab->next;
189 } else {
190 memTable_Root = tab->next;
191 }
192 delete tab;
193 // increment count of arrays dealloced
194 statTable->dearrays++;
195 } else {
196 tab_prev = tab;
197 }
198 tab = tab_next;
199 }
200
201 totalElements -= len;
202
203 // increment count of elements dealloced
204 statTable->deelements+=len;
205
206 } else {
207 // what to do if no arrays under management?
208 }
209 }
210
211 int
212 Taipan::num_arrays() {
213
214 assert(totalElements >= 0);
215
216 int num_arrays = 0;
217
218 Taipan_MemTable *tab;
219
220 // count all managed arrays in the memTable
221 tab = memTable_Root;
222 while (tab != 0) {
223 num_arrays++;
224 tab = tab->next;
225 }
226
227 return num_arrays;
228 }
229
230 int
231 Taipan::num_arrays(int N) {
232
233 assert(totalElements >= 0);
234
235 int num_arrays = 0;
236
237 Taipan_MemTable *tab;
238
239 // count all managed arrays of N blocks in the memTable
240 tab = memTable_Root;
241 while (tab != 0) {
242 if (tab->N == N) {
243 num_arrays++;
244 }
245 tab = tab->next;
246 }
247
248 return num_arrays;
249 }
250
251 int
252 Taipan::num_free(int N) {
253
254 assert(totalElements >= 0);
255
256 int num_free = 0;
257
258 Taipan_MemTable *tab;
259
260 // count all free managed arrays of N blocks in the memTable
261 tab = memTable_Root;
262 while (tab != 0) {
263 if (tab->N == N) {
264 if (tab->free) {
265 num_free++;
266 }
267 }
268 tab = tab->next;
269 }
270
271 return num_free;
272 }
273
274 long
275 Taipan::num_elements() {
276 assert(totalElements >= 0);
277 return totalElements;
278 }
279
280 void
281 Taipan::dump_stats() {
282 float elMb=statTable->elements*8.0/1048576;
283 float deelMb=statTable->deelements*8.0/1048576;
284 cout << "========== Mem Stats ==================" << endl;
285 cout << "Total Num allocs: " << statTable->allocs << endl;
286 cout << "Total Num deallocs: " << statTable->deallocs << endl;
287 cout << "Total Num arrays: " << statTable->arrays << endl;
288 cout << "Total Num dearrays: " << statTable->dearrays << endl;
289 cout << "Total Num elements: " << statTable->elements << " (" << elMb << " Mb)" << endl;
290 cout << "Total Num deelements: " << statTable->deelements << " (" << deelMb << " Mb)" << endl;
291 cout << "Curr Num arrays: " << num_arrays() << endl;
292 cout << "Curr Num elements: " << num_elements() << endl;
293 cout << "=======================================" << endl;
294 }
295
296 void
297 Taipan::clear_stats() {
298 statTable->allocs=0;
299 statTable->deallocs=0;
300 statTable->arrays=0;
301 statTable->dearrays=0;
302 statTable->elements=0;
303 statTable->deelements=0;
304 }
305
306 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26