Actual source code: ex145.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Tests LU, Cholesky factorization and MatMatSolve() for an Elemental dense matrix.\n\n";

  4: #include <petscmat.h>

  8: int main(int argc,char **argv)
  9: {
 10:   Mat            A,F,B,X,C,Aher,G;
 11:   Vec            b,x,c,d,e;
 13:   PetscInt       m = 5,n,p,i,j,nrows,ncols;
 14:   PetscScalar    *v,*barray,rval;
 15:   PetscReal      norm,tol=1.e-12;
 16:   PetscMPIInt    size,rank;
 17:   PetscRandom    rand;
 18:   const PetscInt *rows,*cols;
 19:   IS             isrows,iscols;
 20:   PetscBool      mats_view=PETSC_FALSE;
 21:   MatFactorInfo  finfo;

 23:   PetscInitialize(&argc,&argv,(char*) 0,help);
 24:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 25:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

 27:   PetscRandomCreate(PETSC_COMM_WORLD,&rand);
 28:   PetscRandomSetFromOptions(rand);

 30:   /* Get local dimensions of matrices */
 31:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 32:   n    = m;
 33:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 34:   p    = m/2;
 35:   PetscOptionsGetInt(NULL,NULL,"-p",&p,NULL);
 36:   PetscOptionsHasName(NULL,NULL,"-mats_view",&mats_view);

 38:   /* Create matrix A */
 39:   PetscPrintf(PETSC_COMM_WORLD," Create Elemental matrix A\n");
 40:   MatCreate(PETSC_COMM_WORLD,&A);
 41:   MatSetSizes(A,m,n,PETSC_DECIDE,PETSC_DECIDE);
 42:   MatSetType(A,MATELEMENTAL);
 43:   MatSetFromOptions(A);
 44:   MatSetUp(A);
 45:   /* Set local matrix entries */
 46:   MatGetOwnershipIS(A,&isrows,&iscols);
 47:   ISGetLocalSize(isrows,&nrows);
 48:   ISGetIndices(isrows,&rows);
 49:   ISGetLocalSize(iscols,&ncols);
 50:   ISGetIndices(iscols,&cols);
 51:   PetscMalloc1(nrows*ncols,&v);
 52:   for (i=0; i<nrows; i++) {
 53:     for (j=0; j<ncols; j++) {
 54:       PetscRandomGetValue(rand,&rval);
 55:       v[i*ncols+j] = rval;
 56:     }
 57:   }
 58:   MatSetValues(A,nrows,rows,ncols,cols,v,INSERT_VALUES);
 59:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 60:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 61:   ISRestoreIndices(isrows,&rows);
 62:   ISRestoreIndices(iscols,&cols);
 63:   ISDestroy(&isrows);
 64:   ISDestroy(&iscols);
 65:   PetscFree(v);
 66:   if (mats_view) {
 67:     PetscPrintf(PETSC_COMM_WORLD, "A: nrows %d, m %d; ncols %d, n %d\n",nrows,m,ncols,n);
 68:     MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 69:   }

 71:   /* Create rhs matrix B */
 72:   PetscPrintf(PETSC_COMM_WORLD," Create rhs matrix B\n");
 73:   MatCreate(PETSC_COMM_WORLD,&B);
 74:   MatSetSizes(B,m,p,PETSC_DECIDE,PETSC_DECIDE);
 75:   MatSetType(B,MATELEMENTAL);
 76:   MatSetFromOptions(B);
 77:   MatSetUp(B);
 78:   MatGetOwnershipIS(B,&isrows,&iscols);
 79:   ISGetLocalSize(isrows,&nrows);
 80:   ISGetIndices(isrows,&rows);
 81:   ISGetLocalSize(iscols,&ncols);
 82:   ISGetIndices(iscols,&cols);
 83:   PetscMalloc1(nrows*ncols,&v);
 84:   for (i=0; i<nrows; i++) {
 85:     for (j=0; j<ncols; j++) {
 86:       PetscRandomGetValue(rand,&rval);
 87:       v[i*ncols+j] = rval;
 88:     }
 89:   }
 90:   MatSetValues(B,nrows,rows,ncols,cols,v,INSERT_VALUES);
 91:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
 92:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
 93:   ISRestoreIndices(isrows,&rows);
 94:   ISRestoreIndices(iscols,&cols);
 95:   ISDestroy(&isrows);
 96:   ISDestroy(&iscols);
 97:   PetscFree(v);
 98:   if (mats_view) {
 99:     PetscPrintf(PETSC_COMM_WORLD, "B: nrows %d, m %d; ncols %d, p %d\n",nrows,m,ncols,p);
100:     MatView(B,PETSC_VIEWER_STDOUT_WORLD);
101:   }

103:   /* Create rhs vector b and solution x (same size as b) */
104:   VecCreate(PETSC_COMM_WORLD,&b);
105:   VecSetSizes(b,m,PETSC_DECIDE);
106:   VecSetFromOptions(b);
107:   VecGetArray(b,&barray);
108:   for (j=0; j<m; j++) {
109:     PetscRandomGetValue(rand,&rval);
110:     barray[j] = rval;
111:   }
112:   VecRestoreArray(b,&barray);
113:   VecAssemblyBegin(b);
114:   VecAssemblyEnd(b);
115:   if (mats_view) {
116:     PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d] b: m %d\n",rank,m);
117:     PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
118:     VecView(b,PETSC_VIEWER_STDOUT_WORLD);
119:   }
120:   VecDuplicate(b,&x);

