From c1acda90f0563dccc6f3093c10a852bd96405a8b Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 21 Mar 2024 01:18:21 +0100 Subject: [PATCH] fix in obj_handle + mpi: RArray test works --- src/include/base/obj_handle.cc.h | 2 +- src/opt/mpi/include/cnorxz_mpi.cc.h | 2 ++ src/opt/mpi/include/cnorxz_mpi.h | 6 ++-- src/opt/mpi/include/mpi_base.h | 6 ++++ src/opt/mpi/include/raindex.cc.h | 35 ++++++++++++-------- src/opt/mpi/include/raindex.h | 7 ++-- src/opt/mpi/include/rarray.cc.h | 46 +++++++++++++++++++-------- src/opt/mpi/include/rarray.h | 10 ++++-- src/opt/mpi/tests/CMakeLists.txt | 7 ++++ src/opt/mpi/tests/rarray_unit_test.cc | 34 ++++++++++++-------- 10 files changed, 105 insertions(+), 50 deletions(-) diff --git a/src/include/base/obj_handle.cc.h b/src/include/base/obj_handle.cc.h index a72a498..1d11ee9 100644 --- a/src/include/base/obj_handle.cc.h +++ b/src/include/base/obj_handle.cc.h @@ -41,7 +41,7 @@ namespace CNORXZ template ObjHandle& ObjHandle::operator=(ObjHandle&& a) { - mC = a.mC; + mC = std::move(a.mC); return *this; } diff --git a/src/opt/mpi/include/cnorxz_mpi.cc.h b/src/opt/mpi/include/cnorxz_mpi.cc.h index 7a2846c..ed4ca4e 100644 --- a/src/opt/mpi/include/cnorxz_mpi.cc.h +++ b/src/opt/mpi/include/cnorxz_mpi.cc.h @@ -12,3 +12,5 @@ #include "mpi_wrappers.cc.h" //#include "rop_types.cc.h" #include "rrange.cc.h" +#include "raindex.cc.h" +#include "rarray.cc.h" diff --git a/src/opt/mpi/include/cnorxz_mpi.h b/src/opt/mpi/include/cnorxz_mpi.h index dac771d..e96e4d1 100644 --- a/src/opt/mpi/include/cnorxz_mpi.h +++ b/src/opt/mpi/include/cnorxz_mpi.h @@ -11,10 +11,10 @@ #include "mpi_base.h" #include "mpi_wrappers.h" -//#include "raindex.h" -//#include "rarray.h" -//#include "rop_types.h" #include "rrange.h" +#include "raindex.h" +#include "rarray.h" +//#include "rop_types.h" #include "typemap.h" #include "cnorxz_mpi.cc.h" diff --git a/src/opt/mpi/include/mpi_base.h b/src/opt/mpi/include/mpi_base.h index bcb8cf0..af0b344 100644 --- a/src/opt/mpi/include/mpi_base.h +++ b/src/opt/mpi/include/mpi_base.h @@ -27,6 +27,12 @@ namespace CNORXZ template class RIndex; + + template + class RAIndex; + + template + class RArray; // wrapper functions diff --git a/src/opt/mpi/include/raindex.cc.h b/src/opt/mpi/include/raindex.cc.h index 4b30ca9..6534a60 100644 --- a/src/opt/mpi/include/raindex.cc.h +++ b/src/opt/mpi/include/raindex.cc.h @@ -12,7 +12,10 @@ #ifndef __cxz_mpi_raindex_cc_h__ #define __cxz_mpi_raindex_cc_h__ -namespace CNOXRZ +#include +#include "raindex.h" + +namespace CNORXZ { namespace mpi { @@ -20,7 +23,7 @@ namespace CNOXRZ RAIndex::RAIndex(const T* loc, const RangePtr& range, SizeT lexpos) : RIndex(range, lexpos), mLoc(loc), - mCur(i.rank()), + mCur(rank()), mThisRank(getRankNumber()) { setBufferSize(); @@ -30,9 +33,9 @@ namespace CNOXRZ template RAIndex::RAIndex(const T* loc, const RIndex& i) : - RIndex(i) + RIndex(i), mLoc(loc), - mCur(i.rank()), + mCur(rank()), mThisRank(getRankNumber()) { setBufferSize(); @@ -65,25 +68,25 @@ namespace CNOXRZ template const T& RAIndex::operator*() const { + if(mCur != rank()){ + setBuffer(); + } if(rank() != mThisRank){ - if(mCur != rank()){ - setBuffer(); - } return mBuf[local()->pos() % mBufSize]; } else { - mLoc[local()->pos()]; + return mLoc[local()->pos()]; } } template const T* RAIndex::operator->() const { + if(mCur != rank()){ + setBuffer(); + } if(rank() != mThisRank){ - if(mCur != rank()){ - setBuffer(); - } - return mBuf + local()->pos() % mBufSize; + return mBuf.data() + local()->pos() % mBufSize; } else { return mLoc + local()->pos(); @@ -104,16 +107,20 @@ namespace CNOXRZ } template - void RAIndex::setBuffer() + void RAIndex::setBuffer() const { if(mBuf.size() != mBufSize){ mBuf.resize(mBufSize); } // A Bcast alternative with const pointer to source would be better... - std::memcpy(mBuf.data(), mLoc + local()->pos() / mBufSize, mBufSize*sizeof(T)); + const T* d = mLoc + (local()->pos() / mBufSize) * mBufSize; + std::memcpy(mBuf.data(), d, mBufSize*sizeof(T)); MPI_Bcast(mBuf.data(), mBufSize*sizeof(T), MPI_BYTE, static_cast(rank()), MPI_COMM_WORLD ); + mCur = rank(); } } // namespace mpi } // namespace CNOXRZ + +#endif diff --git a/src/opt/mpi/include/raindex.h b/src/opt/mpi/include/raindex.h index 22a0bee..2ffe939 100644 --- a/src/opt/mpi/include/raindex.h +++ b/src/opt/mpi/include/raindex.h @@ -13,6 +13,7 @@ #define __cxz_mpi_raindex_h__ #include "cnorxz.h" +#include "rrange.h" namespace CNORXZ { @@ -49,11 +50,11 @@ namespace CNORXZ private: - void setBuffer(); + void setBuffer() const; const T* mLoc = nullptr; - Vector mBuf; // used if iterating over content on different rank - SizeT mCur; // current rank in the buffer + mutable Vector mBuf; // used if iterating over content on different rank + mutable SizeT mCur; // current rank in the buffer SizeT mBufSize; SizeT mThisRank; }; diff --git a/src/opt/mpi/include/rarray.cc.h b/src/opt/mpi/include/rarray.cc.h index 3d630b0..9ef65a4 100644 --- a/src/opt/mpi/include/rarray.cc.h +++ b/src/opt/mpi/include/rarray.cc.h @@ -12,16 +12,34 @@ #ifndef __cxz_mpi_rarray_cc_h__ #define __cxz_mpi_rarray_cc_h__ +#include "rarray.h" +#include "raindex.h" + namespace CNORXZ { namespace mpi { template - RCArray::RCArray(const Sptr> a, const RangePtr& geom) : - mA(a), + RCArray::RCArray(const RCArray& a) : + mA(a.mA->copy()), + mGeom(a.mGeom), + mGlobal(a.mGlobal) + {} + + template + RCArray& RCArray::operator=(const RCArray& a) + { + mA = ObjHandle>(a.mA->copy()); + mGeom = a.mGeom; + mGlobal = a.mGlobal; + } + + template + RCArray::RCArray(const CArrayBase& a, const RangePtr& geom) : + mA(a.copy()), mGeom(geom), - mGlobal(RRangeFactory(a->range(),mGeom).create()) + mGlobal(RRangeFactory(rangeCast(a.range()),rangeCast(mGeom)).create()) {} template @@ -102,14 +120,16 @@ namespace CNORXZ inline decltype(auto) RCArray::operator()(const SPack& pack) const { CXZ_ERROR("not implemented"); - return COpRoot(); + //return COpRoot(); + return 0; } template inline decltype(auto) RCArray::operator()(const DPack& pack) const { CXZ_ERROR("not implemented"); - return COpRoot(); + //return COpRoot(); + return 0; } template @@ -131,27 +151,27 @@ namespace CNORXZ } template - const_iterator RCArray::begin() const + typename RCArray::const_iterator RCArray::begin() const { - return const_iterator(mA.data(), mGlobal); + return const_iterator(mA->data(), mGlobal); } template - const_iterator RCArray::end() const + typename RCArray::const_iterator RCArray::end() const { - return const_iterator(mA.data(), mGlobal, mGlobal->size()); + return const_iterator(mA->data(), mGlobal, mGlobal->size()); } template - const_iterator RCArray::cbegin() const + typename RCArray::const_iterator RCArray::cbegin() const { - return const_iterator(mA.data(), mGlobal); + return const_iterator(mA->data(), mGlobal); } template - const_iterator RCArray::cend() const + typename RCArray::const_iterator RCArray::cend() const { - return const_iterator(mA.data(), mGlobal, mGlobal->size()); + return const_iterator(mA->data(), mGlobal, mGlobal->size()); } template diff --git a/src/opt/mpi/include/rarray.h b/src/opt/mpi/include/rarray.h index bf82331..b5dfe75 100644 --- a/src/opt/mpi/include/rarray.h +++ b/src/opt/mpi/include/rarray.h @@ -13,6 +13,7 @@ #define __cxz_mpi_rarray_h__ #include "cnorxz.h" +#include "raindex.h" namespace CNORXZ { @@ -26,15 +27,18 @@ namespace CNORXZ class RCArray { public: - typedef RAIndex const_iterator; + typedef RAIndex const_iterator; - DEFAULT_MEMBERS(RCArray); + DEFAULT_C(RCArray); + DEFAULT_MOVE(RCArray); + RCArray(const RCArray& a); + RCArray& operator=(const RCArray& a); /** Construct from local array object. @param a Local array. @param geom Rank geometry. */ - RCArray(const Sptr> a, const RangePtr& geom); + RCArray(const CArrayBase& a, const RangePtr& geom); /** @copydoc CArrayBase::operator[] */ template diff --git a/src/opt/mpi/tests/CMakeLists.txt b/src/opt/mpi/tests/CMakeLists.txt index 3edd335..4de0e9b 100644 --- a/src/opt/mpi/tests/CMakeLists.txt +++ b/src/opt/mpi/tests/CMakeLists.txt @@ -1,8 +1,15 @@ add_definitions(-DTEST_NUMBER_FILE="${CMAKE_SOURCE_DIR}/src/tests/numbers.txt") include_directories(${CMAKE_SOURCE_DIR}/src/tests) + add_executable(mpirrutest rrange_unit_test.cc) add_dependencies(mpirrutest cnorxz cnorxzmpi test_lib) target_link_libraries(mpirrutest ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${MPI_LIBS} cnorxz cnorxzmpi test_lib) set(MPI_TEST_COMMAND mpirun -n 4 mpirrutest) add_test(NAME mpirrutest COMMAND ${MPI_TEST_COMMAND}) + +add_executable(mpirautest rarray_unit_test.cc) +add_dependencies(mpirautest cnorxz cnorxzmpi test_lib) +target_link_libraries(mpirautest ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${MPI_LIBS} cnorxz cnorxzmpi test_lib) +set(MPI_TEST_COMMAND mpirun -n 4 mpirautest) +add_test(NAME mpirautest COMMAND ${MPI_TEST_COMMAND}) diff --git a/src/opt/mpi/tests/rarray_unit_test.cc b/src/opt/mpi/tests/rarray_unit_test.cc index 506f4f9..8838851 100644 --- a/src/opt/mpi/tests/rarray_unit_test.cc +++ b/src/opt/mpi/tests/rarray_unit_test.cc @@ -77,16 +77,23 @@ namespace mGeom = YRangeFactory(gs).create(); mRRange = rrange(mGRange, mGeom); const SizeT size = ts.size()*xs.size()*xs.size()*xs.size(); - Vector vec = Numbers::get(0,size/4+10); - Vector data(size); - Vector mData(size*4); + const SizeT locsize = size/4; + Vector vec = Numbers::get(0,locsize+10); + Vector data(locsize); + mData.resize(size); const SizeT myrank = getRankNumber(); - for(SizeT i = 0; i != size; ++i){ + for(SizeT i = 0; i != locsize; ++i){ + assert(i < data.size()); data[i] = vec[i] * vec[i+myrank] / vec[i+2*myrank]; - mData[i + size*myrank] = data[i]; - MPI_Bcast(mData.data() + size*i, size, MPI_DOUBLE, i, MPI_COMM_WORLD); + assert(i + locsize*myrank < mData.size()); + mData[i + locsize*myrank] = data[i]; } - mLoc = std::make_shared>( mRRange->sub(1), data); + MPI_Barrier(MPI_COMM_WORLD); + for(SizeT r = 0; r != 4; ++r){ + MPI_Bcast(mData.data() + locsize*r, locsize, MPI_DOUBLE, r, MPI_COMM_WORLD); + MPI_Barrier(MPI_COMM_WORLD); + } + mLoc = MArray( mRRange->sub(1), data); mA = RCArray(mLoc, mGeom); } @@ -95,22 +102,23 @@ namespace RangePtr mGRange; RangePtr mGeom; RangePtr mRRange; - Sptr> mLoc; - RCArray mA; + MArray mLoc; + RCArray mA; Vector mData; }; - TEST_F(RArray_Test, Basics) + TEST_F(RCArray_Test, Basics) { EXPECT_EQ(mA.size(), mRRange->size()); } - - TEST_F(RArray_Test, GlobalIterate) + + TEST_F(RCArray_Test, GlobalIterate) { const SizeT size = mRRange->sub(1)->size(); auto e = mA.end(); for(auto i = mA.begin(); i != e; ++i){ - EXPECT_EQ(*i, mData[i.rank()*size + i.local().pos()]); + const Double x = *i; + EXPECT_EQ(x, mData[i.rank()*size + i.local()->pos()]); } } }