/[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 797 by gross, Thu Aug 3 02:03:18 2006 UTC
# Line 1  Line 1 
1  /*  /*
2   ******************************************************************************   ************************************************************
3   *                                                                            *   *          Copyright 2006 by ACcESS MNRF                   *
4   *       COPYRIGHT  ACcESS 2004 -  All Rights Reserved                        *   *                                                          *
5   *                                                                            *   *              http://www.access.edu.au                    *
6   * This software is the property of ACcESS. No part of this code              *   *       Primary Business: Queensland, Australia            *
7   * may be copied in any form or by any means without the expressed written    *   *  Licensed under the Open Software License version 3.0    *
8   * consent of ACcESS.  Copying, use or modification of this software          *   *     http://www.opensource.org/licenses/osl-3.0.php       *
9   * by any unauthorised person is illegal unless that person has a software    *   *                                                          *
10   * license agreement with ACcESS.                                             *   ************************************************************
  *                                                                            *  
  ******************************************************************************  
11  */  */
12    
13  #include "escript/Data/Taipan.h"  #include "Taipan.h"
14    
15  #include <iostream>  #include <iostream>
16  #include <cassert>  #include <cassert>
17    
18  #ifdef _OPENMP  #ifdef _OPENMP
19  #include <omp.h>  #include <omp.h>
20  #endif  #endif
# Line 35  Taipan::Taipan() : Line 34  Taipan::Taipan() :
34    
35  Taipan::~Taipan() {  Taipan::~Taipan() {
36    
37      long len=0;
38    Taipan_MemTable *tab;    Taipan_MemTable *tab;
39    Taipan_MemTable *tab_next;    Taipan_MemTable *tab_next;
40    
# Line 48  Taipan::~Taipan() { Line 48  Taipan::~Taipan() {
48    tab = memTable_Root;    tab = memTable_Root;
49    while (tab != 0) {    while (tab != 0) {
50      tab_next = tab->next;      tab_next = tab->next;
51        len = tab->dim * tab->N;
52        totalElements -= len;
53      delete[] tab->array;      delete[] tab->array;
54      delete tab;      delete tab;
55      tab = tab_next;      tab = tab_next;
56    }    }
57    
58      assert(totalElements == 0);
59    
60    // clear the MemTable root node    // clear the MemTable root node
61    memTable_Root = 0;    memTable_Root = 0;
62    
63    // clear the totalElements counter    // reset totalElements counter
64    totalElements = -1;    totalElements = -1;
65  }  }
66    
67    void
68    Taipan::release_unused_arrays()
69    {
70      long len=0;
71      Taipan_MemTable *tab;
72      Taipan_MemTable *tab_next, *tab_prev=0;
73      tab = memTable_Root;
74      while (tab != 0) {
75          tab_next = tab->next;
76          if (tab->free) {
77            delete[] tab->array;
78            len += tab->dim * tab->N;
79            if (tab_prev != 0) {
80              tab_prev->next = tab->next;
81            } else {
82              memTable_Root = tab->next;
83            }
84            delete tab;
85            // increment count of arrays dealloced
86            statTable->deallocations++;
87          } else {
88            tab_prev = tab;
89          }
90          tab = tab_next;
91      }
92      totalElements -= len;
93      statTable->deallocated_elements += len;
94      cout << len*8./1048576 << " Mbytes unused memory has been released." << endl;
95    }
96    
97    
98  double*  double*
99  Taipan::new_array(int dim, int N) {  Taipan::new_array(int dim, int N) {
100    
# Line 76  Taipan::new_array(int dim, int N) { Line 111  Taipan::new_array(int dim, int N) {
111    Taipan_MemTable *new_tab;    Taipan_MemTable *new_tab;
112    Taipan_MemTable *tab_prev;    Taipan_MemTable *tab_prev;
113    
114    // increment count of alloc opperations called    // increment count of alloc operations called
115    statTable->allocs++;    statTable->requests++;
   
   // numThreads = omp_get_max_threads();  
116    
117    // is a suitable array already available?    // is a suitable array already available?
118    if (memTable_Root != 0) {    if (memTable_Root != 0) {
# Line 97  Taipan::new_array(int dim, int N) { Line 130  Taipan::new_array(int dim, int N) {
130      }      }
131    }    }
132    
   //  
133    // otherwise a new array must be allocated    // otherwise a new array must be allocated
134    
135    // create the corresponding memTable entry    // create the corresponding memTable entry
# Line 117  Taipan::new_array(int dim, int N) { Line 149  Taipan::new_array(int dim, int N) {
149    // allocate and initialise the new array    // allocate and initialise the new array
150    new_tab->array = new double[len];    new_tab->array = new double[len];
151    int i,j;    int i,j;
152    #pragma omp parallel for private(i,j) schedule(static)    if (N==1) {
153    for (i=0; i<N; i++) {      for (j=0; j<dim; j++)
154      for (j=0; j<dim; j++) {        new_tab->array[j]=0.0;
155        new_tab->array[j+dim*i]=0.0;    } else if (N>1) {
156        #pragma omp parallel for private(i,j) schedule(static)
157        for (i=0; i<N; i++) {
158          for (j=0; j<dim; j++)
159            new_tab->array[j+dim*i]=0.0;
160      }      }
161    }    }
162    
163    totalElements += len;    totalElements += len;
164    
165      // update maximum table size
166      statTable->max_tab_size = (statTable->max_tab_size < totalElements) ? totalElements : statTable->max_tab_size;
167    
168    // increment count of arrays allocated    // increment count of arrays allocated
169    statTable->arrays++;    statTable->allocations++;
170    
171    // increment count of elements allocated    // increment count of elements allocated
172    statTable->elements+=len;    statTable->allocated_elements += len;
173    
174    return new_tab->array;    return new_tab->array;
175  }  }
# Line 147  Taipan::delete_array(double* array) { Line 187  Taipan::delete_array(double* array) {
187    Taipan_MemTable *tab_next;    Taipan_MemTable *tab_next;
188    Taipan_MemTable *tab_prev = 0;    Taipan_MemTable *tab_prev = 0;
189    
190      // increment count of free operations called
191      statTable->frees++;
192    
193      if (array == 0) {
194        // have been given an empty array, so quit now
195        return;
196      }
197    
198    if (memTable_Root != 0) {    if (memTable_Root != 0) {
199    
200      // 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 213  Taipan::delete_array(double* array) {
213        return;        return;
214      }      }
215    
216      // increment count of dealloc opperations called      if (N<=1) {
217      statTable->deallocs++;        // we never deallocate arrays with N<=1, so quit now
218          return;
219        }
220    
221      // are there any N block arrays still in use?      // are there any N block arrays still in use?
222      tab = memTable_Root;      tab = memTable_Root;
223      while (tab != 0) {      while (tab != 0) {
224        if (tab->N==N && !tab->free) {        if (tab->N==N && !tab->free)
225          return;          return;
       }  
226        tab = tab->next;        tab = tab->next;
227      }      }
228    
# Line 191  Taipan::delete_array(double* array) { Line 240  Taipan::delete_array(double* array) {
240          }          }
241          delete tab;          delete tab;
242          // increment count of arrays dealloced          // increment count of arrays dealloced
243          statTable->dearrays++;          statTable->deallocations++;
244        } else {        } else {
245          tab_prev = tab;          tab_prev = tab;
246        }        }
247        tab = tab_next;        tab = tab_next;
248      }      }
249      
250      totalElements -= len;      totalElements -= len;
251    
252      // increment count of elements dealloced      // increment count of elements deallocated
253      statTable->deelements+=len;      statTable->deallocated_elements += len;
254    
255    } else {    } else {
256      // what to do if no arrays under management?      // what to do if no arrays under management?
# Line 267  Taipan::num_free(int N) { Line 316  Taipan::num_free(int N) {
316      }      }
317      tab = tab->next;      tab = tab->next;
318    }    }
   
