Actual source code: ex26.c

  1: static char help[] ="Solves Laplacian with multigrid. Tests block API for PCMG\n\
  2:   -mx <xg>, where <xg> = number of grid points in the x-direction\n\
  3:   -my <yg>, where <yg> = number of grid points in the y-direction\n\
  4:   -Nx <npx>, where <npx> = number of processors in the x-direction\n\
  5:   -Ny <npy>, where <npy> = number of processors in the y-direction\n\n";

  7: /*  Modified from ~src/ksp/tests/ex19.c. Used for testing ML 6.2 interface.

  9:     This problem is modeled by
 10:     the partial differential equation

 12:             -Laplacian u  = g,  0 < x,y < 1,

 14:     with boundary conditions

 16:              u = 0  for  x = 0, x = 1, y = 0, y = 1.

 18:     A finite difference approximation with the usual 5-point stencil
 19:     is used to discretize the boundary value problem to obtain a linear
 20:     system of equations.

 22:     Usage: ./ex26 -ksp_monitor_short -pc_type ml
 23:            -mg_coarse_ksp_max_it 10
 24:            -mg_levels_1_ksp_max_it 10 -mg_levels_2_ksp_max_it 10
 25:            -mg_fine_ksp_max_it 10
 26: */

 28: #include <petscksp.h>
 29: #include <petscdm.h>
 30: #include <petscdmda.h>

 32: /* User-defined application contexts */
 33: typedef struct {
 34:   PetscInt mx,my;              /* number grid points in x and y direction */
 35:   Vec      localX,localF;      /* local vectors with ghost region */
 36:   DM       da;
 37:   Vec      x,b,r;              /* global vectors */
 38:   Mat      J;                  /* Jacobian on grid */
 39:   Mat      A,P,R;
 40:   KSP      ksp;
 41: } GridCtx;

 43: static PetscErrorCode FormJacobian_Grid(GridCtx*,Mat);

 45: int main(int argc,char **argv)
 46: {
 47:   PetscInt       i,its,Nx=PETSC_DECIDE,Ny=PETSC_DECIDE,nlocal,nrhs = 1;
 48:   PetscScalar    one = 1.0;
 49:   Mat            A,B,X;
 50:   GridCtx        fine_ctx;
 51:   KSP            ksp;
 52:   PetscBool      Brand = PETSC_FALSE,flg;

 54:   PetscInitialize(&argc,&argv,(char*)0,help);
 55:   /* set up discretization matrix for fine grid */
 56:   fine_ctx.mx = 9;
 57:   fine_ctx.my = 9;
 58:   PetscOptionsGetInt(NULL,NULL,"-mx",&fine_ctx.mx,NULL);
 59:   PetscOptionsGetInt(NULL,NULL,"-my",&fine_ctx.my,NULL);
 60:   PetscOptionsGetInt(NULL,NULL,"-nrhs",&nrhs,NULL);
 61:   PetscOptionsGetInt(NULL,NULL,"-Nx",&Nx,NULL);
 62:   PetscOptionsGetInt(NULL,NULL,"-Ny",&Ny,NULL);
 63:   PetscOptionsGetBool(NULL,NULL,"-rand",&Brand,NULL);
 64:   PetscPrintf(PETSC_COMM_WORLD,"Fine grid size %D by %D\n",fine_ctx.mx,fine_ctx.my);

 66:   /* Set up distributed array for fine grid */
 67:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,fine_ctx.mx,fine_ctx.my,Nx,Ny,1,1,NULL,NULL,&fine_ctx.da);
 68:   DMSetFromOptions(fine_ctx.da);
 69:   DMSetUp(fine_ctx.da);
 70:   DMCreateGlobalVector(fine_ctx.da,&fine_ctx.x);
 71:   VecDuplicate(fine_ctx.x,&fine_ctx.b);
 72:   VecGetLocalSize(fine_ctx.x,&nlocal);
 73:   DMCreateLocalVector(fine_ctx.da,&fine_ctx.localX);
 74:   VecDuplicate(fine_ctx.localX,&fine_ctx.localF);
 75:   DMCreateMatrix(fine_ctx.da,&A);
 76:   FormJacobian_Grid(&fine_ctx,A);

 78:   /* create linear solver */
 79:   KSPCreate(PETSC_COMM_WORLD,&ksp);
 80:   KSPSetDM(ksp,fine_ctx.da);
 81:   KSPSetDMActive(ksp,PETSC_FALSE);

 83:   /* set values for rhs vector */
 84:   VecSet(fine_ctx.b,one);

 86:   /* set options, then solve system */
 87:   KSPSetFromOptions(ksp); /* calls PCSetFromOptions_ML if 'pc_type=ml' */
 88:   KSPSetOperators(ksp,A,A);
 89:   KSPSolve(ksp,fine_ctx.b,fine_ctx.x);
 90:   VecViewFromOptions(fine_ctx.x,NULL,"-debug");
 91:   KSPGetIterationNumber(ksp,&its);
 92:   KSPGetIterationNumber(ksp,&its);
 93:   PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %D\n",its);

 95:   /* test multiple right-hand side */
 96:   MatCreateDense(PETSC_COMM_WORLD,nlocal,PETSC_DECIDE,fine_ctx.mx*fine_ctx.my,nrhs,NULL,&B);
 97:   MatSetOptionsPrefix(B,"rhs_");
 98:   MatSetFromOptions(B);
 99:   MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&X);
