Actual source code: sfgather.c

petsc-3.12.5 2020-03-29
Report Typos and Errors
  1:  #include <../src/vec/is/sf/impls/basic/gatherv/sfgatherv.h>

  3: #define PetscSFPackGet_Gather PetscSFPackGet_Allgatherv

  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;

  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          rank,sendcount;
 13:   MPI_Comm             comm;
 14:   char                 *recvbuf;

 17:   PetscSFPackGet_Gather(sf,unit,rootmtype,rootdata,leafmtype,leafdata,&link);
 18:   PetscObjectGetComm((PetscObject)sf,&comm);

 20:   if (op == MPIU_REPLACE) {
 21:     recvbuf = (char*)leafdata;
 22:   } else {
 23:     MPI_Comm_rank(comm,&rank);
 24:     if (!link->leafbuf[leafmtype] && !rank) {PetscMallocWithMemType(leafmtype,sf->nleaves*link->unitbytes,(void**)&link->leafbuf[leafmtype]);}
 25:     recvbuf = link->leafbuf[leafmtype];
 26:   }

 28:   PetscMPIIntCast(sf->nroots,&sendcount);
 29:   MPIU_Igather(rootdata,sendcount,unit,recvbuf,sendcount,unit,0/*rank 0*/,comm,link->rootreqs[PETSCSF_../../../../../..2LEAF_BCAST][rootmtype]);
 30:   return(0);
 31: }

 33: static PetscErrorCode PetscSFReduceBegin_Gather(PetscSF sf,MPI_Datatype unit,PetscMemType leafmtype,const void *leafdata,PetscMemType rootmtype,void *rootdata,MPI_Op op)
 34: {
 35:   PetscErrorCode       ierr;
 36:   PetscSFPack          link;
 37:   PetscMPIInt          recvcount;
 38:   MPI_Comm             comm;
 39:   void                 *recvbuf;

 42:   PetscSFPackGet_Gather(sf,unit,rootmtype,rootdata,leafmtype,leafdata,&link);
 43:   PetscObjectGetComm((PetscObject)sf,&comm);

 45:   if (op == MPIU_REPLACE) {
 46:     recvbuf = (char*)rootdata;
 47:   } else {
 48:     if (!link->rootbuf[rootmtype]) {PetscMallocWithMemType(rootmtype,sf->nroots*link->unitbytes,(void**)&link->rootbuf[rootmtype]);}
 49:     recvbuf = link->rootbuf[rootmtype];
 50:   }

 52:   PetscMPIIntCast(sf->nroots,&recvcount);
 53:   MPIU_Iscatter(leafdata,recvcount,unit,recvbuf,recvcount,unit,0/*rank 0*/,comm,link->rootreqs[PETSCSF_LEAF2../../../../../.._REDUCE][rootmtype]);
 54:   return(0);
 55: }

 57: PETSC_INTERN PetscErrorCode PetscSFCreate_Gather(PetscSF sf)
 58: {
 59:   PetscErrorCode  ierr;
 60:   PetscSF_Gather  *dat = (PetscSF_Gather*)sf->data;

 63:   /* Inherit from Allgatherv */
 64:   sf->ops->Reset           = PetscSFReset_Allgatherv;
 65:   sf->ops->Destroy         = PetscSFDestroy_Allgatherv;
 66:   sf->ops->GetGraph        = PetscSFGetGraph_Allgatherv;
 67:   sf->ops->GetRootRanks    = PetscSFGetRootRanks_Allgatherv;
 68:   sf->ops->GetLeafRanks    = PetscSFGetLeafRanks_Allgatherv;
 69:   sf->ops->BcastAndOpEnd   = PetscSFBcastAndOpEnd_Allgatherv;
 70:   sf->ops->ReduceEnd       = PetscSFReduceEnd_Allgatherv;
 71:   sf->ops->FetchAndOpEnd   = PetscSFFetchAndOpEnd_Allgatherv;
 72:   sf->ops->CreateLocalSF   = PetscSFCreateLocalSF_Allgatherv;

 74:   /* Inherit from Gatherv */
 75:   sf->ops->FetchAndOpBegin = PetscSFFetchAndOpBegin_Gatherv;

 77:   /* Gather stuff */
 78:   sf->ops->BcastAndOpBegin = PetscSFBcastAndOpBegin_Gather;
 79:   sf->ops->ReduceBegin     = PetscSFReduceBegin_Gather;

 81:   PetscNewLog(sf,&dat);
 82:   sf->data = (void*)dat;
 83:   return(0);
 84: }