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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2380 - (show annotations)
Tue Apr 14 03:23:29 2009 UTC (10 years, 6 months ago) by artak
File MIME type: text/plain
File size: 11452 byte(s)
minor typo
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 has 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 #pragma omp parallel for private(i) schedule(static)
176 for(i=0;i<A->numOutput;++i) {
177 index_list[i].extension=NULL;
178 index_list[i].n=0;
179 }
180 }
181
182 #pragma omp parallel for private(i,iptrA,j,iptrB,k) schedule(static)
183 for(i = 0; i < A->numOutput; i++) {
184 for(iptrA = A->ptr[i]; iptrA < A->ptr[i+1]; ++iptrA) {
185 j = A->index[iptrA];
186 for(iptrB = B->ptr[j]; iptrB < B->ptr[j+1]; ++iptrB) {
187 k = B->index[iptrB];
188 Paso_IndexList_insertIndex(&(index_list[i]),k);
189 }
190 }
191 }
192
193 out=Paso_IndexList_createPattern(0, A->numOutput,index_list,0,B->numInput,0);
194
195 #ifdef Paso_TRACE
196 printf("Paso_Pattern_multipy: new pattern has been allocated.\n");
197 #endif
198
199 /* clean up */
200 if (index_list!=NULL) {
201 #pragma omp parallel for private(i) schedule(static)
202 for(i=0;i<A->numOutput;++i) Paso_IndexList_free(index_list[i].extension);
203 }
204 TMPMEMFREE(index_list);
205
206 return out;
207 }
208
209
210
211 /*
212 * Computes the pattern of C = A binary operation B for CSR matrices A,B
213 *
214 * Note: we do not check whether A_ij(op)B_ij=0
215 *
216 */
217 Paso_Pattern* Paso_Pattern_binop(int type, Paso_Pattern* A, Paso_Pattern* B) {
218 Paso_Pattern *out=NULL;
219 index_t iptrA,iptrB;
220 dim_t i,j,k;
221
222 Paso_IndexList* index_list=NULL;
223
224 index_list=TMPMEMALLOC(A->numOutput,Paso_IndexList);
225 if (! Paso_checkPtr(index_list)) {
226 #pragma omp parallel for private(i) schedule(static)
227 for(i=0;i<A->numOutput;++i) {
228 index_list[i].extension=NULL;
229 index_list[i].n=0;
230 }
231 }
232
233 #pragma omp parallel for private(i,iptrA,j,iptrB,k) schedule(static)
234 for(i = 0; i < B->numOutput; i++){
235 iptrA = A->ptr[i],
236 iptrB = B->ptr[i];
237
238 while (iptrA < A->ptr[i+1] && iptrB < B->ptr[i+1]) {
239 j = A->index[iptrA];
240 k = B->index[iptrB];
241 if (j<k) {
242 Paso_IndexList_insertIndex(&(index_list[i]),j);
243 iptrA++;
244 } else if (j>k) {
245 Paso_IndexList_insertIndex(&(index_list[i]),k);
246 iptrB++;
247 } else if (j==k) {
248 Paso_IndexList_insertIndex(&(index_list[i]),j);
249 iptrB++;
250 iptrA++;
251 }
252 }
253 while(iptrA < A->ptr[i+1]) {
254 j = A->index[iptrA];
255 Paso_IndexList_insertIndex(&(index_list[i]),j);
256 iptrA++;
257 }
258 while(iptrB < B->ptr[i+1]) {
259 k = B->index[iptrB];
260 Paso_IndexList_insertIndex(&(index_list[i]),k);
261 iptrB++;
262 }
263 }
264
265 out=Paso_IndexList_createPattern(0, A->numOutput,index_list,0,A->numInput,0);
266
267 #ifdef Paso_TRACE
268 printf("Paso_Pattern_binop: new pattern has been allocated.\n");
269 #endif
270
271 /* clean up */
272 if (index_list!=NULL) {
273 #pragma omp parallel for private(i) schedule(static)
274 for(i=0;i<A->numOutput;++i) Paso_IndexList_free(index_list[i].extension);
275 }
276 TMPMEMFREE(index_list);
277
278 return out;
279 }
280
281 /* inserts row index row into the Paso_IndexList in if it does not exist */
282
283 void Paso_IndexList_insertIndex(Paso_IndexList* in, index_t index) {
284 dim_t i;
285 /* is index in in? */
286 for (i=0;i<in->n;i++) {
287 if (in->index[i]==index) return;
288 }
289 /* index could not be found */
290 if (in->n==INDEXLIST_LENGTH) {
291 /* if in->index is full check the extension */
292 if (in->extension==NULL) {
293 in->extension=TMPMEMALLOC(1,Paso_IndexList);
294 if (Paso_checkPtr(in->extension)) return;
295 in->extension->n=0;
296 in->extension->extension=NULL;
297 }
298 Paso_IndexList_insertIndex(in->extension,index);
299 } else {
300 /* insert index into in->index*/
301 in->index[in->n]=index;
302 in->n++;
303 }
304 }
305
306 /* counts the number of row indices in the Paso_IndexList in */
307
308 dim_t Paso_IndexList_count(Paso_IndexList* in, index_t range_min,index_t range_max) {
309 dim_t i;
310 dim_t out=0;
311 register index_t itmp;
312 if (in==NULL) {
313 return 0;
314 } else {
315 for (i=0;i<in->n;i++) {
316 itmp=in->index[i];
317 if ((itmp>=range_min) && (range_max>itmp)) ++out;
318 }
319 return out+Paso_IndexList_count(in->extension, range_min,range_max);
320 }
321 }
322
323 /* count the number of row indices in the Paso_IndexList in */
324
325 void Paso_IndexList_toArray(Paso_IndexList* in, index_t* array, index_t range_min,index_t range_max, index_t index_offset) {
326 dim_t i, ptr;
327 register index_t itmp;
328 if (in!=NULL) {
329 ptr=0;
330 for (i=0;i<in->n;i++) {
331 itmp=in->index[i];
332 if ((itmp>=range_min) && (range_max>itmp)) {
333 array[ptr]=itmp+index_offset;
334 ptr++;
335 }
336
337 }
338 Paso_IndexList_toArray(in->extension,&(array[ptr]), range_min, range_max, index_offset);
339 }
340 }
341
342 /* deallocates the Paso_IndexList in by recursive calls */
343
344 void Paso_IndexList_free(Paso_IndexList* in) {
345 if (in!=NULL) {
346 Paso_IndexList_free(in->extension);
347 TMPMEMFREE(in);
348 }
349 }
350
351 /* creates a Paso_pattern from a range of indices */
352 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)
353 {
354 dim_t *ptr=NULL;
355 register dim_t s,i,itmp;
356 index_t *index=NULL;
357 Paso_Pattern* out=NULL;
358
359 ptr=MEMALLOC(n+1-n0,index_t);
360 if (! Paso_checkPtr(ptr) ) {
361 /* get the number of connections per row */
362 #pragma omp parallel for private(i) schedule(static)
363 for(i=n0;i<n;++i) {
364 ptr[i-n0]=Paso_IndexList_count(&index_list[i],range_min,range_max);
365 }
366 /* accumulate ptr */
367 s=0;
368 for(i=n0;i<n;++i) {
369 itmp=ptr[i-n0];
370 ptr[i-n0]=s;
371 s+=itmp;
372 }
373 ptr[n-n0]=s;
374 /* fill index */
375 index=MEMALLOC(ptr[n-n0],index_t);
376 if (! Paso_checkPtr(index)) {
377 #pragma omp parallel for private(i) schedule(static)
378 for(i=n0;i<n;++i) {
379 Paso_IndexList_toArray(&index_list[i],&index[ptr[i-n0]],range_min,range_max,index_offset);
380 }
381 out=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,n-n0,range_max+index_offset,ptr,index);
382 }
383 }
384 if (! Paso_noError()) {
385 MEMFREE(ptr);
386 MEMFREE(index);
387 Paso_Pattern_free(out);
388 }
389 return out;
390 }

  ViewVC Help
Powered by ViewVC 1.1.26