From adc25abcaf8140d48f17a334a3051bec48ba829c Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 13 Feb 2018 16:54:13 +0100 Subject: [PATCH] loop unrolling seeems to work now --- src/multi_array_operation.h | 38 +++++++++++++++-------------- src/ranges/container_range.h | 4 ++- src/ranges/multi_range.h | 4 ++- src/ranges/range_types/spin_range.h | 7 ++++-- src/ranges/rpack_num.h | 14 +++++++++++ src/ranges/single_range.h | 7 ++++-- src/tests/op_perf_test.cc | 6 ++++- src/xfor/xfor.h | 29 +++++++++++++++++++--- 8 files changed, 81 insertions(+), 28 deletions(-) diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 12de4f2..6d86c0f 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -130,9 +130,10 @@ namespace MultiArrayTools //MBlock& get(); //const Block& get() const; - inline void set(size_t pos, T val) { mDataPtr[pos] = val }; - inline T get(size_t pos); - inline const T& get(size_t pos) 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; @@ -172,7 +173,7 @@ namespace MultiArrayTools //const Block& get() const; template - inline const T& get(const ET& pos) const; + inline T get(ET pos) const; //std::vector block(const IndexInfo* blockIndex, bool init = false) const; //const ConstOperationRoot& block() const; @@ -219,11 +220,11 @@ namespace MultiArrayTools //const MBlock& get() const; //MBlock& get(); - template - inline const T& get(const ET& pos) const; + //template + //inline const T& get(const ET& pos) const; template - inline T& get(const ET& pos); + inline T get(ET pos) const; //OperationRoot& set(const IndexInfo* blockIndex); //std::vector block(const IndexInfo* blockIndex, bool init = false) const; @@ -315,7 +316,7 @@ namespace MultiArrayTools //const BlockResult& get() const; template - inline T get(const ET& pos) const; + inline T get(ET pos) const; //std::vector block(const IndexInfo* blockIndex, bool init = false) const; //const Operation& block() const; @@ -355,7 +356,7 @@ namespace MultiArrayTools //const BlockResult& get() const; template - inline T get(const ET& pos) const; + inline T get(ET pos) const; //std::vector block(const IndexInfo* blockIndex, bool init = false) const; //const Contraction& block() const; @@ -492,7 +493,7 @@ namespace MultiArrayTools inline void OperationMaster::AssignmentExpr:: operator()(size_t start, ExtType last) const { - mM.get(start) += mSec.template get(last); + mM.add(start, mSec.template get(last) ); } template @@ -585,7 +586,7 @@ namespace MultiArrayTools } */ template - inline T& OperationMaster::get(size_t pos) + inline T OperationMaster::get(size_t pos) const { //assert(pos < mIndex->max()); //if(pos >= mIndex->max()) { VCHECK(pos); VCHECK(mIndex->max()); assert(0); } @@ -593,12 +594,13 @@ namespace MultiArrayTools 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 @@ -652,7 +654,7 @@ namespace MultiArrayTools */ template template - inline const T& ConstOperationRoot::get(const ET& pos) const + inline T ConstOperationRoot::get(ET pos) const { return mDataPtr[pos.val()]; } @@ -738,17 +740,17 @@ namespace MultiArrayTools block(); return mBlock; } - */ + template template inline const T& OperationRoot::get(const ET& pos) const { return mDataPtr[pos.val()]; } - + */ template template - inline T& OperationRoot::get(const ET& pos) + inline T OperationRoot::get(ET pos) const { return mDataPtr[pos.val()]; } @@ -809,7 +811,7 @@ namespace MultiArrayTools */ template template - inline T Operation::get(const ET& pos) const + inline T Operation::get(ET pos) const { typedef std::tuple OpTuple; return PackNum:: @@ -874,7 +876,7 @@ namespace MultiArrayTools // forward loop !!!! template template - inline T Contraction::get(const ET& pos) const + inline T Contraction::get(ET pos) const { return mOp.template get(pos); } diff --git a/src/ranges/container_range.h b/src/ranges/container_range.h index eb69649..f2f0c8c 100644 --- a/src/ranges/container_range.h +++ b/src/ranges/container_range.h @@ -158,7 +158,9 @@ namespace MultiArrayTools friend ContainerRangeFactory; - static const bool defaultable = false; + static constexpr bool defaultable = false; + static constexpr size_t ISSTATIC = SubProp::ISSTATIC; + static constexpr size_t SIZE = SubProp::SIZE; }; } // end namespace MultiArrayTools diff --git a/src/ranges/multi_range.h b/src/ranges/multi_range.h index 23dfb84..c6d27fe 100644 --- a/src/ranges/multi_range.h +++ b/src/ranges/multi_range.h @@ -178,7 +178,9 @@ namespace MultiArrayTools friend MultiRangeFactory; - static const bool defaultable = false; + static constexpr bool defaultable = false; + static constexpr size_t ISSTATIC = SubProp::ISSTATIC; + static constexpr size_t SIZE = SubProp::SIZE; }; } diff --git a/src/ranges/range_types/spin_range.h b/src/ranges/range_types/spin_range.h index 54ae06d..77be683 100644 --- a/src/ranges/range_types/spin_range.h +++ b/src/ranges/range_types/spin_range.h @@ -45,8 +45,11 @@ namespace MultiArrayTools friend SingleRangeFactory; - static const bool defaultable = true; - static const size_t mSpinNum = 4; + static constexpr bool defaultable = true; + static constexpr size_t mSpinNum = 4; + + static constexpr size_t ISSTATIC = 1; + static constexpr size_t SIZE = mSpinNum; static SingleRangeFactory factory() { return SingleRangeFactory(); } diff --git a/src/ranges/rpack_num.h b/src/ranges/rpack_num.h index d0e44c9..e41f3b3 100644 --- a/src/ranges/rpack_num.h +++ b/src/ranges/rpack_num.h @@ -23,6 +23,20 @@ namespace MultiArrayHelper return Index1::totalDim() * mkTotalDim(); } + + template + struct SubProp + { + static constexpr size_t ISSTATIC = RangeType::ISSTATIC & SubProp::ISSTATIC; + static constexpr size_t SIZE = RangeType::SIZE * SubProp::SIZE; + }; + + template <> + struct SubProp + { + static constexpr size_t ISSTATIC = 1; + static constexpr size_t SIZE = 1; + }; template struct RPackNum diff --git a/src/ranges/single_range.h b/src/ranges/single_range.h index 8bd3b4d..f85f7b4 100644 --- a/src/ranges/single_range.h +++ b/src/ranges/single_range.h @@ -26,14 +26,14 @@ namespace MultiArrayTools typedef IndexInterface,U> IB; typedef U MetaType; typedef SingleRange RangeType; - + //DEFAULT_MEMBERS_X(SingleIndex); SingleIndex(const std::shared_ptr >& range); static IndexType sType() { return IndexType::SINGLE; } static size_t totalDim() { return 1; } - + // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== IndexType type() const; @@ -109,6 +109,9 @@ namespace MultiArrayTools friend SingleRangeFactory; static constexpr bool defaultable = false; + static constexpr size_t ISSTATIC = 0; + static constexpr size_t SIZE = -1; + protected: diff --git a/src/tests/op_perf_test.cc b/src/tests/op_perf_test.cc index 4aec260..ab100d9 100644 --- a/src/tests/op_perf_test.cc +++ b/src/tests/op_perf_test.cc @@ -168,7 +168,9 @@ namespace { std::clock_t begin = std::clock(); //res1(delta, deltap).set(vdeltap) = ma(delta, alpha, alpha, beta, beta, gamma, gamma, deltap).c(mix); - res1(delta, deltap) = ma(delta, alpha, alpha, beta, beta, gamma, gamma, deltap).c(mix); + for(size_t i = 0; i != 1000; ++i){ + res1(delta, deltap) = ma(delta, alpha, alpha, beta, beta, gamma, gamma, deltap).c(mix); + } std::clock_t end = std::clock(); std::cout << "MultiArray time: " << static_cast( end - begin ) / CLOCKS_PER_SEC << std::endl; @@ -176,6 +178,7 @@ namespace { std::vector vres(4*4); std::clock_t begin2 = std::clock(); + for(size_t i = 0; i != 1000; ++i){ for(size_t d = 0; d != 4; ++d){ for(size_t p = 0; p != 4; ++p){ const size_t tidx = d*4 + p; @@ -190,6 +193,7 @@ namespace { } } } + } std::clock_t end2 = std::clock(); assert( xround(res1.at(mkts(0,0))) == xround(vres[0]) ); diff --git a/src/xfor/xfor.h b/src/xfor/xfor.h index 2277d1c..cec68a4 100644 --- a/src/xfor/xfor.h +++ b/src/xfor/xfor.h @@ -32,6 +32,26 @@ namespace MultiArrayHelper return last; } }; + + template + struct ForBound + { + template + static inline size_t bound(size_t bound) + { + return bound; + } + }; + + template <> + struct ForBound<1> + { + template + static constexpr size_t bound(size_t bound) + { + return BOUND; + } + }; template class For @@ -79,7 +99,6 @@ namespace MultiArrayHelper template <> size_t exceptMax<1>(size_t max) { return 1; } - } // namespace MultiArrayHelper /* ========================= * @@ -118,7 +137,9 @@ namespace MultiArrayHelper inline void For::operator()(size_t mlast, ExtType last) const { - for(size_t pos = mSPos; pos != mMax; ++pos){ + 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 ExtType npos = last + mExt*pos; mExpr(mnpos, npos); @@ -128,8 +149,10 @@ namespace MultiArrayHelper template inline void For::operator()(size_t mlast) const { + typedef typename IndexClass::RangeType RangeType; const ExtType last; - for(size_t pos = mSPos; pos != mMax; ++pos){ + 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 ExtType npos = last + mExt*pos; mExpr(mnpos, npos);