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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 615 - (hide annotations)
Wed Mar 22 02:12:00 2006 UTC (13 years, 7 months ago) by elspeth
File size: 7427 byte(s)
More copyright information.

1 jgs 121 /*
2 elspeth 615 ************************************************************
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 jgs 121 */
12    
13 jgs 474 #include "Taipan.h"
14 jgs 121
15 jgs 477 #include <iostream>
16     #include <cassert>
17    
18     #ifdef _OPENMP
19     #include <omp.h>
20     #endif
21    
22 jgs 121 using namespace std;
23    
24     namespace escript {
25    
26     Taipan::Taipan() :
27     memTable_Root(0),
28     totalElements(0)
29     {
30 jgs 149 // create and initialise a new StatTable
31     statTable = new Taipan_StatTable;
32     clear_stats();
33 jgs 121 }
34    
35     Taipan::~Taipan() {
36    
37 jgs 151 long len=0;
38 jgs 121 Taipan_MemTable *tab;
39     Taipan_MemTable *tab_next;
40    
41 jgs 149 // dump memory usage statistics
42     dump_stats();
43    
44     // deallocate StatTable object
45     delete statTable;
46    
47 jgs 121 // deallocate all managed arrays and the memTable
48     tab = memTable_Root;
49     while (tab != 0) {
50     tab_next = tab->next;
51 jgs 151 len = tab->dim * tab->N;
52     totalElements -= len;
53 jgs 121 delete[] tab->array;
54     delete tab;
55     tab = tab_next;
56     }
57    
58 jgs 151 assert(totalElements == 0);
59    
60 jgs 121 // clear the MemTable root node
61     memTable_Root = 0;
62    
63 jgs 151 // reset totalElements counter
64 jgs 121 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 jgs 122 #ifdef _OPENMP
74     int numThreads = omp_get_num_threads();
75     #else
76 jgs 121 int numThreads = 1;
77 jgs 122 #endif
78 jgs 121
79     Taipan_MemTable *tab;
80     Taipan_MemTable *new_tab;
81     Taipan_MemTable *tab_prev;
82    
83 jgs 151 // increment count of alloc operations called
84     statTable->requests++;
85 jgs 121
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 jgs 122 int i,j;
121 jgs 151 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 jgs 121 }
130     }
131 jgs 151
132 jgs 121 totalElements += len;
133    
134 jgs 151 // update maximum table size
135     statTable->max_tab_size = (statTable->max_tab_size < totalElements) ? totalElements : statTable->max_tab_size;
136    
137 jgs 149 // increment count of arrays allocated
138 jgs 151 statTable->allocations++;
139 jgs 149
140     // increment count of elements allocated
141 jgs 151 statTable->allocated_elements += len;
142 jgs 149
143 jgs 121 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 jgs 151 // 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 jgs 121 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 jgs 151 if (N<=1) {
186     // we never deallocate arrays with N<=1, so quit now
187     return;
188     }
189 jgs 149
190 jgs 121 // are there any N block arrays still in use?
191     tab = memTable_Root;
192     while (tab != 0) {
193 jgs 151 if (tab->N==N && !tab->free)
194 jgs 121 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 jgs 149 // increment count of arrays dealloced
212 jgs 151 statTable->deallocations++;
213 jgs 121 } else {
214     tab_prev = tab;
215     }
216     tab = tab_next;
217     }
218 jgs 151
219 jgs 121 totalElements -= len;
220    
221 jgs 151 // increment count of elements deallocated
222     statTable->deallocated_elements += len;
223 jgs 149
224 jgs 121 } 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 jgs 151
294 jgs 121 assert(totalElements >= 0);
295 jgs 151
296 jgs 121 return totalElements;
297     }
298    
299 jgs 149 void
300     Taipan::dump_stats() {
301 jgs 151
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    
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 jgs 149 }
320    
321     void
322     Taipan::clear_stats() {
323 jgs 151
324     assert(totalElements >= 0);
325    
326     statTable->requests=0;
327     statTable->frees=0;
328     statTable->allocations=0;
329     statTable->deallocations=0;
330     statTable->allocated_elements=0;
331     statTable->deallocated_elements=0;
332     statTable->max_tab_size=0;
333 jgs 149 }
334    
335 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