319    return num_free;    return num_free;
320  }  }
321    
322  long  long
323  Taipan::num_elements() {  Taipan::num_elements() {
324    
325    assert(totalElements >= 0);    assert(totalElements >= 0);
326    
327    return totalElements;    return totalElements;
328  }  }
329    
330  void  void
331  Taipan::dump_stats() {  Taipan::dump_stats() {
332    float elMb=statTable->elements*8.0/1048576;  
333    float deelMb=statTable->deelements*8.0/1048576;    assert(totalElements >= 0);
334    cout << "========== Mem Stats ==================" << endl;  
335    cout << "Total Num allocs:     " << statTable->allocs << endl;    float elMb=statTable->allocated_elements*8.0/1048576;
336    cout << "Total Num deallocs:   " << statTable->deallocs << endl;    float deelMb=statTable->deallocated_elements*8.0/1048576;
337    cout << "Total Num arrays:     " << statTable->arrays << endl;    float tszMb=statTable->max_tab_size*8.0/1048576;
338    cout << "Total Num dearrays:   " << statTable->dearrays << endl;  #ifndef PASO_MPI
339    cout << "Total Num elements:   " << statTable->elements << " (" << elMb << " Mb)" << endl;    cout << "========== Mem Stats =============================" << endl;
340    cout << "Total Num deelements: " << statTable->deelements << " (" << deelMb << " Mb)" << endl;    cout << "Total Num requests:             " << statTable->requests << endl;
341    cout << "Curr  Num arrays:     " << num_arrays() << endl;    cout << "Total Num releases:             " << statTable->frees << endl;
342    cout << "Curr  Num elements:   " << num_elements() << endl;    cout << "Total Num allocated arrays:     " << statTable->allocations << endl;
343    cout << "=======================================" << endl;    cout << "Total Num deallocated arrays:   " << statTable->deallocations << endl;
344      cout << "Total Num allocated elements:   " << statTable->allocated_elements << " (" << elMb << " Mb)" << endl;
345      cout << "Total Num deallocated elements: " << statTable->deallocated_elements << " (" << deelMb << " Mb)" << endl;
346      cout << "Maximum memory buffer size:     " << statTable->max_tab_size << " (" << tszMb << " Mb)" << endl;
347      cout << "Curr Num arrays:                " << num_arrays() << endl;
348      cout << "Curr Num elements in buffer:    " << num_elements() << endl;
349      cout << "==================================================" << endl;
350    #endif
351  }  }
352    
353  void  void
354  Taipan::clear_stats() {  Taipan::clear_stats() {
355    statTable->allocs=0;  
356    statTable->deallocs=0;    assert(totalElements >= 0);
357    statTable->arrays=0;  
358    statTable->dearrays=0;    statTable->requests=0;
359    statTable->elements=0;    statTable->frees=0;
360    statTable->deelements=0;    statTable->allocations=0;
361      statTable->deallocations=0;
362      statTable->allocated_elements=0;
363      statTable->deallocated_elements=0;
364      statTable->max_tab_size=0;
365  }  }
366    
367  }  // end of namespace  }  // end of namespace

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

  ViewVC Help
Powered by ViewVC 1.1.26