xref: /petsc/src/ksp/pc/impls/redistribute/redistribute.c (revision 181dd33403cd901179373d5d74f6e9bc5dcdcb6a)
1df826632SBarry Smith #define PETSCKSP_DLL
2df826632SBarry Smith 
3df826632SBarry Smith /*
4df826632SBarry Smith   This file defines a "solve the problem redistributely on each subgroup of processor" preconditioner.
5df826632SBarry Smith */
6df826632SBarry Smith #include "private/pcimpl.h"     /*I "petscpc.h" I*/
7df826632SBarry Smith #include "petscksp.h"
8df826632SBarry Smith 
9df826632SBarry Smith typedef struct {
10df826632SBarry Smith   KSP          ksp;
11df826632SBarry Smith   Vec          x,b;
12df826632SBarry Smith   Mat          mat;
13df826632SBarry Smith   VecScatter   scatter;
14911f9fe8SBarry Smith   IS           is;
15*181dd334SBarry Smith   PetscInt     dcnt,*drows;   /* these are the local rows that have only diagonal entry */
16*181dd334SBarry Smith   PetscScalar  *diag;
17*181dd334SBarry Smith   Vec          work;
18df826632SBarry Smith } PC_Redistribute;
19df826632SBarry Smith 
20df826632SBarry Smith #undef __FUNCT__
21df826632SBarry Smith #define __FUNCT__ "PCView_Redistribute"
22df826632SBarry Smith static PetscErrorCode PCView_Redistribute(PC pc,PetscViewer viewer)
23df826632SBarry Smith {
24df826632SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
25df826632SBarry Smith   PetscErrorCode  ierr;
26df826632SBarry Smith   PetscTruth      iascii,isstring;
27*181dd334SBarry Smith   PetscInt        ncnt,N;
28df826632SBarry Smith 
29df826632SBarry Smith   PetscFunctionBegin;
30df826632SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
31df826632SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
32df826632SBarry Smith   if (iascii) {
33*181dd334SBarry Smith     ierr = MPI_Allreduce(&red->dcnt,&ncnt,1,MPIU_INT,MPI_SUM,((PetscObject)pc)->comm);CHKERRQ(ierr);
34*181dd334SBarry Smith     ierr = MatGetSize(pc->pmat,&N,PETSC_NULL);CHKERRQ(ierr);
35*181dd334SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"    Number rows eliminated %D Percentage rows eliminated %g\n",ncnt,100.0*((PetscScalar)ncnt)/((PetscScalar)N));CHKERRQ(ierr);
36df826632SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  Redistribute preconditioner: \n");CHKERRQ(ierr);
37df826632SBarry Smith     ierr = KSPView(red->ksp,viewer);CHKERRQ(ierr);
38df826632SBarry Smith   } else if (isstring) {
39df826632SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," Redistribute preconditioner");CHKERRQ(ierr);
40df826632SBarry Smith     ierr = KSPView(red->ksp,viewer);CHKERRQ(ierr);
41df826632SBarry Smith   } else {
42df826632SBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for PC redistribute",((PetscObject)viewer)->type_name);
43df826632SBarry Smith   }
44df826632SBarry Smith   PetscFunctionReturn(0);
45df826632SBarry Smith }
46df826632SBarry Smith 
47df826632SBarry Smith #include "private/matimpl.h"        /*I "petscmat.h" I*/
48df826632SBarry Smith #undef __FUNCT__
49df826632SBarry Smith #define __FUNCT__ "PCSetUp_Redistribute"
50df826632SBarry Smith static PetscErrorCode PCSetUp_Redistribute(PC pc)
51df826632SBarry Smith {
52df826632SBarry Smith   PC_Redistribute   *red = (PC_Redistribute*)pc->data;
53df826632SBarry Smith   PetscErrorCode    ierr;
54df826632SBarry Smith   MPI_Comm          comm;
55*181dd334SBarry Smith   PetscInt          rstart,rend,i,nz,cnt,*rows,ncnt,dcnt,*drows;
5626283091SBarry Smith   PetscLayout       map,nmap;
57df826632SBarry Smith   PetscMPIInt       size,rank,imdex,tag,n;
58e8dd6687SHong Zhang   PetscInt          *source = PETSC_NULL;
59df826632SBarry Smith   PetscMPIInt       *nprocs = PETSC_NULL,nrecvs;
60df826632SBarry Smith   PetscInt          j,nsends;
61df826632SBarry Smith   PetscInt          *owner = PETSC_NULL,*starts = PETSC_NULL,count,slen;
62e8dd6687SHong Zhang   PetscInt          *rvalues,*svalues,recvtotal;
63df826632SBarry Smith   PetscMPIInt       *onodes1,*olengths1;
64df826632SBarry Smith   MPI_Request       *send_waits = PETSC_NULL,*recv_waits = PETSC_NULL;
65df826632SBarry Smith   MPI_Status        recv_status,*send_status;
66*181dd334SBarry Smith   Vec               tvec,diag;
67ca320bd4SBarry Smith   Mat               tmat;
68*181dd334SBarry Smith   const PetscScalar *d;
69df826632SBarry Smith 
70df826632SBarry Smith   PetscFunctionBegin;
71dc9360f3SBarry Smith   if (pc->setupcalled) {
72dc9360f3SBarry Smith     ierr = KSPGetOperators(red->ksp,PETSC_NULL,&tmat,PETSC_NULL);CHKERRQ(ierr);
73dc9360f3SBarry Smith     ierr = MatGetSubMatrix(pc->pmat,red->is,red->is,MAT_REUSE_MATRIX,&tmat);CHKERRQ(ierr);
74dc9360f3SBarry Smith   } else {
75df826632SBarry Smith     ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
76df826632SBarry Smith     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
77df826632SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
78361c1e09SMatthew Knepley     ierr = PetscObjectGetNewTag((PetscObject)pc,&tag);CHKERRQ(ierr);
79df826632SBarry Smith 
80ca320bd4SBarry Smith     /* count non-diagonal rows on process */
81df826632SBarry Smith     ierr = MatGetOwnershipRange(pc->mat,&rstart,&rend);CHKERRQ(ierr);
82ca320bd4SBarry Smith     cnt  = 0;
83df826632SBarry Smith     for (i=rstart; i<rend; i++) {
84df826632SBarry Smith       ierr = MatGetRow(pc->mat,i,&nz,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
85df826632SBarry Smith       if (nz > 1) cnt++;
86911f9fe8SBarry Smith       ierr = MatRestoreRow(pc->mat,i,&nz,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
87df826632SBarry Smith     }
88df826632SBarry Smith     ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr);
89*181dd334SBarry Smith     ierr = PetscMalloc((rend - rstart - cnt)*sizeof(PetscInt),&drows);CHKERRQ(ierr);
90ca320bd4SBarry Smith 
91ca320bd4SBarry Smith     /* list non-diagonal rows on process */
92*181dd334SBarry Smith     cnt  = 0; dcnt = 0;
93df826632SBarry Smith     for (i=rstart; i<rend; i++) {
94df826632SBarry Smith       ierr = MatGetRow(pc->mat,i,&nz,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
95df826632SBarry Smith       if (nz > 1) rows[cnt++] = i;
96*181dd334SBarry Smith       else drows[dcnt++] = i - rstart;
97911f9fe8SBarry Smith       ierr = MatRestoreRow(pc->mat,i,&nz,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
98df826632SBarry Smith     }
99ca320bd4SBarry Smith 
10026283091SBarry Smith     /* create PetscLayout for non-diagonal rows on each process */
10126283091SBarry Smith     ierr = PetscLayoutCreate(comm,&map);CHKERRQ(ierr);
10226283091SBarry Smith     ierr = PetscLayoutSetLocalSize(map,cnt);CHKERRQ(ierr);
10326283091SBarry Smith     ierr = PetscLayoutSetBlockSize(map,1);CHKERRQ(ierr);
10426283091SBarry Smith     ierr = PetscLayoutSetUp(map);CHKERRQ(ierr);
105df826632SBarry Smith     rstart = map->rstart;
106df826632SBarry Smith     rend   = map->rend;
107df826632SBarry Smith 
10826283091SBarry Smith     /* create PetscLayout for load-balanced non-diagonal rows on each process */
10926283091SBarry Smith     ierr = PetscLayoutCreate(comm,&nmap);CHKERRQ(ierr);
110df826632SBarry Smith     ierr = MPI_Allreduce(&cnt,&ncnt,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
11126283091SBarry Smith     ierr = PetscLayoutSetSize(nmap,ncnt);CHKERRQ(ierr);
11226283091SBarry Smith     ierr = PetscLayoutSetBlockSize(nmap,1);CHKERRQ(ierr);
11326283091SBarry Smith     ierr = PetscLayoutSetUp(nmap);CHKERRQ(ierr);
114df826632SBarry Smith 
115*181dd334SBarry Smith     ierr = PetscInfo2(pc,"Number of diagonal rows eliminated %d, percentage eliminated %g\n",pc->pmat->rmap->N-ncnt,((PetscScalar)(pc->pmat->rmap->N-ncnt))/((PetscScalar)(pc->pmat->rmap->N)));
116ca320bd4SBarry Smith     /*
117ca320bd4SBarry Smith 	this code is taken from VecScatterCreate_PtoS()
118ca320bd4SBarry Smith 	Determines what rows need to be moved where to
119ca320bd4SBarry Smith 	load balance the non-diagonal rows
120ca320bd4SBarry Smith     */
121df826632SBarry Smith     /*  count number of contributors to each processor */
122df826632SBarry Smith     ierr = PetscMalloc2(size,PetscMPIInt,&nprocs,cnt,PetscInt,&owner);CHKERRQ(ierr);
123df826632SBarry Smith     ierr = PetscMemzero(nprocs,size*sizeof(PetscMPIInt));CHKERRQ(ierr);
124df826632SBarry Smith     j      = 0;
125df826632SBarry Smith     nsends = 0;
126df826632SBarry Smith     for (i=rstart; i<rend; i++) {
127df826632SBarry Smith       if (i < nmap->range[j]) j = 0;
128df826632SBarry Smith       for (; j<size; j++) {
129df826632SBarry Smith 	if (i < nmap->range[j+1]) {
130df826632SBarry Smith 	  if (!nprocs[j]++) nsends++;
131ca320bd4SBarry Smith 	  owner[i-rstart] = j;
132df826632SBarry Smith 	  break;
133df826632SBarry Smith 	}
134df826632SBarry Smith       }
135df826632SBarry Smith     }
136df826632SBarry Smith     /* inform other processors of number of messages and max length*/
137df826632SBarry Smith     ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,nprocs,&nrecvs);CHKERRQ(ierr);
138df826632SBarry Smith     ierr = PetscGatherMessageLengths(comm,nsends,nrecvs,nprocs,&onodes1,&olengths1);CHKERRQ(ierr);
139df826632SBarry Smith     ierr = PetscSortMPIIntWithArray(nrecvs,onodes1,olengths1);CHKERRQ(ierr);
140df826632SBarry Smith     recvtotal = 0; for (i=0; i<nrecvs; i++) recvtotal += olengths1[i];
141df826632SBarry Smith 
142df826632SBarry Smith     /* post receives:  rvalues - rows I will own; count - nu */
143df826632SBarry Smith     ierr = PetscMalloc3(recvtotal,PetscInt,&rvalues,nrecvs,PetscInt,&source,nrecvs,MPI_Request,&recv_waits);CHKERRQ(ierr);
144df826632SBarry Smith     count  = 0;
145df826632SBarry Smith     for (i=0; i<nrecvs; i++) {
146df826632SBarry Smith       ierr  = MPI_Irecv((rvalues+count),olengths1[i],MPIU_INT,onodes1[i],tag,comm,recv_waits+i);CHKERRQ(ierr);
147df826632SBarry Smith       count += olengths1[i];
148df826632SBarry Smith     }
149df826632SBarry Smith 
150df826632SBarry Smith     /* do sends:
151df826632SBarry Smith        1) starts[i] gives the starting index in svalues for stuff going to
152df826632SBarry Smith        the ith processor
153df826632SBarry Smith     */
154911f9fe8SBarry Smith     ierr = PetscMalloc3(cnt,PetscInt,&svalues,nsends,MPI_Request,&send_waits,size,PetscInt,&starts);CHKERRQ(ierr);
155df826632SBarry Smith     starts[0]  = 0;
156df826632SBarry Smith     for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
157df826632SBarry Smith     for (i=0; i<cnt; i++) {
158df826632SBarry Smith       svalues[starts[owner[i]]++] = rows[i];
159df826632SBarry Smith     }
160*181dd334SBarry Smith     for (i=0; i<cnt; i++) rows[i] = rows[i] - rstart;
161*181dd334SBarry Smith     red->drows = drows;
162*181dd334SBarry Smith     red->dcnt  = dcnt;
163ca320bd4SBarry Smith     ierr = PetscFree(rows);CHKERRQ(ierr);
164*181dd334SBarry Smith 
165df826632SBarry Smith     starts[0] = 0;
166911f9fe8SBarry Smith     for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
167df826632SBarry Smith     count = 0;
168df826632SBarry Smith     for (i=0; i<size; i++) {
169df826632SBarry Smith       if (nprocs[i]) {
170df826632SBarry Smith 	ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
171df826632SBarry Smith       }
172df826632SBarry Smith     }
173df826632SBarry Smith 
174df826632SBarry Smith     /*  wait on receives */
175df826632SBarry Smith     count  = nrecvs;
176df826632SBarry Smith     slen   = 0;
177df826632SBarry Smith     while (count) {
178df826632SBarry Smith       ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
179df826632SBarry Smith       /* unpack receives into our local space */
180df826632SBarry Smith       ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
181df826632SBarry Smith       slen += n;
182df826632SBarry Smith       count--;
183df826632SBarry Smith     }
184df826632SBarry Smith     if (slen != recvtotal) SETERRQ2(PETSC_ERR_PLIB,"Total message lengths %D not expected %D",slen,recvtotal);
185df826632SBarry Smith 
186911f9fe8SBarry Smith     ierr = ISCreateGeneral(comm,slen,rvalues,&red->is);CHKERRQ(ierr);
187911f9fe8SBarry Smith 
188ca320bd4SBarry Smith     /* free up all work space */
189df826632SBarry Smith     ierr = PetscFree(olengths1);CHKERRQ(ierr);
190df826632SBarry Smith     ierr = PetscFree(onodes1);CHKERRQ(ierr);
191df826632SBarry Smith     ierr = PetscFree3(rvalues,source,recv_waits);CHKERRQ(ierr);
192ca320bd4SBarry Smith     ierr = PetscFree2(nprocs,owner);CHKERRQ(ierr);
193ca320bd4SBarry Smith     if (nsends) {   /* wait on sends */
194df826632SBarry Smith       ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
195df826632SBarry Smith       ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
196df826632SBarry Smith       ierr = PetscFree(send_status);CHKERRQ(ierr);
197df826632SBarry Smith     }
198df826632SBarry Smith     ierr = PetscFree3(svalues,send_waits,starts);CHKERRQ(ierr);
19926283091SBarry Smith     ierr = PetscLayoutDestroy(map);CHKERRQ(ierr);
20026283091SBarry Smith     ierr = PetscLayoutDestroy(nmap);CHKERRQ(ierr);
201df826632SBarry Smith 
202ca320bd4SBarry Smith     ierr = VecCreateMPI(comm,slen,PETSC_DETERMINE,&red->b);CHKERRQ(ierr);
203ca320bd4SBarry Smith     ierr = VecDuplicate(red->b,&red->x);CHKERRQ(ierr);
204ca320bd4SBarry Smith     ierr = MatGetVecs(pc->pmat,&tvec,PETSC_NULL);CHKERRQ(ierr);
205ca320bd4SBarry Smith     ierr = VecScatterCreate(tvec,red->is,red->b,PETSC_NULL,&red->scatter);CHKERRQ(ierr);
206ca320bd4SBarry Smith     ierr = VecDestroy(tvec);CHKERRQ(ierr);
207ca320bd4SBarry Smith     ierr = MatGetSubMatrix(pc->pmat,red->is,red->is,MAT_INITIAL_MATRIX,&tmat);CHKERRQ(ierr);
208dc9360f3SBarry Smith   }
209ca320bd4SBarry Smith   ierr = KSPSetOperators(red->ksp,tmat,tmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
210ca320bd4SBarry Smith   ierr = MatDestroy(tmat);CHKERRQ(ierr);
211*181dd334SBarry Smith 
212*181dd334SBarry Smith   /* get diagonal portion of matrix */
213*181dd334SBarry Smith   ierr = PetscMalloc(red->dcnt*sizeof(PetscScalar),&red->diag);CHKERRQ(ierr);
214*181dd334SBarry Smith   ierr = MatGetVecs(pc->pmat,&diag,PETSC_NULL);CHKERRQ(ierr);
215*181dd334SBarry Smith   ierr = MatGetDiagonal(pc->pmat,diag);CHKERRQ(ierr);
216*181dd334SBarry Smith   ierr = VecGetArray(diag,(PetscScalar**)&d);CHKERRQ(ierr);
217*181dd334SBarry Smith   for (i=0; i<red->dcnt; i++) {
218*181dd334SBarry Smith     red->diag[i] = 1.0/d[red->drows[i]];
219*181dd334SBarry Smith   }
220*181dd334SBarry Smith   ierr = VecRestoreArray(diag,(PetscScalar**)&d);CHKERRQ(ierr);
221*181dd334SBarry Smith   ierr = VecDestroy(diag);CHKERRQ(ierr);
222*181dd334SBarry Smith 
223df826632SBarry Smith   ierr = KSPSetUp(red->ksp);CHKERRQ(ierr);
224df826632SBarry Smith   PetscFunctionReturn(0);
225df826632SBarry Smith }
226df826632SBarry Smith 
227df826632SBarry Smith #undef __FUNCT__
228df826632SBarry Smith #define __FUNCT__ "PCApply_Redistribute"
229df826632SBarry Smith static PetscErrorCode PCApply_Redistribute(PC pc,Vec b,Vec x)
230df826632SBarry Smith {
231df826632SBarry Smith   PC_Redistribute   *red = (PC_Redistribute*)pc->data;
232df826632SBarry Smith   PetscErrorCode    ierr;
233*181dd334SBarry Smith   PetscInt          dcnt = red->dcnt,i;
234*181dd334SBarry Smith   const PetscInt    *drows = red->drows;
235*181dd334SBarry Smith   PetscScalar       *xwork;
236*181dd334SBarry Smith   const PetscScalar *bwork,*diag = red->diag;
237df826632SBarry Smith 
238df826632SBarry Smith   PetscFunctionBegin;
239*181dd334SBarry Smith   if (!red->work) {
240*181dd334SBarry Smith     ierr = VecDuplicate(b,&red->work);CHKERRQ(ierr);
241*181dd334SBarry Smith   }
242*181dd334SBarry Smith   /* compute the rows of solution that have diagonal entries only */
243*181dd334SBarry Smith   ierr = VecSet(x,0.0);CHKERRQ(ierr);         /* x = diag(A)^{-1} b */
244*181dd334SBarry Smith   ierr = VecGetArray(x,&xwork);CHKERRQ(ierr);
245*181dd334SBarry Smith   ierr = VecGetArray(b,(PetscScalar**)&bwork);CHKERRQ(ierr);
246*181dd334SBarry Smith   for (i=0; i<dcnt; i++) {
247*181dd334SBarry Smith     xwork[drows[i]] = diag[i]*bwork[drows[i]];
248*181dd334SBarry Smith   }
249*181dd334SBarry Smith   ierr = PetscLogFlops(dcnt);CHKERRQ(ierr);
250*181dd334SBarry Smith   ierr = VecRestoreArray(red->work,&xwork);CHKERRQ(ierr);
251*181dd334SBarry Smith   ierr = VecRestoreArray(b,(PetscScalar**)&bwork);CHKERRQ(ierr);
252*181dd334SBarry Smith   /* update the right hand side for the reduced system with diagonal rows (and corresponding columns) removed */
253*181dd334SBarry Smith   ierr = MatMult(pc->pmat,x,red->work);CHKERRQ(ierr);
254*181dd334SBarry Smith   ierr = VecAYPX(red->work,-1.0,b);CHKERRQ(ierr);   /* red->work = b - A x */
255*181dd334SBarry Smith 
256*181dd334SBarry Smith   ierr = VecScatterBegin(red->scatter,red->work,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
257*181dd334SBarry Smith   ierr = VecScatterEnd(red->scatter,red->work,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
258df826632SBarry Smith   ierr = KSPSolve(red->ksp,red->b,red->x);CHKERRQ(ierr);
259df826632SBarry Smith   ierr = VecScatterBegin(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
260df826632SBarry Smith   ierr = VecScatterEnd(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
261df826632SBarry Smith   PetscFunctionReturn(0);
262df826632SBarry Smith }
263df826632SBarry Smith 
264df826632SBarry Smith #undef __FUNCT__
265df826632SBarry Smith #define __FUNCT__ "PCDestroy_Redistribute"
266df826632SBarry Smith static PetscErrorCode PCDestroy_Redistribute(PC pc)
267df826632SBarry Smith {
268df826632SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
269df826632SBarry Smith   PetscErrorCode  ierr;
270df826632SBarry Smith 
271df826632SBarry Smith   PetscFunctionBegin;
272df826632SBarry Smith   if (red->scatter)  {ierr = VecScatterDestroy(red->scatter);CHKERRQ(ierr);}
273ca320bd4SBarry Smith   if (red->is)       {ierr = ISDestroy(red->is);CHKERRQ(ierr);}
274df826632SBarry Smith   if (red->b)        {ierr = VecDestroy(red->b);CHKERRQ(ierr);}
275df826632SBarry Smith   if (red->x)        {ierr = VecDestroy(red->x);CHKERRQ(ierr);}
276df826632SBarry Smith   if (red->mat)      {ierr = MatDestroy(red->mat);CHKERRQ(ierr);}
277df826632SBarry Smith   if (red->ksp)      {ierr = KSPDestroy(red->ksp);CHKERRQ(ierr);}
278*181dd334SBarry Smith   if (red->work)     {ierr = VecDestroy(red->work);CHKERRQ(ierr);}
279*181dd334SBarry Smith   ierr = PetscFree(red->drows);
280*181dd334SBarry Smith   ierr = PetscFree(red->diag);
281df826632SBarry Smith   ierr = PetscFree(red);CHKERRQ(ierr);
282df826632SBarry Smith   PetscFunctionReturn(0);
283df826632SBarry Smith }
284df826632SBarry Smith 
285df826632SBarry Smith #undef __FUNCT__
286df826632SBarry Smith #define __FUNCT__ "PCSetFromOptions_Redistribute"
287df826632SBarry Smith static PetscErrorCode PCSetFromOptions_Redistribute(PC pc)
288df826632SBarry Smith {
289df826632SBarry Smith   PetscErrorCode  ierr;
290df826632SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
291df826632SBarry Smith 
292df826632SBarry Smith   PetscFunctionBegin;
293df826632SBarry Smith   ierr = KSPSetFromOptions(red->ksp);CHKERRQ(ierr);
294df826632SBarry Smith   PetscFunctionReturn(0);
295df826632SBarry Smith }
296df826632SBarry Smith 
297df826632SBarry Smith /* -------------------------------------------------------------------------------------*/
298df826632SBarry Smith /*MC
299*181dd334SBarry 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
300df826632SBarry Smith 
301df826632SBarry Smith      Options for the redistribute preconditioners can be set with -redistribute_ksp_xxx <values> and -redistribute_pc_xxx <values>
302df826632SBarry Smith 
303*181dd334SBarry Smith      Notes:  Usually run this with -ksp_type preonly
304*181dd334SBarry Smith 
305*181dd334SBarry 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
306*181dd334SBarry Smith      -pc_type redistribute -redistribute_ksp_type cg -redistribute_pc_type bjacobi -redistribute_sub_pc_type icc to take advantage of the symmetry.
307*181dd334SBarry Smith 
308df826632SBarry Smith    Level: intermediate
309df826632SBarry Smith 
310df826632SBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types)
311df826632SBarry Smith M*/
312df826632SBarry Smith 
313df826632SBarry Smith EXTERN_C_BEGIN
314df826632SBarry Smith #undef __FUNCT__
315df826632SBarry Smith #define __FUNCT__ "PCCreate_Redistribute"
316df826632SBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT PCCreate_Redistribute(PC pc)
317df826632SBarry Smith {
318df826632SBarry Smith   PetscErrorCode  ierr;
319df826632SBarry Smith   PC_Redistribute *red;
320911f9fe8SBarry Smith   const char      *prefix;
321df826632SBarry Smith 
322df826632SBarry Smith   PetscFunctionBegin;
323df826632SBarry Smith   ierr = PetscNewLog(pc,PC_Redistribute,&red);CHKERRQ(ierr);
324df826632SBarry Smith   pc->data            = (void*)red;
325df826632SBarry Smith 
326df826632SBarry Smith   pc->ops->apply           = PCApply_Redistribute;
327df826632SBarry Smith   pc->ops->applytranspose  = 0;
328df826632SBarry Smith   pc->ops->setup           = PCSetUp_Redistribute;
329df826632SBarry Smith   pc->ops->destroy         = PCDestroy_Redistribute;
330df826632SBarry Smith   pc->ops->setfromoptions  = PCSetFromOptions_Redistribute;
331df826632SBarry Smith   pc->ops->view            = PCView_Redistribute;
332911f9fe8SBarry Smith 
333911f9fe8SBarry Smith   ierr = KSPCreate(((PetscObject)pc)->comm,&red->ksp);CHKERRQ(ierr);
334911f9fe8SBarry Smith   ierr = PetscObjectIncrementTabLevel((PetscObject)red->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
335911f9fe8SBarry Smith   ierr = PetscLogObjectParent(pc,red->ksp);CHKERRQ(ierr);
336911f9fe8SBarry Smith   ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
337911f9fe8SBarry Smith   ierr = KSPSetOptionsPrefix(red->ksp,prefix);CHKERRQ(ierr);
338911f9fe8SBarry Smith   ierr = KSPAppendOptionsPrefix(red->ksp,"redistribute_");CHKERRQ(ierr);
339df826632SBarry Smith   PetscFunctionReturn(0);
340df826632SBarry Smith }
341df826632SBarry Smith EXTERN_C_END
342