WIP: RIndex + ROp types
This commit is contained in:
parent
194cc4ebad
commit
94367c60ac
3 changed files with 355 additions and 3 deletions
73
src/opt/mpi/include/rop_types.h
Normal file
73
src/opt/mpi/include/rop_types.h
Normal file
|
@ -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 <typename T, class IndexT>
|
||||
class CROpRoot : public COpInterface<CROpRoot<T,IndexT>>
|
||||
{
|
||||
public:
|
||||
typedef COpInterface<CROpRoot<T,IndexT>> OI;
|
||||
|
||||
constexpr CROpRoot() = default;
|
||||
|
||||
template <class PosT>
|
||||
constexpr decltype(auto) operator()(const PosT& pos) const;
|
||||
|
||||
constexpr decltype(auto) operator()() const;
|
||||
|
||||
template <SizeT I>
|
||||
constexpr decltype(auto) rootSteps(const IndexId<I>& id) const;
|
||||
};
|
||||
|
||||
template <typename T, class IndexT>
|
||||
class ROpRoot : public OpInterface<ROpRoot<T,IndexT>>
|
||||
{
|
||||
public:
|
||||
typedef OpInterface<ROpRoot<T,IndexT>> OI;
|
||||
|
||||
constexpr ROpRoot() = default;
|
||||
|
||||
template <class PosT>
|
||||
constexpr decltype(auto) operator()(const PosT& pos) const;
|
||||
|
||||
constexpr decltype(auto) operator()() const;
|
||||
|
||||
template <SizeT I>
|
||||
constexpr decltype(auto) rootSteps(const IndexId<I>& id) const;
|
||||
};
|
||||
|
||||
template <class CXpr>
|
||||
class RContraction : public OpInterfacte<RContraction<CXpr>>
|
||||
{
|
||||
public:
|
||||
typedef OpInterfacte<RContraction<CXpr>> OI;
|
||||
|
||||
constexpr RContraction() = default;
|
||||
constexpr RContraction(CXpr&& cxpr);
|
||||
|
||||
template <class PosT>
|
||||
constexpr decltype(auto) operator()(const PosT& pos) const;
|
||||
|
||||
constexpr decltype(auto) operator()() const;
|
||||
|
||||
template <SizeT I>
|
||||
constexpr decltype(auto) rootSteps(const IndexId<I>& id) const;
|
||||
|
||||
private:
|
||||
CXpr mCXpr;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
|
@ -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<SizeT> 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<DType> operator*() const;
|
||||
|
||||
/** @copydoc IndexInterface::dim() */
|
||||
SizeT dim() const;
|
||||
|
||||
/** @copydoc IndexInterface::range() */
|
||||
Sptr<YRange> range() const;
|
||||
|
||||
/** @copydoc IndexInterface::stepSize() */
|
||||
UPos stepSize(const IndexId<0> id) const;
|
||||
|
||||
/** @copydoc IndexInterface::stringMeta() */
|
||||
String stringMeta() const;
|
||||
|
||||
/** @copydoc IndexInterface::meta() */
|
||||
Vector<DType> meta() const;
|
||||
|
||||
/** @copydoc IndexInterface::at() */
|
||||
RIndex& at(const Vector<DType>& meta);
|
||||
|
||||
/** @copydoc IndexInterface::prange() */
|
||||
RangePtr prange(const RIndex& last) const;
|
||||
|
||||
/** @copydoc IndexInterface::deepFormat() */
|
||||
Vector<SizeT> deepFormat() const;
|
||||
|
||||
/** @copydoc IndexInterface::deepMax() */
|
||||
Vector<SizeT> 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;
|
||||
|
||||
/** @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);
|
||||
|
||||
/** 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!!!
|
||||
|
@ -62,7 +201,7 @@ namespace CNORXZ
|
|||
RRangeFactory() = default;
|
||||
virtual void make() override final;
|
||||
|
||||
Vector<SizeT> mGeom;
|
||||
MArray<RangePtr> 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<RangePtr>& rvec);
|
||||
RRange(const MArray<RangePtr>& rvec);
|
||||
|
||||
Vector<RangePtr> mRVec;
|
||||
MArray<RangePtr> mRA;
|
||||
int mMyRank = 0;
|
||||
|
||||
virtual Vector<Uuid> key() const override final;
|
||||
};
|
||||
|
|
137
src/opt/mpi/lib/rrange.cc
Normal file
137
src/opt/mpi/lib/rrange.cc
Normal file
|
@ -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<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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue