more on operations

This commit is contained in:
Christian Zimmermann 2022-10-21 02:24:30 +02:00
parent aafc6f0e2a
commit 98f71b874b
2 changed files with 130 additions and 510 deletions

View file

@ -46,390 +46,124 @@ namespace CNORXZ
} }
/**************************** /***************
* ConstOperationRoot * * COpRoot *
****************************/ ***************/
template <typename T, class... Ranges> template <typename T, class IndexT>
ConstOperationRoot<T,Ranges...>:: constexpr COpRoot<T,IndexT>::COpRoot(const DArrayBase<T>& a, const Sptr<IndexT>& ind) :
ConstOperationRoot(const ArrayBase<T,Ranges...>& ma, mData(a.data()),
const std::shared_ptr<typename Ranges::IndexType>&... indices) :
mDataPtr(ma.data()),
mOrigDataPtr(ma.data()),
mIndex( ma.begin() )
{
mIndex(indices...);
mDataPtr = mOrigDataPtr + mIndex.pos();
}
template <typename T, class... Ranges>
ConstOperationRoot<T,Ranges...>::
ConstOperationRoot(std::shared_ptr<ArrayBase<T,Ranges...> > maptr,
const std::shared_ptr<typename Ranges::IndexType>&... indices) :
mDataPtr(maptr->data()),
mOrigDataPtr(maptr->data()),
mIndex(maptr->begin()),
mMaPtr(maptr)
{
mIndex(indices...);
mDataPtr = mOrigDataPtr + mIndex.pos();
}
template <typename T, class... Ranges>
ConstOperationRoot<T,Ranges...>::
ConstOperationRoot(const T* data, const IndexType& ind) :
mDataPtr(data),
mOrigDataPtr(data),
mIndex(ind) mIndex(ind)
{ {}
mDataPtr = mOrigDataPtr + mIndex.pos();
}
template <typename T, class... Ranges> template <typename T, class IndexT>
template <class ET> constexpr COpRoot<T,IndexT>::COpRoot(const T* data, const Sptr<IndexT>& ind) :
inline const T& ConstOperationRoot<T,Ranges...>::get(ET pos) const mData(data),
{
return mDataPtr[pos.val()];
}
template <typename T, class... Ranges>
template <typename V, class ET>
inline const V& ConstOperationRoot<T,Ranges...>::vget(ET pos) const
{
return *(reinterpret_cast<const V*>(mDataPtr+pos.val()));
}
template <typename T, class... Ranges>
template <class ET>
inline ConstOperationRoot<T,Ranges...>& ConstOperationRoot<T,Ranges...>::set(ET pos)
{
mDataPtr = mOrigDataPtr + pos.val();
return *this;
}
template <typename T, class... Ranges>
const T* ConstOperationRoot<T,Ranges...>::data() const
{
return mDataPtr;
}
template <typename T, class... Ranges>
MExt<None> ConstOperationRoot<T,Ranges...>::rootSteps(std::intptr_t iPtrNum) const
{
return MExt<None>(RangeHelper::getStepSize( mIndex, iPtrNum ));
}
template <typename T, class... Ranges>
template <class Expr>
Expr ConstOperationRoot<T,Ranges...>::loop(Expr exp) const
{
return exp;
}
/********************
* StaticCast *
********************/
template <typename T, class Op>
StaticCast<T,Op>::StaticCast(const Op& op) : mOp(op) {}
template <typename T, class Op>
template <class ET>
inline T StaticCast<T,Op>::get(ET pos) const
{
return static_cast<T>( mOp.get(pos) );
}
template <typename T, class Op>
template <typename V, class ET>
inline V StaticCast<T,Op>::vget(ET pos) const
{
assert(0); // !!!
return V();
}
template <typename T, class Op>
template <class ET>
inline StaticCast<T,Op>& StaticCast<T,Op>::set(ET pos)
{
mOp.set(pos);
return *this;
}
template <typename T, class Op>
auto StaticCast<T,Op>::rootSteps(std::intptr_t iPtrNum) const
-> decltype(mOp.rootSteps(iPtrNum))
{
return mOp.rootSteps(iPtrNum);
}
template <typename T, class Op>
template <class Expr>
Expr StaticCast<T,Op>::loop(Expr exp) const
{
return mOp.loop(exp);
}
/****************************
* MetaOperationRoot *
****************************/
template <class Range>
MetaOperationRoot<Range>::
MetaOperationRoot(const std::shared_ptr<IndexType>& ind) :
mWorkIndex(*ind), mIndex( ind ) {}
template <class Range>
template <class ET>
inline typename MetaOperationRoot<Range>::value_type
MetaOperationRoot<Range>::get(ET pos) const
{
return (mWorkIndex = pos.val()).meta();
}
template <class Range>
template <typename V, class ET>
inline V
MetaOperationRoot<Range>::vget(ET pos) const
{
assert(0); // !!!
return V();
}
template <class Range>
template <class ET>
inline MetaOperationRoot<Range>& MetaOperationRoot<Range>::set(ET pos)
{
return *this;
}
template <class Range>
MExt<None> MetaOperationRoot<Range>::rootSteps(std::intptr_t iPtrNum) const
{
return MExt<None>(RangeHelper::getStepSize( *mIndex, iPtrNum ));
}
template <class Range>
template <class Expr>
Expr MetaOperationRoot<Range>::loop(Expr exp) const
{
return exp;
}
/***********************
* OperationRoot *
***********************/
template <typename T, class... Ranges>
OperationRoot<T,Ranges...>::
OperationRoot(MutableArrayBase<T,Ranges...>& ma,
const std::shared_ptr<typename Ranges::IndexType>&... indices) :
mDataPtr(ma.data()),
mOrigDataPtr(ma.data()),
mDataAcc( ma.data(), ma.data() ),
mIndex( ma.begin() )
{
mIndex(indices...);
mDataAcc.set( mIndex.pos() );
mDataPtr = mOrigDataPtr + mIndex.pos();
}
template <typename T, class... Ranges>
OperationRoot<T,Ranges...>::
OperationRoot(MutableArrayBase<T,Ranges...>& ma,
const std::tuple<std::shared_ptr<typename Ranges::IndexType>...>& indices) :
mDataPtr(ma.data()),
mOrigDataPtr(ma.data()),
mDataAcc( ma.data(), ma.data() ),
mIndex( ma.begin() )
{
mIndex(indices);
mDataAcc.set( mIndex.pos() );
mDataPtr = mOrigDataPtr + mIndex.pos();
}
template <typename T, class... Ranges>
OperationRoot<T,Ranges...>::
OperationRoot(T* data, const IndexType& ind) :
mDataPtr(data),
mOrigDataPtr(data),
mDataAcc( data, data ),
mIndex(ind) mIndex(ind)
{ {}
mDataAcc.set( mIndex.pos() );
mDataPtr = mOrigDataPtr + mIndex.pos();
}
template <typename T, class... Ranges> template <typename T, class IndexT>
template <class Func, class Access, class OpClass> template <class PosT>
auto OperationRoot<T,Ranges...>::asx(const OpClass& in) const constexpr decltype(auto) COpRoot<T,IndexT>::get(const PosT& pos) const
{ {
static_assert(Access::VSIZE > 0, "Access::VSIZE failed"); if constexpr(is_epos_type<PosT>::value){
return mIndex.ifor(1,this->template asxExpr<Func,Access>(in)).template vec<Access::VSIZE>(); return vreg(mData,pos); // distinguish between consecutive/non-consecutive
}
template <typename T, class... Ranges>
template <class Func, class Access, class OpClass>
auto OperationRoot<T,Ranges...>::asxExpr(const OpClass& in) const
{
static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" );
return in.loop(AssignmentExpr<T,Func,Access,OperationRoot<T,Ranges...>,OpClass>
(Access(mDataAcc),*this,in));
}
template <typename T, class... Ranges>
template <class Func, class Access, class OpClass, class Index>
auto OperationRoot<T,Ranges...>::asx(const OpClass& in, const std::shared_ptr<Index>& i) const
{
static_assert(Access::VSIZE > 0, "Access::VSIZE failed");
return i->ifor(1,this->template asxExpr<Func,Access>(in)).template vec<Access::VSIZE>();
}
template <typename T, class... Ranges>
template <class OpClass>
auto OperationRoot<T,Ranges...>::assign(const OpClass& in) const
{
return this->template asx<identity<T>,PointerAccess<T>>(in);
}
template <typename T, class... Ranges>
template <class OpClass>
auto OperationRoot<T,Ranges...>::assignExpr(const OpClass& in) const
{
return this->template asxExpr<identity<T>,PointerAccess<T>>(in);
}
template <typename T, class... Ranges>
template <class OpClass, class Index>
auto OperationRoot<T,Ranges...>::assign(const OpClass& in, const std::shared_ptr<Index>& i) const
{
return this->template asx<identity<T>,PointerAccess<T>>(in,i);
}
template <typename T>
using xplus = plus<T>;
template <typename T, class... Ranges>
template <class OpClass>
auto OperationRoot<T,Ranges...>::plus(const OpClass& in) const
{
return this->template asx<xplus<T>,PointerAccess<T>>(in);
}
template <typename T, class... Ranges>
template <class OpClass, class Index>
auto OperationRoot<T,Ranges...>::plus(const OpClass& in, const std::shared_ptr<Index>& i) const
{
return this->template asx<xplus<T>,PointerAccess<T>>(in,i);
}
template <bool VABLE, class Func, typename TarOp, class OpClass>
inline void vexec(TarOp& th, const OpClass& in)
{
// VECTORIZATION!!!
// PARALLEL!!!
if constexpr(VABLE){
CHECK;
typedef typename TarOp::value_type T;
auto x = th.template asx<VFunc<Func>,VPointerAccess<T>>(in);
if(x.rootSteps(x.vI()) == 1){
//if(0){
CHECK;
x();
} }
else { else {
th.template asx<Func,PointerAccess<T>>(in)(); return mData[pos.val()];
} }
} }
template <typename T, class IndexT>
template <SizeT I>
constexpr decltype(auto) COpRoot<T,IndexT>::rootSteps(const IndexId<I>& id) const
{
return mIndex->stepSize(id);
}
template <typename T, class IndexT>
template <class Expr>
constexpr decltype(auto) COpRoot<T,IndexT>::loop(Xpr&& xpr) const
{
return xpr;
}
template <typename T, class IndexT>
const T* COpRoot<T,IndexT>::data() const
{
return mData;
}
/****************
* OpRoot *
****************/
template <typename T, class IndexT>
constexpr OpRoot<T,IndexT>::OpRoot(MDArrayBase<T>& a, const Sptr<IndexT>& ind);
mData(a.data()),
mIndex(ind)
{}
template <typename T, class IndexT>
constexpr OpRoot<T,IndexT>::OpRoot(T* data, const Sptr<IndexT>& ind) :
mData(data),
mIndex(ind)
{}
template <typename T, class IndexT>
template <class Op>
constexpr OpRoot<T,IndexT>& OpRoot<T,IndexT>::operator=(const Op& o)
{
a(mInd, [](auto& a, const auto& b) { a = b; }, o)
return *this;
}
template <typename T, class IndexT>
template <class Op>
constexpr OpRoot<T,IndexT>& OpRoot<T,IndexT>::operator+=(const Op& o)
{
a(mInd, [](auto& a, const auto& b) { a += b; }, o)
return *this;
}
template <typename T, class IndexT>
constexpr OpRoot<T,IndexT>& OpRoot<T,IndexT>::operator=(const OpRoot<T,IndexT>& in)
{
a(mInd, [](auto& a, const auto& b) { a = b; }, o)
return *this;
}
template <typename T, class IndexT>
template <class PosT>
constexpr decltype(auto) OpRoot<T,IndexT>::get(const PosT& pos) const
{
if constexpr(is_epos_type<PosT>::value){
return vreg(mData,pos); // distinguish between consecutive/non-consecutive
}
else { else {
typedef typename TarOp::value_type T; return mData[pos.val()];
//IAccess<T,F<T>> tmp;
th.template asx<Func,PointerAccess<T>>(in)();
} }
} }
template <typename T, class... Ranges> template <typename T, class IndexT>
template <class OpClass> template <SizeT I>
OperationRoot<T,Ranges...>& OperationRoot<T,Ranges...>::operator=(const OpClass& in) constexpr decltype(auto) OpRoot<T,IndexT>::rootSteps(const IndexId<I>& id) const
{ {
vexec<OpClass::VABLE,identity<T>>(*this,in); return mIndex->stepSize(id);
return *this;
} }
template <typename T, class... Ranges> template <typename T, class IndexT>
template <class OpClass>
OperationRoot<T,Ranges...>& OperationRoot<T,Ranges...>::operator+=(const OpClass& in)
{
vexec<OpClass::VABLE,xplus<T>>(*this,in);
return *this;
}
template <typename T, class... Ranges>
OperationRoot<T,Ranges...>& OperationRoot<T,Ranges...>::operator=(const OperationRoot<T,Ranges...>& in)
{
return operator=<OperationRoot<T,Ranges...> >(in);
}
/*
template <typename T, class... Ranges>
ParallelOperationRoot<T,Ranges...> OperationRoot<T,Ranges...>::par()
{
return ParallelOperationRoot<T,Ranges...>(mOrigDataPtr, mIndex);
}
*/
template <typename T, class... Ranges>
template <class ET>
inline T& OperationRoot<T,Ranges...>::get(ET pos) const
{
return *mDataAcc.get(pos.val());
}
template <typename T, class... Ranges>
template <typename V, class ET>
inline V& OperationRoot<T,Ranges...>::vget(ET pos) const
{
return *(reinterpret_cast<V*>(mDataAcc.get(pos.val())));
}
template <typename T, class... Ranges>
template <class ET>
inline OperationRoot<T,Ranges...>& OperationRoot<T,Ranges...>::set(ET pos)
{
mDataAcc.set(pos.val());
mDataPtr = mOrigDataPtr + pos.val();
return *this;
}
template <typename T, class... Ranges>
MExt<None> OperationRoot<T,Ranges...>::rootSteps(std::intptr_t iPtrNum) const
{
return MExt<None>(RangeHelper::getStepSize( mIndex, iPtrNum ));
}
template <typename T, class... Ranges>
template <class Expr> template <class Expr>
Expr OperationRoot<T,Ranges...>::loop(Expr exp) const constexpr decltype(auto) OpRoot<T,IndexT>::loop(Xpr&& xpr) const;
{ {
return exp; return xpr;
} }
template <typename T, class... Ranges> template <typename T, class IndexT>
T* OperationRoot<T,Ranges...>::data() const T* OpRoot<T,IndexT>::data() const
{ {
return mDataAcc.get(0); return mData;
}
template <typename T, class... Ranges>
template <class... Indices>
auto OperationRoot<T,Ranges...>::sl(const std::shared_ptr<Indices>&... inds)
-> Slice<T,typename Indices::RangeType...>
{
Slice<T,typename Indices::RangeType...> out(inds->range()...);
out.define(inds...) = *this;
return out;
} }

