xref: /petsc/src/ksp/pc/impls/redundant/redundant.c (revision fcfd50eb5fbe12291db06fc1b33ad004ead64926)
1dba47a55SKris Buschelman 
24b9ad928SBarry Smith /*
33f457be1SHong Zhang   This file defines a "solve the problem redundantly on each subgroup of processor" preconditioner.
44b9ad928SBarry Smith */
5c6db04a5SJed Brown #include <private/pcimpl.h>     /*I "petscpc.h" I*/
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 */
117adad957SLisandro Dalcin   Vec          xsub,ysub;            /* vectors of a subcommunicator to hold parallel vectors of ((PetscObject)pc)->comm */
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 */
184b9ad928SBarry Smith } PC_Redundant;
194b9ad928SBarry Smith 
204b9ad928SBarry Smith #undef __FUNCT__
214b9ad928SBarry Smith #define __FUNCT__ "PCView_Redundant"
226849ba73SBarry Smith static PetscErrorCode PCView_Redundant(PC pc,PetscViewer viewer)
234b9ad928SBarry Smith {
244b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
25dfbe8321SBarry Smith   PetscErrorCode ierr;
26ace3abfcSBarry Smith   PetscBool      iascii,isstring;
2703ccd0b4SBarry Smith   PetscViewer    subviewer;
284b9ad928SBarry Smith 
294b9ad928SBarry Smith   PetscFunctionBegin;
302692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
312692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
3232077d6dSBarry Smith   if (iascii) {
3303ccd0b4SBarry Smith     if (!red->psubcomm) {
3403ccd0b4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Redundant preconditioner: Not yet setup\n");CHKERRQ(ierr);
3503ccd0b4SBarry Smith     } else {
363e065800SHong Zhang       ierr = PetscViewerASCIIPrintf(viewer,"  Redundant preconditioner: First (color=0) of %D PCs follows\n",red->nsubcomm);CHKERRQ(ierr);
377adad957SLisandro Dalcin       ierr = PetscViewerGetSubcomm(viewer,((PetscObject)red->pc)->comm,&subviewer);CHKERRQ(ierr);
38f5dd71faSBarry Smith       if (!red->psubcomm->color) { /* only view first redundant pc */
394b9ad928SBarry Smith 	ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
403e065800SHong Zhang 	ierr = KSPView(red->ksp,subviewer);CHKERRQ(ierr);
414b9ad928SBarry Smith 	ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
424b9ad928SBarry Smith       }
437adad957SLisandro Dalcin       ierr = PetscViewerRestoreSubcomm(viewer,((PetscObject)red->pc)->comm,&subviewer);CHKERRQ(ierr);
444b9ad928SBarry Smith     }
4503ccd0b4SBarry Smith   } else if (isstring) {
4603ccd0b4SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," Redundant solver preconditioner");CHKERRQ(ierr);
474b9ad928SBarry Smith   } else {
4865e19b50SBarry Smith     SETERRQ1(((PetscObject)pc)->comm,PETSC_ERR_SUP,"Viewer type %s not supported for PC redundant",((PetscObject)viewer)->type_name);
494b9ad928SBarry Smith   }
504b9ad928SBarry Smith   PetscFunctionReturn(0);
514b9ad928SBarry Smith }
524b9ad928SBarry Smith 
534b9ad928SBarry Smith #undef __FUNCT__
544b9ad928SBarry Smith #define __FUNCT__ "PCSetUp_Redundant"
556849ba73SBarry Smith static PetscErrorCode PCSetUp_Redundant(PC pc)
564b9ad928SBarry Smith {
574b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
58dfbe8321SBarry Smith   PetscErrorCode ierr;
5945fc02eaSBarry Smith   PetscInt       mstart,mend,mlocal,m,mlocal_sub,rstart_sub,rend_sub,mloc_sub;
6013f74950SBarry Smith   PetscMPIInt    size;
614b9ad928SBarry Smith   MatReuse       reuse = MAT_INITIAL_MATRIX;
624b9ad928SBarry Smith   MatStructure   str = DIFFERENT_NONZERO_PATTERN;
637adad957SLisandro Dalcin   MPI_Comm       comm = ((PetscObject)pc)->comm,subcomm;
6423ce1328SBarry Smith   Vec            vec;
653f457be1SHong Zhang   PetscMPIInt    subsize,subrank;
661fbd8f88SHong Zhang   const char     *prefix;
67b862ddfaSBarry Smith   const PetscInt *range;
683f457be1SHong Zhang 
694b9ad928SBarry Smith   PetscFunctionBegin;
7023ce1328SBarry Smith   ierr = MatGetVecs(pc->pmat,&vec,0);CHKERRQ(ierr);
7123ce1328SBarry Smith   ierr = VecGetSize(vec,&m);CHKERRQ(ierr);
721fbd8f88SHong Zhang 
734b9ad928SBarry Smith   if (!pc->setupcalled) {
745f06b7aaSBarry Smith     if (!red->psubcomm) {
75d8a68f86SHong Zhang       ierr = PetscSubcommCreate(comm,&red->psubcomm);CHKERRQ(ierr);
76d8a68f86SHong Zhang       ierr = PetscSubcommSetNumber(red->psubcomm,red->nsubcomm);CHKERRQ(ierr);
77d8a68f86SHong Zhang       ierr = PetscSubcommSetType(red->psubcomm,PETSC_SUBCOMM_INTERLACED);CHKERRQ(ierr);
781fbd8f88SHong Zhang       ierr = PetscLogObjectMemory(pc,sizeof(PetscSubcomm));CHKERRQ(ierr);
791fbd8f88SHong Zhang 
801fbd8f88SHong Zhang       /* create a new PC that processors in each subcomm have copy of */
810d7810c8SBarry Smith       subcomm = red->psubcomm->comm;
825f06b7aaSBarry Smith       ierr = KSPCreate(subcomm,&red->ksp);CHKERRQ(ierr);
835f06b7aaSBarry Smith       ierr = PetscObjectIncrementTabLevel((PetscObject)red->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
845f06b7aaSBarry Smith       ierr = PetscLogObjectParent(pc,red->ksp);CHKERRQ(ierr);
855f06b7aaSBarry Smith       ierr = KSPSetType(red->ksp,KSPPREONLY);CHKERRQ(ierr);
865f06b7aaSBarry Smith       ierr = KSPGetPC(red->ksp,&red->pc);CHKERRQ(ierr);
87cf52b8b1SHong Zhang       ierr = PCSetType(red->pc,PCLU);CHKERRQ(ierr);
88cf52b8b1SHong Zhang 
891fbd8f88SHong Zhang       ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
905f06b7aaSBarry Smith       ierr = KSPSetOptionsPrefix(red->ksp,prefix);CHKERRQ(ierr);
915f06b7aaSBarry Smith       ierr = KSPAppendOptionsPrefix(red->ksp,"redundant_");CHKERRQ(ierr);
925f06b7aaSBarry Smith     } else {
935f06b7aaSBarry Smith        subcomm = red->psubcomm->comm;
945f06b7aaSBarry Smith     }
951fbd8f88SHong Zhang 
963f457be1SHong Zhang     /* create working vectors xsub/ysub and xdup/ydup */
9723ce1328SBarry Smith     ierr = VecGetLocalSize(vec,&mlocal);CHKERRQ(ierr);
983f457be1SHong Zhang     ierr = VecGetOwnershipRange(vec,&mstart,&mend);CHKERRQ(ierr);
994b9ad928SBarry Smith 
1003f457be1SHong Zhang     /* get local size of xsub/ysub */
1011fbd8f88SHong Zhang     ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr);
1021fbd8f88SHong Zhang     ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr);
103b862ddfaSBarry Smith     ierr = MatGetOwnershipRanges(pc->pmat,&range);CHKERRQ(ierr);
104b862ddfaSBarry Smith     rstart_sub = range[red->psubcomm->n*subrank]; /* rstart in xsub/ysub */
1053f457be1SHong Zhang     if (subrank+1 < subsize){
106b862ddfaSBarry Smith       rend_sub = range[red->psubcomm->n*(subrank+1)];
1073f457be1SHong Zhang     } else {
1083f457be1SHong Zhang       rend_sub = m;
1093f457be1SHong Zhang     }
1103f457be1SHong Zhang     mloc_sub = rend_sub - rstart_sub;
1111fbd8f88SHong Zhang     ierr = VecCreateMPI(subcomm,mloc_sub,PETSC_DECIDE,&red->ysub);CHKERRQ(ierr);
1123f457be1SHong Zhang     /* create xsub with empty local arrays, because xdup's arrays will be placed into it */
1131fbd8f88SHong Zhang     ierr = VecCreateMPIWithArray(subcomm,mloc_sub,PETSC_DECIDE,PETSC_NULL,&red->xsub);CHKERRQ(ierr);
1143f457be1SHong Zhang 
1153f457be1SHong Zhang     /* create xdup and ydup. ydup has empty local arrays because ysub's arrays will be place into it.
1167adad957SLisandro Dalcin        Note: we use communicator dupcomm, not ((PetscObject)pc)->comm! */
1171fbd8f88SHong Zhang     ierr = VecCreateMPI(red->psubcomm->dupparent,mloc_sub,PETSC_DECIDE,&red->xdup);CHKERRQ(ierr);
1181fbd8f88SHong Zhang     ierr = VecCreateMPIWithArray(red->psubcomm->dupparent,mloc_sub,PETSC_DECIDE,PETSC_NULL,&red->ydup);CHKERRQ(ierr);
1193f457be1SHong Zhang 
1203f457be1SHong Zhang     /* create vec scatters */
1213f457be1SHong Zhang     if (!red->scatterin){
1223f457be1SHong Zhang       IS       is1,is2;
1233f457be1SHong Zhang       PetscInt *idx1,*idx2,i,j,k;
12445fc02eaSBarry Smith 
1251d79065fSBarry Smith       ierr = PetscMalloc2(red->psubcomm->n*mlocal,PetscInt,&idx1,red->psubcomm->n*mlocal,PetscInt,&idx2);CHKERRQ(ierr);
1263f457be1SHong Zhang       j = 0;
1271fbd8f88SHong Zhang       for (k=0; k<red->psubcomm->n; k++){
1283f457be1SHong Zhang         for (i=mstart; i<mend; i++){
1293f457be1SHong Zhang           idx1[j]   = i;
1303f457be1SHong Zhang           idx2[j++] = i + m*k;
1313f457be1SHong Zhang         }
1323f457be1SHong Zhang       }
13370b3c8c7SBarry Smith       ierr = ISCreateGeneral(comm,red->psubcomm->n*mlocal,idx1,PETSC_COPY_VALUES,&is1);CHKERRQ(ierr);
13470b3c8c7SBarry Smith       ierr = ISCreateGeneral(comm,red->psubcomm->n*mlocal,idx2,PETSC_COPY_VALUES,&is2);CHKERRQ(ierr);
1353f457be1SHong Zhang       ierr = VecScatterCreate(vec,is1,red->xdup,is2,&red->scatterin);CHKERRQ(ierr);
136*fcfd50ebSBarry Smith       ierr = ISDestroy(&is1);CHKERRQ(ierr);
137*fcfd50ebSBarry Smith       ierr = ISDestroy(&is2);CHKERRQ(ierr);
1383f457be1SHong Zhang 
1391fbd8f88SHong Zhang       ierr = ISCreateStride(comm,mlocal,mstart+ red->psubcomm->color*m,1,&is1);CHKERRQ(ierr);
1403f457be1SHong Zhang       ierr = ISCreateStride(comm,mlocal,mstart,1,&is2);CHKERRQ(ierr);
1413f457be1SHong Zhang       ierr = VecScatterCreate(red->xdup,is1,vec,is2,&red->scatterout);CHKERRQ(ierr);
142*fcfd50ebSBarry Smith       ierr = ISDestroy(&is1);CHKERRQ(ierr);
143*fcfd50ebSBarry Smith       ierr = ISDestroy(&is2);CHKERRQ(ierr);
1441d79065fSBarry Smith       ierr = PetscFree2(idx1,idx2);CHKERRQ(ierr);
1454b9ad928SBarry Smith     }
1464b9ad928SBarry Smith   }
14723ce1328SBarry Smith   ierr = VecDestroy(vec);CHKERRQ(ierr);
1484b9ad928SBarry Smith 
1494b9ad928SBarry Smith   /* if pmatrix set by user is sequential then we do not need to gather the parallel matrix */
1503f457be1SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1514b9ad928SBarry Smith   if (size == 1) {
1524b9ad928SBarry Smith     red->useparallelmat = PETSC_FALSE;
1534b9ad928SBarry Smith   }
1544b9ad928SBarry Smith 
1554b9ad928SBarry Smith   if (red->useparallelmat) {
1564b9ad928SBarry Smith     if (pc->setupcalled == 1 && pc->flag == DIFFERENT_NONZERO_PATTERN) {
1574b9ad928SBarry Smith       /* destroy old matrices */
1584b9ad928SBarry Smith       if (red->pmats) {
159b3804887SHong Zhang         ierr = MatDestroy(red->pmats);CHKERRQ(ierr);
1604b9ad928SBarry Smith       }
1614b9ad928SBarry Smith     } else if (pc->setupcalled == 1) {
1624b9ad928SBarry Smith       reuse = MAT_REUSE_MATRIX;
1634b9ad928SBarry Smith       str   = SAME_NONZERO_PATTERN;
1644b9ad928SBarry Smith     }
1654b9ad928SBarry Smith 
1663f457be1SHong Zhang     /* grab the parallel matrix and put it into processors of a subcomminicator */
167f664ae05SHong Zhang     /*--------------------------------------------------------------------------*/
168f664ae05SHong Zhang     ierr = VecGetLocalSize(red->ysub,&mlocal_sub);CHKERRQ(ierr);
16969db28dcSHong Zhang     ierr = MatGetRedundantMatrix(pc->pmat,red->psubcomm->n,red->psubcomm->comm,mlocal_sub,reuse,&red->pmats);CHKERRQ(ierr);
1703f457be1SHong Zhang     /* tell PC of the subcommunicator its operators */
17190f1c854SHong Zhang     ierr = KSPSetOperators(red->ksp,red->pmats,red->pmats,str);CHKERRQ(ierr);
1724b9ad928SBarry Smith   } else {
17390f1c854SHong Zhang     ierr = KSPSetOperators(red->ksp,pc->mat,pc->pmat,pc->flag);CHKERRQ(ierr);
1744b9ad928SBarry Smith   }
1750c24e6a1SHong Zhang   if (pc->setfromoptionscalled){
1763e065800SHong Zhang     ierr = KSPSetFromOptions(red->ksp);CHKERRQ(ierr);
1770c24e6a1SHong Zhang   }
1783e065800SHong Zhang   ierr = KSPSetUp(red->ksp);CHKERRQ(ierr);
1794b9ad928SBarry Smith   PetscFunctionReturn(0);
1804b9ad928SBarry Smith }
1814b9ad928SBarry Smith 
1824b9ad928SBarry Smith #undef __FUNCT__
1834b9ad928SBarry Smith #define __FUNCT__ "PCApply_Redundant"
1846849ba73SBarry Smith static PetscErrorCode PCApply_Redundant(PC pc,Vec x,Vec y)
1854b9ad928SBarry Smith {
1864b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
187dfbe8321SBarry Smith   PetscErrorCode ierr;
1883f457be1SHong Zhang   PetscScalar    *array;
1894b9ad928SBarry Smith 
1904b9ad928SBarry Smith   PetscFunctionBegin;
1913f457be1SHong Zhang   /* scatter x to xdup */
192ca9f406cSSatish Balay   ierr = VecScatterBegin(red->scatterin,x,red->xdup,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
193ca9f406cSSatish Balay   ierr = VecScatterEnd(red->scatterin,x,red->xdup,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1943f457be1SHong Zhang 
1953f457be1SHong Zhang   /* place xdup's local array into xsub */
1963f457be1SHong Zhang   ierr = VecGetArray(red->xdup,&array);CHKERRQ(ierr);
1973f457be1SHong Zhang   ierr = VecPlaceArray(red->xsub,(const PetscScalar*)array);CHKERRQ(ierr);
1984b9ad928SBarry Smith 
1994b9ad928SBarry Smith   /* apply preconditioner on each processor */
20083ab6a24SBarry Smith   ierr = KSPSolve(red->ksp,red->xsub,red->ysub);CHKERRQ(ierr);
2013f457be1SHong Zhang   ierr = VecResetArray(red->xsub);CHKERRQ(ierr);
2023f457be1SHong Zhang   ierr = VecRestoreArray(red->xdup,&array);CHKERRQ(ierr);
2034b9ad928SBarry Smith 
2043f457be1SHong Zhang   /* place ysub's local array into ydup */
2053f457be1SHong Zhang   ierr = VecGetArray(red->ysub,&array);CHKERRQ(ierr);
2063f457be1SHong Zhang   ierr = VecPlaceArray(red->ydup,(const PetscScalar*)array);CHKERRQ(ierr);
2073f457be1SHong Zhang 
2083f457be1SHong Zhang   /* scatter ydup to y */
209ca9f406cSSatish Balay   ierr = VecScatterBegin(red->scatterout,red->ydup,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
210ca9f406cSSatish Balay   ierr = VecScatterEnd(red->scatterout,red->ydup,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2113f457be1SHong Zhang   ierr = VecResetArray(red->ydup);CHKERRQ(ierr);
2123f457be1SHong Zhang   ierr = VecRestoreArray(red->ysub,&array);CHKERRQ(ierr);
2134b9ad928SBarry Smith   PetscFunctionReturn(0);
2144b9ad928SBarry Smith }
2154b9ad928SBarry Smith 
2164b9ad928SBarry Smith #undef __FUNCT__
2174b9ad928SBarry Smith #define __FUNCT__ "PCDestroy_Redundant"
2186849ba73SBarry Smith static PetscErrorCode PCDestroy_Redundant(PC pc)
2194b9ad928SBarry Smith {
2204b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
221dfbe8321SBarry Smith   PetscErrorCode ierr;
2224b9ad928SBarry Smith 
2234b9ad928SBarry Smith   PetscFunctionBegin;
2244b9ad928SBarry Smith   if (red->scatterin)  {ierr = VecScatterDestroy(red->scatterin);CHKERRQ(ierr);}
2254b9ad928SBarry Smith   if (red->scatterout) {ierr = VecScatterDestroy(red->scatterout);CHKERRQ(ierr);}
2263f457be1SHong Zhang   if (red->ysub)       {ierr = VecDestroy(red->ysub);CHKERRQ(ierr);}
2273f457be1SHong Zhang   if (red->xsub)       {ierr = VecDestroy(red->xsub);CHKERRQ(ierr);}
2283f457be1SHong Zhang   if (red->xdup)       {ierr = VecDestroy(red->xdup);CHKERRQ(ierr);}
2293f457be1SHong Zhang   if (red->ydup)       {ierr = VecDestroy(red->ydup);CHKERRQ(ierr);}
230b3804887SHong Zhang   if (red->pmats) {
231b3804887SHong Zhang     ierr = MatDestroy(red->pmats);CHKERRQ(ierr);
2323f457be1SHong Zhang   }
2333e065800SHong Zhang   if (red->ksp) {ierr = KSPDestroy(red->ksp);CHKERRQ(ierr);}
23430ca954eSBarry Smith   if (red->psubcomm) {ierr = PetscSubcommDestroy(red->psubcomm);CHKERRQ(ierr);}
235c31cb41cSBarry Smith   ierr = PetscFree(pc->data);CHKERRQ(ierr);
2364b9ad928SBarry Smith   PetscFunctionReturn(0);
2374b9ad928SBarry Smith }
2384b9ad928SBarry Smith 
2394b9ad928SBarry Smith #undef __FUNCT__
2404b9ad928SBarry Smith #define __FUNCT__ "PCSetFromOptions_Redundant"
2416849ba73SBarry Smith static PetscErrorCode PCSetFromOptions_Redundant(PC pc)
2424b9ad928SBarry Smith {
243a98ce0f4SHong Zhang   PetscErrorCode ierr;
244a98ce0f4SHong Zhang   PC_Redundant   *red = (PC_Redundant*)pc->data;
245a98ce0f4SHong Zhang 
2464b9ad928SBarry Smith   PetscFunctionBegin;
247a98ce0f4SHong Zhang   ierr = PetscOptionsHead("Redundant options");CHKERRQ(ierr);
24809a6bc64SHong Zhang   ierr = PetscOptionsInt("-pc_redundant_number","Number of redundant pc","PCRedundantSetNumber",red->nsubcomm,&red->nsubcomm,0);CHKERRQ(ierr);
249a98ce0f4SHong Zhang   ierr = PetscOptionsTail();CHKERRQ(ierr);
2504b9ad928SBarry Smith   PetscFunctionReturn(0);
2514b9ad928SBarry Smith }
2524b9ad928SBarry Smith 
2534b9ad928SBarry Smith EXTERN_C_BEGIN
2544b9ad928SBarry Smith #undef __FUNCT__
25509a6bc64SHong Zhang #define __FUNCT__ "PCRedundantSetNumber_Redundant"
2567087cfbeSBarry Smith PetscErrorCode  PCRedundantSetNumber_Redundant(PC pc,PetscInt nreds)
25709a6bc64SHong Zhang {
25809a6bc64SHong Zhang   PC_Redundant *red = (PC_Redundant*)pc->data;
25909a6bc64SHong Zhang 
26009a6bc64SHong Zhang   PetscFunctionBegin;
26109a6bc64SHong Zhang   red->nsubcomm = nreds;
26209a6bc64SHong Zhang   PetscFunctionReturn(0);
26309a6bc64SHong Zhang }
26409a6bc64SHong Zhang EXTERN_C_END
26509a6bc64SHong Zhang 
26609a6bc64SHong Zhang #undef __FUNCT__
26709a6bc64SHong Zhang #define __FUNCT__ "PCRedundantSetNumber"
26809a6bc64SHong Zhang /*@
26909a6bc64SHong Zhang    PCRedundantSetNumber - Sets the number of redundant preconditioner contexts.
27009a6bc64SHong Zhang 
2713f9fe445SBarry Smith    Logically Collective on PC
27209a6bc64SHong Zhang 
27309a6bc64SHong Zhang    Input Parameters:
27409a6bc64SHong Zhang +  pc - the preconditioner context
2759b21d695SBarry Smith -  nredundant - number of redundant preconditioner contexts; for example if you are using 64 MPI processes and
2769b21d695SBarry Smith                               use an nredundant of 4 there will be 4 parallel solves each on 16 = 64/4 processes.
27709a6bc64SHong Zhang 
27809a6bc64SHong Zhang    Level: advanced
27909a6bc64SHong Zhang 
28009a6bc64SHong Zhang .keywords: PC, redundant solve
28109a6bc64SHong Zhang @*/
2827087cfbeSBarry Smith PetscErrorCode  PCRedundantSetNumber(PC pc,PetscInt nredundant)
28309a6bc64SHong Zhang {
2844ac538c5SBarry Smith   PetscErrorCode ierr;
28509a6bc64SHong Zhang 
28609a6bc64SHong Zhang   PetscFunctionBegin;
2870700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
28865e19b50SBarry Smith   if (nredundant <= 0) SETERRQ1(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONG, "num of redundant pc %D must be positive",nredundant);
2894ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCRedundantSetNumber_C",(PC,PetscInt),(pc,nredundant));CHKERRQ(ierr);
29009a6bc64SHong Zhang   PetscFunctionReturn(0);
29109a6bc64SHong Zhang }
29209a6bc64SHong Zhang 
29309a6bc64SHong Zhang EXTERN_C_BEGIN
29409a6bc64SHong Zhang #undef __FUNCT__
2954b9ad928SBarry Smith #define __FUNCT__ "PCRedundantSetScatter_Redundant"
2967087cfbeSBarry Smith PetscErrorCode  PCRedundantSetScatter_Redundant(PC pc,VecScatter in,VecScatter out)
2974b9ad928SBarry Smith {
2984b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
299dfbe8321SBarry Smith   PetscErrorCode ierr;
3004b9ad928SBarry Smith 
3014b9ad928SBarry Smith   PetscFunctionBegin;
3024b9ad928SBarry Smith   ierr = PetscObjectReference((PetscObject)in);CHKERRQ(ierr);
303c3122656SLisandro Dalcin   if (red->scatterin) { ierr = VecScatterDestroy(red->scatterin);CHKERRQ(ierr); }
304c3122656SLisandro Dalcin   red->scatterin  = in;
3054b9ad928SBarry Smith   ierr = PetscObjectReference((PetscObject)out);CHKERRQ(ierr);
306c3122656SLisandro Dalcin   if (red->scatterout) { ierr = VecScatterDestroy(red->scatterout);CHKERRQ(ierr); }
307c3122656SLisandro Dalcin   red->scatterout = out;
3084b9ad928SBarry Smith   PetscFunctionReturn(0);
3094b9ad928SBarry Smith }
3104b9ad928SBarry Smith EXTERN_C_END
3114b9ad928SBarry Smith 
3124b9ad928SBarry Smith #undef __FUNCT__
3134b9ad928SBarry Smith #define __FUNCT__ "PCRedundantSetScatter"
3144b9ad928SBarry Smith /*@
3154b9ad928SBarry Smith    PCRedundantSetScatter - Sets the scatter used to copy values into the
3164b9ad928SBarry Smith      redundant local solve and the scatter to move them back into the global
3174b9ad928SBarry Smith      vector.
3184b9ad928SBarry Smith 
3193f9fe445SBarry Smith    Logically Collective on PC
3204b9ad928SBarry Smith 
3214b9ad928SBarry Smith    Input Parameters:
3224b9ad928SBarry Smith +  pc - the preconditioner context
3234b9ad928SBarry Smith .  in - the scatter to move the values in
3244b9ad928SBarry Smith -  out - the scatter to move them out
3254b9ad928SBarry Smith 
3264b9ad928SBarry Smith    Level: advanced
3274b9ad928SBarry Smith 
3284b9ad928SBarry Smith .keywords: PC, redundant solve
3294b9ad928SBarry Smith @*/
3307087cfbeSBarry Smith PetscErrorCode  PCRedundantSetScatter(PC pc,VecScatter in,VecScatter out)
3314b9ad928SBarry Smith {
3324ac538c5SBarry Smith   PetscErrorCode ierr;
3334b9ad928SBarry Smith 
3344b9ad928SBarry Smith   PetscFunctionBegin;
3350700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
3360700a824SBarry Smith   PetscValidHeaderSpecific(in,VEC_SCATTER_CLASSID,2);
3370700a824SBarry Smith   PetscValidHeaderSpecific(out,VEC_SCATTER_CLASSID,3);
3384ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCRedundantSetScatter_C",(PC,VecScatter,VecScatter),(pc,in,out));CHKERRQ(ierr);
3394b9ad928SBarry Smith   PetscFunctionReturn(0);
3404b9ad928SBarry Smith }
3414b9ad928SBarry Smith 
3424b9ad928SBarry Smith EXTERN_C_BEGIN
3434b9ad928SBarry Smith #undef __FUNCT__
34483ab6a24SBarry Smith #define __FUNCT__ "PCRedundantGetKSP_Redundant"
34583ab6a24SBarry Smith PetscErrorCode  PCRedundantGetKSP_Redundant(PC pc,KSP *innerksp)
3464b9ad928SBarry Smith {
3475f06b7aaSBarry Smith   PetscErrorCode ierr;
3484b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
3495f06b7aaSBarry Smith   MPI_Comm       comm,subcomm;
3505f06b7aaSBarry Smith   const char     *prefix;
3514b9ad928SBarry Smith 
3524b9ad928SBarry Smith   PetscFunctionBegin;
3535f06b7aaSBarry Smith   if (!red->psubcomm) {
3545f06b7aaSBarry Smith     ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
355d8a68f86SHong Zhang     ierr = PetscSubcommCreate(comm,&red->psubcomm);CHKERRQ(ierr);
356d8a68f86SHong Zhang     ierr = PetscSubcommSetNumber(red->psubcomm,red->nsubcomm);CHKERRQ(ierr);
357d8a68f86SHong Zhang     ierr = PetscSubcommSetType(red->psubcomm,PETSC_SUBCOMM_INTERLACED);CHKERRQ(ierr);
3585f06b7aaSBarry Smith     ierr = PetscLogObjectMemory(pc,sizeof(PetscSubcomm));CHKERRQ(ierr);
3595f06b7aaSBarry Smith 
3605f06b7aaSBarry Smith     /* create a new PC that processors in each subcomm have copy of */
3615f06b7aaSBarry Smith     subcomm = red->psubcomm->comm;
3625f06b7aaSBarry Smith     ierr = KSPCreate(subcomm,&red->ksp);CHKERRQ(ierr);
3635f06b7aaSBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)red->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
3645f06b7aaSBarry Smith     ierr = PetscLogObjectParent(pc,red->ksp);CHKERRQ(ierr);
3655f06b7aaSBarry Smith     ierr = KSPSetType(red->ksp,KSPPREONLY);CHKERRQ(ierr);
3665f06b7aaSBarry Smith     ierr = KSPGetPC(red->ksp,&red->pc);CHKERRQ(ierr);
3675f06b7aaSBarry Smith     ierr = PCSetType(red->pc,PCLU);CHKERRQ(ierr);
3685f06b7aaSBarry Smith 
3695f06b7aaSBarry Smith     ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
3705f06b7aaSBarry Smith     ierr = KSPSetOptionsPrefix(red->ksp,prefix);CHKERRQ(ierr);
3715f06b7aaSBarry Smith     ierr = KSPAppendOptionsPrefix(red->ksp,"redundant_");CHKERRQ(ierr);
3725f06b7aaSBarry Smith   }
37383ab6a24SBarry Smith   *innerksp = red->ksp;
3744b9ad928SBarry Smith   PetscFunctionReturn(0);
3754b9ad928SBarry Smith }
3764b9ad928SBarry Smith EXTERN_C_END
3774b9ad928SBarry Smith 
3784b9ad928SBarry Smith #undef __FUNCT__
37983ab6a24SBarry Smith #define __FUNCT__ "PCRedundantGetKSP"
3804b9ad928SBarry Smith /*@
38183ab6a24SBarry Smith    PCRedundantGetKSP - Gets the less parallel KSP created by the redundant PC.
3824b9ad928SBarry Smith 
3834b9ad928SBarry Smith    Not Collective
3844b9ad928SBarry Smith 
3854b9ad928SBarry Smith    Input Parameter:
3864b9ad928SBarry Smith .  pc - the preconditioner context
3874b9ad928SBarry Smith 
3884b9ad928SBarry Smith    Output Parameter:
38983ab6a24SBarry Smith .  innerksp - the KSP on the smaller set of processes
3904b9ad928SBarry Smith 
3914b9ad928SBarry Smith    Level: advanced
3924b9ad928SBarry Smith 
3934b9ad928SBarry Smith .keywords: PC, redundant solve
3944b9ad928SBarry Smith @*/
39583ab6a24SBarry Smith PetscErrorCode  PCRedundantGetKSP(PC pc,KSP *innerksp)
3964b9ad928SBarry Smith {
3974ac538c5SBarry Smith   PetscErrorCode ierr;
3984b9ad928SBarry Smith 
3994b9ad928SBarry Smith   PetscFunctionBegin;
4000700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
40183ab6a24SBarry Smith   PetscValidPointer(innerksp,2);
40283ab6a24SBarry Smith   ierr = PetscTryMethod(pc,"PCRedundantGetKSP_C",(PC,KSP*),(pc,innerksp));CHKERRQ(ierr);
4034b9ad928SBarry Smith   PetscFunctionReturn(0);
4044b9ad928SBarry Smith }
4054b9ad928SBarry Smith 
4064b9ad928SBarry Smith EXTERN_C_BEGIN
4074b9ad928SBarry Smith #undef __FUNCT__
4084b9ad928SBarry Smith #define __FUNCT__ "PCRedundantGetOperators_Redundant"
4097087cfbeSBarry Smith PetscErrorCode  PCRedundantGetOperators_Redundant(PC pc,Mat *mat,Mat *pmat)
4104b9ad928SBarry Smith {
4114b9ad928SBarry Smith   PC_Redundant *red = (PC_Redundant*)pc->data;
4124b9ad928SBarry Smith 
4134b9ad928SBarry Smith   PetscFunctionBegin;
414b3804887SHong Zhang   if (mat)  *mat  = red->pmats;
415b3804887SHong Zhang   if (pmat) *pmat = red->pmats;
4164b9ad928SBarry Smith   PetscFunctionReturn(0);
4174b9ad928SBarry Smith }
4184b9ad928SBarry Smith EXTERN_C_END
4194b9ad928SBarry Smith 
4204b9ad928SBarry Smith #undef __FUNCT__
4214b9ad928SBarry Smith #define __FUNCT__ "PCRedundantGetOperators"
4224b9ad928SBarry Smith /*@
4234b9ad928SBarry Smith    PCRedundantGetOperators - gets the sequential matrix and preconditioner matrix
4244b9ad928SBarry Smith 
4254b9ad928SBarry Smith    Not Collective
4264b9ad928SBarry Smith 
4274b9ad928SBarry Smith    Input Parameter:
4284b9ad928SBarry Smith .  pc - the preconditioner context
4294b9ad928SBarry Smith 
4304b9ad928SBarry Smith    Output Parameters:
4314b9ad928SBarry Smith +  mat - the matrix
4324b9ad928SBarry Smith -  pmat - the (possibly different) preconditioner matrix
4334b9ad928SBarry Smith 
4344b9ad928SBarry Smith    Level: advanced
4354b9ad928SBarry Smith 
4364b9ad928SBarry Smith .keywords: PC, redundant solve
4374b9ad928SBarry Smith @*/
4387087cfbeSBarry Smith PetscErrorCode  PCRedundantGetOperators(PC pc,Mat *mat,Mat *pmat)
4394b9ad928SBarry Smith {
4404ac538c5SBarry Smith   PetscErrorCode ierr;
4414b9ad928SBarry Smith 
4424b9ad928SBarry Smith   PetscFunctionBegin;
4430700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
4444482741eSBarry Smith   if (mat)  PetscValidPointer(mat,2);
4454482741eSBarry Smith   if (pmat) PetscValidPointer(pmat,3);
4464ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCRedundantGetOperators_C",(PC,Mat*,Mat*),(pc,mat,pmat));CHKERRQ(ierr);
4474b9ad928SBarry Smith   PetscFunctionReturn(0);
4484b9ad928SBarry Smith }
4494b9ad928SBarry Smith 
4504b9ad928SBarry Smith /* -------------------------------------------------------------------------------------*/
45137a17b4dSBarry Smith /*MC
45283ab6a24SBarry Smith      PCREDUNDANT - Runs a KSP solver with preconditioner for the entire problem on subgroups of processors
45337a17b4dSBarry Smith 
45483ab6a24SBarry Smith      Options for the redundant preconditioners can be set with -redundant_pc_xxx for the redundant KSP with -redundant_ksp_xxx
45537a17b4dSBarry Smith 
45609391456SBarry Smith   Options Database:
4579b21d695SBarry Smith .  -pc_redundant_number <n> - number of redundant solves, for example if you are using 64 MPI processes and
4589b21d695SBarry Smith                               use an n of 4 there will be 4 parallel solves each on 16 = 64/4 processes.
45909391456SBarry Smith 
46037a17b4dSBarry Smith    Level: intermediate
46137a17b4dSBarry Smith 
46283ab6a24SBarry Smith    Notes: The default KSP is preonly and the default PC is LU.
46383ab6a24SBarry Smith 
46483ab6a24SBarry Smith    Developer Notes: Note that PCSetInitialGuessNonzero()  is not used by this class but likely should be.
4659cfaa89bSBarry Smith 
46637a17b4dSBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PCRedundantSetScatter(),
46783ab6a24SBarry Smith            PCRedundantGetKSP(), PCRedundantGetOperators(), PCRedundantSetNumber()
46837a17b4dSBarry Smith M*/
46937a17b4dSBarry Smith 
4704b9ad928SBarry Smith EXTERN_C_BEGIN
4714b9ad928SBarry Smith #undef __FUNCT__
4724b9ad928SBarry Smith #define __FUNCT__ "PCCreate_Redundant"
4737087cfbeSBarry Smith PetscErrorCode  PCCreate_Redundant(PC pc)
4744b9ad928SBarry Smith {
475dfbe8321SBarry Smith   PetscErrorCode ierr;
4764b9ad928SBarry Smith   PC_Redundant   *red;
47769db28dcSHong Zhang   PetscMPIInt    size;
4783f457be1SHong Zhang 
4794b9ad928SBarry Smith   PetscFunctionBegin;
48038f2d2fdSLisandro Dalcin   ierr = PetscNewLog(pc,PC_Redundant,&red);CHKERRQ(ierr);
4817adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)pc)->comm,&size);CHKERRQ(ierr);
48269db28dcSHong Zhang   red->nsubcomm       = size;
4834b9ad928SBarry Smith   red->useparallelmat = PETSC_TRUE;
4841fbd8f88SHong Zhang   pc->data            = (void*)red;
4854b9ad928SBarry Smith 
4864b9ad928SBarry Smith   pc->ops->apply           = PCApply_Redundant;
4874b9ad928SBarry Smith   pc->ops->applytranspose  = 0;
4884b9ad928SBarry Smith   pc->ops->setup           = PCSetUp_Redundant;
4894b9ad928SBarry Smith   pc->ops->destroy         = PCDestroy_Redundant;
4904b9ad928SBarry Smith   pc->ops->setfromoptions  = PCSetFromOptions_Redundant;
4914b9ad928SBarry Smith   pc->ops->view            = PCView_Redundant;
4924b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCRedundantSetScatter_C","PCRedundantSetScatter_Redundant",
4934b9ad928SBarry Smith                     PCRedundantSetScatter_Redundant);CHKERRQ(ierr);
49409a6bc64SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCRedundantSetNumber_C","PCRedundantSetNumber_Redundant",
49509a6bc64SHong Zhang                     PCRedundantSetNumber_Redundant);CHKERRQ(ierr);
49683ab6a24SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCRedundantGetKSP_C","PCRedundantGetKSP_Redundant",
49783ab6a24SBarry Smith                     PCRedundantGetKSP_Redundant);CHKERRQ(ierr);
4984b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCRedundantGetOperators_C","PCRedundantGetOperators_Redundant",
4994b9ad928SBarry Smith                     PCRedundantGetOperators_Redundant);CHKERRQ(ierr);
5004b9ad928SBarry Smith   PetscFunctionReturn(0);
5014b9ad928SBarry Smith }
5024b9ad928SBarry Smith EXTERN_C_END
503