From bba1c6944a4531c1b5b63549c15addb4e3ce2fa4 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 18 Jul 2021 23:11:05 +0200 Subject: [PATCH] im com (vectorization (wrong access instance) + parallel not working) --- src/include/multi_array_operation.cc.h | 114 +++++++++++++------------ src/include/multi_array_operation.h | 77 ++++++++++++----- 2 files changed, 116 insertions(+), 75 deletions(-) diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index 8bf41c8..8a127d4 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -181,46 +181,46 @@ namespace MultiArrayTools return 0; } - template - AssignmentExpr::AssignmentExpr(const AccessTemplate& dataAcc, const Target& tar, const OpClass& sec) : + template + AssignmentExpr::AssignmentExpr(const AccessTemplate& dataAcc, const Target& tar, const OpClass& sec) : mTar(tar), mSec(sec), mDataAcc(static_cast(dataAcc)) {} - template - inline void AssignmentExpr::operator()(size_t start) + template + inline void AssignmentExpr::operator()(size_t start) { ExtType last = rootSteps(); last.zero(); // TODO: ask MA container for data (ptr)!!! - mDataAcc.template exec(opIndexResolve(start,last),mSec,last.next()); + mDataAcc.template exec(opIndexResolve(start,last),mSec,last.next()); } - template - inline void AssignmentExpr::operator()(size_t start, ExtType last) + template + inline void AssignmentExpr::operator()(size_t start, ExtType last) { - mDataAcc.template exec(opIndexResolve(start,last),mSec,last.next()); + mDataAcc.template exec(opIndexResolve(start,last),mSec,last.next()); } - template - typename AssignmentExpr::ExtType - AssignmentExpr::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 AssignmentExpr::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 AssignmentExpr::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 AssignmentExpr::dExtension() const + template + inline DExt AssignmentExpr::dExtension() const { return nullptr; //???!!! } @@ -556,95 +556,96 @@ namespace MultiArrayTools } template - template + template auto OperationRoot::asx(const OpClass& in) const { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return mIndex.ifor(1,in.loop(AssignmentExpr,OperationRoot,OpClass,OpIndexAff::TARGET> - (mDataAcc,*this,in))).template vec(); + static_assert(Access::VSIZE > 0, "Access::VSIZE failed"); + return mIndex.ifor(1,this->template asxExpr(in)).template vec(); } template - template + template auto OperationRoot::asxExpr(const OpClass& in) const { static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return in.loop(AssignmentExpr,OperationRoot,OpClass> + return in.loop(AssignmentExpr,OperationRoot,OpClass> (mDataAcc,*this,in)); } template - template + template auto OperationRoot::asx(const OpClass& in, const std::shared_ptr& i) const { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return i->ifor(1,in.loop(AssignmentExpr,OperationRoot,OpClass> - (mDataAcc,*this,in))).template vec(); + static_assert(Access::VSIZE > 0, "Access::VSIZE failed"); + return i->ifor(1,this->template asxExpr(in)).template vec(); } template template auto OperationRoot::assign(const OpClass& in) const { - return this->template asx>(in); + return this->template asx,PointerAccess>(in); } template template auto OperationRoot::assignExpr(const OpClass& in) const { - return this->template asxExpr>(in); + return this->template asxExpr,PointerAccess>(in); } template template auto OperationRoot::assign(const OpClass& in, const std::shared_ptr& i) const { - return this->template asx>(in,i); + return this->template asx,PointerAccess>(in,i); } template template auto OperationRoot::plus(const OpClass& in) const { - return this->template asx>(in); + return this->template asx,PointerAccess>(in); } template template auto OperationRoot::plus(const OpClass& in, const std::shared_ptr& i) const { - return this->template asx>(in,i); + return this->template asx,PointerAccess>(in,i); } - - template class F, typename TarOp, class OpClass> + + template 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>>(in); - if(x.rootSteps(x.vI()) == 1){ - //if(0){ + auto x = th.template asx>(in); + //if(x.rootSteps(x.vI()) == 1){ + if(0){ CHECK; x(); } else { - th.template asx>>(in)(); + th.template asx>(in)(); } } else { typedef typename TarOp::value_type T; - IAccess> tmp; - th.template asx(in)(); + //IAccess> tmp; + th.template asx>(in)(); } } - + template template OperationRoot& OperationRoot::operator=(const OpClass& in) { - vexec(*this,in); + //asx>(in)(); + vexec>(*this,in); return *this; } @@ -652,7 +653,8 @@ namespace MultiArrayTools template OperationRoot& OperationRoot::operator+=(const OpClass& in) { - vexec(*this,in); + //asx>(in)(); + vexec>(*this,in); return *this; } @@ -661,13 +663,13 @@ namespace MultiArrayTools { return operator= >(in); } - + /* template ParallelOperationRoot OperationRoot::par() { return ParallelOperationRoot(mOrigDataPtr, mIndex); } - + */ template template inline T& OperationRoot::get(ET pos) const @@ -722,7 +724,7 @@ namespace MultiArrayTools /******************************* * ParallelOperationRoot * - *******************************/ + ****************************** template ParallelOperationRoot:: @@ -749,30 +751,30 @@ namespace MultiArrayTools } template - template + template auto ParallelOperationRoot::asx(const OpClass& in) const { static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return mIndex.pifor(1,in.loop(AssignmentExpr,ParallelOperationRoot,OpClass,OpIndexAff::TARGET> - (mDataAcc,*this,in))).template vec(); + return mIndex.pifor(1,in.loop(AssignmentExpr,ParallelOperationRoot,OpClass,OpIndexAff::TARGET> + (mDataAcc,*this,in))).template vec(); } template - template + template auto ParallelOperationRoot::asxExpr(const OpClass& in) const { static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return in.loop(AssignmentExpr,ParallelOperationRoot,OpClass> + return in.loop(AssignmentExpr,ParallelOperationRoot,OpClass> (mDataAcc,*this,in)); } template - template + template auto ParallelOperationRoot::asx(const OpClass& in, const std::shared_ptr& i) const { static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return i->pifor(1,in.loop(AssignmentExpr,ParallelOperationRoot,OpClass> - (mDataAcc,*this,in))).template vec(); + return i->pifor(1,in.loop(AssignmentExpr,ParallelOperationRoot,OpClass> + (mDataAcc,*this,in))).template vec(); } template @@ -837,7 +839,7 @@ namespace MultiArrayTools template inline T& ParallelOperationRoot::get(ET pos) const { - return mDataPtr[pos.val()/*+mOff*/]; + return mDataPtr[pos.val()+mOff]; } template @@ -875,7 +877,7 @@ namespace MultiArrayTools auto i = mIndex; return mOrigDataPtr + i().pos(); } - + */ /************************ * OperationValue * diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index 2edb5c4..da20604 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -133,12 +133,12 @@ namespace MultiArrayTools // empty, implement corresponding constructors...!!! } - /* + template using VFunc = decltype(mkVFunc(std::declval())); - */ - template - using VFunc = F; + + //template + //using VFunc = F; template class OpAccess @@ -177,9 +177,9 @@ namespace MultiArrayTools } }; - template - using IVAccess = IAccess; - /* + //template + //using IVAccess = IAccess; + template struct IVAccess { @@ -196,7 +196,7 @@ namespace MultiArrayTools VFunc::selfApply(*reinterpret_cast(t+pos),op.template vget(e)); } }; - */ + template using xxxplus = plus; @@ -246,6 +246,8 @@ namespace MultiArrayTools typedef T value_type; typedef T in_type; + static constexpr size_t VSIZE = sizeof(value_type) / sizeof(in_type); + friend class AccessTemplate>; private: PointerAccess() = default; @@ -273,8 +275,45 @@ namespace MultiArrayTools } }; + template + class VPointerAccess : public AccessTemplate> + { + public: + typedef typename VType::type value_type; + typedef T in_type; - template + static constexpr size_t VSIZE = sizeof(value_type) / sizeof(in_type); + + friend class AccessTemplate>; + private: + VPointerAccess() = default; + + T* mPtr = nullptr; + T* mOrigPtr = nullptr; + + public: + VPointerAccess(T* ptr, T* origPtr) : mPtr(ptr), mOrigPtr(origPtr) {} + + VPointerAccess(const VPointerAccess& in) = default; + VPointerAccess(VPointerAccess&& in) = default; + VPointerAccess& operator=(const VPointerAccess& in) = default; + VPointerAccess& operator=(VPointerAccess&& in) = default; + + VType* get(size_t pos) { return reinterpret_cast*>(mPtr+pos); } + VType* get(size_t pos) const { return reinterpret_cast*>(mPtr+pos); } + VPointerAccess& set(size_t pos) { mPtr = mOrigPtr + pos; return *this; } + VType* oget(size_t pos) const { return reinterpret_cast*>(mOrigPtr+pos); } + + template + inline void exec(size_t pos, const Op& op, ExtType e) const + { + CHECK; + F::selfApply(*get(pos),op.vget(e)); + } + }; + + + template class AssignmentExpr : public ExpressionBase { private: @@ -299,7 +338,7 @@ namespace MultiArrayTools virtual std::shared_ptr deepCopy() const override final { - return std::make_shared,Target,OpClass,OIA>>(*this); + return std::make_shared,Target,OpClass,OIA>>(*this); } inline void operator()(size_t start = 0); @@ -578,13 +617,13 @@ namespace MultiArrayTools OperationRoot(T* data, const IndexType& ind); - template + template auto asx(const OpClass& in) const; - template + template auto asxExpr(const OpClass& in) const; - template + template auto asx(const OpClass& in, const std::shared_ptr& i) const; template @@ -610,7 +649,7 @@ namespace MultiArrayTools OperationRoot& operator=(const OperationRoot& in); - ParallelOperationRoot par(); + auto par() { return *this; } template inline T& get(ET pos) const; @@ -633,7 +672,7 @@ namespace MultiArrayTools -> Slice; }; - + /* template class ParallelOperationRoot : public OperationTemplate > { @@ -661,13 +700,13 @@ namespace MultiArrayTools ParallelOperationRoot(T* data, const IndexType& ind); - template + template auto asx(const OpClass& in) const; - template + template auto asxExpr(const OpClass& in) const; - template + template auto asx(const OpClass& in, const std::shared_ptr& i) const; template @@ -710,7 +749,7 @@ namespace MultiArrayTools T* data() const; }; - + */ template class OperationValue : public OperationTemplate > {