xref: /petsc/src/vec/is/sf/impls/basic/gather/sfgather.c (revision f253e43cc674c8507d337bbb5ef3ec57f9e3fddc)
1 #include <../src/vec/is/sf/impls/basic/gatherv/sfgatherv.h>
2 
3 #define PetscSFPackGet_Gather PetscSFPackGet_Allgatherv
4 
5 /* Reuse the type. The difference is some fields (i.e., displs, recvcounts) are not used in Gather, which is not a big deal */
6 typedef PetscSF_Allgatherv PetscSF_Gather;
7 
8 PETSC_INTERN PetscErrorCode PetscSFBcastAndOpBegin_Gather(PetscSF sf,MPI_Datatype unit,PetscMemType rootmtype,const void *rootdata,PetscMemType leafmtype,void *leafdata,MPI_Op op)
9 {
10   PetscErrorCode       ierr;
11   PetscSFPack          link;
12   PetscMPIInt          sendcount;
13   MPI_Comm             comm;
14   const void           *rootbuf_mpi; /* buffer used by MPI */
15   void                 *leafbuf_mpi;
16   PetscMemType         rootmtype_mpi,leafmtype_mpi;
17 
18   PetscFunctionBegin;
19   ierr = PetscSFPackGet_Gather(sf,unit,rootmtype,rootdata,leafmtype,leafdata,&link);CHKERRQ(ierr);
20   ierr = PetscObjectGetComm((PetscObject)sf,&comm);CHKERRQ(ierr);
21   ierr = PetscMPIIntCast(sf->nroots,&sendcount);CHKERRQ(ierr);
22   ierr = PetscSFBcastPrepareMPIBuffers_Allgatherv(sf,link,op,&rootmtype_mpi,&rootbuf_mpi,&leafmtype_mpi,&leafbuf_mpi);CHKERRQ(ierr);
23   ierr = MPIU_Igather(rootbuf_mpi,sendcount,unit,leafbuf_mpi,sendcount,unit,0/*rank 0*/,comm,link->rootreqs[PETSCSF_ROOT2LEAF_BCAST][rootmtype_mpi]);CHKERRQ(ierr);
24   PetscFunctionReturn(0);
25 }
26 
27 static PetscErrorCode PetscSFReduceBegin_Gather(PetscSF sf,MPI_Datatype unit,PetscMemType leafmtype,const void *leafdata,PetscMemType rootmtype,void *rootdata,MPI_Op op)
28 {
29   PetscErrorCode       ierr;
30   PetscSFPack          link;
31   PetscMPIInt          recvcount;
32   MPI_Comm             comm;
33   const void           *leafbuf_mpi;
34   void                 *rootbuf_mpi;
35   PetscMemType         leafmtype_mpi,rootmtype_mpi;
36 
37   PetscFunctionBegin;
38   ierr = PetscSFPackGet_Gather(sf,unit,rootmtype,rootdata,leafmtype,leafdata,&link);CHKERRQ(ierr);
39   ierr = PetscObjectGetComm((PetscObject)sf,&comm);CHKERRQ(ierr);
40   ierr = PetscMPIIntCast(sf->nroots,&recvcount);CHKERRQ(ierr);
41   ierr = PetscSFReducePrepareMPIBuffers_Gatherv(sf,link,op,&rootmtype_mpi,&rootbuf_mpi,&leafmtype_mpi,&leafbuf_mpi);CHKERRQ(ierr);
42   ierr = MPIU_Iscatter(leafbuf_mpi,recvcount,unit,rootbuf_mpi,recvcount,unit,0/*rank 0*/,comm,link->rootreqs[PETSCSF_LEAF2ROOT_REDUCE][rootmtype_mpi]);CHKERRQ(ierr);
43   PetscFunctionReturn(0);
44 }
45 
46 PETSC_INTERN PetscErrorCode PetscSFCreate_Gather(PetscSF sf)
47 {
48   PetscErrorCode  ierr;
49   PetscSF_Gather  *dat = (PetscSF_Gather*)sf->data;
50 
51   PetscFunctionBegin;
52   /* Inherit from Allgatherv */
53   sf->ops->Reset           = PetscSFReset_Allgatherv;
54   sf->ops->Destroy         = PetscSFDestroy_Allgatherv;
55   sf->ops->GetGraph        = PetscSFGetGraph_Allgatherv;
56   sf->ops->GetRootRanks    = PetscSFGetRootRanks_Allgatherv;
57   sf->ops->GetLeafRanks    = PetscSFGetLeafRanks_Allgatherv;
58   sf->ops->BcastAndOpEnd   = PetscSFBcastAndOpEnd_Allgatherv;
59   sf->ops->ReduceEnd       = PetscSFReduceEnd_Allgatherv;
60   sf->ops->FetchAndOpEnd   = PetscSFFetchAndOpEnd_Allgatherv;
61   sf->ops->CreateLocalSF   = PetscSFCreateLocalSF_Allgatherv;
62 
63   /* Inherit from Gatherv */
64   sf->ops->FetchAndOpBegin = PetscSFFetchAndOpBegin_Gatherv;
65 
66   /* Gather stuff */
67   sf->ops->BcastAndOpBegin = PetscSFBcastAndOpBegin_Gather;
68   sf->ops->ReduceBegin     = PetscSFReduceBegin_Gather;
69 
70   ierr     = PetscNewLog(sf,&dat);CHKERRQ(ierr);
71   sf->data = (void*)dat;
72   PetscFunctionReturn(0);
73 }
74 
75