From f339778a59a3dcf786c96ea13c4041ee7a0d9027 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 12 Feb 2019 20:26:38 +0100 Subject: [PATCH 1/9] generalize single range (static size parameter, -> -1 if non-static size) --- CMakeLists.txt | 2 +- src/include/ranges/anonymous_range.h | 26 +- .../ranges/range_types/classic_range.h | 32 +- src/include/ranges/range_types/null_range.h | 34 +-- src/include/ranges/range_types/space_range.h | 32 +- src/include/ranges/range_types/spin_range.h | 32 +- src/include/ranges/rbase_def.h | 12 +- src/include/ranges/single_range.h | 273 ++++++++++-------- src/lib/ranges/anonymous_range.cc | 2 +- src/lib/ranges/range_types/classic_range.cc | 36 +-- src/lib/ranges/range_types/null_range.cc | 42 +-- src/lib/ranges/range_types/space_range.cc | 36 +-- src/lib/ranges/range_types/spin_range.cc | 38 +-- src/tests/op_perf_test.cc | 18 +- 14 files changed, 320 insertions(+), 295 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba65885..a66ce5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8) project(multi_array) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -Wpedantic -O3 -g -march=native -fopenmp") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -Wpedantic -O3 -g -march=native -fopenmp -funroll-loops -ftree-vectorize") enable_testing() diff --git a/src/include/ranges/anonymous_range.h b/src/include/ranges/anonymous_range.h index 4cfb980..0099258 100644 --- a/src/include/ranges/anonymous_range.h +++ b/src/include/ranges/anonymous_range.h @@ -14,12 +14,12 @@ namespace MultiArrayTools { - typedef SingleIndex AnonymousIndex; + typedef GenSingleIndex AnonymousIndex; //template //using SIZET = size_t; - typedef SingleRange AnonymousRange; + typedef GenSingleRange AnonymousRange; // NOT THREAD SAVE!! class AnonymousRangeFactory : public RangeFactoryBase @@ -53,7 +53,7 @@ namespace MultiArrayTools }; template <> - class SingleRange : public RangeInterface + class GenSingleRange : public RangeInterface { public: @@ -64,7 +64,7 @@ namespace MultiArrayTools typedef RangeBase RB; typedef typename RangeInterface::IndexType IndexType; - typedef SingleRange RangeType; + typedef GenSingleRange RangeType; typedef size_t MetaType; virtual size_t size() const final; @@ -105,16 +105,16 @@ namespace MultiArrayTools protected: - SingleRange() = default; - SingleRange(const AnonymousRange& in) = default; + GenSingleRange() = default; + GenSingleRange(const AnonymousRange& in) = default; template - SingleRange(const std::tuple...>& origs); + GenSingleRange(const std::tuple...>& origs); template - SingleRange(std::shared_ptr... origs); + GenSingleRange(std::shared_ptr... origs); - SingleRange(const std::vector>& origs); + GenSingleRange(const std::vector>& origs); size_t mSize = 1; bool mEmpty = true; @@ -203,7 +203,7 @@ namespace MultiArrayTools ***********************/ template - SingleRange::SingleRange(const std::tuple...>& origs) : + GenSingleRange::GenSingleRange(const std::tuple...>& origs) : RangeInterface() { RPackNum::RangesToVec( origs, mOrig ); @@ -214,7 +214,7 @@ namespace MultiArrayTools } template - SingleRange::SingleRange(std::shared_ptr... origs) : + GenSingleRange::GenSingleRange(std::shared_ptr... origs) : RangeInterface() { auto rst = std::make_tuple(origs...); @@ -226,13 +226,13 @@ namespace MultiArrayTools } template - std::shared_ptr SingleRange::fullsub(size_t num) const + std::shared_ptr GenSingleRange::fullsub(size_t num) const { return std::dynamic_pointer_cast( mOrig.at(num) ); } template - std::shared_ptr > SingleRange::scast(SIZET... sizes) const + std::shared_ptr > GenSingleRange::scast(SIZET... sizes) const { std::tuple...> rtp; RPackNum::resolveRangeType(mOrig, rtp, 0, sizes...); diff --git a/src/include/ranges/range_types/classic_range.h b/src/include/ranges/range_types/classic_range.h index 3a65fea..f9b4ed6 100644 --- a/src/include/ranges/range_types/classic_range.h +++ b/src/include/ranges/range_types/classic_range.h @@ -12,29 +12,29 @@ namespace MultiArrayTools { - typedef SingleIndex ClassicIndex; + typedef GenSingleIndex ClassicIndex; template <> - class SingleRangeFactory : public RangeFactoryBase + class GenSingleRangeFactory : public RangeFactoryBase { public: - typedef SingleRange oType; + typedef GenSingleRange oType; - SingleRangeFactory(size_t size = 0); + GenSingleRangeFactory(size_t size = 0); std::shared_ptr create(); }; template <> - class SingleRange : public RangeInterface + class GenSingleRange : public RangeInterface { public: typedef RangeBase RB; - typedef typename RangeInterface >::IndexType IndexType; - typedef SingleRange RangeType; + typedef typename RangeInterface >::IndexType IndexType; + typedef GenSingleRange RangeType; typedef size_t MetaType; - typedef SingleRangeFactory FType; + typedef GenSingleRangeFactory FType; virtual size_t size() const final; virtual size_t dim() const final; @@ -52,28 +52,28 @@ namespace MultiArrayTools virtual IndexType end() const final; //virtual std::shared_ptr index() const final; - friend SingleRangeFactory; + friend GenSingleRangeFactory; static constexpr bool defaultable = true; static constexpr size_t ISSTATIC = 0; static constexpr size_t SIZE = -1; static constexpr bool HASMETACONT = false; - static SingleRangeFactory factory(size_t size = 0) - { return SingleRangeFactory(size); } + static GenSingleRangeFactory factory(size_t size = 0) + { return GenSingleRangeFactory(size); } protected: size_t mSize = 0; - SingleRange() = default; - SingleRange(const SingleRange& in) = delete; + GenSingleRange() = default; + GenSingleRange(const GenSingleRange& in) = delete; - SingleRange(size_t size); + GenSingleRange(size_t size); }; - typedef SingleRange ClassicRange; - typedef SingleRangeFactory ClassicRF; + typedef GenSingleRange ClassicRange; + typedef GenSingleRangeFactory ClassicRF; } diff --git a/src/include/ranges/range_types/null_range.h b/src/include/ranges/range_types/null_range.h index 968c0c0..994bb29 100644 --- a/src/include/ranges/range_types/null_range.h +++ b/src/include/ranges/range_types/null_range.h @@ -12,19 +12,19 @@ include_range_type(NUL,-2) namespace MultiArrayTools { - typedef SingleIndex NullIndex; + typedef GenSingleIndex NullIndex; - std::shared_ptr > nullr(); + std::shared_ptr > nullr(); std::shared_ptr nulli(); template <> - class SingleRangeFactory : public RangeFactoryBase + class GenSingleRangeFactory : public RangeFactoryBase { public: - typedef SingleRange oType; + typedef GenSingleRange oType; - SingleRangeFactory(); + GenSingleRangeFactory(); std::shared_ptr create(); friend std::shared_ptr nullr(); @@ -34,14 +34,14 @@ namespace MultiArrayTools }; template <> - class SingleRange : public RangeInterface + class GenSingleRange : public RangeInterface { public: typedef RangeBase RB; - typedef typename RangeInterface >::IndexType IndexType; - typedef SingleRange RangeType; + typedef typename RangeInterface >::IndexType IndexType; + typedef GenSingleRange RangeType; typedef size_t MetaType; - typedef SingleRangeFactory FType; + typedef GenSingleRangeFactory FType; virtual size_t size() const final; virtual size_t dim() const final; @@ -59,7 +59,7 @@ namespace MultiArrayTools virtual IndexType end() const final; //virtual std::shared_ptr index() const final; - friend SingleRangeFactory; + friend GenSingleRangeFactory; static constexpr bool defaultable = true; @@ -67,19 +67,19 @@ namespace MultiArrayTools static constexpr size_t SIZE = 1; static constexpr bool HASMETACONT = false; - static SingleRangeFactory factory() - { return SingleRangeFactory(); } + static GenSingleRangeFactory factory() + { return GenSingleRangeFactory(); } protected: - SingleRange() = default; - SingleRange(const SingleRange& in) = delete; + GenSingleRange() = default; + GenSingleRange(const GenSingleRange& in) = delete; - //SingleRange(size_t spinNum); + //GenSingleRange(size_t spinNum); }; - typedef SingleRange NullRange; - typedef SingleRangeFactory NullRF; + typedef GenSingleRange NullRange; + typedef GenSingleRangeFactory NullRF; std::shared_ptr mkNUL(const char* dp, size_t size); diff --git a/src/include/ranges/range_types/space_range.h b/src/include/ranges/range_types/space_range.h index 238afb5..46d660a 100644 --- a/src/include/ranges/range_types/space_range.h +++ b/src/include/ranges/range_types/space_range.h @@ -11,29 +11,29 @@ include_range_type(PSPACE,3) // Periodic 1dim space namespace MultiArrayTools { // Periodic 1dim space - typedef SingleIndex XSpaceIndex; + typedef GenSingleIndex XSpaceIndex; template <> - class SingleRangeFactory : public RangeFactoryBase + class GenSingleRangeFactory : public RangeFactoryBase { public: - typedef SingleRange oType; + typedef GenSingleRange oType; - SingleRangeFactory(size_t size = 0); + GenSingleRangeFactory(size_t size = 0); std::shared_ptr create(); }; template <> - class SingleRange : public RangeInterface + class GenSingleRange : public RangeInterface { public: typedef RangeBase RB; - typedef typename RangeInterface >::IndexType IndexType; - typedef SingleRange RangeType; + typedef typename RangeInterface >::IndexType IndexType; + typedef GenSingleRange RangeType; typedef int MetaType; - typedef SingleRangeFactory FType; + typedef GenSingleRangeFactory FType; virtual size_t size() const final; virtual size_t dim() const final; @@ -50,28 +50,28 @@ namespace MultiArrayTools virtual IndexType begin() const final; virtual IndexType end() const final; - friend SingleRangeFactory; + friend GenSingleRangeFactory; static constexpr bool defaultable = true; static constexpr size_t ISSTATIC = 0; static constexpr size_t SIZE = -1; static constexpr bool HASMETACONT = false; - static SingleRangeFactory factory(size_t size = 0) - { return SingleRangeFactory(size); } + static GenSingleRangeFactory factory(size_t size = 0) + { return GenSingleRangeFactory(size); } protected: size_t mSize = 0; - SingleRange() = default; - SingleRange(const SingleRange& in) = delete; + GenSingleRange() = default; + GenSingleRange(const GenSingleRange& in) = delete; - SingleRange(size_t size); + GenSingleRange(size_t size); }; - typedef SingleRange PSpaceRange; - typedef SingleRangeFactory PSpaceRF; + typedef GenSingleRange PSpaceRange; + typedef GenSingleRangeFactory PSpaceRF; std::shared_ptr mkPSPACE(const char* dp, size_t size); diff --git a/src/include/ranges/range_types/spin_range.h b/src/include/ranges/range_types/spin_range.h index 32c8dff..97992b5 100644 --- a/src/include/ranges/range_types/spin_range.h +++ b/src/include/ranges/range_types/spin_range.h @@ -12,29 +12,29 @@ include_range_type(SPIN,2) namespace MultiArrayTools { - typedef SingleIndex SpinIndex; + typedef GenSingleIndex SpinIndex; template <> - class SingleRangeFactory : public RangeFactoryBase + class GenSingleRangeFactory : public RangeFactoryBase { public: - typedef SingleRange oType; + typedef GenSingleRange oType; - SingleRangeFactory(); + GenSingleRangeFactory(); std::shared_ptr create(); }; template <> - class SingleRange : public RangeInterface + class GenSingleRange : public RangeInterface { public: typedef RangeBase RB; - typedef typename RangeInterface >::IndexType IndexType; - typedef SingleRange RangeType; + typedef typename RangeInterface >::IndexType IndexType; + typedef GenSingleRange RangeType; typedef size_t MetaType; - typedef SingleRangeFactory FType; + typedef GenSingleRangeFactory FType; virtual size_t size() const final; virtual size_t dim() const final; @@ -52,7 +52,7 @@ namespace MultiArrayTools virtual IndexType end() const final; //virtual std::shared_ptr index() const final; - friend SingleRangeFactory; + friend GenSingleRangeFactory; static constexpr bool defaultable = true; static constexpr size_t mSpinNum = 4; @@ -61,19 +61,19 @@ namespace MultiArrayTools static constexpr size_t SIZE = mSpinNum; static constexpr bool HASMETACONT = false; - static SingleRangeFactory factory() - { return SingleRangeFactory(); } + static GenSingleRangeFactory factory() + { return GenSingleRangeFactory(); } protected: - SingleRange() = default; - SingleRange(const SingleRange& in) = delete; + GenSingleRange() = default; + GenSingleRange(const GenSingleRange& in) = delete; - //SingleRange(size_t spinNum); + //GenSingleRange(size_t spinNum); }; - typedef SingleRange SpinRange; - typedef SingleRangeFactory SpinRF; + typedef GenSingleRange SpinRange; + typedef GenSingleRangeFactory SpinRF; std::shared_ptr mkSPIN(const char* dp, size_t size); } diff --git a/src/include/ranges/rbase_def.h b/src/include/ranges/rbase_def.h index 3cffe08..64ccc45 100644 --- a/src/include/ranges/rbase_def.h +++ b/src/include/ranges/rbase_def.h @@ -44,16 +44,16 @@ namespace MultiArrayTools class IndexInterface; // single_range.h - template - class SingleRange; + template + class GenSingleRange; // single_range.h - template - class SingleRangeFactory; + template + class GenSingleRangeFactory; // single_range.h - template - class SingleIndex; + template + class GenSingleIndex; // subrange.h template diff --git a/src/include/ranges/single_range.h b/src/include/ranges/single_range.h index 1affcaf..57d76f7 100644 --- a/src/include/ranges/single_range.h +++ b/src/include/ranges/single_range.h @@ -28,19 +28,19 @@ namespace MultiArrayTools } - template - class SingleIndex : public IndexInterface,U> + template + class GenSingleIndex : public IndexInterface,U> { public: - typedef IndexInterface,U> IB; + typedef IndexInterface,U> IB; typedef U MetaType; - typedef SingleRange RangeType; - typedef SingleIndex IType; + typedef GenSingleRange RangeType; + typedef GenSingleIndex IType; - //DEFAULT_MEMBERS_X(SingleIndex); + //DEFAULT_MEMBERS_X(GenSingleIndex); - SingleIndex(const std::shared_ptr >& range); + GenSingleIndex(const std::shared_ptr >& range); static constexpr IndexType sType() { return IndexType::SINGLE; } static constexpr size_t totalDim() { return 1; } @@ -53,9 +53,9 @@ namespace MultiArrayTools IndexType type() const; - SingleIndex& operator=(size_t pos); - SingleIndex& operator++(); - SingleIndex& operator--(); + GenSingleIndex& operator=(size_t pos); + GenSingleIndex& operator++(); + GenSingleIndex& operator--(); int pp(std::intptr_t idxPtrNum); int mm(std::intptr_t idxPtrNum); @@ -63,7 +63,7 @@ namespace MultiArrayTools std::string stringMeta() const; U meta() const; const U* metaPtr() const; - SingleIndex& at(const U& metaPos); + GenSingleIndex& at(const U& metaPos); size_t posAt(const U& metaPos) const; bool isMeta(const U& metaPos) const; @@ -84,30 +84,30 @@ namespace MultiArrayTools template auto ifor(size_t step, Expr ex) const - -> For,Expr>; + -> For,Expr>; template auto iforh(size_t step, Expr ex) const - -> For,Expr,ForType::HIDDEN>; + -> For,Expr,ForType::HIDDEN>; template auto pifor(size_t step, Expr ex) const - -> PFor,Expr>; + -> PFor,Expr>; private: std::shared_ptr mExplicitRangePtr; const U* mMetaPtr; }; - template - class SingleRangeFactory : public RangeFactoryBase + template + class GenSingleRangeFactory : public RangeFactoryBase { public: - typedef SingleRange oType; + typedef GenSingleRange oType; - SingleRangeFactory() = delete; - SingleRangeFactory(const std::vector& space); + GenSingleRangeFactory() = delete; + GenSingleRangeFactory(const std::vector& space); std::shared_ptr create(); }; @@ -186,17 +186,31 @@ namespace MultiArrayTools } }; - - template - class SingleRange : public RangeInterface > + + template + struct CheckStatic + { + static constexpr size_t ISSTATIC = true; + static constexpr size_t SIZE = S; + }; + + template <> + struct CheckStatic<-1> + { + static constexpr size_t ISSTATIC = false; + static constexpr size_t SIZE = -1; + }; + + template + class GenSingleRange : public RangeInterface > { public: typedef RangeBase RB; - typedef SingleIndex IndexType; - typedef SingleRange RangeType; + typedef GenSingleIndex IndexType; + typedef GenSingleRange RangeType; typedef U MetaType; - typedef SingleRangeFactory FType; - //typedef typename RangeInterface >::IndexType IndexType; + typedef GenSingleRangeFactory FType; + //typedef typename RangeInterface >::IndexType IndexType; virtual size_t size() const final; virtual size_t dim() const final; @@ -215,25 +229,34 @@ namespace MultiArrayTools virtual IndexType begin() const final; virtual IndexType end() const final; - friend SingleRangeFactory; + friend GenSingleRangeFactory; static constexpr bool defaultable = false; - static constexpr size_t ISSTATIC = 0; - static constexpr size_t SIZE = -1; + static constexpr size_t ISSTATIC = CheckStatic::ISSTATIC; + static constexpr size_t SIZE = CheckStatic::SIZE; static constexpr bool HASMETACONT = true; protected: - SingleRange() = delete; - SingleRange(const SingleRange& in) = delete; + GenSingleRange() = delete; + GenSingleRange(const GenSingleRange& in) = delete; - SingleRange(const std::vector& space); + GenSingleRange(const std::vector& space); std::vector mSpace; //std::map mMSpace; MetaMap mMSpace; }; + template + using SingleRange = GenSingleRange; + + template + using SingleIndex = GenSingleIndex; + + template + using SingleRangeFactory = GenSingleRangeFactory; + } /* ========================= * @@ -243,7 +266,7 @@ namespace MultiArrayTools namespace MultiArrayTools { /****************** - * SingleIndex * + * GenSingleIndex * ******************/ template @@ -278,134 +301,134 @@ namespace MultiArrayTools } }; - template - SingleIndex::SingleIndex(const std::shared_ptr >& range) : - IndexInterface,U>(range, 0), + template + GenSingleIndex::GenSingleIndex(const std::shared_ptr >& range) : + IndexInterface,U>(range, 0), mExplicitRangePtr(std::dynamic_pointer_cast(IB::mRangePtr)), - mMetaPtr(MetaPtrHandle::RangeType::HASMETACONT>::set + mMetaPtr(MetaPtrHandle::RangeType::HASMETACONT>::set ( dynamic_cast(IB::mRangePtr.get() ) ) ) {} - template - IndexType SingleIndex::type() const + template + IndexType GenSingleIndex::type() const { return IndexType::SINGLE; } - template - SingleIndex& SingleIndex::operator=(size_t pos) + template + GenSingleIndex& GenSingleIndex::operator=(size_t pos) { IB::mPos = pos; return *this; } - template - SingleIndex& SingleIndex::operator++() + template + GenSingleIndex& GenSingleIndex::operator++() { ++IB::mPos; return *this; } - template - SingleIndex& SingleIndex::operator--() + template + GenSingleIndex& GenSingleIndex::operator--() { --IB::mPos; return *this; } - template - int SingleIndex::pp(std::intptr_t idxPtrNum) + template + int GenSingleIndex::pp(std::intptr_t idxPtrNum) { ++(*this); return 1; } - template - int SingleIndex::mm(std::intptr_t idxPtrNum) + template + int GenSingleIndex::mm(std::intptr_t idxPtrNum) { --(*this); return 1; } - template - std::string SingleIndex::stringMeta() const + template + std::string GenSingleIndex::stringMeta() const { - return std::dynamic_pointer_cast const>( IB::mRangePtr )->stringMeta(IB::mPos); + return std::dynamic_pointer_cast const>( IB::mRangePtr )->stringMeta(IB::mPos); } - template - U SingleIndex::meta() const + template + U GenSingleIndex::meta() const { - return MetaPtrHandle::RangeType::HASMETACONT>::getMeta + return MetaPtrHandle::RangeType::HASMETACONT>::getMeta ( mMetaPtr, IB::mPos, mExplicitRangePtr ); } - template - const U* SingleIndex::metaPtr() const + template + const U* GenSingleIndex::metaPtr() const { return mMetaPtr; } - template - bool SingleIndex::isMeta(const U& metaPos) const + template + bool GenSingleIndex::isMeta(const U& metaPos) const { return mExplicitRangePtr->isMeta(metaPos); } - template - SingleIndex& SingleIndex::at(const U& metaPos) + template + GenSingleIndex& GenSingleIndex::at(const U& metaPos) { - (*this) = std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ); + (*this) = std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ); return *this; } - template - size_t SingleIndex::posAt(const U& metaPos) const + template + size_t GenSingleIndex::posAt(const U& metaPos) const { - return std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ); + return std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ); } - template - size_t SingleIndex::dim() // = 1 + template + size_t GenSingleIndex::dim() // = 1 { return 1; } - template - bool SingleIndex::last() + template + bool GenSingleIndex::last() { return IB::mPos == IB::mMax - 1; } - template - bool SingleIndex::first() + template + bool GenSingleIndex::first() { return IB::mPos == 0; } - template - std::shared_ptr::RangeType> SingleIndex::range() + template + std::shared_ptr::RangeType> GenSingleIndex::range() { return mExplicitRangePtr; } - template + template template - void SingleIndex::getPtr() {} + void GenSingleIndex::getPtr() {} - template - size_t SingleIndex::getStepSize(size_t n) + template + size_t GenSingleIndex::getStepSize(size_t n) { return 1; } - template - std::string SingleIndex::id() const + template + std::string GenSingleIndex::id() const { return std::string("sin") + std::to_string(IB::mId); } - template - void SingleIndex::print(size_t offset) + template + void GenSingleIndex::print(size_t offset) { if(offset == 0){ std::cout << " === " << std::endl; @@ -415,31 +438,31 @@ namespace MultiArrayTools << "](" << IB::mRangePtr << "): " << meta() << std::endl; } - template + template template - auto SingleIndex::ifor(size_t step, Expr ex) const - -> For,Expr> + auto GenSingleIndex::ifor(size_t step, Expr ex) const + -> For,Expr> { //static const size_t LAYER = typename Expr::LAYER; - return For,Expr>(this, step, ex); + return For,Expr>(this, step, ex); } - template + template template - auto SingleIndex::iforh(size_t step, Expr ex) const - -> For,Expr,ForType::HIDDEN> + auto GenSingleIndex::iforh(size_t step, Expr ex) const + -> For,Expr,ForType::HIDDEN> { //static const size_t LAYER = typename Expr::LAYER; - return For,Expr,ForType::HIDDEN>(this, step, ex); + return For,Expr,ForType::HIDDEN>(this, step, ex); } - template + template template - auto SingleIndex::pifor(size_t step, Expr ex) const - -> PFor,Expr> + auto GenSingleIndex::pifor(size_t step, Expr ex) const + -> PFor,Expr> { //static const size_t LAYER = typename Expr::LAYER; - return PFor,Expr>(this, step, ex); + return PFor,Expr>(this, step, ex); } @@ -447,14 +470,14 @@ namespace MultiArrayTools * SingleRange * ********************/ - template - SingleRangeFactory::SingleRangeFactory(const std::vector& space) + template + GenSingleRangeFactory::GenSingleRangeFactory(const std::vector& space) { - mProd = std::shared_ptr( new SingleRange( space ) ); + mProd = std::shared_ptr( new GenSingleRange( space ) ); } - template - std::shared_ptr SingleRangeFactory::create() + template + std::shared_ptr GenSingleRangeFactory::create() { setSelf(); return mProd; @@ -464,9 +487,9 @@ namespace MultiArrayTools * SingleRange * ********************/ - template - SingleRange::SingleRange(const std::vector& space) : - RangeInterface >(), + template + GenSingleRange::GenSingleRange(const std::vector& space) : + RangeInterface >(), mSpace(space), mMSpace(mSpace) { //for(size_t i = 0; i != mSpace.size(); ++i){ @@ -474,50 +497,50 @@ namespace MultiArrayTools //} } - template - const U& SingleRange::get(size_t pos) const + template + const U& GenSingleRange::get(size_t pos) const { return mSpace[pos]; } - template - size_t SingleRange::getMeta(const U& metaPos) const + template + size_t GenSingleRange::getMeta(const U& metaPos) const { return mMSpace.at(metaPos); } - template - size_t SingleRange::size() const + template + size_t GenSingleRange::size() const { return mSpace.size(); } - template - size_t SingleRange::dim() const + template + size_t GenSingleRange::dim() const { return 1; } - template - bool SingleRange::isMeta(const U& metaPos) const + template + bool GenSingleRange::isMeta(const U& metaPos) const { return mMSpace.count(metaPos) != 0; } - template - SpaceType SingleRange::spaceType() const + template + SpaceType GenSingleRange::spaceType() const { return TYPE; } - template - std::string SingleRange::stringMeta(size_t pos) const + template + std::string GenSingleRange::stringMeta(size_t pos) const { return xToString(get(pos)); } - template - std::vector SingleRange::data() const + template + std::vector GenSingleRange::data() const { DataHeader h = dataHeader(); std::vector out; @@ -530,8 +553,8 @@ namespace MultiArrayTools return out; } - template - DataHeader SingleRange::dataHeader() const + template + DataHeader GenSingleRange::dataHeader() const { DataHeader h; h.spaceType = static_cast( TYPE ); @@ -541,19 +564,19 @@ namespace MultiArrayTools return h; } - template - typename SingleRange::IndexType SingleRange::begin() const + template + typename GenSingleRange::IndexType GenSingleRange::begin() const { - SingleIndex i( std::dynamic_pointer_cast > + GenSingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = 0; return i; } - template - typename SingleRange::IndexType SingleRange::end() const + template + typename GenSingleRange::IndexType GenSingleRange::end() const { - SingleIndex i( std::dynamic_pointer_cast > + GenSingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = size(); return i; diff --git a/src/lib/ranges/anonymous_range.cc b/src/lib/ranges/anonymous_range.cc index 3849270..4b5a337 100644 --- a/src/lib/ranges/anonymous_range.cc +++ b/src/lib/ranges/anonymous_range.cc @@ -196,7 +196,7 @@ namespace MultiArrayTools return mOrig; } - SingleRange::SingleRange(const std::vector>& origs) : + GenSingleRange::GenSingleRange(const std::vector>& origs) : RangeInterface(), mOrig(origs) { diff --git a/src/lib/ranges/range_types/classic_range.cc b/src/lib/ranges/range_types/classic_range.cc index 9cdba7d..db147d9 100644 --- a/src/lib/ranges/range_types/classic_range.cc +++ b/src/lib/ranges/range_types/classic_range.cc @@ -5,60 +5,60 @@ namespace MultiArrayTools { /******************** - * SingleRange * + * GenSingleRange * ********************/ - SingleRangeFactory::SingleRangeFactory(size_t size) + GenSingleRangeFactory::GenSingleRangeFactory(size_t size) { // Quasi Singleton if(not mProd){ - mProd = std::shared_ptr( new SingleRange(size) ); + mProd = std::shared_ptr( new GenSingleRange(size) ); setSelf(); } } - std::shared_ptr SingleRangeFactory::create() + std::shared_ptr GenSingleRangeFactory::create() { return mProd; } /******************** - * SingleRange * + * GenSingleRange * ********************/ - SingleRange::SingleRange(size_t size) : mSize(size) {} + GenSingleRange::GenSingleRange(size_t size) : mSize(size) {} - size_t SingleRange::get(size_t pos) const + size_t GenSingleRange::get(size_t pos) const { return pos; } - size_t SingleRange::getMeta(size_t metaPos) const + size_t GenSingleRange::getMeta(size_t metaPos) const { return metaPos; } - size_t SingleRange::size() const + size_t GenSingleRange::size() const { return mSize; } - size_t SingleRange::dim() const + size_t GenSingleRange::dim() const { return 1; } - SpaceType SingleRange::spaceType() const + SpaceType GenSingleRange::spaceType() const { return SpaceType::NONE; } - std::string SingleRange::stringMeta(size_t pos) const + std::string GenSingleRange::stringMeta(size_t pos) const { return std::to_string(get(pos)); } - std::vector SingleRange::data() const + std::vector GenSingleRange::data() const { DataHeader h = dataHeader(); std::vector out; @@ -70,7 +70,7 @@ namespace MultiArrayTools return out; } - DataHeader SingleRange::dataHeader() const + DataHeader GenSingleRange::dataHeader() const { DataHeader h; h.spaceType = static_cast( SpaceType::NONE ); @@ -80,17 +80,17 @@ namespace MultiArrayTools } - typename SingleRange::IndexType SingleRange::begin() const + typename GenSingleRange::IndexType GenSingleRange::begin() const { - SingleIndex i( std::dynamic_pointer_cast > + GenSingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = 0; return i; } - typename SingleRange::IndexType SingleRange::end() const + typename GenSingleRange::IndexType GenSingleRange::end() const { - SingleIndex i( std::dynamic_pointer_cast > + GenSingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = size(); return i; diff --git a/src/lib/ranges/range_types/null_range.cc b/src/lib/ranges/range_types/null_range.cc index d42ebb5..46ac26b 100644 --- a/src/lib/ranges/range_types/null_range.cc +++ b/src/lib/ranges/range_types/null_range.cc @@ -10,11 +10,11 @@ namespace MultiArrayTools } /******************** - * SingleRange * + * GenSingleRange * ********************/ - std::shared_ptr> - SingleRangeFactory::mRInstance = nullptr; + std::shared_ptr> + GenSingleRangeFactory::mRInstance = nullptr; std::shared_ptr nullr() { @@ -28,12 +28,12 @@ namespace MultiArrayTools return std::make_shared(nullr()); } - SingleRangeFactory::SingleRangeFactory() + GenSingleRangeFactory::GenSingleRangeFactory() { // Singleton if(not mRInstance){ if(not mProd){ - mProd = std::shared_ptr( new SingleRange() ); + mProd = std::shared_ptr( new GenSingleRange() ); setSelf(); } mRInstance = std::dynamic_pointer_cast( mProd ); @@ -42,46 +42,46 @@ namespace MultiArrayTools } } - std::shared_ptr SingleRangeFactory::create() + std::shared_ptr GenSingleRangeFactory::create() { return mProd; } /******************** - * SingleRange * + * GenSingleRange * ********************/ - size_t SingleRange::get(size_t pos) const + size_t GenSingleRange::get(size_t pos) const { return 0; } - size_t SingleRange::getMeta(size_t metapos) const + size_t GenSingleRange::getMeta(size_t metapos) const { return 0; } - size_t SingleRange::size() const + size_t GenSingleRange::size() const { return 1; } - size_t SingleRange::dim() const + size_t GenSingleRange::dim() const { return 1; } - SpaceType SingleRange::spaceType() const + SpaceType GenSingleRange::spaceType() const { return SpaceType::NUL; } - std::string SingleRange::stringMeta(size_t pos) const + std::string GenSingleRange::stringMeta(size_t pos) const { return std::to_string(get(pos)); } - std::vector SingleRange::data() const + std::vector GenSingleRange::data() const { DataHeader h = dataHeader(); std::vector out; @@ -91,7 +91,7 @@ namespace MultiArrayTools return out; } - DataHeader SingleRange::dataHeader() const + DataHeader GenSingleRange::dataHeader() const { DataHeader h; h.spaceType = static_cast( SpaceType::NUL ); @@ -100,17 +100,17 @@ namespace MultiArrayTools return h; } - typename SingleRange::IndexType SingleRange::begin() const + typename GenSingleRange::IndexType GenSingleRange::begin() const { - SingleIndex i( std::dynamic_pointer_cast > + GenSingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = 0; return i; } - typename SingleRange::IndexType SingleRange::end() const + typename GenSingleRange::IndexType GenSingleRange::end() const { - SingleIndex i( std::dynamic_pointer_cast > + GenSingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = size(); return i; @@ -118,12 +118,12 @@ namespace MultiArrayTools // put this in the interface class !!! /* - std::shared_ptr SingleRange::index() const + std::shared_ptr GenSingleRange::index() const { typedef IndexWrapper IW; return std::make_shared ( std::make_shared - ( std::dynamic_pointer_cast > + ( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ) ); } */ diff --git a/src/lib/ranges/range_types/space_range.cc b/src/lib/ranges/range_types/space_range.cc index e4ca7f3..ae4758e 100644 --- a/src/lib/ranges/range_types/space_range.cc +++ b/src/lib/ranges/range_types/space_range.cc @@ -11,60 +11,60 @@ namespace MultiArrayTools } /******************** - * SingleRange * + * GenSingleRange * ********************/ - SingleRangeFactory::SingleRangeFactory(size_t size) + GenSingleRangeFactory::GenSingleRangeFactory(size_t size) { // Quasi Singleton if(not mProd){ - mProd = std::shared_ptr( new SingleRange(size) ); + mProd = std::shared_ptr( new GenSingleRange(size) ); setSelf(); } } - std::shared_ptr SingleRangeFactory::create() + std::shared_ptr GenSingleRangeFactory::create() { return mProd; } /******************** - * SingleRange * + * GenSingleRange * ********************/ - SingleRange::SingleRange(size_t size) : mSize(size) { } + GenSingleRange::GenSingleRange(size_t size) : mSize(size) { } - int SingleRange::get(size_t pos) const + int GenSingleRange::get(size_t pos) const { return pos > mSize / 2 ? pos - mSize : pos; } - size_t SingleRange::getMeta(int metaPos) const + size_t GenSingleRange::getMeta(int metaPos) const { return metaPos < 0 ? metaPos + mSize : metaPos; } - size_t SingleRange::size() const + size_t GenSingleRange::size() const { return mSize; } - size_t SingleRange::dim() const + size_t GenSingleRange::dim() const { return 1; } - SpaceType SingleRange::spaceType() const + SpaceType GenSingleRange::spaceType() const { return SpaceType::PSPACE; } - std::string SingleRange::stringMeta(size_t pos) const + std::string GenSingleRange::stringMeta(size_t pos) const { return std::to_string(get(pos)); } - std::vector SingleRange::data() const + std::vector GenSingleRange::data() const { DataHeader h = dataHeader(); std::vector out; @@ -76,7 +76,7 @@ namespace MultiArrayTools return out; } - DataHeader SingleRange::dataHeader() const + DataHeader GenSingleRange::dataHeader() const { DataHeader h; h.spaceType = static_cast( SpaceType::PSPACE ); @@ -85,17 +85,17 @@ namespace MultiArrayTools return h; } - typename SingleRange::IndexType SingleRange::begin() const + typename GenSingleRange::IndexType GenSingleRange::begin() const { - SingleIndex i( std::dynamic_pointer_cast > + GenSingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = 0; return i; } - typename SingleRange::IndexType SingleRange::end() const + typename GenSingleRange::IndexType GenSingleRange::end() const { - SingleIndex i( std::dynamic_pointer_cast > + GenSingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = size(); return i; diff --git a/src/lib/ranges/range_types/spin_range.cc b/src/lib/ranges/range_types/spin_range.cc index b391481..8ba73f4 100644 --- a/src/lib/ranges/range_types/spin_range.cc +++ b/src/lib/ranges/range_types/spin_range.cc @@ -10,58 +10,58 @@ namespace MultiArrayTools } /******************** - * SingleRange * + * GenSingleRange * ********************/ - SingleRangeFactory::SingleRangeFactory() + GenSingleRangeFactory::GenSingleRangeFactory() { // Quasi Singleton if(not mProd){ - mProd = std::shared_ptr( new SingleRange() ); + mProd = std::shared_ptr( new GenSingleRange() ); setSelf(); } } - std::shared_ptr SingleRangeFactory::create() + std::shared_ptr GenSingleRangeFactory::create() { return mProd; } /******************** - * SingleRange * + * GenSingleRange * ********************/ - size_t SingleRange::get(size_t pos) const + size_t GenSingleRange::get(size_t pos) const { return pos; } - size_t SingleRange::getMeta(size_t metaPos) const + size_t GenSingleRange::getMeta(size_t metaPos) const { return metaPos; } - size_t SingleRange::size() const + size_t GenSingleRange::size() const { return mSpinNum; } - size_t SingleRange::dim() const + size_t GenSingleRange::dim() const { return 1; } - SpaceType SingleRange::spaceType() const + SpaceType GenSingleRange::spaceType() const { return SpaceType::SPIN; } - std::string SingleRange::stringMeta(size_t pos) const + std::string GenSingleRange::stringMeta(size_t pos) const { return std::to_string(get(pos)); } - std::vector SingleRange::data() const + std::vector GenSingleRange::data() const { DataHeader h = dataHeader(); std::vector out; @@ -71,7 +71,7 @@ namespace MultiArrayTools return out; } - DataHeader SingleRange::dataHeader() const + DataHeader GenSingleRange::dataHeader() const { DataHeader h; h.spaceType = static_cast( SpaceType::SPIN ); @@ -80,17 +80,17 @@ namespace MultiArrayTools return h; } - typename SingleRange::IndexType SingleRange::begin() const + typename GenSingleRange::IndexType GenSingleRange::begin() const { - SingleIndex i( std::dynamic_pointer_cast > + GenSingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = 0; return i; } - typename SingleRange::IndexType SingleRange::end() const + typename GenSingleRange::IndexType GenSingleRange::end() const { - SingleIndex i( std::dynamic_pointer_cast > + GenSingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = size(); return i; @@ -98,12 +98,12 @@ namespace MultiArrayTools // put this in the interface class !!! /* - std::shared_ptr SingleRange::index() const + std::shared_ptr GenSingleRange::index() const { typedef IndexWrapper IW; return std::make_shared ( std::make_shared - ( std::dynamic_pointer_cast > + ( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ) ); } */ diff --git a/src/tests/op_perf_test.cc b/src/tests/op_perf_test.cc index 4c623ec..62accee 100644 --- a/src/tests/op_perf_test.cc +++ b/src/tests/op_perf_test.cc @@ -134,13 +134,14 @@ namespace { typedef MultiRangeFactory SR8F; typedef SR8F::oType SR8; - static const size_t os = 3000; - static const size_t s = 65536*os; - + static const size_t os = 30; + static const size_t is = 65536; + static const size_t s = is*os; + OpTest_Spin() { - data.resize(s); - for(size_t i = 0; i != s; ++i){ + data.resize(is); + for(size_t i = 0; i != is; ++i){ double arg = static_cast( i - s ) - 0.1; data[i] = sin(arg); //VCHECK(data[i]); @@ -162,7 +163,7 @@ namespace { void OpTest_Spin::contract() { - MultiArray ma( cr, sr, sr, sr, sr, sr, sr, sr, sr, data); + MultiArray ma( sr, sr, sr, sr, sr, sr, sr, sr, data); MultiArray res1( cr, sr, sr ); auto ii = MAT::getIndex(cr); @@ -177,7 +178,8 @@ namespace { std::clock_t begin = std::clock(); //for(size_t i = 0; i != os; ++i){ - res1(ii ,delta, deltap).par() += ma(ii, delta, alpha, alpha, beta, beta, gamma, gamma, deltap).c(mix); + //res1(ii ,delta, deltap).par() += ma(ii, delta, alpha, alpha, beta, beta, gamma, gamma, deltap).c(mix); + res1(ii ,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 @@ -199,7 +201,7 @@ namespace { for(size_t d = 0; d != 4; ++d){ for(size_t p = 0; p != 4; ++p){ const size_t tidx = i*4*4 + d*4 + p; - const size_t sidx = i*65536 + d*4*4*4*4*4*4*4 + a*5*4*4*4*4*4 + b*5*4*4*4 + c*5*4 + p; + const size_t sidx = /*i*65536 +*/ 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]; } } From 0326ebc5050dd8a22dc9e4146353bd966a0a42d1 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 12 Feb 2019 22:34:09 +0100 Subject: [PATCH 2/9] conversions --- src/include/conversions.h | 64 +++++++++++++++++++ src/include/type_operations.h | 112 ++++++++++++++++++++++++++++------ 2 files changed, 156 insertions(+), 20 deletions(-) create mode 100644 src/include/conversions.h diff --git a/src/include/conversions.h b/src/include/conversions.h new file mode 100644 index 0000000..87d1a1e --- /dev/null +++ b/src/include/conversions.h @@ -0,0 +1,64 @@ + +#ifndef __ma_conversions_h__ +#define __ma_conversions_h__ + +#include "multi_array.h" +#include "slice.h" + +namespace MultiArrayTools +{ + + namespace ConversionSizes + { + template + struct OrigSize + { + template + struct FromTo + { + static void check() { static_assert( not N % (sizeof(T) / sizeof(C)), "conversion does not fit" ); } + static constexpr size_t SIZE = N * sizeof(T) / sizeof(C); + }; + }; + + template <> + struct OrigSize<-1> + { + template + struct FromTo + { + static void check() {} + static constexpr size_t SIZE = -1; + }; + }; + } + + namespace + { + template + using SC = ConversionSizes::OrigSize::template FromTo; + + template + using SCR = SC; + + template + using SCRR = GenSingleRange; + } + + template + Slice> tcast(MultiArray& ma) + { + return Slice> + ( ma.range()->space(), reinterpret_cast( ma.data() ) ); + } + + template + ConstSlice> tcast(const MultiArray& ma) + { + return ConstSlice> + ( ma.range()->space(), reinterpret_cast( ma.data() ) ); + } + +} + +#endif diff --git a/src/include/type_operations.h b/src/include/type_operations.h index 6870dac..efa35c6 100644 --- a/src/include/type_operations.h +++ b/src/include/type_operations.h @@ -82,7 +82,60 @@ namespace MultiArrayTools std::transform(a.begin(), a.end(), b.begin(), a.begin(), std::plus()); return a; } - + + template + std::vector& operator-=(std::vector& a, const std::vector& b) + { + std::transform(a.begin(), a.end(), b.begin(), a.begin(), std::minus()); + return a; + } + + template + std::vector& operator*=(std::vector& a, const std::vector& b) + { + std::transform(a.begin(), a.end(), b.begin(), a.begin(), std::multiplies()); + return a; + } + + template + std::vector& operator/=(std::vector& a, const std::vector& b) + { + std::transform(a.begin(), a.end(), b.begin(), a.begin(), std::divides()); + return a; + } + + template + std::vector operator+(std::vector& a, const std::vector& b) + { + std::vector out(a.size()); + std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::plus()); + return out; + } + + template + std::vector operator-(std::vector& a, const std::vector& b) + { + std::vector out(a.size()); + std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::minus()); + return out; + } + + template + std::vector operator*(std::vector& a, const std::vector& b) + { + std::vector out(a.size()); + std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::multiplies()); + return out; + } + + template + std::vector operator/(std::vector& a, const std::vector& b) + { + std::vector out(a.size()); + std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::divides()); + return out; + } + template class OperationTemplate,OperationClass> : public OperationBase,OperationClass> { @@ -101,28 +154,47 @@ namespace MultiArrayTools friend OperationClass; }; - inline std::array& operator+=(std::array& a, const std::array& b) + template + inline std::array operator+(std::array& a, const std::array& b) { - std::get<0>(a) += std::get<0>(b); - std::get<1>(a) += std::get<1>(b); - return a; + std::array out; + for(size_t i = 0; i != N; ++i){ + out[i] = a[i] + b[i]; + } + return out; } + + template + inline std::array operator-(std::array& a, const std::array& b) + { + std::array out; + for(size_t i = 0; i != N; ++i){ + out[i] = a[i] - b[i]; + } + return out; + } + + template + inline std::array operator*(std::array& a, const std::array& b) + { + std::array out; + for(size_t i = 0; i != N; ++i){ + out[i] = a[i] * b[i]; + } + return out; + } + + template + inline std::array operator/(std::array& a, const std::array& b) + { + std::array out; + for(size_t i = 0; i != N; ++i){ + out[i] = a[i] / b[i]; + } + return out; + } + - inline std::array& operator+=(std::array& a, const std::array& b) - { - std::get<0>(a) += std::get<0>(b); - std::get<1>(a) += std::get<1>(b); - std::get<2>(a) += std::get<2>(b); - return a; - } - - inline std::tuple& operator+=(std::tuple& a, const std::tuple& b) - { - std::get<0>(a) += std::get<0>(b); - std::get<1>(a) += std::get<1>(b); - std::get<2>(a) += std::get<2>(b); - return a; - } } // namespace MultiArrayTools From 6d776f853dd9e22951e4686a3616af6b4d85381a Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 13 Feb 2019 21:59:13 +0100 Subject: [PATCH 3/9] im com --- CMakeLists.txt | 2 +- src/include/allocator.h | 74 +++++++++++++++ src/include/allocator.h~ | 62 ++++++++++++ src/include/conversions.h | 94 +++++++++++++++---- src/include/functional_multi_array.h | 2 +- src/include/helper_tools.cc.h | 2 +- src/include/helper_tools.h | 2 +- src/include/map_range.cc.h | 14 +-- src/include/map_range.h | 2 +- src/include/map_range_factory_product_map.h | 2 +- src/include/mbase_def.h | 1 + src/include/multi_array.cc.h | 8 +- src/include/multi_array.h | 19 ++-- src/include/ranges/anonymous_range.h | 20 ++-- src/include/ranges/dynamic_meta.h | 8 +- src/include/ranges/dynamic_range.cc.h | 20 ++-- src/include/ranges/dynamic_range.h | 22 ++--- src/include/ranges/multi_range.h | 8 +- .../ranges/multi_range_factory_product_map.h | 2 +- src/include/ranges/range_base.h | 2 +- .../ranges/range_types/classic_range.h | 2 +- src/include/ranges/range_types/null_range.h | 2 +- src/include/ranges/range_types/space_range.h | 2 +- src/include/ranges/range_types/spin_range.h | 2 +- src/include/ranges/rbase_def.h | 2 +- src/include/ranges/rpack_num.h | 36 +++---- src/include/ranges/single_range.h | 62 +++++++++--- src/include/ranges/subrange.h | 24 ++--- src/include/ranges/type_map.h | 22 +++-- src/include/ranges/value_range.h | 6 +- src/include/ranges/x_to_string.h | 4 +- src/include/slice.cc.h | 11 ++- src/include/slice.h | 2 + src/include/type_operations.h | 93 +++++++++--------- src/include/xfor/xfor.h | 13 +-- src/lib/map_range_factory_product_map.cc | 2 +- src/lib/ranges/anonymous_range.cc | 18 ++-- .../ranges/multi_range_factory_product_map.cc | 2 +- src/lib/ranges/range_base.cc | 4 +- src/lib/ranges/range_types/classic_range.cc | 4 +- src/lib/ranges/range_types/null_range.cc | 4 +- src/lib/ranges/range_types/space_range.cc | 4 +- src/lib/ranges/range_types/spin_range.cc | 4 +- src/tests/ma_unit_test.cc | 8 +- src/tests/op_perf_test.cc | 52 +++++----- src/tests/op_unit_test.cc | 46 ++++----- src/tests/ranges/anonymous_unit_test.cc | 14 +-- src/tests/ranges/index_unit_test.cc | 2 +- 48 files changed, 533 insertions(+), 280 deletions(-) create mode 100644 src/include/allocator.h create mode 100644 src/include/allocator.h~ diff --git a/CMakeLists.txt b/CMakeLists.txt index a66ce5c..77f8ffb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8) project(multi_array) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -Wpedantic -O3 -g -march=native -fopenmp -funroll-loops -ftree-vectorize") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -g -Wpedantic -Ofast -mavx2 -funroll-loops -fopenmp") enable_testing() diff --git a/src/include/allocator.h b/src/include/allocator.h new file mode 100644 index 0000000..2cfdd2c --- /dev/null +++ b/src/include/allocator.h @@ -0,0 +1,74 @@ + +#ifndef __ma_allocator__ +#define __ma_allocator__ + +#include +#include +#include +#include +#include +#include + +namespace MultiArrayHelper +{ + + template + struct Allocator + { + typedef T value_type; + static constexpr size_t type_size = sizeof(value_type); + static constexpr size_t N = 32; + + Allocator() = default; + + template + Allocator(const Allocator& x) {} + + T* allocate(size_t n) + { + //constexpr size_t NN = N*type_size; + const size_t nn = n*type_size; + if(n > static_cast(-1-N)) throw std::bad_alloc(); + auto p = static_cast(std::malloc(nn + N)); + if(not p) throw std::bad_alloc(); + auto ip = reinterpret_cast(p); + auto diff = N - (ip % N); + p += diff; + //auto ipx = reinterpret_cast(p); + //std::cout << "ixp: " << ipx << std::endl; + //std::cout << "ixp %: " << ipx % N << std::endl; + auto ps = reinterpret_cast(p); + ps[-1] = diff; + return reinterpret_cast(p); + } + + void deallocate(T* p, size_t n) + { + auto ps = reinterpret_cast(p); + std::free(reinterpret_cast(p)-ps[-1]); + } + }; + + template + bool operator==(const Allocator& a, const Allocator& b) + { + return true; + } + + template + bool operator!=(const Allocator& a, const Allocator& b) + { + return false; + } + + +} // namespace MultiArrayHelper + +namespace MultiArrayTools +{ + template + using vector = std::vector>; + +} // namespace MultiArrayTools + +#endif diff --git a/src/include/allocator.h~ b/src/include/allocator.h~ new file mode 100644 index 0000000..9636d7a --- /dev/null +++ b/src/include/allocator.h~ @@ -0,0 +1,62 @@ + +#ifndef __ma_allocator__ +#define __ma_allocator__ + +#include +#include +#include + +namespace MultiArrayHelper +{ + + template + struct Allocator + { + typedef T value_type; + static constexpr type_size = sizeof(value_type); + + Allocator() = default; + + template + Allocator(const Allocator& x) {} + + T* allocate(size_t n) + { + //constexpr size_t NN = N*type_size; + const size_t nn = n*type_size; + if(n > static_cast(-1-NN)) throw std::bad_alloc(); + auto p = static_cast(std::malloc(nn + N)); + if(not p) throw std::bad_alloc(); + auto ip = static_cast(p); + mDiff = N - (ip % N); + p += mDiff; + return static_cast(p); + } + + void deacllocate(T* p, size_t n) + { + std::free(p-mDiff); + } + + private: + std::intptr_t mDiff; + }; + + template + bool operator==(const Allocator& a, const Allocator& b) + { + return true; + } + + template + bool operator!=(const Allocator& a, const Allocator& b) + { + return false; + } + + template + using vector = vector; + +} // namespace MultiArrayHelper + +#endif diff --git a/src/include/conversions.h b/src/include/conversions.h index 87d1a1e..60a3b53 100644 --- a/src/include/conversions.h +++ b/src/include/conversions.h @@ -36,29 +36,87 @@ namespace MultiArrayTools namespace { template - using SC = ConversionSizes::OrigSize::template FromTo; + using SC = typename ConversionSizes::OrigSize::template FromTo; template - using SCR = SC; + using SCR = SC; template - using SCRR = GenSingleRange; - } - - template - Slice> tcast(MultiArray& ma) - { - return Slice> - ( ma.range()->space(), reinterpret_cast( ma.data() ) ); - } - - template - ConstSlice> tcast(const MultiArray& ma) - { - return ConstSlice> - ( ma.range()->space(), reinterpret_cast( ma.data() ) ); + using SCRR = GenSingleRange::SIZE>; } + template + struct SubTuple + { + template + static inline auto mk(const RTP& rtp, const Ranges&... rs) + -> decltype(SubTuple::mk(rtp, std::get(rtp), rs...)) + { + return SubTuple::mk(rtp, std::get(rtp), rs...); + } + }; + + template <> + struct SubTuple<0> + { + template + static inline auto mk(const RTP& rtp, const Ranges&... rs) + -> decltype(std::make_tuple(std::get<0>(rtp), rs...)) + { + return std::make_tuple(std::get<0>(rtp), rs...); + } + }; + + template + using LastR = typename std::tuple_element>::type; + + template + auto rtcast(const std::tuple...>& rtp) + -> decltype(std::tuple_cat(SubTuple::mk(rtp), + std::make_tuple( std::dynamic_pointer_cast>> + ( SCRR>::factory().create() ) ) )) + { + return std::tuple_cat(SubTuple::mk(rtp), + std::make_tuple( std::dynamic_pointer_cast>> + ( SCRR>::factory().create() ) ) ); + } + + template + inline Slice rangeTpToSlice( const std::tuple...>& rtp, T* data ) + { + return Slice(rtp, data); + } + + template + inline ConstSlice rangeTpToSlice( const std::tuple...>& rtp, const T* data ) + { + return ConstSlice(rtp, data); + } + + template + auto tcast(MultiArray& ma) + -> decltype(rangeTpToSlice + ( rtcast( ma.range()->space() ), + reinterpret_cast( ma.data() ) )) + { + // VCHECK(reinterpret_cast(ma.data()) % 32); + //VCHECK(reinterpret_cast(reinterpret_cast(ma.data())) % 32); + return rangeTpToSlice + ( rtcast( ma.range()->space() ), + reinterpret_cast( ma.data() ) ); + } + + template + auto tcast(const MultiArray& ma) + -> decltype(rangeTpToSlice + ( rtcast( ma.range()->space() ), + reinterpret_cast( ma.data() ) )) + { + //VCHECK(reinterpret_cast(ma.data()) % 32); + //VCHECK(reinterpret_cast(reinterpret_cast(ma.data())) % 32); + return rangeTpToSlice + ( rtcast( ma.range()->space() ), + reinterpret_cast( ma.data() ) ); + } } - #endif diff --git a/src/include/functional_multi_array.h b/src/include/functional_multi_array.h index 7f4c50b..2e10629 100644 --- a/src/include/functional_multi_array.h +++ b/src/include/functional_multi_array.h @@ -15,7 +15,7 @@ namespace MultiArrayTools static auto mk(const std::shared_ptr& i) -> MultiArray { - std::vector vv(i->range()->size()); + vector vv(i->range()->size()); for(Index j = (*i); j.pos() != j.max(); ++j){ vv[j.pos()] = j.meta(); } diff --git a/src/include/helper_tools.cc.h b/src/include/helper_tools.cc.h index fc09571..28750a3 100644 --- a/src/include/helper_tools.cc.h +++ b/src/include/helper_tools.cc.h @@ -109,7 +109,7 @@ namespace MultiArrayTools } template - auto createRange(const std::vector& cvec) + auto createRange(const vector& cvec) -> std::shared_ptr { const char* dp = cvec.data(); diff --git a/src/include/helper_tools.h b/src/include/helper_tools.h index 1eaa9a6..aa35dd1 100644 --- a/src/include/helper_tools.h +++ b/src/include/helper_tools.h @@ -61,7 +61,7 @@ namespace MultiArrayTools -> std::shared_ptr; template - auto createRange(const std::vector& cvec) + auto createRange(const vector& cvec) -> std::shared_ptr; template diff --git a/src/include/map_range.cc.h b/src/include/map_range.cc.h index c4841d3..2f661a6 100644 --- a/src/include/map_range.cc.h +++ b/src/include/map_range.cc.h @@ -347,7 +347,7 @@ namespace MultiArrayTools } } if(not check){ - std::vector pv(sizeof...(Ranges)); + vector pv(sizeof...(Ranges)); RPackNum::RangesToVec(ptp, pv); pv.push_back( reinterpret_cast ( &std::dynamic_pointer_cast( mProd )->mMapf ) ); @@ -376,8 +376,8 @@ namespace MultiArrayTools mult[mapf[ii]]++; } - std::vector outmeta(mult.size()); - std::vector outmult(mult.size()); + vector outmeta(mult.size()); + vector outmult(mult.size()); size_t cnt = 0; for(auto& x: mult){ @@ -404,12 +404,12 @@ namespace MultiArrayTools for(auto ii = mapf.begin(); ii.max() != ii.pos(); ++ii) { max = mapf[ii]+1 > max ? mapf[ii]+1 : max; } - std::vector mult(max,0); + vector mult(max,0); for(auto ii = mapf.begin(); ii.max() != ii.pos(); ++ii) { mult[mapf[ii]]++; } - std::vector outmult(mult.size()); + vector outmult(mult.size()); size_t cnt = 0; for(auto& x: mult){ @@ -505,10 +505,10 @@ namespace MultiArrayTools } template - std::vector GenMapRange::data() const + vector GenMapRange::data() const { DataHeader h = dataHeader(); - std::vector out; + vector out; //out.reserve(h.metaSize + sizeof(DataHeader)); char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); diff --git a/src/include/map_range.h b/src/include/map_range.h index 61c7d43..0406c7c 100644 --- a/src/include/map_range.h +++ b/src/include/map_range.h @@ -259,7 +259,7 @@ namespace MultiArrayTools virtual DataHeader dataHeader() const final; virtual std::string stringMeta(size_t pos) const final; - virtual std::vector data() const final; + virtual vector data() const final; const Space& space() const; diff --git a/src/include/map_range_factory_product_map.h b/src/include/map_range_factory_product_map.h index 992a822..d02d7c0 100644 --- a/src/include/map_range_factory_product_map.h +++ b/src/include/map_range_factory_product_map.h @@ -18,7 +18,7 @@ namespace MultiArrayTools friend class GenMapRangeFactory; private: - static std::map,std::vector > mAleadyCreated; + static std::map,vector > mAleadyCreated; }; } diff --git a/src/include/mbase_def.h b/src/include/mbase_def.h index 72a8b7f..2c53cb2 100644 --- a/src/include/mbase_def.h +++ b/src/include/mbase_def.h @@ -3,6 +3,7 @@ #define __mbase_def_h__ #include "ranges/rbase_def.h" +#include "allocator.h" namespace MultiArrayTools { diff --git a/src/include/multi_array.cc.h b/src/include/multi_array.cc.h index 55af2c7..1395490 100644 --- a/src/include/multi_array.cc.h +++ b/src/include/multi_array.cc.h @@ -7,7 +7,7 @@ namespace MultiArrayTools Scalar scalar(const T& in) { NullRF nrf; - return Scalar( std::dynamic_pointer_cast( nrf.create() ), std::vector( { in } ) ); + return Scalar( std::dynamic_pointer_cast( nrf.create() ), vector( { in } ) ); } /******************* @@ -24,7 +24,7 @@ namespace MultiArrayTools template MultiArray::MultiArray(const typename CRange::Space& space, - std::vector&& vec) : + vector&& vec) : MutableMultiArrayBase(space), mCont(vec) { @@ -52,7 +52,7 @@ namespace MultiArrayTools } template - MultiArray::MultiArray(const std::shared_ptr&... ranges, const std::vector& vec) : + MultiArray::MultiArray(const std::shared_ptr&... ranges, const vector& vec) : MutableMultiArrayBase(ranges...), mCont(vec) { @@ -63,7 +63,7 @@ namespace MultiArrayTools } template - MultiArray::MultiArray(const std::shared_ptr&... ranges, std::vector&& vec) : + MultiArray::MultiArray(const std::shared_ptr&... ranges, vector&& vec) : MutableMultiArrayBase(ranges...), mCont(vec) { diff --git a/src/include/multi_array.h b/src/include/multi_array.h index 18ec445..aaa54ff 100644 --- a/src/include/multi_array.h +++ b/src/include/multi_array.h @@ -24,7 +24,6 @@ namespace MultiArrayTools return ma; } }; - template class MultiArray : public MutableMultiArrayBase @@ -41,15 +40,15 @@ namespace MultiArrayTools DEFAULT_MEMBERS(MultiArray); MultiArray(const std::shared_ptr&... ranges); MultiArray(const std::shared_ptr&... ranges, const T& val); - MultiArray(const std::shared_ptr&... ranges, const std::vector& vec); - MultiArray(const std::shared_ptr&... ranges, std::vector&& vec); + MultiArray(const std::shared_ptr&... ranges, const vector& vec); + MultiArray(const std::shared_ptr&... ranges, vector&& vec); MultiArray(const typename CRange::Space& space); - MultiArray(const typename CRange::Space& space, std::vector&& vec); + MultiArray(const typename CRange::Space& space, vector&& vec); MultiArray(MultiArray&& ama, SIZET... sizes); // Only if ALL ranges have default extensions: - //MultiArray(const std::vector& vec); - //MultiArray(std::vector&& vec); + //MultiArray(const vector& vec); + //MultiArray(vector&& vec); // template // MultiArray(const MultiArray,Range3> in); @@ -76,8 +75,8 @@ namespace MultiArrayTools virtual const T* data() const override; virtual T* data() override; - virtual std::vector& vdata() { return mCont; } - virtual const std::vector& vdata() const { return mCont; } + virtual vector& vdata() { return mCont; } + virtual const vector& vdata() const { return mCont; } virtual std::shared_ptr > anonymous(bool slice = false) const override; virtual std::shared_ptr > anonymousMove() override; @@ -99,7 +98,7 @@ namespace MultiArrayTools private: - std::vector mCont; + vector mCont; }; template @@ -119,7 +118,7 @@ namespace MultiArrayTools const size_t smas = sma.size(); const size_t mas = ma.size(); auto cr = ma.range()->cat(sma.range()); - std::vector ov; + vector ov; ov.reserve(mas * smas); for(auto& x: ma){ diff --git a/src/include/ranges/anonymous_range.h b/src/include/ranges/anonymous_range.h index 0099258..78ad36c 100644 --- a/src/include/ranges/anonymous_range.h +++ b/src/include/ranges/anonymous_range.h @@ -36,7 +36,7 @@ namespace MultiArrayTools template AnonymousRangeFactory(std::shared_ptr... origs); - AnonymousRangeFactory(const std::vector>& origs); + AnonymousRangeFactory(const vector>& origs); template void append(std::shared_ptr r); @@ -45,9 +45,9 @@ namespace MultiArrayTools private: - std::shared_ptr checkIfCreated(const std::vector >& pvec); + std::shared_ptr checkIfCreated(const vector >& pvec); - static std::map,std::vector > mAleadyCreated; + static std::map,vector > mAleadyCreated; bool mProductCreated = false; }; @@ -81,7 +81,7 @@ namespace MultiArrayTools virtual DataHeader dataHeader() const final; virtual std::string stringMeta(size_t pos) const final; - virtual std::vector data() const final; + virtual vector data() const final; std::shared_ptr sub(size_t num) const; @@ -91,10 +91,10 @@ namespace MultiArrayTools template std::shared_ptr > scast(SIZET... sizes) const; // save cast - const std::vector >& orig() const; + const vector >& orig() const; std::shared_ptr sreplace(const std::shared_ptr in, size_t num) const; - std::shared_ptr sreplace(const std::vector>& in, size_t num) const; + std::shared_ptr sreplace(const vector>& in, size_t num) const; bool isEmpty() const; @@ -114,12 +114,12 @@ namespace MultiArrayTools template GenSingleRange(std::shared_ptr... origs); - GenSingleRange(const std::vector>& origs); + GenSingleRange(const vector>& origs); size_t mSize = 1; bool mEmpty = true; - std::vector > mOrig; + vector > mOrig; }; } @@ -172,7 +172,7 @@ namespace MultiArrayHelper template <> inline void resolveSetRange(std::shared_ptr& rp, - const std::vector >& orig, + const vector >& orig, size_t origpos, size_t size) { AnonymousRangeFactory arf; @@ -184,7 +184,7 @@ namespace MultiArrayHelper } template <> - inline void setRangeToVec(std::vector >& v, + inline void setRangeToVec(vector >& v, std::shared_ptr r) { if(not r->isEmpty()){ diff --git a/src/include/ranges/dynamic_meta.h b/src/include/ranges/dynamic_meta.h index 5f054e8..29346f7 100644 --- a/src/include/ranges/dynamic_meta.h +++ b/src/include/ranges/dynamic_meta.h @@ -5,6 +5,8 @@ #include #include +#include "allocator.h" + namespace MultiArrayTools { @@ -14,7 +16,7 @@ namespace MultiArrayTools struct DynamicMetaSetter { template - static inline void set(const std::tuple& in, std::vector& meta) + static inline void set(const std::tuple& in, vector& meta) { typedef typename std::tuple_element>::type elemtype; meta[N].first = reinterpret_cast( &std::get(in) ); @@ -27,7 +29,7 @@ namespace MultiArrayTools struct DynamicMetaSetter<0> { template - static inline void set(const std::tuple& in, std::vector& meta) + static inline void set(const std::tuple& in, vector& meta) { typedef typename std::tuple_element<0,std::tuple>::type elemtype; meta[0].first = reinterpret_cast( &std::get<0>(in) ); @@ -39,7 +41,7 @@ namespace MultiArrayTools class DynamicMetaT { private: - std::vector mMeta; + vector mMeta; public: DynamicMetaT() = default; diff --git a/src/include/ranges/dynamic_range.cc.h b/src/include/ranges/dynamic_range.cc.h index 254b5c5..69a7980 100644 --- a/src/include/ranges/dynamic_range.cc.h +++ b/src/include/ranges/dynamic_range.cc.h @@ -72,7 +72,7 @@ namespace MultiArrayTools } template - DynamicRangeFactory::DynamicRangeFactory(const std::vector>& origs) + DynamicRangeFactory::DynamicRangeFactory(const vector>& origs) { mProd = std::shared_ptr( new DynamicRange( origs ) ); } @@ -99,10 +99,10 @@ namespace MultiArrayTools // INSTANCIATE IF NEEDED!! template - std::map,std::vector > DynamicRangeFactory::mAleadyCreated; + std::map,vector > DynamicRangeFactory::mAleadyCreated; template - std::shared_ptr DynamicRangeFactory::checkIfCreated(const std::vector >& pvec) + std::shared_ptr DynamicRangeFactory::checkIfCreated(const vector >& pvec) { std::shared_ptr out; bool check = false; @@ -122,7 +122,7 @@ namespace MultiArrayTools } } if(not check){ - std::vector app(pvec.size()); + vector app(pvec.size()); for(size_t i = 0; i != app.size(); ++i){ app[i] = reinterpret_cast( pvec[i].get() ); } @@ -223,7 +223,7 @@ namespace MultiArrayTools } template - DynamicIndex& DynamicIndex::operator()(const std::vector>>& ivec) + DynamicIndex& DynamicIndex::operator()(const vector>>& ivec) { assert(mIVec.size() == ivec.size()); for(size_t i = 0; i != mIVec.size(); ++i){ @@ -237,7 +237,7 @@ namespace MultiArrayTools template DynamicIndex& DynamicIndex::operator()(const std::shared_ptr&... is) { - std::vector>> tmp = + vector>> tmp = { std::make_shared>(is)... }; assert(mIVec.size() == tmp.size()); @@ -489,10 +489,10 @@ namespace MultiArrayTools } template - std::vector DynamicRange::data() const + vector DynamicRange::data() const { DataHeader h = dataHeader(); - std::vector out; + vector out; char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); for(auto& x: mOrig){ @@ -571,7 +571,7 @@ namespace MultiArrayTools } template - DynamicRange::DynamicRange(const std::vector>& origs) : + DynamicRange::DynamicRange(const vector>& origs) : RangeInterface>(), mOrig(origs) { @@ -602,7 +602,7 @@ namespace MultiArrayTools } template - const std::vector >& DynamicRange::orig() const + const vector >& DynamicRange::orig() const { return mOrig; } diff --git a/src/include/ranges/dynamic_range.h b/src/include/ranges/dynamic_range.h index 4388eb0..61e3525 100644 --- a/src/include/ranges/dynamic_range.h +++ b/src/include/ranges/dynamic_range.h @@ -172,7 +172,7 @@ namespace MultiArrayTools class DynamicIndex : public IndexInterface,DynamicMetaT> { private: - typedef std::vector>,size_t>> IVecT; + typedef vector>,size_t>> IVecT; IVecT mIVec; @@ -197,7 +197,7 @@ namespace MultiArrayTools DynamicIndex& operator--(); DynamicIndex& operator()(const IVecT& ivec); - DynamicIndex& operator()(const std::vector>>& ivec); + DynamicIndex& operator()(const vector>>& ivec); template DynamicIndex& operator()(const std::shared_ptr&... is); @@ -259,7 +259,7 @@ namespace MultiArrayTools template DynamicRangeFactory(std::shared_ptr... origs); - DynamicRangeFactory(const std::vector>& origs); + DynamicRangeFactory(const vector>& origs); template void append(std::shared_ptr r); @@ -268,9 +268,9 @@ namespace MultiArrayTools private: - std::shared_ptr checkIfCreated(const std::vector >& pvec); + std::shared_ptr checkIfCreated(const vector >& pvec); - static std::map,std::vector > mAleadyCreated; + static std::map,vector > mAleadyCreated; bool mProductCreated = false; }; @@ -299,12 +299,12 @@ namespace MultiArrayTools template DynamicRange(std::shared_ptr... origs); - DynamicRange(const std::vector>& origs); + DynamicRange(const vector>& origs); size_t mSize = 1; bool mEmpty = true; - std::vector > mOrig; + vector > mOrig; public: virtual size_t size() const final; @@ -320,7 +320,7 @@ namespace MultiArrayTools virtual DataHeader dataHeader() const final; virtual std::string stringMeta(size_t pos) const final; - virtual std::vector data() const final; + virtual vector data() const final; std::shared_ptr sub(size_t num) const; @@ -330,7 +330,7 @@ namespace MultiArrayTools template std::shared_ptr > scast(SIZET... sizes) const; // save cast - const std::vector >& orig() const; + const vector >& orig() const; void sreplace(const std::shared_ptr in, size_t num); @@ -352,7 +352,7 @@ namespace MultiArrayHelper template inline void resolveSetRange(std::shared_ptr>& rp, - const std::vector >& orig, + const vector >& orig, size_t origpos, size_t size) { DynamicRangeFactory arf; @@ -364,7 +364,7 @@ namespace MultiArrayHelper } template - inline void setRangeToVec(std::vector >& v, + inline void setRangeToVec(vector >& v, std::shared_ptr> r) { if(not r->isEmpty()){ diff --git a/src/include/ranges/multi_range.h b/src/include/ranges/multi_range.h index 9f6ae4e..dcf0eb5 100644 --- a/src/include/ranges/multi_range.h +++ b/src/include/ranges/multi_range.h @@ -201,7 +201,7 @@ namespace MultiArrayTools virtual DataHeader dataHeader() const final; virtual std::string stringMeta(size_t pos) const final; - virtual std::vector data() const final; + virtual vector data() const final; const Space& space() const; @@ -519,7 +519,7 @@ namespace MultiArrayTools } } if(not check){ - std::vector pv(sizeof...(Ranges)); + vector pv(sizeof...(Ranges)); RPackNum::RangesToVec(ptp, pv); MultiRangeFactoryProductMap::mAleadyCreated[mProd] = pv; out = mProd; @@ -590,10 +590,10 @@ namespace MultiArrayTools } template - std::vector MultiRange::data() const + vector MultiRange::data() const { DataHeader h = dataHeader(); - std::vector out; + vector out; //out.reserve(h.metaSize + sizeof(DataHeader)); char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); diff --git a/src/include/ranges/multi_range_factory_product_map.h b/src/include/ranges/multi_range_factory_product_map.h index c6b10df..e9dcb38 100644 --- a/src/include/ranges/multi_range_factory_product_map.h +++ b/src/include/ranges/multi_range_factory_product_map.h @@ -17,7 +17,7 @@ namespace MultiArrayTools friend class MultiRangeFactory; private: - static std::map,std::vector > mAleadyCreated; + static std::map,vector > mAleadyCreated; }; } diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index fb5605b..846070e 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -81,7 +81,7 @@ namespace MultiArrayTools std::intptr_t id() const; virtual std::string stringMeta(size_t pos) const = 0; - virtual std::vector data() const = 0; // usefull when writing to files, etc... + virtual vector data() const = 0; // usefull when writing to files, etc... virtual SpaceType spaceType() const = 0; virtual DataHeader dataHeader() const = 0; diff --git a/src/include/ranges/range_types/classic_range.h b/src/include/ranges/range_types/classic_range.h index f9b4ed6..ee4ae3c 100644 --- a/src/include/ranges/range_types/classic_range.h +++ b/src/include/ranges/range_types/classic_range.h @@ -43,7 +43,7 @@ namespace MultiArrayTools virtual DataHeader dataHeader() const final; virtual std::string stringMeta(size_t pos) const final; - virtual std::vector data() const final; + virtual vector data() const final; size_t get(size_t pos) const; size_t getMeta(size_t metaPos) const; diff --git a/src/include/ranges/range_types/null_range.h b/src/include/ranges/range_types/null_range.h index 994bb29..a7db938 100644 --- a/src/include/ranges/range_types/null_range.h +++ b/src/include/ranges/range_types/null_range.h @@ -47,7 +47,7 @@ namespace MultiArrayTools virtual size_t dim() const final; virtual std::string stringMeta(size_t pos) const final; - virtual std::vector data() const final; + virtual vector data() const final; virtual SpaceType spaceType() const final; virtual DataHeader dataHeader() const final; diff --git a/src/include/ranges/range_types/space_range.h b/src/include/ranges/range_types/space_range.h index 46d660a..8235ff4 100644 --- a/src/include/ranges/range_types/space_range.h +++ b/src/include/ranges/range_types/space_range.h @@ -39,7 +39,7 @@ namespace MultiArrayTools virtual size_t dim() const final; virtual std::string stringMeta(size_t pos) const final; - virtual std::vector data() const final; + virtual vector data() const final; virtual SpaceType spaceType() const final; virtual DataHeader dataHeader() const final; diff --git a/src/include/ranges/range_types/spin_range.h b/src/include/ranges/range_types/spin_range.h index 97992b5..d720f94 100644 --- a/src/include/ranges/range_types/spin_range.h +++ b/src/include/ranges/range_types/spin_range.h @@ -40,7 +40,7 @@ namespace MultiArrayTools virtual size_t dim() const final; virtual std::string stringMeta(size_t pos) const final; - virtual std::vector data() const final; + virtual vector data() const final; virtual SpaceType spaceType() const final; virtual DataHeader dataHeader() const final; diff --git a/src/include/ranges/rbase_def.h b/src/include/ranges/rbase_def.h index 64ccc45..41c0a28 100644 --- a/src/include/ranges/rbase_def.h +++ b/src/include/ranges/rbase_def.h @@ -14,7 +14,7 @@ << " in " << __func__ << ": " << #a << " = " << a << std::endl; #endif - +#include "allocator.h" namespace MultiArrayTools { diff --git a/src/include/ranges/rpack_num.h b/src/include/ranges/rpack_num.h index a583455..7cabbe0 100644 --- a/src/include/ranges/rpack_num.h +++ b/src/include/ranges/rpack_num.h @@ -46,7 +46,7 @@ namespace MultiArrayHelper template - inline void resolveSetRange(std::shared_ptr& rp, const std::vector >& orig, + inline void resolveSetRange(std::shared_ptr& rp, const vector >& orig, size_t origpos, size_t size) { assert(size == 1); @@ -54,7 +54,7 @@ namespace MultiArrayHelper } template - inline void setRangeToVec(std::vector >& v, + inline void setRangeToVec(vector >& v, std::shared_ptr r) { v.insert(v.begin(), r); @@ -251,7 +251,7 @@ namespace MultiArrayHelper template static inline void RangesToVec(const std::tuple...>& rst, - std::vector >& v) + vector >& v) { setRangeToVec(v, std::get(rst)); RPackNum::RangesToVec(rst, v); @@ -259,7 +259,7 @@ namespace MultiArrayHelper template static inline void RangesToVec(const std::tuple...>& rst, - std::vector& v) + vector& v) { v[N] = reinterpret_cast( std::get(rst).get() ); RPackNum::RangesToVec(rst, v); @@ -324,7 +324,7 @@ namespace MultiArrayHelper } template - static inline void resolveRangeType(const std::vector >& orig, + static inline void resolveRangeType(const vector >& orig, RangeTuple& rtp, size_t off, size_t size, SIZET... sizes) { constexpr size_t tps = std::tuple_size::value; @@ -334,7 +334,7 @@ namespace MultiArrayHelper template static inline bool checkIfCreated(const std::tuple...>& p, - const std::vector& a) + const vector& a) { return reinterpret_cast( std::get(p).get() ) == a[N] and RPackNum::checkIfCreated(p,a); @@ -347,22 +347,22 @@ namespace MultiArrayHelper } template - static inline void fillRangeDataVec(std::vector& out, + static inline void fillRangeDataVec(vector& out, const std::tuple...>& tp) { - std::vector part = std::get(tp)->data(); + vector part = std::get(tp)->data(); out.insert(out.end(), part.begin(), part.end()); RPackNum::fillRangeDataVec(out, tp); } template - static inline bool compareSpaceTypes(const std::vector >& rbvec) + static inline bool compareSpaceTypes(const vector >& rbvec) { return rbvec[SIZE-N-1]->spaceType() == Range::STYPE and RPackNum::template compareSpaceTypes(rbvec); } template - static inline void setSpace(const std::vector >& rbvec, + static inline void setSpace(const vector >& rbvec, std::tuple...>& stp) { typedef typename std::remove_reference( stp ))>::type RType; @@ -529,14 +529,14 @@ namespace MultiArrayHelper template static inline void RangesToVec(const std::tuple...>& rst, - std::vector& v) + vector& v) { v[0] = reinterpret_cast( std::get<0>(rst).get() );; } template static inline void RangesToVec(const std::tuple...>& rst, - std::vector >& v) + vector >& v) { setRangeToVec(v, std::get<0>(rst)); } @@ -596,7 +596,7 @@ namespace MultiArrayHelper } template - static inline void resolveRangeType(const std::vector >& orig, + static inline void resolveRangeType(const vector >& orig, RangeTuple& rtp, size_t off, size_t size) { constexpr size_t tps = std::tuple_size::value; @@ -605,7 +605,7 @@ namespace MultiArrayHelper template static inline bool checkIfCreated(const std::tuple...>& p, - const std::vector& a) + const vector& a) { return reinterpret_cast( std::get<0>(p).get() ) == a[0]; } @@ -617,21 +617,21 @@ namespace MultiArrayHelper } template - static inline void fillRangeDataVec(std::vector& out, + static inline void fillRangeDataVec(vector& out, const std::tuple...>& tp) { - std::vector part = std::get(tp)->data(); + vector part = std::get(tp)->data(); out.insert(out.end(), part.begin(), part.end()); } template - static inline bool compareSpaceTypes(const std::vector >& rbvec) + static inline bool compareSpaceTypes(const vector >& rbvec) { return rbvec[SIZE-1]->spaceType() == Range::STYPE; } template - static inline void setSpace(const std::vector >& rbvec, + static inline void setSpace(const vector >& rbvec, std::tuple...>& stp) { typedef typename std::remove_reference( stp ))>::type RType; diff --git a/src/include/ranges/single_range.h b/src/include/ranges/single_range.h index 57d76f7..dd88c27 100644 --- a/src/include/ranges/single_range.h +++ b/src/include/ranges/single_range.h @@ -107,9 +107,8 @@ namespace MultiArrayTools typedef GenSingleRange oType; GenSingleRangeFactory() = delete; - GenSingleRangeFactory(const std::vector& space); + GenSingleRangeFactory(const vector& space); std::shared_ptr create(); - }; template @@ -125,7 +124,7 @@ namespace MultiArrayTools MetaMap& operator=(MetaMap&& in) = default; //MetaMap(const std::map& in) : mMap(in) {} - MetaMap(const std::vector& in) + MetaMap(const vector& in) { for(size_t i = 0; i != in.size(); ++i){ mMap[in[i]] = i; @@ -140,7 +139,7 @@ namespace MultiArrayTools class MetaMap > { private: - std::vector mMap; + vector mMap; int min1; int min2; int max1; @@ -156,7 +155,7 @@ namespace MultiArrayTools MetaMap& operator=(const MetaMap& in) = default; MetaMap& operator=(MetaMap&& in) = default; - MetaMap(const std::vector& in) : min1(in[0][0]), + MetaMap(const vector& in) : min1(in[0][0]), min2(in[0][1]), max1(in[0][0]), max2(in[0][1]) @@ -201,6 +200,20 @@ namespace MultiArrayTools static constexpr size_t SIZE = -1; }; + template + struct CheckDefault + { + static constexpr size_t ISDEFAULT = false; + static constexpr size_t HASMETACONT = true; + }; + + template <> + struct CheckDefault + { + static constexpr size_t ISDEFAULT = true; + static constexpr size_t HASMETACONT = false; + }; + template class GenSingleRange : public RangeInterface > { @@ -219,7 +232,7 @@ namespace MultiArrayTools virtual DataHeader dataHeader() const final; virtual std::string stringMeta(size_t pos) const final; - virtual std::vector data() const final; + virtual vector data() const final; bool isMeta(const U& metaPos) const; @@ -231,19 +244,26 @@ namespace MultiArrayTools friend GenSingleRangeFactory; - static constexpr bool defaultable = false; + static GenSingleRangeFactory factory() + { + static_assert( not CheckDefault::HASMETACONT, + "asked for default factory for meta data containing range" ); + return GenSingleRangeFactory(vector()); + } + + static constexpr bool defaultable = CheckDefault::ISDEFAULT; static constexpr size_t ISSTATIC = CheckStatic::ISSTATIC; static constexpr size_t SIZE = CheckStatic::SIZE; - static constexpr bool HASMETACONT = true; + static constexpr bool HASMETACONT = CheckDefault::HASMETACONT; protected: GenSingleRange() = delete; GenSingleRange(const GenSingleRange& in) = delete; - GenSingleRange(const std::vector& space); + GenSingleRange(const vector& space); - std::vector mSpace; + vector mSpace; //std::map mMSpace; MetaMap mMSpace; }; @@ -471,7 +491,7 @@ namespace MultiArrayTools ********************/ template - GenSingleRangeFactory::GenSingleRangeFactory(const std::vector& space) + GenSingleRangeFactory::GenSingleRangeFactory(const vector& space) { mProd = std::shared_ptr( new GenSingleRange( space ) ); } @@ -488,7 +508,7 @@ namespace MultiArrayTools ********************/ template - GenSingleRange::GenSingleRange(const std::vector& space) : + GenSingleRange::GenSingleRange(const vector& space) : RangeInterface >(), mSpace(space), mMSpace(mSpace) { @@ -509,10 +529,22 @@ namespace MultiArrayTools return mMSpace.at(metaPos); } + template + inline size_t getStatSizeOrDyn(size_t size) + { + return SIZE; + } + + template <> + inline size_t getStatSizeOrDyn<-1>(size_t size) + { + return size; + } + template size_t GenSingleRange::size() const { - return mSpace.size(); + return getStatSizeOrDyn(mSpace.size()); } template @@ -540,10 +572,10 @@ namespace MultiArrayTools } template - std::vector GenSingleRange::data() const + vector GenSingleRange::data() const { DataHeader h = dataHeader(); - std::vector out; + vector out; out.reserve(h.metaSize + sizeof(DataHeader)); char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); diff --git a/src/include/ranges/subrange.h b/src/include/ranges/subrange.h index 76e5fa2..d963549 100644 --- a/src/include/ranges/subrange.h +++ b/src/include/ranges/subrange.h @@ -100,7 +100,7 @@ namespace MultiArrayTools SubRangeFactory() = delete; SubRangeFactory(const std::shared_ptr& fullRange, - const std::vector& subset); + const vector& subset); std::shared_ptr create(); }; @@ -121,7 +121,7 @@ namespace MultiArrayTools virtual DataHeader dataHeader() const final; virtual std::string stringMeta(size_t pos) const final; - virtual std::vector data() const final; + virtual vector data() const final; bool isMeta(const MetaType& metaPos) const; @@ -132,7 +132,7 @@ namespace MultiArrayTools virtual IndexType end() const final; std::shared_ptr fullRange() const; - const std::vector& subset() const; + const vector& subset() const; std::shared_ptr> outRange() const; friend SubRangeFactory; @@ -147,10 +147,10 @@ namespace MultiArrayTools SubRange() = delete; SubRange(const SubRange& in) = delete; - SubRange(const std::shared_ptr& fullRange, const std::vector& subset); + SubRange(const std::shared_ptr& fullRange, const vector& subset); std::shared_ptr mFullRange; - std::vector mSubSet; + vector mSubSet; }; } // namespace MultiArrayTools @@ -350,7 +350,7 @@ namespace MultiArrayTools template SubRangeFactory::SubRangeFactory(const std::shared_ptr& fullRange, - const std::vector& subset) + const vector& subset) { mProd = std::shared_ptr( new SubRange( fullRange, subset ) ); } @@ -368,7 +368,7 @@ namespace MultiArrayTools template SubRange::SubRange(const std::shared_ptr& fullRange, - const std::vector& subset) : + const vector& subset) : RangeInterface>(), mFullRange(fullRange), mSubSet(subset) {} @@ -408,14 +408,14 @@ namespace MultiArrayTools } template - std::vector SubRange::data() const + vector SubRange::data() const { DataHeader h = dataHeader(); - std::vector out; + vector out; out.reserve(h.metaSize + sizeof(DataHeader)); char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); - std::vector subvec(mSubSet.size()); + vector subvec(mSubSet.size()); size_t i = 0; for(auto& x: mSubSet){ subvec[i++] = mFullRange->get(x); @@ -477,7 +477,7 @@ namespace MultiArrayTools } template - const std::vector& SubRange::subset() const + const vector& SubRange::subset() const { return mSubSet; } @@ -485,7 +485,7 @@ namespace MultiArrayTools template std::shared_ptr::MetaType,SpaceType::ANY>> SubRange::outRange() const { - std::vector ometa(mSubSet.size()); + vector ometa(mSubSet.size()); size_t i = 0; for(auto& x: mSubSet){ ometa[i++] = mFullRange->get(x); diff --git a/src/include/ranges/type_map.h b/src/include/ranges/type_map.h index fb3c608..1088afd 100644 --- a/src/include/ranges/type_map.h +++ b/src/include/ranges/type_map.h @@ -7,6 +7,8 @@ #include #include +#include "allocator.h" + namespace MultiArrayTools { @@ -23,7 +25,7 @@ namespace MultiArrayTools }; template - inline void stringCat(std::vector& out, const std::vector& in) + inline void stringCat(vector& out, const vector& in) { //for(auto& x: in) { std::cout << x << std::endl; } const char* scp = reinterpret_cast(in.data()); @@ -31,20 +33,20 @@ namespace MultiArrayTools } template - inline size_t metaSize(const std::vector& in) + inline size_t metaSize(const vector& in) { return in.size() * sizeof(T); } template - inline void metaCat(std::vector& vec, const char* begin, size_t size) + inline void metaCat(vector& vec, const char* begin, size_t size) { const T* tp = reinterpret_cast( begin ); vec.insert(vec.end(), tp, tp + size / sizeof(T)); } template <> - inline void stringCat(std::vector& out, const std::vector& in) + inline void stringCat(vector& out, const vector& in) { //for(auto& x: in) { std::cout << x << std::endl; } std::string tmp = ""; @@ -56,7 +58,7 @@ namespace MultiArrayTools } template <> - inline size_t metaSize(const std::vector& in) + inline size_t metaSize(const vector& in) { size_t out = 0; for(auto& x: in){ @@ -66,7 +68,7 @@ namespace MultiArrayTools } template <> - inline void metaCat(std::vector& vec, const char* begin, size_t size) + inline void metaCat(vector& vec, const char* begin, size_t size) { std::string tmp(begin, size); @@ -92,10 +94,10 @@ namespace MultiArrayTools include_type(float,4) include_type(double,5) include_type(std::string,6) - include_type(std::vector,101) - include_type(std::vector,102) - include_type(std::vector,105) - include_type(std::vector,106) + include_type(vector,101) + include_type(vector,102) + include_type(vector,105) + include_type(vector,106) include_type(std::array,201) include_type(std::array,202) include_type(std::array,301) diff --git a/src/include/ranges/value_range.h b/src/include/ranges/value_range.h index 146b010..93b9a23 100644 --- a/src/include/ranges/value_range.h +++ b/src/include/ranges/value_range.h @@ -112,7 +112,7 @@ namespace MultiArrayTools virtual DataHeader dataHeader() const final; virtual std::string stringMeta(size_t pos) const final; - virtual std::vector data() const final; + virtual vector data() const final; U get(size_t pos) const; @@ -361,11 +361,11 @@ namespace MultiArrayTools } template - std::vector ValueRange::data() const + vector ValueRange::data() const { assert(0); DataHeader h = dataHeader(); - std::vector out; + vector out; out.reserve(h.metaSize + sizeof(DataHeader)); char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); diff --git a/src/include/ranges/x_to_string.h b/src/include/ranges/x_to_string.h index 8d00dd4..7dfbc6e 100644 --- a/src/include/ranges/x_to_string.h +++ b/src/include/ranges/x_to_string.h @@ -27,7 +27,7 @@ namespace MultiArrayHelper inline std::string xToString(const DynamicMetaT& x); template - inline std::string xToString(const std::vector& x); + inline std::string xToString(const vector& x); template inline std::string xToString(const std::array& x); @@ -90,7 +90,7 @@ namespace MultiArrayHelper } template - inline std::string xToString(const std::vector& x) + inline std::string xToString(const vector& x) { std::string out = "[ "; for(auto& y: x){ diff --git a/src/include/slice.cc.h b/src/include/slice.cc.h index b777fca..919161a 100644 --- a/src/include/slice.cc.h +++ b/src/include/slice.cc.h @@ -115,7 +115,16 @@ namespace MultiArrayTools Slice::Slice(const std::shared_ptr&... ranges, T* data) : MutableMultiArrayBase(ranges...), mData(data) {} - + + template + Slice::Slice(const std::tuple...>& ranges, + T* data) : + MutableMultiArrayBase(ranges), + mData(data) + { + MAB::mInit = true; + } + template const T& Slice::operator[](const IType& i) const { diff --git a/src/include/slice.h b/src/include/slice.h index e727a46..32daf22 100644 --- a/src/include/slice.h +++ b/src/include/slice.h @@ -66,6 +66,8 @@ namespace MultiArrayTools DEFAULT_MEMBERS(Slice); + Slice(const std::tuple...>& ranges, + T* data = nullptr); Slice(const std::shared_ptr&... ranges, T* data = nullptr); virtual const T& operator[](const IType& i) const final; diff --git a/src/include/type_operations.h b/src/include/type_operations.h index efa35c6..564b83b 100644 --- a/src/include/type_operations.h +++ b/src/include/type_operations.h @@ -70,77 +70,77 @@ namespace MultiArrayTools getter(size_t i) : mPos(i) {} - inline T operator()(const std::vector& in) + inline T operator()(const vector& in) { return in[mPos]; } }; template - std::vector& operator+=(std::vector& a, const std::vector& b) + vector& operator+=(vector& a, const vector& b) { std::transform(a.begin(), a.end(), b.begin(), a.begin(), std::plus()); return a; } template - std::vector& operator-=(std::vector& a, const std::vector& b) + vector& operator-=(vector& a, const vector& b) { std::transform(a.begin(), a.end(), b.begin(), a.begin(), std::minus()); return a; } template - std::vector& operator*=(std::vector& a, const std::vector& b) + vector& operator*=(vector& a, const vector& b) { std::transform(a.begin(), a.end(), b.begin(), a.begin(), std::multiplies()); return a; } template - std::vector& operator/=(std::vector& a, const std::vector& b) + vector& operator/=(vector& a, const vector& b) { std::transform(a.begin(), a.end(), b.begin(), a.begin(), std::divides()); return a; } template - std::vector operator+(std::vector& a, const std::vector& b) + vector operator+(vector& a, const vector& b) { - std::vector out(a.size()); + vector out(a.size()); std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::plus()); return out; } template - std::vector operator-(std::vector& a, const std::vector& b) + vector operator-(vector& a, const vector& b) { - std::vector out(a.size()); + vector out(a.size()); std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::minus()); return out; } template - std::vector operator*(std::vector& a, const std::vector& b) + vector operator*(vector& a, const vector& b) { - std::vector out(a.size()); + vector out(a.size()); std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::multiplies()); return out; } template - std::vector operator/(std::vector& a, const std::vector& b) + vector operator/(vector& a, const vector& b) { - std::vector out(a.size()); + vector out(a.size()); std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::divides()); return out; } template - class OperationTemplate,OperationClass> : public OperationBase,OperationClass> + class OperationTemplate,OperationClass> : public OperationBase,OperationClass> { public: - typedef OperationBase,OperationClass> OB; + typedef OperationBase,OperationClass> OB; auto operator[](size_t i) -> Operation,OperationClass> @@ -154,46 +154,55 @@ namespace MultiArrayTools friend OperationClass; }; - template - inline std::array operator+(std::array& a, const std::array& b) + typedef struct v256 { double _x[4]; } v256; + + template + inline void xadd(double* o, const double* a, const double* b) { - std::array out; - for(size_t i = 0; i != N; ++i){ - out[i] = a[i] + b[i]; + //#pragma omp simd aligned(o, a, b: 16) + for(int i = 0; i < N; i++) { + o[i] = a[i] + b[i]; + } + } + + inline v256 operator+(const v256& a, const v256& b) + { + alignas(32) v256 out; + xadd<4>( reinterpret_cast(&out), reinterpret_cast(&a), + reinterpret_cast(&b) ); + return out; + } + /* + inline v256 operator-(const v256& a, const v256& b) + { + alignas(32) v256 out; +#pragma omp simd aligned(outp, ap, bp: 32) + for(int i = 0; i < IN; ++i){ + outp[i] = ap[i] - bp[i]; } return out; } - template - inline std::array operator-(std::array& a, const std::array& b) + inline v256 operator*(const v256& a, const v256& b) { - std::array out; - for(size_t i = 0; i != N; ++i){ - out[i] = a[i] - b[i]; + alignas(32) v256 out; +#pragma omp simd aligned(outp, ap, bp: 32) + for(int i = 0; i < IN; ++i){ + outp[i] = ap[i] * bp[i]; } return out; } - - template - inline std::array operator*(std::array& a, const std::array& b) + + inline v256 operator/(const v256& a, const v256& b) { - std::array out; - for(size_t i = 0; i != N; ++i){ - out[i] = a[i] * b[i]; + alignas(32) v256 out; +#pragma omp simd aligned(outp, ap, bp: 32) + for(int i = 0; i < IN; ++i){ + outp[i] = ap[i] / bp[i]; } return out; } - - template - inline std::array operator/(std::array& a, const std::array& b) - { - std::array out; - for(size_t i = 0; i != N; ++i){ - out[i] = a[i] / b[i]; - } - return out; - } - + */ } // namespace MultiArrayTools diff --git a/src/include/xfor/xfor.h b/src/include/xfor/xfor.h index c854af3..3cda525 100644 --- a/src/include/xfor/xfor.h +++ b/src/include/xfor/xfor.h @@ -9,6 +9,7 @@ #include "xfor/for_utils.h" #include "xfor/exttype.h" +#include "allocator.h" #include #define VCHECK(a) std::cout << __FILE__ << ": @" << __LINE__ \ @@ -16,7 +17,7 @@ namespace MultiArrayHelper { - + using namespace MultiArrayTools; // 'HIDDEN FOR' CLASS for nested for loops in contractions a.s.o. // (NO COUNTING OF MASTER POSITION !!!!!) @@ -152,7 +153,7 @@ namespace MultiArrayHelper typedef decltype(mkExt(0).extend(mExpr.rootSteps())) ExtType; ExtType mExt; - const std::vector* mSubSet; + const vector* mSubSet; mutable ExtType mRootSteps; @@ -169,10 +170,10 @@ namespace MultiArrayHelper SubExpr(const std::shared_ptr& indPtr, std::intptr_t siptr, - const std::vector* subset, Expr expr); + const vector* subset, Expr expr); SubExpr(const IndexClass* indPtr, std::intptr_t siptr, - const std::vector* subset, Expr expr); + const vector* subset, Expr expr); inline void operator()(size_t mlast, DExt last) const override final; @@ -650,7 +651,7 @@ namespace MultiArrayHelper template SubExpr::SubExpr(const std::shared_ptr& indPtr, std::intptr_t siptr, - const std::vector* subset, Expr expr) : + const vector* subset, Expr expr) : mIndPtr(indPtr.get()), mSIPtr(siptr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mExpr(expr), mExt( mkExt(0).extend( mExpr.rootSteps( reinterpret_cast( mIndPtr )) ) ), @@ -661,7 +662,7 @@ namespace MultiArrayHelper template SubExpr::SubExpr(const IndexClass* indPtr, std::intptr_t siptr, - const std::vector* subset, Expr expr) : + const vector* subset, Expr expr) : mIndPtr(indPtr), mSIPtr(siptr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mExpr(expr), mExt( mkExt(0).extend( mExpr.rootSteps( reinterpret_cast( mIndPtr )) ) ), diff --git a/src/lib/map_range_factory_product_map.cc b/src/lib/map_range_factory_product_map.cc index 563ec74..28fbe39 100644 --- a/src/lib/map_range_factory_product_map.cc +++ b/src/lib/map_range_factory_product_map.cc @@ -3,5 +3,5 @@ namespace MultiArrayTools { - std::map,std::vector > MapRangeFactoryProductMap::mAleadyCreated; + std::map,vector > MapRangeFactoryProductMap::mAleadyCreated; } diff --git a/src/lib/ranges/anonymous_range.cc b/src/lib/ranges/anonymous_range.cc index 4b5a337..ae3ecb7 100644 --- a/src/lib/ranges/anonymous_range.cc +++ b/src/lib/ranges/anonymous_range.cc @@ -13,14 +13,14 @@ namespace MultiArrayTools } - std::map,std::vector > AnonymousRangeFactory::mAleadyCreated; + std::map,vector > AnonymousRangeFactory::mAleadyCreated; - AnonymousRangeFactory::AnonymousRangeFactory(const std::vector>& origs) + AnonymousRangeFactory::AnonymousRangeFactory(const vector>& origs) { mProd = std::shared_ptr( new AnonymousRange( origs ) ); } - std::shared_ptr AnonymousRangeFactory::checkIfCreated(const std::vector >& pvec) + std::shared_ptr AnonymousRangeFactory::checkIfCreated(const vector >& pvec) { std::shared_ptr out; bool check = false; @@ -40,7 +40,7 @@ namespace MultiArrayTools } } if(not check){ - std::vector app(pvec.size()); + vector app(pvec.size()); for(size_t i = 0; i != app.size(); ++i){ app[i] = reinterpret_cast( pvec[i].get() ); } @@ -108,10 +108,10 @@ namespace MultiArrayTools return out; } - std::vector AnonymousRange::data() const + vector AnonymousRange::data() const { DataHeader h = dataHeader(); - std::vector out; + vector out; char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); for(auto& x: mOrig){ @@ -168,7 +168,7 @@ namespace MultiArrayTools return std::dynamic_pointer_cast(arf.create()); } - std::shared_ptr AnonymousRange::sreplace(const std::vector>& in, size_t num) const + std::shared_ptr AnonymousRange::sreplace(const vector>& in, size_t num) const { size_t nsize = 1; for(auto& x: in){ @@ -191,12 +191,12 @@ namespace MultiArrayTools return std::dynamic_pointer_cast(arf.create()); } - const std::vector >& AnonymousRange::orig() const + const vector >& AnonymousRange::orig() const { return mOrig; } - GenSingleRange::GenSingleRange(const std::vector>& origs) : + GenSingleRange::GenSingleRange(const vector>& origs) : RangeInterface(), mOrig(origs) { diff --git a/src/lib/ranges/multi_range_factory_product_map.cc b/src/lib/ranges/multi_range_factory_product_map.cc index 30da2dc..226ce7b 100644 --- a/src/lib/ranges/multi_range_factory_product_map.cc +++ b/src/lib/ranges/multi_range_factory_product_map.cc @@ -3,5 +3,5 @@ namespace MultiArrayTools { - std::map,std::vector > MultiRangeFactoryProductMap::mAleadyCreated; + std::map,vector > MultiRangeFactoryProductMap::mAleadyCreated; } diff --git a/src/lib/ranges/range_base.cc b/src/lib/ranges/range_base.cc index bfe422e..6f1d5eb 100644 --- a/src/lib/ranges/range_base.cc +++ b/src/lib/ranges/range_base.cc @@ -15,7 +15,7 @@ namespace MultiArrayTools template using STP = std::tuple...>; - typedef std::vector > RVEC; + typedef vector > RVEC; template inline bool compareSpaceTypes(const RVEC& rvec) @@ -127,7 +127,7 @@ namespace MultiArrayTools assert(0); } #define register_type(x) else if(x == h.metaType) { \ - std::vector::type> vd;\ + vector::type> vd;\ metaCat(vd, *dp, h.metaSize); \ out = std::make_shared::type, \ SpaceType::ANY> >(vd); } diff --git a/src/lib/ranges/range_types/classic_range.cc b/src/lib/ranges/range_types/classic_range.cc index db147d9..7fbd227 100644 --- a/src/lib/ranges/range_types/classic_range.cc +++ b/src/lib/ranges/range_types/classic_range.cc @@ -58,10 +58,10 @@ namespace MultiArrayTools return std::to_string(get(pos)); } - std::vector GenSingleRange::data() const + vector GenSingleRange::data() const { DataHeader h = dataHeader(); - std::vector out; + vector out; out.reserve(h.metaSize + sizeof(DataHeader)); char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); diff --git a/src/lib/ranges/range_types/null_range.cc b/src/lib/ranges/range_types/null_range.cc index 46ac26b..107d4b0 100644 --- a/src/lib/ranges/range_types/null_range.cc +++ b/src/lib/ranges/range_types/null_range.cc @@ -81,10 +81,10 @@ namespace MultiArrayTools return std::to_string(get(pos)); } - std::vector GenSingleRange::data() const + vector GenSingleRange::data() const { DataHeader h = dataHeader(); - std::vector out; + vector out; out.reserve(h.metaSize + sizeof(DataHeader)); char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); diff --git a/src/lib/ranges/range_types/space_range.cc b/src/lib/ranges/range_types/space_range.cc index ae4758e..b867f2a 100644 --- a/src/lib/ranges/range_types/space_range.cc +++ b/src/lib/ranges/range_types/space_range.cc @@ -64,10 +64,10 @@ namespace MultiArrayTools return std::to_string(get(pos)); } - std::vector GenSingleRange::data() const + vector GenSingleRange::data() const { DataHeader h = dataHeader(); - std::vector out; + vector out; out.reserve(h.metaSize + sizeof(DataHeader)); char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); diff --git a/src/lib/ranges/range_types/spin_range.cc b/src/lib/ranges/range_types/spin_range.cc index 8ba73f4..191ae2f 100644 --- a/src/lib/ranges/range_types/spin_range.cc +++ b/src/lib/ranges/range_types/spin_range.cc @@ -61,10 +61,10 @@ namespace MultiArrayTools return std::to_string(get(pos)); } - std::vector GenSingleRange::data() const + vector GenSingleRange::data() const { DataHeader h = dataHeader(); - std::vector out; + vector out; out.reserve(h.metaSize + sizeof(DataHeader)); char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); diff --git a/src/tests/ma_unit_test.cc b/src/tests/ma_unit_test.cc index 705c748..20847ee 100644 --- a/src/tests/ma_unit_test.cc +++ b/src/tests/ma_unit_test.cc @@ -15,7 +15,7 @@ namespace { template void swapFactory(std::shared_ptr& fptr, std::initializer_list ilist) { - std::vector tmp = ilist; + vector tmp = ilist; auto nptr = std::make_shared( tmp ); fptr = nptr; } @@ -50,7 +50,7 @@ namespace { std::shared_ptr rfbptr; std::shared_ptr srptr; - std::vector vv = { 3.141, 2.718, 1.618, 0.693, 0.577 }; + vector vv = { 3.141, 2.718, 1.618, 0.693, 0.577 }; }; @@ -91,7 +91,7 @@ namespace { std::shared_ptr sr3ptr; std::shared_ptr sr4ptr; std::shared_ptr mrptr; - std::vector vv = { 2.917, 9.436, 0.373, 7.192, 7.315, 1.536, 4.892, 0.280, + vector vv = { 2.917, 9.436, 0.373, 7.192, 7.315, 1.536, 4.892, 0.280, 8.870, 4.790, 8.215, 5.063, 1.530, 3.084, 1.609, 4.847, 8.175, 0.112, 6.712, 6.408, 1.959, 0.331, 4.209, 2.951 }; }; @@ -114,7 +114,7 @@ namespace { TEST_F(MATest_1Dim, ForLoop) { - std::vector v2 = { 0.693 , 2.718, 3.141, 1.618, 9.98 }; + vector v2 = { 0.693 , 2.718, 3.141, 1.618, 9.98 }; MultiArray ma(srptr, std::move( v2 ) ); size_t cnt = 0; for(auto el: ma){ diff --git a/src/tests/op_perf_test.cc b/src/tests/op_perf_test.cc index 62accee..90af492 100644 --- a/src/tests/op_perf_test.cc +++ b/src/tests/op_perf_test.cc @@ -5,6 +5,7 @@ #include #include "multi_array_header.h" +#include "conversions.h" #include #include @@ -32,15 +33,15 @@ namespace { template void swapFactory(std::shared_ptr& fptr, std::initializer_list ilist) { - std::vector tmp = ilist; + vector tmp = ilist; auto nptr = std::make_shared( tmp ); fptr = nptr; } template - void swapFactory(std::shared_ptr& fptr, std::vector& ilist) + void swapFactory(std::shared_ptr& fptr, vector& ilist) { - std::vector tmp = ilist; + vector tmp = ilist; auto nptr = std::make_shared( tmp ); fptr = nptr; } @@ -78,14 +79,14 @@ namespace { OpTest_Performance() { - std::vector initvec1(vs1); - cv1.resize(vs1); + vector initvec1(vs1); + cv1.resize(vs1); for(size_t i = 0; i != vs1; ++i){ initvec1[i] = i; cv1[i] = sqrt( static_cast(i)*0.53 ); } - std::vector initvec2(vs2); + vector initvec2(vs2); cv2.resize(vs2*vs1); for(size_t i = 0; i != vs2; ++i){ initvec2[i] = i; @@ -112,14 +113,14 @@ namespace { //const size_t vs2 = 1000; const size_t vs1 = 4000; const size_t vs2 = 2500; - + std::shared_ptr rfbptr; std::shared_ptr sr1ptr; std::shared_ptr sr2ptr; std::shared_ptr mrptr; - std::vector cv1; - std::vector cv2; + vector cv1; + vector cv2; }; #endif class OpTest_Spin @@ -132,12 +133,12 @@ namespace { typedef SpinRF SRF; typedef SpinRange SR; typedef MultiRangeFactory SR8F; - typedef SR8F::oType SR8; - - static const size_t os = 30; - static const size_t is = 65536; - static const size_t s = is*os; - + typedef SR8F::oType SR8; + + static const size_t os = 300; + static const size_t is = 65536; + static const size_t s = is*os; + OpTest_Spin() { data.resize(is); @@ -152,11 +153,11 @@ namespace { cr = std::dynamic_pointer_cast(cf.create()); } - void contract(); + void contract(); private: - std::vector data; + vector data; std::shared_ptr sr; std::shared_ptr cr; }; @@ -172,26 +173,27 @@ namespace { auto beta = MAT::getIndex(); auto gamma = MAT::getIndex(); auto delta = MAT::getIndex(); - auto deltap = MAT::getIndex(); + //auto deltap = MAT::getIndex(); + auto deltap = MAT::getIndex>(); auto mix = MAT::mkMIndex( alpha, beta, gamma, jj ); std::clock_t begin = std::clock(); //for(size_t i = 0; i != os; ++i){ //res1(ii ,delta, deltap).par() += ma(ii, delta, alpha, alpha, beta, beta, gamma, gamma, deltap).c(mix); - res1(ii ,delta, deltap) += ma(delta, alpha, alpha, beta, beta, gamma, gamma, deltap).c(mix); - //} + tcast(res1)(ii ,delta, deltap) += tcast(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; - std::vector vres(4*4*os); + vector vres(4*4*os); for(size_t d = 0; d != 4; ++d){ for(size_t p = 0; p != 4; ++p){ const size_t tidx = d*4 + p; vres[tidx] = 0.; } - } + } std::clock_t begin2 = std::clock(); for(size_t j = 0; j != os; ++j) { for(size_t i = 0; i != os; ++i){ @@ -232,7 +234,7 @@ namespace { assert( xround(res1.at(mkts(0,3,2))) == xround(vres[14]) ); assert( xround(res1.at(mkts(0,3,3))) == xround(vres[15]) ); - std::cout << "std::vector - for loop time: " << static_cast( end2 - begin2 ) / CLOCKS_PER_SEC + std::cout << "vector - for loop time: " << static_cast( end2 - begin2 ) / CLOCKS_PER_SEC << std::endl; std::cout << "ratio: " << static_cast( end - begin ) / static_cast( end2 - begin2 ) << std::endl; } @@ -256,7 +258,7 @@ namespace { std::cout << "MultiArray time: " << static_cast( end - begin ) / CLOCKS_PER_SEC << std::endl; - std::vector res2(vs1*vs2); + vector res2(vs1*vs2); std::clock_t begin2 = std::clock(); for(size_t i = 0; i != vs2; ++i){ @@ -266,7 +268,7 @@ namespace { } std::clock_t end2 = std::clock(); - std::cout << "std::vector - for loop time: " << static_cast( end2 - begin2 ) / CLOCKS_PER_SEC + std::cout << "vector - for loop time: " << static_cast( end2 - begin2 ) / CLOCKS_PER_SEC << std::endl; std::cout << "ratio: " << static_cast( end - begin ) / static_cast( end2 - begin2 ) << std::endl; diff --git a/src/tests/op_unit_test.cc b/src/tests/op_unit_test.cc index 4bd8bfb..350f964 100644 --- a/src/tests/op_unit_test.cc +++ b/src/tests/op_unit_test.cc @@ -31,15 +31,15 @@ namespace { template void swapFactory(std::shared_ptr& fptr, std::initializer_list ilist) { - std::vector tmp = ilist; + vector tmp = ilist; auto nptr = std::make_shared( tmp ); fptr = nptr; } template - void swapFactory(std::shared_ptr& fptr, std::vector& ilist) + void swapFactory(std::shared_ptr& fptr, vector& ilist) { - std::vector tmp = ilist; + vector tmp = ilist; auto nptr = std::make_shared( tmp ); fptr = nptr; } @@ -106,8 +106,8 @@ namespace { std::shared_ptr rfbptr; std::shared_ptr srptr; - std::vector v1 = { 2.917, 9.436, 0.373, 7.192 }; - std::vector v2 = { 8.870, 4.790, 8.215, 5.063 }; + vector v1 = { 2.917, 9.436, 0.373, 7.192 }; + vector v2 = { 8.870, 4.790, 8.215, 5.063 }; }; class OpTest_MDim : public ::testing::Test @@ -151,11 +151,11 @@ namespace { std::shared_ptr sr4ptr; std::shared_ptr mr1ptr; std::shared_ptr mr2ptr; - std::vector v1 = { 2.917, 9.436, 0.373 }; - std::vector v2 = { 8.870, 4.790 }; - std::vector v3 = { 0.353, 4.005, 1.070, 2.310, 9.243, 2.911 }; - std::vector v4 = { 1.470, 2.210 }; - std::vector v5 = { 30.932, -33.693, -26.205, -15.504, 21.227, 17.829, + vector v1 = { 2.917, 9.436, 0.373 }; + vector v2 = { 8.870, 4.790 }; + vector v3 = { 0.353, 4.005, 1.070, 2.310, 9.243, 2.911 }; + vector v4 = { 1.470, 2.210 }; + vector v5 = { 30.932, -33.693, -26.205, -15.504, 21.227, 17.829, -14.364, -1.868, -25.703, 13.836, 23.563, 41.339 }; }; @@ -184,10 +184,10 @@ namespace { std::shared_ptr sr2ptr; std::shared_ptr sr3ptr; - std::vector v1 = { 2.917, 9.436, 0.373, 0.353, 4.005, 1.070, + vector v1 = { 2.917, 9.436, 0.373, 0.353, 4.005, 1.070, -14.364, -1.868, -25.703, 13.836, 23.563, 41.339 }; - std::vector v2 = { 0.353, 4.005, 1.070, 2.310, 9.243, 2.911 }; + vector v2 = { 0.353, 4.005, 1.070, 2.310, 9.243, 2.911 }; }; @@ -222,7 +222,7 @@ namespace { std::shared_ptr sr1ptr; std::shared_ptr sr2ptr; std::shared_ptr mpr1ptr; - std::vector v1 = { -31.71, -77.16, -18.81, + vector v1 = { -31.71, -77.16, -18.81, -67.06, 72.31, -54.48, -50.91, -11.62, -59.57, -42.53, 80.41, 6.35 }; @@ -241,14 +241,14 @@ namespace { OpTest_Performance() { - std::vector initvec1(vs1); + vector initvec1(vs1); cv1.resize(vs1); for(size_t i = 0; i != vs1; ++i){ initvec1[i] = i; cv1[i] = sqrt( static_cast(i)*0.53 ); } - std::vector initvec2(vs2); + vector initvec2(vs2); cv2.resize(vs2*vs1); for(size_t i = 0; i != vs2; ++i){ initvec2[i] = i; @@ -277,8 +277,8 @@ namespace { std::shared_ptr sr2ptr; std::shared_ptr mrptr; - std::vector cv1; - std::vector cv2; + vector cv1; + vector cv2; }; class OpTest_Spin : public ::testing::Test @@ -303,7 +303,7 @@ namespace { sr = std::dynamic_pointer_cast(f.create()); } - std::vector data; + vector data; std::shared_ptr sr; }; @@ -337,8 +337,8 @@ namespace { std::shared_ptr sr1ptr; std::shared_ptr sr2ptr; - std::vector mv1; - std::vector mv2; + vector mv1; + vector mv2; double vcontract(size_t i) { @@ -458,7 +458,7 @@ namespace { res2(delta, deltap).par() += ma(delta, alpha, alpha, beta, beta, gamma, gamma, deltap).c(alpha).c(beta).c(gamma); - std::vector vres(4*4); + vector vres(4*4); std::clock_t begin2 = std::clock(); for(size_t d = 0; d != 4; ++d){ @@ -517,7 +517,7 @@ namespace { EXPECT_EQ( xround(res2.at(mkts(3,2))), xround(vres[14]) ); EXPECT_EQ( xround(res2.at(mkts(3,3))), xround(vres[15]) ); - std::cout << "std::vector - for loop time: " << static_cast( end2 - begin2 ) / CLOCKS_PER_SEC + std::cout << "vector - for loop time: " << static_cast( end2 - begin2 ) / CLOCKS_PER_SEC << std::endl; std::cout << "ratio: " << static_cast( end - begin ) / static_cast( end2 - begin2 ) << std::endl; } @@ -543,7 +543,7 @@ namespace { MultiArray ma1(sr1ptr, sr2ptr, sr3ptr, v1); MultiArray ma2(sr3ptr, sr2ptr, v2); - SubRangeFactory subf(sr2ptr, std::vector({0,2})); + SubRangeFactory subf(sr2ptr, vector({0,2})); auto subptr = MAT::createExplicit(subf); MultiArray res(sr3ptr,sr1ptr,0.); diff --git a/src/tests/ranges/anonymous_unit_test.cc b/src/tests/ranges/anonymous_unit_test.cc index b12065e..cba81cb 100644 --- a/src/tests/ranges/anonymous_unit_test.cc +++ b/src/tests/ranges/anonymous_unit_test.cc @@ -18,7 +18,7 @@ namespace { template void swapFactory(std::shared_ptr& fptr, std::initializer_list ilist) { - std::vector tmp = ilist; + vector tmp = ilist; auto nptr = std::make_shared( tmp ); fptr = nptr; } @@ -160,7 +160,7 @@ namespace { TEST_F(AnonymousTest, ToString1) { - std::vector vv = sr1ptr->data(); + vector vv = sr1ptr->data(); const char* dp = vv.data(); auto ff = createRangeFactory(&dp); auto ss = std::dynamic_pointer_cast( ff->create() ); @@ -183,9 +183,9 @@ namespace { auto sr = std::dynamic_pointer_cast( srf.create() ); auto nr = std::dynamic_pointer_cast( nrf.create() ); - std::vector cv = cr->data(); - std::vector sv = sr->data(); - std::vector nv = nr->data(); + vector cv = cr->data(); + vector sv = sr->data(); + vector nv = nr->data(); const char* cp = cv.data(); const char* sp = sv.data(); const char* np = nv.data(); @@ -212,7 +212,7 @@ namespace { TEST_F(AnonymousTest, ToStringMulti) { - std::vector vv = m3rptr->data(); + vector vv = m3rptr->data(); const char* dp = vv.data(); auto ff2 = std::dynamic_pointer_cast( createRangeFactory(&dp) ); @@ -235,7 +235,7 @@ namespace { { AnonymousRangeFactory arf2(sr1ptr,m3rptr,sr2ptr); auto ar = std::dynamic_pointer_cast( arf2.create() ); - std::vector vv = ar->data(); + vector vv = ar->data(); const char* dp = vv.data(); auto ff2 = std::dynamic_pointer_cast( createRangeFactory(&dp) ); diff --git a/src/tests/ranges/index_unit_test.cc b/src/tests/ranges/index_unit_test.cc index d4289f6..5da0265 100644 --- a/src/tests/ranges/index_unit_test.cc +++ b/src/tests/ranges/index_unit_test.cc @@ -17,7 +17,7 @@ namespace { template void swapFactory(std::shared_ptr& fptr, std::initializer_list ilist) { - std::vector tmp = ilist; + vector tmp = ilist; auto nptr = std::make_shared( tmp ); fptr = nptr; } From 53c433f8dfc9fdea49dca2879d5fc331a007f2d8 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 14 Feb 2019 14:39:59 +0100 Subject: [PATCH 4/9] vectorizes, but not that optimal... --- src/include/arith.h | 6 ++++++ src/include/multi_array_operation.h | 6 +++--- src/include/type_operations.h | 27 +++++++++++++++++++++------ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/include/arith.h b/src/include/arith.h index c3d94b7..5bf1a09 100644 --- a/src/include/arith.h +++ b/src/include/arith.h @@ -86,6 +86,12 @@ namespace MultiArrayTools { return a1 + a2; } + + static inline T& sapply(T& a1, T a2) + { + return a1 += a2; + } + }; template diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index a5a3dfe..31e997f 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -91,9 +91,9 @@ namespace MultiArrayTools template struct SelfIdentity { - static inline T apply(const T& a, const T& b) + static inline T& sapply(T& a, const T& b) { - return b; + return a = b; } }; @@ -140,7 +140,7 @@ namespace MultiArrayTools OperationMaster(T* data, const OpClass& second, IndexType& index, bool doParallel = false); - inline void set(size_t pos, T val) { mDataPtr[pos] = AOp::apply(mDataPtr[pos],val); } + 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; diff --git a/src/include/type_operations.h b/src/include/type_operations.h index 564b83b..8508fb5 100644 --- a/src/include/type_operations.h +++ b/src/include/type_operations.h @@ -154,25 +154,40 @@ namespace MultiArrayTools friend OperationClass; }; - typedef struct v256 { double _x[4]; } v256; + typedef struct v256 { alignas(32) double _x[4]; } v256; template inline void xadd(double* o, const double* a, const double* b) { - //#pragma omp simd aligned(o, a, b: 16) +#pragma omp simd aligned(o, a, b: 32) for(int i = 0; i < N; i++) { o[i] = a[i] + b[i]; } } + + template + inline void xsadd(double* o, const double* a) + { +#pragma omp simd aligned(o, a: 32) + for(int i = 0; i < N; i++) { + o[i] += a[i]; + } + } inline v256 operator+(const v256& a, const v256& b) { - alignas(32) v256 out; - xadd<4>( reinterpret_cast(&out), reinterpret_cast(&a), + alignas(32) v256 o; + xadd<4>( reinterpret_cast(&o), reinterpret_cast(&a), reinterpret_cast(&b) ); - return out; + return o; } - /* + + inline v256& operator+=(v256& o, const v256& a) + { + xsadd<4>( reinterpret_cast(&o), reinterpret_cast(&a) ); + return o; + } +/* inline v256 operator-(const v256& a, const v256& b) { alignas(32) v256 out; From 0559372b0fca251089759a69ec7c284914c85e80 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 14 Feb 2019 16:20:15 +0100 Subject: [PATCH 5/9] march=native --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 77f8ffb..805c8e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8) project(multi_array) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -g -Wpedantic -Ofast -mavx2 -funroll-loops -fopenmp") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -g -Wpedantic -Ofast -march=native -funroll-loops -fopenmp") enable_testing() From 59ffd5adcc0dec6d11ebbd7cff5eaa9c30b798e3 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 14 Feb 2019 22:04:33 +0100 Subject: [PATCH 6/9] use omp simd in performance comparision loop --- src/tests/op_perf_test.cc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/tests/op_perf_test.cc b/src/tests/op_perf_test.cc index 90af492..5f5de8c 100644 --- a/src/tests/op_perf_test.cc +++ b/src/tests/op_perf_test.cc @@ -195,16 +195,22 @@ namespace { } } std::clock_t begin2 = std::clock(); + double* vrptr = vres.data(); + double* dptr = data.data(); + for(size_t j = 0; j != os; ++j) { for(size_t i = 0; i != os; ++i){ for(size_t a = 0; a != 4; ++a){ for(size_t b = 0; b != 4; ++b){ for(size_t c = 0; c != 4; ++c){ for(size_t d = 0; d != 4; ++d){ - for(size_t p = 0; p != 4; ++p){ - const size_t tidx = i*4*4 + d*4 + p; - const size_t sidx = /*i*65536 +*/ 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]; + const size_t tidx = i*4*4 + d*4; + const size_t sidx = /*i*65536 +*/ d*4*4*4*4*4*4*4 + a*5*4*4*4*4*4 + b*5*4*4*4 + c*5*4; + double* xvrptr = vrptr + tidx; + double* xdptr = dptr + sidx; +#pragma omp simd aligned(xvrptr, xdptr: 32) + for(int p = 0; p < 4; p++){ + xvrptr[p] += xdptr[p]; } } } From 6f320fe9e96abf10e190e667928d6430d3811745 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 26 Feb 2019 18:56:57 +0100 Subject: [PATCH 7/9] still not better... --- src/include/map_range.cc.h | 4 +- src/include/map_range.h | 4 +- src/include/multi_array_operation.cc.h | 123 +++++++++++++++++++------ src/include/multi_array_operation.h | 89 +++++++++++++----- src/include/pack_num.h | 6 +- src/include/type_operations.h | 19 ++-- src/include/xfor/xfor.h | 72 +++++++-------- 7 files changed, 212 insertions(+), 105 deletions(-) diff --git a/src/include/map_range.cc.h b/src/include/map_range.cc.h index 2f661a6..939657c 100644 --- a/src/include/map_range.cc.h +++ b/src/include/map_range.cc.h @@ -29,7 +29,7 @@ namespace MultiArrayTools template inline void OpExpr::operator()(size_t mlast, - ExtType last) const + ExtType last) { constexpr size_t NEXT = Op::SIZE; const ExtType nxpos = last; @@ -42,7 +42,7 @@ namespace MultiArrayTools } template - inline void OpExpr::operator()(size_t mlast) const + inline void OpExpr::operator()(size_t mlast) { const ExtType last; constexpr size_t NEXT = Op::SIZE; diff --git a/src/include/map_range.h b/src/include/map_range.h index 0406c7c..129dd10 100644 --- a/src/include/map_range.h +++ b/src/include/map_range.h @@ -66,8 +66,8 @@ namespace MultiArrayTools OpExpr(const MapF& mapf, const IndexPack& ipack, const std::shared_ptr& oind, size_t step, Expr ex); - inline void operator()(size_t mlast, ExtType last) const; - inline void operator()(size_t mlast = 0) const; + inline void operator()(size_t mlast, ExtType last); + inline void operator()(size_t mlast = 0); auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index 4b1abed..83e1cc0 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -109,18 +109,25 @@ namespace MultiArrayTools /***************************************** * OperationMaster::AssignmentExpr * *****************************************/ - + /* template OperationMaster::AssignmentExpr:: AssignmentExpr(OperationMaster& m, const OpClass& sec) : mM(m), mSec(sec) {} - + */ + template + OperationMaster::AssignmentExpr:: + AssignmentExpr(T* dataPtr, const OpClass& sec) : + mSec(sec), mDataPtr(dataPtr) {} + template inline void OperationMaster::AssignmentExpr:: - operator()(size_t start, ExtType last) const + operator()(size_t start, ExtType last) { //VCHECK(mSec.template get(last)); - mM.set(start, 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 @@ -130,7 +137,30 @@ namespace MultiArrayTools { return mSec.rootSteps(iPtrNum); } + + template + OperationMaster::AddExpr:: + AddExpr(T* dataPtr, const OpClass& sec) : + mSec(sec), mDataPtr(dataPtr) {} + template + inline void OperationMaster::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 + { + return mSec.rootSteps(iPtrNum); + } + /************************* * OperationMaster * @@ -159,15 +189,17 @@ namespace MultiArrayTools template void OperationMaster::performAssignment(std::intptr_t blockIndexNum) { + /* AssignmentExpr ae(*this, mSecond); // Expression to be executed within loop if(mDoParallel){ - const auto ploop = mIndex.pifor( 1, mSecond.loop(ae) ); + auto ploop = mIndex.pifor( 1, mSecond.loop(ae) ); ploop(); // execute overall loop(s) and so internal hidden loops and so the inherited expressions } else { - const auto loop = mIndex.ifor( 1, mSecond.loop(ae) ); + auto loop = mIndex.ifor( 1, mSecond.loop(ae) ); loop(); // execute overall loop(s) and so internal hidden loops and so the inherited expressions } + */ } template @@ -186,11 +218,12 @@ namespace MultiArrayTools ConstOperationRoot(const MultiArrayBase& ma, const std::shared_ptr&... indices) : mDataPtr(ma.data()), + mOrigDataPtr(ma.data()), mIndex( ma.begin() ) { //VCHECK(ma.data()); mIndex(indices...); - mOff = mIndex.pos(); + //mOff = mIndex.pos(); } template @@ -198,35 +231,37 @@ namespace MultiArrayTools ConstOperationRoot(std::shared_ptr > maptr, const std::shared_ptr&... indices) : mDataPtr(maptr->data()), + mOrigDataPtr(maptr->data()), mIndex(maptr->begin()), mMaPtr(maptr) { mIndex(indices...); - mOff = mIndex.pos(); + //mOff = mIndex.pos(); } template ConstOperationRoot:: ConstOperationRoot(const T* data, const IndexType& ind) : mDataPtr(data), + mOrigDataPtr(data), mIndex( ind ) { - mOff = mIndex.pos(); + //mOff = mIndex.pos(); } template template inline T ConstOperationRoot::get(ET pos) const { - return mDataPtr[pos.val()+mOff]; + return mDataPtr[pos.val()/*+mOff*/]; } template template - inline const ConstOperationRoot& ConstOperationRoot::set(ET pos) const + inline ConstOperationRoot& ConstOperationRoot::set(ET pos) { mIndex = pos.val(); - mOff = mIndex.pos(); + mDataPtr = mOrigDataPtr + mIndex.pos(); return *this; } @@ -267,7 +302,7 @@ namespace MultiArrayTools template template - inline const StaticCast& StaticCast::set(ET pos) const + inline StaticCast& StaticCast::set(ET pos) { mOp.set(pos); return *this; @@ -311,7 +346,7 @@ namespace MultiArrayTools template template - inline const MetaOperationRoot& MetaOperationRoot::set(ET pos) const + inline MetaOperationRoot& MetaOperationRoot::set(ET pos) { assert(0); //(*mIndex) = pos.val(); @@ -342,38 +377,65 @@ namespace MultiArrayTools OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices) : mDataPtr(ma.data()), + mOrigDataPtr(ma.data()), mIndex( ma.begin() ) { mIndex(indices...); - mOff = mIndex.pos(); + //mOff = mIndex.pos(); } template OperationRoot:: OperationRoot(T* data, const IndexType& ind) : mDataPtr(data), + mOrigDataPtr(data), mIndex( ind ) { - mOff = mIndex.pos(); + //mOff = mIndex.pos(); } template template - OperationMaster,OpClass,Ranges...> OperationRoot::operator=(const OpClass& in) + OperationRoot& OperationRoot::operator=(const OpClass& in) + //OperationMaster,OpClass,Ranges...> OperationRoot::operator=(const OpClass& in) { - return OperationMaster,OpClass,Ranges...>(mDataPtr, in, mIndex, mDoParallel); + typename OperationMaster,OpClass,Ranges...>::AssignmentExpr ae(mDataPtr, 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 + } + return *this; + //return OperationMaster,OpClass,Ranges...>(mDataPtr, in, mIndex, mDoParallel); } template template - OperationMaster,OpClass,Ranges...> OperationRoot::operator+=(const OpClass& in) + OperationRoot& OperationRoot::operator+=(const OpClass& in) + //OperationMaster,OpClass,Ranges...> OperationRoot::operator+=(const OpClass& in) { - return OperationMaster,OpClass,Ranges...>(mDataPtr, in, mIndex, mDoParallel); + typename OperationMaster,OpClass,Ranges...>::AddExpr ae(mDataPtr, 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 + } + return *this; + //return OperationMaster,OpClass,Ranges...>(mDataPtr, in, mIndex, mDoParallel); } template - OperationMaster,OperationRoot,Ranges...> - OperationRoot::operator=(const OperationRoot& in) + OperationRoot& OperationRoot::operator=(const OperationRoot& in) + //OperationMaster,OperationRoot,Ranges...> + //OperationRoot::operator=(const OperationRoot& in) { return operator= >(in); } @@ -389,15 +451,15 @@ namespace MultiArrayTools template inline T OperationRoot::get(ET pos) const { - return mDataPtr[pos.val()+mOff]; + return mDataPtr[pos.val()/*+mOff*/]; } template template - inline const OperationRoot& OperationRoot::set(ET pos) const + inline OperationRoot& OperationRoot::set(ET pos) { mIndex = pos.val(); - mOff = mIndex.pos(); + mDataPtr = mOrigDataPtr + mIndex.pos(); return *this; } @@ -418,7 +480,8 @@ namespace MultiArrayTools template T* OperationRoot::data() const { - return mDataPtr + mIndex().pos(); + auto i = mIndex; + return mDataPtr + i().pos(); } template @@ -448,7 +511,7 @@ namespace MultiArrayTools template template - inline const OperationValue& OperationValue::set(ET pos) const + inline OperationValue& OperationValue::set(ET pos) { return *this; } @@ -498,7 +561,7 @@ namespace MultiArrayTools template template - inline const Operation& Operation::set(ET pos) const + inline Operation& Operation::set(ET pos) { PackNum::setOpPos(mOps,pos); return *this; @@ -539,7 +602,7 @@ namespace MultiArrayTools template template - inline const Contraction& Contraction::set(ET pos) const + inline Contraction& Contraction::set(ET pos) { mOp.set(pos); return *this; @@ -586,7 +649,7 @@ namespace MultiArrayTools template template - inline const SliceContraction& SliceContraction::set(ET pos) const + inline SliceContraction& SliceContraction::set(ET pos) { mOp.set(pos); return *this; diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index 31e997f..89846d5 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -91,7 +91,7 @@ namespace MultiArrayTools template struct SelfIdentity { - static inline T& sapply(T& a, const T& b) + static inline T& sapply(T& a, T b) { return a = b; } @@ -107,27 +107,57 @@ namespace MultiArrayTools private: AssignmentExpr() = default; - OperationMaster& mM; - const OpClass& mSec; - + //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); + //AssignmentExpr(OperationMaster& m, const OpClass& sec); + AssignmentExpr(T* dataPtr, const OpClass& sec); AssignmentExpr(const AssignmentExpr& in) = default; AssignmentExpr(AssignmentExpr&& in) = default; - inline void operator()(size_t start = 0) const; - inline void operator()(size_t start, ExtType last) const; + 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; @@ -180,7 +210,7 @@ namespace MultiArrayTools inline T get(ET pos) const; template - inline const ConstOperationRoot& set(ET pos) const; + inline ConstOperationRoot& set(ET pos); MExt rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype @@ -193,8 +223,9 @@ namespace MultiArrayTools //MultiArrayBase const& mArrayRef; const T* mDataPtr; - mutable IndexType mIndex; - mutable size_t mOff = 0; + const T* mOrigDataPtr; + IndexType mIndex; + //size_t mOff = 0; std::shared_ptr > mMaPtr; // never remove this ptr, otherwise we lose temporary container instances! }; @@ -202,7 +233,7 @@ namespace MultiArrayTools class StaticCast : public OperationTemplate > { private: - const Op& mOp; + Op mOp; public: @@ -219,7 +250,7 @@ namespace MultiArrayTools inline T get(ET pos) const; template - inline const StaticCast& set(ET pos) const; + inline StaticCast& set(ET pos); auto rootSteps(std::intptr_t iPtrNum = 0) const -> decltype(mOp.rootSteps(iPtrNum)); @@ -253,7 +284,7 @@ namespace MultiArrayTools inline value_type get(ET pos) const; template - inline const MetaOperationRoot& set(ET pos) const; + inline MetaOperationRoot& set(ET pos); MExt rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype @@ -284,7 +315,7 @@ namespace MultiArrayTools const std::shared_ptr&... indices); OperationRoot(T* data, const IndexType& ind); - + /* template OperationMaster,OpClass,Ranges...> operator=(const OpClass& in); @@ -292,14 +323,23 @@ namespace MultiArrayTools OperationMaster,OpClass,Ranges...> operator+=(const OpClass& in); OperationMaster,OperationRoot,Ranges...> operator=(const OperationRoot& in); + */ + template + OperationRoot& operator=(const OpClass& in); + + template + OperationRoot& operator+=(const OpClass& in); + + OperationRoot& operator=(const OperationRoot& in); + OperationRoot& par(); template inline T get(ET pos) const; template - inline const OperationRoot& set(ET pos) const; + inline OperationRoot& set(ET pos); MExt rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype @@ -316,8 +356,9 @@ namespace MultiArrayTools //MutableMultiArrayBase& mArrayRef; T* mDataPtr; - mutable IndexType mIndex; - mutable size_t mOff = 0; + T* mOrigDataPtr; + IndexType mIndex; + //size_t mOff = 0; bool mDoParallel = false; }; @@ -338,7 +379,7 @@ namespace MultiArrayTools inline T get(ET pos) const; template - inline const OperationValue& set(ET pos) const; + inline OperationValue& set(ET pos); MExt rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype @@ -415,7 +456,7 @@ namespace MultiArrayTools inline T get(ET pos) const; template - inline const Operation& set(ET pos) const; + inline Operation& set(ET pos); auto rootSteps(std::intptr_t iPtrNum = 0) const // nullptr for simple usage with decltype -> decltype(PackNum::mkSteps(iPtrNum, mOps)); @@ -473,7 +514,7 @@ namespace MultiArrayTools private: - const Op& mOp; + Op mOp; std::shared_ptr mInd; public: @@ -485,7 +526,7 @@ namespace MultiArrayTools inline T get(ET pos) const; template - inline const Contraction& set(ET pos) const; + inline Contraction& set(ET pos); auto rootSteps(std::intptr_t iPtrNum = 0) const // nullptr for simple usage with decltype -> decltype(mOp.rootSteps(iPtrNum)); @@ -509,7 +550,7 @@ namespace MultiArrayTools private: - const Op& mOp; + mutable Op mOp; mutable std::shared_ptr > mCont; mutable OperationRoot mTarOp; @@ -522,7 +563,7 @@ namespace MultiArrayTools inline const value_type& get(ET pos) const; template - inline const SliceContraction& set(ET pos) const; + inline SliceContraction& set(ET pos); auto rootSteps(std::intptr_t iPtrNum = 0) const // nullptr for simple usage with decltype -> decltype(mOp.rootSteps(iPtrNum)); diff --git a/src/include/pack_num.h b/src/include/pack_num.h index d9bf56e..d369b17 100644 --- a/src/include/pack_num.h +++ b/src/include/pack_num.h @@ -71,7 +71,7 @@ namespace MultiArrayHelper } template - static inline T mkOpExpr(std::shared_ptr f, const ETuple& pos, const OpTuple& ops, const Args&... args) + static inline T mkOpExpr(std::shared_ptr f, const ETuple& pos, const OpTuple& ops, Args... args) { typedef typename std::remove_reference(ops))>::type NextOpType; static_assert(LAST > NextOpType::SIZE, "inconsistent array positions"); @@ -116,7 +116,7 @@ namespace MultiArrayHelper } template - static inline void setOpPos(const OpTuple& ot, const ETuple& et) + static inline void setOpPos(OpTuple& ot, const ETuple& et) { typedef typename std::remove_reference(ot))>::type NextOpType; static_assert(LAST > NextOpType::SIZE, "inconsistent array positions"); @@ -203,7 +203,7 @@ namespace MultiArrayHelper } template - static inline void setOpPos(const OpTuple& ot, const ETuple& et) + static inline void setOpPos(OpTuple& ot, const ETuple& et) { typedef typename std::remove_reference(et))>::type NextOpType; static_assert(LAST > NextOpType::SIZE, "inconsistent array positions"); diff --git a/src/include/type_operations.h b/src/include/type_operations.h index 8508fb5..8f970d9 100644 --- a/src/include/type_operations.h +++ b/src/include/type_operations.h @@ -154,7 +154,10 @@ namespace MultiArrayTools friend OperationClass; }; - typedef struct v256 { alignas(32) double _x[4]; } v256; + struct v256 + { + alignas(32) double _x[4]; + }; template inline void xadd(double* o, const double* a, const double* b) @@ -173,16 +176,16 @@ namespace MultiArrayTools o[i] += a[i]; } } - - inline v256 operator+(const v256& a, const v256& b) + + inline v256 operator+(v256 a, v256 b) { - alignas(32) v256 o; + v256 o; xadd<4>( reinterpret_cast(&o), reinterpret_cast(&a), reinterpret_cast(&b) ); return o; } - inline v256& operator+=(v256& o, const v256& a) + inline v256& operator+=(v256& o, v256 a) { xsadd<4>( reinterpret_cast(&o), reinterpret_cast(&a) ); return o; @@ -190,7 +193,7 @@ namespace MultiArrayTools /* inline v256 operator-(const v256& a, const v256& b) { - alignas(32) v256 out; + v256 out; #pragma omp simd aligned(outp, ap, bp: 32) for(int i = 0; i < IN; ++i){ outp[i] = ap[i] - bp[i]; @@ -200,7 +203,7 @@ namespace MultiArrayTools inline v256 operator*(const v256& a, const v256& b) { - alignas(32) v256 out; + v256 out; #pragma omp simd aligned(outp, ap, bp: 32) for(int i = 0; i < IN; ++i){ outp[i] = ap[i] * bp[i]; @@ -210,7 +213,7 @@ namespace MultiArrayTools inline v256 operator/(const v256& a, const v256& b) { - alignas(32) v256 out; + v256 out; #pragma omp simd aligned(outp, ap, bp: 32) for(int i = 0; i < IN; ++i){ outp[i] = ap[i] / bp[i]; diff --git a/src/include/xfor/xfor.h b/src/include/xfor/xfor.h index 3cda525..7af5458 100644 --- a/src/include/xfor/xfor.h +++ b/src/include/xfor/xfor.h @@ -35,8 +35,8 @@ namespace MultiArrayHelper ExpressionBase& operator=(const ExpressionBase& in) = default; ExpressionBase& operator=(ExpressionBase&& in) = default; - virtual void operator()(size_t mlast, DExt last) const = 0; - virtual void operator()(size_t mlast = 0) const = 0; + virtual void operator()(size_t mlast, DExt last) = 0; + virtual void operator()(size_t mlast = 0) = 0; virtual DExt dRootSteps(std::intptr_t iPtrNum = 0) const = 0; virtual DExt dExtension() const = 0; @@ -127,9 +127,9 @@ namespace MultiArrayHelper Expr expr); - inline void operator()(size_t mlast, DExt last) const override final; - inline void operator()(size_t mlast, ExtType last) const; - inline void operator()(size_t mlast = 0) const override final; + inline void operator()(size_t mlast, DExt last) override final; + inline void operator()(size_t mlast, ExtType last); + inline void operator()(size_t mlast = 0) override final; DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; DExt dExtension() const override final; @@ -176,9 +176,9 @@ namespace MultiArrayHelper const vector* subset, Expr expr); - inline void operator()(size_t mlast, DExt last) const override final; - inline void operator()(size_t mlast, ExtType last) const; - inline void operator()(size_t mlast = 0) const override final; + inline void operator()(size_t mlast, DExt last) override final; + inline void operator()(size_t mlast, ExtType last) ; + inline void operator()(size_t mlast = 0) override final; DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; DExt dExtension() const override final; @@ -224,9 +224,9 @@ namespace MultiArrayHelper For(const IndexClass* indPtr, size_t step, Expr expr); - inline void operator()(size_t mlast, DExt last) const override final; - inline void operator()(size_t mlast, ExtType last) const; - inline void operator()(size_t mlast = 0) const override final; + inline void operator()(size_t mlast, DExt last) override final; + inline void operator()(size_t mlast, ExtType last) ; + inline void operator()(size_t mlast = 0) override final; PFor parallel() const; @@ -272,9 +272,9 @@ namespace MultiArrayHelper PFor(const IndexClass* indPtr, size_t step, Expr expr); - inline void operator()(size_t mlast, DExt last) const override final; - inline void operator()(size_t mlast, ExtType last) const; - inline void operator()(size_t mlast = 0) const override final; + inline void operator()(size_t mlast, DExt last) override final; + inline void operator()(size_t mlast, ExtType last) ; + inline void operator()(size_t mlast = 0) override final; DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; DExt dExtension() const override final; @@ -311,8 +311,8 @@ namespace MultiArrayHelper template DynamicExpression(Expr ex) : mNext( std::make_shared(ex) ) {} - inline void operator()(size_t mlast, DExt last) const override final; - inline void operator()(size_t mlast = 0) const override final; + inline void operator()(size_t mlast, DExt last) override final; + inline void operator()(size_t mlast = 0) override final; inline DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; inline DExt dExtension() const override final; @@ -345,9 +345,9 @@ namespace MultiArrayHelper ExpressionHolder(DynamicExpression expr); - inline void operator()(size_t mlast, DExt last) const override final; - inline void operator()(size_t mlast, ExtType last) const; - inline void operator()(size_t mlast = 0) const override final; + inline void operator()(size_t mlast, DExt last) override final; + inline void operator()(size_t mlast, ExtType last) ; + inline void operator()(size_t mlast = 0) override final; DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; DExt dExtension() const override final; @@ -391,14 +391,14 @@ namespace MultiArrayHelper } template - inline void For::operator()(size_t mlast, DExt last) const + inline void For::operator()(size_t mlast, DExt last) { operator()(mlast, *reinterpret_cast(last.first)); } template inline void For::operator()(size_t mlast, - ExtType last) const + ExtType last) { typedef typename IndexClass::RangeType RangeType; for(size_t pos = 0u; pos != ForBound::template bound(mMax); ++pos){ @@ -411,7 +411,7 @@ namespace MultiArrayHelper } template - inline void For::operator()(size_t mlast) const + inline void For::operator()(size_t mlast) { typedef typename IndexClass::RangeType RangeType; const ExtType last; @@ -484,14 +484,14 @@ namespace MultiArrayHelper } template - inline void PFor::operator()(size_t mlast, DExt last) const + inline void PFor::operator()(size_t mlast, DExt last) { operator()(mlast, *reinterpret_cast(last.first)); } template inline void PFor::operator()(size_t mlast, - ExtType last) const + ExtType last) { CHECK; typedef typename IndexClass::RangeType RangeType; @@ -511,7 +511,7 @@ namespace MultiArrayHelper } template - inline void PFor::operator()(size_t mlast) const + inline void PFor::operator()(size_t mlast) { CHECK; typedef typename IndexClass::RangeType RangeType; @@ -588,14 +588,14 @@ namespace MultiArrayHelper } template - inline void SingleExpression::operator()(size_t mlast, DExt last) const + inline void SingleExpression::operator()(size_t mlast, DExt last) { operator()(mlast, *reinterpret_cast(last.first)); } template inline void SingleExpression::operator()(size_t mlast, - ExtType last) const + ExtType last) { //typedef typename IndexClass::RangeType RangeType; const size_t pos = mIndPtr->pos(); @@ -605,7 +605,7 @@ namespace MultiArrayHelper } template - inline void SingleExpression::operator()(size_t mlast) const + inline void SingleExpression::operator()(size_t mlast) { //typedef typename IndexClass::RangeType RangeType; const ExtType last; @@ -672,14 +672,14 @@ namespace MultiArrayHelper } template - inline void SubExpr::operator()(size_t mlast, DExt last) const + inline void SubExpr::operator()(size_t mlast, DExt last) { operator()(mlast, *reinterpret_cast(last.first)); } template inline void SubExpr::operator()(size_t mlast, - ExtType last) const + ExtType last) { const size_t pos = (*mSubSet)[last.val()]; const size_t mnpos = mlast; @@ -688,7 +688,7 @@ namespace MultiArrayHelper } template - inline void SubExpr::operator()(size_t mlast) const + inline void SubExpr::operator()(size_t mlast) { const ExtType last; const size_t pos = (*mSubSet)[last.val()]; @@ -731,12 +731,12 @@ namespace MultiArrayHelper * DynamicExpression * ***************************/ - inline void DynamicExpression::operator()(size_t mlast, DExt last) const + inline void DynamicExpression::operator()(size_t mlast, DExt last) { (*mNext)(mlast,last); } - inline void DynamicExpression::operator()(size_t mlast) const + inline void DynamicExpression::operator()(size_t mlast) { (*mNext)(mlast); } @@ -759,13 +759,13 @@ namespace MultiArrayHelper ExpressionHolder::ExpressionHolder(DynamicExpression expr) : mExpr(expr) {} template - inline void ExpressionHolder::operator()(size_t mlast, DExt last) const + inline void ExpressionHolder::operator()(size_t mlast, DExt last) { mExpr(mlast,last); } template - inline void ExpressionHolder::operator()(size_t mlast, ExtType last) const + inline void ExpressionHolder::operator()(size_t mlast, ExtType last) { mExpr(mlast, std::make_pair @@ -774,7 +774,7 @@ namespace MultiArrayHelper } template - inline void ExpressionHolder::operator()(size_t mlast) const + inline void ExpressionHolder::operator()(size_t mlast) { mExpr(mlast); } From 09f80749104645afc921a8c84026f3e8eb2a35cd Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 27 Feb 2019 13:37:53 +0100 Subject: [PATCH 8/9] NOW ALMOST --- src/include/multi_array_operation.cc.h | 9 +++++---- src/include/multi_array_operation.h | 24 ++++++++++++++++-------- src/include/type_operations.h | 10 +++++----- src/include/xfor/exttype.h | 8 ++++---- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index 83e1cc0..cd9bb24 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -251,7 +251,7 @@ namespace MultiArrayTools template template - inline T ConstOperationRoot::get(ET pos) const + inline const T& ConstOperationRoot::get(ET pos) const { return mDataPtr[pos.val()/*+mOff*/]; } @@ -449,7 +449,7 @@ namespace MultiArrayTools template template - inline T OperationRoot::get(ET pos) const + inline T& OperationRoot::get(ET pos) const { return mDataPtr[pos.val()/*+mOff*/]; } @@ -504,7 +504,7 @@ namespace MultiArrayTools template template - inline T OperationValue::get(ET pos) const + inline const T& OperationValue::get(ET pos) const { return mVal; } @@ -595,7 +595,8 @@ namespace MultiArrayTools // forward loop !!!! template template - inline T Contraction::get(ET pos) const + inline auto Contraction::get(ET pos) const + -> decltype(mOp.template get(pos)) { return mOp.template get(pos); } diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index 89846d5..450e183 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -197,6 +197,7 @@ namespace MultiArrayTools typedef ContainerIndex IndexType; static constexpr size_t SIZE = 1; + static constexpr bool CONT = true; ConstOperationRoot(const MultiArrayBase& ma, const std::shared_ptr&... indices); @@ -207,7 +208,7 @@ namespace MultiArrayTools ConstOperationRoot(const T* data, const IndexType& ind); template - inline T get(ET pos) const; + inline const T& get(ET pos) const; template inline ConstOperationRoot& set(ET pos); @@ -243,6 +244,7 @@ namespace MultiArrayTools typedef typename Op::IndexType IndexType; static constexpr size_t SIZE = Op::SIZE; + static constexpr bool CONT = false; StaticCast(const Op& op); @@ -277,7 +279,8 @@ namespace MultiArrayTools typedef OperationBase > OT; static constexpr size_t SIZE = 1; - + static constexpr bool CONT = false; + MetaOperationRoot(const std::shared_ptr& ind); template @@ -310,6 +313,7 @@ namespace MultiArrayTools typedef ContainerIndex IndexType; static constexpr size_t SIZE = 1; + static constexpr bool CONT = true; OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices); @@ -336,7 +340,7 @@ namespace MultiArrayTools OperationRoot& par(); template - inline T get(ET pos) const; + inline T& get(ET pos) const; template inline OperationRoot& set(ET pos); @@ -372,11 +376,12 @@ namespace MultiArrayTools typedef ContainerIndex IndexType; static constexpr size_t SIZE = 1; + static constexpr bool CONT = true; OperationValue(const T& val); template - inline T get(ET pos) const; + inline const T& get(ET pos) const; template inline OperationValue& set(ET pos); @@ -441,6 +446,7 @@ namespace MultiArrayTools static constexpr size_t SIZE = RootSum::SIZE; static constexpr bool FISSTATIC = OpFunction::FISSTATIC; + static constexpr bool CONT = false; private: std::tuple mOps; @@ -500,7 +506,6 @@ namespace MultiArrayTools return OpMaker::mkOperation(f, ops...); } - template class Contraction : public OperationTemplate > @@ -511,7 +516,8 @@ namespace MultiArrayTools typedef OperationBase > OT; static constexpr size_t SIZE = Op::SIZE; - + static constexpr bool CONT = Op::CONT; + private: Op mOp; @@ -523,7 +529,8 @@ namespace MultiArrayTools Contraction(const Op& op, std::shared_ptr ind); template - inline T get(ET pos) const; + inline auto get(ET pos) const + -> decltype(mOp.template get(pos)); template inline Contraction& set(ET pos); @@ -547,7 +554,8 @@ namespace MultiArrayTools typedef OperationTemplate > OT; static constexpr size_t SIZE = Op::SIZE; - + static constexpr bool CONT = false; + private: mutable Op mOp; diff --git a/src/include/type_operations.h b/src/include/type_operations.h index 8f970d9..d40d68e 100644 --- a/src/include/type_operations.h +++ b/src/include/type_operations.h @@ -177,17 +177,17 @@ namespace MultiArrayTools } } - inline v256 operator+(v256 a, v256 b) + inline v256 operator+(const v256& a, const v256& b) { v256 o; - xadd<4>( reinterpret_cast(&o), reinterpret_cast(&a), - reinterpret_cast(&b) ); + xadd<4>( o._x, a._x, b._x ); return o; } - inline v256& operator+=(v256& o, v256 a) + inline v256& operator+=(v256& o, const v256& a) { - xsadd<4>( reinterpret_cast(&o), reinterpret_cast(&a) ); + //xsadd<4>( reinterpret_cast(&o), reinterpret_cast(&a) ); + xsadd<4>( o._x, a._x ); return o; } /* diff --git a/src/include/xfor/exttype.h b/src/include/xfor/exttype.h index 65718b3..ffaccf2 100644 --- a/src/include/xfor/exttype.h +++ b/src/include/xfor/exttype.h @@ -36,7 +36,7 @@ namespace MultiArrayHelper template inline MExt(const std::array& arr); - inline size_t val() const; + inline const size_t& val() const; inline const X& next() const; inline MExt operator+(const MExt& in) const; @@ -76,7 +76,7 @@ namespace MultiArrayHelper template inline MExt(const std::array& arr); - inline size_t val() const; + inline const size_t& val() const; inline size_t next() const { return 0; } inline MExt operator+(const MExt& in) const; @@ -151,7 +151,7 @@ namespace MultiArrayHelper mExt(y.val()), mNext(y.next(), z) {} template - inline size_t MExt::val() const + inline const size_t& MExt::val() const { return mExt; } @@ -196,7 +196,7 @@ namespace MultiArrayHelper mExt(std::get(arr)) {} //template <> - inline size_t MExt::val() const + inline const size_t& MExt::val() const { return mExt; } From 25fadae9741bcc2d6e5c815c923996fbac775db0 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 27 Feb 2019 19:02:06 +0100 Subject: [PATCH 9/9] make comparison loop absolutely equivalent to ma test loop -> there is no notable difference in the performance anymore --- src/tests/op_perf_test.cc | 50 +++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/tests/op_perf_test.cc b/src/tests/op_perf_test.cc index 5f5de8c..131db7e 100644 --- a/src/tests/op_perf_test.cc +++ b/src/tests/op_perf_test.cc @@ -9,6 +9,7 @@ #include #include +#include #define ONLY_SPIN @@ -135,7 +136,7 @@ namespace { typedef MultiRangeFactory SR8F; typedef SR8F::oType SR8; - static const size_t os = 300; + static const size_t os = 3000; static const size_t is = 65536; static const size_t s = is*os; @@ -176,17 +177,9 @@ namespace { //auto deltap = MAT::getIndex(); auto deltap = MAT::getIndex>(); - auto mix = MAT::mkMIndex( alpha, beta, gamma, jj ); + auto mix = MAT::mkMIndex( jj, alpha, beta, gamma ); - std::clock_t begin = std::clock(); - //for(size_t i = 0; i != os; ++i){ - //res1(ii ,delta, deltap).par() += ma(ii, delta, alpha, alpha, beta, beta, gamma, gamma, deltap).c(mix); - tcast(res1)(ii ,delta, deltap) += tcast(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; - + vector vres(4*4*os); for(size_t d = 0; d != 4; ++d){ for(size_t p = 0; p != 4; ++p){ @@ -194,16 +187,15 @@ namespace { vres[tidx] = 0.; } } - std::clock_t begin2 = std::clock(); + auto begin2 = std::chrono::system_clock::now(); double* vrptr = vres.data(); double* dptr = data.data(); - - for(size_t j = 0; j != os; ++j) { - for(size_t i = 0; i != os; ++i){ - for(size_t a = 0; a != 4; ++a){ - for(size_t b = 0; b != 4; ++b){ - for(size_t c = 0; c != 4; ++c){ - for(size_t d = 0; d != 4; ++d){ + for(size_t i = 0; i != os; ++i){ + for(size_t d = 0; d != 4; ++d){ + for(size_t j = 0; j != os; ++j) { + 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 tidx = i*4*4 + d*4; const size_t sidx = /*i*65536 +*/ d*4*4*4*4*4*4*4 + a*5*4*4*4*4*4 + b*5*4*4*4 + c*5*4; double* xvrptr = vrptr + tidx; @@ -218,8 +210,20 @@ namespace { } } } - std::clock_t end2 = std::clock(); + auto end2 = std::chrono::system_clock::now(); + std::cout << "vector - for loop time: " << std::chrono::duration(end2-begin2).count() + << std::endl; + + auto begin = std::chrono::system_clock::now(); + //for(size_t i = 0; i != os; ++i){ + //res1(ii ,delta, deltap).par() += ma(ii, delta, alpha, alpha, beta, beta, gamma, gamma, deltap).c(mix); + tcast(res1)(ii ,delta, deltap).par() += tcast(ma)(delta, alpha, alpha, beta, beta, gamma, gamma, deltap).c(mix); + //} + auto end = std::chrono::system_clock::now(); + std::cout << "MultiArray time: " << std::chrono::duration(end-begin).count() + << std::endl; + assert( xround(res1.at(mkts(0,0,0))) == xround(vres[0]) ); assert( xround(res1.at(mkts(0,0,1))) == xround(vres[1]) ); assert( xround(res1.at(mkts(0,0,2))) == xround(vres[2]) ); @@ -240,9 +244,9 @@ namespace { assert( xround(res1.at(mkts(0,3,2))) == xround(vres[14]) ); assert( xround(res1.at(mkts(0,3,3))) == xround(vres[15]) ); - std::cout << "vector - for loop time: " << static_cast( end2 - begin2 ) / CLOCKS_PER_SEC - << std::endl; - std::cout << "ratio: " << static_cast( end - begin ) / static_cast( end2 - begin2 ) << std::endl; + std::cout << "ratio: " + << std::chrono::duration(end-begin).count() / std::chrono::duration(end2-begin2).count() + << std::endl; } #ifndef ONLY_SPIN void OpTest_Performance::PCheck()