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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3303 - (show annotations)
Mon Oct 25 04:33:31 2010 UTC (8 years, 10 months ago) by gross
File MIME type: text/plain
File size: 12148 byte(s)
more clean up work on the AMG
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: SystemMatrix */
18
19 /**************************************************************/
20
21 /* Author: Lutz Gross, l.gross@uq.edu.au */
22
23 /**************************************************************/
24
25 #include "SystemMatrix.h"
26 #include "Preconditioner.h"
27
28 /**************************************************************/
29
30 /* allocates a SystemMatrix of type type using the given matrix pattern
31 Values are initialized by zero.
32 if patternIsUnrolled and type & MATRIX_FORMAT_BLK1, it is assumed that the pattern is allready unrolled to match the requested block size
33 and offsets otherwise unrolling and offset adjustment will be performed.
34 */
35
36 Paso_SystemMatrix* Paso_SystemMatrix_alloc(Paso_SystemMatrixType type,Paso_SystemMatrixPattern *pattern, int row_block_size, int col_block_size,
37 const bool_t patternIsUnrolled) {
38
39 Paso_SystemMatrix*out=NULL;
40 dim_t n_norm,i;
41 Paso_SystemMatrixType pattern_format_out;
42 bool_t unroll=FALSE;
43
44 pattern_format_out= (type & MATRIX_FORMAT_OFFSET1)? PATTERN_FORMAT_OFFSET1: PATTERN_FORMAT_DEFAULT;
45 Esys_resetError();
46 if (patternIsUnrolled) {
47 if ( ! XNOR(type & MATRIX_FORMAT_OFFSET1, pattern->type & PATTERN_FORMAT_OFFSET1) ) {
48 Esys_setError(TYPE_ERROR,"Paso_SystemMatrix_alloc: requested offset and pattern offset does not match.");
49 return NULL;
50 }
51 }
52 /* do we need to apply unrolling ? */
53 unroll
54 /* we don't like non-square blocks */
55 = (row_block_size!=col_block_size)
56 /* or any block size bigger than 3 */
57 || (col_block_size>3)
58 /* or if lock size one requested and the block size is not 1 */
59 || ((type & MATRIX_FORMAT_BLK1) && (col_block_size>1) )
60 /* or the offsets are wrong */
61 || ((type & MATRIX_FORMAT_OFFSET1) != ( pattern->type & PATTERN_FORMAT_OFFSET1));
62
63 out=MEMALLOC(1,Paso_SystemMatrix);
64 if (! Esys_checkPtr(out)) {
65 out->type=type;
66 out->pattern=NULL;
67 out->row_distribution=NULL;
68 out->col_distribution=NULL;
69 out->mpi_info=Esys_MPIInfo_getReference(pattern->mpi_info);
70 out->row_coupler=NULL;
71 out->col_coupler=NULL;
72 out->mainBlock=NULL;
73 out->row_coupleBlock=NULL;
74 out->col_coupleBlock=NULL;
75 out->normalizer_is_valid=FALSE;
76 out->normalizer=NULL;
77 out->solver_package=PASO_PASO;
78 out->solver_p=NULL;
79 out->trilinos_data=NULL;
80 out->reference_counter=1;
81 out->logical_row_block_size=row_block_size;
82 out->logical_col_block_size=col_block_size;
83
84
85 if (type & MATRIX_FORMAT_CSC) {
86 if (unroll) {
87 if (patternIsUnrolled) {
88 out->pattern=Paso_SystemMatrixPattern_getReference(pattern);
89 } else {
90 out->pattern=Paso_SystemMatrixPattern_unrollBlocks(pattern,pattern_format_out,col_block_size,row_block_size);
91 }
92 out->row_block_size=1;
93 out->col_block_size=1;
94 } else {
95 out->pattern=Paso_SystemMatrixPattern_unrollBlocks(pattern,pattern_format_out,1,1);
96 out->row_block_size=row_block_size;
97 out->col_block_size=col_block_size;
98 }
99 if (Esys_noError()) {
100 out->row_distribution=Paso_Distribution_getReference(out->pattern->input_distribution);
101 out->col_distribution=Paso_Distribution_getReference(out->pattern->output_distribution);
102 }
103 } else {
104 if (unroll) {
105 if (patternIsUnrolled) {
106 out->pattern=Paso_SystemMatrixPattern_getReference(pattern);
107 } else {
108 out->pattern=Paso_SystemMatrixPattern_unrollBlocks(pattern,pattern_format_out,row_block_size,col_block_size);
109 }
110 out->row_block_size=1;
111 out->col_block_size=1;
112 } else {
113 out->pattern=Paso_SystemMatrixPattern_unrollBlocks(pattern,pattern_format_out,1,1);
114 out->row_block_size=row_block_size;
115 out->col_block_size=col_block_size;
116 }
117 if (Esys_noError()) {
118 out->row_distribution=Paso_Distribution_getReference(out->pattern->output_distribution);
119 out->col_distribution=Paso_Distribution_getReference(out->pattern->input_distribution);
120 }
121 }
122 if (Esys_noError()) {
123 out->block_size=out->row_block_size*out->col_block_size;
124 out->col_coupler=Paso_Coupler_alloc(pattern->col_connector,out->col_block_size);
125 out->row_coupler=Paso_Coupler_alloc(pattern->row_connector,out->row_block_size);
126 /* this should be bypassed if trilinos is used */
127 if (type & MATRIX_FORMAT_TRILINOS_CRS) {
128 #ifdef TRILINOS
129 out->trilinos_data=Paso_TRILINOS_alloc();
130 #endif
131 } else {
132 out->solver_package=PASO_PASO;
133 out->mainBlock=Paso_SparseMatrix_alloc(type,out->pattern->mainPattern,row_block_size,col_block_size,TRUE);
134 out->col_coupleBlock=Paso_SparseMatrix_alloc(type,out->pattern->col_couplePattern,row_block_size,col_block_size,TRUE);
135 out->row_coupleBlock=Paso_SparseMatrix_alloc(type,out->pattern->row_couplePattern,row_block_size,col_block_size,TRUE);
136 if (Esys_noError()) {
137 /* allocate memory for matrix entries */
138 if (type & MATRIX_FORMAT_CSC) {
139 n_norm = out->mainBlock->numCols * out->col_block_size;
140 } else {
141 n_norm = out->mainBlock->numRows * out->row_block_size;
142 }
143 out->normalizer=MEMALLOC(n_norm,double);
144 out->normalizer_is_valid=FALSE;
145 if (! Esys_checkPtr(out->normalizer)) {
146 #pragma omp parallel for private(i) schedule(static)
147 for (i=0;i<n_norm;++i) out->normalizer[i]=0.;
148 }
149 }
150 }
151 }
152 }
153 /* all done: */
154 if (! Esys_noError()) {
155 Paso_SystemMatrix_free(out);
156 return NULL;
157 } else {
158 return out;
159 }
160 }
161
162 /* returns a reference to Paso_SystemMatrix in */
163
164 Paso_SystemMatrix* Paso_SystemMatrix_getReference(Paso_SystemMatrix* in) {
165 if (in!=NULL) ++(in->reference_counter);
166 return in;
167 }
168
169 /* deallocates a SystemMatrix: */
170
171 void Paso_SystemMatrix_free(Paso_SystemMatrix* in) {
172 if (in!=NULL) {
173 in->reference_counter--;
174 if (in->reference_counter<=0) {
175 Paso_solve_free(in);
176 Paso_SystemMatrixPattern_free(in->pattern);
177 Paso_Distribution_free(in->row_distribution);
178 Paso_Distribution_free(in->col_distribution);
179 Esys_MPIInfo_free(in->mpi_info);
180 Paso_Coupler_free(in->row_coupler);
181 Paso_Coupler_free(in->col_coupler);
182 Paso_SparseMatrix_free(in->mainBlock);
183 Paso_SparseMatrix_free(in->col_coupleBlock);
184 Paso_SparseMatrix_free(in->row_coupleBlock);
185 MEMFREE(in->normalizer);
186 Paso_solve_free(in);
187 #ifdef TRILINOS
188 Paso_TRILINOS_free(in->trilinos_data);
189 #endif
190 MEMFREE(in);
191 #ifdef Paso_TRACE
192 printf("Paso_SystemMatrix_free: system matrix as been deallocated.\n");
193 #endif
194 }
195 }
196 }
197 void Paso_SystemMatrix_startCollect(Paso_SystemMatrix* A,const double* in)
198 {
199 Paso_SystemMatrix_startColCollect(A,in);
200 }
201 double* Paso_SystemMatrix_finishCollect(Paso_SystemMatrix* A)
202 {
203 return Paso_SystemMatrix_finishColCollect(A);
204 }
205
206 void Paso_SystemMatrix_startColCollect(Paso_SystemMatrix* A,const double* in)
207 {
208 Paso_Coupler_startCollect(A->col_coupler, in);
209 }
210 double* Paso_SystemMatrix_finishColCollect(Paso_SystemMatrix* A)
211 {
212 Paso_Coupler_finishCollect(A->col_coupler);
213 return A->col_coupler->recv_buffer;
214 }
215 void Paso_SystemMatrix_startRowCollect(Paso_SystemMatrix* A,const double* in)
216 {
217 Paso_Coupler_startCollect(A->row_coupler, in);
218 }
219 double* Paso_SystemMatrix_finishRowCollect(Paso_SystemMatrix* A)
220 {
221 Paso_Coupler_finishCollect(A->row_coupler);
222 return A->row_coupler->recv_buffer;
223 }
224
225 dim_t Paso_SystemMatrix_getTotalNumRows(const Paso_SystemMatrix* A){
226 return A->mainBlock->numRows * A->row_block_size;
227 }
228
229 dim_t Paso_SystemMatrix_getTotalNumCols(const Paso_SystemMatrix* A){
230 return A->mainBlock->numCols * A->col_block_size;
231 }
232 dim_t Paso_SystemMatrix_getGlobalNumRows(Paso_SystemMatrix* A) {
233 if (A->type & MATRIX_FORMAT_CSC) {
234 return Paso_Distribution_getGlobalNumComponents(A->pattern->input_distribution);
235 } else {
236 return Paso_Distribution_getGlobalNumComponents(A->pattern->output_distribution);
237 }
238 }
239 dim_t Paso_SystemMatrix_getGlobalNumCols(Paso_SystemMatrix* A) {
240 if (A->type & MATRIX_FORMAT_CSC) {
241 return Paso_Distribution_getGlobalNumComponents(A->pattern->output_distribution);
242 } else {
243 return Paso_Distribution_getGlobalNumComponents(A->pattern->input_distribution);
244 }
245
246 }
247 dim_t Paso_SystemMatrix_getNumOutput(Paso_SystemMatrix* A) {
248 return Paso_SystemMatrixPattern_getNumOutput(A->pattern);
249 }
250
251 index_t* Paso_SystemMatrix_borrowMainDiagonalPointer(Paso_SystemMatrix * A_p)
252 {
253 index_t* out=NULL;
254 int fail=0;
255 out=Paso_SparseMatrix_borrowMainDiagonalPointer(A_p->mainBlock);
256 if (out==NULL) fail=1;
257 #ifdef ESYS_MPI
258 {
259 int fail_loc = fail;
260 MPI_Allreduce(&fail_loc, &fail, 1, MPI_INT, MPI_MAX, A_p->mpi_info->comm);
261 }
262 #endif
263 if (fail>0) Esys_setError(VALUE_ERROR, "Paso_SystemMatrix_borrowMainDiagonalPointer: no main diagonal");
264 return out;
265 }
266
267 void Paso_SystemMatrix_makeZeroRowSums(Paso_SystemMatrix * A_p, double* left_over)
268 {
269 index_t ir, ib, irow;
270 register double rtmp1, rtmp2;
271 const dim_t n = Paso_SystemMatrixPattern_getNumOutput(A_p->pattern);
272 const dim_t nblk = A_p->block_size;
273 const dim_t blk = A_p->row_block_size;
274 const index_t* main_ptr=Paso_SystemMatrix_borrowMainDiagonalPointer(A_p);
275
276
277 Paso_SystemMatrix_rowSum(A_p, left_over); /* left_over now hold the row sum */
278
279 #pragma omp parallel for private(ir,ib, rtmp1, rtmp2) schedule(static)
280 for (ir=0;ir< n;ir++) {
281 for (ib=0;ib<blk; ib++) {
282 irow=ib+blk*ir;
283 rtmp1=left_over[irow];
284 rtmp2=A_p->mainBlock->val[main_ptr[ir]*nblk+ib+blk*ib];
285 A_p->mainBlock->val[main_ptr[ir]*nblk+ib+blk*ib] = -rtmp1;
286 left_over[irow]=rtmp2+rtmp1;
287 }
288 }
289 }
290 void Paso_SystemMatrix_copyBlockFromMainDiagonal(Paso_SystemMatrix * A_p, double* out)
291 {
292 Paso_SparseMatrix_copyBlockFromMainDiagonal(A_p->mainBlock, out);
293 return;
294 }
295 void Paso_SystemMatrix_copyBlockToMainDiagonal(Paso_SystemMatrix * A_p, const double* in)
296 {
297 Paso_SparseMatrix_copyBlockToMainDiagonal(A_p->mainBlock, in);
298 return;
299 }
300 void Paso_SystemMatrix_copyFromMainDiagonal(Paso_SystemMatrix * A_p, double* out)
301 {
302 Paso_SparseMatrix_copyFromMainDiagonal(A_p->mainBlock, out);
303 return;
304 }
305 void Paso_SystemMatrix_copyToMainDiagonal(Paso_SystemMatrix * A_p, const double* in)
306 {
307 Paso_SparseMatrix_copyToMainDiagonal(A_p->mainBlock, in);
308 return;
309 }
310
311 void Paso_SystemMatrix_setPreconditioner(Paso_SystemMatrix* A,Paso_Options* options) {
312 if (A->solver_p==NULL) {
313 A->solver_p=Paso_Preconditioner_alloc(A,options);
314 }
315 }
316
317 /* applies the preconditioner */
318 /* has to be called within a parallel reqion */
319 /* barrier synchronization is performed before the evaluation to make sure that the input vector is available */
320 void Paso_SystemMatrix_solvePreconditioner(Paso_SystemMatrix* A,double* x,double* b){
321 Paso_Preconditioner* prec=(Paso_Preconditioner*) A->solver_p;
322 Paso_Preconditioner_solve(prec, A,x,b);
323 }
324 void Paso_SystemMatrix_freePreconditioner(Paso_SystemMatrix* A) {
325 Paso_Preconditioner* prec=NULL;
326 if (A!=NULL) {
327 prec=(Paso_Preconditioner*) A->solver_p;
328 Paso_Preconditioner_free(prec);
329 A->solver_p=NULL;
330 }
331 }

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26