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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 782 - (show annotations)
Tue Jul 18 00:47:47 2006 UTC (13 years, 3 months ago) by bcumming
File size: 7450 byte(s)
Large number of changes to Finley for meshing in MPI.

- optimisation and neatening up of rectcanglular mesh generation code
- first and second order 1D, 2D and 3D rectangular meshes are now
  available in finley and escript using MPI.
- reduced meshes now generated in MPI, and interpolation to and from 
  reduced data types now supported.  

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26