xref: /petsc/src/ksp/pc/impls/redistribute/redistribute.c (revision 003249c0bc070cfc45870f038e1df11823daa9d2)
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;
48ec4bef21SJose E. Roman   PetscMPIInt              size,tag,n;
49ec4bef21SJose E. Roman   PETSC_UNUSED PetscMPIInt imdex;
500298fd71SBarry Smith   PetscInt                 *source = NULL;
5176ec1555SBarry Smith   PetscMPIInt              *sizes = NULL,nrecvs;
52df826632SBarry Smith   PetscInt                 j,nsends;
530298fd71SBarry Smith   PetscInt                 *owner = NULL,*starts = NULL,count,slen;
54e8dd6687SHong Zhang   PetscInt                 *rvalues,*svalues,recvtotal;
55df826632SBarry Smith   PetscMPIInt              *onodes1,*olengths1;
560298fd71SBarry Smith   MPI_Request              *send_waits = NULL,*recv_waits = NULL;
57df826632SBarry Smith   MPI_Status               recv_status,*send_status;
58181dd334SBarry Smith   Vec                      tvec,diag;
59ca320bd4SBarry Smith   Mat                      tmat;
60*003249c0SBarry Smith   const PetscScalar        *d,*values;
61*003249c0SBarry Smith   const PetscInt           *cols;
62df826632SBarry Smith 
63df826632SBarry Smith   PetscFunctionBegin;
64dc9360f3SBarry Smith   if (pc->setupcalled) {
6523ee1639SBarry Smith     ierr = KSPGetOperators(red->ksp,NULL,&tmat);CHKERRQ(ierr);
667dae84e0SHong Zhang     ierr = MatCreateSubMatrix(pc->pmat,red->is,red->is,MAT_REUSE_MATRIX,&tmat);CHKERRQ(ierr);
6723ee1639SBarry Smith     ierr = KSPSetOperators(red->ksp,tmat,tmat);CHKERRQ(ierr);
68dc9360f3SBarry Smith   } else {
69b862ddfaSBarry Smith     PetscInt NN;
70b862ddfaSBarry Smith 
71df826632SBarry Smith     ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
72ffc4695bSBarry Smith     ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr);
73361c1e09SMatthew Knepley     ierr = PetscObjectGetNewTag((PetscObject)pc,&tag);CHKERRQ(ierr);
74df826632SBarry Smith 
75ca320bd4SBarry Smith     /* count non-diagonal rows on process */
76df826632SBarry Smith     ierr = MatGetOwnershipRange(pc->mat,&rstart,&rend);CHKERRQ(ierr);
77ca320bd4SBarry Smith     cnt  = 0;
78df826632SBarry Smith     for (i=rstart; i<rend; i++) {
79*003249c0SBarry Smith       ierr = MatGetRow(pc->mat,i,&nz,&cols,&values);CHKERRQ(ierr);
80*003249c0SBarry Smith       for (PetscInt j=0; j<nz; j++) {
81*003249c0SBarry Smith         if (values[j] != 0 && cols[j] != i) {
82*003249c0SBarry Smith           cnt++;
83*003249c0SBarry Smith           break;
84*003249c0SBarry Smith         }
85*003249c0SBarry Smith       }
86*003249c0SBarry Smith       ierr = MatRestoreRow(pc->mat,i,&nz,&cols,&values);CHKERRQ(ierr);
87df826632SBarry Smith     }
88785e854fSJed Brown     ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr);
89854ce69bSBarry Smith     ierr = PetscMalloc1(rend - rstart - cnt,&drows);CHKERRQ(ierr);
90ca320bd4SBarry Smith 
91ca320bd4SBarry Smith     /* list non-diagonal rows on process */
92181dd334SBarry Smith     cnt = 0; dcnt = 0;
93df826632SBarry Smith     for (i=rstart; i<rend; i++) {
94*003249c0SBarry Smith       PetscBool diagonly = PETSC_TRUE;
95*003249c0SBarry Smith       ierr = MatGetRow(pc->mat,i,&nz,&cols,&values);CHKERRQ(ierr);
96*003249c0SBarry Smith       for (PetscInt j=0; j<nz; j++) {
97*003249c0SBarry Smith         if (values[j] != 0 && cols[j] != i) {
98*003249c0SBarry Smith           diagonly = PETSC_FALSE;
99*003249c0SBarry Smith           break;
100*003249c0SBarry Smith         }
101*003249c0SBarry Smith       }
102*003249c0SBarry Smith       if (!diagonly) rows[cnt++] = i;
103181dd334SBarry Smith       else drows[dcnt++] = i - rstart;
104*003249c0SBarry Smith       ierr = MatRestoreRow(pc->mat,i,&nz,&cols,&values);CHKERRQ(ierr);
105df826632SBarry Smith     }
106ca320bd4SBarry Smith 
10726283091SBarry Smith     /* create PetscLayout for non-diagonal rows on each process */
10826283091SBarry Smith     ierr   = PetscLayoutCreate(comm,&map);CHKERRQ(ierr);
10926283091SBarry Smith     ierr   = PetscLayoutSetLocalSize(map,cnt);CHKERRQ(ierr);
11026283091SBarry Smith     ierr   = PetscLayoutSetBlockSize(map,1);CHKERRQ(ierr);
11126283091SBarry Smith     ierr   = PetscLayoutSetUp(map);CHKERRQ(ierr);
112df826632SBarry Smith     rstart = map->rstart;
113df826632SBarry Smith     rend   = map->rend;
114df826632SBarry Smith 
11526283091SBarry Smith     /* create PetscLayout for load-balanced non-diagonal rows on each process */
11626283091SBarry Smith     ierr = PetscLayoutCreate(comm,&nmap);CHKERRQ(ierr);
117b2566f29SBarry Smith     ierr = MPIU_Allreduce(&cnt,&ncnt,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
11826283091SBarry Smith     ierr = PetscLayoutSetSize(nmap,ncnt);CHKERRQ(ierr);
11926283091SBarry Smith     ierr = PetscLayoutSetBlockSize(nmap,1);CHKERRQ(ierr);
12026283091SBarry Smith     ierr = PetscLayoutSetUp(nmap);CHKERRQ(ierr);
121df826632SBarry Smith 
1220298fd71SBarry Smith     ierr = MatGetSize(pc->pmat,&NN,NULL);CHKERRQ(ierr);
1233bf036e2SBarry Smith     ierr = PetscInfo2(pc,"Number of diagonal rows eliminated %d, percentage eliminated %g\n",NN-ncnt,((PetscReal)(NN-ncnt))/((PetscReal)(NN)));CHKERRQ(ierr);
124*003249c0SBarry Smith 
125*003249c0SBarry Smith     if (size > 1) {
126*003249c0SBarry Smith       /* the following block of code assumes MPI can send messages to self, which is not supported for MPI-uni hence we need to handle the size 1 case as a special case */
127ca320bd4SBarry Smith       /*
128ca320bd4SBarry Smith        this code is taken from VecScatterCreate_PtoS()
129ca320bd4SBarry Smith        Determines what rows need to be moved where to
130ca320bd4SBarry Smith        load balance the non-diagonal rows
131ca320bd4SBarry Smith        */
132df826632SBarry Smith       /*  count number of contributors to each processor */
133037dbc42SBarry Smith       ierr   = PetscMalloc2(size,&sizes,cnt,&owner);CHKERRQ(ierr);
134580bdb30SBarry Smith       ierr   = PetscArrayzero(sizes,size);CHKERRQ(ierr);
135df826632SBarry Smith       j      = 0;
136df826632SBarry Smith       nsends = 0;
137df826632SBarry Smith       for (i=rstart; i<rend; i++) {
138df826632SBarry Smith         if (i < nmap->range[j]) j = 0;
139df826632SBarry Smith         for (; j<size; j++) {
140df826632SBarry Smith           if (i < nmap->range[j+1]) {
14176ec1555SBarry Smith             if (!sizes[j]++) nsends++;
142ca320bd4SBarry Smith             owner[i-rstart] = j;
143df826632SBarry Smith             break;
144df826632SBarry Smith           }
145df826632SBarry Smith         }
146df826632SBarry Smith       }
147df826632SBarry Smith       /* inform other processors of number of messages and max length*/
14876ec1555SBarry Smith       ierr      = PetscGatherNumberOfMessages(comm,NULL,sizes,&nrecvs);CHKERRQ(ierr);
14976ec1555SBarry Smith       ierr      = PetscGatherMessageLengths(comm,nsends,nrecvs,sizes,&onodes1,&olengths1);CHKERRQ(ierr);
150df826632SBarry Smith       ierr      = PetscSortMPIIntWithArray(nrecvs,onodes1,olengths1);CHKERRQ(ierr);
151df826632SBarry Smith       recvtotal = 0; for (i=0; i<nrecvs; i++) recvtotal += olengths1[i];
152df826632SBarry Smith 
153df826632SBarry Smith       /* post receives:  rvalues - rows I will own; count - nu */
154dcca6d9dSJed Brown       ierr  = PetscMalloc3(recvtotal,&rvalues,nrecvs,&source,nrecvs,&recv_waits);CHKERRQ(ierr);
155df826632SBarry Smith       count = 0;
156df826632SBarry Smith       for (i=0; i<nrecvs; i++) {
15755b25c41SPierre Jolivet         ierr   = MPI_Irecv((rvalues+count),olengths1[i],MPIU_INT,onodes1[i],tag,comm,recv_waits+i);CHKERRMPI(ierr);
158df826632SBarry Smith         count += olengths1[i];
159df826632SBarry Smith       }
160df826632SBarry Smith 
161df826632SBarry Smith       /* do sends:
162df826632SBarry Smith        1) starts[i] gives the starting index in svalues for stuff going to
163df826632SBarry Smith        the ith processor
164df826632SBarry Smith        */
165dcca6d9dSJed Brown       ierr      = PetscMalloc3(cnt,&svalues,nsends,&send_waits,size,&starts);CHKERRQ(ierr);
166df826632SBarry Smith       starts[0] = 0;
16776ec1555SBarry Smith       for (i=1; i<size; i++) starts[i] = starts[i-1] + sizes[i-1];
1682fa5cd67SKarl Rupp       for (i=0; i<cnt; i++)  svalues[starts[owner[i]]++] = rows[i];
169181dd334SBarry Smith       for (i=0; i<cnt; i++)  rows[i] = rows[i] - rstart;
170181dd334SBarry Smith       red->drows = drows;
171181dd334SBarry Smith       red->dcnt  = dcnt;
172ca320bd4SBarry Smith       ierr       = PetscFree(rows);CHKERRQ(ierr);
173181dd334SBarry Smith 
174df826632SBarry Smith       starts[0] = 0;
17576ec1555SBarry Smith       for (i=1; i<size; i++) starts[i] = starts[i-1] + sizes[i-1];
176df826632SBarry Smith       count = 0;
177df826632SBarry Smith       for (i=0; i<size; i++) {
17876ec1555SBarry Smith         if (sizes[i]) {
179ffc4695bSBarry Smith           ierr = MPI_Isend(svalues+starts[i],sizes[i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRMPI(ierr);
180df826632SBarry Smith         }
181df826632SBarry Smith       }
182df826632SBarry Smith 
183df826632SBarry Smith       /*  wait on receives */
184df826632SBarry Smith       count = nrecvs;
185df826632SBarry Smith       slen  = 0;
186df826632SBarry Smith       while (count) {
187ffc4695bSBarry Smith         ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRMPI(ierr);
188df826632SBarry Smith         /* unpack receives into our local space */
18955b25c41SPierre Jolivet         ierr  = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRMPI(ierr);
190df826632SBarry Smith         slen += n;
191df826632SBarry Smith         count--;
192df826632SBarry Smith       }
193e32f2f54SBarry Smith       if (slen != recvtotal) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Total message lengths %D not expected %D",slen,recvtotal);
19470b3c8c7SBarry Smith       ierr = ISCreateGeneral(comm,slen,rvalues,PETSC_COPY_VALUES,&red->is);CHKERRQ(ierr);
195911f9fe8SBarry Smith 
196*003249c0SBarry Smith       /* free all work space */
197df826632SBarry Smith       ierr = PetscFree(olengths1);CHKERRQ(ierr);
198df826632SBarry Smith       ierr = PetscFree(onodes1);CHKERRQ(ierr);
199df826632SBarry Smith       ierr = PetscFree3(rvalues,source,recv_waits);CHKERRQ(ierr);
20076ec1555SBarry Smith       ierr = PetscFree2(sizes,owner);CHKERRQ(ierr);
201ca320bd4SBarry Smith       if (nsends) {   /* wait on sends */
202785e854fSJed Brown         ierr = PetscMalloc1(nsends,&send_status);CHKERRQ(ierr);
203ffc4695bSBarry Smith         ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRMPI(ierr);
204df826632SBarry Smith         ierr = PetscFree(send_status);CHKERRQ(ierr);
205df826632SBarry Smith       }
206df826632SBarry Smith       ierr = PetscFree3(svalues,send_waits,starts);CHKERRQ(ierr);
207*003249c0SBarry Smith     } else {
208*003249c0SBarry Smith       ierr = ISCreateGeneral(comm,cnt,rows,PETSC_OWN_POINTER,&red->is);CHKERRQ(ierr);
209*003249c0SBarry Smith       red->drows = drows;
210*003249c0SBarry Smith       red->dcnt  = dcnt;
211*003249c0SBarry Smith       slen = cnt;
212*003249c0SBarry Smith     }
2136bf464f9SBarry Smith     ierr = PetscLayoutDestroy(&map);CHKERRQ(ierr);
2146bf464f9SBarry Smith     ierr = PetscLayoutDestroy(&nmap);CHKERRQ(ierr);
215df826632SBarry Smith 
216ca320bd4SBarry Smith     ierr = VecCreateMPI(comm,slen,PETSC_DETERMINE,&red->b);CHKERRQ(ierr);
217ca320bd4SBarry Smith     ierr = VecDuplicate(red->b,&red->x);CHKERRQ(ierr);
2182a7a6963SBarry Smith     ierr = MatCreateVecs(pc->pmat,&tvec,NULL);CHKERRQ(ierr);
2199448b7f1SJunchao Zhang     ierr = VecScatterCreate(tvec,red->is,red->b,NULL,&red->scatter);CHKERRQ(ierr);
2206bf464f9SBarry Smith     ierr = VecDestroy(&tvec);CHKERRQ(ierr);
2217dae84e0SHong Zhang     ierr = MatCreateSubMatrix(pc->pmat,red->is,red->is,MAT_INITIAL_MATRIX,&tmat);CHKERRQ(ierr);
22223ee1639SBarry Smith     ierr = KSPSetOperators(red->ksp,tmat,tmat);CHKERRQ(ierr);
2236bf464f9SBarry Smith     ierr = MatDestroy(&tmat);CHKERRQ(ierr);
2241d805cfdSBarry Smith   }
225181dd334SBarry Smith 
226181dd334SBarry Smith   /* get diagonal portion of matrix */
22791e38401SBarry Smith   ierr = PetscFree(red->diag);CHKERRQ(ierr);
228785e854fSJed Brown   ierr = PetscMalloc1(red->dcnt,&red->diag);CHKERRQ(ierr);
2292a7a6963SBarry Smith   ierr = MatCreateVecs(pc->pmat,&diag,NULL);CHKERRQ(ierr);
230181dd334SBarry Smith   ierr = MatGetDiagonal(pc->pmat,diag);CHKERRQ(ierr);
2313649974fSBarry Smith   ierr = VecGetArrayRead(diag,&d);CHKERRQ(ierr);
2322fa5cd67SKarl Rupp   for (i=0; i<red->dcnt; i++) red->diag[i] = 1.0/d[red->drows[i]];
2333649974fSBarry Smith   ierr = VecRestoreArrayRead(diag,&d);CHKERRQ(ierr);
2346bf464f9SBarry Smith   ierr = VecDestroy(&diag);CHKERRQ(ierr);
235df826632SBarry Smith   ierr = KSPSetUp(red->ksp);CHKERRQ(ierr);
236df826632SBarry Smith   PetscFunctionReturn(0);
237df826632SBarry Smith }
238df826632SBarry Smith 
239df826632SBarry Smith static PetscErrorCode PCApply_Redistribute(PC pc,Vec b,Vec x)
240df826632SBarry Smith {
241df826632SBarry Smith   PC_Redistribute   *red = (PC_Redistribute*)pc->data;
242df826632SBarry Smith   PetscErrorCode    ierr;
243181dd334SBarry Smith   PetscInt          dcnt   = red->dcnt,i;
244181dd334SBarry Smith   const PetscInt    *drows = red->drows;
245181dd334SBarry Smith   PetscScalar       *xwork;
246181dd334SBarry Smith   const PetscScalar *bwork,*diag = red->diag;
247df826632SBarry Smith 
248df826632SBarry Smith   PetscFunctionBegin;
249181dd334SBarry Smith   if (!red->work) {
250181dd334SBarry Smith     ierr = VecDuplicate(b,&red->work);CHKERRQ(ierr);
251181dd334SBarry Smith   }
252181dd334SBarry Smith   /* compute the rows of solution that have diagonal entries only */
253181dd334SBarry Smith   ierr = VecSet(x,0.0);CHKERRQ(ierr);         /* x = diag(A)^{-1} b */
254181dd334SBarry Smith   ierr = VecGetArray(x,&xwork);CHKERRQ(ierr);
2553649974fSBarry Smith   ierr = VecGetArrayRead(b,&bwork);CHKERRQ(ierr);
2562fa5cd67SKarl Rupp   for (i=0; i<dcnt; i++) xwork[drows[i]] = diag[i]*bwork[drows[i]];
257181dd334SBarry Smith   ierr = PetscLogFlops(dcnt);CHKERRQ(ierr);
258181dd334SBarry Smith   ierr = VecRestoreArray(red->work,&xwork);CHKERRQ(ierr);
2593649974fSBarry Smith   ierr = VecRestoreArrayRead(b,&bwork);CHKERRQ(ierr);
260181dd334SBarry Smith   /* update the right hand side for the reduced system with diagonal rows (and corresponding columns) removed */
261181dd334SBarry Smith   ierr = MatMult(pc->pmat,x,red->work);CHKERRQ(ierr);
262181dd334SBarry Smith   ierr = VecAYPX(red->work,-1.0,b);CHKERRQ(ierr);   /* red->work = b - A x */
263181dd334SBarry Smith 
264181dd334SBarry Smith   ierr = VecScatterBegin(red->scatter,red->work,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
265181dd334SBarry Smith   ierr = VecScatterEnd(red->scatter,red->work,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
266df826632SBarry Smith   ierr = KSPSolve(red->ksp,red->b,red->x);CHKERRQ(ierr);
267c0decd05SBarry Smith   ierr = KSPCheckSolve(red->ksp,pc,red->x);CHKERRQ(ierr);
268df826632SBarry Smith   ierr = VecScatterBegin(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
269df826632SBarry Smith   ierr = VecScatterEnd(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
270df826632SBarry Smith   PetscFunctionReturn(0);
271df826632SBarry Smith }
272df826632SBarry Smith 
273df826632SBarry Smith static PetscErrorCode PCDestroy_Redistribute(PC pc)
274df826632SBarry Smith {
275df826632SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
276df826632SBarry Smith   PetscErrorCode  ierr;
277df826632SBarry Smith 
278df826632SBarry Smith   PetscFunctionBegin;
279fcfd50ebSBarry Smith   ierr = VecScatterDestroy(&red->scatter);CHKERRQ(ierr);
280fcfd50ebSBarry Smith   ierr = ISDestroy(&red->is);CHKERRQ(ierr);
281fcfd50ebSBarry Smith   ierr = VecDestroy(&red->b);CHKERRQ(ierr);
282fcfd50ebSBarry Smith   ierr = VecDestroy(&red->x);CHKERRQ(ierr);
283fcfd50ebSBarry Smith   ierr = KSPDestroy(&red->ksp);CHKERRQ(ierr);
284fcfd50ebSBarry Smith   ierr = VecDestroy(&red->work);CHKERRQ(ierr);
2853bf036e2SBarry Smith   ierr = PetscFree(red->drows);CHKERRQ(ierr);
2863bf036e2SBarry Smith   ierr = PetscFree(red->diag);CHKERRQ(ierr);
287c31cb41cSBarry Smith   ierr = PetscFree(pc->data);CHKERRQ(ierr);
288df826632SBarry Smith   PetscFunctionReturn(0);
289df826632SBarry Smith }
290df826632SBarry Smith 
2914416b707SBarry Smith static PetscErrorCode PCSetFromOptions_Redistribute(PetscOptionItems *PetscOptionsObject,PC pc)
292df826632SBarry Smith {
293df826632SBarry Smith   PetscErrorCode  ierr;
294df826632SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
295df826632SBarry Smith 
296df826632SBarry Smith   PetscFunctionBegin;
297df826632SBarry Smith   ierr = KSPSetFromOptions(red->ksp);CHKERRQ(ierr);
298df826632SBarry Smith   PetscFunctionReturn(0);
299df826632SBarry Smith }
300df826632SBarry Smith 
3015e7ef714SBarry Smith /*@
3025e7ef714SBarry Smith    PCRedistributeGetKSP - Gets the KSP created by the PCREDISTRIBUTE
3035e7ef714SBarry Smith 
3045e7ef714SBarry Smith    Not Collective
3055e7ef714SBarry Smith 
3065e7ef714SBarry Smith    Input Parameter:
3075e7ef714SBarry Smith .  pc - the preconditioner context
3085e7ef714SBarry Smith 
3095e7ef714SBarry Smith    Output Parameter:
3105e7ef714SBarry Smith .  innerksp - the inner KSP
3115e7ef714SBarry Smith 
3125e7ef714SBarry Smith    Level: advanced
3135e7ef714SBarry Smith 
3145e7ef714SBarry Smith @*/
3155e7ef714SBarry Smith PetscErrorCode  PCRedistributeGetKSP(PC pc,KSP *innerksp)
3165e7ef714SBarry Smith {
3175e7ef714SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
3185e7ef714SBarry Smith 
3195e7ef714SBarry Smith   PetscFunctionBegin;
3205e7ef714SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
3215e7ef714SBarry Smith   PetscValidPointer(innerksp,2);
3225e7ef714SBarry Smith   *innerksp = red->ksp;
3235e7ef714SBarry Smith   PetscFunctionReturn(0);
3245e7ef714SBarry Smith }
3255e7ef714SBarry Smith 
326df826632SBarry Smith /* -------------------------------------------------------------------------------------*/
327df826632SBarry Smith /*MC
328181dd334SBarry 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
329df826632SBarry Smith 
330df826632SBarry Smith      Options for the redistribute preconditioners can be set with -redistribute_ksp_xxx <values> and -redistribute_pc_xxx <values>
331df826632SBarry Smith 
33295452b02SPatrick Sanan      Notes:
33395452b02SPatrick Sanan     Usually run this with -ksp_type preonly
334181dd334SBarry Smith 
335181dd334SBarry 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
336181dd334SBarry Smith      -pc_type redistribute -redistribute_ksp_type cg -redistribute_pc_type bjacobi -redistribute_sub_pc_type icc to take advantage of the symmetry.
337181dd334SBarry Smith 
3382dfef595SBarry 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.
3392dfef595SBarry Smith 
34095452b02SPatrick Sanan      Developer Notes:
34195452b02SPatrick Sanan     Should add an option to this preconditioner to use a partitioner to redistribute the rows to lower communication.
3422dfef595SBarry Smith 
343df826632SBarry Smith    Level: intermediate
344df826632SBarry Smith 
3455e7ef714SBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PCRedistributeGetKSP()
346df826632SBarry Smith M*/
347df826632SBarry Smith 
3488cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_Redistribute(PC pc)
349df826632SBarry Smith {
350df826632SBarry Smith   PetscErrorCode  ierr;
351df826632SBarry Smith   PC_Redistribute *red;
352911f9fe8SBarry Smith   const char      *prefix;
353df826632SBarry Smith 
354df826632SBarry Smith   PetscFunctionBegin;
355b00a9115SJed Brown   ierr     = PetscNewLog(pc,&red);CHKERRQ(ierr);
356df826632SBarry Smith   pc->data = (void*)red;
357df826632SBarry Smith 
358df826632SBarry Smith   pc->ops->apply          = PCApply_Redistribute;
3590a545947SLisandro Dalcin   pc->ops->applytranspose = NULL;
360df826632SBarry Smith   pc->ops->setup          = PCSetUp_Redistribute;
361df826632SBarry Smith   pc->ops->destroy        = PCDestroy_Redistribute;
362df826632SBarry Smith   pc->ops->setfromoptions = PCSetFromOptions_Redistribute;
363df826632SBarry Smith   pc->ops->view           = PCView_Redistribute;
364911f9fe8SBarry Smith 
365ce94432eSBarry Smith   ierr = KSPCreate(PetscObjectComm((PetscObject)pc),&red->ksp);CHKERRQ(ierr);
366422a814eSBarry Smith   ierr = KSPSetErrorIfNotConverged(red->ksp,pc->erroriffailure);CHKERRQ(ierr);
367911f9fe8SBarry Smith   ierr = PetscObjectIncrementTabLevel((PetscObject)red->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
3683bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)red->ksp);CHKERRQ(ierr);
369911f9fe8SBarry Smith   ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
370911f9fe8SBarry Smith   ierr = KSPSetOptionsPrefix(red->ksp,prefix);CHKERRQ(ierr);
371911f9fe8SBarry Smith   ierr = KSPAppendOptionsPrefix(red->ksp,"redistribute_");CHKERRQ(ierr);
372df826632SBarry Smith   PetscFunctionReturn(0);
373df826632SBarry Smith }
374