122:   /* Create matrix X - same size as B */
123:   PetscPrintf(PETSC_COMM_WORLD," Create solution matrix X\n");
124:   MatCreate(PETSC_COMM_WORLD,&X);
125:   MatSetSizes(X,m,p,PETSC_DECIDE,PETSC_DECIDE);
126:   MatSetType(X,MATELEMENTAL);
127:   MatSetFromOptions(X);
128:   MatSetUp(X);
129:   MatAssemblyBegin(X,MAT_FINAL_ASSEMBLY);
130:   MatAssemblyEnd(X,MAT_FINAL_ASSEMBLY);

132:   /* Cholesky factorization */
133:   /*------------------------*/
134:   PetscPrintf(PETSC_COMM_WORLD," Create Elemental matrix Aher\n");
135:   MatHermitianTranspose(A,MAT_INITIAL_MATRIX,&Aher);
136:   MatAXPY(Aher,1.0,A,SAME_NONZERO_PATTERN); /* Aher = A + A^T */
137:   if(!rank) { /* add 100.0 to diagonals of Aher to make it spd */

139:     /* TODO: Replace this with a call to El::ShiftDiagonal( A, 100. ),
140:              or at least pre-allocate the right amount of space */
141:     PetscInt M,N;
142:     MatGetSize(Aher,&M,&N);
143:     for (i=0; i<M; i++) {
144:       rval = 100.0;
145:       MatSetValues(Aher,1,&i,1,&i,&rval,ADD_VALUES);
146:     }
147:   }
148:   MatAssemblyBegin(Aher,MAT_FINAL_ASSEMBLY);
149:   MatAssemblyEnd(Aher,MAT_FINAL_ASSEMBLY);
150:   if (mats_view) {
151:     PetscPrintf(PETSC_COMM_WORLD, "Aher:\n");
152:     MatView(Aher,PETSC_VIEWER_STDOUT_WORLD);
153:   }

155:   /* Cholesky factorization */
156:   /*------------------------*/
157:   PetscPrintf(PETSC_COMM_WORLD," Test Cholesky Solver \n");
158:   /* In-place Cholesky */
159:   /* Create matrix factor G, then copy Aher to G */
160:   MatCreate(PETSC_COMM_WORLD,&G);
161:   MatSetSizes(G,m,n,PETSC_DECIDE,PETSC_DECIDE);
162:   MatSetType(G,MATELEMENTAL);
163:   MatSetFromOptions(G);
164:   MatSetUp(G);
165:   MatAssemblyBegin(G,MAT_FINAL_ASSEMBLY);
166:   MatAssemblyEnd(G,MAT_FINAL_ASSEMBLY);
167:   MatCopy(Aher,G,SAME_NONZERO_PATTERN);

169:   /* Only G = U^T * U is implemented for now */
170:   MatCholeskyFactor(G,0,0);
171:   if (mats_view) {
172:     PetscPrintf(PETSC_COMM_WORLD, "Cholesky Factor G:\n");
173:     MatView(G,PETSC_VIEWER_STDOUT_WORLD);
174:   }

176:   /* Solve U^T * U x = b and U^T * U X = B */
177:   MatSolve(G,b,x);
178:   MatMatSolve(G,B,X);
179:   MatDestroy(&G);

181:   /* Out-place Cholesky */
182:   MatGetFactor(Aher,MATSOLVERELEMENTAL,MAT_FACTOR_CHOLESKY,&G);
183:   MatCholeskyFactorSymbolic(G,Aher,0,&finfo);
184:   MatCholeskyFactorNumeric(G,Aher,&finfo);
185:   if (mats_view) {
186:     MatView(G,PETSC_VIEWER_STDOUT_WORLD);
187:   }
188:   MatSolve(G,b,x);
189:   MatMatSolve(G,B,X);
190:   MatDestroy(&G);

