/[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 1811 by ksteube, Thu Sep 25 23:11:13 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"  /*******************************************************
3    *
4    * Copyright (c) 2003-2008 by University of Queensland
5    * Earth Systems Science Computational Center (ESSCC)
6    * http://www.uq.edu.au/esscc
7    *
8    * Primary Business: Queensland, Australia
9    * Licensed under the Open Software License version 3.0
10    * http://www.opensource.org/licenses/osl-3.0.php
11    *
12    *******************************************************/
13    
14    
15    #include "Taipan.h"
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    
69    void
70    Taipan::release_unused_arrays()
71    {
72      long len=0;
73      Taipan_MemTable *tab;
74      Taipan_MemTable *tab_next, *tab_prev=0;
75      tab = memTable_Root;
76      while (tab != 0) {
77          tab_next = tab->next;
78          if (tab->free) {
79            delete[] tab->array;
80            len += tab->dim * tab->N;
81            if (tab_prev != 0) {
82              tab_prev->next = tab->next;
83            } else {
84              memTable_Root = tab->next;
85            }
86            delete tab;
87            // increment count of arrays dealloced
88            statTable->deallocations++;
89          } else {
90            tab_prev = tab;
91          }
92          tab = tab_next;
93      }
94      totalElements -= len;
95      statTable->deallocated_elements += len;
96      cout << len*8./1048576 << " Mbytes unused memory has been released." << endl;
97    }
98    
99    
100  double*  double*
101  Taipan::new_array(int dim, int N) {  Taipan::new_array(int dim, int N) {
102    
# Line 76  Taipan::new_array(int dim, int N) { Line 113  Taipan::new_array(int dim, int N) {
113    Taipan_MemTable *new_tab;    Taipan_MemTable *new_tab;
114    Taipan_MemTable *tab_prev;    Taipan_MemTable *tab_prev;
115    
116    // increment count of alloc opperations called    // increment count of alloc operations called
117    statTable->allocs++;    statTable->requests++;
   
   // numThreads = omp_get_max_threads();  
118    
119    // is a suitable array already available?    // is a suitable array already available?
120    if (memTable_Root != 0) {    if (memTable_Root != 0) {
# Line 97  Taipan::new_array(int dim, int N) { Line 132  Taipan::new_array(int dim, int N) {
132      }      }
133    }    }
134    
   //  
135    // otherwise a new array must be allocated    // otherwise a new array must be allocated
136    
137    // create the corresponding memTable entry    // create the corresponding memTable entry
# Line 117  Taipan::new_array(int dim, int N) { Line 151  Taipan::new_array(int dim, int N) {
151    // allocate and initialise the new array    // allocate and initialise the new array
152    new_tab->array = new double[len];    new_tab->array = new double[len];
153    int i,j;    int i,j;
154    #pragma omp parallel for private(i,j) schedule(static)    if (N==1) {
155    for (i=0; i<N; i++) {      for (j=0; j<dim; j++)
156      for (j=0; j<dim; j++) {        new_tab->array[j]=0.0;
157        new_tab->array[j+dim*i]=0.0;    } else if (N>1) {
158        #pragma omp parallel for private(i,j) schedule(static)
159        for (i=0; i<N; i++) {
160          for (j=0; j<dim; j++)
161            new_tab->array[j+dim*i]=0.0;
162      }      }
163    }    }
164    
165    totalElements += len;    totalElements += len;
166    
167      // update maximum table size
168      statTable->max_tab_size = (statTable->max_tab_size < totalElements) ? totalElements : statTable->max_tab_size;
169    
170    // increment count of arrays allocated    // increment count of arrays allocated
171    statTable->arrays++;    statTable->allocations++;
172    
173    // increment count of elements allocated    // increment count of elements allocated
174    statTable->elements+=len;    statTable->allocated_elements += len;
175    
176    return new_tab->array;    return new_tab->array;
177  }  }
# Line 147  Taipan::delete_array(double* array) { Line 189  Taipan::delete_array(double* array) {
189    Taipan_MemTable *tab_next;    Taipan_MemTable *tab_next;
190    Taipan_MemTable *tab_prev = 0;    Taipan_MemTable *tab_prev = 0;
191    
192      // increment count of free operations called
193      statTable->frees++;
194    
195      if (array == 0) {
196        // have been given an empty array, so quit now
197        return;
198      }
199    
200    if (memTable_Root != 0) {    if (memTable_Root != 0) {
201    
202      // 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 215  Taipan::delete_array(double* array) {
215        return;        return;
216      }      }
217    
218      // increment count of dealloc opperations called      if (N<=1) {
219      statTable->deallocs++;        // we never deallocate arrays with N<=1, so quit now
220          return;
221        }
222    
223      // are there any N block arrays still in use?      // are there any N block arrays still in use?
224      tab = memTable_Root;      tab = memTable_Root;
225      while (tab != 0) {      while (tab != 0) {
226        if (tab->N==N && !tab->free) {        if (tab->N==N && !tab->free)
227          return;          return;
       }  
228        tab = tab->next;        tab = tab->next;
229      }      }
230    
# Line 191  Taipan::delete_array(double* array) { Line 242  Taipan::delete_array(double* array) {
242          }          }
243          delete tab;          delete tab;
244          // increment count of arrays dealloced          // increment count of arrays dealloced
245          statTable->dearrays++;          statTable->deallocations++;
246        } else {        } else {
247          tab_prev = tab;          tab_prev = tab;
248        }        }
249        tab = tab_next;        tab = tab_next;
250      }      }
251      
252      totalElements -= len;      totalElements -= len;
253    
254      // increment count of elements dealloced      // increment count of elements deallocated
255      statTable->deelements+=len;      statTable->deallocated_elements += len;
256    
257    } else {    } else {
258      // what to do if no arrays under management?      // what to do if no arrays under management?
# Line 267  Taipan::num_free(int N) { Line 318  Taipan::num_free(int N) {
318      }      }
319      tab = tab->next;      tab = tab->next;
320    }    }
   
