xref: /petsc/src/ksp/pc/impls/redundant/redundant.c (revision 0d7810c853017fa53c3d82acf107fc8509d995a6)
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;
161fbd8f88SHong 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;
68*0d7810c8SBarry Smith   MPI_Comm       comm = pc->comm,subcomm;
6923ce1328SBarry Smith   Vec            vec;
703f457be1SHong Zhang   PetscInt       mlocal_sub;
713f457be1SHong Zhang   PetscMPIInt    subsize,subrank;
721fbd8f88SHong Zhang   PetscInt       rstart_sub,rend_sub,mloc_sub,nsubcomm;
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 */
84*0d7810c8SBarry 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   }
2271fbd8f88SHong Zhang   ierr = PetscSubcommDestroy(red->psubcomm);CHKERRQ(ierr);
2284b9ad928SBarry Smith   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;
2391fbd8f88SHong Zhang   PetscMPIInt    size;
240a98ce0f4SHong Zhang 
2414b9ad928SBarry Smith   PetscFunctionBegin;
242a98ce0f4SHong Zhang   ierr = PetscOptionsHead("Redundant options");CHKERRQ(ierr);
24369db28dcSHong Zhang   ierr = PetscOptionsInt("-pc_redundant_number_comm","Number of subcommunicators","PCRedundantSetNumComm",red->nsubcomm,&red->nsubcomm,0);CHKERRQ(ierr);
244a98ce0f4SHong Zhang   ierr = PetscOptionsTail();CHKERRQ(ierr);
2454b9ad928SBarry Smith   PetscFunctionReturn(0);
2464b9ad928SBarry Smith }
2474b9ad928SBarry Smith 
2484b9ad928SBarry Smith EXTERN_C_BEGIN
2494b9ad928SBarry Smith #undef __FUNCT__
2504b9ad928SBarry Smith #define __FUNCT__ "PCRedundantSetScatter_Redundant"
251dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantSetScatter_Redundant(PC pc,VecScatter in,VecScatter out)
2524b9ad928SBarry Smith {
2534b9ad928SBarry Smith   PC_Redundant   *red = (PC_Redundant*)pc->data;
254dfbe8321SBarry Smith   PetscErrorCode ierr;
2554b9ad928SBarry Smith 
2564b9ad928SBarry Smith   PetscFunctionBegin;
2574b9ad928SBarry Smith   red->scatterin  = in;
2584b9ad928SBarry Smith   red->scatterout = out;
2594b9ad928SBarry Smith   ierr = PetscObjectReference((PetscObject)in);CHKERRQ(ierr);
2604b9ad928SBarry Smith   ierr = PetscObjectReference((PetscObject)out);CHKERRQ(ierr);
2614b9ad928SBarry Smith   PetscFunctionReturn(0);
2624b9ad928SBarry Smith }
2634b9ad928SBarry Smith EXTERN_C_END
2644b9ad928SBarry Smith 
2654b9ad928SBarry Smith #undef __FUNCT__
2664b9ad928SBarry Smith #define __FUNCT__ "PCRedundantSetScatter"
2674b9ad928SBarry Smith /*@
2684b9ad928SBarry Smith    PCRedundantSetScatter - Sets the scatter used to copy values into the
2694b9ad928SBarry Smith      redundant local solve and the scatter to move them back into the global
2704b9ad928SBarry Smith      vector.
2714b9ad928SBarry Smith 
2724b9ad928SBarry Smith    Collective on PC
2734b9ad928SBarry Smith 
2744b9ad928SBarry Smith    Input Parameters:
2754b9ad928SBarry Smith +  pc - the preconditioner context
2764b9ad928SBarry Smith .  in - the scatter to move the values in
2774b9ad928SBarry Smith -  out - the scatter to move them out
2784b9ad928SBarry Smith 
2794b9ad928SBarry Smith    Level: advanced
2804b9ad928SBarry Smith 
2814b9ad928SBarry Smith .keywords: PC, redundant solve
2824b9ad928SBarry Smith @*/
283dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantSetScatter(PC pc,VecScatter in,VecScatter out)
2844b9ad928SBarry Smith {
285dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(PC,VecScatter,VecScatter);
2864b9ad928SBarry Smith 
2874b9ad928SBarry Smith   PetscFunctionBegin;
2884482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
2894482741eSBarry Smith   PetscValidHeaderSpecific(in,VEC_SCATTER_COOKIE,2);
2904482741eSBarry Smith   PetscValidHeaderSpecific(out,VEC_SCATTER_COOKIE,3);
2914b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCRedundantSetScatter_C",(void (**)(void))&f);CHKERRQ(ierr);
2924b9ad928SBarry Smith   if (f) {
2934b9ad928SBarry Smith     ierr = (*f)(pc,in,out);CHKERRQ(ierr);
2944b9ad928SBarry Smith   }
2954b9ad928SBarry Smith   PetscFunctionReturn(0);
2964b9ad928SBarry Smith }
2974b9ad928SBarry Smith 
2984b9ad928SBarry Smith EXTERN_C_BEGIN
2994b9ad928SBarry Smith #undef __FUNCT__
3004b9ad928SBarry Smith #define __FUNCT__ "PCRedundantGetPC_Redundant"
301dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantGetPC_Redundant(PC pc,PC *innerpc)
3024b9ad928SBarry Smith {
3034b9ad928SBarry Smith   PC_Redundant *red = (PC_Redundant*)pc->data;
3044b9ad928SBarry Smith 
3054b9ad928SBarry Smith   PetscFunctionBegin;
3064b9ad928SBarry Smith   *innerpc = red->pc;
3074b9ad928SBarry Smith   PetscFunctionReturn(0);
3084b9ad928SBarry Smith }
3094b9ad928SBarry Smith EXTERN_C_END
3104b9ad928SBarry Smith 
3114b9ad928SBarry Smith #undef __FUNCT__
3124b9ad928SBarry Smith #define __FUNCT__ "PCRedundantGetPC"
3134b9ad928SBarry Smith /*@
3144b9ad928SBarry Smith    PCRedundantGetPC - Gets the sequential PC created by the redundant PC.
3154b9ad928SBarry Smith 
3164b9ad928SBarry Smith    Not Collective
3174b9ad928SBarry Smith 
3184b9ad928SBarry Smith    Input Parameter:
3194b9ad928SBarry Smith .  pc - the preconditioner context
3204b9ad928SBarry Smith 
3214b9ad928SBarry Smith    Output Parameter:
3224b9ad928SBarry Smith .  innerpc - the sequential PC
3234b9ad928SBarry Smith 
3244b9ad928SBarry Smith    Level: advanced
3254b9ad928SBarry Smith 
3264b9ad928SBarry Smith .keywords: PC, redundant solve
3274b9ad928SBarry Smith @*/
328dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantGetPC(PC pc,PC *innerpc)
3294b9ad928SBarry Smith {
330dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(PC,PC*);
3314b9ad928SBarry Smith 
3324b9ad928SBarry Smith   PetscFunctionBegin;
3334482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
3344482741eSBarry Smith   PetscValidPointer(innerpc,2);
3354b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCRedundantGetPC_C",(void (**)(void))&f);CHKERRQ(ierr);
3364b9ad928SBarry Smith   if (f) {
3374b9ad928SBarry Smith     ierr = (*f)(pc,innerpc);CHKERRQ(ierr);
3384b9ad928SBarry Smith   }
3394b9ad928SBarry Smith   PetscFunctionReturn(0);
3404b9ad928SBarry Smith }
3414b9ad928SBarry Smith 
3424b9ad928SBarry Smith EXTERN_C_BEGIN
3434b9ad928SBarry Smith #undef __FUNCT__
3444b9ad928SBarry Smith #define __FUNCT__ "PCRedundantGetOperators_Redundant"
345dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantGetOperators_Redundant(PC pc,Mat *mat,Mat *pmat)
3464b9ad928SBarry Smith {
3474b9ad928SBarry Smith   PC_Redundant *red = (PC_Redundant*)pc->data;
3484b9ad928SBarry Smith 
3494b9ad928SBarry Smith   PetscFunctionBegin;
350b3804887SHong Zhang   if (mat)  *mat  = red->pmats;
351b3804887SHong Zhang   if (pmat) *pmat = red->pmats;
3524b9ad928SBarry Smith   PetscFunctionReturn(0);
3534b9ad928SBarry Smith }
3544b9ad928SBarry Smith EXTERN_C_END
3554b9ad928SBarry Smith 
3564b9ad928SBarry Smith #undef __FUNCT__
3574b9ad928SBarry Smith #define __FUNCT__ "PCRedundantGetOperators"
3584b9ad928SBarry Smith /*@
3594b9ad928SBarry Smith    PCRedundantGetOperators - gets the sequential matrix and preconditioner matrix
3604b9ad928SBarry Smith 
3614b9ad928SBarry Smith    Not Collective
3624b9ad928SBarry Smith 
3634b9ad928SBarry Smith    Input Parameter:
3644b9ad928SBarry Smith .  pc - the preconditioner context
3654b9ad928SBarry Smith 
3664b9ad928SBarry Smith    Output Parameters:
3674b9ad928SBarry Smith +  mat - the matrix
3684b9ad928SBarry Smith -  pmat - the (possibly different) preconditioner matrix
3694b9ad928SBarry Smith 
3704b9ad928SBarry Smith    Level: advanced
3714b9ad928SBarry Smith 
3724b9ad928SBarry Smith .keywords: PC, redundant solve
3734b9ad928SBarry Smith @*/
374dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantGetOperators(PC pc,Mat *mat,Mat *pmat)
3754b9ad928SBarry Smith {
376dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(PC,Mat*,Mat*);
3774b9ad928SBarry Smith 
3784b9ad928SBarry Smith   PetscFunctionBegin;
3794482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
3804482741eSBarry Smith   if (mat)  PetscValidPointer(mat,2);
3814482741eSBarry Smith   if (pmat) PetscValidPointer(pmat,3);
3824b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCRedundantGetOperators_C",(void (**)(void))&f);CHKERRQ(ierr);
3834b9ad928SBarry Smith   if (f) {
3844b9ad928SBarry Smith     ierr = (*f)(pc,mat,pmat);CHKERRQ(ierr);
3854b9ad928SBarry Smith   }
3864b9ad928SBarry Smith   PetscFunctionReturn(0);
3874b9ad928SBarry Smith }
3884b9ad928SBarry Smith 
3894b9ad928SBarry Smith /* -------------------------------------------------------------------------------------*/
39037a17b4dSBarry Smith /*MC
391a98ce0f4SHong Zhang      PCREDUNDANT - Runs a preconditioner for the entire problem on subgroups of processors
39237a17b4dSBarry Smith 
39337a17b4dSBarry Smith      Options for the redundant preconditioners can be set with -redundant_pc_xxx
39437a17b4dSBarry Smith 
39509391456SBarry Smith   Options Database:
39609391456SBarry Smith .  -pc_redundant_number_comm - number of sub communicators to use
39709391456SBarry Smith 
39837a17b4dSBarry Smith    Level: intermediate
39937a17b4dSBarry Smith 
40037a17b4dSBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PCRedundantSetScatter(),
40137a17b4dSBarry Smith            PCRedundantGetPC(), PCRedundantGetOperators()
40237a17b4dSBarry Smith M*/
40337a17b4dSBarry Smith 
4044b9ad928SBarry Smith EXTERN_C_BEGIN
4054b9ad928SBarry Smith #undef __FUNCT__
4064b9ad928SBarry Smith #define __FUNCT__ "PCCreate_Redundant"
407dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCCreate_Redundant(PC pc)
4084b9ad928SBarry Smith {
409dfbe8321SBarry Smith   PetscErrorCode ierr;
4104b9ad928SBarry Smith   PC_Redundant   *red;
41169db28dcSHong Zhang   PetscMPIInt    size;
4123f457be1SHong Zhang 
4134b9ad928SBarry Smith   PetscFunctionBegin;
4144b9ad928SBarry Smith   ierr = PetscNew(PC_Redundant,&red);CHKERRQ(ierr);
41552e6d16bSBarry Smith   ierr = PetscLogObjectMemory(pc,sizeof(PC_Redundant));CHKERRQ(ierr);
41669db28dcSHong Zhang   ierr = MPI_Comm_size(pc->comm,&size);CHKERRQ(ierr);
41769db28dcSHong Zhang   red->nsubcomm       = size;
4184b9ad928SBarry Smith   red->useparallelmat = PETSC_TRUE;
4191fbd8f88SHong Zhang   pc->data            = (void*)red;
4204b9ad928SBarry Smith 
4214b9ad928SBarry Smith   pc->ops->apply           = PCApply_Redundant;
4224b9ad928SBarry Smith   pc->ops->applytranspose  = 0;
4234b9ad928SBarry Smith   pc->ops->setup           = PCSetUp_Redundant;
4244b9ad928SBarry Smith   pc->ops->destroy         = PCDestroy_Redundant;
4254b9ad928SBarry Smith   pc->ops->setfromoptions  = PCSetFromOptions_Redundant;
4264b9ad928SBarry Smith   pc->ops->view            = PCView_Redundant;
4274b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCRedundantSetScatter_C","PCRedundantSetScatter_Redundant",
4284b9ad928SBarry Smith                     PCRedundantSetScatter_Redundant);CHKERRQ(ierr);
4294b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCRedundantGetPC_C","PCRedundantGetPC_Redundant",
4304b9ad928SBarry Smith                     PCRedundantGetPC_Redundant);CHKERRQ(ierr);
4314b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCRedundantGetOperators_C","PCRedundantGetOperators_Redundant",
4324b9ad928SBarry Smith                     PCRedundantGetOperators_Redundant);CHKERRQ(ierr);
4334b9ad928SBarry Smith   PetscFunctionReturn(0);
4344b9ad928SBarry Smith }
4354b9ad928SBarry Smith EXTERN_C_END
436