/[escript]/trunk/paso/src/Pattern.c
ViewVC logotype

Contents of /trunk/paso/src/Pattern.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1976 - (show annotations)
Thu Nov 6 03:14:52 2008 UTC (10 years, 11 months ago) by artak
File MIME type: text/plain
File size: 11332 byte(s)
unused variables are removed to get rid of compiler warnings
1
2 /*******************************************************
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
16 /**************************************************************/
17
18 /* Paso: Pattern */
19
20 /**************************************************************/
21
22 /* Author: gross@access.edu.au */
23
24 /**************************************************************/
25
26 #include "Paso.h"
27 #include "Pattern.h"
28
29 /**************************************************************/
30
31 /* allocates a Pattern */
32
33 Paso_Pattern* Paso_Pattern_alloc(int type, dim_t input_block_size, dim_t output_block_size, dim_t numOutput, dim_t numInput, index_t* ptr, index_t* index) {
34 Paso_Pattern *out=NULL;
35 index_t index_offset=(type & PATTERN_FORMAT_OFFSET1 ? 1:0);
36 index_t loc_min_index,loc_max_index,min_index=index_offset,max_index=index_offset-1;
37 dim_t i;
38 Paso_resetError();
39
40 if (type & PATTERN_FORMAT_SYM) {
41 Paso_setError(TYPE_ERROR,"Paso_Pattern_alloc: symmetric matrix pattern is not supported yet");
42 return NULL;
43 }
44 if (ptr!=NULL && index != NULL) {
45 #pragma omp parallel private(loc_min_index,loc_max_index,i)
46 {
47 loc_min_index=index_offset;
48 loc_max_index=index_offset-1;
49 if (type & PATTERN_FORMAT_OFFSET1) {
50 #pragma omp for schedule(static)
51 for (i=0;i<numOutput;++i) {
52 if (ptr[i]<ptr[i+1]) {
53 #ifdef USE_QSORTG
54 qsortG(&(index[ptr[i]-1]),(size_t)(ptr[i+1]-ptr[i]),sizeof(index_t),Paso_comparIndex);
55 #else
56 qsort(&(index[ptr[i]-1]),(size_t)(ptr[i+1]-ptr[i]),sizeof(index_t),Paso_comparIndex);
57 #endif
58 loc_min_index=MIN(loc_min_index,index[ptr[i]-1]);
59 loc_max_index=MAX(loc_max_index,index[ptr[i+1]-2]);
60 }
61 }
62 } else {
63 #pragma omp for schedule(static)
64 for (i=0;i<numOutput;++i) {
65 if (ptr[i]<ptr[i+1]) {
66 #ifdef USE_QSORTG
67 qsortG(&(index[ptr[i]]),(size_t)(ptr[i+1]-ptr[i]),sizeof(index_t),Paso_comparIndex);
68 #else
69 qsort(&(index[ptr[i]]),(size_t)(ptr[i+1]-ptr[i]),sizeof(index_t),Paso_comparIndex);
70 #endif
71 loc_min_index=MIN(loc_min_index,index[ptr[i]]);
72 loc_max_index=MAX(loc_max_index,index[ptr[i+1]-1]);
73 }
74 }
75 }
76 #pragma omp critical
77 {
78 min_index=MIN(loc_min_index,min_index);
79 max_index=MAX(loc_max_index,max_index);
80 }
81 }
82 if ( (min_index<index_offset) || (max_index>=numInput+index_offset) ) {
83 Paso_setError(TYPE_ERROR,"Paso_Pattern_alloc: Pattern index out of range.");
84 return NULL;
85 }
86 }
87 out=MEMALLOC(1,Paso_Pattern);
88 if (! Paso_checkPtr(out)) {
89 out->type=type;
90 out->reference_counter=1;
91 out->numOutput=numOutput;
92 out->numInput=numInput;
93 out->ptr=ptr;
94 out->index=index;
95 out->input_block_size=input_block_size;
96 out->output_block_size=output_block_size;
97 out->block_size=out->input_block_size * out->output_block_size;
98 if (out->ptr == NULL) {
99 out->len=0;
100 } else {
101 out->len=out->ptr[out->numOutput] - index_offset;
102 }
103 }
104 #ifdef Paso_TRACE
105 printf("Paso_Pattern_alloc: system matrix pattern as been allocated.\n");
106 #endif
107 return out;
108 }
109
110 /* returns a reference to in */
111
112 Paso_Pattern* Paso_Pattern_getReference(Paso_Pattern* in) {
113 if (in!=NULL) {
114 ++(in->reference_counter);
115 }
116 return in;
117 }
118
119 /* deallocates a Pattern: */
120
121 void Paso_Pattern_free(Paso_Pattern* in) {
122 if (in!=NULL) {
123 in->reference_counter--;
124 if (in->reference_counter<=0) {
125 MEMFREE(in->ptr);
126 MEMFREE(in->index);
127 MEMFREE(in);
128 #ifdef Paso_TRACE
129 printf("Paso_Pattern_free: pattern as been deallocated.\n");
130 #endif
131 }
132 }
133 }
134 /* *************************************************************/
135
136 /* some routines which help to get the matrix pattern from elements: */
137
138 /* this routine is used by qsort called in Paso_Pattern_alloc */
139
140 int Paso_comparIndex(const void *index1,const void *index2){
141 index_t Iindex1,Iindex2;
142 Iindex1=*(index_t*)index1;
143 Iindex2=*(index_t*)index2;
144 if (Iindex1<Iindex2) {
145 return -1;
146 } else {
147 if (Iindex1>Iindex2) {
148 return 1;
149 } else {
150 return 0;
151 }
152 }
153 }
154
155 bool_t Paso_Pattern_isEmpty(Paso_Pattern* in) {
156 if (in != NULL) {
157 if ((in->ptr != NULL) && (in->index != NULL)) return FALSE;
158 }
159 return TRUE;
160 }
161
162
163 /* computes the pattern coming from matrix-matrix multiplication
164 *
165 **/
166
167 Paso_Pattern* Paso_Pattern_multiply(int type, Paso_Pattern* A, Paso_Pattern* B) {
168 Paso_Pattern*out=NULL;
169 index_t iptrA,iptrB;
170 dim_t i,j,k;
171 Paso_IndexList* index_list=NULL;
172
173 index_list=TMPMEMALLOC(A->numOutput,Paso_IndexList);
174 if (! Paso_checkPtr(index_list)) {
175
176 #pragma omp parallel private(i)
177 {
178 #pragma omp for schedule(static)
179 for(i=0;i<A->numOutput;++i) {
180 index_list[i].extension=NULL;
181 index_list[i].n=0;
182 }
183 }
184 }
185
186 for(i = 0; i < A->numOutput; i++) {
187 for(iptrA = A->ptr[i]; iptrA < A->ptr[i+1]; ++iptrA) {
188 j = A->index[iptrA];
189 for(iptrB = B->ptr[j]; iptrB < B->ptr[j+1]; ++iptrB) {
190 k = B->index[iptrB];
191 Paso_IndexList_insertIndex(&(index_list[i]),k);
192 }
193 }
194 }
195
196 out=Paso_IndexList_createPattern(0, A->numOutput,index_list,0,B->numInput,0);
197
198 #ifdef Paso_TRACE
199 printf("Paso_Pattern_multipy: new pattern has been allocated.\n");
200 #endif
201
202 /* clean up */
203 if (index_list!=NULL) {
204 #pragma omp parallel for private(i)
205 for(i=0;i<A->numOutput;++i) Paso_IndexList_free(index_list[i].extension);
206 }
207 TMPMEMFREE(index_list);
208
209 return out;
210 }
211
212
213
214 /*
215 * Computes the pattern of C = A binary operation B for CSR matrices A,B
216 *
217 * Note: we do not check whether A_ij(op)B_ij=0
218 *
219 */
220 Paso_Pattern* Paso_Pattern_binop(int type, Paso_Pattern* A, Paso_Pattern* B) {
221 Paso_Pattern *out=NULL;
222 index_t iptrA,iptrB;
223 dim_t i,j,k;
224
225 Paso_IndexList* index_list=NULL;
226
227 index_list=TMPMEMALLOC(A->numOutput,Paso_IndexList);
228 if (! Paso_checkPtr(index_list)) {
229
230 #pragma omp parallel private(i)
231 {
232 #pragma omp for schedule(static)
233 for(i=0;i<A->numOutput;++i) {
234 index_list[i].extension=NULL;
235 index_list[i].n=0;
236 }
237 }
238 }
239 for(i = 0; i < B->numOutput; i++){
240 iptrA = A->ptr[i],
241 iptrB = B->ptr[i];
242
243 while (iptrA < A->ptr[i+1] && iptrB < B->ptr[i+1]) {
244 j = A->index[iptrA];
245 k = B->index[iptrB];
246 if (j<k) {
247 Paso_IndexList_insertIndex(&(index_list[i]),j);
248 iptrA++;
249 } else if (j>k) {
250 Paso_IndexList_insertIndex(&(index_list[i]),k);
251 iptrB++;
252 } else if (j==k) {
253 Paso_IndexList_insertIndex(&(index_list[i]),j);
254 iptrB++;
255 iptrA++;
256 }
257 }
258 while(iptrA < A->ptr[i+1]) {
259 j = A->index[iptrA];
260 Paso_IndexList_insertIndex(&(index_list[i]),j);
261 iptrA++;
262 }
263 while(iptrB < B->ptr[i+1]) {
264 k = B->index[iptrB];
265 Paso_IndexList_insertIndex(&(index_list[i]),k);
266 iptrB++;
267 }
268 }
269
270 out=Paso_IndexList_createPattern(0, A->numOutput,index_list,0,A->numInput,0);
271
272 #ifdef Paso_TRACE
273 printf("Paso_Pattern_binop: new pattern has been allocated.\n");
274 #endif
275
276 /* clean up */
277 if (index_list!=NULL) {
278 #pragma omp parallel for private(i)
279 for(i=0;i<A->numOutput;++i) Paso_IndexList_free(index_list[i].extension);
280 }
281 TMPMEMFREE(index_list);
282
283 return out;
284 }
285
286 /* inserts row index row into the Paso_IndexList in if it does not exist */
287
288 void Paso_IndexList_insertIndex(Paso_IndexList* in, index_t index) {
289 dim_t i;
290 /* is index in in? */
291 for (i=0;i<in->n;i++) {
292 if (in->index[i]==index) return;
293 }
294 /* index could not be found */
295 if (in->n==INDEXLIST_LENGTH) {
296 /* if in->index is full check the extension */
297 if (in->extension==NULL) {
298 in->extension=TMPMEMALLOC(1,Paso_IndexList);
299 if (Paso_checkPtr(in->extension)) return;
300 in->extension->n=0;
301 in->extension->extension=NULL;
302 }
303 Paso_IndexList_insertIndex(in->extension,index);
304 } else {
305 /* insert index into in->index*/
306 in->index[in->n]=index;
307 in->n++;
308 }
309 }
310
311 /* counts the number of row indices in the Paso_IndexList in */
312
313 dim_t Paso_IndexList_count(Paso_IndexList* in, index_t range_min,index_t range_max) {
314 dim_t i;
315 dim_t out=0;
316 register index_t itmp;
317 if (in==NULL) {
318 return 0;
319 } else {
320 for (i=0;i<in->n;i++) {
321 itmp=in->index[i];
322 if ((itmp>=range_min) && (range_max>itmp)) ++out;
323 }
324 return out+Paso_IndexList_count(in->extension, range_min,range_max);
325 }
326 }
327
328 /* count the number of row indices in the Paso_IndexList in */
329
330 void Paso_IndexList_toArray(Paso_IndexList* in, index_t* array, index_t range_min,index_t range_max, index_t index_offset) {
331 dim_t i, ptr;
332 register index_t itmp;
333 if (in!=NULL) {
334 ptr=0;
335 for (i=0;i<in->n;i++) {
336 itmp=in->index[i];
337 if ((itmp>=range_min) && (range_max>itmp)) {
338 array[ptr]=itmp+index_offset;
339 ptr++;
340 }
341
342 }
343 Paso_IndexList_toArray(in->extension,&(array[ptr]), range_min, range_max, index_offset);
344 }
345 }
346
347 /* deallocates the Paso_IndexList in by recursive calls */
348
349 void Paso_IndexList_free(Paso_IndexList* in) {
350 if (in!=NULL) {
351 Paso_IndexList_free(in->extension);
352 TMPMEMFREE(in);
353 }
354 }
355
356 /* creates a Paso_pattern from a range of indices */
357 Paso_Pattern* Paso_IndexList_createPattern(dim_t n0, dim_t n,Paso_IndexList* index_list,index_t range_min,index_t range_max,index_t index_offset)
358 {
359 dim_t *ptr=NULL;
360 register dim_t s,i,itmp;
361 index_t *index=NULL;
362 Paso_Pattern* out=NULL;
363
364 ptr=MEMALLOC(n+1-n0,index_t);
365 if (! Paso_checkPtr(ptr) ) {
366 /* get the number of connections per row */
367 #pragma omp parallel for schedule(static) private(i)
368 for(i=n0;i<n;++i) {
369 ptr[i-n0]=Paso_IndexList_count(&index_list[i],range_min,range_max);
370 }
371 /* accumulate ptr */
372 s=0;
373 for(i=n0;i<n;++i) {
374 itmp=ptr[i-n0];
375 ptr[i-n0]=s;
376 s+=itmp;
377 }
378 ptr[n-n0]=s;
379 /* fill index */
380 index=MEMALLOC(ptr[n-n0],index_t);
381 if (! Paso_checkPtr(index)) {
382 #pragma omp parallel for schedule(static)
383 for(i=n0;i<n;++i) {
384 Paso_IndexList_toArray(&index_list[i],&index[ptr[i-n0]],range_min,range_max,index_offset);
385 }
386 out=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,n-n0,range_max+index_offset,ptr,index);
387 }
388 }
389 if (! Paso_noError()) {
390 MEMFREE(ptr);
391 MEMFREE(index);
392 Paso_Pattern_free(out);
393 }
394 return out;
395 }

  ViewVC Help
Powered by ViewVC 1.1.26