diff --git a/src/opt/mpi/include/rop_types.h b/src/opt/mpi/include/rop_types.h new file mode 100644 index 0000000..50cb6e5 --- /dev/null +++ b/src/opt/mpi/include/rop_types.h @@ -0,0 +1,73 @@ +// -*- C++ -*- +/** + + @file opt/include/rop_types.h + @brief Ranked Operation types declarations. + + Copyright (c) 2024 Christian Zimmermann. All rights reserved. + Mail: chizeta@f3l.de + +**/ + +#ifndef __cxz_mpi_rop_types_h__ +#define __cxz_mpi_rop_types_h__ + +namespace CNORXZ +{ + template + class CROpRoot : public COpInterface> + { + public: + typedef COpInterface> OI; + + constexpr CROpRoot() = default; + + template + constexpr decltype(auto) operator()(const PosT& pos) const; + + constexpr decltype(auto) operator()() const; + + template + constexpr decltype(auto) rootSteps(const IndexId& id) const; + }; + + template + class ROpRoot : public OpInterface> + { + public: + typedef OpInterface> OI; + + constexpr ROpRoot() = default; + + template + constexpr decltype(auto) operator()(const PosT& pos) const; + + constexpr decltype(auto) operator()() const; + + template + constexpr decltype(auto) rootSteps(const IndexId& id) const; + }; + + template + class RContraction : public OpInterfacte> + { + public: + typedef OpInterfacte> OI; + + constexpr RContraction() = default; + constexpr RContraction(CXpr&& cxpr); + + template + constexpr decltype(auto) operator()(const PosT& pos) const; + + constexpr decltype(auto) operator()() const; + + template + constexpr decltype(auto) rootSteps(const IndexId& id) const; + + private: + CXpr mCXpr; + }; +} + +#endif diff --git a/src/opt/mpi/include/rrange.h b/src/opt/mpi/include/rrange.h index 4b4fb1d..a98bb83 100644 --- a/src/opt/mpi/include/rrange.h +++ b/src/opt/mpi/include/rrange.h @@ -12,6 +12,8 @@ #ifndef __cxz_mpi_rrange_h__ #define __cxz_mpi_rrange_h__ +#include "cnorxz.h" + namespace CNORXZ { /** **** @@ -24,6 +26,143 @@ namespace CNORXZ typedef RRange RangeType; typedef Vector MetaType; + 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); + + /** @copydoc IndexInterface::operator=(SizeT) */ + RIndex& operator=(SizeT lexpos); + + /** @copydoc IndexInterface::operator++() */ + RIndex& operator++(); + + /** @copydoc IndexInterface::operator--() */ + RIndex& operator--(); + + /** @copydoc IndexInterface::operator+() */ + RIndex operator+(Int n) const; + + /** @copydoc IndexInterface::operator-() */ + RIndex operator-(Int n) const; + + /** @copydoc IndexInterface::operator-(CIndex) */ + SizeT operator-(const RIndex& i) const; + + /** @copydoc IndexInterface::operator+=() */ + RIndex& operator+=(Int n); + + /** @copydoc IndexInterface::operator-=() */ + RIndex& operator-=(Int n); + + /** @copydoc IndexInterface::lex() */ + SizeT lex() const; + + /** @copydoc IndexInterface::pmax() */ + UPos pmax() const; + + /** @copydoc IndexInterface::lmax() */ + UPos lmax() const; + + /** @copydoc IndexInterface::id() */ + IndexId<0> id() const; + + /** @copydoc IndexInterface::operator*() */ + Vector operator*() const; + + /** @copydoc IndexInterface::dim() */ + SizeT dim() const; + + /** @copydoc IndexInterface::range() */ + Sptr range() const; + + /** @copydoc IndexInterface::stepSize() */ + UPos stepSize(const IndexId<0> id) const; + + /** @copydoc IndexInterface::stringMeta() */ + String stringMeta() const; + + /** @copydoc IndexInterface::meta() */ + Vector meta() const; + + /** @copydoc IndexInterface::at() */ + RIndex& at(const Vector& meta); + + /** @copydoc IndexInterface::prange() */ + RangePtr prange(const RIndex& last) const; + + /** @copydoc IndexInterface::deepFormat() */ + Vector deepFormat() const; + + /** @copydoc IndexInterface::deepMax() */ + Vector deepMax() const; + + /** @copydoc IndexInterface::reformat() */ + RIndex& reformat(const Vector& f, const Vector& s); + + /** @copydoc IndexInterface::ifor() */ + DXpr ifor(const DXpr& xpr, NoF&& 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); + + /** 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!!! @@ -62,7 +201,7 @@ namespace CNORXZ RRangeFactory() = default; virtual void make() override final; - Vector mGeom; + MArray mRA; }; /** **** @@ -85,13 +224,16 @@ namespace CNORXZ virtual const TypeInfo& metaType() const override final; virtual RangePtr extend(const RangePtr& r) const override final; + int myrank() const; + private: RRange() = default; RRange(const RRange& a) = delete; - RRange(const Vector& rvec); + RRange(const MArray& rvec); - Vector mRVec; + MArray mRA; + int mMyRank = 0; virtual Vector key() const override final; }; diff --git a/src/opt/mpi/lib/rrange.cc b/src/opt/mpi/lib/rrange.cc new file mode 100644 index 0000000..3dd7228 --- /dev/null +++ b/src/opt/mpi/lib/rrange.cc @@ -0,0 +1,137 @@ +// -*- 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; + } + +}