192:   /* Check norm(Aher*x - b) */
193:   VecCreate(PETSC_COMM_WORLD,&c);
194:   VecSetSizes(c,m,PETSC_DECIDE);
195:   VecSetFromOptions(c);
196:   MatMult(Aher,x,c);
197:   VecAXPY(c,-1.0,b);
198:   VecNorm(c,NORM_1,&norm);
199:   if (norm > tol) {
200:     PetscPrintf(PETSC_COMM_WORLD,"Warning: |Aher*x - b| for Cholesky %g\n",(double)norm);
201:   }

203:   /* Check norm(Aher*X - B) */
204:   MatMatMult(Aher,X,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&C);
205:   MatAXPY(C,-1.0,B,SAME_NONZERO_PATTERN);
206:   MatNorm(C,NORM_1,&norm);
207:   if (norm > tol) {
208:     PetscPrintf(PETSC_COMM_WORLD,"Warning: |Aher*X - B| for Cholesky %g\n",(double)norm);
209:   }

211:   /* LU factorization */
212:   /*------------------*/
213:   PetscPrintf(PETSC_COMM_WORLD," Test LU Solver \n");
214:   /* In-place LU */
215:   /* Create matrix factor F, then copy A to F */
216:   MatCreate(PETSC_COMM_WORLD,&F);
217:   MatSetSizes(F,m,n,PETSC_DECIDE,PETSC_DECIDE);
218:   MatSetType(F,MATELEMENTAL);
219:   MatSetFromOptions(F);
220:   MatSetUp(F);
221:   MatAssemblyBegin(F,MAT_FINAL_ASSEMBLY);
222:   MatAssemblyEnd(F,MAT_FINAL_ASSEMBLY);
223:   MatCopy(A,F,SAME_NONZERO_PATTERN);
224:   /* Create vector d to test MatSolveAdd() */
225:   VecDuplicate(x,&d);
226:   VecCopy(x,d);

228:   /* PF=LU or F=LU factorization - perms is ignored by Elemental;
229:      set finfo.dtcol !0 or 0 to enable/disable partial pivoting */
230:   finfo.dtcol = 0.1;
231:   MatLUFactor(F,0,0,&finfo);

233:   /* Solve LUX = PB or LUX = B */
234:   MatSolveAdd(F,b,d,x);
235:   MatMatSolve(F,B,X);
236:   MatDestroy(&F);

238:   /* Check norm(A*X - B) */
239:   VecCreate(PETSC_COMM_WORLD,&e);
240:   VecSetSizes(e,m,PETSC_DECIDE);
241:   VecSetFromOptions(e);
242:   MatMult(A,x,c);
243:   MatMult(A,d,e);
244:   VecAXPY(c,-1.0,e);
245:   VecAXPY(c,-1.0,b);
246:   VecNorm(c,NORM_1,&norm);
247:   if (norm > tol) {
248:     PetscPrintf(PETSC_COMM_WORLD,"Warning: |A*x - b| for LU %g\n",(double)norm);
249:   }
250:   MatMatMult(A,X,MAT_REUSE_MATRIX,PETSC_DEFAULT,&C);
251:   MatAXPY(C,-1.0,B,SAME_NONZERO_PATTERN);
252:   MatNorm(C,NORM_1,&norm);
253:   if (norm > tol) {
254:     PetscPrintf(PETSC_COMM_WORLD,"Warning: |A*X - B| for LU %g\n",(double)norm);
255:   }

257:   /* Out-place LU */
258:   MatGetFactor(A,MATSOLVERELEMENTAL,MAT_FACTOR_LU,&F);
259:   MatLUFactorSymbolic(F,A,0,0,&finfo);
260:   MatLUFactorNumeric(F,A,&finfo);
261:   if (mats_view) {
262:     MatView(F,PETSC_VIEWER_STDOUT_WORLD);
263:   }
264:   MatSolve(F,b,x);
265:   MatMatSolve(F,B,X);
266:   MatDestroy(&F);

268:   /* Free space */
269:   MatDestroy(&A);
270:   MatDestroy(&Aher);
271:   MatDestroy(&B);
272:   MatDestroy(&C);
273:   MatDestroy(&X);
274:   VecDestroy(&b);
275:   VecDestroy(&c);
276:   VecDestroy(&d);
277:   VecDestroy(&e);
278:   VecDestroy(&x);
279:   PetscRandomDestroy(&rand);
280:   PetscFinalize();
281:   return 0;
282: }