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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3445 - (show annotations)
Wed Jan 19 06:02:15 2011 UTC (8 years, 5 months ago) by gross
File MIME type: text/plain
File size: 13516 byte(s)
more work towards MPI 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)? MATRIX_FORMAT_OFFSET1: MATRIX_FORMAT_DEFAULT;
45 Esys_resetError();
46 if (patternIsUnrolled) {
47 if ( ! XNOR(type & MATRIX_FORMAT_OFFSET1, pattern->type & MATRIX_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 #ifndef USE_LAPACK
57 /* or any block size bigger than 3 */
58 || (col_block_size>3)
59 # endif
60 /* or if lock size one requested and the block size is not 1 */
61 || ((type & MATRIX_FORMAT_BLK1) && (col_block_size>1) )
62 /* or the offsets are wrong */
63 || ((type & MATRIX_FORMAT_OFFSET1) != ( pattern->type & MATRIX_FORMAT_OFFSET1));
64
65 out=MEMALLOC(1,Paso_SystemMatrix);
66 if (! Esys_checkPtr(out)) {
67 out->type=type;
68 out->pattern=NULL;
69 out->row_distribution=NULL;
70 out->col_distribution=NULL;
71 out->mpi_info=Esys_MPIInfo_getReference(pattern->mpi_info);
72 out->row_coupler=NULL;
73 out->col_coupler=NULL;
74 out->mainBlock=NULL;
75 out->row_coupleBlock=NULL;
76 out->col_coupleBlock=NULL;
77 out->is_balanced=FALSE;
78 out->balance_vector=NULL;
79 out->solver_package=PASO_PASO;
80 out->solver_p=NULL;
81 out->trilinos_data=NULL;
82 out->reference_counter=1;
83 out->logical_row_block_size=row_block_size;
84 out->logical_col_block_size=col_block_size;
85
86
87 if (type & MATRIX_FORMAT_CSC) {
88 if (unroll) {
89 if (patternIsUnrolled) {
90 out->pattern=Paso_SystemMatrixPattern_getReference(pattern);
91 } else {
92 out->pattern=Paso_SystemMatrixPattern_unrollBlocks(pattern,pattern_format_out,col_block_size,row_block_size);
93 }
94 out->row_block_size=1;
95 out->col_block_size=1;
96 } else {
97 out->pattern=Paso_SystemMatrixPattern_unrollBlocks(pattern,pattern_format_out,1,1);
98 out->row_block_size=row_block_size;
99 out->col_block_size=col_block_size;
100 }
101 if (Esys_noError()) {
102 out->row_distribution=Paso_Distribution_getReference(out->pattern->input_distribution);
103 out->col_distribution=Paso_Distribution_getReference(out->pattern->output_distribution);
104 }
105 } else {
106 if (unroll) {
107 if (patternIsUnrolled) {
108 out->pattern=Paso_SystemMatrixPattern_getReference(pattern);
109 } else {
110 out->pattern=Paso_SystemMatrixPattern_unrollBlocks(pattern,pattern_format_out,row_block_size,col_block_size);
111 }
112 out->row_block_size=1;
113 out->col_block_size=1;
114 } else {
115 out->pattern=Paso_SystemMatrixPattern_unrollBlocks(pattern,pattern_format_out,1,1);
116 out->row_block_size=row_block_size;
117 out->col_block_size=col_block_size;
118 }
119 if (Esys_noError()) {
120 out->row_distribution=Paso_Distribution_getReference(out->pattern->output_distribution);
121 out->col_distribution=Paso_Distribution_getReference(out->pattern->input_distribution);
122 }
123 }
124 if (Esys_noError()) {
125 out->block_size=out->row_block_size*out->col_block_size;
126 out->col_coupler=Paso_Coupler_alloc(pattern->col_connector,out->col_block_size);
127 out->row_coupler=Paso_Coupler_alloc(pattern->row_connector,out->row_block_size);
128 /* this should be bypassed if trilinos is used */
129 if (type & MATRIX_FORMAT_TRILINOS_CRS) {
130 #ifdef TRILINOS
131 out->trilinos_data=Paso_TRILINOS_alloc();
132 #endif
133 } else {
134 out->solver_package=PASO_PASO;
135 out->mainBlock=Paso_SparseMatrix_alloc(type,out->pattern->mainPattern,row_block_size,col_block_size,TRUE);
136 out->col_coupleBlock=Paso_SparseMatrix_alloc(type,out->pattern->col_couplePattern,row_block_size,col_block_size,TRUE);
137 out->row_coupleBlock=Paso_SparseMatrix_alloc(type,out->pattern->row_couplePattern,row_block_size,col_block_size,TRUE);
138 if (Esys_noError()) {
139 /* allocate memory for matrix entries */
140 n_norm = MAX(out->mainBlock->numCols * out->col_block_size, out->mainBlock->numRows * out->row_block_size);
141 out->balance_vector=MEMALLOC(n_norm,double);
142 out->is_balanced=FALSE;
143 if (! Esys_checkPtr(out->balance_vector)) {
144 #pragma omp parallel for private(i) schedule(static)
145 for (i=0;i<n_norm;++i) out->balance_vector[i]=1.;
146 }
147 }
148 }
149 }
150 }
151 /* all done: */
152 if (! Esys_noError()) {
153 Paso_SystemMatrix_free(out);
154 return NULL;
155 } else {
156 return out;
157 }
158 }
159
160 /* returns a reference to Paso_SystemMatrix in */
161
162 Paso_SystemMatrix* Paso_SystemMatrix_getReference(Paso_SystemMatrix* in) {
163 if (in!=NULL) ++(in->reference_counter);
164 return in;
165 }
166
167 /* deallocates a SystemMatrix: */
168
169 void Paso_SystemMatrix_free(Paso_SystemMatrix* in) {
170 if (in!=NULL) {
171 in->reference_counter--;
172 if (in->reference_counter<=0) {
173 Paso_solve_free(in);
174 Paso_SystemMatrixPattern_free(in->pattern);
175 Paso_Distribution_free(in->row_distribution);
176 Paso_Distribution_free(in->col_distribution);
177 Esys_MPIInfo_free(in->mpi_info);
178 Paso_Coupler_free(in->row_coupler);
179 Paso_Coupler_free(in->col_coupler);
180 Paso_SparseMatrix_free(in->mainBlock);
181 Paso_SparseMatrix_free(in->col_coupleBlock);
182 Paso_SparseMatrix_free(in->row_coupleBlock);
183 MEMFREE(in->balance_vector);
184 #ifdef TRILINOS
185 Paso_TRILINOS_free(in->trilinos_data);
186 #endif
187 MEMFREE(in);
188 #ifdef Paso_TRACE
189 printf("Paso_SystemMatrix_free: system matrix as been deallocated.\n");
190 #endif
191 }
192 }
193 }
194 void Paso_SystemMatrix_startCollect(Paso_SystemMatrix* A,const double* in)
195 {
196 Paso_SystemMatrix_startColCollect(A,in);
197 }
198 double* Paso_SystemMatrix_finishCollect(Paso_SystemMatrix* A)
199 {
200 return Paso_SystemMatrix_finishColCollect(A);
201 }
202
203 void Paso_SystemMatrix_startColCollect(Paso_SystemMatrix* A,const double* in)
204 {
205 Paso_Coupler_startCollect(A->col_coupler, in);
206 }
207 double* Paso_SystemMatrix_finishColCollect(Paso_SystemMatrix* A)
208 {
209 Paso_Coupler_finishCollect(A->col_coupler);
210 return A->col_coupler->recv_buffer;
211 }
212 void Paso_SystemMatrix_startRowCollect(Paso_SystemMatrix* A,const double* in)
213 {
214 Paso_Coupler_startCollect(A->row_coupler, in);
215 }
216 double* Paso_SystemMatrix_finishRowCollect(Paso_SystemMatrix* A)
217 {
218 Paso_Coupler_finishCollect(A->row_coupler);
219 return A->row_coupler->recv_buffer;
220 }
221
222
223 dim_t Paso_SystemMatrix_getNumRows(const Paso_SystemMatrix* A){
224 return A->mainBlock->numRows;
225 }
226
227 dim_t Paso_SystemMatrix_getNumCols(const Paso_SystemMatrix* A){
228 return A->mainBlock->numCols;
229 }
230
231 dim_t Paso_SystemMatrix_getTotalNumRows(const Paso_SystemMatrix* A){
232 return Paso_SystemMatrix_getNumRows(A) * A->row_block_size;
233 }
234 dim_t Paso_SystemMatrix_getTotalNumCols(const Paso_SystemMatrix* A){
235 return Paso_SystemMatrix_getNumCols(A) * A->col_block_size;
236 }
237
238 dim_t Paso_SystemMatrix_getRowOverlap(const Paso_SystemMatrix* A)
239 {
240 return Paso_Coupler_getNumOverlapComponents(A->row_coupler);
241 }
242 dim_t Paso_SystemMatrix_getColOverlap(const Paso_SystemMatrix* A)
243 {
244 return Paso_Coupler_getNumOverlapComponents(A->col_coupler);
245 }
246
247
248
249 dim_t Paso_SystemMatrix_getGlobalNumRows(const Paso_SystemMatrix* A) {
250 if (A->type & MATRIX_FORMAT_CSC) {
251 return Paso_Distribution_getGlobalNumComponents(A->pattern->input_distribution);
252 } else {
253 return Paso_Distribution_getGlobalNumComponents(A->pattern->output_distribution);
254 }
255 }
256 dim_t Paso_SystemMatrix_getGlobalNumCols(const Paso_SystemMatrix* A) {
257 if (A->type & MATRIX_FORMAT_CSC) {
258 return Paso_Distribution_getGlobalNumComponents(A->pattern->output_distribution);
259 } else {
260 return Paso_Distribution_getGlobalNumComponents(A->pattern->input_distribution);
261 }
262
263 }
264 dim_t Paso_SystemMatrix_getGlobalTotalNumRows(const Paso_SystemMatrix* A){
265 return Paso_SystemMatrix_getGlobalNumRows(A) * A->row_block_size;
266 }
267
268 dim_t Paso_SystemMatrix_getGlobalTotalNumCols(const Paso_SystemMatrix* A){
269 return Paso_SystemMatrix_getGlobalNumCols(A) * A->col_block_size;
270 }
271 double Paso_SystemMatrix_getGlobalSize(const Paso_SystemMatrix*A) {
272 double global_size=0;
273 if (A!=NULL) {
274 double my_size=Paso_SparseMatrix_getSize(A->mainBlock)+Paso_SparseMatrix_getSize(A->col_coupleBlock);
275 if (A->mpi_info->size > 1) {
276
277 #ifdef ESYS_MPI
278 MPI_Allreduce(&my_size,&global_size, 1, MPI_DOUBLE, MPI_SUM, A->mpi_info->comm);
279 #else
280 global_size=my_size;
281 #endif
282
283 } else {
284 global_size=my_size;
285 }
286 }
287 return global_size;
288 }
289 double Paso_SystemMatrix_getSparsity(const Paso_SystemMatrix*A) {
290 return Paso_SystemMatrix_getGlobalSize(A)/(DBLE(Paso_SystemMatrix_getGlobalTotalNumRows(A))*DBLE(Paso_SystemMatrix_getGlobalTotalNumCols(A)));
291 }
292
293 dim_t Paso_SystemMatrix_getNumOutput(Paso_SystemMatrix* A) {
294 return Paso_SystemMatrixPattern_getNumOutput(A->pattern);
295 }
296
297 index_t* Paso_SystemMatrix_borrowMainDiagonalPointer(Paso_SystemMatrix * A_p)
298 {
299 index_t* out=NULL;
300 int fail=0;
301 out=Paso_SparseMatrix_borrowMainDiagonalPointer(A_p->mainBlock);
302 if (out==NULL) fail=1;
303 #ifdef ESYS_MPI
304 {
305 int fail_loc = fail;
306 MPI_Allreduce(&fail_loc, &fail, 1, MPI_INT, MPI_MAX, A_p->mpi_info->comm);
307 }
308 #endif
309 if (fail>0) Esys_setError(VALUE_ERROR, "Paso_SystemMatrix_borrowMainDiagonalPointer: no main diagonal");
310 return out;
311 }
312
313 void Paso_SystemMatrix_makeZeroRowSums(Paso_SystemMatrix * A_p, double* left_over)
314 {
315 index_t ir, ib, irow;
316 register double rtmp1, rtmp2;
317 const dim_t n = Paso_SystemMatrixPattern_getNumOutput(A_p->pattern);
318 const dim_t nblk = A_p->block_size;
319 const dim_t blk = A_p->row_block_size;
320 const index_t* main_ptr=Paso_SystemMatrix_borrowMainDiagonalPointer(A_p);
321
322
323 Paso_SystemMatrix_rowSum(A_p, left_over); /* left_over now hold the row sum */
324
325 #pragma omp parallel for private(ir,ib, rtmp1, rtmp2) schedule(static)
326 for (ir=0;ir< n;ir++) {
327 for (ib=0;ib<blk; ib++) {
328 irow=ib+blk*ir;
329 rtmp1=left_over[irow];
330 rtmp2=A_p->mainBlock->val[main_ptr[ir]*nblk+ib+blk*ib];
331 A_p->mainBlock->val[main_ptr[ir]*nblk+ib+blk*ib] = -rtmp1;
332 left_over[irow]=rtmp2+rtmp1;
333 }
334 }
335 }
336 void Paso_SystemMatrix_copyBlockFromMainDiagonal(Paso_SystemMatrix * A_p, double* out)
337 {
338 Paso_SparseMatrix_copyBlockFromMainDiagonal(A_p->mainBlock, out);
339 return;
340 }
341 void Paso_SystemMatrix_copyBlockToMainDiagonal(Paso_SystemMatrix * A_p, const double* in)
342 {
343 Paso_SparseMatrix_copyBlockToMainDiagonal(A_p->mainBlock, in);
344 return;
345 }
346 void Paso_SystemMatrix_copyFromMainDiagonal(Paso_SystemMatrix * A_p, double* out)
347 {
348 Paso_SparseMatrix_copyFromMainDiagonal(A_p->mainBlock, out);
349 return;
350 }
351 void Paso_SystemMatrix_copyToMainDiagonal(Paso_SystemMatrix * A_p, const double* in)
352 {
353 Paso_SparseMatrix_copyToMainDiagonal(A_p->mainBlock, in);
354 return;
355 }
356
357 void Paso_SystemMatrix_setPreconditioner(Paso_SystemMatrix* A,Paso_Options* options) {
358 if (A->solver_p==NULL) {
359 A->solver_p=Paso_Preconditioner_alloc(A,options);
360 }
361 }
362
363 /* applies the preconditioner */
364 /* has to be called within a parallel reqion */
365 /* barrier synchronization is performed before the evaluation to make sure that the input vector is available */
366 void Paso_SystemMatrix_solvePreconditioner(Paso_SystemMatrix* A,double* x,double* b){
367 Paso_Preconditioner* prec=(Paso_Preconditioner*) A->solver_p;
368 Paso_Preconditioner_solve(prec, A,x,b);
369 }
370 void Paso_SystemMatrix_freePreconditioner(Paso_SystemMatrix* A) {
371 Paso_Preconditioner* prec=NULL;
372 if (A!=NULL) {
373 prec=(Paso_Preconditioner*) A->solver_p;
374 Paso_Preconditioner_free(prec);
375 A->solver_p=NULL;
376 }
377 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26