From 9a2b9fdf1ce342147ec532352c10655d48c83968 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 23 Apr 2020 23:23:41 +0200 Subject: [PATCH] WIP --- src/MEDCoupling/MCAuto.hxx | 20 ++++++++++++---- .../MEDCouplingDataArrayTypemaps.i | 11 +++++++++ src/ParaMEDMEM/CommInterface.cxx | 23 +++++++++++++++++-- src/ParaMEDMEM/CommInterface.hxx | 18 ++++++++++++++- src/ParaMEDMEM_Swig/ParaMEDMEMCommon.i | 19 +++++++++++++++ 5 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/MEDCoupling/MCAuto.hxx b/src/MEDCoupling/MCAuto.hxx index 1d83037b9..5b29608b5 100644 --- a/src/MEDCoupling/MCAuto.hxx +++ b/src/MEDCoupling/MCAuto.hxx @@ -32,14 +32,14 @@ namespace MEDCoupling class MCAuto { public: - static MCAuto TakeRef(T *ptr) { MCAuto ret(MCAuto(nullptr)); ret.takeRef(ptr); return ret; } - MCAuto(const MCAuto& other):_ptr(0) { referPtr(other._ptr); } - MCAuto(T *ptr=0):_ptr(ptr) { } + static MCAuto TakeRef(T *ptr) { MCAuto ret; ret.takeRef(ptr); return ret; } + MCAuto(const MCAuto& other):_ptr(nullptr) { referPtr(other._ptr); } + MCAuto(T *ptr=nullptr):_ptr(ptr) { } ~MCAuto() { destroyPtr(); } void checkNotNull() const { if(!_ptr) throw INTERP_KERNEL::Exception("Pointer is nullptr !"); } - bool isNull() const { return _ptr==0; } + bool isNull() const { return _ptr==nullptr; } bool isNotNull() const { return !isNull(); } - void nullify() { destroyPtr(); _ptr=0; } + void nullify() { destroyPtr(); _ptr=nullptr; } bool operator==(const MCAuto& other) const { return _ptr==other._ptr; } bool operator==(const T *other) const { return _ptr==other; } MCAuto &operator=(const MCAuto& other) { if(_ptr!=other._ptr) { destroyPtr(); referPtr(other._ptr); } return *this; } @@ -71,6 +71,16 @@ namespace MEDCoupling return ret; } + template + std::vector> FromVecToVecAuto(const std::vector& inputVec) + { + std::size_t size(inputVec.size()); + std::vector> ret(size); + typename std::vector>::iterator itArrays(ret.begin()); + std::for_each(inputVec.begin(),inputVec.end(),[&itArrays](T *elt) { (*itArrays).takeRef(elt); itArrays++; }); + return ret; + } + template typename MEDCoupling::MCAuto DynamicCast(typename MEDCoupling::MCAuto& autoSubPtr) throw() { diff --git a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i index f6d8969d4..a3f52dec0 100644 --- a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i @@ -1389,6 +1389,17 @@ static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, cons throw INTERP_KERNEL::Exception("convertFromPyObjVectorOfObj : not a list nor a tuple"); } +//convertFromVectorAutoObjToPyObj(inpv,SWIGTYPE_p_MEDCoupling__MEDCouplingUMesh) +template +static PyObject *convertFromVectorAutoObjToPyObj(std::vector< MCAuto >& inpVector, swig_type_info *ty) +{ + std::size_t sz(inpVector.size()); + PyObject *ret = PyList_New(sz); + for(std::size_t i=0;i cpp int sw=1 * if python list[int] -> cpp vector sw=2 diff --git a/src/ParaMEDMEM/CommInterface.cxx b/src/ParaMEDMEM/CommInterface.cxx index 9aaebdaec..be6b42a94 100644 --- a/src/ParaMEDMEM/CommInterface.cxx +++ b/src/ParaMEDMEM/CommInterface.cxx @@ -62,7 +62,25 @@ namespace MEDCoupling * Generalized AllGather collective communication. * This method send input \a array to all procs. */ - void CommInterface::allGatherArrays(MPI_Comm comm, const DataArrayIdType *array, std::unique_ptr& result, std::unique_ptr& resultIndex) const + void CommInterface::allGatherArrays(MPI_Comm comm, const DataArrayIdType *array, std::vector< MCAuto >& arraysOut) const + { + std::unique_ptr result, resultIndex; + int size(this->allGatherArrays(comm,array,result,resultIndex)); + arraysOut.resize(size); + for(int i = 0 ; i < size ; ++i) + { + arraysOut[i] = DataArrayIdType::New(); + mcIdType nbOfEltPack(resultIndex[i+1]-resultIndex[i]); + arraysOut[i]->alloc(nbOfEltPack,1); + std::copy(result.get()+resultIndex[i],result.get()+resultIndex[i+1],arraysOut[i]->getPointer()); + } + } + + /*! + * Generalized AllGather collective communication. + * This method send input \a array to all procs. + */ + int CommInterface::allGatherArrays(MPI_Comm comm, const DataArrayIdType *array, std::unique_ptr& result, std::unique_ptr& resultIndex) const { int size; this->commSize(comm,&size); @@ -74,7 +92,8 @@ namespace MEDCoupling std::unique_ptr nbOfElemsInt( CommInterface::ToIntArray(nbOfElems,size) ); std::unique_ptr offsetsIn( CommInterface::ComputeOffset(nbOfElemsInt,size) ); this->allGatherV(array->begin(),nbOfCellsRequested,MPI_ID_TYPE,result.get(),nbOfElemsInt.get(),offsetsIn.get(),MPI_ID_TYPE,comm); - resultIndex = std::move(nbOfElems); + resultIndex = ComputeOffsetFull(nbOfElems,size); + return size; } /*! diff --git a/src/ParaMEDMEM/CommInterface.hxx b/src/ParaMEDMEM/CommInterface.hxx index d132d09db..d64eaebec 100644 --- a/src/ParaMEDMEM/CommInterface.hxx +++ b/src/ParaMEDMEM/CommInterface.hxx @@ -94,7 +94,8 @@ namespace MEDCoupling MPI_Op op, int root, MPI_Comm comm) const { return MPI_Reduce(sendbuf, recvbuf, count, datatype, op, root, comm); } int allReduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) const { return MPI_Allreduce(sendbuf, recvbuf, count, datatype, op, comm); } public: - void allGatherArrays(MPI_Comm comm, const DataArrayIdType *array, std::unique_ptr& result, std::unique_ptr& resultIndex) const; + void allGatherArrays(MPI_Comm comm, const DataArrayIdType *array, std::vector< MCAuto >& arraysOut) const; + int allGatherArrays(MPI_Comm comm, const DataArrayIdType *array, std::unique_ptr& result, std::unique_ptr& resultIndex) const; void allToAllArrays(MPI_Comm comm, const std::vector< MCAuto >& arrays, std::vector< MCAuto >& arraysOut) const; public: @@ -138,5 +139,20 @@ namespace MEDCoupling } return ret; } + + /*! + * Helper of alltoallv and allgatherv + */ + template + static std::unique_ptr ComputeOffsetFull(const std::unique_ptr& counts, std::size_t sizeOfCounts) + { + std::unique_ptr ret(new T[sizeOfCounts+1]); + ret[0] = static_cast(0); + for(std::size_t i = 1 ; i < sizeOfCounts+1 ; ++i) + { + ret[i] = ret[i-1] + counts[i-1]; + } + return ret; + } }; } diff --git a/src/ParaMEDMEM_Swig/ParaMEDMEMCommon.i b/src/ParaMEDMEM_Swig/ParaMEDMEMCommon.i index 452834e76..5bdcc0533 100644 --- a/src/ParaMEDMEM_Swig/ParaMEDMEMCommon.i +++ b/src/ParaMEDMEM_Swig/ParaMEDMEMCommon.i @@ -127,6 +127,25 @@ namespace MEDCoupling int reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) const; int allReduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) const; + %extend + { + PyObject *allGatherArrays(const DataArrayIdType *array) const + { + std::vector< MCAuto > ret; + self->allGatherArrays(MPI_COMM_WORLD,array,ret); + return convertFromVectorAutoObjToPyObj(ret,SWIGTITraits::TI); + } + + PyObject *allToAllArrays(PyObject *arrays) const + { + std::vector< DataArrayIdType * > arraysIn; + std::vector< MCAuto > arrayOut; + convertFromPyObjVectorOfObj(arrays,SWIGTITraits::TI,"DataArrayIdType",arraysIn); + std::vector< MCAuto > arraysIn2(FromVecToVecAuto(arraysIn)); + self->allToAllArrays(MPI_COMM_WORLD,arraysIn2,arrayOut); + return convertFromVectorAutoObjToPyObj(arrayOut,SWIGTITraits::TI); + } + } }; class ParaUMesh -- 2.39.2