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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2551 - (show annotations)
Thu Jul 23 09:19:15 2009 UTC (10 years, 2 months ago) by gross
File MIME type: text/plain
File size: 12186 byte(s)
a problem with the sparse matrix unrolling fixed.
1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2009 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: Paso_Pattern_coupling
18
19 searches for a maximal independent set MIS in the matrix pattern
20 vertices in the maximal independent set are marked in mis_marker
21 nodes to be considered are marked by -1 on the input in mis_marker
22
23 */
24 /**********************************************************************/
25
26 /* Copyrights by ACcESS Australia 2003,2004,2005 */
27 /* Author: artak@uq.edu.au */
28
29 /**************************************************************/
30
31 #include "PasoUtil.h"
32 #include "Pattern_coupling.h"
33 #include <limits.h>
34
35
36 /***************************************************************/
37
38 #define IS_AVAILABLE -1
39 #define IS_IN_SET -3 /* Week connection */
40 #define IS_REMOVED -4 /* strong */
41
42 void Paso_Pattern_coup(Paso_SparseMatrix* A, index_t* mis_marker, double threshold) {
43
44 dim_t i,j;
45 /*double sum;*/
46 index_t iptr,*index,*where_p,*diagptr;
47 bool_t passed=FALSE;
48 dim_t n=A->numRows;
49 diagptr=MEMALLOC(n,index_t);
50
51 if (A->pattern->type & PATTERN_FORMAT_SYM) {
52 Paso_setError(TYPE_ERROR,"Paso_Pattern_coup: symmetric matrix pattern is not supported yet");
53 return;
54 }
55
56 #pragma omp parallel for private(i) schedule(static)
57 for (i=0;i<n;++i)
58 if(mis_marker[i]==IS_AVAILABLE)
59 mis_marker[i]=IS_IN_SET;
60
61 #pragma omp parallel for private(i,index,where_p) schedule(static)
62 for (i=0;i<n;++i) {
63 diagptr[i]=A->pattern->ptr[i];
64 index=&(A->pattern->index[diagptr[i]]);
65 where_p=(index_t*)bsearch(&i,
66 index,
67 A->pattern->ptr[i + 1]-A->pattern->ptr[i],
68 sizeof(index_t),
69 Paso_comparIndex);
70 if (where_p==NULL) {
71 Paso_setError(VALUE_ERROR, "Paso_Pattern_coup: main diagonal element missing.");
72 } else {
73 diagptr[i]+=(index_t)(where_p-index);
74 }
75 }
76
77
78
79 /*This loop cannot be parallelized, as order matters here.*/
80 for (i=0;i<n;++i) {
81 if (mis_marker[i]==IS_IN_SET) {
82 for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
83 j=A->pattern->index[iptr];
84 if (j!=i && ABS(A->val[iptr])>=threshold*ABS(A->val[diagptr[i]])) {
85 mis_marker[j]=IS_REMOVED;
86 }
87 }
88 }
89 }
90
91
92
93 /*This loop cannot be parallelized, as order matters here.*/
94 for (i=0;i<n;i++) {
95 if (mis_marker[i]==IS_REMOVED) {
96 passed=TRUE;
97 for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
98 j=A->pattern->index[iptr];
99 if (mis_marker[j]==IS_IN_SET) {
100 if ((A->val[iptr]/A->val[diagptr[i]])>=-threshold) {
101 passed=TRUE;
102 }
103 else {
104 passed=FALSE;
105 break;
106 }
107 }
108 }
109 if (passed) mis_marker[i]=IS_IN_SET;
110 }
111 }
112 /* This check is to make sure we dont get some nusty rows which were not removed durring coarsening process.*/
113 /* TODO: we have to mechanism that this does not happend at all, and get rid of this 'If'. */
114 /*#pragma omp parallel for private(i,iptr,j,sum) schedule(static)
115 for (i=0;i<n;i++) {
116 if (mis_marker[i]==IS_REMOVED) {
117 sum=0;
118 for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
119 j=A->pattern->index[iptr];
120 if (mis_marker[j]==IS_REMOVED)
121 sum+=A->val[iptr];
122 }
123 if(ABS(sum)<1.e-25)
124 mis_marker[i]=IS_IN_SET;
125 }
126 }
127 */
128
129 /* swap to TRUE/FALSE in mis_marker */
130 #pragma omp parallel for private(i) schedule(static)
131 for (i=0;i<n;i++) mis_marker[i]=(mis_marker[i]!=IS_IN_SET);
132
133 MEMFREE(diagptr);
134 }
135
136 /*
137 * Ruge-Stueben strength of connection mask.
138 *
139 */
140 void Paso_Pattern_RS(Paso_SparseMatrix* A, index_t* mis_marker, double theta)
141 {
142 dim_t i,n,j;
143 index_t iptr;
144 double threshold,min_offdiagonal;
145
146 Paso_Pattern *out=NULL;
147
148 Paso_IndexList* index_list=NULL;
149
150 index_list=TMPMEMALLOC(A->pattern->numOutput,Paso_IndexList);
151 if (! Paso_checkPtr(index_list)) {
152 #pragma omp parallel for private(i) schedule(static)
153 for(i=0;i<A->pattern->numOutput;++i) {
154 index_list[i].extension=NULL;
155 index_list[i].n=0;
156 }
157 }
158
159
160 n=A->numRows;
161 if (A->pattern->type & PATTERN_FORMAT_SYM) {
162 Paso_setError(TYPE_ERROR,"Paso_Pattern_RS: symmetric matrix pattern is not supported yet");
163 return;
164 }
165 #pragma omp parallel for private(i,iptr,min_offdiagonal,threshold,j) schedule(static)
166 for (i=0;i<n;++i) {
167 if(mis_marker[i]==IS_AVAILABLE) {
168 min_offdiagonal = DBL_MAX;
169 for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
170 if(A->pattern->index[iptr] != i){
171 min_offdiagonal = MIN(min_offdiagonal,A->val[iptr]);
172 }
173 }
174
175 threshold = theta*min_offdiagonal;
176 for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
177 j=A->pattern->index[iptr];
178 if(A->val[iptr]<=threshold) {
179 if(j!=i) {
180 Paso_IndexList_insertIndex(&(index_list[i]),j);
181 Paso_IndexList_insertIndex(&(index_list[j]),i);
182 }
183 }
184 }
185 }
186 }
187
188
189 out=Paso_IndexList_createPattern(0, A->pattern->numOutput,index_list,0,A->pattern->numInput,0);
190
191 /* clean up */
192 if (index_list!=NULL) {
193 #pragma omp parallel for private(i) schedule(static)
194 for(i=0;i<A->pattern->numOutput;++i) Paso_IndexList_free(index_list[i].extension);
195 }
196 TMPMEMFREE(index_list);
197
198 /*Paso_Pattern_mis(out,mis_marker);*/
199 Paso_Pattern_greedy(out,mis_marker);
200 Paso_Pattern_free(out);
201 }
202
203 void Paso_Pattern_Aggregiation(Paso_SparseMatrix* A, index_t* mis_marker, double theta)
204 {
205 dim_t i,j,n;
206 index_t iptr;
207 double diag,eps_Aii,val;
208 double* diags;
209
210
211 Paso_Pattern *out=NULL;
212 Paso_IndexList* index_list=NULL;
213
214 n=A->numRows;
215 diags=MEMALLOC(n,double);
216
217 index_list=TMPMEMALLOC(A->pattern->numOutput,Paso_IndexList);
218 if (! Paso_checkPtr(index_list)) {
219 #pragma omp parallel for private(i) schedule(static)
220 for(i=0;i<A->pattern->numOutput;++i) {
221 index_list[i].extension=NULL;
222 index_list[i].n=0;
223 }
224 }
225
226 if (A->pattern->type & PATTERN_FORMAT_SYM) {
227 Paso_setError(TYPE_ERROR,"Paso_Pattern_Aggregiation: symmetric matrix pattern is not supported yet");
228 return;
229 }
230
231
232 #pragma omp parallel for private(i,iptr) reduction(+:diag) schedule(static)
233 for (i=0;i<n;++i) {
234 diag = 0;
235 for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
236 if(A->pattern->index[iptr] == i){
237 diag+=A->val[iptr];
238 }
239 }
240 diags[i]=ABS(diag);
241 }
242
243
244 #pragma omp parallel for private(i,iptr,j,val,eps_Aii) schedule(static)
245 for (i=0;i<n;++i) {
246 if (mis_marker[i]==IS_AVAILABLE) {
247 eps_Aii = theta*theta*diags[i];
248 val=0.;
249 for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
250 j=A->pattern->index[iptr];
251 val=A->val[iptr];
252 if(j!= i) {
253 if(val*val>=eps_Aii * diags[j]) {
254 Paso_IndexList_insertIndex(&(index_list[i]),j);
255 }
256 }
257 }
258 }
259 }
260
261 out=Paso_IndexList_createPattern(0, A->pattern->numOutput,index_list,0,A->pattern->numInput,0);
262
263 /* clean up */
264 if (index_list!=NULL) {
265 #pragma omp parallel for private(i) schedule(static)
266 for(i=0;i<A->pattern->numOutput;++i) Paso_IndexList_free(index_list[i].extension);
267 }
268
269 TMPMEMFREE(index_list);
270 MEMFREE(diags);
271
272
273 /*Paso_Pattern_mis(out,mis_marker);*/
274 Paso_Pattern_greedy(out,mis_marker);
275
276 }
277
278 /* Greedy algorithm */
279 void Paso_Pattern_greedy(Paso_Pattern* pattern, index_t* mis_marker) {
280
281 dim_t i,j;
282 /*double sum;*/
283 index_t iptr;
284 bool_t passed=FALSE;
285 dim_t n=pattern->numOutput;
286
287 if (pattern->type & PATTERN_FORMAT_SYM) {
288 Paso_setError(TYPE_ERROR,"Paso_Pattern_greedy: symmetric matrix pattern is not supported yet");
289 return;
290 }
291
292 /* We do not need this loop if we set IS_IN_MIS=IS_AVAILABLE. */
293 #pragma omp parallel for private(i) schedule(static)
294 for (i=0;i<n;++i)
295 if(mis_marker[i]==IS_AVAILABLE)
296 mis_marker[i]=IS_IN_SET;
297
298
299 for (i=0;i<n;++i) {
300 if (mis_marker[i]==IS_IN_SET) {
301 for (iptr=pattern->ptr[i];iptr<pattern->ptr[i+1]; ++iptr) {
302 j=pattern->index[iptr];
303 mis_marker[j]=IS_REMOVED;
304 }
305 }
306 }
307
308
309
310 for (i=0;i<n;i++) {
311 if (mis_marker[i]==IS_REMOVED) {
312 passed=TRUE;
313 for (iptr=pattern->ptr[i];iptr<pattern->ptr[i+1]; ++iptr) {
314 j=pattern->index[iptr];
315 if (mis_marker[j]==IS_REMOVED) {
316 passed=TRUE;
317 }
318 else {
319 passed=FALSE;
320 break;
321 }
322 }
323 }
324 if (passed) mis_marker[i]=IS_IN_SET;
325 }
326
327 /* swap to TRUE/FALSE in mis_marker */
328 #pragma omp parallel for private(i) schedule(static)
329 for (i=0;i<n;i++) mis_marker[i]=(mis_marker[i]!=IS_IN_SET);
330
331 }
332
333
334 void Paso_Pattern_greedy_color(Paso_Pattern* pattern, index_t* mis_marker) {
335
336 dim_t i,j;
337 /*double sum;*/
338 index_t iptr;
339 index_t num_colors;
340 index_t* colorOf;
341 register index_t color;
342 bool_t passed=FALSE;
343 dim_t n=pattern->numOutput;
344
345
346 colorOf=MEMALLOC(n,index_t);
347
348 if (pattern->type & PATTERN_FORMAT_SYM) {
349 Paso_setError(TYPE_ERROR,"Paso_Pattern_greedy: symmetric matrix pattern is not supported yet");
350 return;
351 }
352
353 Paso_Pattern_color(pattern,&num_colors,colorOf);
354
355 /* We do not need this loop if we set IS_IN_MIS=IS_AVAILABLE. */
356 #pragma omp parallel for private(i) schedule(static)
357 for (i=0;i<n;++i)
358 if(mis_marker[i]==IS_AVAILABLE)
359 mis_marker[i]=IS_IN_SET;
360
361 #pragma omp barrier
362 for (color=0;color<num_colors;++color) {
363 #pragma omp parallel for schedule(static) private(i,iptr,j)
364 for (i=0;i<n;++i) {
365 if (colorOf[i]==color) {
366 if (mis_marker[i]==IS_IN_SET) {
367 for (iptr=pattern->ptr[i];iptr<pattern->ptr[i+1]; ++iptr) {
368 j=pattern->index[iptr];
369 if (colorOf[j]<color)
370 mis_marker[j]=IS_REMOVED;
371 }
372 }
373 }
374 }
375 }
376
377
378 #pragma omp barrier
379 for (color=0;color<num_colors;++color) {
380 #pragma omp parallel for schedule(static) private(i,iptr,j)
381 for (i=0;i<n;i++) {
382 if (colorOf[i]==color) {
383 if (mis_marker[i]==IS_REMOVED) {
384 passed=TRUE;
385 for (iptr=pattern->ptr[i];iptr<pattern->ptr[i+1]; ++iptr) {
386 j=pattern->index[iptr];
387 if (colorOf[j]<color && passed) {
388 if (mis_marker[j]==IS_REMOVED) {
389 passed=TRUE;
390 }
391 else {
392 passed=FALSE;
393 /*break;*/
394 }
395 }
396 }
397 }
398 if (passed) mis_marker[i]=IS_IN_SET;
399 }
400 }
401 }
402
403 /* swap to TRUE/FALSE in mis_marker */
404 #pragma omp parallel for private(i) schedule(static)
405 for (i=0;i<n;i++) mis_marker[i]=(mis_marker[i]!=IS_IN_SET);
406
407 MEMFREE(colorOf);
408 }
409
410
411
412 #undef IS_AVAILABLE
413 #undef IS_IN_SET
414 #undef IS_REMOVED

  ViewVC Help
Powered by ViewVC 1.1.26