xref: /petsc/src/ksp/pc/impls/redundant/redundant.c (revision c540e29ca36598c0bb6ebaa5114648bff8a6255c)
1dba47a55SKris Buschelman #define PETSCKSP_DLL
2dba47a55SKris Buschelman 
34b9ad928SBarry Smith /*
43f457be1SHong Zhang   This file defines a "solve the problem redundantly on each subgroup of processor" preconditioner.
54b9ad928SBarry Smith */
66356e834SBarry Smith #include "private/pcimpl.h"     /*I "petscpc.h" I*/
74b9ad928SBarry Smith #include "petscksp.h"
84b9ad928SBarry Smith 
94b9ad928SBarry Smith typedef struct {
104b9ad928SBarry Smith   PC           pc;                   /* actual preconditioner used on each processor */
113f457be1SHong Zhang   Vec          xsub,ysub;            /* vectors of a subcommunicator to hold parallel vectors of 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) */
154b9ad928SBarry Smith   PetscTruth   useparallelmat;
16*c540e29cSHong 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;
2613f74950SBarry Smith   PetscMPIInt    rank;
2732077d6dSBarry Smith   PetscTruth     iascii,isstring;
28a47c9f9aSHong Zhang   PetscViewer    sviewer,subviewer;
291fbd8f88SHong Zhang   PetscInt       color = red->psubcomm->color;
304b9ad928SBarry Smith 
314b9ad928SBarry Smith   PetscFunctionBegin;
324b9ad928SBarry Smith   ierr = MPI_Comm_rank(pc->comm,&rank);CHKERRQ(ierr);
3332077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
344b9ad928SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
3532077d6dSBarry Smith   if (iascii) {
36a98ce0f4SHong Zhang     ierr = PetscViewerASCIIPrintf(viewer,"  Redundant solver preconditioner: First PC (color=0) follows\n");CHKERRQ(ierr);
37a98ce0f4SHong Zhang     ierr = PetscViewerGetSubcomm(viewer,red->pc->comm,&subviewer);CHKERRQ(ierr);
38a98ce0f4SHong Zhang     if (!color) { /* only view first redundant pc */
394b9ad928SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
40a47c9f9aSHong Zhang       ierr = PCView(red->pc,subviewer);CHKERRQ(ierr);
414b9ad928SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
424b9ad928SBarry Smith     }
43a98ce0f4SHong Zhang     ierr = PetscViewerRestoreSubcomm(viewer,red->pc->comm,&subviewer);CHKERRQ(ierr);
44a98ce0f4SHong Zhang   } else if (isstring) { /* not test it yet! */
454b9ad928SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," Redundant solver preconditioner");CHKERRQ(ierr);
464b9ad928SBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
474b9ad928SBarry Smith     if (!rank) {
484b9ad928SBarry Smith       ierr = PCView(red->pc,sviewer);CHKERRQ(ierr);
494b9ad928SBarry Smith     }
504b9ad928SBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
514b9ad928SBarry Smith   } else {
5279a5c55eSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for PC redundant",((PetscObject)viewer)->type_name);
534b9ad928SBarry Smith   }
544b9ad928SBarry Smith   PetscFunctionReturn(0);
554b9ad928SBarry Smith }
564b9ad928SBarry Smith 
57b9147fbbSdalcinl #include "include/private/matimpl.h"        /*I "petscmat.h" I*/
584b9ad928SBarry Smith #undef __FUNCT__
594b9ad928SBarry Smith #define __FUNCT__ "PCSetUp_Redundant"
606849ba73SBarry Smith static PetscErrorCode PCSetUp_Redundant(PC pc)
614b9ad928SBarry Smith {
624b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
63dfbe8321SBarry Smith   PetscErrorCode ierr;
643f457be1SHong Zhang   PetscInt       mstart,mend,mlocal,m;
6513f74950SBarry Smith   PetscMPIInt    size;
664b9ad928SBarry Smith   MatReuse       reuse = MAT_INITIAL_MATRIX;
674b9ad928SBarry Smith   MatStructure   str   = DIFFERENT_NONZERO_PATTERN;
680d7810c8SBarry Smith   MPI_Comm       comm = pc->comm,subcomm;
6923ce1328SBarry Smith   Vec            vec;
703f457be1SHong Zhang   PetscInt       mlocal_sub;
713f457be1SHong Zhang   PetscMPIInt    subsize,subrank;
72e5a9bf91SBarry Smith   PetscInt       rstart_sub,rend_sub,mloc_sub;
731fbd8f88SHong Zhang   const char     *prefix;
743f457be1SHong Zhang 
754b9ad928SBarry Smith   PetscFunctionBegin;
7623ce1328SBarry Smith   ierr = MatGetVecs(pc->pmat,&vec,0);CHKERRQ(ierr);
7723ce1328SBarry Smith   ierr = VecGetSize(vec,&m);CHKERRQ(ierr);
781fbd8f88SHong Zhang 
794b9ad928SBarry Smith   if (!pc->setupcalled) {
801fbd8f88SHong Zhang     ierr = PetscSubcommCreate(comm,red->nsubcomm,&red->psubcomm);CHKERRQ(ierr);
811fbd8f88SHong Zhang     ierr = PetscLogObjectMemory(pc,sizeof(PetscSubcomm));CHKERRQ(ierr);
821fbd8f88SHong Zhang 
831fbd8f88SHong Zhang     /* create a new PC that processors in each subcomm have copy of */
840d7810c8SBarry Smith     subcomm = red->psubcomm->comm;
851fbd8f88SHong Zhang     ierr = PCCreate(subcomm,&red->pc);CHKERRQ(ierr);
861fbd8f88SHong Zhang     ierr = PCSetType(red->pc,PCLU);CHKERRQ(ierr);
871fbd8f88SHong Zhang     ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
881fbd8f88SHong Zhang     ierr = PCSetOptionsPrefix(red->pc,prefix);CHKERRQ(ierr);
891fbd8f88SHong Zhang     ierr = PCAppendOptionsPrefix(red->pc,"redundant_");CHKERRQ(ierr);
901fbd8f88SHong Zhang     ierr = PCSetFromOptions(red->pc);CHKERRQ(ierr);
911fbd8f88SHong Zhang 
923f457be1SHong Zhang     /* create working vectors xsub/ysub and xdup/ydup */
9323ce1328SBarry Smith     ierr = VecGetLocalSize(vec,&mlocal);CHKERRQ(ierr);
943f457be1SHong Zhang     ierr = VecGetOwnershipRange(vec,&mstart,&mend);CHKERRQ(ierr);
954b9ad928SBarry Smith 
963f457be1SHong Zhang     /* get local size of xsub/ysub */
971fbd8f88SHong Zhang     ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr);
981fbd8f88SHong Zhang     ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr);
991fbd8f88SHong Zhang     rstart_sub = pc->pmat->rmap.range[red->psubcomm->n*subrank]; /* rstart in xsub/ysub */
1003f457be1SHong Zhang     if (subrank+1 < subsize){
1011fbd8f88SHong Zhang       rend_sub = pc->pmat->rmap.range[red->psubcomm->n*(subrank+1)];
1023f457be1SHong Zhang     } else {
1033f457be1SHong Zhang       rend_sub = m;
1043f457be1SHong Zhang     }
1053f457be1SHong Zhang     mloc_sub = rend_sub - rstart_sub;
1061fbd8f88SHong Zhang     ierr = VecCreateMPI(subcomm,mloc_sub,PETSC_DECIDE,&red->ysub);CHKERRQ(ierr);
1073f457be1SHong Zhang     /* create xsub with empty local arrays, because xdup's arrays will be placed into it */
1081fbd8f88SHong Zhang     ierr = VecCreateMPIWithArray(subcomm,mloc_sub,PETSC_DECIDE,PETSC_NULL,&red->xsub);CHKERRQ(ierr);
1093f457be1SHong Zhang 
1103f457be1SHong Zhang     /* create xdup and ydup. ydup has empty local arrays because ysub's arrays will be place into it.
1113f457be1SHong Zhang        Note: we use communicator dupcomm, not pc->comm! */
1121fbd8f88SHong Zhang     ierr = VecCreateMPI(red->psubcomm->dupparent,mloc_sub,PETSC_DECIDE,&red->xdup);CHKERRQ(ierr);
1131fbd8f88SHong Zhang     ierr = VecCreateMPIWithArray(red->psubcomm->dupparent,mloc_sub,PETSC_DECIDE,PETSC_NULL,&red->ydup);CHKERRQ(ierr);
1143f457be1SHong Zhang 
1153f457be1SHong Zhang     /* create vec scatters */
1163f457be1SHong Zhang     if (!red->scatterin){
1173f457be1SHong Zhang       IS       is1,is2;
1183f457be1SHong Zhang       PetscInt *idx1,*idx2,i,j,k;
1191fbd8f88SHong Zhang       ierr = PetscMalloc(2*red->psubcomm->n*mlocal*sizeof(PetscInt),&idx1);CHKERRQ(ierr);
1201fbd8f88SHong Zhang       idx2 = idx1 + red->psubcomm->n*mlocal;
1213f457be1SHong Zhang       j = 0;
1221fbd8f88SHong Zhang       for (k=0; k<red->psubcomm->n; k++){
1233f457be1SHong Zhang         for (i=mstart; i<mend; i++){
1243f457be1SHong Zhang           idx1[j]   = i;
1253f457be1SHong Zhang           idx2[j++] = i + m*k;
1263f457be1SHong Zhang         }
1273f457be1SHong Zhang       }
1281fbd8f88SHong Zhang       ierr = ISCreateGeneral(comm,red->psubcomm->n*mlocal,idx1,&is1);CHKERRQ(ierr);
1291fbd8f88SHong Zhang       ierr = ISCreateGeneral(comm,red->psubcomm->n*mlocal,idx2,&is2);CHKERRQ(ierr);
1303f457be1SHong Zhang       ierr = VecScatterCreate(vec,is1,red->xdup,is2,&red->scatterin);CHKERRQ(ierr);
1313f457be1SHong Zhang       ierr = ISDestroy(is1);CHKERRQ(ierr);
1323f457be1SHong Zhang       ierr = ISDestroy(is2);CHKERRQ(ierr);
1333f457be1SHong Zhang 
1341fbd8f88SHong Zhang       ierr = ISCreateStride(comm,mlocal,mstart+ red->psubcomm->color*m,1,&is1);CHKERRQ(ierr);
1353f457be1SHong Zhang       ierr = ISCreateStride(comm,mlocal,mstart,1,&is2);CHKERRQ(ierr);
1363f457be1SHong Zhang       ierr = VecScatterCreate(red->xdup,is1,vec,is2,&red->scatterout);CHKERRQ(ierr);
1373f457be1SHong Zhang       ierr = ISDestroy(is1);CHKERRQ(ierr);
1383f457be1SHong Zhang       ierr = ISDestroy(is2);CHKERRQ(ierr);
1393f457be1SHong Zhang       ierr = PetscFree(idx1);CHKERRQ(ierr);
1404b9ad928SBarry Smith     }
1414b9ad928SBarry Smith   }
14223ce1328SBarry Smith   ierr = VecDestroy(vec);CHKERRQ(ierr);
1434b9ad928SBarry Smith 
1444b9ad928SBarry Smith   /* if pmatrix set by user is sequential then we do not need to gather the parallel matrix */
1453f457be1SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1464b9ad928SBarry Smith   if (size == 1) {
1474b9ad928SBarry Smith     red->useparallelmat = PETSC_FALSE;
1484b9ad928SBarry Smith   }
1494b9ad928SBarry Smith 
1504b9ad928SBarry Smith   if (red->useparallelmat) {
1514b9ad928SBarry Smith     if (pc->setupcalled == 1 && pc->flag == DIFFERENT_NONZERO_PATTERN) {
1524b9ad928SBarry Smith       /* destroy old matrices */
1534b9ad928SBarry Smith       if (red->pmats) {
154b3804887SHong Zhang         ierr = MatDestroy(red->pmats);CHKERRQ(ierr);
1554b9ad928SBarry Smith       }
1564b9ad928SBarry Smith     } else if (pc->setupcalled == 1) {
1574b9ad928SBarry Smith       reuse = MAT_REUSE_MATRIX;
1584b9ad928SBarry Smith       str   = SAME_NONZERO_PATTERN;
1594b9ad928SBarry Smith     }
1604b9ad928SBarry Smith 
1613f457be1SHong Zhang     /* grab the parallel matrix and put it into processors of a subcomminicator */
162f664ae05SHong Zhang     /*--------------------------------------------------------------------------*/
163f664ae05SHong Zhang     ierr = VecGetLocalSize(red->ysub,&mlocal_sub);CHKERRQ(ierr);
16469db28dcSHong Zhang     ierr = MatGetRedundantMatrix(pc->pmat,red->psubcomm->n,red->psubcomm->comm,mlocal_sub,reuse,&red->pmats);CHKERRQ(ierr);
16569db28dcSHong Zhang 
1663f457be1SHong Zhang     /* tell PC of the subcommunicator its operators */
167b3804887SHong Zhang     ierr = PCSetOperators(red->pc,red->pmats,red->pmats,str);CHKERRQ(ierr);
1684b9ad928SBarry Smith   } else {
1694b9ad928SBarry Smith     ierr = PCSetOperators(red->pc,pc->mat,pc->pmat,pc->flag);CHKERRQ(ierr);
1704b9ad928SBarry Smith   }
1714b9ad928SBarry Smith   ierr = PCSetFromOptions(red->pc);CHKERRQ(ierr);
1724b9ad928SBarry Smith   ierr = PCSetUp(red->pc);CHKERRQ(ierr);
1734b9ad928SBarry Smith   PetscFunctionReturn(0);
1744b9ad928SBarry Smith }
1754b9ad928SBarry Smith 
1764b9ad928SBarry Smith #undef __FUNCT__
1774b9ad928SBarry Smith #define __FUNCT__ "PCApply_Redundant"
1786849ba73SBarry Smith static PetscErrorCode PCApply_Redundant(PC pc,Vec x,Vec y)
1794b9ad928SBarry Smith {
1804b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
181dfbe8321SBarry Smith   PetscErrorCode ierr;
1823f457be1SHong Zhang   PetscScalar    *array;
1834b9ad928SBarry Smith 
1844b9ad928SBarry Smith   PetscFunctionBegin;
1853f457be1SHong Zhang   /* scatter x to xdup */
1863f457be1SHong Zhang   ierr = VecScatterBegin(x,red->xdup,INSERT_VALUES,SCATTER_FORWARD,red->scatterin);CHKERRQ(ierr);
1873f457be1SHong Zhang   ierr = VecScatterEnd(x,red->xdup,INSERT_VALUES,SCATTER_FORWARD,red->scatterin);CHKERRQ(ierr);
1883f457be1SHong Zhang 
1893f457be1SHong Zhang   /* place xdup's local array into xsub */
1903f457be1SHong Zhang   ierr = VecGetArray(red->xdup,&array);CHKERRQ(ierr);
1913f457be1SHong Zhang   ierr = VecPlaceArray(red->xsub,(const PetscScalar*)array);CHKERRQ(ierr);
1924b9ad928SBarry Smith 
1934b9ad928SBarry Smith   /* apply preconditioner on each processor */
1943f457be1SHong Zhang   ierr = PCApply(red->pc,red->xsub,red->ysub);CHKERRQ(ierr);
1953f457be1SHong Zhang   ierr = VecResetArray(red->xsub);CHKERRQ(ierr);
1963f457be1SHong Zhang   ierr = VecRestoreArray(red->xdup,&array);CHKERRQ(ierr);
1974b9ad928SBarry Smith 
1983f457be1SHong Zhang   /* place ysub's local array into ydup */
1993f457be1SHong Zhang   ierr = VecGetArray(red->ysub,&array);CHKERRQ(ierr);
2003f457be1SHong Zhang   ierr = VecPlaceArray(red->ydup,(const PetscScalar*)array);CHKERRQ(ierr);
2013f457be1SHong Zhang 
2023f457be1SHong Zhang   /* scatter ydup to y */
2033f457be1SHong Zhang   ierr = VecScatterBegin(red->ydup,y,INSERT_VALUES,SCATTER_FORWARD,red->scatterout);CHKERRQ(ierr);
2043f457be1SHong Zhang   ierr = VecScatterEnd(red->ydup,y,INSERT_VALUES,SCATTER_FORWARD,red->scatterout);CHKERRQ(ierr);
2053f457be1SHong Zhang   ierr = VecResetArray(red->ydup);CHKERRQ(ierr);
2063f457be1SHong Zhang   ierr = VecRestoreArray(red->ysub,&array);CHKERRQ(ierr);
2074b9ad928SBarry Smith   PetscFunctionReturn(0);
2084b9ad928SBarry Smith }
2094b9ad928SBarry Smith 
2104b9ad928SBarry Smith #undef __FUNCT__
2114b9ad928SBarry Smith #define __FUNCT__ "PCDestroy_Redundant"
2126849ba73SBarry Smith static PetscErrorCode PCDestroy_Redundant(PC pc)
2134b9ad928SBarry Smith {
2144b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
215dfbe8321SBarry Smith   PetscErrorCode ierr;
2164b9ad928SBarry Smith 
2174b9ad928SBarry Smith   PetscFunctionBegin;
2184b9ad928SBarry Smith   if (red->scatterin)  {ierr = VecScatterDestroy(red->scatterin);CHKERRQ(ierr);}
2194b9ad928SBarry Smith   if (red->scatterout) {ierr = VecScatterDestroy(red->scatterout);CHKERRQ(ierr);}
2203f457be1SHong Zhang   if (red->ysub)       {ierr = VecDestroy(red->ysub);CHKERRQ(ierr);}
2213f457be1SHong Zhang   if (red->xsub)       {ierr = VecDestroy(red->xsub);CHKERRQ(ierr);}
2223f457be1SHong Zhang   if (red->xdup)       {ierr = VecDestroy(red->xdup);CHKERRQ(ierr);}
2233f457be1SHong Zhang   if (red->ydup)       {ierr = VecDestroy(red->ydup);CHKERRQ(ierr);}
224b3804887SHong Zhang   if (red->pmats) {
225b3804887SHong Zhang     ierr = MatDestroy(red->pmats);CHKERRQ(ierr);
2263f457be1SHong Zhang   }
227e5a9bf91SBarry Smith   if (red->psubcomm) {ierr = PetscSubcommDestroy(red->psubcomm);CHKERRQ(ierr);}
228e5a9bf91SBarry Smith   if (red->pc) {ierr = PCDestroy(red->pc);CHKERRQ(ierr);}
2294b9ad928SBarry Smith   ierr = PetscFree(red);CHKERRQ(ierr);
2304b9ad928SBarry Smith   PetscFunctionReturn(0);
2314b9ad928SBarry Smith }
2324b9ad928SBarry Smith 
2334b9ad928SBarry Smith #undef __FUNCT__
2344b9ad928SBarry Smith #define __FUNCT__ "PCSetFromOptions_Redundant"
2356849ba73SBarry Smith static PetscErrorCode PCSetFromOptions_Redundant(PC pc)
2364b9ad928SBarry Smith {
237a98ce0f4SHong Zhang   PetscErrorCode ierr;
238a98ce0f4SHong Zhang   PC_Redundant   *red = (PC_Redundant*)pc->data;
239a98ce0f4SHong Zhang 
2404b9ad928SBarry Smith   PetscFunctionBegin;
241a98ce0f4SHong Zhang   ierr = PetscOptionsHead("Redundant options");CHKERRQ(ierr);
24209a6bc64SHong Zhang   ierr = PetscOptionsInt("-pc_redundant_number","Number of redundant pc","PCRedundantSetNumber",red->nsubcomm,&red->nsubcomm,0);CHKERRQ(ierr);
243a98ce0f4SHong Zhang   ierr = PetscOptionsTail();CHKERRQ(ierr);
2444b9ad928SBarry Smith   PetscFunctionReturn(0);
2454b9ad928SBarry Smith }
2464b9ad928SBarry Smith 
2474b9ad928SBarry Smith EXTERN_C_BEGIN
2484b9ad928SBarry Smith #undef __FUNCT__
24909a6bc64SHong Zhang #define __FUNCT__ "PCRedundantSetNumber_Redundant"
25009a6bc64SHong Zhang PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantSetNumber_Redundant(PC pc,PetscInt nreds)
25109a6bc64SHong Zhang {
25209a6bc64SHong Zhang   PC_Redundant *red = (PC_Redundant*)pc->data;
25309a6bc64SHong Zhang 
25409a6bc64SHong Zhang   PetscFunctionBegin;
25509a6bc64SHong Zhang   red->nsubcomm = nreds;
25609a6bc64SHong Zhang   PetscFunctionReturn(0);
25709a6bc64SHong Zhang }
25809a6bc64SHong Zhang EXTERN_C_END
25909a6bc64SHong Zhang 
26009a6bc64SHong Zhang #undef __FUNCT__
26109a6bc64SHong Zhang #define __FUNCT__ "PCRedundantSetNumber"
26209a6bc64SHong Zhang /*@
26309a6bc64SHong Zhang    PCRedundantSetNumber - Sets the number of redundant preconditioner contexts.
26409a6bc64SHong Zhang 
26509a6bc64SHong Zhang    Collective on PC
26609a6bc64SHong Zhang 
26709a6bc64SHong Zhang    Input Parameters:
26809a6bc64SHong Zhang +  pc - the preconditioner context
26909a6bc64SHong Zhang -  nredundant - number of redundant preconditioner contexts
27009a6bc64SHong Zhang 
27109a6bc64SHong Zhang    Level: advanced
27209a6bc64SHong Zhang 
27309a6bc64SHong Zhang .keywords: PC, redundant solve
27409a6bc64SHong Zhang @*/
27509a6bc64SHong Zhang PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantSetNumber(PC pc,PetscInt nredundant)
27609a6bc64SHong Zhang {
27709a6bc64SHong Zhang   PetscErrorCode ierr,(*f)(PC,PetscInt);
27809a6bc64SHong Zhang 
27909a6bc64SHong Zhang   PetscFunctionBegin;
28009a6bc64SHong Zhang   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
28109a6bc64SHong Zhang   if (nredundant <= 0) SETERRQ1(PETSC_ERR_ARG_WRONG, "num of redundant pc %D must be positive",nredundant);
28209a6bc64SHong Zhang   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCRedundantSetNumber_C",(void (**)(void))&f);CHKERRQ(ierr);
28309a6bc64SHong Zhang   if (f) {
28409a6bc64SHong Zhang     ierr = (*f)(pc,nredundant);CHKERRQ(ierr);
28509a6bc64SHong Zhang   }
28609a6bc64SHong Zhang   PetscFunctionReturn(0);
28709a6bc64SHong Zhang }
28809a6bc64SHong Zhang 
28909a6bc64SHong Zhang EXTERN_C_BEGIN
29009a6bc64SHong Zhang #undef __FUNCT__
2914b9ad928SBarry Smith #define __FUNCT__ "PCRedundantSetScatter_Redundant"
292dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantSetScatter_Redundant(PC pc,VecScatter in,VecScatter out)
2934b9ad928SBarry Smith {
2944b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
295dfbe8321SBarry Smith   PetscErrorCode ierr;
2964b9ad928SBarry Smith 
2974b9ad928SBarry Smith   PetscFunctionBegin;
2984b9ad928SBarry Smith   red->scatterin  = in;
2994b9ad928SBarry Smith   red->scatterout = out;
3004b9ad928SBarry Smith   ierr = PetscObjectReference((PetscObject)in);CHKERRQ(ierr);
3014b9ad928SBarry Smith   ierr = PetscObjectReference((PetscObject)out);CHKERRQ(ierr);
3024b9ad928SBarry Smith   PetscFunctionReturn(0);
3034b9ad928SBarry Smith }
3044b9ad928SBarry Smith EXTERN_C_END
3054b9ad928SBarry Smith 
3064b9ad928SBarry Smith #undef __FUNCT__
3074b9ad928SBarry Smith #define __FUNCT__ "PCRedundantSetScatter"
3084b9ad928SBarry Smith /*@
3094b9ad928SBarry Smith    PCRedundantSetScatter - Sets the scatter used to copy values into the
3104b9ad928SBarry Smith      redundant local solve and the scatter to move them back into the global
3114b9ad928SBarry Smith      vector.
3124b9ad928SBarry Smith 
3134b9ad928SBarry Smith    Collective on PC
3144b9ad928SBarry Smith 
3154b9ad928SBarry Smith    Input Parameters:
3164b9ad928SBarry Smith +  pc - the preconditioner context
3174b9ad928SBarry Smith .  in - the scatter to move the values in
3184b9ad928SBarry Smith -  out - the scatter to move them out
3194b9ad928SBarry Smith 
3204b9ad928SBarry Smith    Level: advanced
3214b9ad928SBarry Smith 
3224b9ad928SBarry Smith .keywords: PC, redundant solve
3234b9ad928SBarry Smith @*/
324dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantSetScatter(PC pc,VecScatter in,VecScatter out)
3254b9ad928SBarry Smith {
326dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(PC,VecScatter,VecScatter);
3274b9ad928SBarry Smith 
3284b9ad928SBarry Smith   PetscFunctionBegin;
3294482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
3304482741eSBarry Smith   PetscValidHeaderSpecific(in,VEC_SCATTER_COOKIE,2);
3314482741eSBarry Smith   PetscValidHeaderSpecific(out,VEC_SCATTER_COOKIE,3);
3324b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCRedundantSetScatter_C",(void (**)(void))&f);CHKERRQ(ierr);
3334b9ad928SBarry Smith   if (f) {
3344b9ad928SBarry Smith     ierr = (*f)(pc,in,out);CHKERRQ(ierr);
3354b9ad928SBarry Smith   }
3364b9ad928SBarry Smith   PetscFunctionReturn(0);
3374b9ad928SBarry Smith }
3384b9ad928SBarry Smith 
3394b9ad928SBarry Smith EXTERN_C_BEGIN
3404b9ad928SBarry Smith #undef __FUNCT__
3414b9ad928SBarry Smith #define __FUNCT__ "PCRedundantGetPC_Redundant"
342dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantGetPC_Redundant(PC pc,PC *innerpc)
3434b9ad928SBarry Smith {
3444b9ad928SBarry Smith   PC_Redundant *red = (PC_Redundant*)pc->data;
3454b9ad928SBarry Smith 
3464b9ad928SBarry Smith   PetscFunctionBegin;
3474b9ad928SBarry Smith   *innerpc = red->pc;
3484b9ad928SBarry Smith   PetscFunctionReturn(0);
3494b9ad928SBarry Smith }
3504b9ad928SBarry Smith EXTERN_C_END
3514b9ad928SBarry Smith 
3524b9ad928SBarry Smith #undef __FUNCT__
3534b9ad928SBarry Smith #define __FUNCT__ "PCRedundantGetPC"
3544b9ad928SBarry Smith /*@
3554b9ad928SBarry Smith    PCRedundantGetPC - Gets the sequential PC created by the redundant PC.
3564b9ad928SBarry Smith 
3574b9ad928SBarry Smith    Not Collective
3584b9ad928SBarry Smith 
3594b9ad928SBarry Smith    Input Parameter:
3604b9ad928SBarry Smith .  pc - the preconditioner context
3614b9ad928SBarry Smith 
3624b9ad928SBarry Smith    Output Parameter:
3634b9ad928SBarry Smith .  innerpc - the sequential PC
3644b9ad928SBarry Smith 
3654b9ad928SBarry Smith    Level: advanced
3664b9ad928SBarry Smith 
3674b9ad928SBarry Smith .keywords: PC, redundant solve
3684b9ad928SBarry Smith @*/
369dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantGetPC(PC pc,PC *innerpc)
3704b9ad928SBarry Smith {
371dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(PC,PC*);
3724b9ad928SBarry Smith 
3734b9ad928SBarry Smith   PetscFunctionBegin;
3744482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
3754482741eSBarry Smith   PetscValidPointer(innerpc,2);
3764b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCRedundantGetPC_C",(void (**)(void))&f);CHKERRQ(ierr);
3774b9ad928SBarry Smith   if (f) {
3784b9ad928SBarry Smith     ierr = (*f)(pc,innerpc);CHKERRQ(ierr);
3794b9ad928SBarry Smith   }
3804b9ad928SBarry Smith   PetscFunctionReturn(0);
3814b9ad928SBarry Smith }
3824b9ad928SBarry Smith 
3834b9ad928SBarry Smith EXTERN_C_BEGIN
3844b9ad928SBarry Smith #undef __FUNCT__
3854b9ad928SBarry Smith #define __FUNCT__ "PCRedundantGetOperators_Redundant"
386dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantGetOperators_Redundant(PC pc,Mat *mat,Mat *pmat)
3874b9ad928SBarry Smith {
3884b9ad928SBarry Smith   PC_Redundant *red = (PC_Redundant*)pc->data;
3894b9ad928SBarry Smith 
3904b9ad928SBarry Smith   PetscFunctionBegin;
391b3804887SHong Zhang   if (mat)  *mat  = red->pmats;
392b3804887SHong Zhang   if (pmat) *pmat = red->pmats;
3934b9ad928SBarry Smith   PetscFunctionReturn(0);
3944b9ad928SBarry Smith }
3954b9ad928SBarry Smith EXTERN_C_END
3964b9ad928SBarry Smith 
3974b9ad928SBarry Smith #undef __FUNCT__
3984b9ad928SBarry Smith #define __FUNCT__ "PCRedundantGetOperators"
3994b9ad928SBarry Smith /*@
4004b9ad928SBarry Smith    PCRedundantGetOperators - gets the sequential matrix and preconditioner matrix
4014b9ad928SBarry Smith 
4024b9ad928SBarry Smith    Not Collective
4034b9ad928SBarry Smith 
4044b9ad928SBarry Smith    Input Parameter:
4054b9ad928SBarry Smith .  pc - the preconditioner context
4064b9ad928SBarry Smith 
4074b9ad928SBarry Smith    Output Parameters:
4084b9ad928SBarry Smith +  mat - the matrix
4094b9ad928SBarry Smith -  pmat - the (possibly different) preconditioner matrix
4104b9ad928SBarry Smith 
4114b9ad928SBarry Smith    Level: advanced
4124b9ad928SBarry Smith 
4134b9ad928SBarry Smith .keywords: PC, redundant solve
4144b9ad928SBarry Smith @*/
415dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantGetOperators(PC pc,Mat *mat,Mat *pmat)
4164b9ad928SBarry Smith {
417dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(PC,Mat*,Mat*);
4184b9ad928SBarry Smith 
4194b9ad928SBarry Smith   PetscFunctionBegin;
4204482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
4214482741eSBarry Smith   if (mat)  PetscValidPointer(mat,2);
4224482741eSBarry Smith   if (pmat) PetscValidPointer(pmat,3);
4234b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCRedundantGetOperators_C",(void (**)(void))&f);CHKERRQ(ierr);
4244b9ad928SBarry Smith   if (f) {
4254b9ad928SBarry Smith     ierr = (*f)(pc,mat,pmat);CHKERRQ(ierr);
4264b9ad928SBarry Smith   }
4274b9ad928SBarry Smith   PetscFunctionReturn(0);
4284b9ad928SBarry Smith }
4294b9ad928SBarry Smith 
4304b9ad928SBarry Smith /* -------------------------------------------------------------------------------------*/
43137a17b4dSBarry Smith /*MC
432a98ce0f4SHong Zhang      PCREDUNDANT - Runs a preconditioner for the entire problem on subgroups of processors
43337a17b4dSBarry Smith 
43437a17b4dSBarry Smith      Options for the redundant preconditioners can be set with -redundant_pc_xxx
43537a17b4dSBarry Smith 
43609391456SBarry Smith   Options Database:
43709391456SBarry Smith .  -pc_redundant_number_comm - number of sub communicators to use
43809391456SBarry Smith 
43937a17b4dSBarry Smith    Level: intermediate
44037a17b4dSBarry Smith 
44137a17b4dSBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PCRedundantSetScatter(),
44237a17b4dSBarry Smith            PCRedundantGetPC(), PCRedundantGetOperators()
44337a17b4dSBarry Smith M*/
44437a17b4dSBarry Smith 
4454b9ad928SBarry Smith EXTERN_C_BEGIN
4464b9ad928SBarry Smith #undef __FUNCT__
4474b9ad928SBarry Smith #define __FUNCT__ "PCCreate_Redundant"
448dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCCreate_Redundant(PC pc)
4494b9ad928SBarry Smith {
450dfbe8321SBarry Smith   PetscErrorCode ierr;
4514b9ad928SBarry Smith   PC_Redundant   *red;
45269db28dcSHong Zhang   PetscMPIInt    size;
4533f457be1SHong Zhang 
4544b9ad928SBarry Smith   PetscFunctionBegin;
4554b9ad928SBarry Smith   ierr = PetscNew(PC_Redundant,&red);CHKERRQ(ierr);
45652e6d16bSBarry Smith   ierr = PetscLogObjectMemory(pc,sizeof(PC_Redundant));CHKERRQ(ierr);
45769db28dcSHong Zhang   ierr = MPI_Comm_size(pc->comm,&size);CHKERRQ(ierr);
45869db28dcSHong Zhang   red->nsubcomm       = size;
4594b9ad928SBarry Smith   red->useparallelmat = PETSC_TRUE;
4601fbd8f88SHong Zhang   pc->data            = (void*)red;
4614b9ad928SBarry Smith 
4624b9ad928SBarry Smith   pc->ops->apply           = PCApply_Redundant;
4634b9ad928SBarry Smith   pc->ops->applytranspose  = 0;
4644b9ad928SBarry Smith   pc->ops->setup           = PCSetUp_Redundant;
4654b9ad928SBarry Smith   pc->ops->destroy         = PCDestroy_Redundant;
4664b9ad928SBarry Smith   pc->ops->setfromoptions  = PCSetFromOptions_Redundant;
4674b9ad928SBarry Smith   pc->ops->view            = PCView_Redundant;
4684b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCRedundantSetScatter_C","PCRedundantSetScatter_Redundant",
4694b9ad928SBarry Smith                     PCRedundantSetScatter_Redundant);CHKERRQ(ierr);
47009a6bc64SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCRedundantSetNumber_C","PCRedundantSetNumber_Redundant",
47109a6bc64SHong Zhang                     PCRedundantSetNumber_Redundant);CHKERRQ(ierr);
4724b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCRedundantGetPC_C","PCRedundantGetPC_Redundant",
4734b9ad928SBarry Smith                     PCRedundantGetPC_Redundant);CHKERRQ(ierr);
4744b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCRedundantGetOperators_C","PCRedundantGetOperators_Redundant",
4754b9ad928SBarry Smith                     PCRedundantGetOperators_Redundant);CHKERRQ(ierr);
4764b9ad928SBarry Smith   PetscFunctionReturn(0);
4774b9ad928SBarry Smith }
4784b9ad928SBarry Smith EXTERN_C_END
479