loop unrolling seeems to work now

This commit is contained in:
Christian Zimmermann 2018-02-13 16:54:13 +01:00
parent 4d8d4d31f6
commit adc25abcaf
8 changed files with 81 additions and 28 deletions

View file

@ -130,9 +130,10 @@ namespace MultiArrayTools
//MBlock<T>& get(); //MBlock<T>& get();
//const Block<T>& get() const; //const Block<T>& get() const;
inline void set(size_t pos, T val) { mDataPtr[pos] = val }; inline void set(size_t pos, T val) { mDataPtr[pos] = val; }
inline T get(size_t pos); inline void add(size_t pos, T val) { mDataPtr[pos] += val; }
inline const T& get(size_t pos) const; inline T get(size_t pos) const;
//inline const T& get(size_t pos) const;
//std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const; //std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const;
//const OperationMaster& block() const; //const OperationMaster& block() const;
@ -172,7 +173,7 @@ namespace MultiArrayTools
//const Block<T>& get() const; //const Block<T>& get() const;
template <class ET> template <class ET>
inline const T& get(const ET& pos) const; inline T get(ET pos) const;
//std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const; //std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const;
//const ConstOperationRoot& block() const; //const ConstOperationRoot& block() const;
@ -219,11 +220,11 @@ namespace MultiArrayTools
//const MBlock<T>& get() const; //const MBlock<T>& get() const;
//MBlock<T>& get(); //MBlock<T>& get();
template <class ET> //template <class ET>
inline const T& get(const ET& pos) const; //inline const T& get(const ET& pos) const;
template <class ET> template <class ET>
inline T& get(const ET& pos); inline T get(ET pos) const;
//OperationRoot& set(const IndexInfo* blockIndex); //OperationRoot& set(const IndexInfo* blockIndex);
//std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const; //std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const;
@ -315,7 +316,7 @@ namespace MultiArrayTools
//const BlockResult<T>& get() const; //const BlockResult<T>& get() const;
template <class ET> template <class ET>
inline T get(const ET& pos) const; inline T get(ET pos) const;
//std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const; //std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const;
//const Operation& block() const; //const Operation& block() const;
@ -355,7 +356,7 @@ namespace MultiArrayTools
//const BlockResult<T>& get() const; //const BlockResult<T>& get() const;
template <class ET> template <class ET>
inline T get(const ET& pos) const; inline T get(ET pos) const;
//std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const; //std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const;
//const Contraction& block() const; //const Contraction& block() const;
@ -492,7 +493,7 @@ namespace MultiArrayTools
inline void OperationMaster<T,OpClass,Ranges...>::AssignmentExpr:: inline void OperationMaster<T,OpClass,Ranges...>::AssignmentExpr::
operator()(size_t start, ExtType last) const operator()(size_t start, ExtType last) const
{ {
mM.get(start) += mSec.template get<ExtType>(last); mM.add(start, mSec.template get<ExtType>(last) );
} }
template <typename T, class OpClass, class... Ranges> template <typename T, class OpClass, class... Ranges>
@ -585,7 +586,7 @@ namespace MultiArrayTools
} }
*/ */
template <typename T, class OpClass, class... Ranges> template <typename T, class OpClass, class... Ranges>
inline T& OperationMaster<T,OpClass,Ranges...>::get(size_t pos) inline T OperationMaster<T,OpClass,Ranges...>::get(size_t pos) const
{ {
//assert(pos < mIndex->max()); //assert(pos < mIndex->max());
//if(pos >= mIndex->max()) { VCHECK(pos); VCHECK(mIndex->max()); assert(0); } //if(pos >= mIndex->max()) { VCHECK(pos); VCHECK(mIndex->max()); assert(0); }
@ -593,12 +594,13 @@ namespace MultiArrayTools
return mDataPtr[pos]; return mDataPtr[pos];
} }
/*
template <typename T, class OpClass, class... Ranges> template <typename T, class OpClass, class... Ranges>
inline const T& OperationMaster<T,OpClass,Ranges...>::get(size_t pos) const inline const T& OperationMaster<T,OpClass,Ranges...>::get(size_t pos) const
{ {
//VCHECK(pos); //VCHECK(pos);
return mDataPtr[pos]; return mDataPtr[pos];
} }*/
/* /*
template <typename T, class OpClass, class... Ranges> template <typename T, class OpClass, class... Ranges>
std::vector<BTSS> OperationMaster<T,OpClass,Ranges...>::block(const IndexInfo* blockIndex, bool init) const std::vector<BTSS> OperationMaster<T,OpClass,Ranges...>::block(const IndexInfo* blockIndex, bool init) const
@ -652,7 +654,7 @@ namespace MultiArrayTools
*/ */
template <typename T, class... Ranges> template <typename T, class... Ranges>
template <class ET> template <class ET>
inline const T& ConstOperationRoot<T,Ranges...>::get(const ET& pos) const inline T ConstOperationRoot<T,Ranges...>::get(ET pos) const
{ {
return mDataPtr[pos.val()]; return mDataPtr[pos.val()];
} }
@ -738,17 +740,17 @@ namespace MultiArrayTools
block(); block();
return mBlock; return mBlock;
} }
*/
template <typename T, class... Ranges> template <typename T, class... Ranges>
template <class ET> template <class ET>
inline const T& OperationRoot<T,Ranges...>::get(const ET& pos) const inline const T& OperationRoot<T,Ranges...>::get(const ET& pos) const
{ {
return mDataPtr[pos.val()]; return mDataPtr[pos.val()];
} }
*/
template <typename T, class... Ranges> template <typename T, class... Ranges>
template <class ET> template <class ET>
inline T& OperationRoot<T,Ranges...>::get(const ET& pos) inline T OperationRoot<T,Ranges...>::get(ET pos) const
{ {
return mDataPtr[pos.val()]; return mDataPtr[pos.val()];
} }
@ -809,7 +811,7 @@ namespace MultiArrayTools
*/ */
template <typename T, class OpFunction, class... Ops> template <typename T, class OpFunction, class... Ops>
template <class ET> template <class ET>
inline T Operation<T,OpFunction,Ops...>::get(const ET& pos) const inline T Operation<T,OpFunction,Ops...>::get(ET pos) const
{ {
typedef std::tuple<Ops const&...> OpTuple; typedef std::tuple<Ops const&...> OpTuple;
return PackNum<sizeof...(Ops)-1>:: return PackNum<sizeof...(Ops)-1>::
@ -874,7 +876,7 @@ namespace MultiArrayTools
// forward loop !!!! // forward loop !!!!
template <typename T, class Op, class IndexType> template <typename T, class Op, class IndexType>
template <class ET> template <class ET>
inline T Contraction<T,Op,IndexType>::get(const ET& pos) const inline T Contraction<T,Op,IndexType>::get(ET pos) const
{ {
return mOp.template get<ET>(pos); return mOp.template get<ET>(pos);
} }

View file

@ -158,7 +158,9 @@ namespace MultiArrayTools
friend ContainerRangeFactory<Ranges...>; friend ContainerRangeFactory<Ranges...>;
static const bool defaultable = false; static constexpr bool defaultable = false;
static constexpr size_t ISSTATIC = SubProp<Ranges...>::ISSTATIC;
static constexpr size_t SIZE = SubProp<Ranges...>::SIZE;
}; };
} // end namespace MultiArrayTools } // end namespace MultiArrayTools

