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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (hide annotations)
Fri May 6 04:26:16 2005 UTC (14 years, 5 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/Taipan.cpp
File size: 5027 byte(s)
Merge of development branch back to main trunk on 2005-05-06

1 jgs 121 /*
2     ******************************************************************************
3     * *
4     * COPYRIGHT ACcESS 2004 - All Rights Reserved *
5     * *
6     * This software is the property of ACcESS. No part of this code *
7     * may be copied in any form or by any means without the expressed written *
8     * consent of ACcESS. Copying, use or modification of this software *
9     * by any unauthorised person is illegal unless that person has a software *
10     * license agreement with ACcESS. *
11     * *
12     ******************************************************************************
13     */
14    
15     #include "escript/Data/Taipan.h"
16    
17     #include <iostream>
18    
19     using namespace std;
20    
21     namespace escript {
22    
23     Taipan::Taipan() :
24     memTable_Root(0),
25     totalElements(0)
26     {
27     }
28    
29     Taipan::~Taipan() {
30    
31     Taipan_MemTable *tab;
32     Taipan_MemTable *tab_next;
33    
34     // deallocate all managed arrays and the memTable
35     tab = memTable_Root;
36     while (tab != 0) {
37     tab_next = tab->next;
38     delete[] tab->array;
39     delete tab;
40     tab = tab_next;
41     }
42    
43     // clear the MemTable root node
44     memTable_Root = 0;
45    
46     // clear the totalElements counter
47     totalElements = -1;
48     }
49    
50     double*
51     Taipan::new_array(int dim, int N) {
52    
53     assert(totalElements >= 0);
54    
55     int len = 0;
56     int numThreads = 1;
57    
58     Taipan_MemTable *tab;
59     Taipan_MemTable *new_tab;
60     Taipan_MemTable *tab_prev;
61    
62     //numThreads = omp_get_max_threads();
63    
64     // is a suitable array already available?
65     if (memTable_Root != 0) {
66     tab = memTable_Root;
67     while (tab != 0) {
68     if (tab->dim == dim &&
69     tab->N == N &&
70     tab->free &&
71     tab->numThreads == numThreads) {
72     tab->free = false;
73     return tab->array;
74     }
75     tab_prev = tab;
76     tab = tab->next;
77     }
78     }
79    
80     //
81     // otherwise a new array must be allocated
82    
83     // create the corresponding memTable entry
84     len = dim * N;
85     new_tab = new Taipan_MemTable;
86     new_tab->dim = dim;
87     new_tab->N = N;
88     new_tab->numThreads = numThreads;
89     new_tab->free = false;
90     new_tab->next = 0;
91     if (memTable_Root == 0) {
92     memTable_Root = new_tab;
93     } else {
94     tab_prev->next = new_tab;
95     }
96    
97     // allocate and initialise the new array
98     new_tab->array = new double[len];
99    
100     //#pragma omp parallel for private(i,j)
101     for (int i=0; i<N; i++) {
102     for (int j=0; j<dim; j++) {
103     new_tab->array[j+dim*i]=0.0;
104     }
105     }
106    
107     totalElements += len;
108    
109     return new_tab->array;
110     }
111    
112     void
113     Taipan::delete_array(double* array) {
114    
115     assert(totalElements >= 0);
116    
117     int N;
118     int len = 0;
119     bool found = false;
120    
121     Taipan_MemTable *tab;
122     Taipan_MemTable *tab_next;
123     Taipan_MemTable *tab_prev = 0;
124    
125     if (memTable_Root != 0) {
126    
127     // find the table entry for this array and mark it as free
128     tab = memTable_Root;
129     while (tab != 0) {
130     if (tab->array == array) {
131     N = tab->N;
132     tab->free = true;
133     found = true;
134     break;
135     }
136     tab = tab->next;
137     }
138     if (!found) {
139     // this wasn't an array under management, so quit now
140     return;
141     }
142    
143     // are there any N block arrays still in use?
144     tab = memTable_Root;
145     while (tab != 0) {
146     if (tab->N==N && !tab->free) {
147     return;
148     }
149     tab = tab->next;
150     }
151    
152     // if not, all N block arrays are deallocated
153     tab = memTable_Root;
154     while (tab != 0) {
155     tab_next = tab->next;
156     if (tab->N == N) {
157     delete[] tab->array;
158     len += tab->dim * N;
159     if (tab_prev != 0) {
160     tab_prev->next = tab->next;
161     } else {
162     memTable_Root = tab->next;
163     }
164     delete tab;
165     } else {
166     tab_prev = tab;
167     }
168     tab = tab_next;
169     }
170    
171     totalElements -= len;
172    
173     } else {
174     // what to do if no arrays under management?
175     }
176     }
177    
178     int
179     Taipan::num_arrays() {
180    
181     assert(totalElements >= 0);
182    
183     int num_arrays = 0;
184    
185     Taipan_MemTable *tab;
186    
187     // count all managed arrays in the memTable
188     tab = memTable_Root;
189     while (tab != 0) {
190     num_arrays++;
191     tab = tab->next;
192     }
193    
194     return num_arrays;
195     }
196    
197     int
198     Taipan::num_arrays(int N) {
199    
200     assert(totalElements >= 0);
201    
202     int num_arrays = 0;
203    
204     Taipan_MemTable *tab;
205    
206     // count all managed arrays of N blocks in the memTable
207     tab = memTable_Root;
208     while (tab != 0) {
209     if (tab->N == N) {
210     num_arrays++;
211     }
212     tab = tab->next;
213     }
214    
215     return num_arrays;
216     }
217    
218     int
219     Taipan::num_free(int N) {
220    
221     assert(totalElements >= 0);
222    
223     int num_free = 0;
224    
225     Taipan_MemTable *tab;
226    
227     // count all free managed arrays of N blocks in the memTable
228     tab = memTable_Root;
229     while (tab != 0) {
230     if (tab->N == N) {
231     if (tab->free) {
232     num_free++;
233     }
234     }
235     tab = tab->next;
236     }
237    
238     return num_free;
239     }
240    
241     long
242     Taipan::num_elements() {
243     assert(totalElements >= 0);
244     return totalElements;
245     }
246    
247     } // end of namespace

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26