321    return num_free;    return num_free;
322  }  }
323    
324  long  long
325  Taipan::num_elements() {  Taipan::num_elements() {
326    
327    assert(totalElements >= 0);    assert(totalElements >= 0);
328    
329    return totalElements;    return totalElements;
330  }  }
331    
332  void  void
333  Taipan::dump_stats() {  Taipan::dump_stats() {
334    float elMb=statTable->elements*8.0/1048576;  
335    float deelMb=statTable->deelements*8.0/1048576;    assert(totalElements >= 0);
336    cout << "========== Mem Stats ==================" << endl;  
337    cout << "Total Num allocs:     " << statTable->allocs << endl;    double elMb=statTable->allocated_elements*8.0/1048576;
338    cout << "Total Num deallocs:   " << statTable->deallocs << endl;    double deelMb=statTable->deallocated_elements*8.0/1048576;
339    cout << "Total Num arrays:     " << statTable->arrays << endl;    double tszMb=statTable->max_tab_size*8.0/1048576;
340    cout << "Total Num dearrays:   " << statTable->dearrays << endl;  #ifdef TAIPAN_STATS
341    cout << "Total Num elements:   " << statTable->elements << " (" << elMb << " Mb)" << endl;    cout << "======= escript Mem Stats ===========================" << endl;
342    cout << "Total Num deelements: " << statTable->deelements << " (" << deelMb << " Mb)" << endl;    cout << "Total Num requests:             " << statTable->requests << endl;
343    cout << "Curr  Num arrays:     " << num_arrays() << endl;    cout << "Total Num releases:             " << statTable->frees << endl;
344    cout << "Curr  Num elements:   " << num_elements() << endl;    cout << "Total Num allocated arrays:     " << statTable->allocations << endl;
345    cout << "=======================================" << endl;    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    #endif
353  }  }
354    
355  void  void
356  Taipan::clear_stats() {  Taipan::clear_stats() {
357    statTable->allocs=0;  
358    statTable->deallocs=0;    assert(totalElements >= 0);
359    statTable->arrays=0;  
360    statTable->dearrays=0;    statTable->requests=0;
361    statTable->elements=0;    statTable->frees=0;
362    statTable->deelements=0;    statTable->allocations=0;
363      statTable->deallocations=0;
364      statTable->allocated_elements=0;
365      statTable->deallocated_elements=0;
366      statTable->max_tab_size=0;
367  }  }
368    
369  }  // end of namespace  }  // end of namespace

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

  ViewVC Help
Powered by ViewVC 1.1.26