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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3412 - (show annotations)
Fri Dec 10 02:03:49 2010 UTC (8 years, 10 months ago) by gross
File MIME type: text/plain
File size: 15414 byte(s)
some fixes for MKL lapack
1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 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: Gauss-Seidel */
18
19 /**************************************************************/
20
21 /* Author: artak@uq.edu.au */
22
23 /**************************************************************/
24
25 #include "Paso.h"
26 #include "Preconditioner.h"
27 #include "PasoUtil.h"
28 #include "BlockOps.h"
29
30 #include <stdio.h>
31
32
33 /**************************************************************/
34
35 /* free all memory used by Smoother */
36
37 void Paso_Preconditioner_Smoother_free(Paso_Preconditioner_Smoother * in) {
38 if (in!=NULL) {
39 Paso_Preconditioner_LocalSmoother_free(in->localSmoother);
40 MEMFREE(in);
41 }
42 }
43 void Paso_Preconditioner_LocalSmoother_free(Paso_Preconditioner_LocalSmoother * in) {
44 if (in!=NULL) {
45 MEMFREE(in->diag);
46 MEMFREE(in->pivot);
47 MEMFREE(in->buffer);
48 MEMFREE(in);
49 }
50 }
51 /**************************************************************/
52
53 /* constructs the symmetric Gauss-Seidel preconditioner
54
55 */
56 Paso_Preconditioner_Smoother* Paso_Preconditioner_Smoother_alloc(Paso_SystemMatrix * A_p, const bool_t jacobi, const bool_t is_local, const bool_t verbose)
57 {
58
59 /* allocations: */
60 Paso_Preconditioner_Smoother* out=MEMALLOC(1,Paso_Preconditioner_Smoother);
61 if (! Esys_checkPtr(out)) {
62 out->localSmoother=Paso_Preconditioner_LocalSmoother_alloc(A_p->mainBlock,jacobi,verbose);
63 out->is_local=is_local;
64 }
65 if (Esys_MPIInfo_noError(A_p->mpi_info)) {
66 return out;
67 } else {
68 Paso_Preconditioner_Smoother_free(out);
69 return NULL;
70 }
71 }
72 Paso_Preconditioner_LocalSmoother* Paso_Preconditioner_LocalSmoother_alloc(Paso_SparseMatrix * A_p, const bool_t jacobi, bool_t verbose)
73 {
74
75 dim_t n=A_p->numRows;
76 dim_t n_block=A_p->row_block_size;
77 dim_t block_size=A_p->block_size;
78
79 double time0=Esys_timer();
80 /* allocations: */
81 Paso_Preconditioner_LocalSmoother* out=MEMALLOC(1,Paso_Preconditioner_LocalSmoother);
82 if (! Esys_checkPtr(out)) {
83
84 out->diag=MEMALLOC( ((size_t) n) * ((size_t) block_size),double);
85 out->pivot=MEMALLOC( ((size_t) n) * ((size_t) n_block), index_t);
86 out->buffer=MEMALLOC( ((size_t) n) * ((size_t) n_block), double);
87 out->Jacobi=jacobi;
88
89 if ( ! ( Esys_checkPtr(out->diag) || Esys_checkPtr(out->pivot) ) ) {
90 Paso_SparseMatrix_invMain(A_p, out->diag, out->pivot );
91 }
92
93 }
94 time0=Esys_timer()-time0;
95
96 if (Esys_noError()) {
97 return out;
98 } else {
99 Paso_Preconditioner_LocalSmoother_free(out);
100 return NULL;
101 }
102 }
103
104 /*
105
106 performs a few sweeps of the from
107
108 S (x_{k} - x_{k-1}) = b - A x_{k-1}
109
110 where x_{0}=0 and S provides some approximatioon of A.
111
112 Under MPI S is build on using A_p->mainBlock only.
113 if Smoother is local the defect b - A x_{k-1} is calculated using A_p->mainBlock only.
114
115 */
116
117 void Paso_Preconditioner_Smoother_solve(Paso_SystemMatrix* A_p, Paso_Preconditioner_Smoother * smoother, double * x, const double * b,
118 const dim_t sweeps, const bool_t x_is_initial)
119 {
120 const dim_t n= (A_p->mainBlock->numRows) * (A_p->mainBlock->row_block_size);
121
122 double *b_new = smoother->localSmoother->buffer;
123 dim_t nsweeps=sweeps;
124 if (smoother->is_local) {
125 Paso_Preconditioner_LocalSmoother_solve(A_p->mainBlock,smoother->localSmoother,x,b,sweeps,x_is_initial);
126 } else {
127 if (! x_is_initial) {
128 Paso_Copy(n, x, b);
129 Paso_Preconditioner_LocalSmoother_Sweep(A_p->mainBlock,smoother->localSmoother,x);
130 nsweeps--;
131 }
132 while (nsweeps > 0 ) {
133 Paso_Copy(n, b_new, b);
134 Paso_SystemMatrix_MatrixVector((-1.), A_p, x, 1., b_new); /* b_new = b - A*x */
135 Paso_Preconditioner_LocalSmoother_Sweep(A_p->mainBlock,smoother->localSmoother,b_new);
136 Paso_AXPY(n, x, 1., b_new);
137 nsweeps--;
138 }
139
140 }
141 }
142 void Paso_Preconditioner_LocalSmoother_solve(Paso_SparseMatrix* A_p, Paso_Preconditioner_LocalSmoother * smoother, double * x, const double * b,
143 const dim_t sweeps, const bool_t x_is_initial)
144 {
145 const dim_t n= (A_p->numRows) * (A_p->row_block_size);
146 double *b_new = smoother->buffer;
147 dim_t nsweeps=sweeps;
148
149 if (! x_is_initial) {
150 Paso_Copy(n, x, b);
151 Paso_Preconditioner_LocalSmoother_Sweep(A_p,smoother,x);
152 nsweeps--;
153 }
154
155 while (nsweeps > 0 ) {
156 Paso_Copy(n, b_new, b);
157
158 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0((-1.), A_p, x, 1., b_new); /* b_new = b - A*x */
159 Paso_Preconditioner_LocalSmoother_Sweep(A_p,smoother,b_new);
160 Paso_AXPY(n, x, 1., b_new);
161 nsweeps--;
162 }
163 }
164
165 void Paso_Preconditioner_LocalSmoother_Sweep(Paso_SparseMatrix* A, Paso_Preconditioner_LocalSmoother * smoother, double * x)
166 {
167 const dim_t nt=omp_get_max_threads();
168 if (smoother->Jacobi) {
169 Paso_BlockOps_solveAll(A->row_block_size,A->numRows,smoother->diag,smoother->pivot,x);
170 } else {
171 if (nt < 2) {
172 Paso_Preconditioner_LocalSmoother_Sweep_sequential(A,smoother,x);
173 } else {
174 Paso_Preconditioner_LocalSmoother_Sweep_colored(A,smoother,x);
175 }
176 }
177 }
178
179 /* inplace Gauss-Seidel sweep in seqential mode: */
180
181 void Paso_Preconditioner_LocalSmoother_Sweep_sequential(Paso_SparseMatrix* A_p, Paso_Preconditioner_LocalSmoother * smoother, double * x)
182 {
183 const dim_t n=A_p->numRows;
184 const dim_t n_block=A_p->row_block_size;
185 double *diag = smoother->diag;
186 index_t* pivot = smoother->pivot;
187 const dim_t block_len=A_p->block_size;
188
189 register dim_t i,k;
190 register index_t iptr_ik, mm;
191 register double rtmp;
192 int failed = 0;
193
194 const index_t* ptr_main = Paso_SparseMatrix_borrowMainDiagonalPointer(A_p);
195 /* forward substitution */
196
197 if (n_block==1) {
198 x[0]*=diag[0];
199 for (i = 1; i < n; ++i) {
200 mm=ptr_main[i];
201 /* x_i=x_i-a_ik*x_k (with k<i) */
202 rtmp=x[i];
203 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<mm; ++iptr_ik) {
204 k=A_p->pattern->index[iptr_ik];
205 rtmp-=A_p->val[iptr_ik]*x[k];
206 }
207 x[i]=rtmp*diag[i];
208 }
209 } else if (n_block==2) {
210 Paso_BlockOps_MViP_2(&diag[0], &x[0]);
211 for (i = 1; i < n; ++i) {
212 mm=ptr_main[i];
213 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<mm; ++iptr_ik) {
214 k=A_p->pattern->index[iptr_ik];
215 Paso_BlockOps_SMV_2(&x[2*i], &A_p->val[4*iptr_ik], &x[2*k]);
216 }
217 Paso_BlockOps_MViP_2(&diag[4*i], &x[2*i]);
218 }
219 } else if (n_block==3) {
220 Paso_BlockOps_MViP_3(&diag[0], &x[0]);
221 for (i = 1; i < n; ++i) {
222 mm=ptr_main[i];
223 /* x_i=x_i-a_ik*x_k */
224 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<mm; ++iptr_ik) {
225 k=A_p->pattern->index[iptr_ik];
226 Paso_BlockOps_SMV_3(&x[3*i], &A_p->val[9*iptr_ik], &x[3*k]);
227 }
228 Paso_BlockOps_MViP_3(&diag[9*i], &x[3*i]);
229 }
230 } else {
231 Paso_BlockOps_solve_N(n_block, &x[0], &diag[0], &pivot[0], &failed);
232 for (i = 1; i < n; ++i) {
233 mm=ptr_main[i];
234 /* x_i=x_i-a_ik*x_k */
235 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<mm; ++iptr_ik) {
236 k=A_p->pattern->index[iptr_ik];
237 Paso_BlockOps_SMV_N(n_block, &x[n_block*i], &A_p->val[block_len*iptr_ik], &x[n_block*k]);
238 }
239 Paso_BlockOps_solve_N(n_block, &x[n_block*i], &diag[block_len*i], &pivot[n_block*i], &failed)
240 }
241 }
242 /* backward substitution */
243
244 if (n_block==1) {
245 for (i = n-2; i > -1; --i) {
246 mm=ptr_main[i];
247 rtmp=x[i]*A_p->val[mm];
248 for (iptr_ik=mm+1; iptr_ik < A_p->pattern->ptr[i+1]; ++iptr_ik) {
249 k=A_p->pattern->index[iptr_ik];
250 rtmp-=A_p->val[iptr_ik]*x[k];
251 }
252 x[i]=diag[i]*rtmp;
253 }
254
255 } else if (n_block==2) {
256 for (i = n-2; i > -1; --i) {
257 mm=ptr_main[i];
258 Paso_BlockOps_MViP_2(&A_p->val[4*mm], &x[2*i]);
259 for (iptr_ik=mm+1; iptr_ik < A_p->pattern->ptr[i+1]; ++iptr_ik) {
260 k=A_p->pattern->index[iptr_ik];
261 Paso_BlockOps_SMV_2(&x[2*i], &A_p->val[4*iptr_ik], &x[2*k]);
262 }
263 Paso_BlockOps_MViP_2(&diag[i*4], &x[2*i]);
264
265 }
266 } else if (n_block==3) {
267 for (i = n-2; i > -1; --i) {
268 mm=ptr_main[i];
269 Paso_BlockOps_MViP_3(&A_p->val[9*mm], &x[3*i]);
270 for (iptr_ik=mm+1; iptr_ik < A_p->pattern->ptr[i+1]; ++iptr_ik) {
271 k=A_p->pattern->index[iptr_ik];
272 Paso_BlockOps_SMV_3(&x[3*i], &A_p->val[9*iptr_ik], &x[3*k]);
273 }
274 Paso_BlockOps_MViP_3(&diag[i*9], &x[3*i]);
275 }
276
277 } else {
278 double *y=TMPMEMALLOC(n_block, double);
279 for (i = n-2; i > -1; --i) {
280 mm=ptr_main[i];
281 Paso_BlockOps_MV_N(n_block, &y[0], &A_p->val[block_len*mm], &x[n_block*i]);
282 for (iptr_ik=mm+1; iptr_ik < A_p->pattern->ptr[i+1]; ++iptr_ik) {
283 k=A_p->pattern->index[iptr_ik];
284 Paso_BlockOps_SMV_N(n_block, &y[0], &A_p->val[block_len*iptr_ik], &x[n_block*k]);
285 }
286 Paso_BlockOps_Cpy_N(n_block ,&x[n_block*i], &y[0]);
287 Paso_BlockOps_solve_N(n_block, &x[n_block*i], &diag[i*block_len], &pivot[i*n_block], &failed);
288 }
289 TMPMEMFREE(y);
290 }
291
292 if (failed > 0) {
293 Esys_setError(ZERO_DIVISION_ERROR, "Paso_Preconditioner_LocalSmoother_Sweep_sequential: non-regular main diagonal block.");
294 }
295
296 return;
297 }
298
299 void Paso_Preconditioner_LocalSmoother_Sweep_colored(Paso_SparseMatrix* A_p, Paso_Preconditioner_LocalSmoother * smoother, double * x)
300 {
301 const dim_t n=A_p->numRows;
302 const dim_t n_block=A_p->row_block_size;
303 double *diag = smoother->diag;
304 index_t* pivot = smoother->pivot;
305 const dim_t block_len=A_p->block_size;
306 double *y;
307
308 register dim_t i,k;
309 register index_t color,iptr_ik, mm;
310 register double rtmp;
311 int failed = 0;
312
313 const index_t* coloring = Paso_Pattern_borrowColoringPointer(A_p->pattern);
314 const dim_t num_colors = Paso_Pattern_getNumColors(A_p->pattern);
315 const index_t* ptr_main = Paso_SparseMatrix_borrowMainDiagonalPointer(A_p);
316
317 #pragma omp parallel private(mm, i,iptr_ik,k,rtmp, color, y)
318 {
319 if (n_block>3) {
320 y=TMPMEMALLOC(n_block, double);
321 } else {
322 y=NULL;
323 }
324 /* forward substitution */
325
326 /* color = 0 */
327 if (n_block==1) {
328 #pragma omp for schedule(static)
329 for (i = 0; i < n; ++i) {
330 if (coloring[i]== 0 ) x[i]*=diag[i];
331 }
332 } else if (n_block==2) {
333 #pragma omp for schedule(static)
334 for (i = 0; i < n; ++i) {
335 if (coloring[i]== 0 ) Paso_BlockOps_MViP_2(&diag[i*4], &x[2*i]);
336 }
337 } else if (n_block==3) {
338 #pragma omp for schedule(static)
339 for (i = 0; i < n; ++i) {
340 if (coloring[i]==0) Paso_BlockOps_MViP_3(&diag[i*9], &x[3*i]);
341 }
342 } else {
343 #pragma omp for schedule(static)
344 for (i = 0; i < n; ++i) {
345 if (coloring[i]==0) Paso_BlockOps_solve_N(n_block, &x[n_block*i], &diag[block_len*i], &pivot[n_block*i], &failed);
346 }
347 }
348
349
350 for (color=1;color<num_colors;++color) {
351
352 if (n_block==1) {
353 #pragma omp for schedule(static)
354 for (i = 0; i < n; ++i) {
355 if (coloring[i]==color) {
356 /* x_i=x_i-a_ik*x_k */
357 rtmp=x[i];
358 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
359 k=A_p->pattern->index[iptr_ik];
360 if (coloring[k]<color) rtmp-=A_p->val[iptr_ik]*x[k];
361 }
362 x[i]=diag[i]*rtmp;
363 }
364 }
365 } else if (n_block==2) {
366 #pragma omp for schedule(static)
367 for (i = 0; i < n; ++i) {
368 if (coloring[i]==color) {
369 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
370 k=A_p->pattern->index[iptr_ik];
371 if (coloring[k]<color) Paso_BlockOps_SMV_2(&x[2*i], &A_p->val[4*iptr_ik], &x[2*k]);
372 }
373 Paso_BlockOps_MViP_2(&diag[4*i], &x[2*i]);
374 }
375 }
376 } else if (n_block==3) {
377 #pragma omp for schedule(static)
378 for (i = 0; i < n; ++i) {
379 if (coloring[i]==color) {
380 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
381 k=A_p->pattern->index[iptr_ik];
382 if (coloring[k]<color) Paso_BlockOps_SMV_3(&x[3*i], &A_p->val[9*iptr_ik], &x[3*k]);
383 }
384 Paso_BlockOps_MViP_3(&diag[9*i], &x[3*i]);
385 }
386 }
387 } else {
388 #pragma omp for schedule(static)
389 for (i = 0; i < n; ++i) {
390 if (coloring[i] == color) {
391 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
392 k=A_p->pattern->index[iptr_ik];
393 if (coloring[k]<color) Paso_BlockOps_SMV_N(n_block, &x[n_block*i], &A_p->val[block_len*iptr_ik], &x[n_block*k]);
394 }
395 Paso_BlockOps_solve_N(n_block, &x[n_block*i], &diag[block_len*i], &pivot[n_block*i], &failed);
396 }
397 }
398 }
399 } /* end of coloring loop */
400
401 /* backward substitution */
402 for (color=(num_colors)-2 ;color>-1;--color) { /* Note: color=(num_colors)-1 is not required */
403 if (n_block==1) {
404 #pragma omp for schedule(static)
405 for (i = 0; i < n; ++i) {
406 if (coloring[i]==color) {
407 mm=ptr_main[i];
408 rtmp=A_p->val[mm]*x[i];
409 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
410 k=A_p->pattern->index[iptr_ik];
411 if (coloring[k]>color) rtmp-=A_p->val[iptr_ik]*x[k];
412 }
413 x[i]= rtmp*diag[i];
414 }
415 }
416 } else if (n_block==2) {
417 #pragma omp for schedule(static)
418 for (i = 0; i < n; ++i) {
419 if (coloring[i]==color) {
420 mm=ptr_main[i];
421 Paso_BlockOps_MViP_2(&A_p->val[4*mm], &x[2*i]);
422 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
423 k=A_p->pattern->index[iptr_ik];
424 if (coloring[k]>color) Paso_BlockOps_SMV_2(&x[2*i], &A_p->val[4*iptr_ik], &x[2*k]);
425 }
426 Paso_BlockOps_MViP_2(&diag[4*i], &x[2*i]);
427 }
428 }
429 } else if (n_block==3) {
430 #pragma omp for schedule(static)
431 for (i = 0; i < n; ++i) {
432 if (coloring[i]==color) {
433 mm=ptr_main[i];
434 Paso_BlockOps_MViP_3(&A_p->val[9*mm], &x[3*i]);
435 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
436 k=A_p->pattern->index[iptr_ik];
437 if (coloring[k]>color) Paso_BlockOps_SMV_3(&x[3*i], &A_p->val[9*iptr_ik], &x[3*k]);
438 }
439 Paso_BlockOps_MViP_3(&diag[9*i], &x[3*i]);
440 }
441 }
442 } else {
443 #pragma omp for schedule(static)
444 for (i = 0; i < n; ++i) {
445 if (coloring[i]==color) {
446 mm=ptr_main[i];
447 Paso_BlockOps_MV_N(n_block, &y[0], &A_p->val[block_len*mm], &x[n_block*i]);
448 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
449 k=A_p->pattern->index[iptr_ik];
450 if (coloring[k]>color) Paso_BlockOps_SMV_N(n_block, &y[0], &A_p->val[block_len*iptr_ik], &x[n_block*k]);
451 }
452 Paso_BlockOps_Cpy_N(n_block ,&x[n_block*i], &y[0]);
453 Paso_BlockOps_solve_N(n_block, &x[n_block*i], &diag[i*block_len], &pivot[i*n_block], &failed);
454 }
455 }
456 }
457 }
458 TMPMEMFREE(y);
459 }
460 if (failed > 0) {
461 Esys_setError(ZERO_DIVISION_ERROR, "Paso_Preconditioner_LocalSmoother_Sweep_colored: non-regular main diagonal block.");
462 }
463 return;
464 }

  ViewVC Help
Powered by ViewVC 1.1.26