100:   if (Brand) {
101:     MatSetRandom(B,NULL);
102:   } else {
103:     PetscScalar *b;

105:     MatDenseGetArrayWrite(B,&b);
106:     for (i=0;i<nlocal*nrhs;i++) b[i] = 1.0;
107:     MatDenseRestoreArrayWrite(B,&b);
108:     MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
109:     MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
110:   }
111:   KSPMatSolve(ksp,B,X);
112:   MatViewFromOptions(X,NULL,"-debug");

114:   PetscObjectTypeCompare((PetscObject)ksp,KSPPREONLY,&flg);
115:   if ((flg || nrhs == 1) && !Brand) {
116:     PetscInt          n;
117:     const PetscScalar *xx,*XX;

119:     VecGetArrayRead(fine_ctx.x,&xx);
120:     MatDenseGetArrayRead(X,&XX);
121:     for (n=0;n<nrhs;n++) {
122:       for (i=0;i<nlocal;i++) {
123:         if (PetscAbsScalar(xx[i] - XX[nlocal*n + i]) > PETSC_SMALL) {
124:           PetscPrintf(PETSC_COMM_SELF,"[%d] Error local solve %D, entry %D -> %g + i %g != %g + i %g\n",PetscGlobalRank,n,i,(double)PetscRealPart(xx[i]),(double)PetscImaginaryPart(xx[i]),(double)PetscRealPart(XX[i]),(double)PetscImaginaryPart(XX[i]));
125:         }
126:       }
127:     }
128:     MatDenseRestoreArrayRead(X,&XX);
129:     VecRestoreArrayRead(fine_ctx.x,&xx);
130:   }

132:   /* free data structures */
133:   VecDestroy(&fine_ctx.x);
134:   VecDestroy(&fine_ctx.b);
135:   DMDestroy(&fine_ctx.da);
136:   VecDestroy(&fine_ctx.localX);
137:   VecDestroy(&fine_ctx.localF);
138:   MatDestroy(&A);
139:   MatDestroy(&B);
140:   MatDestroy(&X);
141:   KSPDestroy(&ksp);

143:   PetscFinalize();
144:   return 0;
145: }

147: PetscErrorCode FormJacobian_Grid(GridCtx *grid,Mat jac)
148: {
149:   PetscInt               i,j,row,mx,my,xs,ys,xm,ym,Xs,Ys,Xm,Ym,col[5];
150:   PetscInt               grow;
151:   const PetscInt         *ltog;
152:   PetscScalar            two = 2.0,one = 1.0,v[5],hx,hy,hxdhy,hydhx,value;
153:   ISLocalToGlobalMapping ltogm;

156:   mx    = grid->mx;            my = grid->my;
157:   hx    = one/(PetscReal)(mx-1);  hy = one/(PetscReal)(my-1);
158:   hxdhy = hx/hy;            hydhx = hy/hx;

160:   /* Get ghost points */
161:   DMDAGetCorners(grid->da,&xs,&ys,0,&xm,&ym,0);
162:   DMDAGetGhostCorners(grid->da,&Xs,&Ys,0,&Xm,&Ym,0);
163:   DMGetLocalToGlobalMapping(grid->da,&ltogm);
164:   ISLocalToGlobalMappingGetIndices(ltogm,&ltog);

166:   /* Evaluate Jacobian of function */
167:   for (j=ys; j<ys+ym; j++) {
168:     row = (j - Ys)*Xm + xs - Xs - 1;
169:     for (i=xs; i<xs+xm; i++) {
170:       row++;
171:       grow = ltog[row];
172:       if (i > 0 && i < mx-1 && j > 0 && j < my-1) {
173:         v[0] = -hxdhy; col[0] = ltog[row - Xm];
174:         v[1] = -hydhx; col[1] = ltog[row - 1];
175:         v[2] = two*(hydhx + hxdhy); col[2] = grow;
176:         v[3] = -hydhx; col[3] = ltog[row + 1];
177:         v[4] = -hxdhy; col[4] = ltog[row + Xm];
178:         MatSetValues(jac,1,&grow,5,col,v,INSERT_VALUES);
179:       } else if ((i > 0 && i < mx-1) || (j > 0 && j < my-1)) {
180:         value = .5*two*(hydhx + hxdhy);
181:         MatSetValues(jac,1,&grow,1,&grow,&value,INSERT_VALUES);
182:       } else {
183:         value = .25*two*(hydhx + hxdhy);
184:         MatSetValues(jac,1,&grow,1,&grow,&value,INSERT_VALUES);
185:       }
186:     }
187:   }
188:   ISLocalToGlobalMappingRestoreIndices(ltogm,&ltog);
189:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
190:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);
191:   return 0;
192: }

