xref: /petsc/src/ksp/pc/impls/redistribute/redistribute.c (revision ca320bd4c9457dcef2a04988d08661bd87361252)
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;
15df826632SBarry Smith } PC_Redistribute;
16df826632SBarry Smith 
17df826632SBarry Smith #undef __FUNCT__
18df826632SBarry Smith #define __FUNCT__ "PCView_Redistribute"
19df826632SBarry Smith static PetscErrorCode PCView_Redistribute(PC pc,PetscViewer viewer)
20df826632SBarry Smith {
21df826632SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
22df826632SBarry Smith   PetscErrorCode  ierr;
23df826632SBarry Smith   PetscTruth      iascii,isstring;
24df826632SBarry Smith 
25df826632SBarry Smith   PetscFunctionBegin;
26df826632SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
27df826632SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
28df826632SBarry Smith   if (iascii) {
29df826632SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  Redistribute preconditioner: \n");CHKERRQ(ierr);
30df826632SBarry Smith     ierr = KSPView(red->ksp,viewer);CHKERRQ(ierr);
31df826632SBarry Smith   } else if (isstring) {
32df826632SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," Redistribute preconditioner");CHKERRQ(ierr);
33df826632SBarry Smith     ierr = KSPView(red->ksp,viewer);CHKERRQ(ierr);
34df826632SBarry Smith   } else {
35df826632SBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for PC redistribute",((PetscObject)viewer)->type_name);
36df826632SBarry Smith   }
37df826632SBarry Smith   PetscFunctionReturn(0);
38df826632SBarry Smith }
39df826632SBarry Smith 
40df826632SBarry Smith #include "private/matimpl.h"        /*I "petscmat.h" I*/
41df826632SBarry Smith #undef __FUNCT__
42df826632SBarry Smith #define __FUNCT__ "PCSetUp_Redistribute"
43df826632SBarry Smith static PetscErrorCode PCSetUp_Redistribute(PC pc)
44df826632SBarry Smith {
45df826632SBarry Smith   PC_Redistribute   *red = (PC_Redistribute*)pc->data;
46df826632SBarry Smith   PetscErrorCode    ierr;
47df826632SBarry Smith   MPI_Comm          comm;
48df826632SBarry Smith   PetscInt          rstart,rend,i,nz,cnt,*rows,ncnt;
49df826632SBarry Smith   PetscMap          *map,*nmap;
50df826632SBarry Smith   PetscMPIInt       size,rank,imdex,tag,n;
51e8dd6687SHong Zhang   PetscInt          *source = PETSC_NULL;
52df826632SBarry Smith   PetscMPIInt       *nprocs = PETSC_NULL,nrecvs;
53df826632SBarry Smith   PetscInt          j,nsends;
54df826632SBarry Smith   PetscInt          *owner = PETSC_NULL,*starts = PETSC_NULL,count,slen;
55e8dd6687SHong Zhang   PetscInt          *rvalues,*svalues,recvtotal;
56df826632SBarry Smith   PetscMPIInt       *onodes1,*olengths1;
57df826632SBarry Smith   MPI_Request       *send_waits = PETSC_NULL,*recv_waits = PETSC_NULL;
58df826632SBarry Smith   MPI_Status        recv_status,*send_status;
59*ca320bd4SBarry Smith   Vec               tvec;
60*ca320bd4SBarry Smith   Mat               tmat;
61df826632SBarry Smith 
62df826632SBarry Smith   PetscFunctionBegin;
63df826632SBarry Smith   ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
64df826632SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
65df826632SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
66361c1e09SMatthew Knepley   ierr = PetscObjectGetNewTag((PetscObject)pc,&tag);CHKERRQ(ierr);
67df826632SBarry Smith 
68df826632SBarry Smith   if (!pc->setupcalled) {
69df826632SBarry Smith   } else SETERRQ(PETSC_ERR_SUP,"Cannot yet re-setup a redistribute KSP");
70df826632SBarry Smith 
71*ca320bd4SBarry Smith   /* count non-diagonal rows on process */
72df826632SBarry Smith   ierr = MatGetOwnershipRange(pc->mat,&rstart,&rend);CHKERRQ(ierr);
73*ca320bd4SBarry Smith   cnt  = 0;
74df826632SBarry Smith   for (i=rstart; i<rend; i++) {
75df826632SBarry Smith     ierr = MatGetRow(pc->mat,i,&nz,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
76df826632SBarry Smith     if (nz > 1) cnt++;
77911f9fe8SBarry Smith     ierr = MatRestoreRow(pc->mat,i,&nz,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
78df826632SBarry Smith   }
79df826632SBarry Smith   ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr);
80*ca320bd4SBarry Smith 
81*ca320bd4SBarry Smith   /* list non-diagonal rows on process */
82df826632SBarry 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) rows[cnt++] = i;
86911f9fe8SBarry Smith     ierr = MatRestoreRow(pc->mat,i,&nz,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
87df826632SBarry Smith   }
88*ca320bd4SBarry Smith 
89*ca320bd4SBarry Smith   /* create PetscMap for non-diagonal rows on each process */
90df826632SBarry Smith   ierr = PetscMalloc(sizeof(PetscMap),&map);CHKERRQ(ierr);
91df826632SBarry Smith   ierr = PetscMapInitialize(comm,map);CHKERRQ(ierr);
92df826632SBarry Smith   ierr = PetscMapSetLocalSize(map,cnt);CHKERRQ(ierr);
93df826632SBarry Smith   ierr = PetscMapSetBlockSize(map,1);CHKERRQ(ierr);
94df826632SBarry Smith   ierr = PetscMapSetUp(map);CHKERRQ(ierr);
95df826632SBarry Smith   rstart = map->rstart;
96df826632SBarry Smith   rend   = map->rend;
97df826632SBarry Smith 
98*ca320bd4SBarry Smith   /* create PetscMap for load-balanced non-diagonal rows on each process */
99df826632SBarry Smith   ierr = PetscMalloc(sizeof(PetscMap),&nmap);CHKERRQ(ierr);
100df826632SBarry Smith   ierr = PetscMapInitialize(comm,nmap);CHKERRQ(ierr);
101df826632SBarry Smith   ierr = MPI_Allreduce(&cnt,&ncnt,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
102df826632SBarry Smith   ierr = PetscMapSetSize(nmap,ncnt);CHKERRQ(ierr);
103df826632SBarry Smith   ierr = PetscMapSetBlockSize(nmap,1);CHKERRQ(ierr);
104df826632SBarry Smith   ierr = PetscMapSetUp(nmap);CHKERRQ(ierr);
105df826632SBarry Smith 
106*ca320bd4SBarry Smith   /*
107*ca320bd4SBarry Smith       this code is taken from VecScatterCreate_PtoS()
108*ca320bd4SBarry Smith       Determines what rows need to be moved where to
109*ca320bd4SBarry Smith       load balance the non-diagonal rows
110*ca320bd4SBarry Smith   */
111df826632SBarry Smith   /*  count number of contributors to each processor */
112df826632SBarry Smith   ierr = PetscMalloc2(size,PetscMPIInt,&nprocs,cnt,PetscInt,&owner);CHKERRQ(ierr);
113df826632SBarry Smith   ierr = PetscMemzero(nprocs,size*sizeof(PetscMPIInt));CHKERRQ(ierr);
114df826632SBarry Smith   j      = 0;
115df826632SBarry Smith   nsends = 0;
116df826632SBarry Smith   for (i=rstart; i<rend; i++) {
117df826632SBarry Smith     if (i < nmap->range[j]) j = 0;
118df826632SBarry Smith     for (; j<size; j++) {
119df826632SBarry Smith       if (i < nmap->range[j+1]) {
120df826632SBarry Smith         if (!nprocs[j]++) nsends++;
121*ca320bd4SBarry Smith         owner[i-rstart] = j;
122df826632SBarry Smith         break;
123df826632SBarry Smith       }
124df826632SBarry Smith     }
125df826632SBarry Smith   }
126df826632SBarry Smith   /* inform other processors of number of messages and max length*/
127df826632SBarry Smith   ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,nprocs,&nrecvs);CHKERRQ(ierr);
128df826632SBarry Smith   ierr = PetscGatherMessageLengths(comm,nsends,nrecvs,nprocs,&onodes1,&olengths1);CHKERRQ(ierr);
129df826632SBarry Smith   ierr = PetscSortMPIIntWithArray(nrecvs,onodes1,olengths1);CHKERRQ(ierr);
130df826632SBarry Smith   recvtotal = 0; for (i=0; i<nrecvs; i++) recvtotal += olengths1[i];
131df826632SBarry Smith 
132df826632SBarry Smith   /* post receives:  rvalues - rows I will own; count - nu */
133df826632SBarry Smith   ierr = PetscMalloc3(recvtotal,PetscInt,&rvalues,nrecvs,PetscInt,&source,nrecvs,MPI_Request,&recv_waits);CHKERRQ(ierr);
134df826632SBarry Smith   count  = 0;
135df826632SBarry Smith   for (i=0; i<nrecvs; i++) {
136df826632SBarry Smith     ierr  = MPI_Irecv((rvalues+count),olengths1[i],MPIU_INT,onodes1[i],tag,comm,recv_waits+i);CHKERRQ(ierr);
137df826632SBarry Smith     count += olengths1[i];
138df826632SBarry Smith   }
139df826632SBarry Smith 
140df826632SBarry Smith   /* do sends:
141df826632SBarry Smith      1) starts[i] gives the starting index in svalues for stuff going to
142df826632SBarry Smith      the ith processor
143df826632SBarry Smith   */
144911f9fe8SBarry Smith   ierr = PetscMalloc3(cnt,PetscInt,&svalues,nsends,MPI_Request,&send_waits,size,PetscInt,&starts);CHKERRQ(ierr);
145df826632SBarry Smith   starts[0]  = 0;
146df826632SBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
147df826632SBarry Smith   for (i=0; i<cnt; i++) {
148df826632SBarry Smith     svalues[starts[owner[i]]++] = rows[i];
149df826632SBarry Smith   }
150*ca320bd4SBarry Smith   ierr = PetscFree(rows);CHKERRQ(ierr);
151df826632SBarry Smith   starts[0] = 0;
152911f9fe8SBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
153df826632SBarry Smith   count = 0;
154df826632SBarry Smith   for (i=0; i<size; i++) {
155df826632SBarry Smith     if (nprocs[i]) {
156df826632SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
157df826632SBarry Smith     }
158df826632SBarry Smith   }
159df826632SBarry Smith 
160df826632SBarry Smith   /*  wait on receives */
161df826632SBarry Smith   count  = nrecvs;
162df826632SBarry Smith   slen   = 0;
163df826632SBarry Smith   while (count) {
164df826632SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
165df826632SBarry Smith     /* unpack receives into our local space */
166df826632SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
167df826632SBarry Smith     slen += n;
168df826632SBarry Smith     count--;
169df826632SBarry Smith   }
170df826632SBarry Smith   if (slen != recvtotal) SETERRQ2(PETSC_ERR_PLIB,"Total message lengths %D not expected %D",slen,recvtotal);
171df826632SBarry Smith 
172911f9fe8SBarry Smith   ierr = ISCreateGeneral(comm,slen,rvalues,&red->is);CHKERRQ(ierr);
173911f9fe8SBarry Smith 
174*ca320bd4SBarry Smith   /* free up all work space */
175df826632SBarry Smith   ierr = PetscFree(olengths1);CHKERRQ(ierr);
176df826632SBarry Smith   ierr = PetscFree(onodes1);CHKERRQ(ierr);
177df826632SBarry Smith   ierr = PetscFree3(rvalues,source,recv_waits);CHKERRQ(ierr);
178*ca320bd4SBarry Smith   ierr = PetscFree2(nprocs,owner);CHKERRQ(ierr);
179*ca320bd4SBarry Smith   if (nsends) {   /* wait on sends */
180df826632SBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
181df826632SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
182df826632SBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
183df826632SBarry Smith   }
184df826632SBarry Smith   ierr = PetscFree3(svalues,send_waits,starts);CHKERRQ(ierr);
185*ca320bd4SBarry Smith   ierr = PetscFree(map->range);CHKERRQ(ierr);
186df826632SBarry Smith   ierr = PetscFree(map);CHKERRQ(ierr);
187*ca320bd4SBarry Smith   ierr = PetscFree(nmap->range);CHKERRQ(ierr);
188df826632SBarry Smith   ierr = PetscFree(nmap);CHKERRQ(ierr);
189df826632SBarry Smith 
190*ca320bd4SBarry Smith ierr = ISView(red->is,PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);
191*ca320bd4SBarry Smith 
192*ca320bd4SBarry Smith   ierr = VecCreateMPI(comm,slen,PETSC_DETERMINE,&red->b);CHKERRQ(ierr);
193*ca320bd4SBarry Smith   ierr = VecDuplicate(red->b,&red->x);CHKERRQ(ierr);
194*ca320bd4SBarry Smith   ierr = MatGetVecs(pc->pmat,&tvec,PETSC_NULL);CHKERRQ(ierr);
195*ca320bd4SBarry Smith   ierr = VecScatterCreate(tvec,red->is,red->b,PETSC_NULL,&red->scatter);CHKERRQ(ierr);
196*ca320bd4SBarry Smith   ierr = VecDestroy(tvec);CHKERRQ(ierr);
197*ca320bd4SBarry Smith   ierr = MatGetSubMatrix(pc->pmat,red->is,red->is,MAT_INITIAL_MATRIX,&tmat);CHKERRQ(ierr);
198*ca320bd4SBarry Smith   ierr = KSPSetOperators(red->ksp,tmat,tmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
199*ca320bd4SBarry Smith   ierr = MatDestroy(tmat);CHKERRQ(ierr);
200*ca320bd4SBarry Smith 
201df826632SBarry Smith   ierr = KSPSetUp(red->ksp);CHKERRQ(ierr);
202df826632SBarry Smith   PetscFunctionReturn(0);
203df826632SBarry Smith }
204df826632SBarry Smith 
205df826632SBarry Smith #undef __FUNCT__
206df826632SBarry Smith #define __FUNCT__ "PCApply_Redistribute"
207df826632SBarry Smith static PetscErrorCode PCApply_Redistribute(PC pc,Vec b,Vec x)
208df826632SBarry Smith {
209df826632SBarry Smith   PC_Redistribute   *red = (PC_Redistribute*)pc->data;
210df826632SBarry Smith   PetscErrorCode    ierr;
211df826632SBarry Smith 
212df826632SBarry Smith   PetscFunctionBegin;
213df826632SBarry Smith   ierr = VecScatterBegin(red->scatter,b,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
214df826632SBarry Smith   ierr = VecScatterEnd(red->scatter,b,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
215df826632SBarry Smith   ierr = KSPSolve(red->ksp,red->b,red->x);CHKERRQ(ierr);
216df826632SBarry Smith   ierr = VecScatterBegin(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
217df826632SBarry Smith   ierr = VecScatterEnd(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
218df826632SBarry Smith   PetscFunctionReturn(0);
219df826632SBarry Smith }
220df826632SBarry Smith 
221df826632SBarry Smith #undef __FUNCT__
222df826632SBarry Smith #define __FUNCT__ "PCDestroy_Redistribute"
223df826632SBarry Smith static PetscErrorCode PCDestroy_Redistribute(PC pc)
224df826632SBarry Smith {
225df826632SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
226df826632SBarry Smith   PetscErrorCode  ierr;
227df826632SBarry Smith 
228df826632SBarry Smith   PetscFunctionBegin;
229df826632SBarry Smith   if (red->scatter)  {ierr = VecScatterDestroy(red->scatter);CHKERRQ(ierr);}
230*ca320bd4SBarry Smith   if (red->is)       {ierr = ISDestroy(red->is);CHKERRQ(ierr);}
231df826632SBarry Smith   if (red->b)        {ierr = VecDestroy(red->b);CHKERRQ(ierr);}
232df826632SBarry Smith   if (red->x)        {ierr = VecDestroy(red->x);CHKERRQ(ierr);}
233df826632SBarry Smith   if (red->mat)      {ierr = MatDestroy(red->mat);CHKERRQ(ierr);}
234df826632SBarry Smith   if (red->ksp)      {ierr = KSPDestroy(red->ksp);CHKERRQ(ierr);}
235df826632SBarry Smith   ierr = PetscFree(red);CHKERRQ(ierr);
236df826632SBarry Smith   PetscFunctionReturn(0);
237df826632SBarry Smith }
238df826632SBarry Smith 
239df826632SBarry Smith #undef __FUNCT__
240df826632SBarry Smith #define __FUNCT__ "PCSetFromOptions_Redistribute"
241df826632SBarry Smith static PetscErrorCode PCSetFromOptions_Redistribute(PC pc)
242df826632SBarry Smith {
243df826632SBarry Smith   PetscErrorCode  ierr;
244df826632SBarry Smith   PC_Redistribute *red = (PC_Redistribute*)pc->data;
245df826632SBarry Smith 
246df826632SBarry Smith   PetscFunctionBegin;
247df826632SBarry Smith   ierr = KSPSetFromOptions(red->ksp);CHKERRQ(ierr);
248df826632SBarry Smith   PetscFunctionReturn(0);
249df826632SBarry Smith }
250df826632SBarry Smith 
251df826632SBarry Smith /* -------------------------------------------------------------------------------------*/
252df826632SBarry Smith /*MC
253df826632SBarry Smith      PCREDISTRIBUTE - Redistributes a matrix for load balancing and then applys a KSP to that new matrix
254df826632SBarry Smith 
255df826632SBarry Smith      Options for the redistribute preconditioners can be set with -redistribute_ksp_xxx <values> and -redistribute_pc_xxx <values>
256df826632SBarry Smith 
257df826632SBarry Smith    Level: intermediate
258df826632SBarry Smith 
259df826632SBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types)
260df826632SBarry Smith M*/
261df826632SBarry Smith 
262df826632SBarry Smith EXTERN_C_BEGIN
263df826632SBarry Smith #undef __FUNCT__
264df826632SBarry Smith #define __FUNCT__ "PCCreate_Redistribute"
265df826632SBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT PCCreate_Redistribute(PC pc)
266df826632SBarry Smith {
267df826632SBarry Smith   PetscErrorCode  ierr;
268df826632SBarry Smith   PC_Redistribute *red;
269911f9fe8SBarry Smith   const char      *prefix;
270df826632SBarry Smith 
271df826632SBarry Smith   PetscFunctionBegin;
272df826632SBarry Smith   ierr = PetscNewLog(pc,PC_Redistribute,&red);CHKERRQ(ierr);
273df826632SBarry Smith   pc->data            = (void*)red;
274df826632SBarry Smith 
275df826632SBarry Smith   pc->ops->apply           = PCApply_Redistribute;
276df826632SBarry Smith   pc->ops->applytranspose  = 0;
277df826632SBarry Smith   pc->ops->setup           = PCSetUp_Redistribute;
278df826632SBarry Smith   pc->ops->destroy         = PCDestroy_Redistribute;
279df826632SBarry Smith   pc->ops->setfromoptions  = PCSetFromOptions_Redistribute;
280df826632SBarry Smith   pc->ops->view            = PCView_Redistribute;
281911f9fe8SBarry Smith 
282911f9fe8SBarry Smith   ierr = KSPCreate(((PetscObject)pc)->comm,&red->ksp);CHKERRQ(ierr);
283911f9fe8SBarry Smith   ierr = PetscObjectIncrementTabLevel((PetscObject)red->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
284911f9fe8SBarry Smith   ierr = PetscLogObjectParent(pc,red->ksp);CHKERRQ(ierr);
285911f9fe8SBarry Smith   ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
286911f9fe8SBarry Smith   ierr = KSPSetOptionsPrefix(red->ksp,prefix);CHKERRQ(ierr);
287911f9fe8SBarry Smith   ierr = KSPAppendOptionsPrefix(red->ksp,"redistribute_");CHKERRQ(ierr);
288df826632SBarry Smith   PetscFunctionReturn(0);
289df826632SBarry Smith }
290df826632SBarry Smith EXTERN_C_END
291