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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/esys2/escript/src/Data/Taipan.cpp revision 149 by jgs, Thu Sep 1 03:31:39 2005 UTC trunk/escript/src/Taipan.cpp revision 1559 by ksteube, Wed May 21 04:27:15 2008 UTC
# Line 1  Line 1 
 /*  
  ******************************************************************************  
  *                                                                            *  
  *       COPYRIGHT  ACcESS 2004 -  All Rights Reserved                        *  
  *                                                                            *  
  * This software is the property of ACcESS. No part of this code              *  
  * may be copied in any form or by any means without the expressed written    *  
  * consent of ACcESS.  Copying, use or modification of this software          *  
  * by any unauthorised person is illegal unless that person has a software    *  
  * license agreement with ACcESS.                                             *  
  *                                                                            *  
  ******************************************************************************  
 */  
1    
2  #include "escript/Data/Taipan.h"  /* $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>  #include <iostream>
19  #include <cassert>  #include <cassert>
20    
21  #ifdef _OPENMP  #ifdef _OPENMP
22  #include <omp.h>  #include <omp.h>
23  #endif  #endif
# Line 35  Taipan::Taipan() : Line 37  Taipan::Taipan() :
37    
38  Taipan::~Taipan() {  Taipan::~Taipan() {
39    
40      long len=0;
41    Taipan_MemTable *tab;    Taipan_MemTable *tab;
42    Taipan_MemTable *tab_next;    Taipan_MemTable *tab_next;
43    
# Line 48  Taipan::~Taipan() { Line 51  Taipan::~Taipan() {
51    tab = memTable_Root;    tab = memTable_Root;
52    while (tab != 0) {    while (tab != 0) {
53      tab_next = tab->next;      tab_next = tab->next;
54        len = tab->dim * tab->N;
55        totalElements -= len;
56      delete[] tab->array;      delete[] tab->array;
57      delete tab;      delete tab;
58      tab = tab_next;      tab = tab_next;
59    }    }
60    
61      assert(totalElements == 0);
62    
63    // clear the MemTable root node    // clear the MemTable root node
64    memTable_Root = 0;    memTable_Root = 0;
65    
66    // clear the totalElements counter    // reset totalElements counter
67    totalElements = -1;    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*  double*
102  Taipan::new_array(int dim, int N) {  Taipan::new_array(int dim, int N) {
103    
# Line 76  Taipan::new_array(int dim, int N) { Line 114  Taipan::new_array(int dim, int N) {
114    Taipan_MemTable *new_tab;    Taipan_MemTable *new_tab;
115    Taipan_MemTable *tab_prev;    Taipan_MemTable *tab_prev;
116    
117    // increment count of alloc opperations called    // increment count of alloc operations called
118    statTable->allocs++;    statTable->requests++;
   
   // numThreads = omp_get_max_threads();  
119    
120    // is a suitable array already available?    // is a suitable array already available?
121    if (memTable_Root != 0) {    if (memTable_Root != 0) {
# Line 97  Taipan::new_array(int dim, int N) { Line 133  Taipan::new_array(int dim, int N) {
133      }      }
134    }    }
135    
   //  
136    // otherwise a new array must be allocated    // otherwise a new array must be allocated
137    
138    // create the corresponding memTable entry    // create the corresponding memTable entry
# Line 117  Taipan::new_array(int dim, int N) { Line 152  Taipan::new_array(int dim, int N) {
152    // allocate and initialise the new array    // allocate and initialise the new array
153    new_tab->array = new double[len];    new_tab->array = new double[len];
154    int i,j;    int i,j;
155    #pragma omp parallel for private(i,j) schedule(static)    if (N==1) {
156    for (i=0; i<N; i++) {      for (j=0; j<dim; j++)
157      for (j=0; j<dim; j++) {        new_tab->array[j]=0.0;
158        new_tab->array[j+dim*i]=0.0;    } 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;    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    // increment count of arrays allocated
172    statTable->arrays++;    statTable->allocations++;
173    
174    // increment count of elements allocated    // increment count of elements allocated
175    statTable->elements+=len;    statTable->allocated_elements += len;
176    
177    return new_tab->array;    return new_tab->array;
178  }  }
# Line 147  Taipan::delete_array(double* array) { Line 190  Taipan::delete_array(double* array) {
190    Taipan_MemTable *tab_next;    Taipan_MemTable *tab_next;
191    Taipan_MemTable *tab_prev = 0;    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) {    if (memTable_Root != 0) {
202    
203      // find the table entry for this array and mark it as free      // find the table entry for this array and mark it as free
# Line 165  Taipan::delete_array(double* array) { Line 216  Taipan::delete_array(double* array) {
216        return;        return;
217      }      }
218    
219      // increment count of dealloc opperations called      if (N<=1) {
220      statTable->deallocs++;        // we never deallocate arrays with N<=1, so quit now
221          return;
222        }
223    
224      // are there any N block arrays still in use?      // are there any N block arrays still in use?
225      tab = memTable_Root;      tab = memTable_Root;
226      while (tab != 0) {      while (tab != 0) {
227        if (tab->N==N && !tab->free) {        if (tab->N==N && !tab->free)
228          return;          return;
       }  
229        tab = tab->next;        tab = tab->next;
230      }      }
231    
# Line 191  Taipan::delete_array(double* array) { Line 243  Taipan::delete_array(double* array) {
243          }          }
244          delete tab;          delete tab;
245          // increment count of arrays dealloced          // increment count of arrays dealloced
246          statTable->dearrays++;          statTable->deallocations++;
247        } else {        } else {
248          tab_prev = tab;          tab_prev = tab;
249        }        }
250        tab = tab_next;        tab = tab_next;
251      }      }
252      
253      totalElements -= len;      totalElements -= len;
254    
255      // increment count of elements dealloced      // increment count of elements deallocated
256      statTable->deelements+=len;      statTable->deallocated_elements += len;
257    
258    } else {    } else {
259      // what to do if no arrays under management?      // what to do if no arrays under management?
# Line 267  Taipan::num_free(int N) { Line 319  Taipan::num_free(int N) {
319      }      }
320      tab = tab->next;      tab = tab->next;
321    }    }
   
