#include /*I "petscsys.h" I*/ #include PetscLogEvent PETSC_BuildTwoSided; PetscLogEvent PETSC_BuildTwoSidedF; const char *const PetscBuildTwoSidedTypes[] = { "ALLREDUCE", "IBARRIER", "REDSCATTER", "PetscBuildTwoSidedType", "PETSC_BUILDTWOSIDED_", NULL }; static PetscBuildTwoSidedType _twosided_type = PETSC_BUILDTWOSIDED_NOTSET; /*@ PetscCommBuildTwoSidedSetType - set algorithm to use when building two-sided communication Logically Collective Input Arguments: + comm - PETSC_COMM_WORLD - twosided - algorithm to use in subsequent calls to PetscCommBuildTwoSided() Level: developer Note: This option is currently global, but could be made per-communicator. .seealso: PetscCommBuildTwoSided(), PetscCommBuildTwoSidedGetType() @*/ PetscErrorCode PetscCommBuildTwoSidedSetType(MPI_Comm comm,PetscBuildTwoSidedType twosided) { PetscFunctionBegin; if (PetscDefined(USE_DEBUG)) { /* We don't have a PetscObject so can't use PetscValidLogicalCollectiveEnum */ PetscMPIInt ierr; PetscMPIInt b1[2],b2[2]; b1[0] = -(PetscMPIInt)twosided; b1[1] = (PetscMPIInt)twosided; ierr = MPIU_Allreduce(b1,b2,2,MPI_INT,MPI_MAX,comm);CHKERRQ(ierr); if (-b2[0] != b2[1]) SETERRQ(comm,PETSC_ERR_ARG_WRONG,"Enum value must be same on all processes"); } _twosided_type = twosided; PetscFunctionReturn(0); } /*@ PetscCommBuildTwoSidedGetType - set algorithm to use when building two-sided communication Logically Collective Output Arguments: + comm - communicator on which to query algorithm - twosided - algorithm to use for PetscCommBuildTwoSided() Level: developer .seealso: PetscCommBuildTwoSided(), PetscCommBuildTwoSidedSetType() @*/ PetscErrorCode PetscCommBuildTwoSidedGetType(MPI_Comm comm,PetscBuildTwoSidedType *twosided) { PetscErrorCode ierr; PetscMPIInt size; PetscFunctionBegin; *twosided = PETSC_BUILDTWOSIDED_NOTSET; if (_twosided_type == PETSC_BUILDTWOSIDED_NOTSET) { ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); _twosided_type = PETSC_BUILDTWOSIDED_ALLREDUCE; /* default for small comms, see https://gitlab.com/petsc/petsc/-/merge_requests/2611 */ #if defined(PETSC_HAVE_MPI_IBARRIER) if (size > 1024) _twosided_type = PETSC_BUILDTWOSIDED_IBARRIER; #endif ierr = PetscOptionsGetEnum(NULL,NULL,"-build_twosided",PetscBuildTwoSidedTypes,(PetscEnum*)&_twosided_type,NULL);CHKERRQ(ierr); } *twosided = _twosided_type; PetscFunctionReturn(0); } #if defined(PETSC_HAVE_MPI_IBARRIER) || defined(PETSC_HAVE_MPIX_IBARRIER) static PetscErrorCode PetscCommBuildTwoSided_Ibarrier(MPI_Comm comm,PetscMPIInt count,MPI_Datatype dtype,PetscMPIInt nto,const PetscMPIInt *toranks,const void *todata,PetscMPIInt *nfrom,PetscMPIInt **fromranks,void *fromdata) { PetscErrorCode ierr; PetscMPIInt nrecvs,tag,done,i; MPI_Aint lb,unitbytes; char *tdata; MPI_Request *sendreqs,barrier; PetscSegBuffer segrank,segdata; PetscBool barrier_started; PetscFunctionBegin; ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr); ierr = MPI_Type_get_extent(dtype,&lb,&unitbytes);CHKERRQ(ierr); if (lb != 0) SETERRQ1(comm,PETSC_ERR_SUP,"Datatype with nonzero lower bound %ld\n",(long)lb); tdata = (char*)todata; ierr = PetscMalloc1(nto,&sendreqs);CHKERRQ(ierr); for (i=0; iiflags) { ierr = PetscCalloc1(size,&counter->iflags);CHKERRQ(ierr); iflags = counter->iflags; } else { iflags = counter->iflags; ierr = PetscArrayzero(iflags,size);CHKERRQ(ierr); } for (i=0; iiflags) { ierr = PetscCalloc1(size,&counter->iflags);CHKERRQ(ierr); iflags = counter->iflags; } else { iflags = counter->iflags; ierr = PetscArrayzero(iflags,size);CHKERRQ(ierr); } for (i=0; i - algorithm to set up two-sided communication. Default is allreduce for communicators with <= 1024 ranks, otherwise ibarrier. Notes: This memory-scalable interface is an alternative to calling PetscGatherNumberOfMessages() and PetscGatherMessageLengths(), possibly with a subsequent round of communication to send other constant-size data. Basic data types as well as contiguous types are supported, but non-contiguous (e.g., strided) types are not. References: . 1. - Hoefler, Siebert and Lumsdaine, The MPI_Ibarrier implementation uses the algorithm in Scalable communication protocols for dynamic sparse data exchange, 2010. .seealso: PetscGatherNumberOfMessages(), PetscGatherMessageLengths() @*/ PetscErrorCode PetscCommBuildTwoSided(MPI_Comm comm,PetscMPIInt count,MPI_Datatype dtype,PetscMPIInt nto,const PetscMPIInt *toranks,const void *todata,PetscMPIInt *nfrom,PetscMPIInt **fromranks,void *fromdata) { PetscErrorCode ierr; PetscBuildTwoSidedType buildtype = PETSC_BUILDTWOSIDED_NOTSET; PetscFunctionBegin; ierr = PetscSysInitializePackage();CHKERRQ(ierr); ierr = PetscLogEventSync(PETSC_BuildTwoSided,comm);CHKERRQ(ierr); ierr = PetscLogEventBegin(PETSC_BuildTwoSided,0,0,0,0);CHKERRQ(ierr); ierr = PetscCommBuildTwoSidedGetType(comm,&buildtype);CHKERRQ(ierr); switch (buildtype) { case PETSC_BUILDTWOSIDED_IBARRIER: #if defined(PETSC_HAVE_MPI_IBARRIER) || defined(PETSC_HAVE_MPIX_IBARRIER) ierr = PetscCommBuildTwoSided_Ibarrier(comm,count,dtype,nto,toranks,todata,nfrom,fromranks,fromdata);CHKERRQ(ierr); #else SETERRQ(comm,PETSC_ERR_PLIB,"MPI implementation does not provide MPI_Ibarrier (part of MPI-3)"); #endif break; case PETSC_BUILDTWOSIDED_ALLREDUCE: ierr = PetscCommBuildTwoSided_Allreduce(comm,count,dtype,nto,toranks,todata,nfrom,fromranks,fromdata);CHKERRQ(ierr); break; case PETSC_BUILDTWOSIDED_REDSCATTER: #if defined(PETSC_HAVE_MPI_REDUCE_SCATTER_BLOCK) ierr = PetscCommBuildTwoSided_RedScatter(comm,count,dtype,nto,toranks,todata,nfrom,fromranks,fromdata);CHKERRQ(ierr); #else SETERRQ(comm,PETSC_ERR_PLIB,"MPI implementation does not provide MPI_Reduce_scatter_block (part of MPI-2.2)"); #endif break; default: SETERRQ(comm,PETSC_ERR_PLIB,"Unknown method for building two-sided communication"); } ierr = PetscLogEventEnd(PETSC_BuildTwoSided,0,0,0,0);CHKERRQ(ierr); PetscFunctionReturn(0); } static PetscErrorCode PetscCommBuildTwoSidedFReq_Reference(MPI_Comm comm,PetscMPIInt count,MPI_Datatype dtype,PetscMPIInt nto,const PetscMPIInt *toranks,const void *todata, PetscMPIInt *nfrom,PetscMPIInt **fromranks,void *fromdata,PetscMPIInt ntags,MPI_Request **toreqs,MPI_Request **fromreqs, PetscErrorCode (*send)(MPI_Comm,const PetscMPIInt[],PetscMPIInt,PetscMPIInt,void*,MPI_Request[],void*), PetscErrorCode (*recv)(MPI_Comm,const PetscMPIInt[],PetscMPIInt,void*,MPI_Request[],void*),void *ctx) { PetscErrorCode ierr; PetscMPIInt i,*tag; MPI_Aint lb,unitbytes; MPI_Request *sendreq,*recvreq; PetscFunctionBegin; ierr = PetscMalloc1(ntags,&tag);CHKERRQ(ierr); if (ntags > 0) { ierr = PetscCommDuplicate(comm,&comm,&tag[0]);CHKERRQ(ierr); } for (i=1; i