xref: /petsc/src/vec/is/sf/impls/basic/allgatherv/sfallgatherv.c (revision a2aba86c77ac869ca1007cc1e6f5ae9e8649f479)
1 #include <../src/vec/is/sf/impls/basic/allgatherv/sfallgatherv.h>
2 
3 PETSC_INTERN PetscErrorCode PetscSFBcastBegin_Gatherv(PetscSF,MPI_Datatype,PetscMemType,const void*,PetscMemType,void*,MPI_Op);
4 
5 /* PetscSFGetGraph is non-collective. An implementation should not have collective calls */
6 PETSC_INTERN PetscErrorCode PetscSFGetGraph_Allgatherv(PetscSF sf,PetscInt *nroots,PetscInt *nleaves,const PetscInt **ilocal,const PetscSFNode **iremote)
7 {
8   PetscErrorCode ierr;
9   PetscInt       i,j,k;
10   const PetscInt *range;
11   PetscMPIInt    size;
12 
13   PetscFunctionBegin;
14   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)sf),&size);CHKERRMPI(ierr);
15   if (nroots)  *nroots  = sf->nroots;
16   if (nleaves) *nleaves = sf->nleaves;
17   if (ilocal)  *ilocal  = NULL; /* Contiguous leaves */
18   if (iremote) {
19     if (!sf->remote && sf->nleaves) { /* The && sf->nleaves makes sfgatherv able to inherit this routine */
20       ierr = PetscLayoutGetRanges(sf->map,&range);CHKERRQ(ierr);
21       ierr = PetscMalloc1(sf->nleaves,&sf->remote);CHKERRQ(ierr);
22       sf->remote_alloc = sf->remote;
23       for (i=0; i<size; i++) {
24         for (j=range[i],k=0; j<range[i+1]; j++,k++) {
25           sf->remote[j].rank  = i;
26           sf->remote[j].index = k;
27         }
28       }
29     }
30     *iremote = sf->remote;
31   }
32   PetscFunctionReturn(0);
33 }
34 
35 PETSC_INTERN PetscErrorCode PetscSFSetUp_Allgatherv(PetscSF sf)
36 {
37   PetscErrorCode     ierr;
38   PetscSF_Allgatherv *dat = (PetscSF_Allgatherv*)sf->data;
39   PetscMPIInt        size;
40   PetscInt           i;
41   const PetscInt     *range;
42 
43   PetscFunctionBegin;
44   ierr = PetscSFSetUp_Allgather(sf);CHKERRQ(ierr);
45   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)sf),&size);CHKERRMPI(ierr);
46   if (sf->nleaves) { /* This if (sf->nleaves) test makes sfgatherv able to inherit this routine */
47     ierr = PetscMalloc1(size,&dat->recvcounts);CHKERRQ(ierr);
48     ierr = PetscMalloc1(size,&dat->displs);CHKERRQ(ierr);
49     ierr = PetscLayoutGetRanges(sf->map,&range);CHKERRQ(ierr);
50 
51     for (i=0; i<size; i++) {
52       ierr = PetscMPIIntCast(range[i],&dat->displs[i]);CHKERRQ(ierr);
53       ierr = PetscMPIIntCast(range[i+1]-range[i],&dat->recvcounts[i]);CHKERRQ(ierr);
54     }
55   }
56   PetscFunctionReturn(0);
57 }
58 
59 PETSC_INTERN PetscErrorCode PetscSFReset_Allgatherv(PetscSF sf)
60 {
61   PetscErrorCode         ierr;
62   PetscSF_Allgatherv     *dat = (PetscSF_Allgatherv*)sf->data;
63   PetscSFLink            link = dat->avail,next;
64 
65   PetscFunctionBegin;
66   ierr = PetscFree(dat->iranks);CHKERRQ(ierr);
67   ierr = PetscFree(dat->ioffset);CHKERRQ(ierr);
68   ierr = PetscFree(dat->irootloc);CHKERRQ(ierr);
69   ierr = PetscFree(dat->recvcounts);CHKERRQ(ierr);
70   ierr = PetscFree(dat->displs);CHKERRQ(ierr);
71   PetscCheckFalse(dat->inuse,PetscObjectComm((PetscObject)sf),PETSC_ERR_ARG_WRONGSTATE,"Outstanding operation has not been completed");
72   for (; link; link=next) {next = link->next; ierr = PetscSFLinkDestroy(sf,link);CHKERRQ(ierr);}
73   dat->avail = NULL;
74   PetscFunctionReturn(0);
75 }
76 
77 PETSC_INTERN PetscErrorCode PetscSFDestroy_Allgatherv(PetscSF sf)
78 {
79   PetscErrorCode ierr;
80 
81   PetscFunctionBegin;
82   ierr = PetscSFReset_Allgatherv(sf);CHKERRQ(ierr);
83   ierr = PetscFree(sf->data);CHKERRQ(ierr);
84   PetscFunctionReturn(0);
85 }
86 
87 static PetscErrorCode PetscSFBcastBegin_Allgatherv(PetscSF sf,MPI_Datatype unit,PetscMemType rootmtype,const void *rootdata,PetscMemType leafmtype,void *leafdata,MPI_Op op)
88 {
89   PetscErrorCode         ierr;
90   PetscSFLink            link;
91   PetscMPIInt            sendcount;
92   MPI_Comm               comm;
93   void                   *rootbuf = NULL,*leafbuf = NULL;
94   MPI_Request            *req;
95   PetscSF_Allgatherv     *dat = (PetscSF_Allgatherv*)sf->data;
96 
97   PetscFunctionBegin;
98   ierr = PetscSFLinkCreate(sf,unit,rootmtype,rootdata,leafmtype,leafdata,op,PETSCSF_BCAST,&link);CHKERRQ(ierr);
99   ierr = PetscSFLinkPackRootData(sf,link,PETSCSF_REMOTE,rootdata);CHKERRQ(ierr);
100   ierr = PetscSFLinkCopyRootBufferInCaseNotUseGpuAwareMPI(sf,link,PETSC_TRUE/* device2host before sending */);CHKERRQ(ierr);
101   ierr = PetscObjectGetComm((PetscObject)sf,&comm);CHKERRQ(ierr);
102   ierr = PetscMPIIntCast(sf->nroots,&sendcount);CHKERRQ(ierr);
103   ierr = PetscSFLinkGetMPIBuffersAndRequests(sf,link,PETSCSF_ROOT2LEAF,&rootbuf,&leafbuf,&req,NULL);CHKERRQ(ierr);
104   ierr = PetscSFLinkSyncStreamBeforeCallMPI(sf,link,PETSCSF_ROOT2LEAF);CHKERRQ(ierr);
105   ierr = MPIU_Iallgatherv(rootbuf,sendcount,unit,leafbuf,dat->recvcounts,dat->displs,unit,comm,req);CHKERRMPI(ierr);
106   PetscFunctionReturn(0);
107 }
108 
109 static PetscErrorCode PetscSFReduceBegin_Allgatherv(PetscSF sf,MPI_Datatype unit,PetscMemType leafmtype,const void *leafdata,PetscMemType rootmtype,void *rootdata,MPI_Op op)
110 {
111   PetscErrorCode         ierr;
112   PetscSFLink            link;
113   PetscSF_Allgatherv     *dat = (PetscSF_Allgatherv*)sf->data;
114   PetscInt               rstart;
115   PetscMPIInt            rank,count,recvcount;
116   MPI_Comm               comm;
117   void                   *rootbuf = NULL,*leafbuf = NULL;
118   MPI_Request            *req;
119 
120   PetscFunctionBegin;
121   ierr = PetscSFLinkCreate(sf,unit,rootmtype,rootdata,leafmtype,leafdata,op,PETSCSF_REDUCE,&link);CHKERRQ(ierr);
122   if (op == MPI_REPLACE) {
123     /* REPLACE is only meaningful when all processes have the same leafdata to reduce. Therefore copying from local leafdata is fine */
124     ierr = PetscLayoutGetRange(sf->map,&rstart,NULL);CHKERRQ(ierr);
125     ierr = (*link->Memcpy)(link,rootmtype,rootdata,leafmtype,(const char*)leafdata+(size_t)rstart*link->unitbytes,(size_t)sf->nroots*link->unitbytes);CHKERRQ(ierr);
126     if (PetscMemTypeDevice(leafmtype) && PetscMemTypeHost(rootmtype)) {ierr = (*link->SyncStream)(link);CHKERRQ(ierr);}
127   } else {
128     /* Reduce leafdata, then scatter to rootdata */
129     ierr = PetscObjectGetComm((PetscObject)sf,&comm);CHKERRQ(ierr);
130     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
131     ierr = PetscSFLinkPackLeafData(sf,link,PETSCSF_REMOTE,leafdata);CHKERRQ(ierr);
132     ierr = PetscSFLinkCopyLeafBufferInCaseNotUseGpuAwareMPI(sf,link,PETSC_TRUE/* device2host before sending */);CHKERRQ(ierr);
133     ierr = PetscSFLinkGetMPIBuffersAndRequests(sf,link,PETSCSF_LEAF2ROOT,&rootbuf,&leafbuf,&req,NULL);CHKERRQ(ierr);
134     ierr = PetscMPIIntCast(dat->rootbuflen[PETSCSF_REMOTE],&recvcount);CHKERRQ(ierr);
135     /* Allocate a separate leaf buffer on rank 0 */
136     if (rank == 0 && !link->leafbuf_alloc[PETSCSF_REMOTE][link->leafmtype_mpi]) {
137       ierr = PetscSFMalloc(sf,link->leafmtype_mpi,sf->leafbuflen[PETSCSF_REMOTE]*link->unitbytes,(void**)&link->leafbuf_alloc[PETSCSF_REMOTE][link->leafmtype_mpi]);CHKERRQ(ierr);
138     }
139     /* In case we already copied leafdata from device to host (i.e., no use_gpu_aware_mpi), we need to adjust leafbuf on rank 0 */
140     if (rank == 0 && link->leafbuf_alloc[PETSCSF_REMOTE][link->leafmtype_mpi] == leafbuf) leafbuf = MPI_IN_PLACE;
141     ierr = PetscMPIIntCast(sf->nleaves*link->bs,&count);CHKERRQ(ierr);
142     ierr = PetscSFLinkSyncStreamBeforeCallMPI(sf,link,PETSCSF_LEAF2ROOT);CHKERRQ(ierr);
143     ierr = MPI_Reduce(leafbuf,link->leafbuf_alloc[PETSCSF_REMOTE][link->leafmtype_mpi],count,link->basicunit,op,0,comm);CHKERRMPI(ierr); /* Must do reduce with MPI builltin datatype basicunit */
144     ierr = MPIU_Iscatterv(link->leafbuf_alloc[PETSCSF_REMOTE][link->leafmtype_mpi],dat->recvcounts,dat->displs,unit,rootbuf,recvcount,unit,0,comm,req);CHKERRMPI(ierr);
145   }
146   PetscFunctionReturn(0);
147 }
148 
149 PETSC_INTERN PetscErrorCode PetscSFReduceEnd_Allgatherv(PetscSF sf,MPI_Datatype unit,const void *leafdata,void *rootdata,MPI_Op op)
150 {
151   PetscErrorCode        ierr;
152   PetscSFLink           link;
153 
154   PetscFunctionBegin;
155   if (op == MPI_REPLACE) {
156     /* A rare case happens when op is MPI_REPLACE, using GPUs but no GPU aware MPI. In PetscSFReduceBegin_Allgather(v),
157       we did a device to device copy and in effect finished the communication. But in PetscSFLinkFinishCommunication()
158       of PetscSFReduceEnd_Basic(), it thinks since there is rootbuf, it calls PetscSFLinkCopyRootBufferInCaseNotUseGpuAwareMPI().
159       It does a host to device memory copy on rootbuf, wrongly overwritting the results. So we don't overload
160       PetscSFReduceEnd_Basic() in this case, and just reclaim the link.
161      */
162     ierr = PetscSFLinkGetInUse(sf,unit,rootdata,leafdata,PETSC_OWN_POINTER,&link);CHKERRQ(ierr);
163     ierr = PetscSFLinkReclaim(sf,&link);CHKERRQ(ierr);
164   } else {
165     ierr = PetscSFReduceEnd_Basic(sf,unit,leafdata,rootdata,op);CHKERRQ(ierr);
166   }
167   PetscFunctionReturn(0);
168 }
169 
170 static PetscErrorCode PetscSFBcastToZero_Allgatherv(PetscSF sf,MPI_Datatype unit,PetscMemType rootmtype,const void *rootdata,PetscMemType leafmtype,void *leafdata)
171 {
172   PetscErrorCode         ierr;
173   PetscSFLink            link;
174   PetscMPIInt            rank;
175 
176   PetscFunctionBegin;
177   ierr = PetscSFBcastBegin_Gatherv(sf,unit,rootmtype,rootdata,leafmtype,leafdata,MPI_REPLACE);CHKERRQ(ierr);
178   ierr = PetscSFLinkGetInUse(sf,unit,rootdata,leafdata,PETSC_OWN_POINTER,&link);CHKERRQ(ierr);
179   ierr = PetscSFLinkFinishCommunication(sf,link,PETSCSF_ROOT2LEAF);CHKERRQ(ierr);
180   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)sf),&rank);CHKERRMPI(ierr);
181   if (rank == 0 && PetscMemTypeDevice(leafmtype) && !sf->use_gpu_aware_mpi) {
182     ierr = (*link->Memcpy)(link,PETSC_MEMTYPE_DEVICE,leafdata,PETSC_MEMTYPE_HOST,link->leafbuf[PETSC_MEMTYPE_HOST],sf->leafbuflen[PETSCSF_REMOTE]*link->unitbytes);CHKERRQ(ierr);
183   }
184   ierr = PetscSFLinkReclaim(sf,&link);CHKERRQ(ierr);
185   PetscFunctionReturn(0);
186 }
187 
188 /* This routine is very tricky (I believe it is rarely used with this kind of graph so just provide a simple but not-optimal implementation).
189 
190    Suppose we have three ranks. Rank 0 has a root with value 1. Rank 0,1,2 has a leaf with value 2,3,4 respectively. The leaves are connected
191    to the root on rank 0. Suppose op=MPI_SUM and rank 0,1,2 gets root state in their rank order. By definition of this routine, rank 0 sees 1
192    in root, fetches it into its leafupate, then updates root to 1 + 2 = 3; rank 1 sees 3 in root, fetches it into its leafupate, then updates
193    root to 3 + 3 = 6; rank 2 sees 6 in root, fetches it into its leafupdate, then updates root to 6 + 4 = 10.  At the end, leafupdate on rank
194    0,1,2 is 1,3,6 respectively. root is 10.
195 
196    We use a simpler implementation. From the same initial state, we copy leafdata to leafupdate
197              rank-0   rank-1    rank-2
198         Root     1
199         Leaf     2       3         4
200      Leafupdate  2       3         4
201 
202    Do MPI_Exscan on leafupdate,
203              rank-0   rank-1    rank-2
204         Root     1
205         Leaf     2       3         4
206      Leafupdate  2       2         5
207 
208    BcastAndOp from root to leafupdate,
209              rank-0   rank-1    rank-2
210         Root     1
211         Leaf     2       3         4
212      Leafupdate  3       3         6
213 
214    Copy root to leafupdate on rank-0
215              rank-0   rank-1    rank-2
216         Root     1
217         Leaf     2       3         4
218      Leafupdate  1       3         6
219 
220    Reduce from leaf to root,
221              rank-0   rank-1    rank-2
222         Root     10
223         Leaf     2       3         4
224      Leafupdate  1       3         6
225 */
226 PETSC_INTERN PetscErrorCode PetscSFFetchAndOpBegin_Allgatherv(PetscSF sf,MPI_Datatype unit,PetscMemType rootmtype,void *rootdata,PetscMemType leafmtype,const void *leafdata,void *leafupdate,MPI_Op op)
227 {
228   PetscErrorCode         ierr;
229   PetscSFLink            link;
230   MPI_Comm               comm;
231   PetscMPIInt            count;
232 
233   PetscFunctionBegin;
234   ierr = PetscObjectGetComm((PetscObject)sf,&comm);CHKERRQ(ierr);
235   PetscCheckFalse(PetscMemTypeDevice(rootmtype) || PetscMemTypeDevice(leafmtype),comm,PETSC_ERR_SUP,"Do FetchAndOp on device");
236   /* Copy leafdata to leafupdate */
237   ierr = PetscSFLinkCreate(sf,unit,rootmtype,rootdata,leafmtype,leafdata,op,PETSCSF_FETCH,&link);CHKERRQ(ierr);
238   ierr = PetscSFLinkPackLeafData(sf,link,PETSCSF_REMOTE,leafdata);CHKERRQ(ierr); /* Sync the device */
239   ierr = (*link->Memcpy)(link,leafmtype,leafupdate,leafmtype,leafdata,sf->nleaves*link->unitbytes);CHKERRQ(ierr);
240   ierr = PetscSFLinkGetInUse(sf,unit,rootdata,leafdata,PETSC_OWN_POINTER,&link);CHKERRQ(ierr);
241 
242   /* Exscan on leafupdate and then BcastAndOp rootdata to leafupdate */
243   if (op == MPI_REPLACE) {
244     PetscMPIInt size,rank,prev,next;
245     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
246     ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr);
247     prev = rank ?            rank-1 : MPI_PROC_NULL;
248     next = (rank < size-1) ? rank+1 : MPI_PROC_NULL;
249     ierr = PetscMPIIntCast(sf->nleaves,&count);CHKERRQ(ierr);
250     ierr = MPI_Sendrecv_replace(leafupdate,count,unit,next,link->tag,prev,link->tag,comm,MPI_STATUSES_IGNORE);CHKERRMPI(ierr);
251   } else {
252     ierr = PetscMPIIntCast(sf->nleaves*link->bs,&count);CHKERRQ(ierr);
253     ierr = MPI_Exscan(MPI_IN_PLACE,leafupdate,count,link->basicunit,op,comm);CHKERRMPI(ierr);
254   }
255   ierr = PetscSFLinkReclaim(sf,&link);CHKERRQ(ierr);
256   ierr = PetscSFBcastBegin(sf,unit,rootdata,leafupdate,op);CHKERRQ(ierr);
257   ierr = PetscSFBcastEnd(sf,unit,rootdata,leafupdate,op);CHKERRQ(ierr);
258 
259   /* Bcast roots to rank 0's leafupdate */
260   ierr = PetscSFBcastToZero_Private(sf,unit,rootdata,leafupdate);CHKERRQ(ierr); /* Using this line makes Allgather SFs able to inherit this routine */
261 
262   /* Reduce leafdata to rootdata */
263   ierr = PetscSFReduceBegin(sf,unit,leafdata,rootdata,op);CHKERRQ(ierr);
264   PetscFunctionReturn(0);
265 }
266 
267 PETSC_INTERN PetscErrorCode PetscSFFetchAndOpEnd_Allgatherv(PetscSF sf,MPI_Datatype unit,void *rootdata,const void *leafdata,void *leafupdate,MPI_Op op)
268 {
269   PetscErrorCode         ierr;
270 
271   PetscFunctionBegin;
272   ierr = PetscSFReduceEnd(sf,unit,leafdata,rootdata,op);CHKERRQ(ierr);
273   PetscFunctionReturn(0);
274 }
275 
276 /* Get root ranks accessing my leaves */
277 PETSC_INTERN PetscErrorCode PetscSFGetRootRanks_Allgatherv(PetscSF sf,PetscInt *nranks,const PetscMPIInt **ranks,const PetscInt **roffset,const PetscInt **rmine,const PetscInt **rremote)
278 {
279   PetscErrorCode ierr;
280   PetscInt       i,j,k,size;
281   const PetscInt *range;
282 
283   PetscFunctionBegin;
284   /* Lazily construct these large arrays if users really need them for this type of SF. Very likely, they do not */
285   if (sf->nranks && !sf->ranks) { /* On rank!=0, sf->nranks=0. The sf->nranks test makes this routine also works for sfgatherv */
286     size = sf->nranks;
287     ierr = PetscLayoutGetRanges(sf->map,&range);CHKERRQ(ierr);
288     ierr = PetscMalloc4(size,&sf->ranks,size+1,&sf->roffset,sf->nleaves,&sf->rmine,sf->nleaves,&sf->rremote);CHKERRQ(ierr);
289     for (i=0; i<size; i++) sf->ranks[i] = i;
290     ierr = PetscArraycpy(sf->roffset,range,size+1);CHKERRQ(ierr);
291     for (i=0; i<sf->nleaves; i++) sf->rmine[i] = i; /*rmine are never NULL even for contiguous leaves */
292     for (i=0; i<size; i++) {
293       for (j=range[i],k=0; j<range[i+1]; j++,k++) sf->rremote[j] = k;
294     }
295   }
296 
297   if (nranks)  *nranks  = sf->nranks;
298   if (ranks)   *ranks   = sf->ranks;
299   if (roffset) *roffset = sf->roffset;
300   if (rmine)   *rmine   = sf->rmine;
301   if (rremote) *rremote = sf->rremote;
302   PetscFunctionReturn(0);
303 }
304 
305 /* Get leaf ranks accessing my roots */
306 PETSC_INTERN PetscErrorCode PetscSFGetLeafRanks_Allgatherv(PetscSF sf,PetscInt *niranks,const PetscMPIInt **iranks,const PetscInt **ioffset,const PetscInt **irootloc)
307 {
308   PetscErrorCode     ierr;
309   PetscSF_Allgatherv *dat = (PetscSF_Allgatherv*)sf->data;
310   MPI_Comm           comm;
311   PetscMPIInt        size,rank;
312   PetscInt           i,j;
313 
314   PetscFunctionBegin;
315   /* Lazily construct these large arrays if users really need them for this type of SF. Very likely, they do not */
316   ierr = PetscObjectGetComm((PetscObject)sf,&comm);CHKERRQ(ierr);
317   ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr);
318   ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
319   if (niranks) *niranks = size;
320 
321   /* PetscSF_Basic has distinguished incoming ranks. Here we do not need that. But we must put self as the first and
322      sort other ranks. See comments in PetscSFSetUp_Basic about MatGetBrowsOfAoCols_MPIAIJ on why.
323    */
324   if (iranks) {
325     if (!dat->iranks) {
326       ierr = PetscMalloc1(size,&dat->iranks);CHKERRQ(ierr);
327       dat->iranks[0] = rank;
328       for (i=0,j=1; i<size; i++) {if (i == rank) continue; dat->iranks[j++] = i;}
329     }
330     *iranks = dat->iranks; /* dat->iranks was init'ed to NULL by PetscNewLog */
331   }
332 
333   if (ioffset) {
334     if (!dat->ioffset) {
335       ierr = PetscMalloc1(size+1,&dat->ioffset);CHKERRQ(ierr);
336       for (i=0; i<=size; i++) dat->ioffset[i] = i*sf->nroots;
337     }
338     *ioffset = dat->ioffset;
339   }
340 
341   if (irootloc) {
342     if (!dat->irootloc) {
343       ierr = PetscMalloc1(sf->nleaves,&dat->irootloc);CHKERRQ(ierr);
344       for (i=0; i<size; i++) {
345         for (j=0; j<sf->nroots; j++) dat->irootloc[i*sf->nroots+j] = j;
346       }
347     }
348     *irootloc = dat->irootloc;
349   }
350   PetscFunctionReturn(0);
351 }
352 
353 PETSC_INTERN PetscErrorCode PetscSFCreateLocalSF_Allgatherv(PetscSF sf,PetscSF *out)
354 {
355   PetscInt       i,nroots,nleaves,rstart,*ilocal;
356   PetscSFNode    *iremote;
357   PetscSF        lsf;
358   PetscErrorCode ierr;
359 
360   PetscFunctionBegin;
361   nleaves = sf->nleaves ? sf->nroots : 0; /* sf->nleaves can be zero with SFGather(v) */
362   nroots  = nleaves;
363   ierr    = PetscMalloc1(nleaves,&ilocal);CHKERRQ(ierr);
364   ierr    = PetscMalloc1(nleaves,&iremote);CHKERRQ(ierr);
365   ierr    = PetscLayoutGetRange(sf->map,&rstart,NULL);CHKERRQ(ierr);
366 
367   for (i=0; i<nleaves; i++) {
368     ilocal[i]        = rstart + i; /* lsf does not change leave indices */
369     iremote[i].rank  = 0;          /* rank in PETSC_COMM_SELF */
370     iremote[i].index = i;          /* root index */
371   }
372 
373   ierr = PetscSFCreate(PETSC_COMM_SELF,&lsf);CHKERRQ(ierr);
374   ierr = PetscSFSetGraph(lsf,nroots,nleaves,ilocal,PETSC_OWN_POINTER,iremote,PETSC_OWN_POINTER);CHKERRQ(ierr);
375   ierr = PetscSFSetUp(lsf);CHKERRQ(ierr);
376   *out = lsf;
377   PetscFunctionReturn(0);
378 }
379 
380 PETSC_INTERN PetscErrorCode PetscSFCreate_Allgatherv(PetscSF sf)
381 {
382   PetscErrorCode     ierr;
383   PetscSF_Allgatherv *dat = (PetscSF_Allgatherv*)sf->data;
384 
385   PetscFunctionBegin;
386   sf->ops->BcastEnd        = PetscSFBcastEnd_Basic;
387   sf->ops->ReduceEnd       = PetscSFReduceEnd_Allgatherv;
388 
389   sf->ops->SetUp           = PetscSFSetUp_Allgatherv;
390   sf->ops->Reset           = PetscSFReset_Allgatherv;
391   sf->ops->Destroy         = PetscSFDestroy_Allgatherv;
392   sf->ops->GetRootRanks    = PetscSFGetRootRanks_Allgatherv;
393   sf->ops->GetLeafRanks    = PetscSFGetLeafRanks_Allgatherv;
394   sf->ops->GetGraph        = PetscSFGetGraph_Allgatherv;
395   sf->ops->BcastBegin      = PetscSFBcastBegin_Allgatherv;
396   sf->ops->ReduceBegin     = PetscSFReduceBegin_Allgatherv;
397   sf->ops->FetchAndOpBegin = PetscSFFetchAndOpBegin_Allgatherv;
398   sf->ops->FetchAndOpEnd   = PetscSFFetchAndOpEnd_Allgatherv;
399   sf->ops->CreateLocalSF   = PetscSFCreateLocalSF_Allgatherv;
400   sf->ops->BcastToZero     = PetscSFBcastToZero_Allgatherv;
401 
402   ierr = PetscNewLog(sf,&dat);CHKERRQ(ierr);
403   sf->data = (void*)dat;
404   PetscFunctionReturn(0);
405 }
406