diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 5bdbf44..a4d399a 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -166,8 +166,8 @@ namespace MultiArrayTools MBlock& get(); const Block& get() const; - T& get(size_t pos); - const T& get(size_t pos) const; + inline T& get(size_t pos); + inline const T& get(size_t pos) const; std::vector block(const IndexInfo* blockIndex, bool init = false) const; const OperationMaster& block() const; @@ -178,7 +178,7 @@ namespace MultiArrayTools void performAssignment(std::intptr_t blockIndexNum); OpClass const& mSecond; MutableMultiArrayBase& mArrayRef; - T* mData; + std::vector& mData; std::shared_ptr mIndex; IndexInfo mIInfo; mutable bType mBlock; @@ -224,7 +224,7 @@ namespace MultiArrayTools const std::shared_ptr&... indices); MultiArrayBase const& mArrayRef; - const T* mData; + const std::vector& mData; std::shared_ptr mIndex; IndexInfo mIInfo; mutable bType mBlock; @@ -276,7 +276,7 @@ namespace MultiArrayTools const std::shared_ptr&... indices); MutableMultiArrayBase& mArrayRef; - T* mData; + std::vector& mData; std::shared_ptr mIndex; IndexInfo mIInfo; mutable bType mBlock; @@ -399,7 +399,7 @@ namespace MultiArrayTools -> decltype(mOp.rootSteps(iPtrNum)); template - auto loop(Expr&& exp) const -> decltype(mInd->ifor(exp))&&; + auto loop(Expr&& exp) const -> decltype(mInd->iforh(exp))&&; }; } @@ -547,7 +547,7 @@ namespace MultiArrayTools OperationMaster:: OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, std::shared_ptr& index) : - mSecond(second), mArrayRef(ma), mData(mArrayRef.data()), + mSecond(second), mArrayRef(ma), mData(mArrayRef.datav()), mIndex(mkIndex(index)), mIInfo(*mIndex) { auto blockIndex = seekBlockIndex( &mIInfo, second); @@ -564,7 +564,7 @@ namespace MultiArrayTools OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, std::shared_ptr& index, const IndexInfo* blockIndex) : - mSecond(second), mArrayRef(ma), mData(mArrayRef.data()), + mSecond(second), mArrayRef(ma), mData(mArrayRef.datav()), mIndex(mkIndex(index)), mIInfo(*mIndex) { std::intptr_t blockIndexNum = blockIndex->getPtrNum(); @@ -620,16 +620,18 @@ namespace MultiArrayTools } template - T& OperationMaster::get(size_t pos) + inline T& OperationMaster::get(size_t pos) { - VCHECK(pos); + //assert(pos < mIndex->max()); + //if(pos >= mIndex->max()) { VCHECK(pos); VCHECK(mIndex->max()); assert(0); } + //VCHECK(pos); return mData[pos]; } template - const T& OperationMaster::get(size_t pos) const + inline const T& OperationMaster::get(size_t pos) const { - VCHECK(pos); + //VCHECK(pos); return mData[pos]; } @@ -661,7 +663,7 @@ namespace MultiArrayTools ConstOperationRoot(const MultiArrayBase& ma, const std::shared_ptr&... indices) : //OperationTemplate >(this), - mArrayRef(ma), mData(mArrayRef.data()), + mArrayRef(ma), mData(mArrayRef.datav()), mIndex( mkIndex(ma,indices...) ), mIInfo(*mIndex) {} @@ -730,7 +732,7 @@ namespace MultiArrayTools OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices) : //OperationTemplate >(this), - mArrayRef(ma), mData(mArrayRef.data()), + mArrayRef(ma), mData(mArrayRef.datav()), mIndex( mkIndex( ma, indices... ) ), mIInfo(*mIndex), mBlockII(nullptr) {} @@ -939,10 +941,10 @@ namespace MultiArrayTools template template - auto Contraction::loop(Expr&& exp) const -> decltype(mInd->ifor(exp))&& + auto Contraction::loop(Expr&& exp) const -> decltype(mInd->iforh(exp))&& { - typedef decltype(mInd->ifor(exp)) LType; - LType&& loop = mInd->ifor(exp); + typedef decltype(mInd->iforh(exp)) LType; + LType&& loop = mInd->iforh(exp); return std::forward( loop ); } diff --git a/src/ranges/container_range.h b/src/ranges/container_range.h index 4998e59..8d1240c 100644 --- a/src/ranges/container_range.h +++ b/src/ranges/container_range.h @@ -92,7 +92,11 @@ namespace MultiArrayTools template auto ifor(Exprs&& exs) const -> decltype(RPackNum::mkFor(mIPack, exs)); - + + template + auto iforh(Exprs&& exs) const + -> decltype(RPackNum::mkForh(mIPack, exs)); + }; @@ -379,6 +383,14 @@ namespace MultiArrayTools return RPackNum::mkFor(mIPack, exs); } + template + template + auto ContainerIndex::iforh(Exprs&& exs) const + -> decltype(RPackNum::mkForh(mIPack, exs)) + { + return RPackNum::mkForh(mIPack, exs); + } + /***************************** * ContainerRangeFactory * diff --git a/src/ranges/index_base.h b/src/ranges/index_base.h index b5287d1..ab32ee3 100644 --- a/src/ranges/index_base.h +++ b/src/ranges/index_base.h @@ -77,7 +77,11 @@ namespace MultiArrayTools template auto ifor(const Expr&& ex) const -> decltype(THIS().template ifor(ex)) { return THIS().template ifor(ex); } - + + template + auto iforh(const Expr&& ex) const -> decltype(THIS().template iforh(ex)) + { return THIS().template iforh(ex); } + private: friend I; diff --git a/src/ranges/multi_range.h b/src/ranges/multi_range.h index cc55e8b..82e0f4a 100644 --- a/src/ranges/multi_range.h +++ b/src/ranges/multi_range.h @@ -108,6 +108,11 @@ namespace MultiArrayTools template auto ifor(Exprs&& exs) const -> decltype(RPackNum::mkFor(mIPack, exs)); + + template + auto iforh(Exprs&& exs) const + -> decltype(RPackNum::mkForh(mIPack, exs)); + }; /************************* @@ -422,7 +427,15 @@ namespace MultiArrayTools { return RPackNum::mkFor(mIPack, exs); } - + + template + template + auto MultiIndex::iforh(Exprs&& exs) const + -> decltype(RPackNum::mkForh(mIPack, exs)) + { + return RPackNum::mkForh(mIPack, exs); + } + /************************* * MultiRangeFactory * *************************/ diff --git a/src/ranges/rpack_num.h b/src/ranges/rpack_num.h index 617fb75..c49c160 100644 --- a/src/ranges/rpack_num.h +++ b/src/ranges/rpack_num.h @@ -235,13 +235,22 @@ namespace MultiArrayHelper template static auto mkFor(const IndexPack& ipack, Exprs&& exs) - -> decltype(std::get::value-N>(ipack) + -> decltype(std::get::value-N-1>(ipack) ->ifor( RPackNum::mkFor(ipack, exs) ) ) { - return std::get::value-N>(ipack) + return std::get::value-N-1>(ipack) ->ifor( RPackNum::mkFor(ipack, exs) ); } + template + static auto mkForh(const IndexPack& ipack, Exprs&& exs) + -> decltype(std::get::value-N-1>(ipack) + ->iforh( RPackNum::mkForh(ipack, exs) ) ) + { + return std::get::value-N-1>(ipack) + ->iforh( RPackNum::mkForh(ipack, exs) ); + } + }; @@ -412,6 +421,15 @@ namespace MultiArrayHelper { return std::get::value-1>(ipack)->ifor(exs); } + + template + static auto mkForh(const IndexPack& ipack, Exprs&& exs) + -> decltype(std::get::value-1>(ipack) + ->iforh(exs) ) + { + return std::get::value-1>(ipack)->iforh(exs); + } + }; diff --git a/src/ranges/single_range.h b/src/ranges/single_range.h index be532e0..51f9e31 100644 --- a/src/ranges/single_range.h +++ b/src/ranges/single_range.h @@ -68,6 +68,11 @@ namespace MultiArrayTools template auto ifor(Expr&& ex) const -> For,Expr>; + + template + auto iforh(Expr&& ex) const + -> For,Expr,ForType::HIDDEN>; + }; template @@ -257,6 +262,15 @@ namespace MultiArrayTools return For,Expr>(this, std::forward( ex )); } + template + template + auto SingleIndex::iforh(Expr&& ex) const + -> For,Expr,ForType::HIDDEN> + { + //static const size_t LAYER = typename Expr::LAYER; + return For,Expr,ForType::HIDDEN>(this, std::forward( ex )); + } + /******************** * SingleRange * diff --git a/src/tests/op_perf_test.cc b/src/tests/op_perf_test.cc index db593c6..4aec260 100644 --- a/src/tests/op_perf_test.cc +++ b/src/tests/op_perf_test.cc @@ -183,7 +183,7 @@ namespace { for(size_t a = 0; a != 4; ++a){ for(size_t b = 0; b != 4; ++b){ for(size_t c = 0; c != 4; ++c){ - const size_t sidx = d*4*4*4*4*4*4*4 + a*5*4*4*4*4 + b*5*4*4*4 + c*5*4 + p; + const size_t sidx = d*4*4*4*4*4*4*4 + a*5*4*4*4*4*4 + b*5*4*4*4 + + c*5*4 + p; vres[tidx] += data[sidx]; } } diff --git a/src/xfor/for_type.h b/src/xfor/for_type.h new file mode 100644 index 0000000..0d2deda --- /dev/null +++ b/src/xfor/for_type.h @@ -0,0 +1,18 @@ + +#ifndef __for_type_h__ +#define __for_type_h__ + +namespace MultiArrayHelper +{ + + + enum class ForType { + DEFAULT = 0, + HIDDEN = 1 + }; + + +} // end namespace MultiArrayHelper + + +#endif diff --git a/src/xfor/xfor.h b/src/xfor/xfor.h index 6fb3412..d8d4fa0 100644 --- a/src/xfor/xfor.h +++ b/src/xfor/xfor.h @@ -6,14 +6,33 @@ #include #include #include "xfor/for_utils.h" +#include "xfor/for_type.h" namespace MultiArrayHelper { // 'HIDDEN FOR' CLASS for nested for loops in contractions a.s.o. // (NO COUNTING OF MASTER POSITION !!!!!) + + template + struct PosForward + { + static inline size_t value(size_t last, size_t max, size_t pos) + { + return last * max + pos; + } + }; + + template <> + struct PosForward + { + static inline size_t value(size_t last, size_t max, size_t pos) + { + return last; + } + }; - template + template class For { private: @@ -56,7 +75,7 @@ namespace MultiArrayHelper template <> size_t exceptMax<1>(size_t max) { return 1; } - + } // namespace MultiArrayHelper /* ========================= * @@ -68,48 +87,60 @@ namespace MultiArrayHelper namespace MultiArrayHelper { - template - For::For(const std::shared_ptr& indPtr, + template + For::For(const std::shared_ptr& indPtr, Expr&& expr) : mIndPtr(indPtr.get()), mExpr(expr), - mExt(expr.rootSteps( reinterpret_cast( mIndPtr.get() ))) { assert(mIndPtr != nullptr); } + mExt(expr.rootSteps( reinterpret_cast( mIndPtr.get() ))) + { + assert(mIndPtr != nullptr); + //VCHECK(mIndPtr->id()); + //VCHECK(mIndPtr->max()); + } - template - For::For(const IndexClass* indPtr, + template + For::For(const IndexClass* indPtr, Expr&& expr) : mIndPtr(indPtr), mExpr(std::forward( expr )), - mExt(expr.rootSteps( reinterpret_cast( mIndPtr ) )) { assert(mIndPtr != nullptr); } + mExt(expr.rootSteps( reinterpret_cast( mIndPtr ) )) + { + assert(mIndPtr != nullptr); + //VCHECK(mIndPtr->id()); + //VCHECK(mIndPtr->max()); + } - template - inline void For::operator()(size_t mlast, - const ETuple& last) const + template + inline void For::operator()(size_t mlast, + const ETuple& last) const { auto& ind = *mIndPtr; - std::cout << mIndPtr << std::endl; + //std::cout << mIndPtr << std::endl; const size_t max = ind.max(); // blocking for(size_t pos = ind.pos(); pos != max; ++pos){ - const size_t mnpos = mlast * max + pos; + //const size_t mnpos = mlast * max + pos; + const size_t mnpos = PosForward::value(mlast, max, pos); const ETuple npos = std::move( XFPackNum::mkPos(pos, mExt, last) ); mExpr(mnpos, npos); } } - template - inline void For::operator()(size_t mlast) const + template + inline void For::operator()(size_t mlast) const { const ETuple last; auto& ind = *mIndPtr; - std::cout << mIndPtr << std::endl; + //std::cout << mIndPtr << std::endl; const size_t max = ind.max(); // blocking for(size_t pos = ind.pos(); pos != max; ++pos){ - const size_t mnpos = mlast * max + pos; + //const size_t mnpos = mlast * max + pos; + const size_t mnpos = PosForward::value(mlast, max, pos); const ETuple npos = std::move( XFPackNum::mkPos(pos, mExt, last) ); mExpr(mnpos, npos); } } - template - typename For::ETuple For::rootSteps(std::intptr_t iPtrNum) const + template + typename For::ETuple For::rootSteps(std::intptr_t iPtrNum) const { return mExpr.rootSteps(iPtrNum); }