WIP: RArray types + RAIndex

This commit is contained in:
Christian Zimmermann 2024-03-18 00:21:26 +01:00
parent ef5b0ac456
commit 38e90bc2c6
4 changed files with 283 additions and 192 deletions

View file

@ -24,181 +24,23 @@ namespace CNORXZ
@tparam T data type. @tparam T data type.
*/ */
template <typename T> template <typename T>
class RAIndex : public IndexInterface<RAIndex,Tuple<SizeT,Vector<DType>>> class RAIndex : public RIndex<YIndex,YIndex>
{ {
public: public:
typedef IndexInterface<RAIndex,Tuple<SizeT,Vector<DType>>> IB; typedef typename RIndex<YIndex,YIndex>::IB IB;
typedef MRange<RRange,YRange> RangeType;
typedef Tuple<SizeT,Vector<DType>> MetaType;
INDEX_RANDOM_ACCESS_ITERATOR_DEFS(MetaType); DEFAULT_MEMBERS(RAIndex);
RAIndex(const T* loc, const RangePtr& range, SizeT lexpos = 0);
RAIndex(const T* loc, const RIndex<YIndex,YIndex>& i);
RAIndex(const T* loc, const RAIndex<T>& i);
/** Default constructor. */
RAIndex() = default;
/** Move constructor. */
RAIndex(RAIndex&& i) = default;
/** Move assignment. */
RAIndex& operator=(RAIndex&& i) = default;
/** Copy constructor.
No default copy: Have to copy sub-index instances
*/
RAIndex(const RAIndex& i);
/** Copy assigment.
No default copy: Have to copy sub-index instances
*/
RAIndex& operator=(const RAIndex& i);
/** Construct from sub-index pointers.
@param is Vector of XIndex pointers.
@param geom Rank geometry.
*/
RAIndex(const Vector<XIndexPtr>& is, const Vector<SizeT>& geom);
/** Construct from sub-index pointers, specify index format.
@param bs Index format (YFormat).
@param is Vector of XIndex pointers.
@param geom Rank geometry.
*/
RAIndex(const YFormat& bs, const Vector<XIndexPtr>& is, const Vector<SizeT>& geom);
/** Construct from a range and an initial lexicographic position
@param range Range to iterate over.
@param geom Rank geometry.
@param lexpos Initial lexicographic position.
*/
RAIndex(const RangePtr& range, const Vector<SizeT>& geom, SizeT lexpos = 0);
/** Construct from a range and an initial lexicographic position, specify format.
@param range Range to iterate over.
@param bs Index format.
@param geom Rank geometry.
@param lexpos Initial lexicographic position.
*/
RAIndex(const RangePtr& range, const YFormat& bs, const Vector<SizeT>& geom, SizeT lexpos = 0);
/** @copydoc IndexInterface::operator=(SizeT) */
RAIndex& operator=(SizeT lexpos);
/** @copydoc IndexInterface::operator++() */
RAIndex& operator++();
/** @copydoc IndexInterface::operator--() */
RAIndex& operator--();
/** @copydoc IndexInterface::operator+() */
RAIndex operator+(Int n) const; RAIndex operator+(Int n) const;
/** @copydoc IndexInterface::operator-() */
RAIndex operator-(Int n) const; RAIndex operator-(Int n) const;
/** @copydoc IndexInterface::operator-(CIndex) */
SizeT operator-(const RAIndex& i) const;
/** @copydoc IndexInterface::operator+=() */
RAIndex& operator+=(Int n);
/** @copydoc IndexInterface::operator-=() */
RAIndex& 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;
const T& operator*() const; const T& operator*() const;
const T& operator->() const;
const T* 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() */
RAIndex& at(const Vector<DType>& meta);
/** @copydoc IndexInterface::prange() */
RangePtr prange(const RAIndex& last) const;
/** @copydoc IndexInterface::deepFormat() */
Vector<SizeT> deepFormat() const;
/** @copydoc IndexInterface::deepMax() */
Vector<SizeT> deepMax() const;
/** @copydoc IndexInterface::reformat() */
RAIndex& 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 DPack which provides the new sub-index instance
*/
RAIndex& operator()(const DPack& i);
/** Update all linearized positions. */
RAIndex& operator()();
/** Get all sub-indices
@return Pack of sub-indices
*/
const DPack& 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.
*/
RAIndex& 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.
*/
RAIndex& setSub(SizeT ind, SizeT lex);
private: private:
Sptr<RangeType> mRange;
MIndex<RIndex,YIndex> mI;
MFormat<2> mFormat;
MFormat<2> mLexFormat;
SizeT mLex = 0;
UPos mPMax = 0;
UPos mLMax = 0;
const T* mLoc = nullptr; const T* mLoc = nullptr;
Vector<T> mBuf; // used if iterating over content on different rank Vector<T> mBuf; // used if iterating over content on different rank
}; };