322    return num_free;    return num_free;
323  }  }
324    
325  long  long
326  Taipan::num_elements() {  Taipan::num_elements() {
327    
328    assert(totalElements >= 0);    assert(totalElements >= 0);
329    
330    return totalElements;    return totalElements;
331  }  }
332    
333  void  void
334  Taipan::dump_stats() {  Taipan::dump_stats() {
335    float elMb=statTable->elements*8.0/1048576;  
336    float deelMb=statTable->deelements*8.0/1048576;    assert(totalElements >= 0);
337    cout << "========== Mem Stats ==================" << endl;  
338    cout << "Total Num allocs:     " << statTable->allocs << endl;    float elMb=statTable->allocated_elements*8.0/1048576;
339    cout << "Total Num deallocs:   " << statTable->deallocs << endl;    float deelMb=statTable->deallocated_elements*8.0/1048576;
340    cout << "Total Num arrays:     " << statTable->arrays << endl;    float tszMb=statTable->max_tab_size*8.0/1048576;
341    cout << "Total Num dearrays:   " << statTable->dearrays << endl;  #ifdef TAIPAN_STATS
342    cout << "Total Num elements:   " << statTable->elements << " (" << elMb << " Mb)" << endl;    cout << "======= escript Mem Stats ===========================" << endl;
343    cout << "Total Num deelements: " << statTable->deelements << " (" << deelMb << " Mb)" << endl;    cout << "Total Num requests:             " << statTable->requests << endl;
344    cout << "Curr  Num arrays:     " << num_arrays() << endl;    cout << "Total Num releases:             " << statTable->frees << endl;
345    cout << "Curr  Num elements:   " << num_elements() << endl;    cout << "Total Num allocated arrays:     " << statTable->allocations << endl;
346    cout << "=======================================" << endl;    cout << "Total Num deallocated arrays:   " << statTable->deallocations << endl;
347      cout << "Total Num allocated elements:   " << statTable->allocated_elements << " (" << elMb << " Mb)" << endl;
348      cout << "Total Num deallocated elements: " << statTable->deallocated_elements << " (" << deelMb << " Mb)" << endl;
349      cout << "Maximum memory buffer size:     " << statTable->max_tab_size << " (" << tszMb << " Mb)" << endl;
350      cout << "Curr Num arrays:                " << num_arrays() << endl;
351      cout << "Curr Num elements in buffer:    " << num_elements() << endl;
352      cout << "==================================================" << endl;
353    #endif
354  }  }
355    
356  void  void
357  Taipan::clear_stats() {  Taipan::clear_stats() {
358    statTable->allocs=0;  
359    statTable->deallocs=0;    assert(totalElements >= 0);
360    statTable->arrays=0;  
361    statTable->dearrays=0;    statTable->requests=0;
362    statTable->elements=0;    statTable->frees=0;
363    statTable->deelements=0;    statTable->allocations=0;
364      statTable->deallocations=0;
365      statTable->allocated_elements=0;
366      statTable->deallocated_elements=0;
367      statTable->max_tab_size=0;
368  }  }
369    
370  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.149  
changed lines
  Added in v.1559

  ViewVC Help
Powered by ViewVC 1.1.26