xref: /petsc/src/ksp/pc/impls/redistribute/redistribute.c (revision c0decd05c6848b80907752eef350b55c8c90e696)
1df826632SBarry Smith 
2df826632SBarry Smith /*
3df826632SBarry Smith   This file defines a "solve the problem redistributely on each subgroup of processor" preconditioner.
4df826632SBarry Smith */
5af0996ceSBarry Smith #include <petsc/private/pcimpl.h>     /*I "petscksp.h" I*/
6c6db04a5SJed Brown #include <petscksp.h>
7df826632SBarry Smith 
8df826632SBarry Smith typedef struct {
9df826632SBarry Smith   KSP         ksp;
10df826632SBarry Smith   Vec         x,b;
11df826632SBarry Smith   VecScatter  scatter;
12911f9fe8SBarry Smith   IS          is;
13181dd334SBarry Smith   PetscInt    dcnt,*drows;    /* these are the local rows that have only diagonal entry */
14181dd334SBarry Smith   PetscScalar *diag;
15181dd334SBarry Smith   Vec         work;
16df826632SBarry Smith } PC_Redistribute;
17df826632SBarry Smith 
18df826632SBarry Smith static PetscErrorCode PCView_Redistribute(PC pc,PetscViewer viewer)
19df826632SBarry Smith {
20df826632SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
21df826632SBarry Smith   PetscErrorCode  ierr;
22ace3abfcSBarry Smith   PetscBool       iascii,isstring;
23181dd334SBarry Smith   PetscInt        ncnt,N;
24df826632SBarry Smith 
25df826632SBarry Smith   PetscFunctionBegin;
26251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
27251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
28df826632SBarry Smith   if (iascii) {
29b2566f29SBarry Smith     ierr = MPIU_Allreduce(&red->dcnt,&ncnt,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)pc));CHKERRQ(ierr);
300298fd71SBarry Smith     ierr = MatGetSize(pc->pmat,&N,NULL);CHKERRQ(ierr);
31572f72c7SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"    Number rows eliminated %D Percentage rows eliminated %g\n",ncnt,100.0*((PetscReal)ncnt)/((PetscReal)N));CHKERRQ(ierr);
32df826632SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  Redistribute preconditioner: \n");CHKERRQ(ierr);
33df826632SBarry Smith     ierr = KSPView(red->ksp,viewer);CHKERRQ(ierr);
34df826632SBarry Smith   } else if (isstring) {
35df826632SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," Redistribute preconditioner");CHKERRQ(ierr);
36df826632SBarry Smith     ierr = KSPView(red->ksp,viewer);CHKERRQ(ierr);
3711aeaf0aSBarry Smith   }
38df826632SBarry Smith   PetscFunctionReturn(0);
39df826632SBarry Smith }
40df826632SBarry Smith 
41df826632SBarry Smith static PetscErrorCode PCSetUp_Redistribute(PC pc)
42df826632SBarry Smith {
43df826632SBarry Smith   PC_Redistribute   *red = (PC_Redistribute*)pc->data;
44df826632SBarry Smith   PetscErrorCode    ierr;
45df826632SBarry Smith   MPI_Comm          comm;
46181dd334SBarry Smith   PetscInt          rstart,rend,i,nz,cnt,*rows,ncnt,dcnt,*drows;
4726283091SBarry Smith   PetscLayout       map,nmap;
485a586d82SBarry Smith   PetscMPIInt       size,imdex,tag,n;
490298fd71SBarry Smith   PetscInt          *source = NULL;
5076ec1555SBarry Smith   PetscMPIInt       *sizes = NULL,nrecvs;
51df826632SBarry Smith   PetscInt          j,nsends;
520298fd71SBarry Smith   PetscInt          *owner = NULL,*starts = NULL,count,slen;
53e8dd6687SHong Zhang   PetscInt          *rvalues,*svalues,recvtotal;
54df826632SBarry Smith   PetscMPIInt       *onodes1,*olengths1;
550298fd71SBarry Smith   MPI_Request       *send_waits = NULL,*recv_waits = NULL;
56df826632SBarry Smith   MPI_Status        recv_status,*send_status;
57181dd334SBarry Smith   Vec               tvec,diag;
58ca320bd4SBarry Smith   Mat               tmat;
59181dd334SBarry Smith   const PetscScalar *d;
60df826632SBarry Smith 
61df826632SBarry Smith   PetscFunctionBegin;
62dc9360f3SBarry Smith   if (pc->setupcalled) {
6323ee1639SBarry Smith     ierr = KSPGetOperators(red->ksp,NULL,&tmat);CHKERRQ(ierr);
647dae84e0SHong Zhang     ierr = MatCreateSubMatrix(pc->pmat,red->is,red->is,MAT_REUSE_MATRIX,&tmat);CHKERRQ(ierr);
6523ee1639SBarry Smith     ierr = KSPSetOperators(red->ksp,tmat,tmat);CHKERRQ(ierr);
66dc9360f3SBarry Smith   } else {
67b862ddfaSBarry Smith     PetscInt NN;
68b862ddfaSBarry Smith 
69df826632SBarry Smith     ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
70df826632SBarry Smith     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
71361c1e09SMatthew Knepley     ierr = PetscObjectGetNewTag((PetscObject)pc,&tag);CHKERRQ(ierr);
72df826632SBarry Smith 
73ca320bd4SBarry Smith     /* count non-diagonal rows on process */
74df826632SBarry Smith     ierr = MatGetOwnershipRange(pc->mat,&rstart,&rend);CHKERRQ(ierr);
75ca320bd4SBarry Smith     cnt  = 0;
76df826632SBarry Smith     for (i=rstart; i<rend; i++) {
770298fd71SBarry Smith       ierr = MatGetRow(pc->mat,i,&nz,NULL,NULL);CHKERRQ(ierr);
78df826632SBarry Smith       if (nz > 1) cnt++;
790298fd71SBarry Smith       ierr = MatRestoreRow(pc->mat,i,&nz,NULL,NULL);CHKERRQ(ierr);
80df826632SBarry Smith     }
81785e854fSJed Brown     ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr);
82854ce69bSBarry Smith     ierr = PetscMalloc1(rend - rstart - cnt,&drows);CHKERRQ(ierr);
83ca320bd4SBarry Smith 
84ca320bd4SBarry Smith     /* list non-diagonal rows on process */
85181dd334SBarry Smith     cnt = 0; dcnt = 0;
86df826632SBarry Smith     for (i=rstart; i<rend; i++) {
870298fd71SBarry Smith       ierr = MatGetRow(pc->mat,i,&nz,NULL,NULL);CHKERRQ(ierr);
88df826632SBarry Smith       if (nz > 1) rows[cnt++] = i;
89181dd334SBarry Smith       else drows[dcnt++] = i - rstart;
900298fd71SBarry Smith       ierr = MatRestoreRow(pc->mat,i,&nz,NULL,NULL);CHKERRQ(ierr);
91df826632SBarry Smith     }
92ca320bd4SBarry Smith 
9326283091SBarry Smith     /* create PetscLayout for non-diagonal rows on each process */
9426283091SBarry Smith     ierr   = PetscLayoutCreate(comm,&map);CHKERRQ(ierr);
9526283091SBarry Smith     ierr   = PetscLayoutSetLocalSize(map,cnt);CHKERRQ(ierr);
9626283091SBarry Smith     ierr   = PetscLayoutSetBlockSize(map,1);CHKERRQ(ierr);
9726283091SBarry Smith     ierr   = PetscLayoutSetUp(map);CHKERRQ(ierr);
98df826632SBarry Smith     rstart = map->rstart;
99df826632SBarry Smith     rend   = map->rend;
100df826632SBarry Smith 
10126283091SBarry Smith     /* create PetscLayout for load-balanced non-diagonal rows on each process */
10226283091SBarry Smith     ierr = PetscLayoutCreate(comm,&nmap);CHKERRQ(ierr);
103b2566f29SBarry Smith     ierr = MPIU_Allreduce(&cnt,&ncnt,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
10426283091SBarry Smith     ierr = PetscLayoutSetSize(nmap,ncnt);CHKERRQ(ierr);
10526283091SBarry Smith     ierr = PetscLayoutSetBlockSize(nmap,1);CHKERRQ(ierr);
10626283091SBarry Smith     ierr = PetscLayoutSetUp(nmap);CHKERRQ(ierr);
107df826632SBarry Smith 
1080298fd71SBarry Smith     ierr = MatGetSize(pc->pmat,&NN,NULL);CHKERRQ(ierr);
1093bf036e2SBarry Smith     ierr = PetscInfo2(pc,"Number of diagonal rows eliminated %d, percentage eliminated %g\n",NN-ncnt,((PetscReal)(NN-ncnt))/((PetscReal)(NN)));CHKERRQ(ierr);
110ca320bd4SBarry Smith     /*
111ca320bd4SBarry Smith         this code is taken from VecScatterCreate_PtoS()
112ca320bd4SBarry Smith         Determines what rows need to be moved where to
113ca320bd4SBarry Smith         load balance the non-diagonal rows
114ca320bd4SBarry Smith     */
115df826632SBarry Smith     /*  count number of contributors to each processor */
116037dbc42SBarry Smith     ierr   = PetscMalloc2(size,&sizes,cnt,&owner);CHKERRQ(ierr);
11776ec1555SBarry Smith     ierr   = PetscMemzero(sizes,size*sizeof(PetscMPIInt));CHKERRQ(ierr);
118df826632SBarry Smith     j      = 0;
119df826632SBarry Smith     nsends = 0;
120df826632SBarry Smith     for (i=rstart; i<rend; i++) {
121df826632SBarry Smith       if (i < nmap->range[j]) j = 0;
122df826632SBarry Smith       for (; j<size; j++) {
123df826632SBarry Smith         if (i < nmap->range[j+1]) {
12476ec1555SBarry Smith           if (!sizes[j]++) nsends++;
125ca320bd4SBarry Smith           owner[i-rstart] = j;
126df826632SBarry Smith           break;
127df826632SBarry Smith         }
128df826632SBarry Smith       }
129df826632SBarry Smith     }
130df826632SBarry Smith     /* inform other processors of number of messages and max length*/
13176ec1555SBarry Smith     ierr      = PetscGatherNumberOfMessages(comm,NULL,sizes,&nrecvs);CHKERRQ(ierr);
13276ec1555SBarry Smith     ierr      = PetscGatherMessageLengths(comm,nsends,nrecvs,sizes,&onodes1,&olengths1);CHKERRQ(ierr);
133df826632SBarry Smith     ierr      = PetscSortMPIIntWithArray(nrecvs,onodes1,olengths1);CHKERRQ(ierr);
134df826632SBarry Smith     recvtotal = 0; for (i=0; i<nrecvs; i++) recvtotal += olengths1[i];
135df826632SBarry Smith 
136df826632SBarry Smith     /* post receives:  rvalues - rows I will own; count - nu */
137dcca6d9dSJed Brown     ierr  = PetscMalloc3(recvtotal,&rvalues,nrecvs,&source,nrecvs,&recv_waits);CHKERRQ(ierr);
138df826632SBarry Smith     count = 0;
139df826632SBarry Smith     for (i=0; i<nrecvs; i++) {
140df826632SBarry Smith       ierr   = MPI_Irecv((rvalues+count),olengths1[i],MPIU_INT,onodes1[i],tag,comm,recv_waits+i);CHKERRQ(ierr);
141df826632SBarry Smith       count += olengths1[i];
142df826632SBarry Smith     }
143df826632SBarry Smith 
144df826632SBarry Smith     /* do sends:
145df826632SBarry Smith        1) starts[i] gives the starting index in svalues for stuff going to
146df826632SBarry Smith        the ith processor
147df826632SBarry Smith     */
148dcca6d9dSJed Brown     ierr      = PetscMalloc3(cnt,&svalues,nsends,&send_waits,size,&starts);CHKERRQ(ierr);
149df826632SBarry Smith     starts[0] = 0;
15076ec1555SBarry Smith     for (i=1; i<size; i++) starts[i] = starts[i-1] + sizes[i-1];
1512fa5cd67SKarl Rupp     for (i=0; i<cnt; i++)  svalues[starts[owner[i]]++] = rows[i];
152181dd334SBarry Smith     for (i=0; i<cnt; i++)  rows[i] = rows[i] - rstart;
153181dd334SBarry Smith     red->drows = drows;
154181dd334SBarry Smith     red->dcnt  = dcnt;
155ca320bd4SBarry Smith     ierr       = PetscFree(rows);CHKERRQ(ierr);
156181dd334SBarry Smith 
157df826632SBarry Smith     starts[0] = 0;
15876ec1555SBarry Smith     for (i=1; i<size; i++) starts[i] = starts[i-1] + sizes[i-1];
159df826632SBarry Smith     count = 0;
160df826632SBarry Smith     for (i=0; i<size; i++) {
16176ec1555SBarry Smith       if (sizes[i]) {
16276ec1555SBarry Smith         ierr = MPI_Isend(svalues+starts[i],sizes[i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
163df826632SBarry Smith       }
164df826632SBarry Smith     }
165df826632SBarry Smith 
166df826632SBarry Smith     /*  wait on receives */
167df826632SBarry Smith     count = nrecvs;
168df826632SBarry Smith     slen  = 0;
169df826632SBarry Smith     while (count) {
170df826632SBarry Smith       ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
171df826632SBarry Smith       /* unpack receives into our local space */
172df826632SBarry Smith       ierr  = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
173df826632SBarry Smith       slen += n;
174df826632SBarry Smith       count--;
175df826632SBarry Smith     }
176e32f2f54SBarry Smith     if (slen != recvtotal) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Total message lengths %D not expected %D",slen,recvtotal);
177df826632SBarry Smith 
17870b3c8c7SBarry Smith     ierr = ISCreateGeneral(comm,slen,rvalues,PETSC_COPY_VALUES,&red->is);CHKERRQ(ierr);
179911f9fe8SBarry Smith 
180ca320bd4SBarry Smith     /* free up all work space */
181df826632SBarry Smith     ierr = PetscFree(olengths1);CHKERRQ(ierr);
182df826632SBarry Smith     ierr = PetscFree(onodes1);CHKERRQ(ierr);
183df826632SBarry Smith     ierr = PetscFree3(rvalues,source,recv_waits);CHKERRQ(ierr);
18476ec1555SBarry Smith     ierr = PetscFree2(sizes,owner);CHKERRQ(ierr);
185ca320bd4SBarry Smith     if (nsends) {   /* wait on sends */
186785e854fSJed Brown       ierr = PetscMalloc1(nsends,&send_status);CHKERRQ(ierr);
187df826632SBarry Smith       ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
188df826632SBarry Smith       ierr = PetscFree(send_status);CHKERRQ(ierr);
189df826632SBarry Smith     }
190df826632SBarry Smith     ierr = PetscFree3(svalues,send_waits,starts);CHKERRQ(ierr);
1916bf464f9SBarry Smith     ierr = PetscLayoutDestroy(&map);CHKERRQ(ierr);
1926bf464f9SBarry Smith     ierr = PetscLayoutDestroy(&nmap);CHKERRQ(ierr);
193df826632SBarry Smith 
194ca320bd4SBarry Smith     ierr = VecCreateMPI(comm,slen,PETSC_DETERMINE,&red->b);CHKERRQ(ierr);
195ca320bd4SBarry Smith     ierr = VecDuplicate(red->b,&red->x);CHKERRQ(ierr);
1962a7a6963SBarry Smith     ierr = MatCreateVecs(pc->pmat,&tvec,NULL);CHKERRQ(ierr);
19735928de7SBarry Smith     ierr = VecScatterCreateWithData(tvec,red->is,red->b,NULL,&red->scatter);CHKERRQ(ierr);
1986bf464f9SBarry Smith     ierr = VecDestroy(&tvec);CHKERRQ(ierr);
1997dae84e0SHong Zhang     ierr = MatCreateSubMatrix(pc->pmat,red->is,red->is,MAT_INITIAL_MATRIX,&tmat);CHKERRQ(ierr);
20023ee1639SBarry Smith     ierr = KSPSetOperators(red->ksp,tmat,tmat);CHKERRQ(ierr);
2016bf464f9SBarry Smith     ierr = MatDestroy(&tmat);CHKERRQ(ierr);
2021d805cfdSBarry Smith   }
203181dd334SBarry Smith 
204181dd334SBarry Smith   /* get diagonal portion of matrix */
20591e38401SBarry Smith   ierr = PetscFree(red->diag);CHKERRQ(ierr);
206785e854fSJed Brown   ierr = PetscMalloc1(red->dcnt,&red->diag);CHKERRQ(ierr);
2072a7a6963SBarry Smith   ierr = MatCreateVecs(pc->pmat,&diag,NULL);CHKERRQ(ierr);
208181dd334SBarry Smith   ierr = MatGetDiagonal(pc->pmat,diag);CHKERRQ(ierr);
2093649974fSBarry Smith   ierr = VecGetArrayRead(diag,&d);CHKERRQ(ierr);
2102fa5cd67SKarl Rupp   for (i=0; i<red->dcnt; i++) red->diag[i] = 1.0/d[red->drows[i]];
2113649974fSBarry Smith   ierr = VecRestoreArrayRead(diag,&d);CHKERRQ(ierr);
2126bf464f9SBarry Smith   ierr = VecDestroy(&diag);CHKERRQ(ierr);
213df826632SBarry Smith   ierr = KSPSetUp(red->ksp);CHKERRQ(ierr);
214df826632SBarry Smith   PetscFunctionReturn(0);
215df826632SBarry Smith }
216df826632SBarry Smith 
217df826632SBarry Smith static PetscErrorCode PCApply_Redistribute(PC pc,Vec b,Vec x)
218df826632SBarry Smith {
219df826632SBarry Smith   PC_Redistribute   *red = (PC_Redistribute*)pc->data;
220df826632SBarry Smith   PetscErrorCode    ierr;
221181dd334SBarry Smith   PetscInt          dcnt   = red->dcnt,i;
222181dd334SBarry Smith   const PetscInt    *drows = red->drows;
223181dd334SBarry Smith   PetscScalar       *xwork;
224181dd334SBarry Smith   const PetscScalar *bwork,*diag = red->diag;
225df826632SBarry Smith 
226df826632SBarry Smith   PetscFunctionBegin;
227181dd334SBarry Smith   if (!red->work) {
228181dd334SBarry Smith     ierr = VecDuplicate(b,&red->work);CHKERRQ(ierr);
229181dd334SBarry Smith   }
230181dd334SBarry Smith   /* compute the rows of solution that have diagonal entries only */
231181dd334SBarry Smith   ierr = VecSet(x,0.0);CHKERRQ(ierr);         /* x = diag(A)^{-1} b */
232181dd334SBarry Smith   ierr = VecGetArray(x,&xwork);CHKERRQ(ierr);
2333649974fSBarry Smith   ierr = VecGetArrayRead(b,&bwork);CHKERRQ(ierr);
2342fa5cd67SKarl Rupp   for (i=0; i<dcnt; i++) xwork[drows[i]] = diag[i]*bwork[drows[i]];
235181dd334SBarry Smith   ierr = PetscLogFlops(dcnt);CHKERRQ(ierr);
236181dd334SBarry Smith   ierr = VecRestoreArray(red->work,&xwork);CHKERRQ(ierr);
2373649974fSBarry Smith   ierr = VecRestoreArrayRead(b,&bwork);CHKERRQ(ierr);
238181dd334SBarry Smith   /* update the right hand side for the reduced system with diagonal rows (and corresponding columns) removed */
239181dd334SBarry Smith   ierr = MatMult(pc->pmat,x,red->work);CHKERRQ(ierr);
240181dd334SBarry Smith   ierr = VecAYPX(red->work,-1.0,b);CHKERRQ(ierr);   /* red->work = b - A x */
241181dd334SBarry Smith 
242181dd334SBarry Smith   ierr = VecScatterBegin(red->scatter,red->work,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
243181dd334SBarry Smith   ierr = VecScatterEnd(red->scatter,red->work,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
244df826632SBarry Smith   ierr = KSPSolve(red->ksp,red->b,red->x);CHKERRQ(ierr);
245*c0decd05SBarry Smith   ierr = KSPCheckSolve(red->ksp,pc,red->x);CHKERRQ(ierr);
246df826632SBarry Smith   ierr = VecScatterBegin(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
247df826632SBarry Smith   ierr = VecScatterEnd(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
248df826632SBarry Smith   PetscFunctionReturn(0);
249df826632SBarry Smith }
250df826632SBarry Smith 
251df826632SBarry Smith static PetscErrorCode PCDestroy_Redistribute(PC pc)
252df826632SBarry Smith {
253df826632SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
254df826632SBarry Smith   PetscErrorCode  ierr;
255df826632SBarry Smith 
256df826632SBarry Smith   PetscFunctionBegin;
257fcfd50ebSBarry Smith   ierr = VecScatterDestroy(&red->scatter);CHKERRQ(ierr);
258fcfd50ebSBarry Smith   ierr = ISDestroy(&red->is);CHKERRQ(ierr);
259fcfd50ebSBarry Smith   ierr = VecDestroy(&red->b);CHKERRQ(ierr);
260fcfd50ebSBarry Smith   ierr = VecDestroy(&red->x);CHKERRQ(ierr);
261fcfd50ebSBarry Smith   ierr = KSPDestroy(&red->ksp);CHKERRQ(ierr);
262fcfd50ebSBarry Smith   ierr = VecDestroy(&red->work);CHKERRQ(ierr);
2633bf036e2SBarry Smith   ierr = PetscFree(red->drows);CHKERRQ(ierr);
2643bf036e2SBarry Smith   ierr = PetscFree(red->diag);CHKERRQ(ierr);
265c31cb41cSBarry Smith   ierr = PetscFree(pc->data);CHKERRQ(ierr);
266df826632SBarry Smith   PetscFunctionReturn(0);
267df826632SBarry Smith }
268df826632SBarry Smith 
2694416b707SBarry Smith static PetscErrorCode PCSetFromOptions_Redistribute(PetscOptionItems *PetscOptionsObject,PC pc)
270df826632SBarry Smith {
271df826632SBarry Smith   PetscErrorCode  ierr;
272df826632SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
273df826632SBarry Smith 
274df826632SBarry Smith   PetscFunctionBegin;
275df826632SBarry Smith   ierr = KSPSetFromOptions(red->ksp);CHKERRQ(ierr);
276df826632SBarry Smith   PetscFunctionReturn(0);
277df826632SBarry Smith }
278df826632SBarry Smith 
2795e7ef714SBarry Smith /*@
2805e7ef714SBarry Smith    PCRedistributeGetKSP - Gets the KSP created by the PCREDISTRIBUTE
2815e7ef714SBarry Smith 
2825e7ef714SBarry Smith    Not Collective
2835e7ef714SBarry Smith 
2845e7ef714SBarry Smith    Input Parameter:
2855e7ef714SBarry Smith .  pc - the preconditioner context
2865e7ef714SBarry Smith 
2875e7ef714SBarry Smith    Output Parameter:
2885e7ef714SBarry Smith .  innerksp - the inner KSP
2895e7ef714SBarry Smith 
2905e7ef714SBarry Smith    Level: advanced
2915e7ef714SBarry Smith 
2925e7ef714SBarry Smith .keywords: PC, redistribute solve
2935e7ef714SBarry Smith @*/
2945e7ef714SBarry Smith PetscErrorCode  PCRedistributeGetKSP(PC pc,KSP *innerksp)
2955e7ef714SBarry Smith {
2965e7ef714SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
2975e7ef714SBarry Smith 
2985e7ef714SBarry Smith   PetscFunctionBegin;
2995e7ef714SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
3005e7ef714SBarry Smith   PetscValidPointer(innerksp,2);
3015e7ef714SBarry Smith   *innerksp = red->ksp;
3025e7ef714SBarry Smith   PetscFunctionReturn(0);
3035e7ef714SBarry Smith }
3045e7ef714SBarry Smith 
305df826632SBarry Smith /* -------------------------------------------------------------------------------------*/
306df826632SBarry Smith /*MC
307181dd334SBarry Smith      PCREDISTRIBUTE - Redistributes a matrix for load balancing, removing the rows that only have a diagonal entry and then applys a KSP to that new matrix
308df826632SBarry Smith 
309df826632SBarry Smith      Options for the redistribute preconditioners can be set with -redistribute_ksp_xxx <values> and -redistribute_pc_xxx <values>
310df826632SBarry Smith 
31195452b02SPatrick Sanan      Notes:
31295452b02SPatrick Sanan     Usually run this with -ksp_type preonly
313181dd334SBarry Smith 
314181dd334SBarry Smith      If you have used MatZeroRows() to eliminate (for example, Dirichlet) boundary conditions for a symmetric problem then you can use, for example, -ksp_type preonly
315181dd334SBarry Smith      -pc_type redistribute -redistribute_ksp_type cg -redistribute_pc_type bjacobi -redistribute_sub_pc_type icc to take advantage of the symmetry.
316181dd334SBarry Smith 
3172dfef595SBarry Smith      This does NOT call a partitioner to reorder rows to lower communication; the ordering of the rows in the original matrix and redistributed matrix is the same.
3182dfef595SBarry Smith 
31995452b02SPatrick Sanan      Developer Notes:
32095452b02SPatrick Sanan     Should add an option to this preconditioner to use a partitioner to redistribute the rows to lower communication.
3212dfef595SBarry Smith 
322df826632SBarry Smith    Level: intermediate
323df826632SBarry Smith 
3245e7ef714SBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PCRedistributeGetKSP()
325df826632SBarry Smith M*/
326df826632SBarry Smith 
3278cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_Redistribute(PC pc)
328df826632SBarry Smith {
329df826632SBarry Smith   PetscErrorCode  ierr;
330df826632SBarry Smith   PC_Redistribute *red;
331911f9fe8SBarry Smith   const char      *prefix;
332df826632SBarry Smith 
333df826632SBarry Smith   PetscFunctionBegin;
334b00a9115SJed Brown   ierr     = PetscNewLog(pc,&red);CHKERRQ(ierr);
335df826632SBarry Smith   pc->data = (void*)red;
336df826632SBarry Smith 
337df826632SBarry Smith   pc->ops->apply          = PCApply_Redistribute;
338df826632SBarry Smith   pc->ops->applytranspose = 0;
339df826632SBarry Smith   pc->ops->setup          = PCSetUp_Redistribute;
340df826632SBarry Smith   pc->ops->destroy        = PCDestroy_Redistribute;
341df826632SBarry Smith   pc->ops->setfromoptions = PCSetFromOptions_Redistribute;
342df826632SBarry Smith   pc->ops->view           = PCView_Redistribute;
343911f9fe8SBarry Smith 
344ce94432eSBarry Smith   ierr = KSPCreate(PetscObjectComm((PetscObject)pc),&red->ksp);CHKERRQ(ierr);
345422a814eSBarry Smith   ierr = KSPSetErrorIfNotConverged(red->ksp,pc->erroriffailure);CHKERRQ(ierr);
346911f9fe8SBarry Smith   ierr = PetscObjectIncrementTabLevel((PetscObject)red->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
3473bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)red->ksp);CHKERRQ(ierr);
348911f9fe8SBarry Smith   ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
349911f9fe8SBarry Smith   ierr = KSPSetOptionsPrefix(red->ksp,prefix);CHKERRQ(ierr);
350911f9fe8SBarry Smith   ierr = KSPAppendOptionsPrefix(red->ksp,"redistribute_");CHKERRQ(ierr);
351df826632SBarry Smith   PetscFunctionReturn(0);
352df826632SBarry Smith }
353