Actual source code: sfgather.c

      1: #include <../src/vec/is/sf/impls/basic/gatherv/sfgatherv.h>
      2: #include <../src/vec/is/sf/impls/basic/allgather/sfallgather.h>

      4: /* Reuse the type. The difference is some fields (i.e., displs, recvcounts) are not used in Gather, which is not a big deal */
      5: typedef PetscSF_Allgatherv PetscSF_Gather;

      7: PETSC_INTERN PetscErrorCode PetscSFBcastBegin_Gather(PetscSF sf,MPI_Datatype unit,PetscMemType rootmtype,const void *rootdata,PetscMemType leafmtype,void *leafdata,MPI_Op op)
      8: {
      9:   PetscSFLink          link;
     10:   PetscMPIInt          sendcount;
     11:   MPI_Comm             comm;
     12:   void                 *rootbuf = NULL,*leafbuf = NULL;
     13:   MPI_Request          *req;

     15:   PetscSFLinkCreate(sf,unit,rootmtype,rootdata,leafmtype,leafdata,op,PETSCSF_BCAST,&link);
     16:   PetscSFLinkPackRootData(sf,link,PETSCSF_REMOTE,rootdata);
     17:   PetscSFLinkCopyRootBufferInCaseNotUseGpuAwareMPI(sf,link,PETSC_TRUE/* device2host before sending */);
     18:   PetscObjectGetComm((PetscObject)sf,&comm);
     19:   PetscMPIIntCast(sf->nroots,&sendcount);
     20:   PetscSFLinkGetMPIBuffersAndRequests(sf,link,PETSCSF_../../../../../..2LEAF,&rootbuf,&leafbuf,&req,NULL);
     21:   PetscSFLinkSyncStreamBeforeCallMPI(sf,link,PETSCSF_../../../../../..2LEAF);
     22:   MPIU_Igather(rootbuf == leafbuf ? MPI_IN_PLACE : rootbuf,sendcount,unit,leafbuf,sendcount,unit,0/*rank 0*/,comm,req);
     23:   return 0;
     24: }

     26: static PetscErrorCode PetscSFReduceBegin_Gather(PetscSF sf,MPI_Datatype unit,PetscMemType leafmtype,const void *leafdata,PetscMemType rootmtype,void *rootdata,MPI_Op op)
     27: {
     28:   PetscSFLink          link;
     29:   PetscMPIInt          recvcount;
     30:   MPI_Comm             comm;
     31:   void                 *rootbuf = NULL,*leafbuf = NULL;
     32:   MPI_Request          *req;

     34:   PetscSFLinkCreate(sf,unit,rootmtype,rootdata,leafmtype,leafdata,op,PETSCSF_REDUCE,&link);
     35:   PetscSFLinkPackLeafData(sf,link,PETSCSF_REMOTE,leafdata);
     36:   PetscSFLinkCopyLeafBufferInCaseNotUseGpuAwareMPI(sf,link,PETSC_TRUE/* device2host before sending */);
     37:   PetscObjectGetComm((PetscObject)sf,&comm);
     38:   PetscMPIIntCast(sf->nroots,&recvcount);
     39:   PetscSFLinkGetMPIBuffersAndRequests(sf,link,PETSCSF_LEAF2../../../../../..,&rootbuf,&leafbuf,&req,NULL);
     40:   PetscSFLinkSyncStreamBeforeCallMPI(sf,link,PETSCSF_LEAF2../../../../../..);
     41:   MPIU_Iscatter(leafbuf,recvcount,unit,rootbuf == leafbuf ? MPI_IN_PLACE : rootbuf,recvcount,unit,0/*rank 0*/,comm,req);
     42:   return 0;
     43: }

     45: PETSC_INTERN PetscErrorCode PetscSFCreate_Gather(PetscSF sf)
     46: {
     47:   PetscSF_Gather  *dat = (PetscSF_Gather*)sf->data;

     49:   sf->ops->BcastEnd        = PetscSFBcastEnd_Basic;
     50:   sf->ops->ReduceEnd       = PetscSFReduceEnd_Basic;

     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->FetchAndOpEnd   = PetscSFFetchAndOpEnd_Allgatherv;
     59:   sf->ops->CreateLocalSF   = PetscSFCreateLocalSF_Allgatherv;

     61:   /* Inherit from Allgather */
     62:   sf->ops->SetUp           = PetscSFSetUp_Allgather;

     64:   /* Inherit from Gatherv */
     65:   sf->ops->FetchAndOpBegin = PetscSFFetchAndOpBegin_Gatherv;

     67:   /* Gather stuff */
     68:   sf->ops->BcastBegin      = PetscSFBcastBegin_Gather;
     69:   sf->ops->ReduceBegin     = PetscSFReduceBegin_Gather;

     71:   PetscNewLog(sf,&dat);
     72:   sf->data = (void*)dat;
     73:   return 0;
     74: }