Date: 2022-03-04T17:39:12+00:00

Actual source code: ex5.c

      2: static char help[] = "Solves two linear systems in parallel with KSP.  The code\n\
      3: illustrates repeated solution of linear systems with the same preconditioner\n\
      4: method but different matrices (having the same nonzero structure).  The code\n\
      5: also uses multiple profiling stages.  Input arguments are\n\
      6:   -m <size> : problem size\n\
      7:   -mat_nonsym : use nonsymmetric matrix (default is symmetric)\n\n";

      9: /*T
     10:    Concepts: KSP^repeatedly solving linear systems;
     11:    Concepts: PetscLog^profiling multiple stages of code;
     12:    Processors: n
     13: T*/

     15: /*
     16:   Include "petscksp.h" so that we can use KSP solvers.  Note that this file
     17:   automatically includes:
     18:      petscsys.h       - base PETSc routines   petscvec.h - vectors
     19:      petscmat.h - matrices
     20:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
     21:      petscviewer.h - viewers               petscpc.h  - preconditioners
     22: */
     23: #include <petscksp.h>

     25: int main(int argc,char **args)
     26: {
     27:   KSP            ksp;              /* linear solver context */
     28:   Mat            C;                /* matrix */
     29:   Vec            x,u,b;            /* approx solution, RHS, exact solution */
     30:   PetscReal      norm;             /* norm of solution error */
     31:   PetscScalar    v,none = -1.0;
     32:   PetscInt       Ii,J,ldim,low,high,iglobal,Istart,Iend;
     34:   PetscInt       i,j,m = 3,n = 2,its;
     35:   PetscMPIInt    size,rank;
     36:   PetscBool      mat_nonsymmetric = PETSC_FALSE;
     37:   PetscBool      testnewC = PETSC_FALSE,testscaledMat=PETSC_FALSE;
     38: #if defined(PETSC_USE_LOG)
     39:   PetscLogStage stages[2];
     40: #endif

     42:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
     43:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
     44:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     45:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
     46:   n    = 2*size;

     48:   /*
     49:      Set flag if we are doing a nonsymmetric problem; the default is symmetric.
     50:   */
     51:   PetscOptionsGetBool(NULL,NULL,"-mat_nonsym",&mat_nonsymmetric,NULL);

     53:   PetscOptionsGetBool(NULL,NULL,"-test_scaledMat",&testscaledMat,NULL);

     55:   /*
     56:      Register two stages for separate profiling of the two linear solves.
     57:      Use the runtime option -log_view for a printout of performance
     58:      statistics at the program's conlusion.
     59:   */
     60:   PetscLogStageRegister("Original Solve",&stages[0]);
     61:   PetscLogStageRegister("Second Solve",&stages[1]);

     63:   /* -------------- Stage 0: Solve Original System ---------------------- */
     64:   /*
     65:      Indicate to PETSc profiling that we're beginning the first stage
     66:   */
     67:   PetscLogStagePush(stages[0]);

     69:   /*
     70:      Create parallel matrix, specifying only its global dimensions.
     71:      When using MatCreate(), the matrix format can be specified at
     72:      runtime. Also, the parallel partitioning of the matrix is
     73:      determined by PETSc at runtime.
     74:   */
     75:   MatCreate(PETSC_COMM_WORLD,&C);
     76:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
     77:   MatSetFromOptions(C);
     78:   MatSetUp(C);

     80:   /*
     81:      Currently, all PETSc parallel matrix formats are partitioned by
     82:      contiguous chunks of rows across the processors.  Determine which
     83:      rows of the matrix are locally owned.
     84:   */
     85:   MatGetOwnershipRange(C,&Istart,&Iend);

     87:   /*
     88:      Set matrix entries matrix in parallel.
     89:       - Each processor needs to insert only elements that it owns
     90:         locally (but any non-local elements will be sent to the
     91:         appropriate processor during matrix assembly).
     92:       - Always specify global row and columns of matrix entries.
     93:   */
     94:   for (Ii=Istart; Ii<Iend; Ii++) {
     95:     v = -1.0; i = Ii/n; j = Ii - i*n;
     96:     if (i>0)   {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
     97:     if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
     98:     if (j>0)   {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
     99:     if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
    100:     v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,ADD_VALUES);
    101:   }

    103:   /*
    104:      Make the matrix nonsymmetric if desired
    105:   */
    106:   if (mat_nonsymmetric) {
    107:     for (Ii=Istart; Ii<Iend; Ii++) {
    108:       v = -1.5; i = Ii/n;
    109:       if (i>1)   {J = Ii-n-1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
    110:     }
    111:   } else {
    112:     MatSetOption(C,MAT_SYMMETRIC,PETSC_TRUE);
    113:     MatSetOption(C,MAT_SYMMETRY_ETERNAL,PETSC_TRUE);
    114:   }

    116:   /*
    117:      Assemble matrix, using the 2-step process:
    118:        MatAssemblyBegin(), MatAssemblyEnd()
    119:      Computations can be done while messages are in transition
    120:      by placing code between these two statements.
    121:   */
    122:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
    123:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

    125:   /*
    126:      Create parallel vectors.
    127:       - When using VecSetSizes(), we specify only the vector's global
    128:         dimension; the parallel partitioning is determined at runtime.
    129:       - Note: We form 1 vector from scratch and then duplicate as needed.
    130:   */
    131:   VecCreate(PETSC_COMM_WORLD,&u);
    132:   VecSetSizes(u,PETSC_DECIDE,m*n);
    133:   VecSetFromOptions(u);
    134:   VecDuplicate(u,&b);
    135:   VecDuplicate(b,&x);

    137:   /*
    138:      Currently, all parallel PETSc vectors are partitioned by
    139:      contiguous chunks across the processors.  Determine which
    140:      range of entries are locally owned.
    141:   */
    142:   VecGetOwnershipRange(x,&low,&high);

    144:   /*
    145:     Set elements within the exact solution vector in parallel.
    146:      - Each processor needs to insert only elements that it owns
    147:        locally (but any non-local entries will be sent to the
    148:        appropriate processor during vector assembly).
    149:      - Always specify global locations of vector entries.
    150:   */
    151:   VecGetLocalSize(x,&ldim);
    152:   for (i=0; i<ldim; i++) {
    153:     iglobal = i + low;
    154:     v       = (PetscScalar)(i + 100*rank);
    155:     VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);
    156:   }

    158:   /*
    159:      Assemble vector, using the 2-step process:
    160:        VecAssemblyBegin(), VecAssemblyEnd()
    161:      Computations can be done while messages are in transition,
    162:      by placing code between these two statements.
    163:   */
    164:   VecAssemblyBegin(u);
    165:   VecAssemblyEnd(u);

    167:   /*
    168:      Compute right-hand-side vector
    169:   */
    170:   MatMult(C,u,b);

    172:   /*
    173:     Create linear solver context
    174:   */
    175:   KSPCreate(PETSC_COMM_WORLD,&ksp);

    177:   /*
    178:      Set operators. Here the matrix that defines the linear system
    179:      also serves as the preconditioning matrix.
    180:   */
    181:   KSPSetOperators(ksp,C,C);

    183:   /*
    184:      Set runtime options (e.g., -ksp_type <type> -pc_type <type>)
    185:   */
    186:   KSPSetFromOptions(ksp);

    188:   /*
    189:      Solve linear system.  Here we explicitly call KSPSetUp() for more
    190:      detailed performance monitoring of certain preconditioners, such
    191:      as ICC and ILU.  This call is optional, as KSPSetUp() will
    192:      automatically be called within KSPSolve() if it hasn't been
    193:      called already.
    194:   */
    195:   KSPSetUp(ksp);
    196:   KSPSolve(ksp,b,x);

    198:   /*
    199:      Check the error
    200:   */
    201:   VecAXPY(x,none,u);
    202:   VecNorm(x,NORM_2,&norm);
    203:   KSPGetIterationNumber(ksp,&its);
    204:   if (!testscaledMat || norm > 1.e-7) {
    205:     PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations %D\n",(double)norm,its);
    206:   }

    208:   /* -------------- Stage 1: Solve Second System ---------------------- */
    209:   /*
    210:      Solve another linear system with the same method.  We reuse the KSP
    211:      context, matrix and vector data structures, and hence save the
    212:      overhead of creating new ones.

    214:      Indicate to PETSc profiling that we're concluding the first
    215:      stage with PetscLogStagePop(), and beginning the second stage with
    216:      PetscLogStagePush().
    217:   */
    218:   PetscLogStagePop();
    219:   PetscLogStagePush(stages[1]);

    221:   /*
    222:      Initialize all matrix entries to zero.  MatZeroEntries() retains the
    223:      nonzero structure of the matrix for sparse formats.
    224:   */
    225:   MatZeroEntries(C);

    227:   /*
    228:      Assemble matrix again.  Note that we retain the same matrix data
    229:      structure and the same nonzero pattern; we just change the values
    230:      of the matrix entries.
    231:   */
    232:   for (i=0; i<m; i++) {
    233:     for (j=2*rank; j<2*rank+2; j++) {
    234:       v = -1.0;  Ii = j + n*i;
    235:       if (i>0)   {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
    236:       if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
    237:       if (j>0)   {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
    238:       if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
    239:       v = 6.0; MatSetValues(C,1,&Ii,1,&Ii,&v,ADD_VALUES);
    240:     }
    241:   }
    242:   if (mat_nonsymmetric) {
    243:     for (Ii=Istart; Ii<Iend; Ii++) {
    244:       v = -1.5; i = Ii/n;
    245:       if (i>1)   {J = Ii-n-1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
    246:     }
    247:   }
    248:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
    249:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

    251:   if (testscaledMat) {
    252:     PetscRandom    rctx;

    254:     /* Scale a(0,0) and a(M-1,M-1) */
    255:     if (rank == 0) {
    256:       v = 6.0*0.00001; Ii = 0; J = 0;
    257:       MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);
    258:     } else if (rank == size -1) {
    259:       v = 6.0*0.00001; Ii = m*n-1; J = m*n-1;
    260:       MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);
    261:     }
    262:     MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
    263:     MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

    265:     /* Compute a new right-hand-side vector */
    266:     VecDestroy(&u);
    267:     VecCreate(PETSC_COMM_WORLD,&u);
    268:     VecSetSizes(u,PETSC_DECIDE,m*n);
    269:     VecSetFromOptions(u);

    271:     PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
    272:     PetscRandomSetFromOptions(rctx);
    273:     VecSetRandom(u,rctx);
    274:     PetscRandomDestroy(&rctx);
    275:     VecAssemblyBegin(u);
    276:     VecAssemblyEnd(u);
    277:   }

    279:   PetscOptionsGetBool(NULL,NULL,"-test_newMat",&testnewC,NULL);
    280:   if (testnewC) {
    281:     /*
    282:      User may use a new matrix C with same nonzero pattern, e.g.
    283:       ./ex5 -ksp_monitor -mat_type sbaij -pc_type cholesky -pc_factor_mat_solver_type mumps -test_newMat
    284:     */
    285:     Mat Ctmp;
    286:     MatDuplicate(C,MAT_COPY_VALUES,&Ctmp);
    287:     MatDestroy(&C);
    288:     MatDuplicate(Ctmp,MAT_COPY_VALUES,&C);
    289:     MatDestroy(&Ctmp);
    290:   }

    292:   MatMult(C,u,b);

    294:   /*
    295:      Set operators. Here the matrix that defines the linear system
    296:      also serves as the preconditioning matrix.
    297:   */
    298:   KSPSetOperators(ksp,C,C);

    300:   /*
    301:      Solve linear system
    302:   */
    303:   KSPSetUp(ksp);
    304:   KSPSolve(ksp,b,x);

    306:   /*
    307:      Check the error
    308:   */
    309:   VecAXPY(x,none,u);
    310:   VecNorm(x,NORM_2,&norm);
    311:   KSPGetIterationNumber(ksp,&its);
    312:   if (!testscaledMat || norm > 1.e-7) {
    313:     PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations %D\n",(double)norm,its);
    314:   }

    316:   /*
    317:      Free work space.  All PETSc objects should be destroyed when they
    318:      are no longer needed.
    319:   */
    320:   KSPDestroy(&ksp);
    321:   VecDestroy(&u);
    322:   VecDestroy(&x);
    323:   VecDestroy(&b);
    324:   MatDestroy(&C);

    326:   /*
    327:      Indicate to PETSc profiling that we're concluding the second stage
    328:   */
    329:   PetscLogStagePop();

    331:   PetscFinalize();
    332:   return ierr;
    333: }

    335: /*TEST

    337:    test:
    338:       args: -pc_type jacobi -ksp_monitor_short -ksp_gmres_cgs_refinement_type refine_always

    340:    test:
    341:       suffix: 2
    342:       nsize: 2
    343:       args: -pc_type jacobi -ksp_monitor_short -ksp_gmres_cgs_refinement_type refine_always -ksp_rtol .000001

    345:    test:
    346:       suffix: 5
    347:       nsize: 2
    348:       args: -ksp_gmres_cgs_refinement_type refine_always -ksp_monitor draw::draw_lg -ksp_monitor_true_residual draw::draw_lg

    350:    test:
    351:       suffix: asm
    352:       nsize: 4
    353:       args: -pc_type asm

    355:    test:
    356:       suffix: asm_baij
    357:       nsize: 4
    358:       args: -pc_type asm -mat_type baij
    359:       output_file: output/ex5_asm.out

    361:    test:
    362:       suffix: redundant_0
    363:       args: -m 1000 -pc_type redundant -pc_redundant_number 1 -redundant_ksp_type gmres -redundant_pc_type jacobi

    365:    test:
    366:       suffix: redundant_1
    367:       nsize: 5
    368:       args: -pc_type redundant -pc_redundant_number 1 -redundant_ksp_type gmres -redundant_pc_type jacobi

    370:    test:
    371:       suffix: redundant_2
    372:       nsize: 5
    373:       args: -pc_type redundant -pc_redundant_number 3 -redundant_ksp_type gmres -redundant_pc_type jacobi

    375:    test:
    376:       suffix: redundant_3
    377:       nsize: 5
    378:       args: -pc_type redundant -pc_redundant_number 5 -redundant_ksp_type gmres -redundant_pc_type jacobi

    380:    test:
    381:       suffix: redundant_4
    382:       nsize: 5
    383:       args: -pc_type redundant -pc_redundant_number 3 -redundant_ksp_type gmres -redundant_pc_type jacobi -psubcomm_type interlaced

    385:    test:
    386:       suffix: superlu_dist
    387:       nsize: 15
    388:       requires: superlu_dist
    389:       args: -pc_type lu -pc_factor_mat_solver_type superlu_dist -mat_superlu_dist_equil false -m 150 -mat_superlu_dist_r 3 -mat_superlu_dist_c 5 -test_scaledMat

    391:    test:
    392:       suffix: superlu_dist_2
    393:       nsize: 15
    394:       requires: superlu_dist
    395:       args: -pc_type lu -pc_factor_mat_solver_type superlu_dist -mat_superlu_dist_equil false -m 150 -mat_superlu_dist_r 3 -mat_superlu_dist_c 5 -test_scaledMat -mat_superlu_dist_fact SamePattern_SameRowPerm
    396:       output_file: output/ex5_superlu_dist.out

    398:    test:
    399:       suffix: superlu_dist_3
    400:       nsize: 15
    401:       requires: superlu_dist
    402:       args: -pc_type lu -pc_factor_mat_solver_type superlu_dist -mat_superlu_dist_equil false -m 500 -mat_superlu_dist_r 3 -mat_superlu_dist_c 5 -test_scaledMat -mat_superlu_dist_fact DOFACT
    403:       output_file: output/ex5_superlu_dist.out

    405:    test:
    406:       suffix: superlu_dist_0
    407:       nsize: 1
    408:       requires: superlu_dist
    409:       args: -pc_type lu -pc_factor_mat_solver_type superlu_dist -test_scaledMat
    410:       output_file: output/ex5_superlu_dist.out

    412: TEST*/