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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 474 - (hide annotations)
Mon Jan 30 04:23:44 2006 UTC (13 years, 10 months ago) by jgs
File size: 7669 byte(s)
restructure escript source tree
move src/Data/* -> src
remove inc
modify #includes and cpppath settings accordingly

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