xref: /petsc/src/ksp/pc/impls/bjacobi/bjacobi.c (revision 4dbf25a8fa98e38799e7b47dcb2d8a9309975f41)
14b9ad928SBarry Smith /*
24b9ad928SBarry Smith    Defines a block Jacobi preconditioner.
34b9ad928SBarry Smith */
400e125f8SBarry Smith 
500e125f8SBarry Smith #include <../src/ksp/pc/impls/bjacobi/bjacobi.h> /*I "petscpc.h" I*/
64b9ad928SBarry Smith 
76849ba73SBarry Smith static PetscErrorCode PCSetUp_BJacobi_Singleblock(PC, Mat, Mat);
86849ba73SBarry Smith static PetscErrorCode PCSetUp_BJacobi_Multiblock(PC, Mat, Mat);
95a7e1895SHong Zhang static PetscErrorCode PCSetUp_BJacobi_Multiproc(PC);
104b9ad928SBarry Smith 
11d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_BJacobi(PC pc)
12d71ae5a4SJacob Faibussowitsch {
134b9ad928SBarry Smith   PC_BJacobi *jac = (PC_BJacobi *)pc->data;
144b9ad928SBarry Smith   Mat         mat = pc->mat, pmat = pc->pmat;
15976e8c5aSLisandro Dalcin   PetscBool   hasop;
1669a612a9SBarry Smith   PetscInt    N, M, start, i, sum, end;
1769a612a9SBarry Smith   PetscInt    bs, i_start = -1, i_end = -1;
1869a612a9SBarry Smith   PetscMPIInt rank, size;
194b9ad928SBarry Smith 
204b9ad928SBarry Smith   PetscFunctionBegin;
219566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
229566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
239566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(pc->pmat, &M, &N));
249566063dSJacob Faibussowitsch   PetscCall(MatGetBlockSize(pc->pmat, &bs));
254b9ad928SBarry Smith 
265a7e1895SHong Zhang   if (jac->n > 0 && jac->n < size) {
279566063dSJacob Faibussowitsch     PetscCall(PCSetUp_BJacobi_Multiproc(pc));
283ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
295a7e1895SHong Zhang   }
305a7e1895SHong Zhang 
31f1580f4eSBarry Smith   /*    Determines the number of blocks assigned to each processor */
324b9ad928SBarry Smith   /*   local block count  given */
334b9ad928SBarry Smith   if (jac->n_local > 0 && jac->n < 0) {
34462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(&jac->n_local, &jac->n, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)pc)));
354b9ad928SBarry Smith     if (jac->l_lens) { /* check that user set these correctly */
364b9ad928SBarry Smith       sum = 0;
374b9ad928SBarry Smith       for (i = 0; i < jac->n_local; i++) {
3808401ef6SPierre Jolivet         PetscCheck(jac->l_lens[i] / bs * bs == jac->l_lens[i], PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Mat blocksize doesn't match block Jacobi layout");
394b9ad928SBarry Smith         sum += jac->l_lens[i];
404b9ad928SBarry Smith       }
4108401ef6SPierre Jolivet       PetscCheck(sum == M, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Local lens set incorrectly");
424b9ad928SBarry Smith     } else {
439566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(jac->n_local, &jac->l_lens));
442fa5cd67SKarl Rupp       for (i = 0; i < jac->n_local; i++) jac->l_lens[i] = bs * ((M / bs) / jac->n_local + (((M / bs) % jac->n_local) > i));
454b9ad928SBarry Smith     }
464b9ad928SBarry Smith   } else if (jac->n > 0 && jac->n_local < 0) { /* global block count given */
474b9ad928SBarry Smith     /* global blocks given: determine which ones are local */
484b9ad928SBarry Smith     if (jac->g_lens) {
494b9ad928SBarry Smith       /* check if the g_lens is has valid entries */
504b9ad928SBarry Smith       for (i = 0; i < jac->n; i++) {
517827d75bSBarry Smith         PetscCheck(jac->g_lens[i], PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Zero block not allowed");
5208401ef6SPierre Jolivet         PetscCheck(jac->g_lens[i] / bs * bs == jac->g_lens[i], PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Mat blocksize doesn't match block Jacobi layout");
534b9ad928SBarry Smith       }
544b9ad928SBarry Smith       if (size == 1) {
554b9ad928SBarry Smith         jac->n_local = jac->n;
569566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(jac->n_local, &jac->l_lens));
579566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(jac->l_lens, jac->g_lens, jac->n_local));
584b9ad928SBarry Smith         /* check that user set these correctly */
594b9ad928SBarry Smith         sum = 0;
604b9ad928SBarry Smith         for (i = 0; i < jac->n_local; i++) sum += jac->l_lens[i];
6108401ef6SPierre Jolivet         PetscCheck(sum == M, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Global lens set incorrectly");
624b9ad928SBarry Smith       } else {
639566063dSJacob Faibussowitsch         PetscCall(MatGetOwnershipRange(pc->pmat, &start, &end));
64aaa8cc7dSPierre Jolivet         /* loop over blocks determining first one owned by me */
654b9ad928SBarry Smith         sum = 0;
664b9ad928SBarry Smith         for (i = 0; i < jac->n + 1; i++) {
679371c9d4SSatish Balay           if (sum == start) {
689371c9d4SSatish Balay             i_start = i;
699371c9d4SSatish Balay             goto start_1;
709371c9d4SSatish Balay           }
714b9ad928SBarry Smith           if (i < jac->n) sum += jac->g_lens[i];
724b9ad928SBarry Smith         }
73e7e72b3dSBarry Smith         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Block sizes used in PCBJacobiSetTotalBlocks()\nare not compatible with parallel matrix layout");
744b9ad928SBarry Smith       start_1:
754b9ad928SBarry Smith         for (i = i_start; i < jac->n + 1; i++) {
769371c9d4SSatish Balay           if (sum == end) {
779371c9d4SSatish Balay             i_end = i;
789371c9d4SSatish Balay             goto end_1;
799371c9d4SSatish Balay           }
804b9ad928SBarry Smith           if (i < jac->n) sum += jac->g_lens[i];
814b9ad928SBarry Smith         }
82e7e72b3dSBarry Smith         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Block sizes used in PCBJacobiSetTotalBlocks()\nare not compatible with parallel matrix layout");
834b9ad928SBarry Smith       end_1:
844b9ad928SBarry Smith         jac->n_local = i_end - i_start;
859566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(jac->n_local, &jac->l_lens));
869566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(jac->l_lens, jac->g_lens + i_start, jac->n_local));
874b9ad928SBarry Smith       }
884b9ad928SBarry Smith     } else { /* no global blocks given, determine then using default layout */
894b9ad928SBarry Smith       jac->n_local = jac->n / size + ((jac->n % size) > rank);
909566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(jac->n_local, &jac->l_lens));
914b9ad928SBarry Smith       for (i = 0; i < jac->n_local; i++) {
924b9ad928SBarry Smith         jac->l_lens[i] = ((M / bs) / jac->n_local + (((M / bs) % jac->n_local) > i)) * bs;
937827d75bSBarry Smith         PetscCheck(jac->l_lens[i], PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Too many blocks given");
944b9ad928SBarry Smith       }
954b9ad928SBarry Smith     }
964b9ad928SBarry Smith   } else if (jac->n < 0 && jac->n_local < 0) { /* no blocks given */
974b9ad928SBarry Smith     jac->n       = size;
984b9ad928SBarry Smith     jac->n_local = 1;
999566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(1, &jac->l_lens));
1004b9ad928SBarry Smith     jac->l_lens[0] = M;
1017271b979SBarry Smith   } else { /* jac->n > 0 && jac->n_local > 0 */
1027271b979SBarry Smith     if (!jac->l_lens) {
1039566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(jac->n_local, &jac->l_lens));
1042fa5cd67SKarl Rupp       for (i = 0; i < jac->n_local; i++) jac->l_lens[i] = bs * ((M / bs) / jac->n_local + (((M / bs) % jac->n_local) > i));
1057271b979SBarry Smith     }
1064b9ad928SBarry Smith   }
10708401ef6SPierre Jolivet   PetscCheck(jac->n_local >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of blocks is less than number of processors");
1084b9ad928SBarry Smith 
109f1580f4eSBarry Smith   /*    Determines mat and pmat */
1109566063dSJacob Faibussowitsch   PetscCall(MatHasOperation(pc->mat, MATOP_GET_DIAGONAL_BLOCK, &hasop));
111976e8c5aSLisandro Dalcin   if (!hasop && size == 1) {
1124b9ad928SBarry Smith     mat  = pc->mat;
1134b9ad928SBarry Smith     pmat = pc->pmat;
1144b9ad928SBarry Smith   } else {
11549517cdeSBarry Smith     if (pc->useAmat) {
11649517cdeSBarry Smith       /* use block from Amat matrix, not Pmat for local MatMult() */
1179566063dSJacob Faibussowitsch       PetscCall(MatGetDiagonalBlock(pc->mat, &mat));
1184b9ad928SBarry Smith     }
11949517cdeSBarry Smith     if (pc->pmat != pc->mat || !pc->useAmat) {
1209566063dSJacob Faibussowitsch       PetscCall(MatGetDiagonalBlock(pc->pmat, &pmat));
1212fa5cd67SKarl Rupp     } else pmat = mat;
1224b9ad928SBarry Smith   }
1234b9ad928SBarry Smith 
124f1580f4eSBarry Smith   /*
1254b9ad928SBarry Smith      Setup code depends on the number of blocks
1264b9ad928SBarry Smith   */
127cc1d6079SHong Zhang   if (jac->n_local == 1) {
1289566063dSJacob Faibussowitsch     PetscCall(PCSetUp_BJacobi_Singleblock(pc, mat, pmat));
1294b9ad928SBarry Smith   } else {
1309566063dSJacob Faibussowitsch     PetscCall(PCSetUp_BJacobi_Multiblock(pc, mat, pmat));
1314b9ad928SBarry Smith   }
1323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1334b9ad928SBarry Smith }
1344b9ad928SBarry Smith 
1354b9ad928SBarry Smith /* Default destroy, if it has never been setup */
136d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_BJacobi(PC pc)
137d71ae5a4SJacob Faibussowitsch {
1384b9ad928SBarry Smith   PC_BJacobi *jac = (PC_BJacobi *)pc->data;
1394b9ad928SBarry Smith 
1404b9ad928SBarry Smith   PetscFunctionBegin;
1419566063dSJacob Faibussowitsch   PetscCall(PetscFree(jac->g_lens));
1429566063dSJacob Faibussowitsch   PetscCall(PetscFree(jac->l_lens));
1432e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetSubKSP_C", NULL));
1442e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiSetTotalBlocks_C", NULL));
1452e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetTotalBlocks_C", NULL));
1462e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiSetLocalBlocks_C", NULL));
1472e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetLocalBlocks_C", NULL));
1489566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc->data));
1493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1504b9ad928SBarry Smith }
1514b9ad928SBarry Smith 
152ce78bad3SBarry Smith static PetscErrorCode PCSetFromOptions_BJacobi(PC pc, PetscOptionItems PetscOptionsObject)
153d71ae5a4SJacob Faibussowitsch {
1544b9ad928SBarry Smith   PC_BJacobi *jac = (PC_BJacobi *)pc->data;
155248bfaf0SJed Brown   PetscInt    blocks, i;
156ace3abfcSBarry Smith   PetscBool   flg;
1574b9ad928SBarry Smith 
1584b9ad928SBarry Smith   PetscFunctionBegin;
159d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "Block Jacobi options");
1609566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_bjacobi_blocks", "Total number of blocks", "PCBJacobiSetTotalBlocks", jac->n, &blocks, &flg));
1619566063dSJacob Faibussowitsch   if (flg) PetscCall(PCBJacobiSetTotalBlocks(pc, blocks, NULL));
1629566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_bjacobi_local_blocks", "Local number of blocks", "PCBJacobiSetLocalBlocks", jac->n_local, &blocks, &flg));
1639566063dSJacob Faibussowitsch   if (flg) PetscCall(PCBJacobiSetLocalBlocks(pc, blocks, NULL));
164248bfaf0SJed Brown   if (jac->ksp) {
165248bfaf0SJed Brown     /* The sub-KSP has already been set up (e.g., PCSetUp_BJacobi_Singleblock), but KSPSetFromOptions was not called
166248bfaf0SJed Brown      * unless we had already been called. */
16748a46eb9SPierre Jolivet     for (i = 0; i < jac->n_local; i++) PetscCall(KSPSetFromOptions(jac->ksp[i]));
168248bfaf0SJed Brown   }
169d0609cedSBarry Smith   PetscOptionsHeadEnd();
1703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1714b9ad928SBarry Smith }
1724b9ad928SBarry Smith 
1739804daf3SBarry Smith #include <petscdraw.h>
174d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_BJacobi(PC pc, PetscViewer viewer)
175d71ae5a4SJacob Faibussowitsch {
1764b9ad928SBarry Smith   PC_BJacobi           *jac   = (PC_BJacobi *)pc->data;
177cbe18068SHong Zhang   PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc *)jac->data;
17869a612a9SBarry Smith   PetscMPIInt           rank;
17969a612a9SBarry Smith   PetscInt              i;
180d9884438SBarry Smith   PetscBool             iascii, isstring, isdraw;
1814b9ad928SBarry Smith   PetscViewer           sviewer;
182020d6619SPierre Jolivet   PetscViewerFormat     format;
183020d6619SPierre Jolivet   const char           *prefix;
1844b9ad928SBarry Smith 
1854b9ad928SBarry Smith   PetscFunctionBegin;
1869566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
1879566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
1889566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
18932077d6dSBarry Smith   if (iascii) {
19048a46eb9SPierre Jolivet     if (pc->useAmat) PetscCall(PetscViewerASCIIPrintf(viewer, "  using Amat local matrix, number of blocks = %" PetscInt_FMT "\n", jac->n));
19163a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  number of blocks = %" PetscInt_FMT "\n", jac->n));
1929566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
1939566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer, &format));
194020d6619SPierre Jolivet     if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) {
1959566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "  Local solver information for first block is in the following KSP and PC objects on rank 0:\n"));
1969566063dSJacob Faibussowitsch       PetscCall(PCGetOptionsPrefix(pc, &prefix));
1979566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "  Use -%sksp_view ::ascii_info_detail to display information for all blocks\n", prefix ? prefix : ""));
198933995ecSHong Zhang       if (jac->ksp && !jac->psubcomm) {
1999566063dSJacob Faibussowitsch         PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
200dd400576SPatrick Sanan         if (rank == 0) {
201b4025f61SBarry Smith           PetscCall(PetscViewerASCIIPushTab(sviewer));
2029566063dSJacob Faibussowitsch           PetscCall(KSPView(jac->ksp[0], sviewer));
203b4025f61SBarry Smith           PetscCall(PetscViewerASCIIPopTab(sviewer));
2044b9ad928SBarry Smith         }
2059566063dSJacob Faibussowitsch         PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
206e4de9e1dSBarry Smith         /*  extra call needed because of the two calls to PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
2079566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPopSynchronized(viewer));
208e4fa1014SBarry Smith       } else if (mpjac && jac->ksp && mpjac->psubcomm) {
2099566063dSJacob Faibussowitsch         PetscCall(PetscViewerGetSubViewer(viewer, mpjac->psubcomm->child, &sviewer));
210e4fa1014SBarry Smith         if (!mpjac->psubcomm->color) {
211b4025f61SBarry Smith           PetscCall(PetscViewerASCIIPushTab(sviewer));
212f4f49eeaSPierre Jolivet           PetscCall(KSPView(*jac->ksp, sviewer));
213b4025f61SBarry Smith           PetscCall(PetscViewerASCIIPopTab(sviewer));
214cbe18068SHong Zhang         }
2159566063dSJacob Faibussowitsch         PetscCall(PetscViewerRestoreSubViewer(viewer, mpjac->psubcomm->child, &sviewer));
2169530cbd7SBarry Smith         /*  extra call needed because of the two calls to PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
2179566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPopSynchronized(viewer));
218e4de9e1dSBarry Smith       }
219e4de9e1dSBarry Smith     } else {
2204814766eSBarry Smith       PetscInt n_global;
221462c564dSBarry Smith       PetscCallMPI(MPIU_Allreduce(&jac->n_local, &n_global, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)pc)));
2229566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushSynchronized(viewer));
2239566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "  Local solver information for each block is in the following KSP and PC objects:\n"));
2249566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
2259566063dSJacob Faibussowitsch       PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
226b4025f61SBarry Smith       PetscCall(PetscViewerASCIIPrintf(sviewer, "[%d] number of local blocks = %" PetscInt_FMT ", first local block number = %" PetscInt_FMT "\n", rank, jac->n_local, jac->first_local));
227dd2fa690SBarry Smith       for (i = 0; i < jac->n_local; i++) {
228b4025f61SBarry Smith         PetscCall(PetscViewerASCIIPrintf(sviewer, "[%d] local block number %" PetscInt_FMT "\n", rank, i));
2299566063dSJacob Faibussowitsch         PetscCall(KSPView(jac->ksp[i], sviewer));
230b4025f61SBarry Smith         PetscCall(PetscViewerASCIIPrintf(sviewer, "- - - - - - - - - - - - - - - - - -\n"));
2314b9ad928SBarry Smith       }
2329566063dSJacob Faibussowitsch       PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
2339566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
2349566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopSynchronized(viewer));
2354b9ad928SBarry Smith     }
2364b9ad928SBarry Smith   } else if (isstring) {
23763a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerStringSPrintf(viewer, " blks=%" PetscInt_FMT, jac->n));
2389566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
2399566063dSJacob Faibussowitsch     if (jac->ksp) PetscCall(KSPView(jac->ksp[0], sviewer));
2409566063dSJacob Faibussowitsch     PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
241d9884438SBarry Smith   } else if (isdraw) {
242d9884438SBarry Smith     PetscDraw draw;
243d9884438SBarry Smith     char      str[25];
244d9884438SBarry Smith     PetscReal x, y, bottom, h;
245d9884438SBarry Smith 
2469566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
2479566063dSJacob Faibussowitsch     PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y));
24863a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(str, 25, "Number blocks %" PetscInt_FMT, jac->n));
2499566063dSJacob Faibussowitsch     PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_RED, PETSC_DRAW_BLACK, str, NULL, &h));
250d9884438SBarry Smith     bottom = y - h;
2519566063dSJacob Faibussowitsch     PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom));
252d9884438SBarry Smith     /* warning the communicator on viewer is different then on ksp in parallel */
2539566063dSJacob Faibussowitsch     if (jac->ksp) PetscCall(KSPView(jac->ksp[0], viewer));
2549566063dSJacob Faibussowitsch     PetscCall(PetscDrawPopCurrentPoint(draw));
2554b9ad928SBarry Smith   }
2563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2574b9ad928SBarry Smith }
2584b9ad928SBarry Smith 
259d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCBJacobiGetSubKSP_BJacobi(PC pc, PetscInt *n_local, PetscInt *first_local, KSP **ksp)
260d71ae5a4SJacob Faibussowitsch {
261feb237baSPierre Jolivet   PC_BJacobi *jac = (PC_BJacobi *)pc->data;
2624b9ad928SBarry Smith 
2634b9ad928SBarry Smith   PetscFunctionBegin;
26428b400f6SJacob Faibussowitsch   PetscCheck(pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call KSPSetUp() or PCSetUp() first");
2654b9ad928SBarry Smith 
2664b9ad928SBarry Smith   if (n_local) *n_local = jac->n_local;
2674b9ad928SBarry Smith   if (first_local) *first_local = jac->first_local;
268020d6619SPierre Jolivet   if (ksp) *ksp = jac->ksp;
2693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2704b9ad928SBarry Smith }
2714b9ad928SBarry Smith 
272f9663b93SPierre Jolivet static PetscErrorCode PCBJacobiSetTotalBlocks_BJacobi(PC pc, PetscInt blocks, const PetscInt *lens)
273d71ae5a4SJacob Faibussowitsch {
2744b9ad928SBarry Smith   PC_BJacobi *jac = (PC_BJacobi *)pc->data;
2754b9ad928SBarry Smith 
2764b9ad928SBarry Smith   PetscFunctionBegin;
2772472a847SBarry Smith   PetscCheck(pc->setupcalled <= 0 || jac->n == blocks, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Cannot alter number of blocks after PCSetUp()/KSPSetUp() has been called");
2784b9ad928SBarry Smith   jac->n = blocks;
2790a545947SLisandro Dalcin   if (!lens) jac->g_lens = NULL;
2802fa5cd67SKarl Rupp   else {
2819566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(blocks, &jac->g_lens));
2829566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(jac->g_lens, lens, blocks));
2834b9ad928SBarry Smith   }
2843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2854b9ad928SBarry Smith }
2864b9ad928SBarry Smith 
287d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCBJacobiGetTotalBlocks_BJacobi(PC pc, PetscInt *blocks, const PetscInt *lens[])
288d71ae5a4SJacob Faibussowitsch {
2894b9ad928SBarry Smith   PC_BJacobi *jac = (PC_BJacobi *)pc->data;
2904b9ad928SBarry Smith 
2914b9ad928SBarry Smith   PetscFunctionBegin;
2924b9ad928SBarry Smith   *blocks = jac->n;
2934b9ad928SBarry Smith   if (lens) *lens = jac->g_lens;
2943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2954b9ad928SBarry Smith }
2964b9ad928SBarry Smith 
297d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCBJacobiSetLocalBlocks_BJacobi(PC pc, PetscInt blocks, const PetscInt lens[])
298d71ae5a4SJacob Faibussowitsch {
2994b9ad928SBarry Smith   PC_BJacobi *jac;
3004b9ad928SBarry Smith 
3014b9ad928SBarry Smith   PetscFunctionBegin;
3024b9ad928SBarry Smith   jac = (PC_BJacobi *)pc->data;
3034b9ad928SBarry Smith 
3044b9ad928SBarry Smith   jac->n_local = blocks;
3050a545947SLisandro Dalcin   if (!lens) jac->l_lens = NULL;
3062fa5cd67SKarl Rupp   else {
3079566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(blocks, &jac->l_lens));
3089566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(jac->l_lens, lens, blocks));
3094b9ad928SBarry Smith   }
3103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3114b9ad928SBarry Smith }
3124b9ad928SBarry Smith 
313d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCBJacobiGetLocalBlocks_BJacobi(PC pc, PetscInt *blocks, const PetscInt *lens[])
314d71ae5a4SJacob Faibussowitsch {
3154b9ad928SBarry Smith   PC_BJacobi *jac = (PC_BJacobi *)pc->data;
3164b9ad928SBarry Smith 
3174b9ad928SBarry Smith   PetscFunctionBegin;
3184b9ad928SBarry Smith   *blocks = jac->n_local;
3194b9ad928SBarry Smith   if (lens) *lens = jac->l_lens;
3203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3214b9ad928SBarry Smith }
3224b9ad928SBarry Smith 
3234b9ad928SBarry Smith /*@C
324f1580f4eSBarry Smith   PCBJacobiGetSubKSP - Gets the local `KSP` contexts for all blocks on
3254b9ad928SBarry Smith   this processor.
3264b9ad928SBarry Smith 
3276da92b7fSHong Zhang   Not Collective
3284b9ad928SBarry Smith 
3294b9ad928SBarry Smith   Input Parameter:
3304b9ad928SBarry Smith . pc - the preconditioner context
3314b9ad928SBarry Smith 
3324b9ad928SBarry Smith   Output Parameters:
3330298fd71SBarry Smith + n_local     - the number of blocks on this processor, or NULL
3340298fd71SBarry Smith . first_local - the global number of the first block on this processor, or NULL
3354b9ad928SBarry Smith - ksp         - the array of KSP contexts
3364b9ad928SBarry Smith 
337ce78bad3SBarry Smith   Level: advanced
338ce78bad3SBarry Smith 
3394b9ad928SBarry Smith   Notes:
340f1580f4eSBarry Smith   After `PCBJacobiGetSubKSP()` the array of `KSP` contexts is not to be freed.
3414b9ad928SBarry Smith 
3424b9ad928SBarry Smith   Currently for some matrix implementations only 1 block per processor
3434b9ad928SBarry Smith   is supported.
3444b9ad928SBarry Smith 
345f1580f4eSBarry Smith   You must call `KSPSetUp()` or `PCSetUp()` before calling `PCBJacobiGetSubKSP()`.
3464b9ad928SBarry Smith 
347562efe2eSBarry Smith .seealso: [](ch_ksp), `PCBJACOBI`, `PCASM`, `PCASMGetSubKSP()`
3484b9ad928SBarry Smith @*/
349d71ae5a4SJacob Faibussowitsch PetscErrorCode PCBJacobiGetSubKSP(PC pc, PetscInt *n_local, PetscInt *first_local, KSP *ksp[])
350d71ae5a4SJacob Faibussowitsch {
3514b9ad928SBarry Smith   PetscFunctionBegin;
3520700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
353cac4c232SBarry Smith   PetscUseMethod(pc, "PCBJacobiGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (pc, n_local, first_local, ksp));
3543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3554b9ad928SBarry Smith }
3564b9ad928SBarry Smith 
3574b9ad928SBarry Smith /*@
3584b9ad928SBarry Smith   PCBJacobiSetTotalBlocks - Sets the global number of blocks for the block
3594b9ad928SBarry Smith   Jacobi preconditioner.
3604b9ad928SBarry Smith 
361c3339decSBarry Smith   Collective
3624b9ad928SBarry Smith 
3634b9ad928SBarry Smith   Input Parameters:
3644b9ad928SBarry Smith + pc     - the preconditioner context
3654b9ad928SBarry Smith . blocks - the number of blocks
3664b9ad928SBarry Smith - lens   - [optional] integer array containing the size of each block
3674b9ad928SBarry Smith 
3684b9ad928SBarry Smith   Options Database Key:
3694b9ad928SBarry Smith . -pc_bjacobi_blocks <blocks> - Sets the number of global blocks
3704b9ad928SBarry Smith 
371ce78bad3SBarry Smith   Level: intermediate
372ce78bad3SBarry Smith 
373f1580f4eSBarry Smith   Note:
3744b9ad928SBarry Smith   Currently only a limited number of blocking configurations are supported.
375f1580f4eSBarry Smith   All processors sharing the `PC` must call this routine with the same data.
3764b9ad928SBarry Smith 
377562efe2eSBarry Smith .seealso: [](ch_ksp), `PCBJACOBI`, `PCSetUseAmat()`, `PCBJacobiSetLocalBlocks()`
3784b9ad928SBarry Smith @*/
379d71ae5a4SJacob Faibussowitsch PetscErrorCode PCBJacobiSetTotalBlocks(PC pc, PetscInt blocks, const PetscInt lens[])
380d71ae5a4SJacob Faibussowitsch {
3814b9ad928SBarry Smith   PetscFunctionBegin;
3820700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
38308401ef6SPierre Jolivet   PetscCheck(blocks > 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Must have positive blocks");
384cac4c232SBarry Smith   PetscTryMethod(pc, "PCBJacobiSetTotalBlocks_C", (PC, PetscInt, const PetscInt[]), (pc, blocks, lens));
3853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3864b9ad928SBarry Smith }
3874b9ad928SBarry Smith 
3884b9ad928SBarry Smith /*@C
3894b9ad928SBarry Smith   PCBJacobiGetTotalBlocks - Gets the global number of blocks for the block
390f1580f4eSBarry Smith   Jacobi, `PCBJACOBI`, preconditioner.
3914b9ad928SBarry Smith 
392ad4df100SBarry Smith   Not Collective
3934b9ad928SBarry Smith 
3944b9ad928SBarry Smith   Input Parameter:
3954b9ad928SBarry Smith . pc - the preconditioner context
3964b9ad928SBarry Smith 
397feefa0e1SJacob Faibussowitsch   Output Parameters:
3984b9ad928SBarry Smith + blocks - the number of blocks
3994b9ad928SBarry Smith - lens   - integer array containing the size of each block
4004b9ad928SBarry Smith 
4014b9ad928SBarry Smith   Level: intermediate
4024b9ad928SBarry Smith 
403562efe2eSBarry Smith .seealso: [](ch_ksp), `PCBJACOBI`, `PCSetUseAmat()`, `PCBJacobiGetLocalBlocks()`
4044b9ad928SBarry Smith @*/
405d71ae5a4SJacob Faibussowitsch PetscErrorCode PCBJacobiGetTotalBlocks(PC pc, PetscInt *blocks, const PetscInt *lens[])
406d71ae5a4SJacob Faibussowitsch {
4074b9ad928SBarry Smith   PetscFunctionBegin;
4080700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
4094f572ea9SToby Isaac   PetscAssertPointer(blocks, 2);
410cac4c232SBarry Smith   PetscUseMethod(pc, "PCBJacobiGetTotalBlocks_C", (PC, PetscInt *, const PetscInt *[]), (pc, blocks, lens));
4113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4124b9ad928SBarry Smith }
4134b9ad928SBarry Smith 
4144b9ad928SBarry Smith /*@
4154b9ad928SBarry Smith   PCBJacobiSetLocalBlocks - Sets the local number of blocks for the block
416f1580f4eSBarry Smith   Jacobi, `PCBJACOBI`,  preconditioner.
4174b9ad928SBarry Smith 
4184b9ad928SBarry Smith   Not Collective
4194b9ad928SBarry Smith 
4204b9ad928SBarry Smith   Input Parameters:
4214b9ad928SBarry Smith + pc     - the preconditioner context
4224b9ad928SBarry Smith . blocks - the number of blocks
4234b9ad928SBarry Smith - lens   - [optional] integer array containing size of each block
4244b9ad928SBarry Smith 
425342c94f9SMatthew G. Knepley   Options Database Key:
426342c94f9SMatthew G. Knepley . -pc_bjacobi_local_blocks <blocks> - Sets the number of local blocks
427342c94f9SMatthew G. Knepley 
428ce78bad3SBarry Smith   Level: intermediate
429ce78bad3SBarry Smith 
4304b9ad928SBarry Smith   Note:
4314b9ad928SBarry Smith   Currently only a limited number of blocking configurations are supported.
4324b9ad928SBarry Smith 
433562efe2eSBarry Smith .seealso: [](ch_ksp), `PCBJACOBI`, `PCSetUseAmat()`, `PCBJacobiSetTotalBlocks()`
4344b9ad928SBarry Smith @*/
435d71ae5a4SJacob Faibussowitsch PetscErrorCode PCBJacobiSetLocalBlocks(PC pc, PetscInt blocks, const PetscInt lens[])
436d71ae5a4SJacob Faibussowitsch {
4374b9ad928SBarry Smith   PetscFunctionBegin;
4380700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
43908401ef6SPierre Jolivet   PetscCheck(blocks >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must have nonegative blocks");
440cac4c232SBarry Smith   PetscTryMethod(pc, "PCBJacobiSetLocalBlocks_C", (PC, PetscInt, const PetscInt[]), (pc, blocks, lens));
4413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4424b9ad928SBarry Smith }
4434b9ad928SBarry Smith 
4444b9ad928SBarry Smith /*@C
4454b9ad928SBarry Smith   PCBJacobiGetLocalBlocks - Gets the local number of blocks for the block
446f1580f4eSBarry Smith   Jacobi, `PCBJACOBI`, preconditioner.
4474b9ad928SBarry Smith 
4484b9ad928SBarry Smith   Not Collective
4494b9ad928SBarry Smith 
4504b9ad928SBarry Smith   Input Parameters:
4514b9ad928SBarry Smith + pc     - the preconditioner context
4524b9ad928SBarry Smith . blocks - the number of blocks
4534b9ad928SBarry Smith - lens   - [optional] integer array containing size of each block
4544b9ad928SBarry Smith 
455ce78bad3SBarry Smith   Level: intermediate
456ce78bad3SBarry Smith 
4574b9ad928SBarry Smith   Note:
4584b9ad928SBarry Smith   Currently only a limited number of blocking configurations are supported.
4594b9ad928SBarry Smith 
460562efe2eSBarry Smith .seealso: [](ch_ksp), `PCBJACOBI`, `PCSetUseAmat()`, `PCBJacobiGetTotalBlocks()`
4614b9ad928SBarry Smith @*/
462d71ae5a4SJacob Faibussowitsch PetscErrorCode PCBJacobiGetLocalBlocks(PC pc, PetscInt *blocks, const PetscInt *lens[])
463d71ae5a4SJacob Faibussowitsch {
4644b9ad928SBarry Smith   PetscFunctionBegin;
4650700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
4664f572ea9SToby Isaac   PetscAssertPointer(blocks, 2);
467cac4c232SBarry Smith   PetscUseMethod(pc, "PCBJacobiGetLocalBlocks_C", (PC, PetscInt *, const PetscInt *[]), (pc, blocks, lens));
4683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4694b9ad928SBarry Smith }
4704b9ad928SBarry Smith 
4714b9ad928SBarry Smith /*MC
4724b9ad928SBarry Smith    PCBJACOBI - Use block Jacobi preconditioning, each block is (approximately) solved with
473f1580f4eSBarry Smith            its own `KSP` object.
4744b9ad928SBarry Smith 
4754b9ad928SBarry Smith    Options Database Keys:
476011ea8aeSBarry Smith +  -pc_use_amat - use Amat to apply block of operator in inner Krylov method
477011ea8aeSBarry Smith -  -pc_bjacobi_blocks <n> - use n total blocks
4784b9ad928SBarry Smith 
479ce78bad3SBarry Smith    Level: beginner
480ce78bad3SBarry Smith 
48195452b02SPatrick Sanan    Notes:
482f1580f4eSBarry Smith     See `PCJACOBI` for diagonal Jacobi, `PCVPBJACOBI` for variable point block, and `PCPBJACOBI` for fixed size point block
483468ae2e8SBarry Smith 
48495452b02SPatrick Sanan     Each processor can have one or more blocks, or a single block can be shared by several processes. Defaults to one block per processor.
4854b9ad928SBarry Smith 
486f1580f4eSBarry Smith      To set options on the solvers for each block append -sub_ to all the `KSP` and `PC`
487d7ee0231SBarry Smith         options database keys. For example, -sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly
4884b9ad928SBarry Smith 
489f1580f4eSBarry Smith      To set the options on the solvers separate for each block call `PCBJacobiGetSubKSP()`
490f1580f4eSBarry Smith          and set the options directly on the resulting `KSP` object (you can access its `PC`
4910462cc06SPierre Jolivet          `KSPGetPC()`)
4924b9ad928SBarry Smith 
493f1580f4eSBarry Smith      For GPU-based vectors (`VECCUDA`, `VECViennaCL`) it is recommended to use exactly one block per MPI process for best
4942210c163SDominic Meiser          performance.  Different block partitioning may lead to additional data transfers
4952210c163SDominic Meiser          between host and GPU that lead to degraded performance.
4962210c163SDominic Meiser 
497011ea8aeSBarry Smith      When multiple processes share a single block, each block encompasses exactly all the unknowns owned its set of processes.
498011ea8aeSBarry Smith 
499562efe2eSBarry Smith .seealso: [](ch_ksp), `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCType`,
500db781477SPatrick Sanan           `PCASM`, `PCSetUseAmat()`, `PCGetUseAmat()`, `PCBJacobiGetSubKSP()`, `PCBJacobiSetTotalBlocks()`,
501db781477SPatrick Sanan           `PCBJacobiSetLocalBlocks()`, `PCSetModifySubMatrices()`, `PCJACOBI`, `PCVPBJACOBI`, `PCPBJACOBI`
5024b9ad928SBarry Smith M*/
5034b9ad928SBarry Smith 
504d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_BJacobi(PC pc)
505d71ae5a4SJacob Faibussowitsch {
50669a612a9SBarry Smith   PetscMPIInt rank;
5074b9ad928SBarry Smith   PC_BJacobi *jac;
5084b9ad928SBarry Smith 
5094b9ad928SBarry Smith   PetscFunctionBegin;
5104dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&jac));
5119566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
5122fa5cd67SKarl Rupp 
5130a545947SLisandro Dalcin   pc->ops->apply             = NULL;
5147b6e2003SPierre Jolivet   pc->ops->matapply          = NULL;
5150a545947SLisandro Dalcin   pc->ops->applytranspose    = NULL;
516*4dbf25a8SPierre Jolivet   pc->ops->matapplytranspose = NULL;
5174b9ad928SBarry Smith   pc->ops->setup             = PCSetUp_BJacobi;
5184b9ad928SBarry Smith   pc->ops->destroy           = PCDestroy_BJacobi;
5194b9ad928SBarry Smith   pc->ops->setfromoptions    = PCSetFromOptions_BJacobi;
5204b9ad928SBarry Smith   pc->ops->view              = PCView_BJacobi;
5210a545947SLisandro Dalcin   pc->ops->applyrichardson   = NULL;
5224b9ad928SBarry Smith 
5234b9ad928SBarry Smith   pc->data         = (void *)jac;
5244b9ad928SBarry Smith   jac->n           = -1;
5254b9ad928SBarry Smith   jac->n_local     = -1;
5264b9ad928SBarry Smith   jac->first_local = rank;
5270a545947SLisandro Dalcin   jac->ksp         = NULL;
5280a545947SLisandro Dalcin   jac->g_lens      = NULL;
5290a545947SLisandro Dalcin   jac->l_lens      = NULL;
5300a545947SLisandro Dalcin   jac->psubcomm    = NULL;
5314b9ad928SBarry Smith 
5329566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetSubKSP_C", PCBJacobiGetSubKSP_BJacobi));
5339566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiSetTotalBlocks_C", PCBJacobiSetTotalBlocks_BJacobi));
5349566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetTotalBlocks_C", PCBJacobiGetTotalBlocks_BJacobi));
5359566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiSetLocalBlocks_C", PCBJacobiSetLocalBlocks_BJacobi));
5369566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetLocalBlocks_C", PCBJacobiGetLocalBlocks_BJacobi));
5373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5384b9ad928SBarry Smith }
5394b9ad928SBarry Smith 
5404b9ad928SBarry Smith /*
5414b9ad928SBarry Smith         These are for a single block per processor; works for AIJ, BAIJ; Seq and MPI
5424b9ad928SBarry Smith */
543d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_BJacobi_Singleblock(PC pc)
544d71ae5a4SJacob Faibussowitsch {
5454b9ad928SBarry Smith   PC_BJacobi             *jac  = (PC_BJacobi *)pc->data;
5464b9ad928SBarry Smith   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data;
5474b9ad928SBarry Smith 
5484b9ad928SBarry Smith   PetscFunctionBegin;
5499566063dSJacob Faibussowitsch   PetscCall(KSPReset(jac->ksp[0]));
5509566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&bjac->x));
5519566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&bjac->y));
5523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
553e91c6855SBarry Smith }
554e91c6855SBarry Smith 
555d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_BJacobi_Singleblock(PC pc)
556d71ae5a4SJacob Faibussowitsch {
557e91c6855SBarry Smith   PC_BJacobi             *jac  = (PC_BJacobi *)pc->data;
558e91c6855SBarry Smith   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data;
559e91c6855SBarry Smith 
560e91c6855SBarry Smith   PetscFunctionBegin;
5619566063dSJacob Faibussowitsch   PetscCall(PCReset_BJacobi_Singleblock(pc));
5629566063dSJacob Faibussowitsch   PetscCall(KSPDestroy(&jac->ksp[0]));
5639566063dSJacob Faibussowitsch   PetscCall(PetscFree(jac->ksp));
5649566063dSJacob Faibussowitsch   PetscCall(PetscFree(bjac));
5652e956fe4SStefano Zampini   PetscCall(PCDestroy_BJacobi(pc));
5663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5674b9ad928SBarry Smith }
5684b9ad928SBarry Smith 
569d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUpOnBlocks_BJacobi_Singleblock(PC pc)
570d71ae5a4SJacob Faibussowitsch {
5714b9ad928SBarry Smith   PC_BJacobi        *jac    = (PC_BJacobi *)pc->data;
5722295b7c8SHong Zhang   KSP                subksp = jac->ksp[0];
573539c167fSBarry Smith   KSPConvergedReason reason;
5744b9ad928SBarry Smith 
5754b9ad928SBarry Smith   PetscFunctionBegin;
5769566063dSJacob Faibussowitsch   PetscCall(KSPSetUp(subksp));
5779566063dSJacob Faibussowitsch   PetscCall(KSPGetConvergedReason(subksp, &reason));
578ad540459SPierre Jolivet   if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
5793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5804b9ad928SBarry Smith }
5814b9ad928SBarry Smith 
582d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_BJacobi_Singleblock(PC pc, Vec x, Vec y)
583d71ae5a4SJacob Faibussowitsch {
5844b9ad928SBarry Smith   PC_BJacobi             *jac  = (PC_BJacobi *)pc->data;
5854b9ad928SBarry Smith   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data;
5866e111a19SKarl Rupp 
5874b9ad928SBarry Smith   PetscFunctionBegin;
5889566063dSJacob Faibussowitsch   PetscCall(VecGetLocalVectorRead(x, bjac->x));
5899566063dSJacob Faibussowitsch   PetscCall(VecGetLocalVector(y, bjac->y));
590bba28a21SBarry Smith   /* Since the inner KSP matrix may point directly to the diagonal block of an MPI matrix the inner
591910cf402Sprj-      matrix may change even if the outer KSP/PC has not updated the preconditioner, this will trigger a rebuild
592910cf402Sprj-      of the inner preconditioner automatically unless we pass down the outer preconditioners reuse flag.*/
5939566063dSJacob Faibussowitsch   PetscCall(KSPSetReusePreconditioner(jac->ksp[0], pc->reusepreconditioner));
5949566063dSJacob Faibussowitsch   PetscCall(KSPSolve(jac->ksp[0], bjac->x, bjac->y));
5959566063dSJacob Faibussowitsch   PetscCall(KSPCheckSolve(jac->ksp[0], pc, bjac->y));
5969566063dSJacob Faibussowitsch   PetscCall(VecRestoreLocalVectorRead(x, bjac->x));
5979566063dSJacob Faibussowitsch   PetscCall(VecRestoreLocalVector(y, bjac->y));
5983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5994b9ad928SBarry Smith }
6004b9ad928SBarry Smith 
601*4dbf25a8SPierre Jolivet static PetscErrorCode PCMatApply_BJacobi_Singleblock_Private(PC pc, Mat X, Mat Y, PetscBool transpose)
602d71ae5a4SJacob Faibussowitsch {
6037b6e2003SPierre Jolivet   PC_BJacobi *jac = (PC_BJacobi *)pc->data;
6047b6e2003SPierre Jolivet   Mat         sX, sY;
6057b6e2003SPierre Jolivet 
6067b6e2003SPierre Jolivet   PetscFunctionBegin;
6077b6e2003SPierre Jolivet   /* Since the inner KSP matrix may point directly to the diagonal block of an MPI matrix the inner
6087b6e2003SPierre Jolivet      matrix may change even if the outer KSP/PC has not updated the preconditioner, this will trigger a rebuild
6097b6e2003SPierre Jolivet      of the inner preconditioner automatically unless we pass down the outer preconditioners reuse flag.*/
6109566063dSJacob Faibussowitsch   PetscCall(KSPSetReusePreconditioner(jac->ksp[0], pc->reusepreconditioner));
6119566063dSJacob Faibussowitsch   PetscCall(MatDenseGetLocalMatrix(X, &sX));
6129566063dSJacob Faibussowitsch   PetscCall(MatDenseGetLocalMatrix(Y, &sY));
613*4dbf25a8SPierre Jolivet   if (!transpose) PetscCall(KSPMatSolve(jac->ksp[0], sX, sY));
614*4dbf25a8SPierre Jolivet   else PetscCall(KSPMatSolveTranspose(jac->ksp[0], sX, sY));
615*4dbf25a8SPierre Jolivet   PetscFunctionReturn(PETSC_SUCCESS);
616*4dbf25a8SPierre Jolivet }
617*4dbf25a8SPierre Jolivet 
618*4dbf25a8SPierre Jolivet static PetscErrorCode PCMatApply_BJacobi_Singleblock(PC pc, Mat X, Mat Y)
619*4dbf25a8SPierre Jolivet {
620*4dbf25a8SPierre Jolivet   PetscFunctionBegin;
621*4dbf25a8SPierre Jolivet   PetscCall(PCMatApply_BJacobi_Singleblock_Private(pc, X, Y, PETSC_FALSE));
622*4dbf25a8SPierre Jolivet   PetscFunctionReturn(PETSC_SUCCESS);
623*4dbf25a8SPierre Jolivet }
624*4dbf25a8SPierre Jolivet 
625*4dbf25a8SPierre Jolivet static PetscErrorCode PCMatApplyTranspose_BJacobi_Singleblock(PC pc, Mat X, Mat Y)
626*4dbf25a8SPierre Jolivet {
627*4dbf25a8SPierre Jolivet   PetscFunctionBegin;
628*4dbf25a8SPierre Jolivet   PetscCall(PCMatApply_BJacobi_Singleblock_Private(pc, X, Y, PETSC_TRUE));
6293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6307b6e2003SPierre Jolivet }
6317b6e2003SPierre Jolivet 
632d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplySymmetricLeft_BJacobi_Singleblock(PC pc, Vec x, Vec y)
633d71ae5a4SJacob Faibussowitsch {
6344b9ad928SBarry Smith   PC_BJacobi             *jac  = (PC_BJacobi *)pc->data;
6354b9ad928SBarry Smith   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data;
636d9ca1df4SBarry Smith   PetscScalar            *y_array;
637d9ca1df4SBarry Smith   const PetscScalar      *x_array;
6384b9ad928SBarry Smith   PC                      subpc;
6394b9ad928SBarry Smith 
6404b9ad928SBarry Smith   PetscFunctionBegin;
6414b9ad928SBarry Smith   /*
6424b9ad928SBarry Smith       The VecPlaceArray() is to avoid having to copy the
6434b9ad928SBarry Smith     y vector into the bjac->x vector. The reason for
6444b9ad928SBarry Smith     the bjac->x vector is that we need a sequential vector
6454b9ad928SBarry Smith     for the sequential solve.
6464b9ad928SBarry Smith   */
6479566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &x_array));
6489566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &y_array));
6499566063dSJacob Faibussowitsch   PetscCall(VecPlaceArray(bjac->x, x_array));
6509566063dSJacob Faibussowitsch   PetscCall(VecPlaceArray(bjac->y, y_array));
6514b9ad928SBarry Smith   /* apply the symmetric left portion of the inner PC operator */
652c3f9dca2SPierre Jolivet   /* note this bypasses the inner KSP and its options completely */
6539566063dSJacob Faibussowitsch   PetscCall(KSPGetPC(jac->ksp[0], &subpc));
6549566063dSJacob Faibussowitsch   PetscCall(PCApplySymmetricLeft(subpc, bjac->x, bjac->y));
6559566063dSJacob Faibussowitsch   PetscCall(VecResetArray(bjac->x));
6569566063dSJacob Faibussowitsch   PetscCall(VecResetArray(bjac->y));
6579566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &x_array));
6589566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &y_array));
6593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6604b9ad928SBarry Smith }
6614b9ad928SBarry Smith 
662d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplySymmetricRight_BJacobi_Singleblock(PC pc, Vec x, Vec y)
663d71ae5a4SJacob Faibussowitsch {
6644b9ad928SBarry Smith   PC_BJacobi             *jac  = (PC_BJacobi *)pc->data;
6654b9ad928SBarry Smith   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data;
666d9ca1df4SBarry Smith   PetscScalar            *y_array;
667d9ca1df4SBarry Smith   const PetscScalar      *x_array;
6684b9ad928SBarry Smith   PC                      subpc;
6694b9ad928SBarry Smith 
6704b9ad928SBarry Smith   PetscFunctionBegin;
6714b9ad928SBarry Smith   /*
6724b9ad928SBarry Smith       The VecPlaceArray() is to avoid having to copy the
6734b9ad928SBarry Smith     y vector into the bjac->x vector. The reason for
6744b9ad928SBarry Smith     the bjac->x vector is that we need a sequential vector
6754b9ad928SBarry Smith     for the sequential solve.
6764b9ad928SBarry Smith   */
6779566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &x_array));
6789566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &y_array));
6799566063dSJacob Faibussowitsch   PetscCall(VecPlaceArray(bjac->x, x_array));
6809566063dSJacob Faibussowitsch   PetscCall(VecPlaceArray(bjac->y, y_array));
6814b9ad928SBarry Smith 
6824b9ad928SBarry Smith   /* apply the symmetric right portion of the inner PC operator */
683c3f9dca2SPierre Jolivet   /* note this bypasses the inner KSP and its options completely */
6844b9ad928SBarry Smith 
6859566063dSJacob Faibussowitsch   PetscCall(KSPGetPC(jac->ksp[0], &subpc));
6869566063dSJacob Faibussowitsch   PetscCall(PCApplySymmetricRight(subpc, bjac->x, bjac->y));
6874b9ad928SBarry Smith 
68811e4f71cSBarry Smith   PetscCall(VecResetArray(bjac->x));
68911e4f71cSBarry Smith   PetscCall(VecResetArray(bjac->y));
6909566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &x_array));
6919566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &y_array));
6923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6934b9ad928SBarry Smith }
6944b9ad928SBarry Smith 
695d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplyTranspose_BJacobi_Singleblock(PC pc, Vec x, Vec y)
696d71ae5a4SJacob Faibussowitsch {
6974b9ad928SBarry Smith   PC_BJacobi             *jac  = (PC_BJacobi *)pc->data;
6984b9ad928SBarry Smith   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data;
699d9ca1df4SBarry Smith   PetscScalar            *y_array;
700d9ca1df4SBarry Smith   const PetscScalar      *x_array;
7014b9ad928SBarry Smith 
7024b9ad928SBarry Smith   PetscFunctionBegin;
7034b9ad928SBarry Smith   /*
7044b9ad928SBarry Smith       The VecPlaceArray() is to avoid having to copy the
7054b9ad928SBarry Smith     y vector into the bjac->x vector. The reason for
7064b9ad928SBarry Smith     the bjac->x vector is that we need a sequential vector
7074b9ad928SBarry Smith     for the sequential solve.
7084b9ad928SBarry Smith   */
7099566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &x_array));
7109566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &y_array));
7119566063dSJacob Faibussowitsch   PetscCall(VecPlaceArray(bjac->x, x_array));
7129566063dSJacob Faibussowitsch   PetscCall(VecPlaceArray(bjac->y, y_array));
7139566063dSJacob Faibussowitsch   PetscCall(KSPSolveTranspose(jac->ksp[0], bjac->x, bjac->y));
7149566063dSJacob Faibussowitsch   PetscCall(KSPCheckSolve(jac->ksp[0], pc, bjac->y));
7159566063dSJacob Faibussowitsch   PetscCall(VecResetArray(bjac->x));
7169566063dSJacob Faibussowitsch   PetscCall(VecResetArray(bjac->y));
7179566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &x_array));
7189566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &y_array));
7193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7204b9ad928SBarry Smith }
7214b9ad928SBarry Smith 
722d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_BJacobi_Singleblock(PC pc, Mat mat, Mat pmat)
723d71ae5a4SJacob Faibussowitsch {
7244b9ad928SBarry Smith   PC_BJacobi             *jac = (PC_BJacobi *)pc->data;
72569a612a9SBarry Smith   PetscInt                m;
7264b9ad928SBarry Smith   KSP                     ksp;
7274b9ad928SBarry Smith   PC_BJacobi_Singleblock *bjac;
728de0953f6SBarry Smith   PetscBool               wasSetup = PETSC_TRUE;
7293f6dc190SJunchao Zhang   VecType                 vectype;
730ea41da7aSPierre Jolivet   const char             *prefix;
7314b9ad928SBarry Smith 
7324b9ad928SBarry Smith   PetscFunctionBegin;
7334b9ad928SBarry Smith   if (!pc->setupcalled) {
734c2efdce3SBarry Smith     if (!jac->ksp) {
7353821be0aSBarry Smith       PetscInt nestlevel;
7363821be0aSBarry Smith 
737302a9ddcSMatthew Knepley       wasSetup = PETSC_FALSE;
7382fa5cd67SKarl Rupp 
7399566063dSJacob Faibussowitsch       PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp));
7403821be0aSBarry Smith       PetscCall(PCGetKSPNestLevel(pc, &nestlevel));
7413821be0aSBarry Smith       PetscCall(KSPSetNestLevel(ksp, nestlevel + 1));
7429566063dSJacob Faibussowitsch       PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
7439566063dSJacob Faibussowitsch       PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1));
7449566063dSJacob Faibussowitsch       PetscCall(KSPSetType(ksp, KSPPREONLY));
7459566063dSJacob Faibussowitsch       PetscCall(PCGetOptionsPrefix(pc, &prefix));
7469566063dSJacob Faibussowitsch       PetscCall(KSPSetOptionsPrefix(ksp, prefix));
7479566063dSJacob Faibussowitsch       PetscCall(KSPAppendOptionsPrefix(ksp, "sub_"));
7484b9ad928SBarry Smith 
749e91c6855SBarry Smith       pc->ops->reset               = PCReset_BJacobi_Singleblock;
7504b9ad928SBarry Smith       pc->ops->destroy             = PCDestroy_BJacobi_Singleblock;
7514b9ad928SBarry Smith       pc->ops->apply               = PCApply_BJacobi_Singleblock;
7527b6e2003SPierre Jolivet       pc->ops->matapply            = PCMatApply_BJacobi_Singleblock;
753*4dbf25a8SPierre Jolivet       pc->ops->matapplytranspose   = PCMatApplyTranspose_BJacobi_Singleblock;
7544b9ad928SBarry Smith       pc->ops->applysymmetricleft  = PCApplySymmetricLeft_BJacobi_Singleblock;
7554b9ad928SBarry Smith       pc->ops->applysymmetricright = PCApplySymmetricRight_BJacobi_Singleblock;
7564b9ad928SBarry Smith       pc->ops->applytranspose      = PCApplyTranspose_BJacobi_Singleblock;
7574b9ad928SBarry Smith       pc->ops->setuponblocks       = PCSetUpOnBlocks_BJacobi_Singleblock;
7584b9ad928SBarry Smith 
7599566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(1, &jac->ksp));
7604b9ad928SBarry Smith       jac->ksp[0] = ksp;
761c2efdce3SBarry Smith 
7624dfa11a4SJacob Faibussowitsch       PetscCall(PetscNew(&bjac));
7634b9ad928SBarry Smith       jac->data = (void *)bjac;
7644b9ad928SBarry Smith     } else {
765c2efdce3SBarry Smith       ksp  = jac->ksp[0];
766c2efdce3SBarry Smith       bjac = (PC_BJacobi_Singleblock *)jac->data;
767c2efdce3SBarry Smith     }
768c2efdce3SBarry Smith 
769c2efdce3SBarry Smith     /*
770c2efdce3SBarry Smith       The reason we need to generate these vectors is to serve
771c2efdce3SBarry Smith       as the right-hand side and solution vector for the solve on the
772c2efdce3SBarry Smith       block. We do not need to allocate space for the vectors since
773c2efdce3SBarry Smith       that is provided via VecPlaceArray() just before the call to
774c2efdce3SBarry Smith       KSPSolve() on the block.
775c2efdce3SBarry Smith     */
7769566063dSJacob Faibussowitsch     PetscCall(MatGetSize(pmat, &m, &m));
7779566063dSJacob Faibussowitsch     PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, m, NULL, &bjac->x));
7789566063dSJacob Faibussowitsch     PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, m, NULL, &bjac->y));
7799566063dSJacob Faibussowitsch     PetscCall(MatGetVecType(pmat, &vectype));
7809566063dSJacob Faibussowitsch     PetscCall(VecSetType(bjac->x, vectype));
7819566063dSJacob Faibussowitsch     PetscCall(VecSetType(bjac->y, vectype));
782c2efdce3SBarry Smith   } else {
7834b9ad928SBarry Smith     ksp  = jac->ksp[0];
7844b9ad928SBarry Smith     bjac = (PC_BJacobi_Singleblock *)jac->data;
7854b9ad928SBarry Smith   }
7869566063dSJacob Faibussowitsch   PetscCall(KSPGetOptionsPrefix(ksp, &prefix));
78749517cdeSBarry Smith   if (pc->useAmat) {
7889566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(ksp, mat, pmat));
7899566063dSJacob Faibussowitsch     PetscCall(MatSetOptionsPrefix(mat, prefix));
7904b9ad928SBarry Smith   } else {
7919566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(ksp, pmat, pmat));
7924b9ad928SBarry Smith   }
7939566063dSJacob Faibussowitsch   PetscCall(MatSetOptionsPrefix(pmat, prefix));
79490f1c854SHong Zhang   if (!wasSetup && pc->setfromoptionscalled) {
795248bfaf0SJed Brown     /* If PCSetFromOptions_BJacobi is called later, KSPSetFromOptions will be called at that time. */
7969566063dSJacob Faibussowitsch     PetscCall(KSPSetFromOptions(ksp));
797302a9ddcSMatthew Knepley   }
7983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7994b9ad928SBarry Smith }
8004b9ad928SBarry Smith 
801d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_BJacobi_Multiblock(PC pc)
802d71ae5a4SJacob Faibussowitsch {
8034b9ad928SBarry Smith   PC_BJacobi            *jac  = (PC_BJacobi *)pc->data;
8044b9ad928SBarry Smith   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data;
80569a612a9SBarry Smith   PetscInt               i;
8064b9ad928SBarry Smith 
8074b9ad928SBarry Smith   PetscFunctionBegin;
808e91c6855SBarry Smith   if (bjac && bjac->pmat) {
8099566063dSJacob Faibussowitsch     PetscCall(MatDestroyMatrices(jac->n_local, &bjac->pmat));
81048a46eb9SPierre Jolivet     if (pc->useAmat) PetscCall(MatDestroyMatrices(jac->n_local, &bjac->mat));
811e91c6855SBarry Smith   }
8124b9ad928SBarry Smith 
8134b9ad928SBarry Smith   for (i = 0; i < jac->n_local; i++) {
8149566063dSJacob Faibussowitsch     PetscCall(KSPReset(jac->ksp[i]));
815e91c6855SBarry Smith     if (bjac && bjac->x) {
8169566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&bjac->x[i]));
8179566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&bjac->y[i]));
8189566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&bjac->is[i]));
8194b9ad928SBarry Smith     }
820e91c6855SBarry Smith   }
8219566063dSJacob Faibussowitsch   PetscCall(PetscFree(jac->l_lens));
8229566063dSJacob Faibussowitsch   PetscCall(PetscFree(jac->g_lens));
8233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
824e91c6855SBarry Smith }
825e91c6855SBarry Smith 
826d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_BJacobi_Multiblock(PC pc)
827d71ae5a4SJacob Faibussowitsch {
828e91c6855SBarry Smith   PC_BJacobi            *jac  = (PC_BJacobi *)pc->data;
829c2efdce3SBarry Smith   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data;
830e91c6855SBarry Smith   PetscInt               i;
831e91c6855SBarry Smith 
832e91c6855SBarry Smith   PetscFunctionBegin;
8339566063dSJacob Faibussowitsch   PetscCall(PCReset_BJacobi_Multiblock(pc));
834c2efdce3SBarry Smith   if (bjac) {
8359566063dSJacob Faibussowitsch     PetscCall(PetscFree2(bjac->x, bjac->y));
8369566063dSJacob Faibussowitsch     PetscCall(PetscFree(bjac->starts));
8379566063dSJacob Faibussowitsch     PetscCall(PetscFree(bjac->is));
838c2efdce3SBarry Smith   }
8399566063dSJacob Faibussowitsch   PetscCall(PetscFree(jac->data));
84048a46eb9SPierre Jolivet   for (i = 0; i < jac->n_local; i++) PetscCall(KSPDestroy(&jac->ksp[i]));
8419566063dSJacob Faibussowitsch   PetscCall(PetscFree(jac->ksp));
8422e956fe4SStefano Zampini   PetscCall(PCDestroy_BJacobi(pc));
8433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8444b9ad928SBarry Smith }
8454b9ad928SBarry Smith 
846d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUpOnBlocks_BJacobi_Multiblock(PC pc)
847d71ae5a4SJacob Faibussowitsch {
8484b9ad928SBarry Smith   PC_BJacobi        *jac = (PC_BJacobi *)pc->data;
84969a612a9SBarry Smith   PetscInt           i, n_local = jac->n_local;
850539c167fSBarry Smith   KSPConvergedReason reason;
8514b9ad928SBarry Smith 
8524b9ad928SBarry Smith   PetscFunctionBegin;
8534b9ad928SBarry Smith   for (i = 0; i < n_local; i++) {
8549566063dSJacob Faibussowitsch     PetscCall(KSPSetUp(jac->ksp[i]));
8559566063dSJacob Faibussowitsch     PetscCall(KSPGetConvergedReason(jac->ksp[i], &reason));
856ad540459SPierre Jolivet     if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
8574b9ad928SBarry Smith   }
8583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8594b9ad928SBarry Smith }
8604b9ad928SBarry Smith 
861d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_BJacobi_Multiblock(PC pc, Vec x, Vec y)
862d71ae5a4SJacob Faibussowitsch {
8634b9ad928SBarry Smith   PC_BJacobi            *jac = (PC_BJacobi *)pc->data;
86469a612a9SBarry Smith   PetscInt               i, n_local = jac->n_local;
8654b9ad928SBarry Smith   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data;
866d9ca1df4SBarry Smith   PetscScalar           *yin;
867d9ca1df4SBarry Smith   const PetscScalar     *xin;
86858ebbce7SBarry Smith 
8694b9ad928SBarry Smith   PetscFunctionBegin;
8709566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &xin));
8719566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &yin));
8724b9ad928SBarry Smith   for (i = 0; i < n_local; i++) {
8734b9ad928SBarry Smith     /*
8744b9ad928SBarry Smith        To avoid copying the subvector from x into a workspace we instead
8754b9ad928SBarry Smith        make the workspace vector array point to the subpart of the array of
8764b9ad928SBarry Smith        the global vector.
8774b9ad928SBarry Smith     */
8789566063dSJacob Faibussowitsch     PetscCall(VecPlaceArray(bjac->x[i], xin + bjac->starts[i]));
8799566063dSJacob Faibussowitsch     PetscCall(VecPlaceArray(bjac->y[i], yin + bjac->starts[i]));
8804b9ad928SBarry Smith 
8819566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(PC_ApplyOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0));
8829566063dSJacob Faibussowitsch     PetscCall(KSPSolve(jac->ksp[i], bjac->x[i], bjac->y[i]));
8839566063dSJacob Faibussowitsch     PetscCall(KSPCheckSolve(jac->ksp[i], pc, bjac->y[i]));
8849566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_ApplyOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0));
885d11f3a42SBarry Smith 
8869566063dSJacob Faibussowitsch     PetscCall(VecResetArray(bjac->x[i]));
8879566063dSJacob Faibussowitsch     PetscCall(VecResetArray(bjac->y[i]));
8884b9ad928SBarry Smith   }
8899566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &xin));
8909566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &yin));
8913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8924b9ad928SBarry Smith }
8934b9ad928SBarry Smith 
894f4d694ddSBarry Smith static PetscErrorCode PCApplySymmetricLeft_BJacobi_Multiblock(PC pc, Vec x, Vec y)
895f4d694ddSBarry Smith {
896f4d694ddSBarry Smith   PC_BJacobi            *jac = (PC_BJacobi *)pc->data;
897f4d694ddSBarry Smith   PetscInt               i, n_local = jac->n_local;
898f4d694ddSBarry Smith   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data;
899f4d694ddSBarry Smith   PetscScalar           *yin;
900f4d694ddSBarry Smith   const PetscScalar     *xin;
901f4d694ddSBarry Smith   PC                     subpc;
902f4d694ddSBarry Smith 
903f4d694ddSBarry Smith   PetscFunctionBegin;
904f4d694ddSBarry Smith   PetscCall(VecGetArrayRead(x, &xin));
905f4d694ddSBarry Smith   PetscCall(VecGetArray(y, &yin));
906f4d694ddSBarry Smith   for (i = 0; i < n_local; i++) {
9074b9ad928SBarry Smith     /*
908f4d694ddSBarry Smith        To avoid copying the subvector from x into a workspace we instead
909f4d694ddSBarry Smith        make the workspace vector array point to the subpart of the array of
910f4d694ddSBarry Smith        the global vector.
9114b9ad928SBarry Smith     */
912f4d694ddSBarry Smith     PetscCall(VecPlaceArray(bjac->x[i], xin + bjac->starts[i]));
913f4d694ddSBarry Smith     PetscCall(VecPlaceArray(bjac->y[i], yin + bjac->starts[i]));
914f4d694ddSBarry Smith 
915f4d694ddSBarry Smith     PetscCall(PetscLogEventBegin(PC_ApplyOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0));
916f4d694ddSBarry Smith     /* apply the symmetric left portion of the inner PC operator */
917c3f9dca2SPierre Jolivet     /* note this bypasses the inner KSP and its options completely */
918f4d694ddSBarry Smith     PetscCall(KSPGetPC(jac->ksp[i], &subpc));
919f4d694ddSBarry Smith     PetscCall(PCApplySymmetricLeft(subpc, bjac->x[i], bjac->y[i]));
920f4d694ddSBarry Smith     PetscCall(PetscLogEventEnd(PC_ApplyOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0));
921f4d694ddSBarry Smith 
922f4d694ddSBarry Smith     PetscCall(VecResetArray(bjac->x[i]));
923f4d694ddSBarry Smith     PetscCall(VecResetArray(bjac->y[i]));
924f4d694ddSBarry Smith   }
925f4d694ddSBarry Smith   PetscCall(VecRestoreArrayRead(x, &xin));
926f4d694ddSBarry Smith   PetscCall(VecRestoreArray(y, &yin));
9273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
928f4d694ddSBarry Smith }
929f4d694ddSBarry Smith 
930f4d694ddSBarry Smith static PetscErrorCode PCApplySymmetricRight_BJacobi_Multiblock(PC pc, Vec x, Vec y)
931f4d694ddSBarry Smith {
932f4d694ddSBarry Smith   PC_BJacobi            *jac = (PC_BJacobi *)pc->data;
933f4d694ddSBarry Smith   PetscInt               i, n_local = jac->n_local;
934f4d694ddSBarry Smith   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data;
935f4d694ddSBarry Smith   PetscScalar           *yin;
936f4d694ddSBarry Smith   const PetscScalar     *xin;
937f4d694ddSBarry Smith   PC                     subpc;
938f4d694ddSBarry Smith 
939f4d694ddSBarry Smith   PetscFunctionBegin;
940f4d694ddSBarry Smith   PetscCall(VecGetArrayRead(x, &xin));
941f4d694ddSBarry Smith   PetscCall(VecGetArray(y, &yin));
942f4d694ddSBarry Smith   for (i = 0; i < n_local; i++) {
943f4d694ddSBarry Smith     /*
944f4d694ddSBarry Smith        To avoid copying the subvector from x into a workspace we instead
945f4d694ddSBarry Smith        make the workspace vector array point to the subpart of the array of
946f4d694ddSBarry Smith        the global vector.
947f4d694ddSBarry Smith     */
948f4d694ddSBarry Smith     PetscCall(VecPlaceArray(bjac->x[i], xin + bjac->starts[i]));
949f4d694ddSBarry Smith     PetscCall(VecPlaceArray(bjac->y[i], yin + bjac->starts[i]));
950f4d694ddSBarry Smith 
951f4d694ddSBarry Smith     PetscCall(PetscLogEventBegin(PC_ApplyOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0));
952f4d694ddSBarry Smith     /* apply the symmetric left portion of the inner PC operator */
953c3f9dca2SPierre Jolivet     /* note this bypasses the inner KSP and its options completely */
954f4d694ddSBarry Smith     PetscCall(KSPGetPC(jac->ksp[i], &subpc));
955f4d694ddSBarry Smith     PetscCall(PCApplySymmetricRight(subpc, bjac->x[i], bjac->y[i]));
956f4d694ddSBarry Smith     PetscCall(PetscLogEventEnd(PC_ApplyOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0));
957f4d694ddSBarry Smith 
958f4d694ddSBarry Smith     PetscCall(VecResetArray(bjac->x[i]));
959f4d694ddSBarry Smith     PetscCall(VecResetArray(bjac->y[i]));
960f4d694ddSBarry Smith   }
961f4d694ddSBarry Smith   PetscCall(VecRestoreArrayRead(x, &xin));
962f4d694ddSBarry Smith   PetscCall(VecRestoreArray(y, &yin));
9633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
964f4d694ddSBarry Smith }
965f4d694ddSBarry Smith 
966d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplyTranspose_BJacobi_Multiblock(PC pc, Vec x, Vec y)
967d71ae5a4SJacob Faibussowitsch {
9684b9ad928SBarry Smith   PC_BJacobi            *jac = (PC_BJacobi *)pc->data;
96969a612a9SBarry Smith   PetscInt               i, n_local = jac->n_local;
9704b9ad928SBarry Smith   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data;
971d9ca1df4SBarry Smith   PetscScalar           *yin;
972d9ca1df4SBarry Smith   const PetscScalar     *xin;
9734b9ad928SBarry Smith 
9744b9ad928SBarry Smith   PetscFunctionBegin;
9759566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &xin));
9769566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &yin));
9774b9ad928SBarry Smith   for (i = 0; i < n_local; i++) {
9784b9ad928SBarry Smith     /*
9794b9ad928SBarry Smith        To avoid copying the subvector from x into a workspace we instead
9804b9ad928SBarry Smith        make the workspace vector array point to the subpart of the array of
9814b9ad928SBarry Smith        the global vector.
9824b9ad928SBarry Smith     */
9839566063dSJacob Faibussowitsch     PetscCall(VecPlaceArray(bjac->x[i], xin + bjac->starts[i]));
9849566063dSJacob Faibussowitsch     PetscCall(VecPlaceArray(bjac->y[i], yin + bjac->starts[i]));
9854b9ad928SBarry Smith 
9869566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(PC_ApplyTransposeOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0));
9879566063dSJacob Faibussowitsch     PetscCall(KSPSolveTranspose(jac->ksp[i], bjac->x[i], bjac->y[i]));
9889566063dSJacob Faibussowitsch     PetscCall(KSPCheckSolve(jac->ksp[i], pc, bjac->y[i]));
9899566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_ApplyTransposeOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0));
990a7ff49e8SJed Brown 
9919566063dSJacob Faibussowitsch     PetscCall(VecResetArray(bjac->x[i]));
9929566063dSJacob Faibussowitsch     PetscCall(VecResetArray(bjac->y[i]));
9934b9ad928SBarry Smith   }
9949566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &xin));
9959566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &yin));
9963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9974b9ad928SBarry Smith }
9984b9ad928SBarry Smith 
999d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_BJacobi_Multiblock(PC pc, Mat mat, Mat pmat)
1000d71ae5a4SJacob Faibussowitsch {
10014b9ad928SBarry Smith   PC_BJacobi            *jac = (PC_BJacobi *)pc->data;
100269a612a9SBarry Smith   PetscInt               m, n_local, N, M, start, i;
1003ea41da7aSPierre Jolivet   const char            *prefix;
10044b9ad928SBarry Smith   KSP                    ksp;
10054b9ad928SBarry Smith   Vec                    x, y;
10064b9ad928SBarry Smith   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data;
10074b9ad928SBarry Smith   PC                     subpc;
10084b9ad928SBarry Smith   IS                     is;
1009434a97beSBrad Aagaard   MatReuse               scall;
10103f6dc190SJunchao Zhang   VecType                vectype;
10114849c82aSBarry Smith   MatNullSpace          *nullsp_mat = NULL, *nullsp_pmat = NULL;
10124b9ad928SBarry Smith 
10134b9ad928SBarry Smith   PetscFunctionBegin;
10149566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(pc->pmat, &M, &N));
10154b9ad928SBarry Smith 
10164b9ad928SBarry Smith   n_local = jac->n_local;
10174b9ad928SBarry Smith 
101849517cdeSBarry Smith   if (pc->useAmat) {
1019ace3abfcSBarry Smith     PetscBool same;
10209566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject)mat, ((PetscObject)pmat)->type_name, &same));
102128b400f6SJacob Faibussowitsch     PetscCheck(same, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Matrices not of same type");
10224b9ad928SBarry Smith   }
10234b9ad928SBarry Smith 
10244b9ad928SBarry Smith   if (!pc->setupcalled) {
10253821be0aSBarry Smith     PetscInt nestlevel;
10263821be0aSBarry Smith 
10274b9ad928SBarry Smith     scall = MAT_INITIAL_MATRIX;
1028c2efdce3SBarry Smith 
1029c2efdce3SBarry Smith     if (!jac->ksp) {
1030e91c6855SBarry Smith       pc->ops->reset               = PCReset_BJacobi_Multiblock;
10314b9ad928SBarry Smith       pc->ops->destroy             = PCDestroy_BJacobi_Multiblock;
10324b9ad928SBarry Smith       pc->ops->apply               = PCApply_BJacobi_Multiblock;
10337b6e2003SPierre Jolivet       pc->ops->matapply            = NULL;
1034*4dbf25a8SPierre Jolivet       pc->ops->matapplytranspose   = NULL;
1035f4d694ddSBarry Smith       pc->ops->applysymmetricleft  = PCApplySymmetricLeft_BJacobi_Multiblock;
1036f4d694ddSBarry Smith       pc->ops->applysymmetricright = PCApplySymmetricRight_BJacobi_Multiblock;
10374b9ad928SBarry Smith       pc->ops->applytranspose      = PCApplyTranspose_BJacobi_Multiblock;
10384b9ad928SBarry Smith       pc->ops->setuponblocks       = PCSetUpOnBlocks_BJacobi_Multiblock;
10394b9ad928SBarry Smith 
10404dfa11a4SJacob Faibussowitsch       PetscCall(PetscNew(&bjac));
10419566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n_local, &jac->ksp));
10429566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(n_local, &bjac->x, n_local, &bjac->y));
10439566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n_local, &bjac->starts));
10444b9ad928SBarry Smith 
10454b9ad928SBarry Smith       jac->data = (void *)bjac;
10469566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n_local, &bjac->is));
10474b9ad928SBarry Smith 
10484b9ad928SBarry Smith       for (i = 0; i < n_local; i++) {
10499566063dSJacob Faibussowitsch         PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp));
10503821be0aSBarry Smith         PetscCall(PCGetKSPNestLevel(pc, &nestlevel));
10513821be0aSBarry Smith         PetscCall(KSPSetNestLevel(ksp, nestlevel + 1));
10529566063dSJacob Faibussowitsch         PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
10539566063dSJacob Faibussowitsch         PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1));
10549566063dSJacob Faibussowitsch         PetscCall(KSPSetType(ksp, KSPPREONLY));
10559566063dSJacob Faibussowitsch         PetscCall(KSPGetPC(ksp, &subpc));
10569566063dSJacob Faibussowitsch         PetscCall(PCGetOptionsPrefix(pc, &prefix));
10579566063dSJacob Faibussowitsch         PetscCall(KSPSetOptionsPrefix(ksp, prefix));
10589566063dSJacob Faibussowitsch         PetscCall(KSPAppendOptionsPrefix(ksp, "sub_"));
10592fa5cd67SKarl Rupp 
1060c2efdce3SBarry Smith         jac->ksp[i] = ksp;
1061c2efdce3SBarry Smith       }
1062c2efdce3SBarry Smith     } else {
1063c2efdce3SBarry Smith       bjac = (PC_BJacobi_Multiblock *)jac->data;
1064c2efdce3SBarry Smith     }
10654b9ad928SBarry Smith 
1066c2efdce3SBarry Smith     start = 0;
10679566063dSJacob Faibussowitsch     PetscCall(MatGetVecType(pmat, &vectype));
1068c2efdce3SBarry Smith     for (i = 0; i < n_local; i++) {
10694b9ad928SBarry Smith       m = jac->l_lens[i];
10704b9ad928SBarry Smith       /*
10714b9ad928SBarry Smith       The reason we need to generate these vectors is to serve
10724b9ad928SBarry Smith       as the right-hand side and solution vector for the solve on the
10734b9ad928SBarry Smith       block. We do not need to allocate space for the vectors since
10744b9ad928SBarry Smith       that is provided via VecPlaceArray() just before the call to
10754b9ad928SBarry Smith       KSPSolve() on the block.
10764b9ad928SBarry Smith 
10774b9ad928SBarry Smith       */
10789566063dSJacob Faibussowitsch       PetscCall(VecCreateSeq(PETSC_COMM_SELF, m, &x));
10799566063dSJacob Faibussowitsch       PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, m, NULL, &y));
10809566063dSJacob Faibussowitsch       PetscCall(VecSetType(x, vectype));
10819566063dSJacob Faibussowitsch       PetscCall(VecSetType(y, vectype));
10822fa5cd67SKarl Rupp 
10834b9ad928SBarry Smith       bjac->x[i]      = x;
10844b9ad928SBarry Smith       bjac->y[i]      = y;
10854b9ad928SBarry Smith       bjac->starts[i] = start;
10864b9ad928SBarry Smith 
10879566063dSJacob Faibussowitsch       PetscCall(ISCreateStride(PETSC_COMM_SELF, m, start, 1, &is));
10884b9ad928SBarry Smith       bjac->is[i] = is;
10894b9ad928SBarry Smith 
10904b9ad928SBarry Smith       start += m;
10914b9ad928SBarry Smith     }
10924b9ad928SBarry Smith   } else {
10934b9ad928SBarry Smith     bjac = (PC_BJacobi_Multiblock *)jac->data;
10944b9ad928SBarry Smith     /*
10954b9ad928SBarry Smith        Destroy the blocks from the previous iteration
10964b9ad928SBarry Smith     */
10974b9ad928SBarry Smith     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
10984849c82aSBarry Smith       PetscCall(MatGetNullSpaces(n_local, bjac->pmat, &nullsp_pmat));
10999566063dSJacob Faibussowitsch       PetscCall(MatDestroyMatrices(n_local, &bjac->pmat));
11004849c82aSBarry Smith       if (pc->useAmat) {
11014849c82aSBarry Smith         PetscCall(MatGetNullSpaces(n_local, bjac->mat, &nullsp_mat));
11024849c82aSBarry Smith         PetscCall(MatDestroyMatrices(n_local, &bjac->mat));
11034849c82aSBarry Smith       }
11044b9ad928SBarry Smith       scall = MAT_INITIAL_MATRIX;
11052fa5cd67SKarl Rupp     } else scall = MAT_REUSE_MATRIX;
11064b9ad928SBarry Smith   }
11074b9ad928SBarry Smith 
11089566063dSJacob Faibussowitsch   PetscCall(MatCreateSubMatrices(pmat, n_local, bjac->is, bjac->is, scall, &bjac->pmat));
11094849c82aSBarry Smith   if (nullsp_pmat) PetscCall(MatRestoreNullSpaces(n_local, bjac->pmat, &nullsp_pmat));
11104849c82aSBarry Smith   if (pc->useAmat) {
11114849c82aSBarry Smith     PetscCall(MatCreateSubMatrices(mat, n_local, bjac->is, bjac->is, scall, &bjac->mat));
11124849c82aSBarry Smith     if (nullsp_mat) PetscCall(MatRestoreNullSpaces(n_local, bjac->mat, &nullsp_mat));
11134849c82aSBarry Smith   }
11144b9ad928SBarry Smith   /* Return control to the user so that the submatrices can be modified (e.g., to apply
11154b9ad928SBarry Smith      different boundary conditions for the submatrices than for the global problem) */
11169566063dSJacob Faibussowitsch   PetscCall(PCModifySubMatrices(pc, n_local, bjac->is, bjac->is, bjac->pmat, pc->modifysubmatricesP));
11174b9ad928SBarry Smith 
11184b9ad928SBarry Smith   for (i = 0; i < n_local; i++) {
11199566063dSJacob Faibussowitsch     PetscCall(KSPGetOptionsPrefix(jac->ksp[i], &prefix));
112049517cdeSBarry Smith     if (pc->useAmat) {
11219566063dSJacob Faibussowitsch       PetscCall(KSPSetOperators(jac->ksp[i], bjac->mat[i], bjac->pmat[i]));
11229566063dSJacob Faibussowitsch       PetscCall(MatSetOptionsPrefix(bjac->mat[i], prefix));
11234b9ad928SBarry Smith     } else {
11249566063dSJacob Faibussowitsch       PetscCall(KSPSetOperators(jac->ksp[i], bjac->pmat[i], bjac->pmat[i]));
11254b9ad928SBarry Smith     }
11269566063dSJacob Faibussowitsch     PetscCall(MatSetOptionsPrefix(bjac->pmat[i], prefix));
112748a46eb9SPierre Jolivet     if (pc->setfromoptionscalled) PetscCall(KSPSetFromOptions(jac->ksp[i]));
112890f1c854SHong Zhang   }
11293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11304b9ad928SBarry Smith }
11315a7e1895SHong Zhang 
11325a7e1895SHong Zhang /*
1133fd0b8932SBarry Smith       These are for a single block with multiple processes
11345a7e1895SHong Zhang */
1135d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUpOnBlocks_BJacobi_Multiproc(PC pc)
1136d71ae5a4SJacob Faibussowitsch {
1137fd0b8932SBarry Smith   PC_BJacobi        *jac    = (PC_BJacobi *)pc->data;
1138fd0b8932SBarry Smith   KSP                subksp = jac->ksp[0];
1139fd0b8932SBarry Smith   KSPConvergedReason reason;
1140fd0b8932SBarry Smith 
1141fd0b8932SBarry Smith   PetscFunctionBegin;
11429566063dSJacob Faibussowitsch   PetscCall(KSPSetUp(subksp));
11439566063dSJacob Faibussowitsch   PetscCall(KSPGetConvergedReason(subksp, &reason));
1144ad540459SPierre Jolivet   if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
11453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1146fd0b8932SBarry Smith }
1147fd0b8932SBarry Smith 
1148d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_BJacobi_Multiproc(PC pc)
1149d71ae5a4SJacob Faibussowitsch {
11505a7e1895SHong Zhang   PC_BJacobi           *jac   = (PC_BJacobi *)pc->data;
11515a7e1895SHong Zhang   PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc *)jac->data;
11525a7e1895SHong Zhang 
11535a7e1895SHong Zhang   PetscFunctionBegin;
11549566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&mpjac->ysub));
11559566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&mpjac->xsub));
11569566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&mpjac->submats));
11579566063dSJacob Faibussowitsch   if (jac->ksp) PetscCall(KSPReset(jac->ksp[0]));
11583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1159e91c6855SBarry Smith }
1160e91c6855SBarry Smith 
1161d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_BJacobi_Multiproc(PC pc)
1162d71ae5a4SJacob Faibussowitsch {
1163e91c6855SBarry Smith   PC_BJacobi           *jac   = (PC_BJacobi *)pc->data;
1164e91c6855SBarry Smith   PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc *)jac->data;
1165e91c6855SBarry Smith 
1166e91c6855SBarry Smith   PetscFunctionBegin;
11679566063dSJacob Faibussowitsch   PetscCall(PCReset_BJacobi_Multiproc(pc));
11689566063dSJacob Faibussowitsch   PetscCall(KSPDestroy(&jac->ksp[0]));
11699566063dSJacob Faibussowitsch   PetscCall(PetscFree(jac->ksp));
11709566063dSJacob Faibussowitsch   PetscCall(PetscSubcommDestroy(&mpjac->psubcomm));
11715a7e1895SHong Zhang 
11729566063dSJacob Faibussowitsch   PetscCall(PetscFree(mpjac));
11732e956fe4SStefano Zampini   PetscCall(PCDestroy_BJacobi(pc));
11743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11755a7e1895SHong Zhang }
11765a7e1895SHong Zhang 
1177d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_BJacobi_Multiproc(PC pc, Vec x, Vec y)
1178d71ae5a4SJacob Faibussowitsch {
11795a7e1895SHong Zhang   PC_BJacobi           *jac   = (PC_BJacobi *)pc->data;
11805a7e1895SHong Zhang   PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc *)jac->data;
1181d9ca1df4SBarry Smith   PetscScalar          *yarray;
1182d9ca1df4SBarry Smith   const PetscScalar    *xarray;
1183539c167fSBarry Smith   KSPConvergedReason    reason;
11845a7e1895SHong Zhang 
11855a7e1895SHong Zhang   PetscFunctionBegin;
11865a7e1895SHong Zhang   /* place x's and y's local arrays into xsub and ysub */
11879566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &xarray));
11889566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &yarray));
11899566063dSJacob Faibussowitsch   PetscCall(VecPlaceArray(mpjac->xsub, xarray));
11909566063dSJacob Faibussowitsch   PetscCall(VecPlaceArray(mpjac->ysub, yarray));
11915a7e1895SHong Zhang 
11925a7e1895SHong Zhang   /* apply preconditioner on each matrix block */
11939566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_ApplyOnBlocks, jac->ksp[0], mpjac->xsub, mpjac->ysub, 0));
11949566063dSJacob Faibussowitsch   PetscCall(KSPSolve(jac->ksp[0], mpjac->xsub, mpjac->ysub));
11959566063dSJacob Faibussowitsch   PetscCall(KSPCheckSolve(jac->ksp[0], pc, mpjac->ysub));
11969566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_ApplyOnBlocks, jac->ksp[0], mpjac->xsub, mpjac->ysub, 0));
11979566063dSJacob Faibussowitsch   PetscCall(KSPGetConvergedReason(jac->ksp[0], &reason));
1198ad540459SPierre Jolivet   if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
1199250cf88bSHong Zhang 
12009566063dSJacob Faibussowitsch   PetscCall(VecResetArray(mpjac->xsub));
12019566063dSJacob Faibussowitsch   PetscCall(VecResetArray(mpjac->ysub));
12029566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &xarray));
12039566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &yarray));
12043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12055a7e1895SHong Zhang }
12065a7e1895SHong Zhang 
1207d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCMatApply_BJacobi_Multiproc(PC pc, Mat X, Mat Y)
1208d71ae5a4SJacob Faibussowitsch {
12097b6e2003SPierre Jolivet   PC_BJacobi        *jac = (PC_BJacobi *)pc->data;
12107b6e2003SPierre Jolivet   KSPConvergedReason reason;
12117b6e2003SPierre Jolivet   Mat                sX, sY;
12127b6e2003SPierre Jolivet   const PetscScalar *x;
12137b6e2003SPierre Jolivet   PetscScalar       *y;
12147b6e2003SPierre Jolivet   PetscInt           m, N, lda, ldb;
12157b6e2003SPierre Jolivet 
12167b6e2003SPierre Jolivet   PetscFunctionBegin;
12177b6e2003SPierre Jolivet   /* apply preconditioner on each matrix block */
12189566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(X, &m, NULL));
12199566063dSJacob Faibussowitsch   PetscCall(MatGetSize(X, NULL, &N));
12209566063dSJacob Faibussowitsch   PetscCall(MatDenseGetLDA(X, &lda));
12219566063dSJacob Faibussowitsch   PetscCall(MatDenseGetLDA(Y, &ldb));
12229566063dSJacob Faibussowitsch   PetscCall(MatDenseGetArrayRead(X, &x));
12239566063dSJacob Faibussowitsch   PetscCall(MatDenseGetArrayWrite(Y, &y));
12249566063dSJacob Faibussowitsch   PetscCall(MatCreateDense(PetscObjectComm((PetscObject)jac->ksp[0]), m, PETSC_DECIDE, PETSC_DECIDE, N, (PetscScalar *)x, &sX));
12259566063dSJacob Faibussowitsch   PetscCall(MatCreateDense(PetscObjectComm((PetscObject)jac->ksp[0]), m, PETSC_DECIDE, PETSC_DECIDE, N, y, &sY));
12269566063dSJacob Faibussowitsch   PetscCall(MatDenseSetLDA(sX, lda));
12279566063dSJacob Faibussowitsch   PetscCall(MatDenseSetLDA(sY, ldb));
12289566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_ApplyOnBlocks, jac->ksp[0], X, Y, 0));
12299566063dSJacob Faibussowitsch   PetscCall(KSPMatSolve(jac->ksp[0], sX, sY));
12309566063dSJacob Faibussowitsch   PetscCall(KSPCheckSolve(jac->ksp[0], pc, NULL));
12319566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_ApplyOnBlocks, jac->ksp[0], X, Y, 0));
12329566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&sY));
12339566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&sX));
12349566063dSJacob Faibussowitsch   PetscCall(MatDenseRestoreArrayWrite(Y, &y));
12359566063dSJacob Faibussowitsch   PetscCall(MatDenseRestoreArrayRead(X, &x));
12369566063dSJacob Faibussowitsch   PetscCall(KSPGetConvergedReason(jac->ksp[0], &reason));
1237ad540459SPierre Jolivet   if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
12383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12397b6e2003SPierre Jolivet }
12407b6e2003SPierre Jolivet 
1241d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_BJacobi_Multiproc(PC pc)
1242d71ae5a4SJacob Faibussowitsch {
12435a7e1895SHong Zhang   PC_BJacobi           *jac   = (PC_BJacobi *)pc->data;
12445a7e1895SHong Zhang   PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc *)jac->data;
12455a7e1895SHong Zhang   PetscInt              m, n;
1246ce94432eSBarry Smith   MPI_Comm              comm, subcomm = 0;
12475a7e1895SHong Zhang   const char           *prefix;
1248de0953f6SBarry Smith   PetscBool             wasSetup = PETSC_TRUE;
12493f6dc190SJunchao Zhang   VecType               vectype;
12501f62890fSKarl Rupp 
12515a7e1895SHong Zhang   PetscFunctionBegin;
12529566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
125308401ef6SPierre Jolivet   PetscCheck(jac->n_local <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Only a single block in a subcommunicator is supported");
12545a7e1895SHong Zhang   jac->n_local = 1; /* currently only a single block is supported for a subcommunicator */
12555a7e1895SHong Zhang   if (!pc->setupcalled) {
12563821be0aSBarry Smith     PetscInt nestlevel;
12573821be0aSBarry Smith 
1258de0953f6SBarry Smith     wasSetup = PETSC_FALSE;
12594dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&mpjac));
12605a7e1895SHong Zhang     jac->data = (void *)mpjac;
12615a7e1895SHong Zhang 
12625a7e1895SHong Zhang     /* initialize datastructure mpjac */
12635a7e1895SHong Zhang     if (!jac->psubcomm) {
12645a7e1895SHong Zhang       /* Create default contiguous subcommunicatiors if user does not provide them */
12659566063dSJacob Faibussowitsch       PetscCall(PetscSubcommCreate(comm, &jac->psubcomm));
12669566063dSJacob Faibussowitsch       PetscCall(PetscSubcommSetNumber(jac->psubcomm, jac->n));
12679566063dSJacob Faibussowitsch       PetscCall(PetscSubcommSetType(jac->psubcomm, PETSC_SUBCOMM_CONTIGUOUS));
12685a7e1895SHong Zhang     }
12695a7e1895SHong Zhang     mpjac->psubcomm = jac->psubcomm;
1270306c2d5bSBarry Smith     subcomm         = PetscSubcommChild(mpjac->psubcomm);
12715a7e1895SHong Zhang 
12725a7e1895SHong Zhang     /* Get matrix blocks of pmat */
12739566063dSJacob Faibussowitsch     PetscCall(MatGetMultiProcBlock(pc->pmat, subcomm, MAT_INITIAL_MATRIX, &mpjac->submats));
12745a7e1895SHong Zhang 
12755a7e1895SHong Zhang     /* create a new PC that processors in each subcomm have copy of */
12769566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(1, &jac->ksp));
12779566063dSJacob Faibussowitsch     PetscCall(KSPCreate(subcomm, &jac->ksp[0]));
12783821be0aSBarry Smith     PetscCall(PCGetKSPNestLevel(pc, &nestlevel));
12793821be0aSBarry Smith     PetscCall(KSPSetNestLevel(jac->ksp[0], nestlevel + 1));
12809566063dSJacob Faibussowitsch     PetscCall(KSPSetErrorIfNotConverged(jac->ksp[0], pc->erroriffailure));
12819566063dSJacob Faibussowitsch     PetscCall(PetscObjectIncrementTabLevel((PetscObject)jac->ksp[0], (PetscObject)pc, 1));
12829566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(jac->ksp[0], mpjac->submats, mpjac->submats));
12839566063dSJacob Faibussowitsch     PetscCall(KSPGetPC(jac->ksp[0], &mpjac->pc));
12845a7e1895SHong Zhang 
12859566063dSJacob Faibussowitsch     PetscCall(PCGetOptionsPrefix(pc, &prefix));
12869566063dSJacob Faibussowitsch     PetscCall(KSPSetOptionsPrefix(jac->ksp[0], prefix));
12879566063dSJacob Faibussowitsch     PetscCall(KSPAppendOptionsPrefix(jac->ksp[0], "sub_"));
12889566063dSJacob Faibussowitsch     PetscCall(KSPGetOptionsPrefix(jac->ksp[0], &prefix));
12899566063dSJacob Faibussowitsch     PetscCall(MatSetOptionsPrefix(mpjac->submats, prefix));
12905a7e1895SHong Zhang 
12915a7e1895SHong Zhang     /* create dummy vectors xsub and ysub */
12929566063dSJacob Faibussowitsch     PetscCall(MatGetLocalSize(mpjac->submats, &m, &n));
12939566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(subcomm, 1, n, PETSC_DECIDE, NULL, &mpjac->xsub));
12949566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(subcomm, 1, m, PETSC_DECIDE, NULL, &mpjac->ysub));
12959566063dSJacob Faibussowitsch     PetscCall(MatGetVecType(mpjac->submats, &vectype));
12969566063dSJacob Faibussowitsch     PetscCall(VecSetType(mpjac->xsub, vectype));
12979566063dSJacob Faibussowitsch     PetscCall(VecSetType(mpjac->ysub, vectype));
12985a7e1895SHong Zhang 
1299fd0b8932SBarry Smith     pc->ops->setuponblocks = PCSetUpOnBlocks_BJacobi_Multiproc;
1300e91c6855SBarry Smith     pc->ops->reset         = PCReset_BJacobi_Multiproc;
13015a7e1895SHong Zhang     pc->ops->destroy       = PCDestroy_BJacobi_Multiproc;
13025a7e1895SHong Zhang     pc->ops->apply         = PCApply_BJacobi_Multiproc;
13037b6e2003SPierre Jolivet     pc->ops->matapply      = PCMatApply_BJacobi_Multiproc;
1304fc08c53fSHong Zhang   } else { /* pc->setupcalled */
1305306c2d5bSBarry Smith     subcomm = PetscSubcommChild(mpjac->psubcomm);
13069e0ae222SHong Zhang     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
13075a7e1895SHong Zhang       /* destroy old matrix blocks, then get new matrix blocks */
13089566063dSJacob Faibussowitsch       if (mpjac->submats) PetscCall(MatDestroy(&mpjac->submats));
13099566063dSJacob Faibussowitsch       PetscCall(MatGetMultiProcBlock(pc->pmat, subcomm, MAT_INITIAL_MATRIX, &mpjac->submats));
1310fc08c53fSHong Zhang     } else {
13119566063dSJacob Faibussowitsch       PetscCall(MatGetMultiProcBlock(pc->pmat, subcomm, MAT_REUSE_MATRIX, &mpjac->submats));
13125a7e1895SHong Zhang     }
13139566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(jac->ksp[0], mpjac->submats, mpjac->submats));
13145a7e1895SHong Zhang   }
13155a7e1895SHong Zhang 
131648a46eb9SPierre Jolivet   if (!wasSetup && pc->setfromoptionscalled) PetscCall(KSPSetFromOptions(jac->ksp[0]));
13173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13185a7e1895SHong Zhang }
1319