From 9489544972578f32177a3b427d49193f934718e6 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 6 Mar 2019 13:08:33 +0100 Subject: [PATCH] remove master class + add separate parallel op root + pfor expression in parallel scope --- src/include/expressions.h | 4 +- src/include/mbase_def.h | 8 +- src/include/multi_array_operation.cc.h | 256 ++++++++++++++----------- src/include/multi_array_operation.h | 207 ++++++++++---------- src/include/xfor/exttype.h | 82 ++++---- src/include/xfor/xfor.h | 12 +- 6 files changed, 304 insertions(+), 265 deletions(-) diff --git a/src/include/expressions.h b/src/include/expressions.h index 2f5a6b5..6d9bda2 100644 --- a/src/include/expressions.h +++ b/src/include/expressions.h @@ -30,10 +30,10 @@ namespace MultiArrayTools using OX = Operation,oo...>; template - using AEXT = typename OperationMaster,Second,DynamicRange>::AssignmentExpr; + using AEXT = AssignmentExpr; template - using AEXT_P = typename OperationMaster,Second,DynamicRange>::AssignmentExpr; + using AEXT_P = AssignmentExpr; template class OpF, class... MAs> using AEX = AEXT>; diff --git a/src/include/mbase_def.h b/src/include/mbase_def.h index 2c53cb2..f981a5b 100644 --- a/src/include/mbase_def.h +++ b/src/include/mbase_def.h @@ -35,14 +35,14 @@ namespace MultiArrayTools template class OperationTemplate; - // multi_array_operation.h - template - class OperationMaster; - // multi_array_operation.h template class OperationRoot; + // multi_array_operation.h + template + class ParallelOperationRoot; + // multi_array_operation.h template class OperationValue; diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index 283af2b..e69f66e 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -117,108 +117,42 @@ namespace MultiArrayTools return Operation,OperationClass, Args...>(ll, THIS(), args...); } - /***************************************** - * OperationMaster::AssignmentExpr * - *****************************************/ - /* - template - OperationMaster::AssignmentExpr:: - AssignmentExpr(OperationMaster& m, const OpClass& sec) : - mM(m), mSec(sec) {} - */ - template - OperationMaster::AssignmentExpr:: - AssignmentExpr(T* dataPtr, const OpClass& sec) : + /************************ + * AssignmentExpr * + ************************/ + + template + AssignmentExpr::AssignmentExpr(T* dataPtr, const OpClass& sec) : mSec(sec), mDataPtr(dataPtr) {} - template - inline void OperationMaster::AssignmentExpr:: - operator()(size_t start, ExtType last) + template + inline void AssignmentExpr::operator()(size_t start, ExtType last) { - //VCHECK(mSec.template get(last)); - //mM.set(start, mSec.template get(last) ); mDataPtr[start] = mSec.template get(last); - //AOp::sapply(mDataPtr[start], mSec.template get(last)); } - template - typename OperationMaster::AssignmentExpr::ExtType - OperationMaster::AssignmentExpr:: - rootSteps(std::intptr_t iPtrNum) const + template + typename AssignmentExpr::ExtType AssignmentExpr::rootSteps(std::intptr_t iPtrNum) const { return mSec.rootSteps(iPtrNum); } - template - OperationMaster::AddExpr:: - AddExpr(T* dataPtr, const OpClass& sec) : + template + AddExpr::AddExpr(T* dataPtr, const OpClass& sec) : mSec(sec), mDataPtr(dataPtr) {} - template - inline void OperationMaster::AddExpr:: - operator()(size_t start, ExtType last) + template + inline void AddExpr::operator()(size_t start, ExtType last) { - //VCHECK(mSec.template get(last)); - //mM.set(start, mSec.template get(last) ); mDataPtr[start] += mSec.template get(last); - //AOp::sapply(mDataPtr[start], mSec.template get(last)); } - template - typename OperationMaster::AddExpr::ExtType - OperationMaster::AddExpr:: - rootSteps(std::intptr_t iPtrNum) const + template + typename AddExpr::ExtType AddExpr::rootSteps(std::intptr_t iPtrNum) const { return mSec.rootSteps(iPtrNum); } - - /************************* - * OperationMaster * - *************************/ - - template - OperationMaster:: - OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, - IndexType& index, bool doParallel) : - mSecond(second), mDataPtr(ma.data()), - mIndex(index), mDoParallel(doParallel) - { - performAssignment(0); - } - - template - OperationMaster:: - OperationMaster(T* data, const OpClass& second, - IndexType& index, bool doParallel) : - mSecond(second), mDataPtr(data), - mIndex(index), mDoParallel(doParallel) - { - performAssignment(0); - } - - template - void OperationMaster::performAssignment(std::intptr_t blockIndexNum) - { - /* - AssignmentExpr ae(*this, mSecond); // Expression to be executed within loop - if(mDoParallel){ - auto ploop = mIndex.pifor( 1, mSecond.loop(ae) ); - ploop(); // execute overall loop(s) and so internal hidden loops and so the inherited expressions - } - else { - auto loop = mIndex.ifor( 1, mSecond.loop(ae) ); - loop(); // execute overall loop(s) and so internal hidden loops and so the inherited expressions - } - */ - } - - template - inline T OperationMaster::get(size_t pos) const - { - return mDataPtr[pos]; - } - /**************************** * ConstOperationRoot * @@ -232,10 +166,8 @@ namespace MultiArrayTools mOrigDataPtr(ma.data()), mIndex( ma.begin() ) { - //VCHECK(ma.data()); mIndex(indices...); mDataPtr = mOrigDataPtr + mIndex.pos(); - //mOff = mIndex.pos(); } template @@ -397,7 +329,6 @@ namespace MultiArrayTools { mIndex(indices...); mDataPtr = mOrigDataPtr + mIndex.pos(); - //mOff = mIndex.pos(); } template @@ -408,67 +339,57 @@ namespace MultiArrayTools mIndex( ind ) { mDataPtr = mOrigDataPtr + mIndex.pos(); - //mOff = mIndex.pos(); + } + + template + template + auto OperationRoot::assign(const OpClass& in) + -> decltype(mIndex.ifor(1,in.loop(AssignmentExpr(mOrigDataPtr,in)))) + { + return mIndex.ifor(1,in.loop(AssignmentExpr(mOrigDataPtr,in))); + } + + template + template + auto OperationRoot::plus(const OpClass& in) + -> decltype(mIndex.ifor(1,in.loop(AddExpr(mOrigDataPtr,in)))) + { + return mIndex.ifor(1,in.loop(AddExpr(mOrigDataPtr,in))); } template template OperationRoot& OperationRoot::operator=(const OpClass& in) - //OperationMaster,OpClass,Ranges...> OperationRoot::operator=(const OpClass& in) { - typename OperationMaster,OpClass,Ranges...>::AssignmentExpr ae(mOrigDataPtr, in); - // Expression to be executed within loop - if(mDoParallel){ - auto ploop = mIndex.pifor( 1, in.loop(ae) ); - ploop(); // execute overall loop(s) and so internal hidden loops and so the inherited expressions - } - else { - auto loop = mIndex.ifor( 1, in.loop(ae) ); - loop(); // execute overall loop(s) and so internal hidden loops and so the inherited expressions - } + assign(in)(); return *this; - //return OperationMaster,OpClass,Ranges...>(mDataPtr, in, mIndex, mDoParallel); } template template OperationRoot& OperationRoot::operator+=(const OpClass& in) - //OperationMaster,OpClass,Ranges...> OperationRoot::operator+=(const OpClass& in) { - typename OperationMaster,OpClass,Ranges...>::AddExpr ae(mOrigDataPtr, in); - // Expression to be executed within loop - if(mDoParallel){ - auto ploop = mIndex.pifor( 1, in.loop(ae) ); - ploop(); // execute overall loop(s) and so internal hidden loops and so the inherited expressions - } - else { - auto loop = mIndex.ifor( 1, in.loop(ae) ); - loop(); // execute overall loop(s) and so internal hidden loops and so the inherited expressions - } + plus(in)(); return *this; - //return OperationMaster,OpClass,Ranges...>(mDataPtr, in, mIndex, mDoParallel); } template OperationRoot& OperationRoot::operator=(const OperationRoot& in) - //OperationMaster,OperationRoot,Ranges...> - //OperationRoot::operator=(const OperationRoot& in) { return operator= >(in); } template - OperationRoot& OperationRoot::par() + ParallelOperationRoot OperationRoot::par() { - mDoParallel = true; - return *this; + return ParallelOperationRoot(mOrigDataPtr, mIndex); } template template inline T& OperationRoot::get(ET pos) const { - return mDataPtr[pos.val()/*+mOff*/]; + return mDataPtr[pos.val()]; } template @@ -484,7 +405,6 @@ namespace MultiArrayTools MExt OperationRoot::rootSteps(std::intptr_t iPtrNum) const { return MExt(getStepSize( mIndex, iPtrNum )); - //return MExt(getStepSize( mIndex.info(), iPtrNum )); } template @@ -511,6 +431,108 @@ namespace MultiArrayTools return out; } + /******************************* + * ParallelOperationRoot * + *******************************/ + + template + ParallelOperationRoot:: + ParallelOperationRoot(MutableMultiArrayBase& ma, + const std::shared_ptr&... indices) : + mDataPtr(ma.data()), + mOrigDataPtr(ma.data()), + mIndex( ma.begin() ) + { + mIndex(indices...); + mDataPtr = mOrigDataPtr + mIndex.pos(); + } + + template + ParallelOperationRoot:: + ParallelOperationRoot(T* data, const IndexType& ind) : + mDataPtr(data), + mOrigDataPtr(data), + mIndex( ind ) + { + mDataPtr = mOrigDataPtr + mIndex.pos(); + } + + template + template + auto ParallelOperationRoot::assign(const OpClass& in) + -> decltype(mIndex.pifor(1,in.loop(AssignmentExpr(mOrigDataPtr,in)))) + { + return mIndex.pifor(1,in.loop(AssignmentExpr(mOrigDataPtr,in))); + } + + template + template + auto ParallelOperationRoot::plus(const OpClass& in) + -> decltype(mIndex.pifor(1,in.loop(AddExpr(mOrigDataPtr,in)))) + { + return mIndex.pifor(1,in.loop(AddExpr(mOrigDataPtr,in))); + } + + template + template + ParallelOperationRoot& ParallelOperationRoot::operator=(const OpClass& in) + { + assign(in)(); + return *this; + } + + template + template + ParallelOperationRoot& ParallelOperationRoot::operator+=(const OpClass& in) + { + plus(in)(); + return *this; + } + + template + ParallelOperationRoot& + ParallelOperationRoot::operator=(const ParallelOperationRoot& in) + { + return operator= >(in); + } + + template + template + inline T& ParallelOperationRoot::get(ET pos) const + { + return mDataPtr[pos.val()/*+mOff*/]; + } + + template + template + inline ParallelOperationRoot& ParallelOperationRoot::set(ET pos) + { + mIndex = pos.val(); + mDataPtr = mOrigDataPtr + mIndex.pos(); + return *this; + } + + template + MExt ParallelOperationRoot::rootSteps(std::intptr_t iPtrNum) const + { + return MExt(getStepSize( mIndex, iPtrNum )); + //return MExt(getStepSize( mIndex.info(), iPtrNum )); + } + + template + template + Expr ParallelOperationRoot::loop(Expr exp) const + { + return exp; + } + + template + T* ParallelOperationRoot::data() const + { + 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 92e96fb..0cd01ad 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -101,95 +101,54 @@ namespace MultiArrayTools } }; - template - class OperationMaster + template + class AssignmentExpr { + private: + AssignmentExpr() = default; + + OpClass mSec; + T* mDataPtr; + public: - class AssignmentExpr - { - private: - AssignmentExpr() = default; - - //OperationMaster mM; - OpClass mSec; - T* mDataPtr; - - public: - - static constexpr size_t LAYER = 0; - static constexpr size_t SIZE = OpClass::SIZE; - typedef decltype(mSec.rootSteps()) ExtType; + static constexpr size_t LAYER = 0; + static constexpr size_t SIZE = OpClass::SIZE; + typedef decltype(mSec.rootSteps()) ExtType; - //AssignmentExpr(OperationMaster& m, const OpClass& sec); - AssignmentExpr(T* dataPtr, const OpClass& sec); - - AssignmentExpr(const AssignmentExpr& in) = default; - AssignmentExpr(AssignmentExpr&& in) = default; + AssignmentExpr(T* dataPtr, const OpClass& sec); + AssignmentExpr(const AssignmentExpr& in) = default; + AssignmentExpr(AssignmentExpr&& in) = default; - inline void operator()(size_t start = 0); - inline void operator()(size_t start, ExtType last); - - auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; - - }; - - class AddExpr - { - private: - AddExpr() = default; - - //OperationMaster mM; - OpClass mSec; - T* mDataPtr; - - public: - - static constexpr size_t LAYER = 0; - static constexpr size_t SIZE = OpClass::SIZE; - typedef decltype(mSec.rootSteps()) ExtType; - - //AssignmentExpr(OperationMaster& m, const OpClass& sec); - AddExpr(T* dataPtr, const OpClass& sec); - - AddExpr(const AddExpr& in) = default; - AddExpr(AddExpr&& in) = default; - - inline void operator()(size_t start = 0); - inline void operator()(size_t start, ExtType last); - - auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; - - }; - - typedef T value_type; - //typedef OperationBase OB; - typedef ContainerRange CRange; - typedef ContainerIndex IndexType; - //typedef typename MultiRange::IndexType IndexType; - - OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, - IndexType& index, bool doParallel = false); - - OperationMaster(T* data, const OpClass& second, - IndexType& index, bool doParallel = false); - - inline void set(size_t pos, T val) { AOp::sapply(mDataPtr[pos],val); } - - //inline void add(size_t pos, T val) { mDataPtr[pos] += val; } - inline T get(size_t pos) const; - - private: - - void performAssignment(std::intptr_t blockIndexNum); - OpClass const& mSecond; - //MutableMultiArrayBase& mArrayRef; - T* mDataPtr; - IndexType mIndex; - bool mDoParallel; + inline void operator()(size_t start = 0); + inline void operator()(size_t start, ExtType last); + auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; + }; + + template + class AddExpr + { + private: + AddExpr() = default; + + OpClass mSec; + T* mDataPtr; + + public: + + 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(const AddExpr& in) = default; + AddExpr(AddExpr&& in) = default; + + inline void operator()(size_t start = 0); + inline void operator()(size_t start, ExtType last); + auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; }; - template class ConstOperationRoot : public OperationTemplate > { @@ -226,11 +185,9 @@ namespace MultiArrayTools private: - //MultiArrayBase const& mArrayRef; const T* mDataPtr; const T* mOrigDataPtr; IndexType mIndex; - //size_t mOff = 0; std::shared_ptr > mMaPtr; // never remove this ptr, otherwise we lose temporary container instances! }; @@ -300,8 +257,6 @@ namespace MultiArrayTools private: - //MultiArrayBase const& mArrayRef; - //const T* mDataPtr; mutable IndexType mWorkIndex; std::shared_ptr mIndex; }; @@ -318,21 +273,27 @@ namespace MultiArrayTools static constexpr size_t SIZE = 1; static constexpr bool CONT = true; - + + private: + + T* mDataPtr; + T* mOrigDataPtr; + IndexType mIndex; + + public: OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices); OperationRoot(T* data, const IndexType& ind); - /* - template - OperationMaster,OpClass,Ranges...> operator=(const OpClass& in); template - OperationMaster,OpClass,Ranges...> operator+=(const OpClass& in); - - OperationMaster,OperationRoot,Ranges...> operator=(const OperationRoot& in); - */ + auto assign(const OpClass& in) + -> decltype(mIndex.ifor(1,in.loop(AssignmentExpr(mOrigDataPtr,in)))); + template + auto plus(const OpClass& in) + -> decltype(mIndex.ifor(1,in.loop(AddExpr(mOrigDataPtr,in)))); + template OperationRoot& operator=(const OpClass& in); @@ -341,7 +302,7 @@ namespace MultiArrayTools OperationRoot& operator=(const OperationRoot& in); - OperationRoot& par(); + ParallelOperationRoot par(); template inline T& get(ET pos) const; @@ -353,21 +314,69 @@ namespace MultiArrayTools template Expr loop(Expr exp) const; - + T* data() const; template auto sl(const std::shared_ptr&... inds) -> Slice; + }; + + template + class ParallelOperationRoot : public OperationTemplate > + { + public: + + typedef T value_type; + typedef OperationBase > OT; + typedef ContainerRange CRange; + typedef ContainerIndex IndexType; + + static constexpr size_t SIZE = 1; + static constexpr bool CONT = true; + private: - //MutableMultiArrayBase& mArrayRef; T* mDataPtr; T* mOrigDataPtr; IndexType mIndex; - //size_t mOff = 0; - bool mDoParallel = false; + + public: + ParallelOperationRoot(MutableMultiArrayBase& ma, + const std::shared_ptr&... indices); + + ParallelOperationRoot(T* data, const IndexType& ind); + + template + auto assign(const OpClass& in) + -> decltype(mIndex.pifor(1,in.loop(AssignmentExpr(mOrigDataPtr,in)))); + + template + auto plus(const OpClass& in) + -> decltype(mIndex.pifor(1,in.loop(AddExpr(mOrigDataPtr,in)))); + + template + ParallelOperationRoot& operator=(const OpClass& in); + + template + ParallelOperationRoot& operator+=(const OpClass& in); + + ParallelOperationRoot& operator=(const ParallelOperationRoot& in); + + template + inline T& get(ET pos) const; + + template + inline ParallelOperationRoot& set(ET pos); + + MExt rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype + + template + Expr loop(Expr exp) const; + + T* data() const; + }; template diff --git a/src/include/xfor/exttype.h b/src/include/xfor/exttype.h index ffaccf2..918ed6c 100644 --- a/src/include/xfor/exttype.h +++ b/src/include/xfor/exttype.h @@ -7,6 +7,41 @@ namespace MultiArrayHelper { + template + struct Getter + { + template + static inline size_t get(const ExtType& et) + { + return Getter::get(et.next()); + } + + template + static inline auto getX(const ExtType& et) + -> decltype(Getter::getX(et.next())) + { + return Getter::getX(et.next()); + } + }; + + template <> + struct Getter<0> + { + template + static inline size_t get(const ExtType& et) + { + return et.get(); + } + + template + static inline auto getX(const ExtType& et) + -> ExtType + { + return et; + } + }; + + template class MExt { @@ -38,7 +73,12 @@ namespace MultiArrayHelper inline const size_t& val() const; inline const X& next() const; - + + template + inline auto nn() const + -> decltype(Getter::getX(*this)) + { return Getter::getX(*this); } + inline MExt operator+(const MExt& in) const; inline MExt operator*(size_t in) const; @@ -78,7 +118,12 @@ namespace MultiArrayHelper inline const size_t& val() const; inline size_t next() const { return 0; } - + + template + inline auto nn() const + -> decltype(Getter::getX(*this)) + { return Getter::getX(*this); } + inline MExt operator+(const MExt& in) const; inline MExt operator*(size_t in) const; @@ -89,39 +134,6 @@ namespace MultiArrayHelper }; - template - struct Getter - { - template - static inline size_t get(const ExtType& et) - { - return Getter::get(et.next()); - } - - template - static inline auto getX(const ExtType& et) - -> decltype(Getter::getX(et.next())) - { - return Getter::getX(et.next()); - } - }; - - template <> - struct Getter<0> - { - template - static inline size_t get(const ExtType& et) - { - return et.get(); - } - - template - static inline auto getX(const ExtType& et) - -> ExtType - { - return et; - } - }; } // end namespace MultiArrayHelper diff --git a/src/include/xfor/xfor.h b/src/include/xfor/xfor.h index 6768755..43da4c5 100644 --- a/src/include/xfor/xfor.h +++ b/src/include/xfor/xfor.h @@ -402,8 +402,6 @@ namespace MultiArrayHelper { typedef typename IndexClass::RangeType RangeType; for(size_t pos = 0u; pos != ForBound::template bound(mMax); ++pos){ - //for(size_t pos = mSPos; pos != mMax; ++pos){ - //const size_t mnpos = PosForward::value(mlast, mMax, pos); const size_t mnpos = PosForward::valuex(mlast, mStep, pos); const ExtType npos = last + mExt*pos; mExpr(mnpos, npos); @@ -416,8 +414,6 @@ namespace MultiArrayHelper typedef typename IndexClass::RangeType RangeType; const ExtType last; for(size_t pos = 0u; pos != ForBound::template bound(mMax); ++pos){ - //for(size_t pos = mSPos; pos != mMax; ++pos){ - //const size_t mnpos = PosForward::value(mlast, mMax, pos); const size_t mnpos = PosForward::valuex(mlast, mStep, pos); const ExtType npos = last + mExt*pos; mExpr(mnpos, npos); @@ -498,9 +494,9 @@ namespace MultiArrayHelper int pos = 0; size_t mnpos = 0; ExtType npos; - auto expr = mExpr; -#pragma omp parallel shared(expr) private(pos,mnpos,npos) +#pragma omp parallel shared(mExpr) private(pos,mnpos,npos) { + auto expr = mExpr; #pragma omp for nowait for(pos = 0; pos < static_cast(ForBound::template bound(mMax)); pos++){ mnpos = PosForward::valuex(mlast, mStep, pos); @@ -519,9 +515,9 @@ namespace MultiArrayHelper int pos = 0; size_t mnpos = 0; ExtType npos; - auto expr = mExpr; -#pragma omp parallel shared(expr) private(pos,mnpos,npos) +#pragma omp parallel shared(mExpr) private(pos,mnpos,npos) { + auto expr = mExpr; #pragma omp for nowait for(pos = 0; pos < static_cast(ForBound::template bound(mMax)); pos++){ mnpos = PosForward::valuex(mlast, mStep, pos);