View file

@ -68,7 +68,7 @@ namespace CNORXZ
{ static constexpr bool value = std::is_base_of<OpInterface,T>::value }; { static constexpr bool value = std::is_base_of<OpInterface,T>::value };
template <typename T, class IndexT> template <typename T, class IndexT>
class COpRoot : public COpInterface<T,COpRoot<T,IndexT>> class COpRoot : public COpInterface<COpRoot<T,IndexT>>
{ {
public: public:
@ -85,193 +85,82 @@ namespace CNORXZ
constexpr decltype(auto) rootSteps(const IndexId<I>& id) const; constexpr decltype(auto) rootSteps(const IndexId<I>& id) const;
template <class Xpr> template <class Xpr>
constexpr decltype(auto) loop(Xpr&& exp) const; constexpr decltype(auto) loop(Xpr&& xpr) const;
const T* data() const; const T* data() const;
private: private:
const T* mData; const T* mData = nullptr;
Sptr<IndexType> mIndex; Sptr<IndexType> mIndex;
}; };
template <typename T, class IndexT>
template <class Range> class OpCont : public OpInterface<OpCont<T,IndexT>>
class MetaOperationRoot : public OperationTemplate<typename Range::MetaType,
MetaOperationRoot<Range> >
{ {
public: // operation container (intermediate operation results)
// TO BE IMPLEMENTED!!!
typedef typename Range::IndexType IndexType;
typedef typename IndexType::MetaType value_type;
typedef OperationBase<value_type,MetaOperationRoot<Range> > OT;
static constexpr size_t SIZE = 1;
static constexpr bool CONT = false;
static constexpr bool VABLE = false;
MetaOperationRoot(const std::shared_ptr<IndexType>& ind);
template <class ET>
inline value_type get(ET pos) const;
template <typename V, class ET>
inline V vget(ET pos) const;
template <class ET>
inline MetaOperationRoot& set(ET pos);
MExt<None> rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype
template <class Expr>
Expr loop(Expr exp) const;
private:
mutable IndexType mWorkIndex;
std::shared_ptr<IndexType> mIndex;
}; };
template <typename T, class... Ranges>
class OperationRoot : public OperationTemplate<T,OperationRoot<T,Ranges...> > template <typename T, class IndexT>
class OpRoot : public OpInterface<OpRoot<T,IndexT>>
{ {
public: public:
typedef T value_type; typedef T value_type;
typedef OperationBase<T,OperationRoot<T,Ranges...> > OT; typedef OpInterface<OpRoot<T,IndexT>> OI;
typedef ContainerRange<Ranges...> CRange;
typedef ConstContainerIndex<T,typename Ranges::IndexType...> IndexType;
static constexpr size_t SIZE = 1; constexpr OpRoot(MDArrayBase<T>& a, const Sptr<IndexT>& ind);
static constexpr bool CONT = true; constexpr OpRoot(T* data, const Sptr<IndexT>& ind);
static constexpr bool VABLE = true;
private: template <class Op>
constexpr OpRoot& operator=(const Op& in);
T* mDataPtr; template <class Op>
T* mOrigDataPtr; constexpr OpRoot& operator+=(const Op& in);
PointerAccess<T> mDataAcc;
IndexType mIndex;
public: constexpr OpRoot& operator=(const OpRoot& in);
OperationRoot(MutableArrayBase<T,Ranges...>& ma,
const std::shared_ptr<typename Ranges::IndexType>&... indices);
OperationRoot(MutableArrayBase<T,Ranges...>& ma, template <class PosT>
const std::tuple<std::shared_ptr<typename Ranges::IndexType>...>& indices); constexpr decltype(auto) get(const PosT& pos) const;
OperationRoot(T* data, const IndexType& ind); template <SizeT I>
constexpr decltype(auto) rootSteps(const IndexId<I>& id) const;
template <class Func, class Access, class OpClass> template <class Xpr>
auto asx(const OpClass& in) const; constexpr decltype(auto) loop(Xpr&& exp) const;
template <class Func, class Access, class OpClass>
auto asxExpr(const OpClass& in) const;
template <class Func, class Access, class OpClass, class Index>
auto asx(const OpClass& in, const std::shared_ptr<Index>& i) const;
template <class OpClass>
auto assign(const OpClass& in) const;
template <class OpClass>
auto assignExpr(const OpClass& in) const;
template <class OpClass, class Index>
auto assign(const OpClass& in, const std::shared_ptr<Index>& i) const;
template <class OpClass>
auto plus(const OpClass& in) const;
template <class OpClass, class Index>
auto plus(const OpClass& in, const std::shared_ptr<Index>& i) const;
template <class OpClass>
OperationRoot& operator=(const OpClass& in);
template <class OpClass>
OperationRoot& operator+=(const OpClass& in);
OperationRoot& operator=(const OperationRoot& in);
auto par() { return *this; }
template <class ET>
inline T& get(ET pos) const;
template <typename V, class ET>
inline V& vget(ET pos) const;
template <class ET>
inline OperationRoot& set(ET pos);
MExt<None> rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype
template <class Expr>
Expr loop(Expr exp) const;
T* data() const; T* data() const;
template <class... Indices> private:
auto sl(const std::shared_ptr<Indices>&... inds) T* mData = nullptr;
-> Slice<T,typename Indices::RangeType...>; Sptr<IndexT> mIndex;
}; };
template <typename T, class OpFunction, class... Ops>
class Operation : public OperationTemplate<T,Operation<T,OpFunction,Ops...> > template <class F, class... Ops>
class Operation : public OpInterface<Operation<F,Ops...>>
{ {
public: public:
typedef OpInterface<T,Operation<T,F,Ops...>> OI;
typedef T value_type; constexpr Operation(F&& f, Ops&&... ops);
typedef OperationBase<T,Operation<T,OpFunction,Ops...> > OT;
typedef OpFunction F;
static constexpr size_t SIZE = (... + Ops::SIZE); template <class PosT>
static constexpr bool FISSTATIC = OpFunction::FISSTATIC; constexpr decltype(auto) get(const PosT& pos) const;
static constexpr bool CONT = false;
static constexpr bool VABLE = template <SizeT I>
(... and (Ops::VABLE and std::is_same<T,typename Ops::value_type>::value)); constexpr decltype(auto) rootSteps(const IndexId<I>& id) const
template <class Xpr>
constexpr decltype(auto) loop(Xpr&& xpr) const;
private: private:
OperationTuple<Ops...> mOps; Tuple<Ops...> mOps;
std::shared_ptr<OpFunction> mF; // only if non-static F mF;
typedef decltype(mOps.rootSteps(0)) ExtType;
public:
Operation(const Ops&... ops);
Operation(std::shared_ptr<OpFunction> ff, const Ops&... ops);
template <class ET>
inline auto get(ET pos) const;
template <typename V, class ET>
inline auto vget(ET pos) const;
template <class ET>
inline Operation& set(ET pos);
auto rootSteps(std::intptr_t iPtrNum = 0) const
-> ExtType; // nullptr for simple usage with decltype
template <class Expr>
auto loop(Expr exp) const;
T* data() const { assert(0); return nullptr; }
}; };
template <class OpFunction, class... Ops>
auto mkOperation(const std::shared_ptr<OpFunction>& f, const Ops&... ops)
-> Operation<typename OpFunction::value_type,OpFunction,Ops...>
{
if constexpr(OpFunction::FISSTATIC){
return Operation<typename OpFunction::value_type,OpFunction,Ops...>(ops...);
}
else {
return Operation<typename OpFunction::value_type,OpFunction,Ops...>(f,ops...);
}
}
template <typename T, class Op, class IndexType> template <typename T, class Op, class IndexType>
class Contraction : public OperationTemplate<T,Contraction<T,Op,IndexType> > class Contraction : public OperationTemplate<T,Contraction<T,Op,IndexType> >
{ {
@ -316,7 +205,4 @@ namespace CNORXZ
}; };
} }
#include "type_operations.h"
#endif #endif