View file

@ -178,7 +178,9 @@ namespace MultiArrayTools
friend MultiRangeFactory<Ranges...>; friend MultiRangeFactory<Ranges...>;
static const bool defaultable = false; static constexpr bool defaultable = false;
static constexpr size_t ISSTATIC = SubProp<Ranges...>::ISSTATIC;
static constexpr size_t SIZE = SubProp<Ranges...>::SIZE;
}; };
} }

View file

@ -45,8 +45,11 @@ namespace MultiArrayTools
friend SingleRangeFactory<size_t,SpaceType::SPIN>; friend SingleRangeFactory<size_t,SpaceType::SPIN>;
static const bool defaultable = true; static constexpr bool defaultable = true;
static const size_t mSpinNum = 4; static constexpr size_t mSpinNum = 4;
static constexpr size_t ISSTATIC = 1;
static constexpr size_t SIZE = mSpinNum;
static SingleRangeFactory<size_t, SpaceType::SPIN> factory() static SingleRangeFactory<size_t, SpaceType::SPIN> factory()
{ return SingleRangeFactory<size_t, SpaceType::SPIN>(); } { return SingleRangeFactory<size_t, SpaceType::SPIN>(); }

View file

@ -23,6 +23,20 @@ namespace MultiArrayHelper
return Index1::totalDim() * mkTotalDim<Index2,Indices...>(); return Index1::totalDim() * mkTotalDim<Index2,Indices...>();
} }
template <class RangeType, class... Ranges>
struct SubProp
{
static constexpr size_t ISSTATIC = RangeType::ISSTATIC & SubProp<Ranges...>::ISSTATIC;
static constexpr size_t SIZE = RangeType::SIZE * SubProp<Ranges...>::SIZE;
};
template <>
struct SubProp<void>
{
static constexpr size_t ISSTATIC = 1;
static constexpr size_t SIZE = 1;
};
template <size_t N> template <size_t N>
struct RPackNum struct RPackNum

View file

