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