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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26