@ -26,14 +26,14 @@ namespace MultiArrayTools
typedef IndexInterface<SingleIndex<U,TYPE>,U> IB; typedef IndexInterface<SingleIndex<U,TYPE>,U> IB;
typedef U MetaType; typedef U MetaType;
typedef SingleRange<U,TYPE> RangeType; typedef SingleRange<U,TYPE> RangeType;
//DEFAULT_MEMBERS_X(SingleIndex); //DEFAULT_MEMBERS_X(SingleIndex);
SingleIndex(const std::shared_ptr<SingleRange<U,TYPE> >& range); SingleIndex(const std::shared_ptr<SingleRange<U,TYPE> >& range);
static IndexType sType() { return IndexType::SINGLE; } static IndexType sType() { return IndexType::SINGLE; }
static size_t totalDim() { return 1; } static size_t totalDim() { return 1; }
// ==== >>>>> STATIC POLYMORPHISM <<<<< ==== // ==== >>>>> STATIC POLYMORPHISM <<<<< ====
IndexType type() const; IndexType type() const;
@ -109,6 +109,9 @@ namespace MultiArrayTools
friend SingleRangeFactory<U,TYPE>; friend SingleRangeFactory<U,TYPE>;
static constexpr bool defaultable = false; static constexpr bool defaultable = false;
static constexpr size_t ISSTATIC = 0;
static constexpr size_t SIZE = -1;
protected: protected:

View file

@ -168,7 +168,9 @@ namespace {
std::clock_t begin = std::clock(); 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).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::clock_t end = std::clock();
std::cout << "MultiArray time: " << static_cast<double>( end - begin ) / CLOCKS_PER_SEC std::cout << "MultiArray time: " << static_cast<double>( end - begin ) / CLOCKS_PER_SEC
<< std::endl; << std::endl;
@ -176,6 +178,7 @@ namespace {
std::vector<double> vres(4*4); std::vector<double> vres(4*4);
std::clock_t begin2 = std::clock(); 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 d = 0; d != 4; ++d){
for(size_t p = 0; p != 4; ++p){ for(size_t p = 0; p != 4; ++p){
const size_t tidx = d*4 + p; const size_t tidx = d*4 + p;
@ -190,6 +193,7 @@ namespace {
} }
} }
} }
}
std::clock_t end2 = std::clock(); std::clock_t end2 = std::clock();
assert( xround(res1.at(mkts(0,0))) == xround(vres[0]) ); assert( xround(res1.at(mkts(0,0))) == xround(vres[0]) );

View file

@ -32,6 +32,26 @@ namespace MultiArrayHelper
return last; return last;
} }
}; };
template <size_t ISSTATIC>
struct ForBound
{
template <size_t BOUND>
static inline size_t bound(size_t bound)
{
return bound;
}
};
template <>
struct ForBound<1>
{
template <size_t BOUND>
static constexpr size_t bound(size_t bound)
{
return BOUND;
}
};
template <class IndexClass, class Expr, ForType FT = ForType::DEFAULT> template <class IndexClass, class Expr, ForType FT = ForType::DEFAULT>
class For class For
@ -79,7 +99,6 @@ namespace MultiArrayHelper
template <> template <>
size_t exceptMax<1>(size_t max) { return 1; } size_t exceptMax<1>(size_t max) { return 1; }
} // namespace MultiArrayHelper } // namespace MultiArrayHelper
/* ========================= * /* ========================= *
@ -118,7 +137,9 @@ namespace MultiArrayHelper
inline void For<IndexClass,Expr,FT>::operator()(size_t mlast, inline void For<IndexClass,Expr,FT>::operator()(size_t mlast,
ExtType last) const ExtType last) const
{ {
for(size_t pos = mSPos; pos != mMax; ++pos){ typedef typename IndexClass::RangeType RangeType;
for(size_t pos = 0u; pos != ForBound<RangeType::ISSTATIC>::template bound<RangeType::SIZE>(mMax); ++pos){
//for(size_t pos = mSPos; pos != mMax; ++pos){
const size_t mnpos = PosForward<FT>::value(mlast, mMax, pos); const size_t mnpos = PosForward<FT>::value(mlast, mMax, pos);
const ExtType npos = last + mExt*pos; const ExtType npos = last + mExt*pos;
mExpr(mnpos, npos); mExpr(mnpos, npos);
@ -128,8 +149,10 @@ namespace MultiArrayHelper
template <class IndexClass, class Expr, ForType FT> template <class IndexClass, class Expr, ForType FT>
inline void For<IndexClass,Expr,FT>::operator()(size_t mlast) const inline void For<IndexClass,Expr,FT>::operator()(size_t mlast) const
{ {
typedef typename IndexClass::RangeType RangeType;
const ExtType last; const ExtType last;
for(size_t pos = mSPos; pos != mMax; ++pos){ for(size_t pos = 0u; pos != ForBound<RangeType::ISSTATIC>::template bound<RangeType::SIZE>(mMax); ++pos){
//for(size_t pos = mSPos; pos != mMax; ++pos){
const size_t mnpos = PosForward<FT>::value(mlast, mMax, pos); const size_t mnpos = PosForward<FT>::value(mlast, mMax, pos);
const ExtType npos = last + mExt*pos; const ExtType npos = last + mExt*pos;
mExpr(mnpos, npos); mExpr(mnpos, npos);