From c674c541a18ede86b5a0d66eed0a20896040ecfa Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 13 Feb 2018 18:18:17 +0100 Subject: [PATCH] clean up + switch loops -> auto vectorizing seems to work; TODO: re-enable step sizes... --- src/arith.h | 17 +- src/multi_array_operation.h | 350 ++---------------------------------- 2 files changed, 25 insertions(+), 342 deletions(-) diff --git a/src/arith.h b/src/arith.h index c3eb3cd..540c071 100644 --- a/src/arith.h +++ b/src/arith.h @@ -8,40 +8,39 @@ namespace MultiArrayHelper template struct plus { - static inline T& acc(T& target, const T& arg) + static inline T apply(T a1, T a2) { - return target += arg; + return a1 + a2; } }; template struct minus { - static inline T& acc(T& target, const T& arg) + static inline T apply(T a1, T a2) { - return target -= arg; + return a1 - a2; } }; template struct multiplies { - static inline T& acc(T& target, const T& arg) + static inline T apply(T a1, T a2) { - return target *= arg; + return a1 * a2; } }; template struct divides { - static inline T& acc(T& target, const T& arg) + static inline T apply(T a1, T a2) { - return target /= arg; + return a1 / a2; } }; - } // end namespace MultiArrayHelper #endif diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 6d86c0f..52fb76b 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -28,22 +28,6 @@ namespace MultiArrayTools using namespace MultiArrayHelper; } - template - Block makeBlock(const T* vec, size_t stepSize, size_t blockSize); - - template - MBlock makeBlock(T* vec, size_t stepSize, size_t blockSize); - - // dont use this for now !! - template - std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, - const OpClass& second); - - template - const IndexInfo* seekBlockIndex(const IndexInfo* ownII, - const OpClass& second); - - template class OperationTemplate { @@ -86,14 +70,11 @@ namespace MultiArrayTools { private: AssignmentExpr() = default; - //AssignmentExpr(const AssignmentExpr& in) = default; - //AssignmentExpr& operator=(const AssignmentExpr& in) = default; OperationMaster& mM; const OpClass& mSec; public: - static size_t layer() { return 0; } static constexpr size_t LAYER = 0; static constexpr size_t SIZE = OpClass::SIZE; @@ -103,7 +84,6 @@ namespace MultiArrayTools AssignmentExpr(const AssignmentExpr& in) = default; AssignmentExpr(AssignmentExpr&& in) = default; - //AssignmentExpr& operator=(const AssignmentExpr&& in) = default; inline void operator()(size_t start = 0) const; inline void operator()(size_t start, ExtType last) const; @@ -118,25 +98,16 @@ namespace MultiArrayTools typedef typename MultiRange::IndexType IndexType; typedef MBlock bType; - static size_t rootNum() { return 1; } - OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, std::shared_ptr& index); OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, std::shared_ptr& index, const IndexInfo* blockIndex); - - //MBlock& get(); - //const Block& get() const; inline void set(size_t pos, T val) { mDataPtr[pos] = val; } inline void add(size_t pos, T val) { mDataPtr[pos] += val; } inline T get(size_t pos) const; - //inline const T& get(size_t pos) const; - - //std::vector block(const IndexInfo* blockIndex, bool init = false) const; - //const OperationMaster& block() const; private: @@ -162,21 +133,14 @@ namespace MultiArrayTools typedef OperationTemplate > OT; typedef ContainerRange CRange; typedef typename CRange::IndexType IndexType; - typedef Block bType; - static size_t rootNum() { return 1; } - static const size_t SIZE = 1; + static constexpr size_t SIZE = 1; ConstOperationRoot(const MultiArrayBase& ma, const std::shared_ptr&... indices); - //const Block& get() const; - template inline T get(ET pos) const; - - //std::vector block(const IndexInfo* blockIndex, bool init = false) const; - //const ConstOperationRoot& block() const; MExt rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype @@ -193,7 +157,6 @@ namespace MultiArrayTools const T* mDataPtr; std::shared_ptr mIndex; IndexInfo mIInfo; - //mutable bType mBlock; }; template @@ -206,29 +169,17 @@ namespace MultiArrayTools typedef OperationTemplate > OT; typedef ContainerRange CRange; typedef typename CRange::IndexType IndexType; - typedef MBlock bType; - static size_t rootNum() { return 1; } - static const size_t SIZE = 1; + static constexpr size_t SIZE = 1; OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices); template OperationMaster operator=(const OpClass& in); - - //const MBlock& get() const; - //MBlock& get(); - - //template - //inline const T& get(const ET& pos) const; template inline T get(ET pos) const; - - //OperationRoot& set(const IndexInfo* blockIndex); - //std::vector block(const IndexInfo* blockIndex, bool init = false) const; - //const OperationRoot& block() const; MExt rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype @@ -245,8 +196,6 @@ namespace MultiArrayTools T* mDataPtr; std::shared_ptr mIndex; IndexInfo mIInfo; - //mutable bType mBlock; - //const IndexInfo* mBlockII; // predefine to save time }; template @@ -298,29 +247,20 @@ namespace MultiArrayTools typedef OperationBase OB; typedef OperationTemplate > OT; typedef OpFunction F; - typedef BlockResult bType; - - static size_t rootNum() { return sumRootNum(); } + static constexpr size_t SIZE = RootSum::SIZE; private: std::tuple mOps; - mutable bType mRes; public: typedef decltype(PackNum::mkSteps(0, mOps)) ETuple; - //typedef decltype(PackNum::template mkLoopType) LType; - + Operation(const Ops&... ops); - //const BlockResult& get() const; - template inline T get(ET pos) const; - //std::vector block(const IndexInfo* blockIndex, bool init = false) const; - //const Operation& block() const; - auto rootSteps(std::intptr_t iPtrNum = 0) const // nullptr for simple usage with decltype -> decltype(PackNum::mkSteps(iPtrNum, mOps)); @@ -337,30 +277,22 @@ namespace MultiArrayTools typedef T value_type; typedef OperationTemplate > OT; - typedef BlockResult bType; - static size_t rootNum() { return typename Op::rootNum(); } - static const size_t SIZE = Op::SIZE; + static constexpr size_t SIZE = Op::SIZE; private: const Op& mOp; std::shared_ptr mInd; - mutable bType mRes; public: typedef decltype(mOp.rootSteps(0)) ETuple; Contraction(const Op& op, std::shared_ptr ind); - - //const BlockResult& get() const; template inline T get(ET pos) const; - //std::vector block(const IndexInfo* blockIndex, bool init = false) const; - //const Contraction& block() const; - auto rootSteps(std::intptr_t iPtrNum = 0) const // nullptr for simple usage with decltype -> decltype(mOp.rootSteps(iPtrNum)); @@ -380,53 +312,6 @@ namespace MultiArrayTools { using namespace MultiArrayHelper; } - - template - Block makeBlock(const T* vec, size_t stepSize, size_t blockSize) - { - return Block(vec, 0, blockSize, stepSize); - } - - template - MBlock makeBlock(T* vec, size_t stepSize, size_t blockSize) - { - return MBlock(vec, 0, blockSize, stepSize); - } - - // dont use this for now !! - template - std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, - const OpClass& second) - { - assert(0); // dont use this for now !! - std::vector > ivec; - seekIndexInst(ownIdx, ivec); - std::map, std::vector > mp; - - for(auto& xx: ivec){ - mp[xx] = second.block(xx); - } - // seek minimal number of VALUEs => guarantees absence of conflicting blocks - minimizeAppearanceOfType(mp, BlockType::VALUE); - // seek mininmal number of SPLITs => maximal vectorization possible - minimizeAppearanceOfType(mp, BlockType::SPLIT); - - return mp.begin()->first; - } - - template - const IndexInfo* seekBlockIndex(const IndexInfo* ownII, - const OpClass& second) - - { - const IndexInfo* ii = ownII; - while(ii->type() == IndexType::CONT or - ii->type() == IndexType::MULTI){ - ii = ii->getPtr(ii->dim()-1); - } - return ii; - } - /*************************** * OperationTemplate * @@ -481,14 +366,6 @@ namespace MultiArrayTools AssignmentExpr(OperationMaster& m, const OpClass& sec) : mM(m), mSec(sec) {} - /* - template - inline void OperationMaster::AssignmentExpr:: - operator()(size_t start) - { - - }*/ - template inline void OperationMaster::AssignmentExpr:: operator()(size_t start, ExtType last) const @@ -516,12 +393,6 @@ namespace MultiArrayTools mSecond(second), mArrayRef(ma), mDataPtr(mArrayRef.data()), mIndex(mkIndex(index)), mIInfo(*mIndex) { - //auto blockIndex = seekBlockIndex( &mIInfo, second); - //std::intptr_t blockIndexNum = blockIndex->getPtrNum(); - - //block(blockIndex, true); - //second.block(blockIndex, true); - performAssignment(0); } @@ -533,9 +404,6 @@ namespace MultiArrayTools mSecond(second), mArrayRef(ma), mDataPtr(mArrayRef.data()), mIndex(mkIndex(index)), mIInfo(*mIndex) { - //std::intptr_t blockIndexNum = blockIndex->getPtrNum(); - //second.block(blockIndex, true); - performAssignment(0); } @@ -555,72 +423,19 @@ namespace MultiArrayTools template void OperationMaster::performAssignment(std::intptr_t blockIndexNum) { -#define XX_USE_NEW_LOOP_ROUTINE_XX -#ifdef XX_USE_NEW_LOOP_ROUTINE_XX - // === N E W === AssignmentExpr ae(*this, mSecond); // Expression to be executed within loop - //const auto hiddenLoop = mSecond.loop(AssignmentExpr(*this, mSecond)); // hidden loop within 'mSecond' e.g. contractions - auto loop = mIndex->ifor - ( mSecond.template loop - ( std::move(ae) ) ); // init overall loop(s) + const auto loop = mSecond.template loopifor(ae))>( mIndex->ifor(ae) ); + // hidden Loops outside ! -> auto vectorizable loop(); // execute overall loop(s) and so internal hidden loops and so the inherited expressions -#else - // === O L D === - for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndexNum) ){ - block(); - get() = mSecond.get(); - } -#endif } - /* - template - MBlock& OperationMaster::get() - { - return mBlock; - } - - template - const Block& OperationMaster::get() const - { - return mBlock; - } - */ + template inline T OperationMaster::get(size_t pos) const { - //assert(pos < mIndex->max()); - //if(pos >= mIndex->max()) { VCHECK(pos); VCHECK(mIndex->max()); assert(0); } - //VCHECK(pos); return mDataPtr[pos]; } - /* - template - inline const T& OperationMaster::get(size_t pos) const - { - //VCHECK(pos); - return mDataPtr[pos]; - }*/ - /* - template - std::vector OperationMaster::block(const IndexInfo* blockIndex, bool init) const - { - std::vector btv(1, getBlockType( &mIInfo, blockIndex, true) ); - if(init){ - mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); - } - return btv; - } - - - template - const OperationMaster& OperationMaster::block() const - { - mBlock.set( mIndex->pos() ); - return *this; - } - */ - + /**************************** * ConstOperationRoot * ****************************/ @@ -629,7 +444,6 @@ namespace MultiArrayTools ConstOperationRoot:: ConstOperationRoot(const MultiArrayBase& ma, const std::shared_ptr&... indices) : - //OperationTemplate >(this), mArrayRef(ma), mDataPtr(mArrayRef.data()), mIndex( mkIndex(ma,indices...) ), mIInfo(*mIndex) {} @@ -644,38 +458,14 @@ namespace MultiArrayTools (*mIndex)(indices...); return i; } - /* - template - const Block& ConstOperationRoot::get() const - { - block(); - return mBlock; - } - */ + template template inline T ConstOperationRoot::get(ET pos) const { return mDataPtr[pos.val()]; } - /* - template - std::vector ConstOperationRoot::block(const IndexInfo* blockIndex, bool init) const - { - std::vector btv(1, getBlockType( &mIInfo, blockIndex, true) ); - if(init){ - mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); - } - return btv; - } - - template - const ConstOperationRoot& ConstOperationRoot::block() const - { - mBlock.set( (*mIndex)().pos() ); - return *this; - } - */ + template MExt ConstOperationRoot::rootSteps(std::intptr_t iPtrNum) const { @@ -698,10 +488,8 @@ namespace MultiArrayTools OperationRoot:: OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices) : - //OperationTemplate >(this), mArrayRef(ma), mDataPtr(mArrayRef.data()), mIndex( mkIndex( ma, indices... ) ), mIInfo(*mIndex) - //mBlockII(nullptr) {} template @@ -719,67 +507,16 @@ namespace MultiArrayTools template OperationMaster OperationRoot::operator=(const OpClass& in) { - //if(mBlockII != nullptr){ - // return OperationMaster(mArrayRef, in, mIndex, mBlockII); - //} - //else { - return OperationMaster(mArrayRef, in, mIndex); - //} + return OperationMaster(mArrayRef, in, mIndex); } - /* - template - const MBlock& OperationRoot::get() const - { - block(); - return mBlock; - } - - template - MBlock& OperationRoot::get() - { - block(); - return mBlock; - } - - template - template - inline const T& OperationRoot::get(const ET& pos) const - { - return mDataPtr[pos.val()]; - } - */ + template template inline T OperationRoot::get(ET pos) const { return mDataPtr[pos.val()]; } - /* - template - OperationRoot& - OperationRoot::set(const IndexInfo* blockIndex) - { - mBlockII = blockIndex; - return *this; - } - - template - std::vector OperationRoot::block(const IndexInfo* blockIndex, bool init) const - { - std::vector btv(1, getBlockType( &mIInfo, blockIndex, true) ); - if(init){ - mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); - } - return btv; - } - - template - const OperationRoot& OperationRoot::block() const - { - mBlock.set( (*mIndex)().pos() ); - return *this; - } - */ + template MExt OperationRoot::rootSteps(std::intptr_t iPtrNum) const { @@ -799,16 +536,8 @@ namespace MultiArrayTools template Operation::Operation(const Ops&... ops) : - //OperationTemplate >(this), mOps(ops...) {} - /* - template - const BlockResult& Operation::get() const - { - PackNum::template unpackArgs(mRes, mOps); - return mRes; - } - */ + template template inline T Operation::get(ET pos) const @@ -817,26 +546,7 @@ namespace MultiArrayTools return PackNum:: template mkOpExpr(pos, mOps); } - /* - template - std::vector Operation::block(const IndexInfo* blockIndex, bool init) const - { - std::vector btv; - PackNum::makeBlockTypeVec(btv, mOps, blockIndex, init); - if(init){ - mRes.init(blockIndex->max()); - } - return btv; - } - - template - const Operation& Operation::block() const - { - //mBlock.set( mIndex->pos() ); - return *this; - } - */ template auto Operation::rootSteps(std::intptr_t iPtrNum) const -> decltype(PackNum::mkSteps(iPtrNum, mOps)) @@ -859,20 +569,9 @@ namespace MultiArrayTools template Contraction::Contraction(const Op& op, std::shared_ptr ind) : - //OperationTemplate >(this), mOp(op), mInd(ind) {} - /* - template - const BlockResult& Contraction::get() const - { - BlockBinaryOpSelf,decltype(mOp.get())> f(mRes); - for(*mInd = 0; mInd->pos() != mInd->max(); ++(*mInd)){ - f(mOp.get()); - } - return mRes; - } - */ + // forward loop !!!! template template @@ -880,22 +579,7 @@ namespace MultiArrayTools { return mOp.template get(pos); } - /* - template - std::vector Contraction::block(const IndexInfo* blockIndex, bool init) const - { - if(init){ - mRes.init(blockIndex->max()); - } - return mOp.block(blockIndex, init); - } - - template - const Contraction& Contraction::block() const - { - return *this; - } - */ + template auto Contraction::rootSteps(std::intptr_t iPtrNum) const -> decltype(mOp.rootSteps(iPtrNum))