View file

@ -0,0 +1,152 @@
// -*- C++ -*-
/**
@file opt/mpi/include/rarray.cc.h
@brief RArray template implementations.
Copyright (c) 2024 Christian Zimmermann. All rights reserved.
Mail: chizeta@f3l.de
**/
#ifndef __cxz_mpi_rarray_cc_h__
#define __cxz_mpi_rarray_cc_h__
namespace CNORXZ
{
namespace mpi
{
template <typename T>
RCArray<T>::RCArray(const Sptr<CArrayBase<T>> a, const RangePtr& geom) :
mA(a),
mGeom(geom)
{}
template <typename T>
template <typename I, typename M>
T RCArray<T>::operator[](const IndexInterface<I,M>& i) const
{
if constexpr(is_rank_index<I>::value){
return *(begin() + i.lex());
}
else {
return (*mA)[i];
}
}
template <typename T>
template <typename I, typename M>
T RCArray<T>::at(const IndexInterface<I,M>& i) const
{
if constexpr(is_rank_index<I>::value){
CXZ_ASSERT(i.rank() == getNumRanks(), "rank number out of scope");
// consistency checks!!!
return *(begin() + i.lex());
}
else {
return mA->at(i);
}
}
template <typename T>
template <class... Indices>
T RCArray<T>::operator[](const SPack<Indices...>& pack) const
{
}
template <typename T>
template <class... Indices>
T RCArray<T>::at(const SPack<Indices...>& pack) const
{
}
template <typename T>
T RCArray<T>::operator[](const DPack& pack) const
{
}
template <typename T>
T RCArray<T>::at(const DPack& pack) const
{
}
template <typename T>
template <typename I, typename M>
Sptr<CArrayBase<T>> RCArray<T>::sl(const IndexInterface<I,M>& begin,
const IndexInterface<I,M>& end) const
{
}
template <typename T>
template <class Index>
COpRoot<T,Index> RCArray<T>::operator()(const Sptr<Index>& i) const
{
}
template <typename T>
template <class... Indices>
inline decltype(auto) RCArray<T>::operator()(const SPack<Indices...>& pack) const
{
}
template <typename T>
inline decltype(auto) RCArray<T>::operator()(const DPack& pack) const
{
}
template <typename T>
const T* RCArray<T>::data() const
{
return mA->data();
}
template <typename T>
SizeT RCArray<T>::size() const
{
return mA->size() * mGeom->size();
}
template <typename T>
RangePtr RCArray<T>::range() const
{
return RRangeFactory(mA->range(),mGeom).create();
}
template <typename T>
const_iterator RCArray<T>::begin() const
{
}
template <typename T>
const_iterator RCArray<T>::end() const
{
}
template <typename T>
const_iterator RCArray<T>::cbegin() const
{
}
template <typename T>
const_iterator RCArray<T>::cend() const
{
}
template <typename T>
bool RCArray<T>::isView() const
{
return mA->isView();
}
template <typename T>
const CArrayBase<T>& RCArray<T>::local() const
{
return *mA;
}
} // namespace mpi
} // namespace CNORXZ
#endif

View file

