From 5db90864c7dbe7e0f6c58d4a757d505cc82f03b5 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 10 Mar 2024 23:56:33 +0100 Subject: [PATCH] WIP: mpi related ranges --- src/include/ranges/mrange.h | 4 +- src/include/ranges/urange.h | 5 +- src/opt/mpi/include/rop_types.cc.h | 87 ++++++++++++ src/opt/mpi/include/rop_types.h | 7 + src/opt/mpi/include/rrange.h | 216 +++++++++++------------------ src/opt/mpi/lib/rrange.cc | 137 ------------------ 6 files changed, 184 insertions(+), 272 deletions(-) create mode 100644 src/opt/mpi/include/rop_types.cc.h delete mode 100644 src/opt/mpi/lib/rrange.cc diff --git a/src/include/ranges/mrange.h b/src/include/ranges/mrange.h index a9a190c..0cbda99 100644 --- a/src/include/ranges/mrange.h +++ b/src/include/ranges/mrange.h @@ -367,8 +367,8 @@ namespace CNORXZ /** Get sub-ranges. */ decltype(auto) space() const; - /** Get sub-range. - @param pos Position of the sub-range. + /** Get meta data for given lexicographic position. + @param pos Lexicographic position. */ const MetaType get(SizeT pos) const; diff --git a/src/include/ranges/urange.h b/src/include/ranges/urange.h index c046081..adcbf25 100644 --- a/src/include/ranges/urange.h +++ b/src/include/ranges/urange.h @@ -162,7 +162,10 @@ namespace CNORXZ /** **** Uni-(1-)dimensional range with non-trivial meta data space - i.e. the parameter space can be arbitrary. + i.e. the parameter space can be arbitrary. However, the parameter space + is assumed to be ordered, i.e. the i-1-th element is assumed to be smaller + (according to std::less) than the i-th element. + (In the future, there will be another range type, where random ordering is possible.) @tparam MetaT Meta data type. */ template diff --git a/src/opt/mpi/include/rop_types.cc.h b/src/opt/mpi/include/rop_types.cc.h new file mode 100644 index 0000000..350fba4 --- /dev/null +++ b/src/opt/mpi/include/rop_types.cc.h @@ -0,0 +1,87 @@ + +#include "rop_types.h" + +namespace CNORXZ +{ + /*================+ + | CROpRoot | + +================*/ + + template + template + constexpr decltype(auto) CROpRoot::operator()(const PosT& pos) const + { + const SizeT L = mIndex->lmax().val(); + const SizeT m = mIndex->myrank() + const SizeT dst = ( m + pos.val() ) % L; + const SizeT src = ( m - pos.val() + L ) % L; + + } + + template + constexpr decltype(auto) CROpRoot::operator()() const + { + + } + + template + template + constexpr decltype(auto) CROpRoot::rootSteps(const IndexId& id) const + { + + } + + /*===============+ + | ROpRoot | + +===============*/ + + template + template + constexpr decltype(auto) ROpRoot::operator()(const PosT& pos) const + { + + } + + template + constexpr decltype(auto) ROpRoot::operator()() const + { + + } + + template + template + constexpr decltype(auto) ROpRoot::rootSteps(const IndexId& id) const + { + + } + + /*====================+ + | RContraction | + +====================*/ + + template + constexpr RContraction::RContraction(CXpr&& cxpr) : + mCXpr(std::forward(cxpr)) + {} + + template + template + constexpr decltype(auto) RContraction::operator()(const PosT& pos) const + { + + } + + template + constexpr decltype(auto) RContraction::operator()() const + { + + } + + template + template + constexpr decltype(auto) RContraction::rootSteps(const IndexId& id) const + { + + } + +} diff --git a/src/opt/mpi/include/rop_types.h b/src/opt/mpi/include/rop_types.h index 50cb6e5..c9f8406 100644 --- a/src/opt/mpi/include/rop_types.h +++ b/src/opt/mpi/include/rop_types.h @@ -29,6 +29,10 @@ namespace CNORXZ template constexpr decltype(auto) rootSteps(const IndexId& id) const; + + private: + const T* mData; + COpRoot }; template @@ -46,6 +50,9 @@ namespace CNORXZ template constexpr decltype(auto) rootSteps(const IndexId& id) const; + + private: + }; template diff --git a/src/opt/mpi/include/rrange.h b/src/opt/mpi/include/rrange.h index a98bb83..df8c11e 100644 --- a/src/opt/mpi/include/rrange.h +++ b/src/opt/mpi/include/rrange.h @@ -1,9 +1,9 @@ // -*- C++ -*- /** - @file opt/mpi/include/rrange.h - @brief RRange and RIndex declaration - + @file opt/include/rrange.h + @brief RRange and RIndex declaration. + Copyright (c) 2024 Christian Zimmermann. All rights reserved. Mail: chizeta@f3l.de @@ -16,45 +16,25 @@ namespace CNORXZ { + /** **** - Specific index for RRanges. + Specific index for RRange. + @tparam Index Local index type. */ - class RIndex : public IndexInterface>> + template + class RIndex : public IndexInterface,typename Index::MetaType> { public: - typedef IndexInterface>> IB; - typedef RRange RangeType; - typedef Vector MetaType; + typedef IndexInterface,typename Index::MetaType> IB; + typedef typename Index::MetaType MetaType; + typedef RRange RangeType; INDEX_RANDOM_ACCESS_ITERATOR_DEFS(MetaType); - /** Default constructor. */ - RIndex() = default; - - /** Move constructor. */ - RIndex(RIndex&& i) = default; - - /** Move assignment. */ - RIndex& operator=(RIndex&& i) = default; - - /** Copy constructor. - No default copy: Have to copy sub-index instances - */ - RIndex(const RIndex& i); - - /** Copy assigment. - No default copy: Have to copy sub-index instances - */ - RIndex& operator=(const RIndex& i); - - /** Construct from a range and an initial lexicographic position - @param range Range to iterate over. - @param lexpos Initial lexicographic position. - */ - RIndex(const RangePtr& range, SizeT lexpos = 0); - + // constructors!!! + /** @copydoc IndexInterface::operator=(SizeT) */ - RIndex& operator=(SizeT lexpos); + RIndex& operator=(SizeT pos); /** @copydoc IndexInterface::operator++() */ RIndex& operator++(); @@ -81,141 +61,83 @@ namespace CNORXZ SizeT lex() const; /** @copydoc IndexInterface::pmax() */ - UPos pmax() const; + constexpr decltype(auto) pmax() const; /** @copydoc IndexInterface::lmax() */ - UPos lmax() const; + constexpr decltype(auto) lmax() const; /** @copydoc IndexInterface::id() */ IndexId<0> id() const; - - /** @copydoc IndexInterface::operator*() */ - Vector operator*() const; + /** @copydoc IndexInterface::operator*() */ + MetaType operator*() const; + /** @copydoc IndexInterface::dim() */ - SizeT dim() const; + constexpr SizeT dim() const; /** @copydoc IndexInterface::range() */ - Sptr range() const; + Sptr range() const; /** @copydoc IndexInterface::stepSize() */ - UPos stepSize(const IndexId<0> id) const; + template + decltype(auto) stepSize(const IndexId& id) const; /** @copydoc IndexInterface::stringMeta() */ String stringMeta() const; /** @copydoc IndexInterface::meta() */ - Vector meta() const; + MetaType meta() const; /** @copydoc IndexInterface::at() */ - RIndex& at(const Vector& meta); + RIndex& at(const MetaType& metaPos); /** @copydoc IndexInterface::prange() */ - RangePtr prange(const RIndex& last) const; + RangePtr prange(const RIndex& last) const; /** @copydoc IndexInterface::deepFormat() */ - Vector deepFormat() const; + auto deepFormat() const; /** @copydoc IndexInterface::deepMax() */ - Vector deepMax() const; + auto deepMax() const; /** @copydoc IndexInterface::reformat() */ RIndex& reformat(const Vector& f, const Vector& s); /** @copydoc IndexInterface::ifor() */ - DXpr ifor(const DXpr& xpr, NoF&& f) const; + template + constexpr decltype(auto) ifor(const Xpr& xpr, F&& f) const; /** @copydoc IndexInterface::formatIsTrivial() */ bool formatIsTrivial() const; - /** Replace sub-index instances. - All linearized positions are updated accordingly. - @param i Pointer to RIndex which provides the new sub-index instance - */ - RIndex& operator()(const Sptr& i); + /** @copydoc IndexInterface::xpr() */ + decltype(auto) xpr(const Sptr>& _this) const; - /** Update all linearized positions. */ - RIndex& operator()(); - - /** Get all sub-indices - @return Pack of sub-indices - */ - const CPack& pack() const; - - /** Get index format. - @return The format. - */ - const YFormat& format() const; - - /** Get lexicographic (trivial) index format. - @return The lexicographic format. - */ - const YFormat& lexFormat() const; - - /** Set the index format. - @param bs The new format. - */ - RIndex& setFormat(const YFormat& bs); - - /** Set position of given sub index and update total index position. - @param ind Sub-index number [0,dim()-1]. - @param lex Lexicographic position to be assigned to the index. - */ - RIndex& setSub(SizeT ind, SizeT lex); + //!!! private: - Sptr mRange; - Vector> mIs; // -> CPack!!! - YFormat mLexFormat; // = mFormat (rank geometry is fixed and unique) - UPos mPMax = 0; // = mLMax (same reason) - } - - /** **** - Specialization: RIndex is a multi-index. - @see index_is_multi - */ - template <> - struct index_is_multi - { static constexpr bool value = true; }; - - /** **** - Specialization: RIndex has sub-indices. - @see has_sub - */ - template <> - struct has_sub - { static constexpr bool value = true; }; - - /** **** - Specific factory for RRange. - */ - class RRangeFactory : public RangeFactoryBase - { - public: - /** Construct and setup factory. - @param geom SizeT vector specifying the rank geometry - */ - RRangeFactory(const Vector& geom); - - private: - RRangeFactory() = default; - virtual void make() override final; - - MArray mRA; + Sptr mRange; + Sptr mLocalI; + //!!! }; + // Factory!!! + /** **** - Multi-dimensional range specifying the rank coordinate system + Range-Wrapper for ranges that are distributed on MPI ranks. + @tparam Range Local range type. */ - class RRange : public RangeInterface + template + class RRange : public RangeInterface> { public: typedef RangeBase RB; - typedef RIndex IndexType; + typedef RIndex IndexType; + typedef typename Range::MetaType MetaType; - friend RRangeFactory; - - virtual RangePtr sub(SizeT i) const override final; + friend RRangeFactory; + + virtual RangePtr sub(SizeT num) const override final; virtual MArray sub() const override final; virtual SizeT size() const override final; virtual SizeT dim() const override final; @@ -224,19 +146,49 @@ namespace CNORXZ virtual const TypeInfo& metaType() const override final; virtual RangePtr extend(const RangePtr& r) const override final; - int myrank() const; + /** Get local range. */ + Sptr local() const; - private: + /** Get meta data for given lexicographic position. + @param pos Lexicographic position. + */ + const MetaType get(SizeT pos) const; + /** Get lexicographic position according to the given meta data value. + @param metaPos Meta data value. + */ + SizeT getMeta(const MetaType& metaPos) const; + + /** Get rank from lexicographic meta data position. + @param pos Lexicographic meta data position. + */ + int getRank(SizeT pos) const; + + protected: + + /** Dafault constructor */ RRange() = default; - RRange(const RRange& a) = delete; - RRange(const MArray& rvec); - MArray mRA; - int mMyRank = 0; + RRange(const RRange& in) = delete; + RRange& operator=(const RRange& in) = delete; - virtual Vector key() const override final; + /** Construct from local range and geometry. + @param loc Local range. + @param geom Rank geometry range. + */ + RRange(const Sptr& loc, const Sptr& geom); + + Sptr mLocal; /**< Local range of THIS rank. */ + Sptr mGeom; /**< Rank geometry range. */ + }; + + /** Create RRange from global range and given rank geometry. + @param global Global range. + @param geom Rank geometry. + */ + template + RangePtr rrange(const Sptr& global, const Sptr& geom); } #endif diff --git a/src/opt/mpi/lib/rrange.cc b/src/opt/mpi/lib/rrange.cc deleted file mode 100644 index 3dd7228..0000000 --- a/src/opt/mpi/lib/rrange.cc +++ /dev/null @@ -1,137 +0,0 @@ -// -*- C++ -*- -/** - - @file opt/mpi/lib/rrange.cc - @brief RRange and RIndex implementations - - Copyright (c) 2024 Christian Zimmermann. All rights reserved. - Mail: chizeta@f3l.de - -**/ - -#include "rrange.h" - -namespace CNORXZ -{ - - /*==============+ - | RIndex | - +==============*/ - - /*=====================+ - | RRangeFactory | - +=====================*/ - - RRangeFactory::RRangeFactory(const Vector& geom) : - mRA( CRangeFactory(geom.size()), geom ) - { - MPI_Comm_rank(MPI_COMM_WORLD, &mMyRank); - int s = 0; - MPI_Comm_size(MPI_COMM_WORLD, &s); - CXZ_ASSERT(static_cast(mRA.size()) == s, "geometry (size = " << mRA.size() - << " ) is inconsistent with number of mpi ranks ( = " << s << ")"); - } - - void RRangeFactory::make() - { - Vector k(mRA.size()); - std::transform(mRA.begin(), mRA.end(), k.begin(), - [&](const RangePtr& r) { return r->id(); } ); - mProd = this->fromCreated(typeid(RRange), k); - if(mProd == nullptr){ - mProd = std::shared_ptr - ( new RRange( std::move(mRA) ) ); - this->addToCreated(typeid(RRange), k, mProd); - } - } - - - /*==============+ - | RRange | - +==============*/ - - RangePtr RRange::sub(SizeT i) const - { - return mRA.data()[i]; - } - - MArray RRange::sub() const - { - return mRA; - } - - SizeT RRange::size() const - { - CIndex i(mRA.range()); - SizeT o = 1; - i->ifor( mRA(i), [&o](const RangePtr& r) { o *= r->size(); } )(); - return o; - } - - SizeT RRange::dim() const - { - return mRA.range().size(); - } - - String RRange::stringMeta(SizeT pos) const - { - const String blim = "["; - const String elim = "]"; - const String dlim = ","; - String out = blim; - CIndex i(mRA.range()); - SizeT block = mRA.size(); - i->ifor( mRA(i), [&pos,&out,&block](const RangePtr& r) { - const SizeT cursize = r->size(); - block /= cursize; - const SizeT curpos = pos / block; - pos -= curpos * block; - out = out + r->stringMeta(curpos); - if(block == 1){ - assert(pos == 0); - out = out + elim; - break; - } - out = out + dlim; - } )(); - return out; - } - - const TypeInfo& RRange::type() const - { - return typeid(RRange); - } - - const TypeInfo& RRange::metaType() const - { - return typeid(Vector); - } - - RangePtr RRange::extend(const RangePtr& r) const - { - // number of ranks is fixed!!! - return nullptr; - } - - /*========================+ - | RRange (private) | - +========================*/ - - RRange::RRange(const MArray& ra) : - mRA( ra ) - { - int s = 0; - MPI_Comm_size(MPI_COMM_WORLD, &s); - CXZ_ASSERT(static_cast(mRA.size()) == s, "geometry (size = " << mRA.size() - << " ) is inconsistent with number of mpi ranks ( = " << s << ")"); - } - - Vector RRange::key() const - { - Vector k(mRA.size()); - std::transform(mRA.begin(), mRA.end(), k.begin(), - [&](const RangePtr& r) { return r->id(); } ); - return k; - } - -}