From ec430efcc6a5e768b865a7e210c617d9f9e16804 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 14 Jan 2021 14:43:09 +0100 Subject: [PATCH 01/56] operations: vget --- src/include/multi_array_operation.cc.h | 64 +++++++++++++++++++++++++- src/include/multi_array_operation.h | 34 ++++++++++++++ src/include/pack_num.h | 21 +++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index 6096506..d1684de 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -222,7 +222,7 @@ namespace MultiArrayTools { return RootSumN::get(last,mOps); } - + template inline MOp& MOp::set(ExtType last) { @@ -390,6 +390,13 @@ namespace MultiArrayTools return mDataPtr[pos.val()/*+mOff*/]; } + template + template + inline const V& ConstOperationRoot::vget(ET pos) const + { + return *reinterpret_cast(mDataPtr+pos.val()); + } + template template inline ConstOperationRoot& ConstOperationRoot::set(ET pos) @@ -437,6 +444,14 @@ namespace MultiArrayTools return static_cast( mOp.get(pos) ); } + template + template + inline V StaticCast::vget(ET pos) const + { + assert(0); // !!! + return V(); + } + template template inline StaticCast& StaticCast::set(ET pos) @@ -481,6 +496,15 @@ namespace MultiArrayTools return (mWorkIndex = pos.val()).meta(); } + template + template + inline V + MetaOperationRoot::vget(ET pos) const + { + assert(0); // !!! + return V(); + } + template template inline MetaOperationRoot& MetaOperationRoot::set(ET pos) @@ -633,6 +657,13 @@ namespace MultiArrayTools return mDataPtr[pos.val()]; } + template + template + inline V& OperationRoot::vget(ET pos) const + { + return *reinterpret_cast(mDataPtr + pos.val()); + } + template template inline OperationRoot& OperationRoot::set(ET pos) @@ -773,6 +804,13 @@ namespace MultiArrayTools return mDataPtr[pos.val()/*+mOff*/]; } + template + template + inline V& ParallelOperationRoot::vget(ET pos) const + { + return *reinterpret_cast(mDataPtr+pos.val()); + } + template template inline ParallelOperationRoot& ParallelOperationRoot::set(ET pos) @@ -818,6 +856,13 @@ namespace MultiArrayTools return mVal; } + template + template + inline V OperationValue::vget(ET pos) const + { + return static_cast(mVal); // implement???!!! + } + template template inline OperationValue& OperationValue::set(ET pos) @@ -868,6 +913,15 @@ namespace MultiArrayTools template mkOpExpr(mF, pos, mOps); } + template + template + inline auto Operation::vget(ET pos) const + { + typedef std::tuple OpTuple; + return PackNum:: + template mkVOpExpr(mF, pos, mOps); // implement!!! + } + template template inline Operation& Operation::set(ET pos) @@ -911,6 +965,14 @@ namespace MultiArrayTools return mOp.template get(pos); } + template + template + inline auto Contraction::vget(ET pos) const + -> decltype(mOp.template vget(pos)) + { + return mOp.template vget(pos); + } + template template inline Contraction& Contraction::set(ET pos) diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index 871db03..280c1cc 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -203,6 +203,7 @@ namespace MultiArrayTools EXTERN = 0, TARGET = 1 }; + template class AssignmentExpr2 : public ExpressionBase @@ -261,6 +262,10 @@ namespace MultiArrayTools MOp& operator=(MOp&& in) = default; inline size_t get(ExtType last) const; + + template + inline size_t vget(ExtType last) const { return get(last); } + inline MOp& set(ExtType last); auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; @@ -301,6 +306,10 @@ namespace MultiArrayTools inline void operator()(size_t start = 0); inline void get(ExtType last); + + template + inline void vget(ExtType last) { get(last); } + inline void operator()(size_t start, ExtType last); auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; @@ -388,6 +397,9 @@ namespace MultiArrayTools template inline const T& get(ET pos) const; + template + inline const V& vget(ET pos) const; + template inline ConstOperationRoot& set(ET pos); @@ -427,6 +439,9 @@ namespace MultiArrayTools template inline T get(ET pos) const; + template + inline V vget(ET pos) const; + template inline StaticCast& set(ET pos); @@ -462,6 +477,9 @@ namespace MultiArrayTools template inline value_type get(ET pos) const; + template + inline V vget(ET pos) const; + template inline MetaOperationRoot& set(ET pos); @@ -541,6 +559,9 @@ namespace MultiArrayTools template inline T& get(ET pos) const; + template + inline V& vget(ET pos) const; + template inline OperationRoot& set(ET pos); @@ -612,6 +633,9 @@ namespace MultiArrayTools template inline T& get(ET pos) const; + + template + inline V& vget(ET pos) const; template inline ParallelOperationRoot& set(ET pos); @@ -642,6 +666,9 @@ namespace MultiArrayTools template inline const T& get(ET pos) const; + template + inline V vget(ET pos) const; + template inline OperationValue& set(ET pos); @@ -682,6 +709,9 @@ namespace MultiArrayTools template inline auto get(ET pos) const; + template + inline auto vget(ET pos) const; + template inline Operation& set(ET pos); @@ -754,6 +784,10 @@ namespace MultiArrayTools inline auto get(ET pos) const -> decltype(mOp.template get(pos)); + template + inline auto vget(ET pos) const + -> decltype(mOp.template vget(pos)); + template inline Contraction& set(ET pos); diff --git a/src/include/pack_num.h b/src/include/pack_num.h index 5a185c5..7b6af61 100644 --- a/src/include/pack_num.h +++ b/src/include/pack_num.h @@ -81,6 +81,17 @@ namespace MultiArrayHelper ( f, pos, ops, std::get(ops).get(Getter::template getX( pos )), args...); } + template + static inline auto mkVOpExpr(std::shared_ptr f, const ETuple& pos, const OpTuple& ops, Args... args) + { + typedef typename std::remove_reference(ops))>::type NextOpType; + static_assert(LAST >= NextOpType::SIZE, "inconsistent array positions"); + static constexpr size_t NEXT = LAST - NextOpType::SIZE; + typedef decltype(std::get(ops).template vget(Getter::template getX( pos ))) ArgT; + return PackNum::template mkVOpExpr + ( f, pos, ops, std::get(ops).template vget(Getter::template getX( pos )), args...); + } + template static auto mkLoop( const OpTuple& ot, Expr exp ) -> decltype(std::get(ot).loop( PackNum::mkLoop(ot,exp) )) @@ -168,6 +179,16 @@ namespace MultiArrayHelper //return OpFunction::apply(std::get<0>(ops).get(Getter<0>::template getX( pos )), args...); } + template + static inline auto mkVOpExpr(std::shared_ptr f, const ETuple& pos, const OpTuple& ops, const Args&... args) + { + typedef typename std::remove_reference(ops))>::type NextOpType; + static constexpr size_t NEXT = LAST - NextOpType::SIZE; + static_assert(NEXT == 0, "inconsistent array positions"); + typedef decltype(std::get<0>(ops).template vget(Getter<0>::template getX( pos ))) ArgT; + return Application::template apply(f, std::get<0>(ops).template vget(Getter<0>::template getX( pos )), args...); + } + template static auto mkLoop( const OpTuple& ot, Expr exp ) -> decltype(std::get<0>(ot).loop( exp )) From 6ecbe5ff27502383a604dffb7870de41884bdabe Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 14 Jan 2021 16:26:53 +0100 Subject: [PATCH 02/56] minor changes --- src/include/multi_array_operation.cc.h | 17 +++++++---- src/include/multi_array_operation.h | 39 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index d1684de..f0d9c32 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -174,15 +174,18 @@ namespace MultiArrayTools { ExtType last = rootSteps(); last.zero(); - mDataPtr[OpIndexResolve::get(start,last)] = mSec.get(last.next()); - //mDataPtr[last.val()] = mSec.get(last.next()); + //mDataPtr[OpIndexResolve::get(start,last)] = mSec.get(last.next()); + IAssign::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); + + //mDataPtr[last.val()] = mSec.get(last.next()); } template inline void AssignmentExpr2::operator()(size_t start, ExtType last) { //CHECK; - mDataPtr[OpIndexResolve::get(start,last)] = mSec.get(last.next()); + //mDataPtr[OpIndexResolve::get(start,last)] = mSec.get(last.next()); + IAssign::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); } template @@ -303,7 +306,9 @@ namespace MultiArrayTools template inline void AddExpr::operator()(size_t start, ExtType last) { - mDataPtr[OpIndexResolve::get(start,last)] += mSec.get(last.next()); + //IPlus::f(mDataPtr[OpIndexResolve::get(start,last)],mSec.get(last.next())); + IPlus::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); + //mDataPtr[OpIndexResolve::get(start,last)] += mSec.get(last.next()); //mDataPtr[start] += mSec.get(last.next()); //mDataPtr[start] += mSec.template get(last); } @@ -313,7 +318,9 @@ namespace MultiArrayTools { ExtType last = rootSteps(); last.zero(); - mDataPtr[OpIndexResolve::get(start,last)] += mSec.get(last.next()); + //mDataPtr[OpIndexResolve::get(start,last)] += mSec.get(last.next()); + //IPlus::f(mDataPtr[OpIndexResolve::get(start,last)],mSec.get(last.next())); + IPlus::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); } template diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index 280c1cc..742fb72 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -204,6 +204,45 @@ namespace MultiArrayTools TARGET = 1 }; + struct IAssign + { + template + static inline void f(T*& t, size_t pos, const Op& op, ExtType e) + { + //a = b; + t[pos] = op.get(e); + } + }; + + template + struct IVAssign + { + template + static inline void f(T1& a, const T2& b) + { + *reinterpret_cast(&a) = b; + } + }; + + struct IPlus + { + template + static inline void f(T*& t, size_t pos, const Op& op, ExtType e) + { + t[pos] += op.get(e); + } + }; + + template + struct IVPlus + { + template + static inline void f(T1& a, const T2& b) + { + *reinterpret_cast(&a) += b; + } + }; + template class AssignmentExpr2 : public ExpressionBase From fdb1bb6833d569b5fe1dfc2da2ca154043d0b5fe Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 14 Jan 2021 17:40:08 +0100 Subject: [PATCH 03/56] xfor: vectrization requirements --- src/include/map_range.cc.h | 23 +++++++++++++++++++++++ src/include/map_range.h | 10 ++++++++-- src/include/multi_array_operation.cc.h | 6 +++--- src/include/xfor/xfor.h | 20 ++++++++++++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/include/map_range.cc.h b/src/include/map_range.cc.h index 678b32e..8f786dd 100644 --- a/src/include/map_range.cc.h +++ b/src/include/map_range.cc.h @@ -27,6 +27,18 @@ namespace MultiArrayTools assert(mIndPtr != nullptr); } + template + std::shared_ptr OpExpr::deepCopy() const + { + return std::make_shared>(*this); + } + + template + inline void OpExpr::operator()(size_t mlast, DExt last) + { + operator()(mlast, std::dynamic_pointer_cast>(last)->ext()); + } + template inline void OpExpr::operator()(size_t mlast, ExtType last) @@ -63,7 +75,18 @@ namespace MultiArrayTools //return mExpr.rootSteps(iPtrNum).extend( mOp.rootSteps(iPtrNum) ); } + template + DExt OpExpr::dRootSteps(std::intptr_t iPtrNum) const + { + return std::make_shared>(rootSteps(iPtrNum)); + } + template + DExt OpExpr::dExtension() const + { + return std::make_shared>(mExt); + } + // -> define in range_base.cc //std::shared_ptr mkMULTI(const char** dp); diff --git a/src/include/map_range.h b/src/include/map_range.h index fcb56f3..42623b0 100644 --- a/src/include/map_range.h +++ b/src/include/map_range.h @@ -50,7 +50,7 @@ namespace MultiArrayTools template //template - class OpExpr + class OpExpr : public ExpressionBase { public: //typedef typename Index::OIType OIType; @@ -80,11 +80,17 @@ namespace MultiArrayTools OpExpr(const Op& mapf, const Index* ind, size_t step, Expr ex); + virtual std::shared_ptr deepCopy() const override final; + + inline void operator()(size_t mlast, DExt last) override final; inline void operator()(size_t mlast, ExtType last); - inline void operator()(size_t mlast = 0); + inline void operator()(size_t mlast = 0) override final; auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; + virtual DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; + virtual DExt dExtension() const override final; + }; template diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index f0d9c32..70dbf7e 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -401,7 +401,7 @@ namespace MultiArrayTools template inline const V& ConstOperationRoot::vget(ET pos) const { - return *reinterpret_cast(mDataPtr+pos.val()); + return *(reinterpret_cast(mDataPtr)+pos.val()); } template @@ -668,7 +668,7 @@ namespace MultiArrayTools template inline V& OperationRoot::vget(ET pos) const { - return *reinterpret_cast(mDataPtr + pos.val()); + return *(reinterpret_cast(mDataPtr)+pos.val()); } template @@ -815,7 +815,7 @@ namespace MultiArrayTools template inline V& ParallelOperationRoot::vget(ET pos) const { - return *reinterpret_cast(mDataPtr+pos.val()); + return *(reinterpret_cast(mDataPtr)+pos.val()); } template diff --git a/src/include/xfor/xfor.h b/src/include/xfor/xfor.h index 58a8106..292b666 100644 --- a/src/include/xfor/xfor.h +++ b/src/include/xfor/xfor.h @@ -173,6 +173,8 @@ namespace MultiArrayHelper ExpressionBase& operator=(const ExpressionBase& in) = default; ExpressionBase& operator=(ExpressionBase&& in) = default; + virtual std::intptr_t vec(size_t vs) { return 0; } + virtual std::shared_ptr deepCopy() const = 0; virtual void operator()(size_t mlast, DExt last) = 0; @@ -377,6 +379,15 @@ namespace MultiArrayHelper return std::make_shared>(*this); } + virtual std::intptr_t vec(size_t vs) override final + { + if(mStep == 1 and mMax % vs == 0){ + mMax /= vs; + return reinterpret_cast(mIndPtr); + } + return mExpr.vec(vs); + } + inline void operator()(size_t mlast, DExt last) override final; inline void operator()(size_t mlast, ExtType last) ; inline void operator()(size_t mlast = 0) override final; @@ -425,6 +436,15 @@ namespace MultiArrayHelper PFor(const IndexClass* indPtr, size_t step, Expr expr); + virtual std::intptr_t vec(size_t vs) override final + { + if(mStep == 1 and mMax % vs == 0){ + mMax /= vs; + return reinterpret_cast(mIndPtr); + } + return mExpr.vec(vs); + } + virtual std::shared_ptr deepCopy() const override final { return std::make_shared>(*this); From 29b1502e60d1e0473180b55e8d96845019e65680 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 14 Jan 2021 19:20:46 +0100 Subject: [PATCH 04/56] unify AssigmentExpr2 and AddExpr --- src/include/multi_array_operation.cc.h | 38 +++++++++++++------------- src/include/multi_array_operation.h | 27 +++++++++++------- src/include/xfor/exttype.h | 36 ++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 32 deletions(-) diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index 70dbf7e..9c88d37 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -165,50 +165,50 @@ namespace MultiArrayTools }; - template - AssignmentExpr2::AssignmentExpr2(T* dataPtr, const Target& tar, const OpClass& sec) : + template + AssignmentExpr::AssignmentExpr(T* dataPtr, const Target& tar, const OpClass& sec) : mTar(tar), mSec(sec), mDataPtr(dataPtr) {} - template - inline void AssignmentExpr2::operator()(size_t start) + template + inline void AssignmentExpr::operator()(size_t start) { ExtType last = rootSteps(); last.zero(); //mDataPtr[OpIndexResolve::get(start,last)] = mSec.get(last.next()); - IAssign::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); + IOp::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); //mDataPtr[last.val()] = mSec.get(last.next()); } - template - inline void AssignmentExpr2::operator()(size_t start, ExtType last) + template + inline void AssignmentExpr::operator()(size_t start, ExtType last) { //CHECK; //mDataPtr[OpIndexResolve::get(start,last)] = mSec.get(last.next()); - IAssign::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); + IOp::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); } - template - typename AssignmentExpr2::ExtType - AssignmentExpr2::rootSteps(std::intptr_t iPtrNum) const + template + typename AssignmentExpr::ExtType + AssignmentExpr::rootSteps(std::intptr_t iPtrNum) const { return mTar.rootSteps(iPtrNum).extend( mSec.rootSteps(iPtrNum) ); } - template - inline void AssignmentExpr2::operator()(size_t mlast, DExt last) + template + inline void AssignmentExpr::operator()(size_t mlast, DExt last) { (*this)(mlast, std::dynamic_pointer_cast>(last)->ext()); } - template - inline DExt AssignmentExpr2::dRootSteps(std::intptr_t iPtrNum) const + template + inline DExt AssignmentExpr::dRootSteps(std::intptr_t iPtrNum) const { return std::make_shared>(rootSteps(iPtrNum)); } - template - inline DExt AssignmentExpr2::dExtension() const + template + inline DExt AssignmentExpr::dExtension() const { CHECK; return nullptr; //???!!! @@ -298,7 +298,7 @@ namespace MultiArrayTools CHECK; return nullptr; //???!!! } - + /* template AddExpr::AddExpr(T* dataPtr, const Target& tar, const OpClass& sec) : mTar(tar), mSec(sec), mDataPtr(dataPtr) {} @@ -348,7 +348,7 @@ namespace MultiArrayTools CHECK; return nullptr; //???!!! } - + */ /**************************** * ConstOperationRoot * ****************************/ diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index 742fb72..b8ac7e8 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -244,11 +244,11 @@ namespace MultiArrayTools }; - template - class AssignmentExpr2 : public ExpressionBase + template + class AssignmentExpr : public ExpressionBase { private: - AssignmentExpr2() = default; + AssignmentExpr() = default; Target mTar; OpClass mSec; @@ -260,15 +260,15 @@ namespace MultiArrayTools static constexpr size_t SIZE = Target::SIZE + OpClass::SIZE; typedef decltype(mTar.rootSteps(0).extend( mSec.rootSteps(0) )) ExtType; - AssignmentExpr2(T* dataPtr, const Target& tar, const OpClass& sec); - AssignmentExpr2(const AssignmentExpr2& in) = default; - AssignmentExpr2(AssignmentExpr2&& in) = default; - AssignmentExpr2& operator=(const AssignmentExpr2& in) = default; - AssignmentExpr2& operator=(AssignmentExpr2&& in) = default; + AssignmentExpr(T* dataPtr, const Target& tar, const OpClass& sec); + AssignmentExpr(const AssignmentExpr& in) = default; + AssignmentExpr(AssignmentExpr&& in) = default; + AssignmentExpr& operator=(const AssignmentExpr& in) = default; + AssignmentExpr& operator=(AssignmentExpr&& in) = default; virtual std::shared_ptr deepCopy() const override final { - return std::make_shared>(*this); + return std::make_shared>(*this); } inline void operator()(size_t start = 0); @@ -281,6 +281,12 @@ namespace MultiArrayTools inline DExt dExtension() const override final; }; + template + using AssignmentExpr2 = AssignmentExpr; + + template + using AddExpr = AssignmentExpr; + template class MOp { @@ -371,6 +377,7 @@ namespace MultiArrayTools } //template + /* template class AddExpr : public ExpressionBase { @@ -411,7 +418,7 @@ namespace MultiArrayTools inline DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; inline DExt dExtension() const override final; }; - + */ template class ConstOperationRoot : public OperationTemplate > { diff --git a/src/include/xfor/exttype.h b/src/include/xfor/exttype.h index 9f94784..b6202c4 100644 --- a/src/include/xfor/exttype.h +++ b/src/include/xfor/exttype.h @@ -85,7 +85,17 @@ namespace MultiArrayHelper inline auto nn() const -> decltype(Getter::getX(*this)) { return Getter::getX(*this); } - + + inline bool operator==(const MExt& in) const + { + return mExt == in.mExt and mNext == in.mNext; + } + + inline bool operator==(size_t in) const + { + return mExt == in and mNext == in; + } + inline MExt operator+(const MExt& in) const; inline MExt operator+(const None& in) const; inline MExt operator*(size_t in) const; @@ -110,7 +120,17 @@ namespace MultiArrayHelper None(const Y& y) {} static constexpr size_t SIZE = 0; - + + inline bool operator==(const None& in) const + { + return true; + } + + inline bool operator==(size_t in) const + { + return true; // CHECK!!! + } + inline None operator+(const None& in) const { return None(); } inline None operator*(size_t in) const { return None(); } @@ -157,7 +177,17 @@ namespace MultiArrayHelper template inline MExt(const MExt& y); - + + inline bool operator==(const MExt& in) const + { + return mExt == in.mExt; + } + + inline bool operator==(size_t in) const + { + return mExt == in; + } + inline const size_t& val() const; inline None next() const { return None(); } From 68ae7d84287179c7118842b838db34a08eed2edd Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 14 Jan 2021 23:57:06 +0100 Subject: [PATCH 05/56] im com --- src/include/multi_array_operation.cc.h | 55 -------------------- src/include/multi_array_operation.h | 70 ++++++++------------------ 2 files changed, 20 insertions(+), 105 deletions(-) diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index 9c88d37..b849726 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -174,17 +174,12 @@ namespace MultiArrayTools { ExtType last = rootSteps(); last.zero(); - //mDataPtr[OpIndexResolve::get(start,last)] = mSec.get(last.next()); IOp::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); - - //mDataPtr[last.val()] = mSec.get(last.next()); } template inline void AssignmentExpr::operator()(size_t start, ExtType last) { - //CHECK; - //mDataPtr[OpIndexResolve::get(start,last)] = mSec.get(last.next()); IOp::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); } @@ -298,57 +293,7 @@ namespace MultiArrayTools CHECK; return nullptr; //???!!! } - /* - template - AddExpr::AddExpr(T* dataPtr, const Target& tar, const OpClass& sec) : - mTar(tar), mSec(sec), mDataPtr(dataPtr) {} - - template - inline void AddExpr::operator()(size_t start, ExtType last) - { - //IPlus::f(mDataPtr[OpIndexResolve::get(start,last)],mSec.get(last.next())); - IPlus::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); - //mDataPtr[OpIndexResolve::get(start,last)] += mSec.get(last.next()); - //mDataPtr[start] += mSec.get(last.next()); - //mDataPtr[start] += mSec.template get(last); - } - template - inline void AddExpr::operator()(size_t start) - { - ExtType last = rootSteps(); - last.zero(); - //mDataPtr[OpIndexResolve::get(start,last)] += mSec.get(last.next()); - //IPlus::f(mDataPtr[OpIndexResolve::get(start,last)],mSec.get(last.next())); - IPlus::f(mDataPtr,OpIndexResolve::get(start,last),mSec,last.next()); - } - - template - typename AddExpr::ExtType AddExpr::rootSteps(std::intptr_t iPtrNum) const - { - return mTar.rootSteps(iPtrNum).extend( mSec.rootSteps(iPtrNum) ); - //return mSec.rootSteps(iPtrNum); - } - - template - inline void AddExpr::operator()(size_t mlast, DExt last) - { - (*this)(mlast, std::dynamic_pointer_cast>(last)->ext()); - } - - template - inline DExt AddExpr::dRootSteps(std::intptr_t iPtrNum) const - { - return std::make_shared>(rootSteps(iPtrNum)); - } - - template - inline DExt AddExpr::dExtension() const - { - CHECK; - return nullptr; //???!!! - } - */ /**************************** * ConstOperationRoot * ****************************/ diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index b8ac7e8..2d3787d 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -16,8 +16,8 @@ #include "pack_num.h" #include "arith.h" - #include "xfor/xfor.h" +#include "type_operations.h" namespace MultiArrayTools { @@ -217,10 +217,10 @@ namespace MultiArrayTools template struct IVAssign { - template - static inline void f(T1& a, const T2& b) + template + static inline void f(T*& t, size_t pos, const Op& op, ExtType e) { - *reinterpret_cast(&a) = b; + reinterpret_cast(t)[pos] = op.template vget(e); } }; @@ -236,13 +236,26 @@ namespace MultiArrayTools template struct IVPlus { - template - static inline void f(T1& a, const T2& b) + template + static inline void f(T*& t, size_t pos, const Op& op, ExtType e) { - *reinterpret_cast(&a) += b; + reinterpret_cast(t)[pos] += op.template vget(e); } }; + template + struct VType + { + typedef T type; + static constexpr size_t MULT = sizeof(type)/sizeof(T); + }; + + template <> + struct VType + { + typedef v256 type; + static constexpr size_t MULT = sizeof(type)/sizeof(double); + }; template class AssignmentExpr : public ExpressionBase @@ -376,49 +389,6 @@ namespace MultiArrayTools return MOp(exprs...); } - //template - /* - template - class AddExpr : public ExpressionBase - { - private: - AddExpr() = default; - - Target mTar; - OpClass mSec; - T* mDataPtr; - - public: - - static constexpr size_t LAYER = 0; - static constexpr size_t SIZE = Target::SIZE + OpClass::SIZE; - typedef decltype(mTar.rootSteps(0).extend( mSec.rootSteps(0) )) ExtType; - // static constexpr size_t LAYER = 0; - //static constexpr size_t SIZE = OpClass::SIZE; - //typedef decltype(mSec.rootSteps()) ExtType; - - //AddExpr(T* dataPtr, const OpClass& sec); - AddExpr(T* dataPtr, const Target& tar, const OpClass& sec); - AddExpr(const AddExpr& in) = default; - AddExpr(AddExpr&& in) = default; - AddExpr& operator=(const AddExpr& in) = default; - AddExpr& operator=(AddExpr&& in) = default; - - virtual std::shared_ptr deepCopy() const override final - { - return std::make_shared>(*this); - } - - inline void operator()(size_t start = 0); - inline void operator()(size_t start, ExtType last); - auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; - - inline void operator()(size_t mlast, DExt last) override final; - - inline DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; - inline DExt dExtension() const override final; - }; - */ template class ConstOperationRoot : public OperationTemplate > { From db9219704835e976958a161e9f9f1b5e83dc73d3 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Fri, 15 Jan 2021 01:05:58 +0100 Subject: [PATCH 06/56] im com (Operation: remove T?) --- src/include/multi_array_operation.cc.h | 77 +++++++++++++++++--------- src/include/multi_array_operation.h | 36 +++++++----- src/include/pack_num.h | 2 +- src/include/xfor/xfor.h | 14 +++++ 4 files changed, 89 insertions(+), 40 deletions(-) diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index b849726..b9d0207 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -519,66 +519,91 @@ namespace MultiArrayTools } template - template - auto OperationRoot::assign(const OpClass& in) const - -> decltype(mIndex.ifor(1,in.loop(AssignmentExpr2,OpClass,OpIndexAff::TARGET> + template + auto OperationRoot::asx(const OpClass& in) const + -> decltype(mIndex.ifor(1,in.loop(AssignmentExpr,OpClass,OpIndexAff::TARGET> (mOrigDataPtr,*this,in)))) { static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return mIndex.ifor(1,in.loop(AssignmentExpr2,OpClass,OpIndexAff::TARGET> + return mIndex.ifor(1,in.loop(AssignmentExpr,OpClass,OpIndexAff::TARGET> (mOrigDataPtr,*this,in))); } + template + template + auto OperationRoot::asxExpr(const OpClass& in) const + -> decltype(in.loop(AssignmentExpr,OpClass> + (mOrigDataPtr,*this,in))) + { + static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); + return in.loop(AssignmentExpr,OpClass> + (mOrigDataPtr,*this,in)); + } + + template + template + auto OperationRoot::asx(const OpClass& in, const std::shared_ptr& i) const + -> decltype(i->ifor(1,in.loop(AssignmentExpr,OpClass> + (mOrigDataPtr,*this,in)))) + { + static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); + return i->ifor(1,in.loop(AssignmentExpr,OpClass> + (mOrigDataPtr,*this,in))); + } + + template + template + auto OperationRoot::assign(const OpClass& in) const + -> decltype(this->template asx(in)) + { + return this->template asx(in); + } + template template auto OperationRoot::assignExpr(const OpClass& in) const - -> decltype(in.loop(AssignmentExpr2,OpClass> - (mOrigDataPtr,*this,in))) + -> decltype(this->template asxExpr(in)) { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return in.loop(AssignmentExpr2,OpClass> - (mOrigDataPtr,*this,in)); + return this->template asxExpr(in); } template template auto OperationRoot::assign(const OpClass& in, const std::shared_ptr& i) const - -> decltype(i->ifor(1,in.loop(AssignmentExpr2,OpClass> - (mOrigDataPtr,*this,in)))) + -> decltype(this->template asx(in,i)) { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return i->ifor(1,in.loop(AssignmentExpr2,OpClass> - (mOrigDataPtr,*this,in))); + return this->template asx(in,i); } template template auto OperationRoot::plus(const OpClass& in) const - -> decltype(mIndex.ifor(1,in.loop(AddExpr,OpClass,OpIndexAff::TARGET> - (mOrigDataPtr,*this,in)))) + -> decltype(this->template asx(in)) { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return mIndex.ifor(1,in.loop(AddExpr,OpClass,OpIndexAff::TARGET> - (mOrigDataPtr,*this,in))); + return this->template asx(in); } template template auto OperationRoot::plus(const OpClass& in, const std::shared_ptr& i) const - -> decltype(i->ifor(1,in.loop(AddExpr,OpClass> - (mOrigDataPtr,*this,in)))) + -> decltype(this->template asx(in,i)) { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return i->ifor(1,in.loop(AddExpr,OpClass> - (mOrigDataPtr,*this,in))); + return this->template asx(in,i); } template template OperationRoot& OperationRoot::operator=(const OpClass& in) { - assign(in)(); + auto x = this->template asx::type>>(in); + const size_t inum = x.vec(VType::MULT); + if(x.rootSteps(inum) == 1){ + x(); + } + else { + assign(in)(); + } return *this; } @@ -613,7 +638,7 @@ namespace MultiArrayTools template inline V& OperationRoot::vget(ET pos) const { - return *(reinterpret_cast(mDataPtr)+pos.val()); + return *(reinterpret_cast(mDataPtr)+pos.val()); } template diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index 2d3787d..53e284b 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -538,29 +538,39 @@ namespace MultiArrayTools OperationRoot(T* data, const IndexType& ind); - template - auto assign(const OpClass& in) const - -> decltype(mIndex.ifor(1,in.loop(AssignmentExpr2,OpClass,OpIndexAff::TARGET> + template + auto asx(const OpClass& in) const + -> decltype(mIndex.ifor(1,in.loop(AssignmentExpr,OpClass,OpIndexAff::TARGET> (mOrigDataPtr,*this,in)))); - template - auto assignExpr(const OpClass& in) const - -> decltype(in.loop(AssignmentExpr2,OpClass>(mOrigDataPtr,*this,in))); + template + auto asxExpr(const OpClass& in) const + -> decltype(in.loop(AssignmentExpr,OpClass>(mOrigDataPtr,*this,in))); - template - auto assign(const OpClass& in, const std::shared_ptr& i) const - -> decltype(i->ifor(1,in.loop(AssignmentExpr2,OpClass> + template + auto asx(const OpClass& in, const std::shared_ptr& i) const + -> decltype(i->ifor(1,in.loop(AssignmentExpr,OpClass> (mOrigDataPtr,*this,in)))); template + auto assign(const OpClass& in) const + -> decltype(this->template asx(in)); + + template + auto assignExpr(const OpClass& in) const + -> decltype(this->template asxExpr(in)); + + template + auto assign(const OpClass& in, const std::shared_ptr& i) const + -> decltype(this->template asx(in,i)); + + template auto plus(const OpClass& in) const - -> decltype(mIndex.ifor(1,in.loop(AddExpr,OpClass,OpIndexAff::TARGET> - (mOrigDataPtr,*this,in)))); + -> decltype(this->template asx(in)); template auto plus(const OpClass& in, const std::shared_ptr& i) const - -> decltype(i->ifor(1,in.loop(AddExpr,OpClass> - (mOrigDataPtr,*this,in)))); + -> decltype(this->template asx(in,i)); template OperationRoot& operator=(const OpClass& in); diff --git a/src/include/pack_num.h b/src/include/pack_num.h index 7b6af61..234e6bf 100644 --- a/src/include/pack_num.h +++ b/src/include/pack_num.h @@ -88,7 +88,7 @@ namespace MultiArrayHelper static_assert(LAST >= NextOpType::SIZE, "inconsistent array positions"); static constexpr size_t NEXT = LAST - NextOpType::SIZE; typedef decltype(std::get(ops).template vget(Getter::template getX( pos ))) ArgT; - return PackNum::template mkVOpExpr + return PackNum::template mkVOpExpr ( f, pos, ops, std::get(ops).template vget(Getter::template getX( pos )), args...); } diff --git a/src/include/xfor/xfor.h b/src/include/xfor/xfor.h index 292b666..1ee58f5 100644 --- a/src/include/xfor/xfor.h +++ b/src/include/xfor/xfor.h @@ -35,6 +35,9 @@ namespace MultiArrayHelper virtual size_t size() const = 0; virtual const size_t& val() const = 0; //virtual size_t rootSteps() const = 0; + virtual bool operator==(const ExtBase& in) const = 0; + virtual bool operator==(size_t in) const = 0; + virtual std::shared_ptr operator+(const ExtBase& in) const = 0; virtual std::shared_ptr operator*(size_t in) const = 0; virtual void zero() = 0; @@ -75,6 +78,12 @@ namespace MultiArrayHelper virtual const size_t& val() const override final { return mExt.val(); } virtual void zero() override final { mExt.zero(); } + virtual bool operator==(const ExtBase& in) const override final + { return mExt == dynamic_cast&>(in).mExt; } + + virtual bool operator==(size_t in) const override final + { return mExt == in; } + virtual DExt operator+(const ExtBase& in) const override final { return std::make_shared>( mExt + dynamic_cast&>(in).mExt ); } virtual DExt operator*(size_t in) const override final @@ -122,6 +131,11 @@ namespace MultiArrayHelper template DExtTX(const Y& y) : mDExt(std::make_shared>(y)) {} */ + bool operator==(const DExtTX& in) const + { return *mDExt == *in.mDExt and mNext == in.mNext; } + + bool operator==(size_t in) const + { return *mDExt == in and mNext == in; } template DExtTX(const DExtTX& in) : mDExt(in.mDExt), mNext(in.mNext) {} From 64d10867dd251b4fca90a6eda0d526ef6c3b9d0e Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Mon, 18 Jan 2021 19:00:12 +0100 Subject: [PATCH 07/56] finish parts of auto-vectorization --- src/include/arith.h | 8 +- src/include/dynamic_operation.h | 7 ++ src/include/multi_array_operation.cc.h | 63 ++++++++----- src/include/multi_array_operation.h | 117 ++++++++++++++++++++----- src/include/type_operations.h | 31 ++++++- 5 files changed, 180 insertions(+), 46 deletions(-) diff --git a/src/include/arith.h b/src/include/arith.h index db34d75..e9339d8 100644 --- a/src/include/arith.h +++ b/src/include/arith.h @@ -48,7 +48,8 @@ namespace MultiArrayTools { static constexpr bool FISSTATIC = true; typedef T value_type; - + typedef F function; + template static auto mk(const Ops&... ops) -> Operation @@ -74,6 +75,11 @@ namespace MultiArrayTools { return a; } + + static inline T selfApply(T& a1, const T& a2) + { + return a1 = a2; + } }; template diff --git a/src/include/dynamic_operation.h b/src/include/dynamic_operation.h index 12c1936..3835019 100644 --- a/src/include/dynamic_operation.h +++ b/src/include/dynamic_operation.h @@ -17,6 +17,7 @@ namespace MultiArrayTools static constexpr size_t SIZE = 1; static constexpr bool CONT = true; + static constexpr bool VABLE = false; DynamicOperationBase() = default; DynamicOperationBase(const DynamicOperationBase& in) = default; @@ -125,6 +126,7 @@ namespace MultiArrayTools static constexpr size_t SIZE = 1; static constexpr bool CONT = true; + static constexpr bool VABLE = false; DynamicO() = default; DynamicO(const DynamicO& in) : mOp(in.mOp ? in.mOp->deepCopy() : nullptr) {} @@ -144,6 +146,11 @@ namespace MultiArrayTools template inline T get(const DExtTX& pos) const { return mOp->get(pos.reduce()); } + + template + inline auto vget(const DExtTX& pos) const + { return mOp->template vget(pos.reduce()); } + template inline DynamicO& set(const DExtTX& pos) { mOp->set(pos.reduce()); return *this; } inline DExtT rootSteps(std::intptr_t iPtrNum = 0) const { return mOp->rootSteps(iPtrNum); } diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index b9d0207..0f1fa41 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -555,55 +555,78 @@ namespace MultiArrayTools template template auto OperationRoot::assign(const OpClass& in) const - -> decltype(this->template asx(in)) + -> decltype(this->template asx>(in)) { - return this->template asx(in); + return this->template asx>(in); } template template auto OperationRoot::assignExpr(const OpClass& in) const - -> decltype(this->template asxExpr(in)) + -> decltype(this->template asxExpr>(in)) { - return this->template asxExpr(in); + return this->template asxExpr>(in); } template template auto OperationRoot::assign(const OpClass& in, const std::shared_ptr& i) const - -> decltype(this->template asx(in,i)) + -> decltype(this->template asx>(in,i)) { - return this->template asx(in,i); + return this->template asx>(in,i); } template template auto OperationRoot::plus(const OpClass& in) const - -> decltype(this->template asx(in)) + -> decltype(this->template asx>(in)) { - return this->template asx(in); + return this->template asx>(in); } template template auto OperationRoot::plus(const OpClass& in, const std::shared_ptr& i) const - -> decltype(this->template asx(in,i)) + -> decltype(this->template asx>(in,i)) { - return this->template asx(in,i); + return this->template asx>(in,i); } - + + template + struct VExec + { + template + static inline void exec(TarOp& th, const OpClass& in) + { + th.assign(in)(); + } + }; + + template <> + struct VExec + { + template + static inline void exec(TarOp& th, const OpClass& in) + { + CHECK; + typedef typename TarOp::value_type T; + auto x = th.template asx::type,T>>(in); + const size_t inum = x.vec(VType::MULT); + if(x.rootSteps(inum) == 1){ + CHECK; + x(); + } + else { + th.assign(in)(); + } + } + }; + template template OperationRoot& OperationRoot::operator=(const OpClass& in) { - auto x = this->template asx::type>>(in); - const size_t inum = x.vec(VType::MULT); - if(x.rootSteps(inum) == 1){ - x(); - } - else { - assign(in)(); - } + VExec::exec(*this,in); return *this; } @@ -896,7 +919,7 @@ namespace MultiArrayTools { typedef std::tuple OpTuple; return PackNum:: - template mkVOpExpr(mF, pos, mOps); // implement!!! + template mkVOpExpr>(mkVFuncPtr(mF), pos, mOps); // implement!!! } template diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index 53e284b..bc35d0e 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -204,12 +204,74 @@ namespace MultiArrayTools TARGET = 1 }; + template + struct VType + { + typedef T type; + static constexpr size_t MULT = sizeof(type)/sizeof(T); + }; + + template <> + struct VType + { + typedef v256 type; + static constexpr size_t MULT = sizeof(type)/sizeof(double); + }; + + template