diff --git a/src/include/ranges/xpr/for.cc.h b/src/include/ranges/xpr/for.cc.h new file mode 100644 index 0000000..c924d41 --- /dev/null +++ b/src/include/ranges/xpr/for.cc.h @@ -0,0 +1,198 @@ + +#ifndef __cxz_for_cc_h__ +#define __cxz_for_cc_h__ + +#include + +#include "for.h" + +namespace CNORXZ +{ + /*********** + * For * + ***********/ + + template + constexpr For::For(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr) : + mSize(size), + mIptrId(iptrId), + mStep(step), + mXpr(xpr), + mExt(mXpr.rootSteps(mIptrId)) + {} + + template + template + 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); + const auto pos = last + mExt * UPos(i); + mXpr(mpos, pos); + } + return 0; + } + + template + inline SizeT For::operator()() const + { + for(SizeT i = 0; i != mSize; ++i){ + const SizeT mpos = mStep * UPos(i); + const auto pos = mExt * UPos(i); + mXpr(mpos, pos); + } + return 0; + } + + template + inline auto For::rootSteps(PtrId ptrId) const + { + return mXpr.rootSteps(ptrId); + } + + template + template + constexpr auto For::staticRootSteps(PtrId ptrId) const + { + return mXpr.template staticRootSteps(ptrId); + } + + + /************* + * SLFor * + *************/ + + template + constexpr SLFor::SLFor(PtrId iptrId, const Xpr& xpr) : + mIptrId(iptrId), + mXpr(xpr), + mExt(mXpr.template staticRootSteps(mIptrId)) + {} + + template + template + constexpr SizeT SLFor::operator()(const PosT1& mlast, const PosT2& last) const + { + return exec<0>(mlast, last); + } + + template + constexpr SizeT SLFor::operator()() const + { + return exec<0>(); + } + + template + constexpr auto SLFor::rootSteps(PtrId ptrId) const + { + return mXpr.rootSteps(ptrId); + } + + template + template + constexpr auto SLFor::staticRootSteps(PtrId ptrId) const + { + return mXpr.template staticRootSteps(ptrId); + } + + template + template + constexpr SizeT SLFor::exec(const PosT1& mlast, const PosT2& last) const + { + constexpr SPos i; + constexpr SPos step; + const auto mpos = mlast + step * i; + const auto pos = last + mExt * i; + mXpr(mpos, pos); + if constexpr(I < N-1){ + return exec(mlast, last); + } + else { + return 0; + } + } + + template + template + constexpr SizeT SLFor::exec() const + { + constexpr SPos i; + constexpr SPos step; + const auto mpos = step * i; + const auto pos = mExt * i; + mXpr(mpos, pos); + if constexpr(I < N-1){ + return exec(); + } + else { + return 0; + } + } + + /************ + * TFor * + ************/ + + template + constexpr TFor::TFor(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr) : + mSize(size), + mIptrId(iptrId), + mStep(step), + mXpr(xpr), + mExt(mXpr.rootSteps(mIptrId)) + {} + + template + template + inline SizeT TFor::operator()(const PosT1& mlast, const PosT2& last) const + { + int i = 0; +#pragma omp parallel shared(mXpr) private(i) + { + 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; + mXpr(mpos, pos); + } + } + return 0; + } + + template + inline SizeT TFor::operator()() const + { + int i = 0; +#pragma omp parallel shared(mXpr) private(i) + { + 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; + mXpr(mpos, pos); + } + } + return 0; + } + + template + inline auto TFor::rootSteps(PtrId ptrId) const + { + return mXpr.rootSteps(ptrId); + } + + template + template + inline auto TFor::staticRootSteps(PtrId ptrId) const + { + return mXpr.template staticRootSteps(ptrId); + } + + +} + +#endif diff --git a/src/include/ranges/xpr/for.h b/src/include/ranges/xpr/for.h new file mode 100644 index 0000000..4023108 --- /dev/null +++ b/src/include/ranges/xpr/for.h @@ -0,0 +1,119 @@ + +#ifndef __cxz_for_h__ +#define __cxz_for_h__ + +#include "base/base.h" +#include "xpr_base.h" + +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> + { + public: + DEFAULT_MEMBERS(For); + + constexpr For(SizeT size, PtrId iptrId, 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 + constexpr auto staticRootSteps(PtrId ptrId) const; + + private: + SizeT mSize = 0; + PtrId mIptrId = 0; + Xpr mXpr; + UPos mStep; + typedef decltype(mXpr.rootSteps(0)) PosT; + PosT mExt; + + }; + + // unrolled loop: + template + class SLFor : public XprInterface> + { + public: + DEFAULT_MEMBERS(SLFor); + + constexpr SLFor(PtrId iptrId, 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; + + private: + + template + constexpr SizeT exec(const PosT1& mlast, const PosT2& last) const; + + template + constexpr SizeT exec() const; + + PtrId mIptrId = 0; + Xpr mXpr; + typedef decltype(mXpr.template staticRootSteps(0)) PosT; + PosT mExt; + + }; + + + // multi-threading + template + class TFor : public XprInterface> + { + public: + DEFAULT_MEMBERS(TFor); + + constexpr TFor(SizeT size, PtrId iptrId, 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; + + private: + SizeT mSize = 0; + PtrId mIptrId = 0; + Xpr mXpr; + UPos mStep; + typedef decltype(mXpr.rootSteps(0)) PosT; + PosT mExt; + + }; + + + +} + +#endif diff --git a/src/include/ranges/xpr/xpr.cc.h b/src/include/ranges/xpr/xpr.cc.h index 7682a98..09025b7 100644 --- a/src/include/ranges/xpr/xpr.cc.h +++ b/src/include/ranges/xpr/xpr.cc.h @@ -1,3 +1,4 @@ #include "vpos_type.cc.h" #include "pos_type.cc.h" +#include "for.cc.h" diff --git a/src/include/ranges/xpr/xpr.h b/src/include/ranges/xpr/xpr.h index 591b196..de8db28 100644 --- a/src/include/ranges/xpr/xpr.h +++ b/src/include/ranges/xpr/xpr.h @@ -1,5 +1,6 @@ #include "vpos_type.h" #include "pos_type.h" +#include "for.h" #include "xpr.cc.h" diff --git a/src/include/ranges/xpr/xpr_base.cc.h b/src/include/ranges/xpr/xpr_base.cc.h index bd3b938..ccb6dac 100644 --- a/src/include/ranges/xpr/xpr_base.cc.h +++ b/src/include/ranges/xpr/xpr_base.cc.h @@ -23,15 +23,15 @@ namespace CNORXZ } template - SizeT VXpr::vexec(SizeT mlast, const DPos& last) const + SizeT VXpr::vexec(const UPos& mlast, const DPos& last) const { return (*this)(mlast, last); } template - SizeT VXpr::vexec(SizeT mlast) const + SizeT VXpr::vexec() const { - return (*this)(mlast); + return (*this)(); } template @@ -50,15 +50,15 @@ namespace CNORXZ {} template - inline SizeT DXpr::operator()(SizeT mlast, const PosT& last) const + inline SizeT DXpr::operator()(const UPos& mlast, const PosT& last) const { DPosRef dlast(&last); return mC->vexec(mlast, dlast); } - inline SizeT DXpr::operator()(SizeT mlast) const + inline SizeT DXpr::operator()() const { - return mC->vexec(mlast); + return mC->vexec(); } inline DPos DXpr::rootSteps(PtrId ptrId) const diff --git a/src/include/ranges/xpr/xpr_base.h b/src/include/ranges/xpr/xpr_base.h index 6bdb8bf..52d53a9 100644 --- a/src/include/ranges/xpr/xpr_base.h +++ b/src/include/ranges/xpr/xpr_base.h @@ -3,6 +3,7 @@ #define __cxz_xpr_base_h__ #include "base/base.h" +#include "pos_type.h" namespace CNORXZ { @@ -13,18 +14,18 @@ namespace CNORXZ public: DEFAULT_MEMBERS(XprInterface); - Xpr& THIS() { return static_cast(*this); } - const Xpr& THIS() const { return static_cast(*this); } + inline Xpr& THIS() { return static_cast(*this); } + inline const Xpr& THIS() const { return static_cast(*this); } - template - SizeT operator()(SizeT mlast, const PosT& last) const { return THIS()(mlast,last); } + template + inline SizeT operator()(const PosT1& mlast, const PosT2& last) const { return THIS()(mlast,last); } - SizeT operator()(SizeT mlast = 0) const { return THIS()(mlast); } + inline SizeT operator()() const { return THIS()(); } - auto rootSteps(PtrId ptrId) const { return THIS().rootSteps(ptrId); } + inline auto rootSteps(PtrId ptrId) const { return THIS().rootSteps(ptrId); } template - constexpr auto staticRootSteps(PtrId ptrId) const { return THIS().staticRootSteps(ptrId); } + constexpr auto staticRootSteps(PtrId ptrId) const { return THIS().template staticRootSteps(ptrId); } }; class VXprBase @@ -34,8 +35,8 @@ namespace CNORXZ virtual Uptr copy() const = 0; - virtual SizeT vexec(SizeT mlast, const DPos& last) const = 0; - virtual SizeT vexec(SizeT mlast) const = 0; + virtual SizeT vexec(const UPos& mlast, const DPos& last) const = 0; + virtual SizeT vexec() const = 0; virtual DPos vrootSteps(PtrId ptrId) const = 0; }; @@ -49,8 +50,8 @@ namespace CNORXZ virtual Uptr copy() const override final; - virtual SizeT vexec(SizeT mlast, const DPos& last) const override final; - virtual SizeT vexec(SizeT mlast) const override final; + 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; }; @@ -65,8 +66,8 @@ namespace CNORXZ explicit DXpr(const Xpr& a); template - inline SizeT operator()(SizeT mlast, const PosT& last) const; - inline SizeT operator()(SizeT mlast) const; + inline SizeT operator()(const UPos& mlast, const PosT& last) const; + inline SizeT operator()() const; inline DPos rootSteps(PtrId ptrId) const;