WIP: mpi related ranges

This commit is contained in:
Christian Zimmermann 2024-03-10 23:56:33 +01:00
parent 94367c60ac
commit 5db90864c7
6 changed files with 184 additions and 272 deletions

View file

@ -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;

View file

@ -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 <typename MetaT>

View file

@ -0,0 +1,87 @@
#include "rop_types.h"
namespace CNORXZ
{
/*================+
| CROpRoot |
+================*/
template <typename T, class IndexT>
template <class PosT>
constexpr decltype(auto) CROpRoot<T,IndexT>::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 <typename T, class IndexT>
constexpr decltype(auto) CROpRoot<T,IndexT>::operator()() const
{
}
template <typename T, class IndexT>
template <SizeT I>
constexpr decltype(auto) CROpRoot<T,IndexT>::rootSteps(const IndexId<I>& id) const
{
}
/*===============+
| ROpRoot |
+===============*/
template <typename T, class IndexT>
template <class PosT>
constexpr decltype(auto) ROpRoot<T,IndexT>::operator()(const PosT& pos) const
{
}
template <typename T, class IndexT>
constexpr decltype(auto) ROpRoot<T,IndexT>::operator()() const
{
}
template <typename T, class IndexT>
template <SizeT I>
constexpr decltype(auto) ROpRoot<T,IndexT>::rootSteps(const IndexId<I>& id) const
{
}
/*====================+
| RContraction |
+====================*/
template <class CXpr>
constexpr RContraction<CXpr>::RContraction(CXpr&& cxpr) :
mCXpr(std::forward<CXpr>(cxpr))
{}
template <class CXpr>
template <class PosT>
constexpr decltype(auto) RContraction<CXpr>::operator()(const PosT& pos) const
{
}
template <class CXpr>
constexpr decltype(auto) RContraction<CXpr>::operator()() const
{
}
template <class CXpr>
template <SizeT I>
constexpr decltype(auto) RContraction<CXpr>::rootSteps(const IndexId<I>& id) const
{
}
}

View file

@ -29,6 +29,10 @@ namespace CNORXZ
template <SizeT I>
constexpr decltype(auto) rootSteps(const IndexId<I>& id) const;
private:
const T* mData;
COpRoot
};
template <typename T, class IndexT>
@ -46,6 +50,9 @@ namespace CNORXZ
template <SizeT I>
constexpr decltype(auto) rootSteps(const IndexId<I>& id) const;
private:
};
template <class CXpr>

View file

