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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3097 - (show annotations)
Fri Aug 20 04:59:12 2010 UTC (9 years, 1 month ago) by gross
File MIME type: text/plain
File size: 14074 byte(s)
some modifications to the GaussSeidel
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 "Solver.h"
27 #include "PasoUtil.h"
28 #include "BlockOps.h"
29
30 #include <stdio.h>
31
32
33 /**************************************************************/
34
35 /* free all memory used by GS */
36
37 void Paso_Solver_GS_free(Paso_Solver_GS * in) {
38 if (in!=NULL) {
39 Paso_Solver_LocalGS_free(in->localGS);
40 MEMFREE(in);
41 }
42 }
43 void Paso_Solver_LocalGS_free(Paso_Solver_LocalGS * 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_Solver_GS* Paso_Solver_getGS(Paso_SystemMatrix * A_p, dim_t sweeps, bool_t is_local, bool_t verbose)
57 {
58
59 /* allocations: */
60 Paso_Solver_GS* out=MEMALLOC(1,Paso_Solver_GS);
61 if (! Paso_checkPtr(out)) {
62 out->localGS=Paso_Solver_getLocalGS(A_p->mainBlock,sweeps,verbose);
63 out->is_local=is_local;
64 }
65 if (Paso_MPIInfo_noError(A_p->mpi_info)) {
66 return out;
67 } else {
68 Paso_Solver_GS_free(out);
69 return NULL;
70 }
71 }
72 Paso_Solver_LocalGS* Paso_Solver_getLocalGS(Paso_SparseMatrix * A_p, dim_t sweeps, bool_t verbose) {
73
74 dim_t n=A_p->numRows;
75 dim_t n_block=A_p->row_block_size;
76 dim_t block_size=A_p->block_size;
77
78 double time0=Paso_timer();
79 /* allocations: */
80 Paso_Solver_LocalGS* out=MEMALLOC(1,Paso_Solver_LocalGS);
81 if (! Paso_checkPtr(out)) {
82
83 out->diag=MEMALLOC( ((size_t) n) * ((size_t) block_size),double);
84 out->pivot=MEMALLOC( ((size_t) n) * ((size_t) n_block), index_t);
85 out->buffer=MEMALLOC( ((size_t) n) * ((size_t) n_block), double);
86 out->sweeps=sweeps;
87
88 if ( ! ( Paso_checkPtr(out->diag) || Paso_checkPtr(out->pivot) ) ) {
89 Paso_SparseMatrix_invMain(A_p, out->diag, out->pivot );
90 }
91
92 }
93 time0=Paso_timer()-time0;
94
95 if (Paso_noError()) {
96 if (verbose) printf("timing: Gauss-Seidel preparation: elemination : %e\n",time0);
97 return out;
98 } else {
99 Paso_Solver_LocalGS_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 GS is local the defect b - A x_{k-1} is calculated using A_p->mainBlock only.
114
115 */
116
117 void Paso_Solver_solveGS(Paso_SystemMatrix* A_p, Paso_Solver_GS * gs, double * x, const double * b)
118 {
119 register dim_t i;
120 const dim_t n= (A_p->mainBlock->numRows) * (A_p->mainBlock->row_block_size);
121
122 double *b_new = gs->localGS->buffer;
123 dim_t sweeps=gs->localGS->sweeps;
124
125 if (gs->is_local) {
126 Paso_Solver_solveLocalGS(A_p->mainBlock,gs->localGS,x,b);
127 } else {
128 #pragma omp parallel for private(i) schedule(static)
129 for (i=0;i<n;++i) x[i]=b[i];
130
131 Paso_Solver_localGSSweep(A_p->mainBlock,gs->localGS,x);
132 while (sweeps > 0 ) {
133 #pragma omp parallel for private(i) schedule(static)
134 for (i=0;i<n;++i) b_new[i]=b[i];
135
136 Paso_SystemMatrix_MatrixVector((-1.), A_p, x, 1., b_new); /* b_new = b - A*x */
137
138 Paso_Solver_localGSSweep(A_p->mainBlock,gs->localGS,b_new);
139
140 #pragma omp parallel for private(i) schedule(static)
141 for (i=0;i<n;++i) x[i]+=b_new[i];
142
143 sweeps--;
144 }
145
146 }
147 }
148 void Paso_Solver_solveLocalGS(Paso_SparseMatrix* A_p, Paso_Solver_LocalGS * gs, double * x, const double * b)
149 {
150 register dim_t i;
151 const dim_t n= (A_p->numRows) * (A_p->row_block_size);
152 double *b_new = gs->buffer;
153 dim_t sweeps=gs->sweeps;
154
155 #pragma omp parallel for private(i) schedule(static)
156 for (i=0;i<n;++i) x[i]=b[i];
157 Paso_Solver_localGSSweep(A_p,gs,x);
158
159 while (sweeps > 0 ) {
160 #pragma omp parallel for private(i) schedule(static)
161 for (i=0;i<n;++i) b_new[i]=b[i];
162
163 Paso_SparseMatrix_MatrixVector_CSC_OFFSET0((-1.), A_p, x, 1., b_new); /* b_new = b - A*x */
164
165 Paso_Solver_localGSSweep(A_p,gs,b_new);
166
167 #pragma omp parallel for private(i) schedule(static)
168 for (i=0;i<n;++i) x[i]+=b_new[i];
169
170 sweeps--;
171 }
172 }
173
174 void Paso_Solver_localGSSweep(Paso_SparseMatrix* A, Paso_Solver_LocalGS * gs, double * x)
175 {
176 #ifdef _OPENMP
177 const dim_t nt=omp_get_max_threads();
178 #else
179 const dim_t nt = 1;
180 #endif
181 if (nt > 1) {
182 Paso_Solver_localGSSweep_sequential(A,gs,x);
183 } else {
184 Paso_Solver_localGSSweep_colored(A,gs,x);
185 }
186 }
187
188 /* inplace Gauss-Seidel sweep in seqential mode: */
189
190 void Paso_Solver_localGSSweep_sequential(Paso_SparseMatrix* A_p, Paso_Solver_LocalGS * gs, double * x)
191 {
192 const dim_t n=A_p->numRows;
193 const dim_t n_block=A_p->row_block_size;
194 const double *diag = gs->diag;
195 /* const index_t* pivot = gs->pivot;
196 const dim_t block_size=A_p->block_size; use for block size >3*/
197
198 register dim_t i,k;
199 register index_t iptr_ik, mm;
200
201 const index_t* ptr_main = Paso_SparseMatrix_borrowMainDiagonalPointer(A_p);
202
203 /* forward substitution */
204
205 if (n_block==1) {
206 for (i = 0; i < n; ++i) {
207 mm=ptr_main[i];
208 /* x_i=x_i-a_ik*x_k (with k<i) */
209 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<mm; ++iptr_ik) {
210 k=A_p->pattern->index[iptr_ik];
211 Paso_BlockOps_SMV_1(&x[i], &A_p->val[iptr_ik], &x[k]);
212 }
213 Paso_BlockOps_MV_1(&x[i], &A_p->val[i], &x[i]);
214 }
215 } else if (n_block==2) {
216 for (i = 0; i < n; ++i) {
217 mm=ptr_main[i];
218
219 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<mm; ++iptr_ik) {
220 k=A_p->pattern->index[iptr_ik];
221 Paso_BlockOps_SMV_2(&x[2*i], &A_p->val[4*iptr_ik], &x[2*k]);
222 }
223 Paso_BlockOps_MV_2(&x[2*i], &A_p->val[4*i], &x[2*i]);
224 }
225 } else if (n_block==3) {
226 for (i = 0; i < n; ++i) {
227 mm=ptr_main[i];
228 /* x_i=x_i-a_ik*x_k */
229 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<mm; ++iptr_ik) {
230 k=A_p->pattern->index[iptr_ik];
231 Paso_BlockOps_SMV_3(&x[3*i], &A_p->val[9*iptr_ik], &x[3*k]);
232 }
233 Paso_BlockOps_MV_3(&x[3*i], &A_p->val[9*i], &x[3*i]);
234 }
235
236 } /* add block size >3 */
237
238
239
240 /* backward substitution */
241
242 if (n_block==1) {
243 for (i = n-2; i > -1; ++i) {
244 mm=ptr_main[i];
245 Paso_BlockOps_MV_1(&x[i], &A_p->val[mm], &x[i]);
246 for (iptr_ik=mm+1; iptr_ik < A_p->pattern->ptr[i+1]; ++iptr_ik) {
247 k=A_p->pattern->index[iptr_ik];
248 Paso_BlockOps_SMV_1(&x[i], &A_p->val[iptr_ik], &x[k]);
249 }
250 Paso_BlockOps_MV_1(&x[i], &diag[i], &x[i]);
251 }
252 } else if (n_block==2) {
253 for (i = n-2; i > -1; ++i) {
254 mm=ptr_main[i];
255 Paso_BlockOps_MV_3(&x[2*i], &A_p->val[4*mm], &x[2*i]);
256 for (iptr_ik=mm+1; iptr_ik < A_p->pattern->ptr[i+1]; ++iptr_ik) {
257 k=A_p->pattern->index[iptr_ik];
258 Paso_BlockOps_SMV_2(&x[2*i], &A_p->val[4*iptr_ik], &x[2*k]);
259 }
260 Paso_BlockOps_MV_2(&x[2*i], &diag[i*4], &x[2*i]);
261 }
262 } else if (n_block==3) {
263 for (i = n-2; i > -1; ++i) {
264
265 mm=ptr_main[i];
266 Paso_BlockOps_MV_3(&x[3*i], &A_p->val[9*mm], &x[3*i]);
267
268 for (iptr_ik=mm+1; iptr_ik < A_p->pattern->ptr[i+1]; ++iptr_ik) {
269 k=A_p->pattern->index[iptr_ik];
270 Paso_BlockOps_SMV_3(&x[3*i], &A_p->val[9*iptr_ik], &x[3*k]);
271 }
272 Paso_BlockOps_MV_3(&x[3*i], &diag[i*9], &x[3*i]);
273 }
274
275 } /* add block size >3 */
276
277 return;
278 }
279
280 void Paso_Solver_localGSSweep_colored(Paso_SparseMatrix* A_p, Paso_Solver_LocalGS * gs, double * x)
281 {
282 const dim_t n=A_p->numRows;
283 const dim_t n_block=A_p->row_block_size;
284 const double *diag = gs->diag;
285 index_t* pivot = gs->pivot;
286 const dim_t block_size=A_p->block_size;
287
288 register dim_t i,k;
289 register index_t color,iptr_ik, mm;
290
291 const index_t* coloring = Paso_Pattern_borrowColoringPointer(A_p->pattern);
292 const dim_t num_colors = Paso_Pattern_getNumColors(A_p->pattern);
293 const index_t* ptr_main = Paso_SparseMatrix_borrowMainDiagonalPointer(A_p);
294
295 /* forward substitution */
296
297 /* color = 0 */
298 if (n_block==1) {
299 #pragma omp parallel for schedule(static) private(i)
300 for (i = 0; i < n; ++i) {
301 if (coloring[i]== 0 ) Paso_BlockOps_MV_1(&x[i], &diag[i], &x[i]);
302 }
303 } else if (n_block==2) {
304 #pragma omp parallel for schedule(static) private(i)
305 for (i = 0; i < n; ++i) {
306 if (coloring[i]== 0 ) Paso_BlockOps_MV_2(&x[2*i], &diag[i*4], &x[2*i]);
307 }
308 } else if (n_block==3) {
309 #pragma omp parallel for schedule(static) private(i)
310 for (i = 0; i < n; ++i) {
311 if (coloring[i]==0) Paso_BlockOps_MV_3(&x[3*i], &diag[i*9], &x[3*i]);
312 }
313 } else {
314 #pragma omp parallel for schedule(static) private(i)
315 for (i = 0; i < n; ++i) {
316 if (coloring[i]==0) Paso_BlockOps_Solve_N(n_block, &x[n_block*i], &diag[block_size*i], &pivot[n_block*i], &x[n_block*i]);
317 }
318 }
319
320 for (color=1;color<num_colors;++color) {
321 if (n_block==1) {
322 #pragma omp parallel for schedule(static) private(i,iptr_ik,k)
323 for (i = 0; i < n; ++i) {
324 if (coloring[i]==color) {
325 /* x_i=x_i-a_ik*x_k */
326 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
327 k=A_p->pattern->index[iptr_ik];
328 if (coloring[k]<color) Paso_BlockOps_SMV_1(&x[i], &A_p->val[iptr_ik], &x[k]);
329 }
330 Paso_BlockOps_MV_1(&x[i], &diag[i], &x[i]);
331 }
332 }
333 } else if (n_block==2) {
334 #pragma omp parallel for schedule(static) private(i,iptr_ik,k)
335 for (i = 0; i < n; ++i) {
336 if (coloring[i]==color) {
337 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
338 k=A_p->pattern->index[iptr_ik];
339 if (coloring[k]<color) Paso_BlockOps_SMV_2(&x[2*i], &A_p->val[4*iptr_ik], &x[2*k]);
340 }
341 Paso_BlockOps_MV_2(&x[2*i], &diag[4*i], &x[2*i]);
342 }
343
344 }
345 } else if (n_block==3) {
346 #pragma omp parallel for schedule(static) private(i,iptr_ik,k)
347 for (i = 0; i < n; ++i) {
348 if (coloring[i]==color) {
349 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
350 k=A_p->pattern->index[iptr_ik];
351 if (coloring[k]<color) Paso_BlockOps_SMV_3(&x[3*i], &A_p->val[9*iptr_ik], &x[3*k]);
352 }
353 Paso_BlockOps_MV_3(&x[3*i], &diag[9*i], &x[3*i]);
354 }
355 }
356 } else {
357 #pragma omp parallel for schedule(static) private(i,iptr_ik,k)
358 for (i = 0; i < n; ++i) {
359 if (coloring[i] == color) {
360 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
361 k=A_p->pattern->index[iptr_ik];
362 if (coloring[k]<color) Paso_BlockOps_SMV_N(n_block, &x[n_block*i], &A_p->val[block_size*iptr_ik], &x[n_block*k]);
363 }
364 Paso_BlockOps_Solve_N(n_block, &x[n_block*i], &diag[block_size*i], &pivot[n_block*i], &x[n_block*i]);
365 }
366 }
367 }
368 } /* end of coloring loop */
369
370
371 /* backward substitution */
372 for (color=(num_colors)-2 ;color>-1;--color) { /* Note: color=(num_colors)-1 is not required */
373 if (n_block==1) {
374 #pragma omp parallel for schedule(static) private(mm, i,iptr_ik,k)
375 for (i = 0; i < n; ++i) {
376 if (coloring[i]==color) {
377 mm=ptr_main[i];
378 Paso_BlockOps_MV_1(&x[i], &A_p->val[mm], &x[i]);
379 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
380 k=A_p->pattern->index[iptr_ik];
381 if (coloring[k]>color) Paso_BlockOps_SMV_1(&x[i], &A_p->val[iptr_ik], &x[k]);
382 }
383 Paso_BlockOps_MV_1(&x[i], &diag[i], &x[i]);
384 }
385 }
386 } else if (n_block==2) {
387 #pragma omp parallel for schedule(static) private(mm, i,iptr_ik,k)
388 for (i = 0; i < n; ++i) {
389 if (coloring[i]==color) {
390 mm=ptr_main[i];
391 Paso_BlockOps_MV_2(&x[2*i], &A_p->val[4*mm], &x[2*i]);
392 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
393 k=A_p->pattern->index[iptr_ik];
394 if (coloring[k]>color) Paso_BlockOps_SMV_2(&x[2*i], &A_p->val[4*iptr_ik], &x[2*k]);
395 }
396 Paso_BlockOps_MV_2(&x[2*i], &diag[4*i], &x[2*i]);
397 }
398 }
399 } else if (n_block==3) {
400 #pragma omp parallel for schedule(static) private(mm, i,iptr_ik,k)
401 for (i = 0; i < n; ++i) {
402 if (coloring[i]==color) {
403 mm=ptr_main[i];
404 Paso_BlockOps_MV_3(&x[3*i], &A_p->val[9*mm], &x[3*i]);
405 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
406 k=A_p->pattern->index[iptr_ik];
407 if (coloring[k]>color) Paso_BlockOps_SMV_3(&x[3*i], &A_p->val[9*iptr_ik], &x[3*k]);
408 }
409 Paso_BlockOps_MV_3(&x[3*i], &diag[9*i], &x[3*i]);
410 }
411 }
412 } else {
413 #pragma omp parallel for schedule(static) private(mm, i,iptr_ik,k)
414 for (i = 0; i < n; ++i) {
415 if (coloring[i]==color) {
416 mm=ptr_main[i];
417 Paso_BlockOps_MV_N( n_block, &x[n_block*i], &A_p->val[block_size*mm], &x[n_block*i] );
418 for (iptr_ik=A_p->pattern->ptr[i];iptr_ik<A_p->pattern->ptr[i+1]; ++iptr_ik) {
419 k=A_p->pattern->index[iptr_ik];
420 if (coloring[k]>color) Paso_BlockOps_SMV_N(n_block, &x[n_block*i], &A_p->val[block_size*iptr_ik], &x[n_block*k]);
421 }
422 Paso_BlockOps_Solve_N(n_block, &x[n_block*i], &diag[block_size*i], &pivot[n_block*i], &x[n_block*i]);
423 }
424 }
425 }
426 }
427 return;
428 }
429
430
431

  ViewVC Help
Powered by ViewVC 1.1.26