194: /*TEST

196:     test:
197:       args: -ksp_monitor_short

199:     test:
200:       suffix: 2
201:       args:  -ksp_monitor_short
202:       nsize: 3

204:     test:
205:       suffix: ml_1
206:       args:  -ksp_monitor_short -pc_type ml -mat_no_inode
207:       nsize: 3
208:       requires: ml

210:     test:
211:       suffix: ml_2
212:       args:  -ksp_monitor_short -pc_type ml -mat_no_inode -ksp_max_it 3
213:       nsize: 3
214:       requires: ml

216:     test:
217:       suffix: ml_3
218:       args:  -ksp_monitor_short -pc_type ml -mat_no_inode -pc_mg_type ADDITIVE -ksp_max_it 7
219:       nsize: 1
220:       requires: ml

222:     test:
223:       suffix: cycles
224:       nsize: {{1 2}}
225:       args: -ksp_view_final_residual -pc_type mg -mx 5 -my 5 -pc_mg_levels 3 -pc_mg_galerkin -ksp_monitor -mg_levels_ksp_type richardson -mg_levels_pc_type jacobi -pc_mg_type {{additive multiplicative full kaskade}separate output} -nrhs 1

227:     test:
228:       suffix: matcycles
229:       nsize: {{1 2}}
230:       args: -ksp_view_final_residual -ksp_type preonly -pc_type mg -mx 5 -my 5 -pc_mg_levels 3 -pc_mg_galerkin -ksp_monitor -mg_levels_ksp_type richardson -mg_levels_pc_type jacobi -pc_mg_type {{additive multiplicative full kaskade}separate output} -nrhs 7 -ksp_matsolve_batch_size {{4 7}separate output}

232:     test:
233:       requires: ml
234:       suffix: matcycles_ml
235:       nsize: {{1 2}}
236:       args: -ksp_view_final_residual -ksp_type preonly -pc_type ml -mx 5 -my 5 -ksp_monitor -mg_levels_ksp_type richardson -mg_levels_pc_type jacobi -pc_mg_type {{additive multiplicative full kaskade}separate output} -nrhs 7 -ksp_matsolve_batch_size {{4 7}separate output}

238:     testset:
239:       requires: hpddm
240:       args: -ksp_view_final_residual -ksp_type hpddm -pc_type mg -pc_mg_levels 3 -pc_mg_galerkin -mx 5 -my 5 -ksp_monitor -mg_levels_ksp_type richardson -mg_levels_pc_type jacobi -nrhs 7
241:       test:
242:         suffix: matcycles_hpddm_mg
243:         nsize: {{1 2}}
244:         args: -pc_mg_type {{additive multiplicative full kaskade}separate output} -ksp_matsolve_batch_size {{4 7}separate output}
245:       test:
246:         suffix: hpddm_mg_mixed_precision
247:         nsize: 2
248:         output_file: output/ex26_matcycles_hpddm_mg_pc_mg_type-multiplicative_ksp_matsolve_batch_size-4.out
249:         args: -ksp_matsolve_batch_size 4 -ksp_hpddm_precision {{single double}shared output}

251:     test:
252:       requires: hpddm
253:       nsize: {{1 2}}
254:       suffix: matcycles_hpddm_ilu
255:       args: -ksp_view_final_residual -ksp_type hpddm -pc_type redundant -redundant_pc_type ilu -mx 5 -my 5 -ksp_monitor -nrhs 7 -ksp_matsolve_batch_size {{4 7}separate output}

257: TEST*/