From 6fc3a50fa9487313959fd721b25bd7b641b36306 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 16 Oct 2022 18:37:14 +0200 Subject: [PATCH] index_id --- src/include/ranges/index_base.h | 8 +-- src/include/ranges/xpr/for.cc.h | 88 +++++++++++----------------- src/include/ranges/xpr/for.h | 58 +++++++----------- src/include/ranges/xpr/index_id.cc.h | 65 ++++++++++++++++++++ src/include/ranges/xpr/index_id.h | 32 ++++++++++ src/include/ranges/xpr/xpr_base.cc.h | 13 ++-- src/include/ranges/xpr/xpr_base.h | 17 +++--- 7 files changed, 166 insertions(+), 115 deletions(-) create mode 100644 src/include/ranges/xpr/index_id.cc.h create mode 100644 src/include/ranges/xpr/index_id.h diff --git a/src/include/ranges/index_base.h b/src/include/ranges/index_base.h index 23be09c..335ee85 100644 --- a/src/include/ranges/index_base.h +++ b/src/include/ranges/index_base.h @@ -36,19 +36,19 @@ namespace CNORXZ bool operator>(const IndexInterface& in) const; bool operator<=(const IndexInterface& in) const; bool operator>=(const IndexInterface& in) const; - auto operator*() const { return THIS().operator*(); } - auto operator->() const { return THIS().operator->(); } + decltype(auto) operator*() const { return THIS().operator*(); } + decltype(auto) operator->() const { return THIS().operator->(); } Int pp(PtrId idxPtrNum) { return THIS().pp(idxPtrNum); } Int mm(PtrId idxPtrNum) { return THIS().mm(idxPtrNum); } SizeT dim() const { return THIS().dim(); } - auto range() const { return THIS().range(); } + decltype(auto) range() const { return THIS().range(); } SizeT getStepSize(PtrId iptr) const { return THIS().getStepSize(iptr); } //SizeT getStepSize(SizeT n) const { return THIS().getStepSize(n); } String stringMeta() const { return THIS().stringMeta(); } - auto meta() const { return THIS().meta(); } + decltype(auto) meta() const { return THIS().meta(); } I& at(const MetaType& meta) { return THIS().at(meta); } /* template diff --git a/src/include/ranges/xpr/for.cc.h b/src/include/ranges/xpr/for.cc.h index c924d41..b91817a 100644 --- a/src/include/ranges/xpr/for.cc.h +++ b/src/include/ranges/xpr/for.cc.h @@ -12,18 +12,18 @@ namespace CNORXZ * For * ***********/ - template - constexpr For::For(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr) : + template + constexpr For::For(SizeT size, const IndexId& id, SizeT step, const Xpr& xpr) : mSize(size), - mIptrId(iptrId), + mId(id), mStep(step), mXpr(xpr), - mExt(mXpr.rootSteps(mIptrId)) + mExt(mXpr.rootSteps(mId)) {} - template + template template - inline SizeT For::operator()(const PosT1& mlast, const PosT2& last) const + inline SizeT For::operator()(const PosT1& mlast, const PosT2& last) const { for(SizeT i = 0; i != mSize; ++i){ const auto mpos = mlast + mStep * UPos(i); @@ -33,8 +33,8 @@ namespace CNORXZ return 0; } - template - inline SizeT For::operator()() const + template + inline SizeT For::operator()() const { for(SizeT i = 0; i != mSize; ++i){ const SizeT mpos = mStep * UPos(i); @@ -44,17 +44,11 @@ namespace CNORXZ return 0; } - template - inline auto For::rootSteps(PtrId ptrId) const + template + template + inline decltype(auto) For::rootSteps(const IndexId& id) const { - return mXpr.rootSteps(ptrId); - } - - template - template - constexpr auto For::staticRootSteps(PtrId ptrId) const - { - return mXpr.template staticRootSteps(ptrId); + return mXpr.rootSteps(id); } @@ -63,10 +57,10 @@ namespace CNORXZ *************/ template - constexpr SLFor::SLFor(PtrId iptrId, const Xpr& xpr) : - mIptrId(iptrId), + constexpr SLFor::SLFor(const IndexId& id, const Xpr& xpr) : + mId(id), mXpr(xpr), - mExt(mXpr.template staticRootSteps(mIptrId)) + mExt(mXpr.RootSteps(mId)) {} template @@ -83,16 +77,10 @@ namespace CNORXZ } template - constexpr auto SLFor::rootSteps(PtrId ptrId) const + template + constexpr decltype(auto) SLFor::rootSteps(const IndexId& id) const { - return mXpr.rootSteps(ptrId); - } - - template - template - constexpr auto SLFor::staticRootSteps(PtrId ptrId) const - { - return mXpr.template staticRootSteps(ptrId); + return mXpr.rootSteps(id); } template @@ -133,18 +121,18 @@ namespace CNORXZ * TFor * ************/ - template - constexpr TFor::TFor(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr) : + template + constexpr TFor::TFor(SizeT size, const IndexId& id, SizeT step, const Xpr& xpr) : mSize(size), - mIptrId(iptrId), + mId(id), mStep(step), mXpr(xpr), - mExt(mXpr.rootSteps(mIptrId)) + mExt(mXpr.rootSteps(mId)) {} - template + template template - inline SizeT TFor::operator()(const PosT1& mlast, const PosT2& last) const + inline SizeT TFor::operator()(const PosT1& mlast, const PosT2& last) const { int i = 0; #pragma omp parallel shared(mXpr) private(i) @@ -152,17 +140,16 @@ namespace CNORXZ auto xpr = mXpr; #pragma omp for for(i = 0; i < mSize; i++){ - const UPos I(i); - const auto mpos = mlast + mStep * I; - const auto pos = last + mExt * I; + const auto mpos = mlast + mStep * UPos(i); + const auto pos = last + mExt * UPos(i); mXpr(mpos, pos); } } return 0; } - template - inline SizeT TFor::operator()() const + template + inline SizeT TFor::operator()() const { int i = 0; #pragma omp parallel shared(mXpr) private(i) @@ -170,26 +157,19 @@ namespace CNORXZ auto xpr = mXpr; #pragma omp for for(i = 0; i < static_cast(mSize); i++){ - const UPos I(i); - const auto mpos = mStep * I; - const auto pos = mExt * I; + const auto mpos = mStep * UPos(i); + const auto pos = mExt * UPos(i); mXpr(mpos, pos); } } return 0; } - template - inline auto TFor::rootSteps(PtrId ptrId) const + template + template + inline decltype(auto) TFor::rootSteps(const IndexId& id) const { - return mXpr.rootSteps(ptrId); - } - - template - template - inline auto TFor::staticRootSteps(PtrId ptrId) const - { - return mXpr.template staticRootSteps(ptrId); + return mXpr.rootSteps(id); } diff --git a/src/include/ranges/xpr/for.h b/src/include/ranges/xpr/for.h index 4023108..ec7033a 100644 --- a/src/include/ranges/xpr/for.h +++ b/src/include/ranges/xpr/for.h @@ -7,43 +7,29 @@ namespace CNORXZ { - /* - template - class ForInterface : public XprInterface> - { - public: - DEFAULT_MEMBERS(ForInterface); - inline auto rootSteps(PtrId ptrId) const; - - template - constexpr auto staticRootSteps(PtrId ptrId) const; - }; - */ - template - class For : public XprInterface> + template + class For : public XprInterface> { public: DEFAULT_MEMBERS(For); - constexpr For(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr); + constexpr For(SizeT size, const IndexId& id, SizeT step, const Xpr& xpr); template inline SizeT operator()(const PosT1& mlast, const PosT2& last) const; inline SizeT operator()() const; - inline auto rootSteps(PtrId ptrId) const; + template + inline decltype(auto) rootSteps(const IndexId& id) const; - template - constexpr auto staticRootSteps(PtrId ptrId) const; - private: SizeT mSize = 0; - PtrId mIptrId = 0; + IndexId mId; Xpr mXpr; UPos mStep; - typedef decltype(mXpr.rootSteps(0)) PosT; + typedef decltype(mXpr.rootSteps(mId)) PosT; PosT mExt; }; @@ -55,17 +41,15 @@ namespace CNORXZ public: DEFAULT_MEMBERS(SLFor); - constexpr SLFor(PtrId iptrId, const Xpr& xpr); + constexpr SLFor(const IndexId& id, const Xpr& xpr); template constexpr SizeT operator()(const PosT1& mlast, const PosT2& last) const; constexpr SizeT operator()() const; - constexpr auto rootSteps(PtrId ptrId) const; - - template - constexpr auto staticRootSteps(PtrId ptrId) const; + template + constexpr decltype(auto) rootSteps(const IndexId& id) const; private: @@ -74,40 +58,38 @@ namespace CNORXZ template constexpr SizeT exec() const; - - PtrId mIptrId = 0; + + IndexId mId; Xpr mXpr; - typedef decltype(mXpr.template staticRootSteps(0)) PosT; + typedef decltype(mXpr.RootSteps(mId)) PosT; PosT mExt; }; // multi-threading - template - class TFor : public XprInterface> + template + class TFor : public XprInterface> { public: DEFAULT_MEMBERS(TFor); - constexpr TFor(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr); + constexpr TFor(SizeT size, const IndexId& id, SizeT step, const Xpr& xpr); template inline SizeT operator()(const PosT1& mlast, const PosT2& last) const; inline SizeT operator()() const; - inline auto rootSteps(PtrId ptrId) const; - - template - inline auto staticRootSteps(PtrId ptrId) const; + template + inline decltype(auto) rootSteps(const IndexId& id) const; private: SizeT mSize = 0; - PtrId mIptrId = 0; + IndexId mId = 0; Xpr mXpr; UPos mStep; - typedef decltype(mXpr.rootSteps(0)) PosT; + typedef decltype(mXpr.rootSteps(mId)) PosT; PosT mExt; }; diff --git a/src/include/ranges/xpr/index_id.cc.h b/src/include/ranges/xpr/index_id.cc.h new file mode 100644 index 0000000..7f9a205 --- /dev/null +++ b/src/include/ranges/xpr/index_id.cc.h @@ -0,0 +1,65 @@ + +#ifndef __cxz_index_id_cc_h__ +#define __cxz_index_id_cc_h__ + +#include "index_id.h" + +namespace CNORXZ +{ + + template + explicit constexpr IndexId::IndexId(PtrId id) : + mId(id) + {} + + template + constexpr const PtrId& IndexId::id() const + { + return mId; + } + + template + template + constexpr decltype(auto) IndexId::operator==(const IndexId& a) const + { + if constexpr(I != 0 and J != 0){ + if constexpr(I == J){ + CXZ_ASSERT(mId == a.id(), + "got different index ids for equal static index label"); + return true; + } + else { + CXZ_ASSERT(mId == a.id(), + "got equal index ids for different static index label"); + return false; + } + } + else { + return mId == a.id(); + } + } + + template + template + constexpr decltype(auto) IndexId::operator!=(const IndexId& a) const + { + if constexpr(I != 0 and J != 0){ + if constexpr(I == J){ + CXZ_ASSERT(mId == a.id(), + "got different index ids for equal static index label"); + return false; + } + else { + CXZ_ASSERT(mId == a.id(), + "got equal index ids for different static index label"); + return true; + } + } + else { + return mId != a.id(); + } + } + +} + +#endif diff --git a/src/include/ranges/xpr/index_id.h b/src/include/ranges/xpr/index_id.h new file mode 100644 index 0000000..ed61d68 --- /dev/null +++ b/src/include/ranges/xpr/index_id.h @@ -0,0 +1,32 @@ + +#ifndef __cxz_index_id_h__ +#define __cxz_index_id_h__ + +#include "base/base.h" + +namespace CNORXZ +{ + + // if I == 0: no static comparison; fall back to usual runtime comparison of mId + template + class IndexId + { + private: + PtrId mId = 0; + + public: + DEFAULT_MEMBERS(IndexId); + + explicit constexpr IndexId(PtrId id); + + constexpr const PtrId& id() const; + + template + constexpr decltype(auto) operator==(const IndexId& a) const; + + template + constexpr decltype(auto) operator!=(const IndexId& a) const; + }; +} + +#endif diff --git a/src/include/ranges/xpr/xpr_base.cc.h b/src/include/ranges/xpr/xpr_base.cc.h index ccb6dac..8269e70 100644 --- a/src/include/ranges/xpr/xpr_base.cc.h +++ b/src/include/ranges/xpr/xpr_base.cc.h @@ -35,7 +35,7 @@ namespace CNORXZ } template - DPos VXpr::vrootSteps(PtrId ptrId) const + DPos VXpr::vrootSteps(const IndexId<0>& id) const { return DPos(this->rootSteps(ptrId)); } @@ -61,15 +61,10 @@ namespace CNORXZ return mC->vexec(); } - inline DPos DXpr::rootSteps(PtrId ptrId) const + template + inline DPos DXpr::rootSteps(const IndexId& id) const { - return mC->rootSteps(ptrId); - } - - template - inline DPos DXpr::staticRootSteps(PtrId ptrId) const - { - return this->rootSteps(ptrId); + return mC->rootSteps(IndexId<0>(id.id())); } } diff --git a/src/include/ranges/xpr/xpr_base.h b/src/include/ranges/xpr/xpr_base.h index 52d53a9..65be564 100644 --- a/src/include/ranges/xpr/xpr_base.h +++ b/src/include/ranges/xpr/xpr_base.h @@ -4,6 +4,7 @@ #include "base/base.h" #include "pos_type.h" +#include "index_id.h" namespace CNORXZ { @@ -22,10 +23,8 @@ namespace CNORXZ inline SizeT operator()() const { return THIS()(); } - inline auto rootSteps(PtrId ptrId) const { return THIS().rootSteps(ptrId); } - - template - constexpr auto staticRootSteps(PtrId ptrId) const { return THIS().template staticRootSteps(ptrId); } + template + inline decltype(auto) rootSteps(const IndexId& id) const { return THIS().rootSteps(id); } }; class VXprBase @@ -38,7 +37,7 @@ namespace CNORXZ virtual SizeT vexec(const UPos& mlast, const DPos& last) const = 0; virtual SizeT vexec() const = 0; - virtual DPos vrootSteps(PtrId ptrId) const = 0; + virtual DPos vrootSteps(const IndexId<0>& id) const = 0; }; template @@ -53,7 +52,7 @@ namespace CNORXZ virtual SizeT vexec(const UPos& mlast, const DPos& last) const override final; virtual SizeT vexec() const override final; - virtual DPos vrootSteps(PtrId ptrId) const override final; + virtual DPos vrootSteps(const IndexId<0>& id) const override final; }; class DXpr : public ObjHandle, @@ -69,10 +68,8 @@ namespace CNORXZ inline SizeT operator()(const UPos& mlast, const PosT& last) const; inline SizeT operator()() const; - inline DPos rootSteps(PtrId ptrId) const; - - template - inline DPos staticRootSteps(PtrId ptrId) const; // fallback to rootSteps + template + inline DPos rootSteps(const IndexId& id) const; }; }