@ -1,8 +1,8 @@
// -*- 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<RIndex<Vector<SizeT>>>
template <class Index>
class RIndex : public IndexInterface<RIndex<Index>,typename Index::MetaType>
{
public:
typedef IndexInterface<RIndex<Vector<SizeT>>> IB;
typedef RRange RangeType;
typedef Vector<SizeT> MetaType;
typedef IndexInterface<RIndex<Index>,typename Index::MetaType> IB;
typedef typename Index::MetaType MetaType;
typedef RRange<typename Index::RangeType> 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<DType> operator*() const;
MetaType operator*() const;
/** @copydoc IndexInterface::dim() */
SizeT dim() const;
constexpr SizeT dim() const;
/** @copydoc IndexInterface::range() */
Sptr<YRange> range() const;
Sptr<RangeType> range() const;
/** @copydoc IndexInterface::stepSize() */
UPos stepSize(const IndexId<0> id) const;
template <SizeT I>
decltype(auto) stepSize(const IndexId<I>& id) const;
/** @copydoc IndexInterface::stringMeta() */
String stringMeta() const;
/** @copydoc IndexInterface::meta() */
Vector<DType> meta() const;
MetaType meta() const;
/** @copydoc IndexInterface::at() */
RIndex& at(const Vector<DType>& meta);
RIndex& at(const MetaType& metaPos);
/** @copydoc IndexInterface::prange() */
RangePtr prange(const RIndex& last) const;
RangePtr prange(const RIndex<Index>& last) const;
/** @copydoc IndexInterface::deepFormat() */
Vector<SizeT> deepFormat() const;
auto deepFormat() const;
/** @copydoc IndexInterface::deepMax() */
Vector<SizeT> deepMax() const;
auto deepMax() const;
/** @copydoc IndexInterface::reformat() */
RIndex& reformat(const Vector<SizeT>& f, const Vector<SizeT>& s);
/** @copydoc IndexInterface::ifor() */
DXpr<None> ifor(const DXpr<None>& xpr, NoF&& f) const;
template <class Xpr, class F>
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<RIndex>& i);
/** @copydoc IndexInterface::xpr() */
decltype(auto) xpr(const Sptr<MIndex<Indices...>>& _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<RRange> mRange;
Vector<Sptr<CIndex>> 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<RIndex>
{ static constexpr bool value = true; };
/** ****
Specialization: RIndex has sub-indices.
@see has_sub
*/
template <>
struct has_sub<RIndex>
{ 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<SizeT>& geom);
private:
RRangeFactory() = default;
virtual void make() override final;
MArray<RangePtr> mRA;
Sptr<RangeType> mRange;
Sptr<Index> 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<RRange>
template <class Range>
class RRange : public RangeInterface<RRange<Range>>
{
public:
typedef RangeBase RB;
typedef RIndex IndexType;
typedef RIndex<typename Range::IndexType> IndexType;
typedef typename Range::MetaType MetaType;
friend RRangeFactory;
friend RRangeFactory<Range>;
virtual RangePtr sub(SizeT i) const override final;
virtual RangePtr sub(SizeT num) const override final;
virtual MArray<RangePtr> 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<Range> 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<RangePtr>& rvec);
MArray<RangePtr> mRA;
int mMyRank = 0;
RRange(const RRange& in) = delete;
RRange& operator=(const RRange& in) = delete;
/** Construct from local range and geometry.
@param loc Local range.
@param geom Rank geometry range.
*/
RRange(const Sptr<Range>& loc, const Sptr<YRange>& geom);
Sptr<Range> mLocal; /**< Local range of THIS rank. */
Sptr<YRange> mGeom; /**< Rank geometry range. */
virtual Vector<Uuid> key() const override final;
};
/** Create RRange from global range and given rank geometry.
@param global Global range.
@param geom Rank geometry.
*/
template <class GRange>
RangePtr rrange(const Sptr<GRange>& global, const Sptr<YRange>& geom);
}
#endif

View file

@ -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<SizeT>& 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<int>(mRA.size()) == s, "geometry (size = " << mRA.size()
<< " ) is inconsistent with number of mpi ranks ( = " << s << ")");
}
void RRangeFactory::make()
{
Vector<Uuid> 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<RRange>
( new RRange( std::move(mRA) ) );
this->addToCreated(typeid(RRange), k, mProd);
}
}
/*==============+
| RRange |
+==============*/
RangePtr RRange::sub(SizeT i) const
{
return mRA.data()[i];
}
MArray<RangePtr> 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<SizeT>);
}
RangePtr RRange::extend(const RangePtr& r) const
{
// number of ranks is fixed!!!
return nullptr;
}
/*========================+
| RRange (private) |
+========================*/
RRange::RRange(const MArray<RangePtr>& ra) :
mRA( ra )
{
int s = 0;
MPI_Comm_size(MPI_COMM_WORLD, &s);
CXZ_ASSERT(static_cast<int>(mRA.size()) == s, "geometry (size = " << mRA.size()
<< " ) is inconsistent with number of mpi ranks ( = " << s << ")");
}
Vector<Uuid> RRange::key() const
{
Vector<Uuid> k(mRA.size());
std::transform(mRA.begin(), mRA.end(), k.begin(),
[&](const RangePtr& r) { return r->id(); } );
return k;
}
}