/[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

revision 150 by jgs, Thu Sep 1 03:31:39 2005 UTC revision 151 by jgs, Thu Sep 22 01:55:00 2005 UTC
# Line 16  Line 16 
16    
17  #include <iostream>  #include <iostream>
18  #include <cassert>  #include <cassert>
19    
20  #ifdef _OPENMP  #ifdef _OPENMP
21  #include <omp.h>  #include <omp.h>
22  #endif  #endif
# Line 35  Taipan::Taipan() : Line 36  Taipan::Taipan() :
36    
37  Taipan::~Taipan() {  Taipan::~Taipan() {
38    
39      long len=0;
40    Taipan_MemTable *tab;    Taipan_MemTable *tab;
41    Taipan_MemTable *tab_next;    Taipan_MemTable *tab_next;
42    
# Line 48  Taipan::~Taipan() { Line 50  Taipan::~Taipan() {
50    tab = memTable_Root;    tab = memTable_Root;
51    while (tab != 0) {    while (tab != 0) {
52      tab_next = tab->next;      tab_next = tab->next;
53        len = tab->dim * tab->N;
54        totalElements -= len;
55      delete[] tab->array;      delete[] tab->array;
56      delete tab;      delete tab;
57      tab = tab_next;      tab = tab_next;
58    }    }
59    
60      assert(totalElements == 0);
61    
62    // clear the MemTable root node    // clear the MemTable root node
63    memTable_Root = 0;    memTable_Root = 0;
64    
65    // clear the totalElements counter    // reset totalElements counter
66    totalElements = -1;    totalElements = -1;
67  }  }
68    
# Line 76  Taipan::new_array(int dim, int N) { Line 82  Taipan::new_array(int dim, int N) {
82    Taipan_MemTable *new_tab;    Taipan_MemTable *new_tab;
83    Taipan_MemTable *tab_prev;    Taipan_MemTable *tab_prev;
84    
85    // increment count of alloc opperations called    // increment count of alloc operations called
86    statTable->allocs++;    statTable->requests++;
   
   // numThreads = omp_get_max_threads();  
87    
88    // is a suitable array already available?    // is a suitable array already available?
89    if (memTable_Root != 0) {    if (memTable_Root != 0) {
# Line 97  Taipan::new_array(int dim, int N) { Line 101  Taipan::new_array(int dim, int N) {
101      }      }
102    }    }
103    
   //  
104    // otherwise a new array must be allocated    // otherwise a new array must be allocated
105    
106    // create the corresponding memTable entry    // create the corresponding memTable entry
# Line 117  Taipan::new_array(int dim, int N) { Line 120  Taipan::new_array(int dim, int N) {
120    // allocate and initialise the new array    // allocate and initialise the new array
121    new_tab->array = new double[len];    new_tab->array = new double[len];
122    int i,j;    int i,j;
123    #pragma omp parallel for private(i,j) schedule(static)    if (N==1) {
124    for (i=0; i<N; i++) {      for (j=0; j<dim; j++)
125      for (j=0; j<dim; j++) {        new_tab->array[j]=0.0;
126        new_tab->array[j+dim*i]=0.0;    } else if (N>1) {
127        #pragma omp parallel for private(i,j) schedule(static)
128        for (i=0; i<N; i++) {
129          for (j=0; j<dim; j++)
130            new_tab->array[j+dim*i]=0.0;
131      }      }
132    }    }
133    
134    totalElements += len;    totalElements += len;
135    
136      // update maximum table size
137      statTable->max_tab_size = (statTable->max_tab_size < totalElements) ? totalElements : statTable->max_tab_size;
138    
139    // increment count of arrays allocated    // increment count of arrays allocated
140    statTable->arrays++;    statTable->allocations++;
141    
142    // increment count of elements allocated    // increment count of elements allocated
143    statTable->elements+=len;    statTable->allocated_elements += len;
144    
145    return new_tab->array;    return new_tab->array;
146  }  }
# Line 147  Taipan::delete_array(double* array) { Line 158  Taipan::delete_array(double* array) {
158    Taipan_MemTable *tab_next;    Taipan_MemTable *tab_next;
159    Taipan_MemTable *tab_prev = 0;    Taipan_MemTable *tab_prev = 0;
160    
161      // increment count of free operations called
162      statTable->frees++;
163    
164      if (array == 0) {
165        // have been given an empty array, so quit now
166        return;
167      }
168    
169    if (memTable_Root != 0) {    if (memTable_Root != 0) {
170    
171      // 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 184  Taipan::delete_array(double* array) {
184        return;        return;
185      }      }
186    
187      // increment count of dealloc opperations called      if (N<=1) {
188      statTable->deallocs++;        // we never deallocate arrays with N<=1, so quit now
189          return;
190        }
191    
192      // are there any N block arrays still in use?      // are there any N block arrays still in use?
193      tab = memTable_Root;      tab = memTable_Root;
194      while (tab != 0) {      while (tab != 0) {
195        if (tab->N==N && !tab->free) {        if (tab->N==N && !tab->free)
196          return;          return;
       }  
197        tab = tab->next;        tab = tab->next;
198      }      }
199    
# Line 191  Taipan::delete_array(double* array) { Line 211  Taipan::delete_array(double* array) {
211          }          }
212          delete tab;          delete tab;
213          // increment count of arrays dealloced          // increment count of arrays dealloced
214          statTable->dearrays++;          statTable->deallocations++;
215        } else {        } else {
216          tab_prev = tab;          tab_prev = tab;
217        }        }
218        tab = tab_next;        tab = tab_next;
219      }      }
220      
221      totalElements -= len;      totalElements -= len;
222    
223      // increment count of elements dealloced      // increment count of elements deallocated
224      statTable->deelements+=len;      statTable->deallocated_elements += len;
225    
226    } else {    } else {
227      // what to do if no arrays under management?      // what to do if no arrays under management?
# Line 267  Taipan::num_free(int N) { Line 287  Taipan::num_free(int N) {
287      }      }
288      tab = tab->next;      tab = tab->next;
289    }    }
   
