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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1977 - (hide annotations)
Thu Nov 6 03:54:35 2008 UTC (10 years, 8 months ago) by jfenwick
File size: 8035 byte(s)
More warning removal.
1 jgs 121
2 ksteube 1312 /*******************************************************
3 ksteube 1811 *
4     * Copyright (c) 2003-2008 by University of Queensland
5     * Earth Systems Science Computational Center (ESSCC)
6     * http://www.uq.edu.au/esscc
7     *
8     * Primary Business: Queensland, Australia
9     * Licensed under the Open Software License version 3.0
10     * http://www.opensource.org/licenses/osl-3.0.php
11     *
12     *******************************************************/
13 ksteube 1312
14 ksteube 1811
15 jgs 474 #include "Taipan.h"
16 jgs 121
17 jgs 477 #include <iostream>
18     #include <cassert>
19    
20     #ifdef _OPENMP
21     #include <omp.h>
22     #endif
23    
24 jgs 121 using namespace std;
25    
26     namespace escript {
27    
28     Taipan::Taipan() :
29     memTable_Root(0),
30     totalElements(0)
31     {
32 jgs 149 // create and initialise a new StatTable
33     statTable = new Taipan_StatTable;
34     clear_stats();
35 jgs 121 }
36    
37     Taipan::~Taipan() {
38    
39 jgs 151 long len=0;
40 jgs 121 Taipan_MemTable *tab;
41     Taipan_MemTable *tab_next;
42    
43 jgs 149 // dump memory usage statistics
44     dump_stats();
45    
46     // deallocate StatTable object
47     delete statTable;
48    
49 jgs 121 // deallocate all managed arrays and the memTable
50     tab = memTable_Root;
51     while (tab != 0) {
52     tab_next = tab->next;
53 jgs 151 len = tab->dim * tab->N;
54     totalElements -= len;
55 jgs 121 delete[] tab->array;
56     delete tab;
57     tab = tab_next;
58     }
59    
60 jgs 151 assert(totalElements == 0);
61    
62 jgs 121 // clear the MemTable root node
63     memTable_Root = 0;
64    
65 jgs 151 // reset totalElements counter
66 jgs 121 totalElements = -1;
67     }
68    
69 gross 797 void
70     Taipan::release_unused_arrays()
71     {
72     long len=0;
73     Taipan_MemTable *tab;
74     Taipan_MemTable *tab_next, *tab_prev=0;
75     tab = memTable_Root;
76     while (tab != 0) {
77     tab_next = tab->next;
78     if (tab->free) {
79     delete[] tab->array;
80     len += tab->dim * tab->N;
81     if (tab_prev != 0) {
82     tab_prev->next = tab->next;
83     } else {
84     memTable_Root = tab->next;
85     }
86     delete tab;
87     // increment count of arrays dealloced
88     statTable->deallocations++;
89     } else {
90     tab_prev = tab;
91     }
92     tab = tab_next;
93     }
94     totalElements -= len;
95     statTable->deallocated_elements += len;
96     cout << len*8./1048576 << " Mbytes unused memory has been released." << endl;
97     }
98    
99    
100 jgs 121 double*
101     Taipan::new_array(int dim, int N) {
102    
103     assert(totalElements >= 0);
104    
105     int len = 0;
106 jgs 122 #ifdef _OPENMP
107     int numThreads = omp_get_num_threads();
108     #else
109 jgs 121 int numThreads = 1;
110 jgs 122 #endif
111 jgs 121
112     Taipan_MemTable *tab;
113     Taipan_MemTable *new_tab;
114 jfenwick 1977 Taipan_MemTable *tab_prev=0;
115 jgs 121
116 jgs 151 // increment count of alloc operations called
117     statTable->requests++;
118 jgs 121
119     // is a suitable array already available?
120     if (memTable_Root != 0) {
121     tab = memTable_Root;
122     while (tab != 0) {
123     if (tab->dim == dim &&
124     tab->N == N &&
125     tab->free &&
126     tab->numThreads == numThreads) {
127     tab->free = false;
128     return tab->array;
129     }
130     tab_prev = tab;
131     tab = tab->next;
132     }
133     }
134    
135     // otherwise a new array must be allocated
136    
137     // create the corresponding memTable entry
138     len = dim * N;
139     new_tab = new Taipan_MemTable;
140     new_tab->dim = dim;
141     new_tab->N = N;
142     new_tab->numThreads = numThreads;
143     new_tab->free = false;
144     new_tab->next = 0;
145     if (memTable_Root == 0) {
146     memTable_Root = new_tab;
147     } else {
148     tab_prev->next = new_tab;
149     }
150    
151     // allocate and initialise the new array
152     new_tab->array = new double[len];
153 jgs 122 int i,j;
154 jgs 151 if (N==1) {
155     for (j=0; j<dim; j++)
156     new_tab->array[j]=0.0;
157     } else if (N>1) {
158     #pragma omp parallel for private(i,j) schedule(static)
159     for (i=0; i<N; i++) {
160     for (j=0; j<dim; j++)
161     new_tab->array[j+dim*i]=0.0;
162 jgs 121 }
163     }
164 jgs 151
165 jgs 121 totalElements += len;
166    
167 jgs 151 // update maximum table size
168     statTable->max_tab_size = (statTable->max_tab_size < totalElements) ? totalElements : statTable->max_tab_size;
169    
170 jgs 149 // increment count of arrays allocated
171 jgs 151 statTable->allocations++;
172 jgs 149
173     // increment count of elements allocated
174 jgs 151 statTable->allocated_elements += len;
175 jgs 149
176 jgs 121 return new_tab->array;
177     }
178    
179     void
180     Taipan::delete_array(double* array) {
181    
182     assert(totalElements >= 0);
183    
184     int N;
185     int len = 0;
186     bool found = false;
187    
188     Taipan_MemTable *tab;
189     Taipan_MemTable *tab_next;
190     Taipan_MemTable *tab_prev = 0;
191    
192 jgs 151 // increment count of free operations called
193     statTable->frees++;
194    
195     if (array == 0) {
196     // have been given an empty array, so quit now
197     return;
198     }
199    
200 jgs 121 if (memTable_Root != 0) {
201    
202     // find the table entry for this array and mark it as free
203     tab = memTable_Root;
204     while (tab != 0) {
205     if (tab->array == array) {
206     N = tab->N;
207     tab->free = true;
208     found = true;
209     break;
210     }
211     tab = tab->next;
212     }
213     if (!found) {
214     // this wasn't an array under management, so quit now
215     return;
216     }
217    
218 jgs 151 if (N<=1) {
219     // we never deallocate arrays with N<=1, so quit now
220     return;
221     }
222 jgs 149
223 jgs 121 // are there any N block arrays still in use?
224     tab = memTable_Root;
225     while (tab != 0) {
226 jgs 151 if (tab->N==N && !tab->free)
227 jgs 121 return;
228     tab = tab->next;
229     }
230    
231     // if not, all N block arrays are deallocated
232     tab = memTable_Root;
233     while (tab != 0) {
234     tab_next = tab->next;
235     if (tab->N == N) {
236     delete[] tab->array;
237     len += tab->dim * N;
238     if (tab_prev != 0) {
239     tab_prev->next = tab->next;
240     } else {
241     memTable_Root = tab->next;
242     }
243     delete tab;
244 jgs 149 // increment count of arrays dealloced
245 jgs 151 statTable->deallocations++;
246 jgs 121 } else {
247     tab_prev = tab;
248     }
249     tab = tab_next;
250     }
251 jgs 151
252 jgs 121 totalElements -= len;
253    
254 jgs 151 // increment count of elements deallocated
255     statTable->deallocated_elements += len;
256 jgs 149
257 jgs 121 } else {
258     // what to do if no arrays under management?
259     }
260     }
261    
262     int
263     Taipan::num_arrays() {
264    
265     assert(totalElements >= 0);
266    
267     int num_arrays = 0;
268    
269     Taipan_MemTable *tab;
270    
271     // count all managed arrays in the memTable
272     tab = memTable_Root;
273     while (tab != 0) {
274     num_arrays++;
275     tab = tab->next;
276     }
277    
278     return num_arrays;
279     }
280    
281     int
282     Taipan::num_arrays(int N) {
283    
284     assert(totalElements >= 0);
285    
286     int num_arrays = 0;
287    
288     Taipan_MemTable *tab;
289    
290     // count all managed arrays of N blocks in the memTable
291     tab = memTable_Root;
292     while (tab != 0) {
293     if (tab->N == N) {
294     num_arrays++;
295     }
296     tab = tab->next;
297     }
298    
299     return num_arrays;
300     }
301    
302     int
303     Taipan::num_free(int N) {
304    
305     assert(totalElements >= 0);
306    
307     int num_free = 0;
308    
309     Taipan_MemTable *tab;
310    
311     // count all free managed arrays of N blocks in the memTable
312     tab = memTable_Root;
313     while (tab != 0) {
314     if (tab->N == N) {
315     if (tab->free) {
316     num_free++;
317     }
318     }
319     tab = tab->next;
320     }
321     return num_free;
322     }
323    
324     long
325     Taipan::num_elements() {
326 jgs 151
327 jgs 121 assert(totalElements >= 0);
328 jgs 151
329 jgs 121 return totalElements;
330     }
331    
332 jgs 149 void
333     Taipan::dump_stats() {
334 jgs 151
335     assert(totalElements >= 0);
336 jfenwick 1977 #ifdef TAIPAN_STATS
337 phornby 1628 double elMb=statTable->allocated_elements*8.0/1048576;
338     double deelMb=statTable->deallocated_elements*8.0/1048576;
339     double tszMb=statTable->max_tab_size*8.0/1048576;
340 jfenwick 1977
341 gross 798 cout << "======= escript Mem Stats ===========================" << endl;
342 jgs 151 cout << "Total Num requests: " << statTable->requests << endl;
343     cout << "Total Num releases: " << statTable->frees << endl;
344     cout << "Total Num allocated arrays: " << statTable->allocations << endl;
345     cout << "Total Num deallocated arrays: " << statTable->deallocations << endl;
346     cout << "Total Num allocated elements: " << statTable->allocated_elements << " (" << elMb << " Mb)" << endl;
347     cout << "Total Num deallocated elements: " << statTable->deallocated_elements << " (" << deelMb << " Mb)" << endl;
348     cout << "Maximum memory buffer size: " << statTable->max_tab_size << " (" << tszMb << " Mb)" << endl;
349     cout << "Curr Num arrays: " << num_arrays() << endl;
350     cout << "Curr Num elements in buffer: " << num_elements() << endl;
351     cout << "==================================================" << endl;
352 ksteube 1559 #endif
353 jgs 149 }
354    
355     void
356     Taipan::clear_stats() {
357 jgs 151
358     assert(totalElements >= 0);
359    
360     statTable->requests=0;
361     statTable->frees=0;
362     statTable->allocations=0;
363     statTable->deallocations=0;
364     statTable->allocated_elements=0;
365     statTable->deallocated_elements=0;
366     statTable->max_tab_size=0;
367 jgs 149 }
368    
369 jgs 121 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26