xref: /petsc/src/ksp/pc/impls/redundant/redundant.c (revision 97929ea760a70c780fc4b78d3065eb9e422113fe)
1dba47a55SKris Buschelman 
24b9ad928SBarry Smith /*
33f457be1SHong Zhang   This file defines a "solve the problem redundantly on each subgroup of processor" preconditioner.
44b9ad928SBarry Smith */
5af0996ceSBarry Smith #include <petsc/private/pcimpl.h>
6c6db04a5SJed Brown #include <petscksp.h>           /*I "petscksp.h" I*/
74b9ad928SBarry Smith 
84b9ad928SBarry Smith typedef struct {
93e065800SHong Zhang   KSP                ksp;
104b9ad928SBarry Smith   PC                 pc;                   /* actual preconditioner used on each processor */
11ce94432eSBarry Smith   Vec                xsub,ysub;            /* vectors of a subcommunicator to hold parallel vectors of PetscObjectComm((PetscObject)pc) */
123f457be1SHong Zhang   Vec                xdup,ydup;            /* parallel vector that congregates xsub or ysub facilitating vector scattering */
13b3804887SHong Zhang   Mat                pmats;                /* matrix and optional preconditioner matrix belong to a subcommunicator */
143f457be1SHong Zhang   VecScatter         scatterin,scatterout; /* scatter used to move all values to each processor group (subcommunicator) */
15ace3abfcSBarry Smith   PetscBool          useparallelmat;
16c540e29cSHong Zhang   PetscSubcomm       psubcomm;
171fbd8f88SHong Zhang   PetscInt           nsubcomm;             /* num of data structure PetscSubcomm */
18753b7fb9SBarry Smith   PetscBool          shifttypeset;
19753b7fb9SBarry Smith   MatFactorShiftType shifttype;
204b9ad928SBarry Smith } PC_Redundant;
214b9ad928SBarry Smith 
22753b7fb9SBarry Smith PetscErrorCode  PCFactorSetShiftType_Redundant(PC pc,MatFactorShiftType shifttype)
23753b7fb9SBarry Smith {
24753b7fb9SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
25753b7fb9SBarry Smith   PetscErrorCode ierr;
26753b7fb9SBarry Smith 
27753b7fb9SBarry Smith   PetscFunctionBegin;
28753b7fb9SBarry Smith   if (red->ksp) {
29753b7fb9SBarry Smith     PC pc;
30753b7fb9SBarry Smith     ierr = KSPGetPC(red->ksp,&pc);CHKERRQ(ierr);
31753b7fb9SBarry Smith     ierr = PCFactorSetShiftType(pc,shifttype);CHKERRQ(ierr);
32753b7fb9SBarry Smith   } else {
33753b7fb9SBarry Smith     red->shifttypeset = PETSC_TRUE;
34753b7fb9SBarry Smith     red->shifttype    = shifttype;
35753b7fb9SBarry Smith   }
36753b7fb9SBarry Smith   PetscFunctionReturn(0);
37753b7fb9SBarry Smith }
38753b7fb9SBarry Smith 
396849ba73SBarry Smith static PetscErrorCode PCView_Redundant(PC pc,PetscViewer viewer)
404b9ad928SBarry Smith {
414b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
42dfbe8321SBarry Smith   PetscErrorCode ierr;
43ace3abfcSBarry Smith   PetscBool      iascii,isstring;
4403ccd0b4SBarry Smith   PetscViewer    subviewer;
454b9ad928SBarry Smith 
464b9ad928SBarry Smith   PetscFunctionBegin;
47251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
48251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
4932077d6dSBarry Smith   if (iascii) {
5003ccd0b4SBarry Smith     if (!red->psubcomm) {
51efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Not yet setup\n");CHKERRQ(ierr);
5203ccd0b4SBarry Smith     } else {
53efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  First (color=0) of %D PCs follows\n",red->nsubcomm);CHKERRQ(ierr);
543f08860eSBarry Smith       ierr = PetscViewerGetSubViewer(viewer,((PetscObject)red->pc)->comm,&subviewer);CHKERRQ(ierr);
55f5dd71faSBarry Smith       if (!red->psubcomm->color) { /* only view first redundant pc */
561575c14dSBarry Smith         ierr = PetscViewerASCIIPushTab(subviewer);CHKERRQ(ierr);
573e065800SHong Zhang         ierr = KSPView(red->ksp,subviewer);CHKERRQ(ierr);
581575c14dSBarry Smith         ierr = PetscViewerASCIIPopTab(subviewer);CHKERRQ(ierr);
594b9ad928SBarry Smith       }
603f08860eSBarry Smith       ierr = PetscViewerRestoreSubViewer(viewer,((PetscObject)red->pc)->comm,&subviewer);CHKERRQ(ierr);
614b9ad928SBarry Smith     }
6203ccd0b4SBarry Smith   } else if (isstring) {
6303ccd0b4SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," Redundant solver preconditioner");CHKERRQ(ierr);
644b9ad928SBarry Smith   }
654b9ad928SBarry Smith   PetscFunctionReturn(0);
664b9ad928SBarry Smith }
674b9ad928SBarry Smith 
6819b3b6edSHong Zhang #include <../src/mat/impls/aij/mpi/mpiaij.h>
696849ba73SBarry Smith static PetscErrorCode PCSetUp_Redundant(PC pc)
704b9ad928SBarry Smith {
714b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
72dfbe8321SBarry Smith   PetscErrorCode ierr;
731b81debcSHong Zhang   PetscInt       mstart,mend,mlocal,M;
7413f74950SBarry Smith   PetscMPIInt    size;
75ce94432eSBarry Smith   MPI_Comm       comm,subcomm;
76ddc54837SHong Zhang   Vec            x;
773f457be1SHong Zhang 
784b9ad928SBarry Smith   PetscFunctionBegin;
79ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
80ddc54837SHong Zhang 
81ddc54837SHong Zhang   /* if pmatrix set by user is sequential then we do not need to gather the parallel matrix */
82ddc54837SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
83ddc54837SHong Zhang   if (size == 1) red->useparallelmat = PETSC_FALSE;
841fbd8f88SHong Zhang 
854b9ad928SBarry Smith   if (!pc->setupcalled) {
861b81debcSHong Zhang     PetscInt mloc_sub;
8775024027SHong Zhang     if (!red->psubcomm) { /* create red->psubcomm, new ksp and pc over subcomm */
8875024027SHong Zhang       KSP ksp;
8975024027SHong Zhang       ierr = PCRedundantGetKSP(pc,&ksp);CHKERRQ(ierr);
901b81debcSHong Zhang     }
9175024027SHong Zhang     subcomm = PetscSubcommChild(red->psubcomm);
921fbd8f88SHong Zhang 
931b81debcSHong Zhang     if (red->useparallelmat) {
941b81debcSHong Zhang       /* grab the parallel matrix and put it into processors of a subcomminicator */
9553cd1579SHong Zhang       ierr = MatCreateRedundantMatrix(pc->pmat,red->psubcomm->n,subcomm,MAT_INITIAL_MATRIX,&red->pmats);CHKERRQ(ierr);
96b85f2e9bSHong Zhang 
97b85f2e9bSHong Zhang       ierr = MPI_Comm_size(subcomm,&size);CHKERRQ(ierr);
98b85f2e9bSHong Zhang       if (size > 1) {
9908cecb0aSPierre Jolivet         PetscBool foundpack,issbaij;
10008cecb0aSPierre Jolivet         ierr = PetscObjectTypeCompare((PetscObject)red->pmats,MATMPISBAIJ,&issbaij);CHKERRQ(ierr);
10108cecb0aSPierre Jolivet         if (!issbaij) {
102b85f2e9bSHong Zhang           ierr = MatGetFactorAvailable(red->pmats,NULL,MAT_FACTOR_LU,&foundpack);CHKERRQ(ierr);
10308cecb0aSPierre Jolivet         } else {
10408cecb0aSPierre Jolivet           ierr = MatGetFactorAvailable(red->pmats,NULL,MAT_FACTOR_CHOLESKY,&foundpack);CHKERRQ(ierr);
10508cecb0aSPierre Jolivet         }
106b85f2e9bSHong Zhang         if (!foundpack) { /* reset default ksp and pc */
107b85f2e9bSHong Zhang           ierr = KSPSetType(red->ksp,KSPGMRES);CHKERRQ(ierr);
108b85f2e9bSHong Zhang           ierr = PCSetType(red->pc,PCBJACOBI);CHKERRQ(ierr);
109c1619fb6SBarry Smith         } else {
1103ca39a21SBarry Smith           ierr = PCFactorSetMatSolverType(red->pc,NULL);CHKERRQ(ierr);
111b85f2e9bSHong Zhang         }
112b85f2e9bSHong Zhang       }
113b85f2e9bSHong Zhang 
11423ee1639SBarry Smith       ierr = KSPSetOperators(red->ksp,red->pmats,red->pmats);CHKERRQ(ierr);
1154b9ad928SBarry Smith 
1161b81debcSHong Zhang       /* get working vectors xsub and ysub */
1172a7a6963SBarry Smith       ierr = MatCreateVecs(red->pmats,&red->xsub,&red->ysub);CHKERRQ(ierr);
1182fa5cd67SKarl Rupp 
1198b96b0d2SHong Zhang       /* create working vectors xdup and ydup.
1208b96b0d2SHong Zhang        xdup concatenates all xsub's contigously to form a mpi vector over dupcomm  (see PetscSubcommCreate_interlaced())
1218b96b0d2SHong Zhang        ydup concatenates all ysub and has empty local arrays because ysub's arrays will be place into it.
122ce94432eSBarry Smith        Note: we use communicator dupcomm, not PetscObjectComm((PetscObject)pc)! */
1231b81debcSHong Zhang       ierr = MatGetLocalSize(red->pmats,&mloc_sub,NULL);CHKERRQ(ierr);
12436be1a5eSBarry Smith       ierr = VecCreateMPI(PetscSubcommContiguousParent(red->psubcomm),mloc_sub,PETSC_DECIDE,&red->xdup);CHKERRQ(ierr);
12536be1a5eSBarry Smith       ierr = VecCreateMPIWithArray(PetscSubcommContiguousParent(red->psubcomm),1,mloc_sub,PETSC_DECIDE,NULL,&red->ydup);CHKERRQ(ierr);
1263f457be1SHong Zhang 
127f68be91cSHong Zhang       /* create vecscatters */
128f68be91cSHong Zhang       if (!red->scatterin) { /* efficiency of scatterin is independent from psubcomm_type! */
1293f457be1SHong Zhang         IS       is1,is2;
1303f457be1SHong Zhang         PetscInt *idx1,*idx2,i,j,k;
13145fc02eaSBarry Smith 
1320a545947SLisandro Dalcin         ierr = MatCreateVecs(pc->pmat,&x,NULL);CHKERRQ(ierr);
1331b81debcSHong Zhang         ierr = VecGetSize(x,&M);CHKERRQ(ierr);
1341b81debcSHong Zhang         ierr = VecGetOwnershipRange(x,&mstart,&mend);CHKERRQ(ierr);
1351b81debcSHong Zhang         mlocal = mend - mstart;
136dcca6d9dSJed Brown         ierr = PetscMalloc2(red->psubcomm->n*mlocal,&idx1,red->psubcomm->n*mlocal,&idx2);CHKERRQ(ierr);
1373f457be1SHong Zhang         j    = 0;
1381fbd8f88SHong Zhang         for (k=0; k<red->psubcomm->n; k++) {
1393f457be1SHong Zhang           for (i=mstart; i<mend; i++) {
1403f457be1SHong Zhang             idx1[j]   = i;
141ddc54837SHong Zhang             idx2[j++] = i + M*k;
1423f457be1SHong Zhang           }
1433f457be1SHong Zhang         }
14470b3c8c7SBarry Smith         ierr = ISCreateGeneral(comm,red->psubcomm->n*mlocal,idx1,PETSC_COPY_VALUES,&is1);CHKERRQ(ierr);
14570b3c8c7SBarry Smith         ierr = ISCreateGeneral(comm,red->psubcomm->n*mlocal,idx2,PETSC_COPY_VALUES,&is2);CHKERRQ(ierr);
1469448b7f1SJunchao Zhang         ierr = VecScatterCreate(x,is1,red->xdup,is2,&red->scatterin);CHKERRQ(ierr);
147fcfd50ebSBarry Smith         ierr = ISDestroy(&is1);CHKERRQ(ierr);
148fcfd50ebSBarry Smith         ierr = ISDestroy(&is2);CHKERRQ(ierr);
1493f457be1SHong Zhang 
1506909748bSHong Zhang         /* Impl below is good for PETSC_SUBCOMM_INTERLACED (no inter-process communication) and PETSC_SUBCOMM_CONTIGUOUS (communication within subcomm) */
151ddc54837SHong Zhang         ierr = ISCreateStride(comm,mlocal,mstart+ red->psubcomm->color*M,1,&is1);CHKERRQ(ierr);
1523f457be1SHong Zhang         ierr = ISCreateStride(comm,mlocal,mstart,1,&is2);CHKERRQ(ierr);
1539448b7f1SJunchao Zhang         ierr = VecScatterCreate(red->xdup,is1,x,is2,&red->scatterout);CHKERRQ(ierr);
154fcfd50ebSBarry Smith         ierr = ISDestroy(&is1);CHKERRQ(ierr);
155fcfd50ebSBarry Smith         ierr = ISDestroy(&is2);CHKERRQ(ierr);
1561d79065fSBarry Smith         ierr = PetscFree2(idx1,idx2);CHKERRQ(ierr);
157ddc54837SHong Zhang         ierr = VecDestroy(&x);CHKERRQ(ierr);
1581b81debcSHong Zhang       }
159ab661555SHong Zhang     } else { /* !red->useparallelmat */
16023ee1639SBarry Smith       ierr = KSPSetOperators(red->ksp,pc->mat,pc->pmat);CHKERRQ(ierr);
1611b81debcSHong Zhang     }
162ab661555SHong Zhang   } else { /* pc->setupcalled */
1634b9ad928SBarry Smith     if (red->useparallelmat) {
164ab661555SHong Zhang       MatReuse       reuse;
1651b81debcSHong Zhang       /* grab the parallel matrix and put it into processors of a subcomminicator */
1661b81debcSHong Zhang       /*--------------------------------------------------------------------------*/
167ab661555SHong Zhang       if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
1684b9ad928SBarry Smith         /* destroy old matrices */
1696bf464f9SBarry Smith         ierr  = MatDestroy(&red->pmats);CHKERRQ(ierr);
170ab661555SHong Zhang         reuse = MAT_INITIAL_MATRIX;
1714b9ad928SBarry Smith       } else {
172ab661555SHong Zhang         reuse = MAT_REUSE_MATRIX;
173ab661555SHong Zhang       }
174306c2d5bSBarry Smith       ierr = MatCreateRedundantMatrix(pc->pmat,red->psubcomm->n,PetscSubcommChild(red->psubcomm),reuse,&red->pmats);CHKERRQ(ierr);
17523ee1639SBarry Smith       ierr = KSPSetOperators(red->ksp,red->pmats,red->pmats);CHKERRQ(ierr);
176ab661555SHong Zhang     } else { /* !red->useparallelmat */
17723ee1639SBarry Smith       ierr = KSPSetOperators(red->ksp,pc->mat,pc->pmat);CHKERRQ(ierr);
1784b9ad928SBarry Smith     }
179ab661555SHong Zhang   }
1801b81debcSHong Zhang 
1810c24e6a1SHong Zhang   if (pc->setfromoptionscalled) {
1823e065800SHong Zhang     ierr = KSPSetFromOptions(red->ksp);CHKERRQ(ierr);
1830c24e6a1SHong Zhang   }
1843e065800SHong Zhang   ierr = KSPSetUp(red->ksp);CHKERRQ(ierr);
1854b9ad928SBarry Smith   PetscFunctionReturn(0);
1864b9ad928SBarry Smith }
1874b9ad928SBarry Smith 
1886849ba73SBarry Smith static PetscErrorCode PCApply_Redundant(PC pc,Vec x,Vec y)
1894b9ad928SBarry Smith {
1904b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
191dfbe8321SBarry Smith   PetscErrorCode ierr;
1923f457be1SHong Zhang   PetscScalar    *array;
1934b9ad928SBarry Smith 
1944b9ad928SBarry Smith   PetscFunctionBegin;
195ddc54837SHong Zhang   if (!red->useparallelmat) {
196ddc54837SHong Zhang     ierr = KSPSolve(red->ksp,x,y);CHKERRQ(ierr);
197c0decd05SBarry Smith     ierr = KSPCheckSolve(red->ksp,pc,y);CHKERRQ(ierr);
198ddc54837SHong Zhang     PetscFunctionReturn(0);
199ddc54837SHong Zhang   }
200ddc54837SHong Zhang 
2013f457be1SHong Zhang   /* scatter x to xdup */
202ca9f406cSSatish Balay   ierr = VecScatterBegin(red->scatterin,x,red->xdup,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
203ca9f406cSSatish Balay   ierr = VecScatterEnd(red->scatterin,x,red->xdup,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2043f457be1SHong Zhang 
2053f457be1SHong Zhang   /* place xdup's local array into xsub */
2063f457be1SHong Zhang   ierr = VecGetArray(red->xdup,&array);CHKERRQ(ierr);
2073f457be1SHong Zhang   ierr = VecPlaceArray(red->xsub,(const PetscScalar*)array);CHKERRQ(ierr);
2084b9ad928SBarry Smith 
2094b9ad928SBarry Smith   /* apply preconditioner on each processor */
21083ab6a24SBarry Smith   ierr = KSPSolve(red->ksp,red->xsub,red->ysub);CHKERRQ(ierr);
211c0decd05SBarry Smith   ierr = KSPCheckSolve(red->ksp,pc,red->ysub);CHKERRQ(ierr);
2123f457be1SHong Zhang   ierr = VecResetArray(red->xsub);CHKERRQ(ierr);
2133f457be1SHong Zhang   ierr = VecRestoreArray(red->xdup,&array);CHKERRQ(ierr);
2144b9ad928SBarry Smith 
2153f457be1SHong Zhang   /* place ysub's local array into ydup */
2163f457be1SHong Zhang   ierr = VecGetArray(red->ysub,&array);CHKERRQ(ierr);
2173f457be1SHong Zhang   ierr = VecPlaceArray(red->ydup,(const PetscScalar*)array);CHKERRQ(ierr);
2183f457be1SHong Zhang 
2193f457be1SHong Zhang   /* scatter ydup to y */
220ca9f406cSSatish Balay   ierr = VecScatterBegin(red->scatterout,red->ydup,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
221ca9f406cSSatish Balay   ierr = VecScatterEnd(red->scatterout,red->ydup,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2223f457be1SHong Zhang   ierr = VecResetArray(red->ydup);CHKERRQ(ierr);
2233f457be1SHong Zhang   ierr = VecRestoreArray(red->ysub,&array);CHKERRQ(ierr);
2244b9ad928SBarry Smith   PetscFunctionReturn(0);
2254b9ad928SBarry Smith }
2264b9ad928SBarry Smith 
227d88bfacbSStefano Zampini static PetscErrorCode PCApplyTranspose_Redundant(PC pc,Vec x,Vec y)
228d88bfacbSStefano Zampini {
229d88bfacbSStefano Zampini   PC_Redundant   *red = (PC_Redundant*)pc->data;
230d88bfacbSStefano Zampini   PetscErrorCode ierr;
231d88bfacbSStefano Zampini   PetscScalar    *array;
232d88bfacbSStefano Zampini 
233d88bfacbSStefano Zampini   PetscFunctionBegin;
234d88bfacbSStefano Zampini   if (!red->useparallelmat) {
235d88bfacbSStefano Zampini     ierr = KSPSolveTranspose(red->ksp,x,y);CHKERRQ(ierr);
236c0decd05SBarry Smith     ierr = KSPCheckSolve(red->ksp,pc,y);CHKERRQ(ierr);
237d88bfacbSStefano Zampini     PetscFunctionReturn(0);
238d88bfacbSStefano Zampini   }
239d88bfacbSStefano Zampini 
240d88bfacbSStefano Zampini   /* scatter x to xdup */
241d88bfacbSStefano Zampini   ierr = VecScatterBegin(red->scatterin,x,red->xdup,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
242d88bfacbSStefano Zampini   ierr = VecScatterEnd(red->scatterin,x,red->xdup,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
243d88bfacbSStefano Zampini 
244d88bfacbSStefano Zampini   /* place xdup's local array into xsub */
245d88bfacbSStefano Zampini   ierr = VecGetArray(red->xdup,&array);CHKERRQ(ierr);
246d88bfacbSStefano Zampini   ierr = VecPlaceArray(red->xsub,(const PetscScalar*)array);CHKERRQ(ierr);
247d88bfacbSStefano Zampini 
248d88bfacbSStefano Zampini   /* apply preconditioner on each processor */
249d88bfacbSStefano Zampini   ierr = KSPSolveTranspose(red->ksp,red->xsub,red->ysub);CHKERRQ(ierr);
250c0decd05SBarry Smith   ierr = KSPCheckSolve(red->ksp,pc,red->ysub);CHKERRQ(ierr);
251d88bfacbSStefano Zampini   ierr = VecResetArray(red->xsub);CHKERRQ(ierr);
252d88bfacbSStefano Zampini   ierr = VecRestoreArray(red->xdup,&array);CHKERRQ(ierr);
253d88bfacbSStefano Zampini 
254d88bfacbSStefano Zampini   /* place ysub's local array into ydup */
255d88bfacbSStefano Zampini   ierr = VecGetArray(red->ysub,&array);CHKERRQ(ierr);
256d88bfacbSStefano Zampini   ierr = VecPlaceArray(red->ydup,(const PetscScalar*)array);CHKERRQ(ierr);
257d88bfacbSStefano Zampini 
258d88bfacbSStefano Zampini   /* scatter ydup to y */
259d88bfacbSStefano Zampini   ierr = VecScatterBegin(red->scatterout,red->ydup,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
260d88bfacbSStefano Zampini   ierr = VecScatterEnd(red->scatterout,red->ydup,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
261d88bfacbSStefano Zampini   ierr = VecResetArray(red->ydup);CHKERRQ(ierr);
262d88bfacbSStefano Zampini   ierr = VecRestoreArray(red->ysub,&array);CHKERRQ(ierr);
263d88bfacbSStefano Zampini   PetscFunctionReturn(0);
264d88bfacbSStefano Zampini }
265d88bfacbSStefano Zampini 
2661ea5a559SBarry Smith static PetscErrorCode PCReset_Redundant(PC pc)
2674b9ad928SBarry Smith {
2684b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
269dfbe8321SBarry Smith   PetscErrorCode ierr;
2704b9ad928SBarry Smith 
2714b9ad928SBarry Smith   PetscFunctionBegin;
2721b81debcSHong Zhang   if (red->useparallelmat) {
2736bf464f9SBarry Smith     ierr = VecScatterDestroy(&red->scatterin);CHKERRQ(ierr);
2746bf464f9SBarry Smith     ierr = VecScatterDestroy(&red->scatterout);CHKERRQ(ierr);
2756bf464f9SBarry Smith     ierr = VecDestroy(&red->ysub);CHKERRQ(ierr);
2766bf464f9SBarry Smith     ierr = VecDestroy(&red->xsub);CHKERRQ(ierr);
2776bf464f9SBarry Smith     ierr = VecDestroy(&red->xdup);CHKERRQ(ierr);
2786bf464f9SBarry Smith     ierr = VecDestroy(&red->ydup);CHKERRQ(ierr);
2791b81debcSHong Zhang   }
2806bf464f9SBarry Smith   ierr = MatDestroy(&red->pmats);CHKERRQ(ierr);
2811b81debcSHong Zhang   ierr = KSPReset(red->ksp);CHKERRQ(ierr);
2821ea5a559SBarry Smith   PetscFunctionReturn(0);
2831ea5a559SBarry Smith }
2841ea5a559SBarry Smith 
2851ea5a559SBarry Smith static PetscErrorCode PCDestroy_Redundant(PC pc)
2861ea5a559SBarry Smith {
2871ea5a559SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
2881ea5a559SBarry Smith   PetscErrorCode ierr;
2891ea5a559SBarry Smith 
2901ea5a559SBarry Smith   PetscFunctionBegin;
2911ea5a559SBarry Smith   ierr = PCReset_Redundant(pc);CHKERRQ(ierr);
2926bf464f9SBarry Smith   ierr = KSPDestroy(&red->ksp);CHKERRQ(ierr);
2936bf464f9SBarry Smith   ierr = PetscSubcommDestroy(&red->psubcomm);CHKERRQ(ierr);
294c31cb41cSBarry Smith   ierr = PetscFree(pc->data);CHKERRQ(ierr);
2954b9ad928SBarry Smith   PetscFunctionReturn(0);
2964b9ad928SBarry Smith }
2974b9ad928SBarry Smith 
2984416b707SBarry Smith static PetscErrorCode PCSetFromOptions_Redundant(PetscOptionItems *PetscOptionsObject,PC pc)
2994b9ad928SBarry Smith {
300a98ce0f4SHong Zhang   PetscErrorCode ierr;
301a98ce0f4SHong Zhang   PC_Redundant   *red = (PC_Redundant*)pc->data;
302a98ce0f4SHong Zhang 
3034b9ad928SBarry Smith   PetscFunctionBegin;
304e55864a3SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"Redundant options");CHKERRQ(ierr);
3050a545947SLisandro Dalcin   ierr = PetscOptionsInt("-pc_redundant_number","Number of redundant pc","PCRedundantSetNumber",red->nsubcomm,&red->nsubcomm,NULL);CHKERRQ(ierr);
306a98ce0f4SHong Zhang   ierr = PetscOptionsTail();CHKERRQ(ierr);
3074b9ad928SBarry Smith   PetscFunctionReturn(0);
3084b9ad928SBarry Smith }
3094b9ad928SBarry Smith 
310f7a08781SBarry Smith static PetscErrorCode PCRedundantSetNumber_Redundant(PC pc,PetscInt nreds)
31109a6bc64SHong Zhang {
31209a6bc64SHong Zhang   PC_Redundant *red = (PC_Redundant*)pc->data;
31309a6bc64SHong Zhang 
31409a6bc64SHong Zhang   PetscFunctionBegin;
31509a6bc64SHong Zhang   red->nsubcomm = nreds;
31609a6bc64SHong Zhang   PetscFunctionReturn(0);
31709a6bc64SHong Zhang }
31809a6bc64SHong Zhang 
31909a6bc64SHong Zhang /*@
32009a6bc64SHong Zhang    PCRedundantSetNumber - Sets the number of redundant preconditioner contexts.
32109a6bc64SHong Zhang 
3223f9fe445SBarry Smith    Logically Collective on PC
32309a6bc64SHong Zhang 
32409a6bc64SHong Zhang    Input Parameters:
32509a6bc64SHong Zhang +  pc - the preconditioner context
3269b21d695SBarry Smith -  nredundant - number of redundant preconditioner contexts; for example if you are using 64 MPI processes and
3279b21d695SBarry Smith                               use an nredundant of 4 there will be 4 parallel solves each on 16 = 64/4 processes.
32809a6bc64SHong Zhang 
32909a6bc64SHong Zhang    Level: advanced
33009a6bc64SHong Zhang 
33109a6bc64SHong Zhang @*/
3327087cfbeSBarry Smith PetscErrorCode PCRedundantSetNumber(PC pc,PetscInt nredundant)
33309a6bc64SHong Zhang {
3344ac538c5SBarry Smith   PetscErrorCode ierr;
33509a6bc64SHong Zhang 
33609a6bc64SHong Zhang   PetscFunctionBegin;
3370700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
338ce94432eSBarry Smith   if (nredundant <= 0) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG, "num of redundant pc %D must be positive",nredundant);
3394ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCRedundantSetNumber_C",(PC,PetscInt),(pc,nredundant));CHKERRQ(ierr);
34009a6bc64SHong Zhang   PetscFunctionReturn(0);
34109a6bc64SHong Zhang }
34209a6bc64SHong Zhang 
343f7a08781SBarry Smith static PetscErrorCode PCRedundantSetScatter_Redundant(PC pc,VecScatter in,VecScatter out)
3444b9ad928SBarry Smith {
3454b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
346dfbe8321SBarry Smith   PetscErrorCode ierr;
3474b9ad928SBarry Smith 
3484b9ad928SBarry Smith   PetscFunctionBegin;
3494b9ad928SBarry Smith   ierr = PetscObjectReference((PetscObject)in);CHKERRQ(ierr);
3506bf464f9SBarry Smith   ierr = VecScatterDestroy(&red->scatterin);CHKERRQ(ierr);
3512fa5cd67SKarl Rupp 
352c3122656SLisandro Dalcin   red->scatterin  = in;
3532fa5cd67SKarl Rupp 
3544b9ad928SBarry Smith   ierr = PetscObjectReference((PetscObject)out);CHKERRQ(ierr);
3556bf464f9SBarry Smith   ierr = VecScatterDestroy(&red->scatterout);CHKERRQ(ierr);
356c3122656SLisandro Dalcin   red->scatterout = out;
3574b9ad928SBarry Smith   PetscFunctionReturn(0);
3584b9ad928SBarry Smith }
3594b9ad928SBarry Smith 
3604b9ad928SBarry Smith /*@
3614b9ad928SBarry Smith    PCRedundantSetScatter - Sets the scatter used to copy values into the
3624b9ad928SBarry Smith      redundant local solve and the scatter to move them back into the global
3634b9ad928SBarry Smith      vector.
3644b9ad928SBarry Smith 
3653f9fe445SBarry Smith    Logically Collective on PC
3664b9ad928SBarry Smith 
3674b9ad928SBarry Smith    Input Parameters:
3684b9ad928SBarry Smith +  pc - the preconditioner context
3694b9ad928SBarry Smith .  in - the scatter to move the values in
3704b9ad928SBarry Smith -  out - the scatter to move them out
3714b9ad928SBarry Smith 
3724b9ad928SBarry Smith    Level: advanced
3734b9ad928SBarry Smith 
3744b9ad928SBarry Smith @*/
3757087cfbeSBarry Smith PetscErrorCode PCRedundantSetScatter(PC pc,VecScatter in,VecScatter out)
3764b9ad928SBarry Smith {
3774ac538c5SBarry Smith   PetscErrorCode ierr;
3784b9ad928SBarry Smith 
3794b9ad928SBarry Smith   PetscFunctionBegin;
3800700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
381*97929ea7SJunchao Zhang   PetscValidHeaderSpecific(in,PETSCSF_CLASSID,2);
382*97929ea7SJunchao Zhang   PetscValidHeaderSpecific(out,PETSCSF_CLASSID,3);
3834ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCRedundantSetScatter_C",(PC,VecScatter,VecScatter),(pc,in,out));CHKERRQ(ierr);
3844b9ad928SBarry Smith   PetscFunctionReturn(0);
3854b9ad928SBarry Smith }
3864b9ad928SBarry Smith 
387f7a08781SBarry Smith static PetscErrorCode PCRedundantGetKSP_Redundant(PC pc,KSP *innerksp)
3884b9ad928SBarry Smith {
3895f06b7aaSBarry Smith   PetscErrorCode ierr;
3904b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
39175024027SHong Zhang   MPI_Comm       comm,subcomm;
39275024027SHong Zhang   const char     *prefix;
39308cecb0aSPierre Jolivet   PetscBool      issbaij;
3944b9ad928SBarry Smith 
3954b9ad928SBarry Smith   PetscFunctionBegin;
39675024027SHong Zhang   if (!red->psubcomm) {
397e5acf8a4SHong Zhang     ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
398e5acf8a4SHong Zhang 
39975024027SHong Zhang     ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
40075024027SHong Zhang     ierr = PetscSubcommCreate(comm,&red->psubcomm);CHKERRQ(ierr);
40175024027SHong Zhang     ierr = PetscSubcommSetNumber(red->psubcomm,red->nsubcomm);CHKERRQ(ierr);
40275024027SHong Zhang     ierr = PetscSubcommSetType(red->psubcomm,PETSC_SUBCOMM_CONTIGUOUS);CHKERRQ(ierr);
403e5acf8a4SHong Zhang 
404e5acf8a4SHong Zhang     ierr = PetscSubcommSetOptionsPrefix(red->psubcomm,prefix);CHKERRQ(ierr);
405e5acf8a4SHong Zhang     ierr = PetscSubcommSetFromOptions(red->psubcomm);CHKERRQ(ierr);
40675024027SHong Zhang     ierr = PetscLogObjectMemory((PetscObject)pc,sizeof(PetscSubcomm));CHKERRQ(ierr);
40775024027SHong Zhang 
40875024027SHong Zhang     /* create a new PC that processors in each subcomm have copy of */
40975024027SHong Zhang     subcomm = PetscSubcommChild(red->psubcomm);
41075024027SHong Zhang 
41175024027SHong Zhang     ierr = KSPCreate(subcomm,&red->ksp);CHKERRQ(ierr);
41275024027SHong Zhang     ierr = KSPSetErrorIfNotConverged(red->ksp,pc->erroriffailure);CHKERRQ(ierr);
41375024027SHong Zhang     ierr = PetscObjectIncrementTabLevel((PetscObject)red->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
41475024027SHong Zhang     ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)red->ksp);CHKERRQ(ierr);
41575024027SHong Zhang     ierr = KSPSetType(red->ksp,KSPPREONLY);CHKERRQ(ierr);
41675024027SHong Zhang     ierr = KSPGetPC(red->ksp,&red->pc);CHKERRQ(ierr);
417a734384aSPierre Jolivet     ierr = PetscObjectTypeCompare((PetscObject)pc->pmat,MATSEQSBAIJ,&issbaij);CHKERRQ(ierr);
41808cecb0aSPierre Jolivet     if (!issbaij) {
419a734384aSPierre Jolivet       ierr = PetscObjectTypeCompare((PetscObject)pc->pmat,MATMPISBAIJ,&issbaij);CHKERRQ(ierr);
42008cecb0aSPierre Jolivet     }
42108cecb0aSPierre Jolivet     if (!issbaij) {
42275024027SHong Zhang       ierr = PCSetType(red->pc,PCLU);CHKERRQ(ierr);
42308cecb0aSPierre Jolivet     } else {
42408cecb0aSPierre Jolivet       ierr = PCSetType(red->pc,PCCHOLESKY);CHKERRQ(ierr);
42508cecb0aSPierre Jolivet     }
426753b7fb9SBarry Smith     if (red->shifttypeset) {
427753b7fb9SBarry Smith       ierr = PCFactorSetShiftType(red->pc,red->shifttype);CHKERRQ(ierr);
428753b7fb9SBarry Smith       red->shifttypeset = PETSC_FALSE;
429753b7fb9SBarry Smith     }
43075024027SHong Zhang     ierr = KSPSetOptionsPrefix(red->ksp,prefix);CHKERRQ(ierr);
43175024027SHong Zhang     ierr = KSPAppendOptionsPrefix(red->ksp,"redundant_");CHKERRQ(ierr);
43275024027SHong Zhang   }
43383ab6a24SBarry Smith   *innerksp = red->ksp;
4344b9ad928SBarry Smith   PetscFunctionReturn(0);
4354b9ad928SBarry Smith }
4364b9ad928SBarry Smith 
4374b9ad928SBarry Smith /*@
43883ab6a24SBarry Smith    PCRedundantGetKSP - Gets the less parallel KSP created by the redundant PC.
4394b9ad928SBarry Smith 
4404b9ad928SBarry Smith    Not Collective
4414b9ad928SBarry Smith 
4424b9ad928SBarry Smith    Input Parameter:
4434b9ad928SBarry Smith .  pc - the preconditioner context
4444b9ad928SBarry Smith 
4454b9ad928SBarry Smith    Output Parameter:
44683ab6a24SBarry Smith .  innerksp - the KSP on the smaller set of processes
4474b9ad928SBarry Smith 
4484b9ad928SBarry Smith    Level: advanced
4494b9ad928SBarry Smith 
4504b9ad928SBarry Smith @*/
45183ab6a24SBarry Smith PetscErrorCode PCRedundantGetKSP(PC pc,KSP *innerksp)
4524b9ad928SBarry Smith {
4534ac538c5SBarry Smith   PetscErrorCode ierr;
4544b9ad928SBarry Smith 
4554b9ad928SBarry Smith   PetscFunctionBegin;
4560700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
45783ab6a24SBarry Smith   PetscValidPointer(innerksp,2);
458753b7fb9SBarry Smith   ierr = PetscUseMethod(pc,"PCRedundantGetKSP_C",(PC,KSP*),(pc,innerksp));CHKERRQ(ierr);
4594b9ad928SBarry Smith   PetscFunctionReturn(0);
4604b9ad928SBarry Smith }
4614b9ad928SBarry Smith 
462f7a08781SBarry Smith static PetscErrorCode PCRedundantGetOperators_Redundant(PC pc,Mat *mat,Mat *pmat)
4634b9ad928SBarry Smith {
4644b9ad928SBarry Smith   PC_Redundant *red = (PC_Redundant*)pc->data;
4654b9ad928SBarry Smith 
4664b9ad928SBarry Smith   PetscFunctionBegin;
467b3804887SHong Zhang   if (mat)  *mat  = red->pmats;
468b3804887SHong Zhang   if (pmat) *pmat = red->pmats;
4694b9ad928SBarry Smith   PetscFunctionReturn(0);
4704b9ad928SBarry Smith }
4714b9ad928SBarry Smith 
4724b9ad928SBarry Smith /*@
4734b9ad928SBarry Smith    PCRedundantGetOperators - gets the sequential matrix and preconditioner matrix
4744b9ad928SBarry Smith 
4754b9ad928SBarry Smith    Not Collective
4764b9ad928SBarry Smith 
4774b9ad928SBarry Smith    Input Parameter:
4784b9ad928SBarry Smith .  pc - the preconditioner context
4794b9ad928SBarry Smith 
4804b9ad928SBarry Smith    Output Parameters:
4814b9ad928SBarry Smith +  mat - the matrix
4824b9ad928SBarry Smith -  pmat - the (possibly different) preconditioner matrix
4834b9ad928SBarry Smith 
4844b9ad928SBarry Smith    Level: advanced
4854b9ad928SBarry Smith 
4864b9ad928SBarry Smith @*/
4877087cfbeSBarry Smith PetscErrorCode PCRedundantGetOperators(PC pc,Mat *mat,Mat *pmat)
4884b9ad928SBarry Smith {
4894ac538c5SBarry Smith   PetscErrorCode ierr;
4904b9ad928SBarry Smith 
4914b9ad928SBarry Smith   PetscFunctionBegin;
4920700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
4934482741eSBarry Smith   if (mat)  PetscValidPointer(mat,2);
4944482741eSBarry Smith   if (pmat) PetscValidPointer(pmat,3);
495163d334eSBarry Smith   ierr = PetscUseMethod(pc,"PCRedundantGetOperators_C",(PC,Mat*,Mat*),(pc,mat,pmat));CHKERRQ(ierr);
4964b9ad928SBarry Smith   PetscFunctionReturn(0);
4974b9ad928SBarry Smith }
4984b9ad928SBarry Smith 
4994b9ad928SBarry Smith /* -------------------------------------------------------------------------------------*/
50037a17b4dSBarry Smith /*MC
50183ab6a24SBarry Smith      PCREDUNDANT - Runs a KSP solver with preconditioner for the entire problem on subgroups of processors
50237a17b4dSBarry Smith 
50383ab6a24SBarry Smith      Options for the redundant preconditioners can be set with -redundant_pc_xxx for the redundant KSP with -redundant_ksp_xxx
50437a17b4dSBarry Smith 
50509391456SBarry Smith   Options Database:
5069b21d695SBarry Smith .  -pc_redundant_number <n> - number of redundant solves, for example if you are using 64 MPI processes and
5079b21d695SBarry Smith                               use an n of 4 there will be 4 parallel solves each on 16 = 64/4 processes.
50809391456SBarry Smith 
50937a17b4dSBarry Smith    Level: intermediate
51037a17b4dSBarry Smith 
51195452b02SPatrick Sanan    Notes:
51208cecb0aSPierre Jolivet     The default KSP is preonly and the default PC is LU or CHOLESKY if Pmat is of type MATSBAIJ.
51383ab6a24SBarry Smith 
514753b7fb9SBarry Smith    PCFactorSetShiftType() applied to this PC will convey they shift type into the inner PC if it is factorization based.
515753b7fb9SBarry Smith 
51695452b02SPatrick Sanan    Developer Notes:
51795452b02SPatrick Sanan     Note that PCSetInitialGuessNonzero()  is not used by this class but likely should be.
5189cfaa89bSBarry Smith 
51937a17b4dSBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PCRedundantSetScatter(),
52083ab6a24SBarry Smith            PCRedundantGetKSP(), PCRedundantGetOperators(), PCRedundantSetNumber()
52137a17b4dSBarry Smith M*/
52237a17b4dSBarry Smith 
5238cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_Redundant(PC pc)
5244b9ad928SBarry Smith {
525dfbe8321SBarry Smith   PetscErrorCode ierr;
5264b9ad928SBarry Smith   PC_Redundant   *red;
52769db28dcSHong Zhang   PetscMPIInt    size;
5283f457be1SHong Zhang 
5294b9ad928SBarry Smith   PetscFunctionBegin;
530b00a9115SJed Brown   ierr = PetscNewLog(pc,&red);CHKERRQ(ierr);
531ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);CHKERRQ(ierr);
5322fa5cd67SKarl Rupp 
53369db28dcSHong Zhang   red->nsubcomm       = size;
5344b9ad928SBarry Smith   red->useparallelmat = PETSC_TRUE;
5351fbd8f88SHong Zhang   pc->data            = (void*)red;
5364b9ad928SBarry Smith 
5374b9ad928SBarry Smith   pc->ops->apply          = PCApply_Redundant;
538d88bfacbSStefano Zampini   pc->ops->applytranspose = PCApplyTranspose_Redundant;
5394b9ad928SBarry Smith   pc->ops->setup          = PCSetUp_Redundant;
5404b9ad928SBarry Smith   pc->ops->destroy        = PCDestroy_Redundant;
5411ea5a559SBarry Smith   pc->ops->reset          = PCReset_Redundant;
5424b9ad928SBarry Smith   pc->ops->setfromoptions = PCSetFromOptions_Redundant;
5434b9ad928SBarry Smith   pc->ops->view           = PCView_Redundant;
5442fa5cd67SKarl Rupp 
545bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCRedundantSetScatter_C",PCRedundantSetScatter_Redundant);CHKERRQ(ierr);
546bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCRedundantSetNumber_C",PCRedundantSetNumber_Redundant);CHKERRQ(ierr);
547bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCRedundantGetKSP_C",PCRedundantGetKSP_Redundant);CHKERRQ(ierr);
548bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCRedundantGetOperators_C",PCRedundantGetOperators_Redundant);CHKERRQ(ierr);
549753b7fb9SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetShiftType_C",PCFactorSetShiftType_Redundant);CHKERRQ(ierr);
5504b9ad928SBarry Smith   PetscFunctionReturn(0);
5514b9ad928SBarry Smith }
552b2573a8aSBarry Smith 
553