@ -16,35 +16,123 @@
namespace CNORXZ namespace CNORXZ
{ {
/** **** namespace mpi
Multi-dimensional multi-rank array.
@tparam T data type.
*/
template <typename T>
class RArray : public ArrayBase<T>
{ {
public: /** ****
typedef CArrayBase<T> AB; Wrapper for cnorxz array types for handling MPI ranks.
typedef typename RAIndex const_iterator; @tparam T data type.
*/
template <typename T>
class RCArray
{
public:
typedef RAIndex const_iterator;
using CArrayBase<T>::operator[]; DEFAULT_MEMBERS(RCArray);
using ArrayBase<T>::operator[];
/** Construct from local array object.
@param a Local array.
@param geom Rank geometry.
*/
RCArray(const Sptr<CArrayBase<T>> a, const RangePtr& geom);
DEFAULT_MEMBERS(RArray); /** @copydoc CArrayBase::operator[] */
template <typename I, typename M>
T operator[](const IndexInterface<I,M>& i) const;
/** @copydoc CArrayBase::at()
Return copy instead of reference.
*/
template <typename I, typename M>
T at(const IndexInterface<I,M>& i) const;
/** @copydoc CArrayBase::operator[] */
template <class... Indices>
T operator[](const SPack<Indices...>& pack) const;
/** @copydoc CArrayBase::at() */
template <class... Indices>
T at(const SPack<Indices...>& pack) const;
/** @copydoc CArrayBase::operator[] */
T operator[](const DPack& pack) const;
/** @copydoc CArrayBase::at() */
T at(const DPack& pack) const;
/** @copydoc CArrayBase::sl() */
template <typename I, typename M>
Sptr<CArrayBase<T>> sl(const IndexInterface<I,M>& begin,
const IndexInterface<I,M>& end) const;
/** @copydoc CArrayBase::operator() */
template <class Index>
COpRoot<T,Index> operator()(const Sptr<Index>& i) const;
/** @copydoc CArrayBase::operator() */
template <class... Indices>
inline decltype(auto) operator()(const SPack<Indices...>& pack) const;
/** @copydoc CArrayBase::operator() */
inline decltype(auto) operator()(const DPack& pack) const;
/** @copydoc CArrayBase::data() */
const T* data() const;
/** @copydoc CArrayBase::size() */
SizeT size() const;
/** @copydoc CArrayBase:range() */
RangePtr range() const;
/** @copydoc CArrayBase::begin() */
const_iterator begin() const;
/** @copydoc CArrayBase::end() */
const_iterator end() const;
/** @copydoc CArrayBase::cbegin() */
const_iterator cbegin() const;
/** @copydoc CArrayBase::cend() */
const_iterator cend() const;
/** @copydoc CArrayBase::isView() */
bool isView() const;
/** Get local array object. */
const CArrayBase<T>& local() const;
virtual const T* data() const override; private:
virtual T* data() override; ObjHandle<CArrayBase<T>> mA;
virtual const_iterator cbegin() const override; RangePtr mGeom;
virtual const_iterator cend() const override; };
virtual bool isView() const override;
protected: /** ****
virtual bool formatIsTrivial() const override final; Wrapper for cnorxz array types for handling MPI ranks.
@tparam T data type.
*/
template <typename T>
class RArray : public RCArray<T>
{
public:
typedef RCArray<T> RCA;
private: using RCA::operator[];
Vector<T> mCont; using RCA::operator();
RangePtr mRRange; using RCA::at;
}; using RCA::data;
} using RCA::begin;
using RCA::end;
using RCA::cbegin;
using RCA::cend;
using RCA::sl;
DEFAULT_MEMBERS(RArray);
};
} // namespace mpi
} // namespace CNORXZ
#endif #endif

View file

@ -141,16 +141,25 @@ namespace CNORXZ
/** Get the local index on THIS rank. */ /** Get the local index on THIS rank. */
Sptr<IndexI> local() const; Sptr<IndexI> local() const;
//!!!
private: private:
Sptr<RangeType> mRange; /**< RRange. */ Sptr<RangeType> mRange; /**< RRange. */
Sptr<IndexI> mI; /**< Index on the local range of the THIS rank. */ Sptr<IndexI> mI; /**< Index on the local range of the THIS rank. */
Sptr<IndexK> mK; /**< Multi-index indicating the current rank. */ Sptr<IndexK> mK; /**< Multi-index indicating the current rank. */
//!!!
}; };
// Traits!!! // Traits!!!
template <class I>
struct is_rank_index
{
constexpr bool value = false;
};
template <class IndexI, class IndexK>
struct is_rank_index<IndexI,IndexK>
{
constexpr bool value = true;
};
/** **** /** ****
Specific factory for RRange. Specific factory for RRange.