290    return num_free;    return num_free;
291  }  }
292    
293  long  long
294  Taipan::num_elements() {  Taipan::num_elements() {
295    
296    assert(totalElements >= 0);    assert(totalElements >= 0);
297    
298    return totalElements;    return totalElements;
299  }  }
300    
301  void  void
302  Taipan::dump_stats() {  Taipan::dump_stats() {
303    float elMb=statTable->elements*8.0/1048576;  
304    float deelMb=statTable->deelements*8.0/1048576;    assert(totalElements >= 0);
305    cout << "========== Mem Stats ==================" << endl;  
306    cout << "Total Num allocs:     " << statTable->allocs << endl;    float elMb=statTable->allocated_elements*8.0/1048576;
307    cout << "Total Num deallocs:   " << statTable->deallocs << endl;    float deelMb=statTable->deallocated_elements*8.0/1048576;
308    cout << "Total Num arrays:     " << statTable->arrays << endl;    float tszMb=statTable->max_tab_size*8.0/1048576;
309    cout << "Total Num dearrays:   " << statTable->dearrays << endl;  
310    cout << "Total Num elements:   " << statTable->elements << " (" << elMb << " Mb)" << endl;    cout << "========== Mem Stats =============================" << endl;
311    cout << "Total Num deelements: " << statTable->deelements << " (" << deelMb << " Mb)" << endl;    cout << "Total Num requests:             " << statTable->requests << endl;
312    cout << "Curr  Num arrays:     " << num_arrays() << endl;    cout << "Total Num releases:             " << statTable->frees << endl;
313    cout << "Curr  Num elements:   " << num_elements() << endl;    cout << "Total Num allocated arrays:     " << statTable->allocations << endl;
314    cout << "=======================================" << endl;    cout << "Total Num deallocated arrays:   " << statTable->deallocations << endl;
315      cout << "Total Num allocated elements:   " << statTable->allocated_elements << " (" << elMb << " Mb)" << endl;
316      cout << "Total Num deallocated elements: " << statTable->deallocated_elements << " (" << deelMb << " Mb)" << endl;
317      cout << "Maximum memory buffer size:     " << statTable->max_tab_size << " (" << tszMb << " Mb)" << endl;
318      cout << "Curr Num arrays:                " << num_arrays() << endl;
319      cout << "Curr Num elements in buffer:    " << num_elements() << endl;
320      cout << "==================================================" << endl;
321  }  }
322    
323  void  void
324  Taipan::clear_stats() {  Taipan::clear_stats() {
325    statTable->allocs=0;  
326    statTable->deallocs=0;    assert(totalElements >= 0);
327    statTable->arrays=0;  
328    statTable->dearrays=0;    statTable->requests=0;
329    statTable->elements=0;    statTable->frees=0;
330    statTable->deelements=0;    statTable->allocations=0;
331      statTable->deallocations=0;
332      statTable->allocated_elements=0;
333      statTable->deallocated_elements=0;
334      statTable->max_tab_size=0;
335  }  }
336    
337  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.150  
changed lines
  Added in v.151

  ViewVC Help
Powered by ViewVC 1.1.26