This commit is contained in:
Christian Zimmermann 2022-10-16 18:37:14 +02:00
parent 3eea7f2314
commit 6fc3a50fa9
7 changed files with 166 additions and 115 deletions

View file

@ -36,19 +36,19 @@ namespace CNORXZ
bool operator>(const IndexInterface& in) const; bool operator>(const IndexInterface& in) const;
bool operator<=(const IndexInterface& in) const; bool operator<=(const IndexInterface& in) const;
bool operator>=(const IndexInterface& in) const; bool operator>=(const IndexInterface& in) const;
auto operator*() const { return THIS().operator*(); } decltype(auto) operator*() const { return THIS().operator*(); }
auto operator->() const { return THIS().operator->(); } decltype(auto) operator->() const { return THIS().operator->(); }
Int pp(PtrId idxPtrNum) { return THIS().pp(idxPtrNum); } Int pp(PtrId idxPtrNum) { return THIS().pp(idxPtrNum); }
Int mm(PtrId idxPtrNum) { return THIS().mm(idxPtrNum); } Int mm(PtrId idxPtrNum) { return THIS().mm(idxPtrNum); }
SizeT dim() const { return THIS().dim(); } 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(PtrId iptr) const { return THIS().getStepSize(iptr); }
//SizeT getStepSize(SizeT n) const { return THIS().getStepSize(n); } //SizeT getStepSize(SizeT n) const { return THIS().getStepSize(n); }
String stringMeta() const { return THIS().stringMeta(); } 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); } I& at(const MetaType& meta) { return THIS().at(meta); }
/* /*
template <class Expr> template <class Expr>

View file

@ -12,18 +12,18 @@ namespace CNORXZ
* For * * For *
***********/ ***********/
template <class Xpr> template <SizeT L, class Xpr>
constexpr For<Xpr>::For(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr) : constexpr For<L,Xpr>::For(SizeT size, const IndexId<L>& id, SizeT step, const Xpr& xpr) :
mSize(size), mSize(size),
mIptrId(iptrId), mId(id),
mStep(step), mStep(step),
mXpr(xpr), mXpr(xpr),
mExt(mXpr.rootSteps(mIptrId)) mExt(mXpr.rootSteps(mId))
{} {}
template <class Xpr> template <SizeT L, class Xpr>
template <class PosT1, class PosT2> template <class PosT1, class PosT2>
inline SizeT For<Xpr>::operator()(const PosT1& mlast, const PosT2& last) const inline SizeT For<L,Xpr>::operator()(const PosT1& mlast, const PosT2& last) const
{ {
for(SizeT i = 0; i != mSize; ++i){ for(SizeT i = 0; i != mSize; ++i){
const auto mpos = mlast + mStep * UPos(i); const auto mpos = mlast + mStep * UPos(i);
@ -33,8 +33,8 @@ namespace CNORXZ
return 0; return 0;
} }
template <class Xpr> template <SizeT L, class Xpr>
inline SizeT For<Xpr>::operator()() const inline SizeT For<L,Xpr>::operator()() const
{ {
for(SizeT i = 0; i != mSize; ++i){ for(SizeT i = 0; i != mSize; ++i){
const SizeT mpos = mStep * UPos(i); const SizeT mpos = mStep * UPos(i);
@ -44,17 +44,11 @@ namespace CNORXZ
return 0; return 0;
} }
template <class Xpr> template <SizeT L, class Xpr>
inline auto For<Xpr>::rootSteps(PtrId ptrId) const template <SizeT I>
inline decltype(auto) For<L,Xpr>::rootSteps(const IndexId<I>& id) const
{ {
return mXpr.rootSteps(ptrId); return mXpr.rootSteps(id);
}
template <class Xpr>
template <SizeT L>
constexpr auto For<Xpr>::staticRootSteps(PtrId ptrId) const
{
return mXpr.template staticRootSteps<L>(ptrId);
} }
@ -63,10 +57,10 @@ namespace CNORXZ
*************/ *************/
template <SizeT N, SizeT L, SizeT S, class Xpr> template <SizeT N, SizeT L, SizeT S, class Xpr>
constexpr SLFor<N,L,S,Xpr>::SLFor(PtrId iptrId, const Xpr& xpr) : constexpr SLFor<N,L,S,Xpr>::SLFor(const IndexId<L>& id, const Xpr& xpr) :
mIptrId(iptrId), mId(id),
mXpr(xpr), mXpr(xpr),
mExt(mXpr.template staticRootSteps<L>(mIptrId)) mExt(mXpr.RootSteps(mId))
{} {}
template <SizeT N, SizeT L, SizeT S, class Xpr> template <SizeT N, SizeT L, SizeT S, class Xpr>
@ -83,16 +77,10 @@ namespace CNORXZ
} }
template <SizeT N, SizeT L, SizeT S, class Xpr> template <SizeT N, SizeT L, SizeT S, class Xpr>
constexpr auto SLFor<N,L,S,Xpr>::rootSteps(PtrId ptrId) const template <SizeT I>
constexpr decltype(auto) SLFor<N,L,S,Xpr>::rootSteps(const IndexId<I>& id) const
{ {
return mXpr.rootSteps(ptrId); return mXpr.rootSteps(id);
}
template <SizeT N, SizeT L, SizeT S, class Xpr>
template <SizeT L2>
constexpr auto SLFor<N,L,S,Xpr>::staticRootSteps(PtrId ptrId) const
{
return mXpr.template staticRootSteps<L2>(ptrId);
} }
template <SizeT N, SizeT L, SizeT S, class Xpr> template <SizeT N, SizeT L, SizeT S, class Xpr>
@ -133,18 +121,18 @@ namespace CNORXZ
* TFor * * TFor *
************/ ************/
template <class Xpr> template <SizeT L, class Xpr>
constexpr TFor<Xpr>::TFor(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr) : constexpr TFor<L,Xpr>::TFor(SizeT size, const IndexId<L>& id, SizeT step, const Xpr& xpr) :
mSize(size), mSize(size),
mIptrId(iptrId), mId(id),
mStep(step), mStep(step),
mXpr(xpr), mXpr(xpr),
mExt(mXpr.rootSteps(mIptrId)) mExt(mXpr.rootSteps(mId))
{} {}
template <class Xpr> template <SizeT L, class Xpr>
template <class PosT1, class PosT2> template <class PosT1, class PosT2>
inline SizeT TFor<Xpr>::operator()(const PosT1& mlast, const PosT2& last) const inline SizeT TFor<L,Xpr>::operator()(const PosT1& mlast, const PosT2& last) const
{ {
int i = 0; int i = 0;
#pragma omp parallel shared(mXpr) private(i) #pragma omp parallel shared(mXpr) private(i)
@ -152,17 +140,16 @@ namespace CNORXZ
auto xpr = mXpr; auto xpr = mXpr;
#pragma omp for #pragma omp for
for(i = 0; i < mSize; i++){ for(i = 0; i < mSize; i++){
const UPos I(i); const auto mpos = mlast + mStep * UPos(i);
const auto mpos = mlast + mStep * I; const auto pos = last + mExt * UPos(i);
const auto pos = last + mExt * I;
mXpr(mpos, pos); mXpr(mpos, pos);
} }
} }
return 0; return 0;
} }
template <class Xpr> template <SizeT L, class Xpr>
inline SizeT TFor<Xpr>::operator()() const inline SizeT TFor<L,Xpr>::operator()() const
{ {
int i = 0; int i = 0;
#pragma omp parallel shared(mXpr) private(i) #pragma omp parallel shared(mXpr) private(i)
@ -170,26 +157,19 @@ namespace CNORXZ
auto xpr = mXpr; auto xpr = mXpr;
#pragma omp for #pragma omp for
for(i = 0; i < static_cast<int>(mSize); i++){ for(i = 0; i < static_cast<int>(mSize); i++){
const UPos I(i); const auto mpos = mStep * UPos(i);
const auto mpos = mStep * I; const auto pos = mExt * UPos(i);
const auto pos = mExt * I;
mXpr(mpos, pos); mXpr(mpos, pos);
} }
} }
return 0; return 0;
} }
template <class Xpr> template <SizeT L, class Xpr>
inline auto TFor<Xpr>::rootSteps(PtrId ptrId) const template <SizeT I>
inline decltype(auto) TFor<L,Xpr>::rootSteps(const IndexId<I>& id) const
{ {
return mXpr.rootSteps(ptrId); return mXpr.rootSteps(id);
}
template <class Xpr>
template <SizeT L2>
inline auto TFor<Xpr>::staticRootSteps(PtrId ptrId) const
{
return mXpr.template staticRootSteps<L2>(ptrId);
} }

View file

@ -7,43 +7,29 @@
namespace CNORXZ namespace CNORXZ
{ {
/*
template <class ForT, class Xpr>
class ForInterface : public XprInterface<ForInterface<ForT,Xpr>>
{
public:
DEFAULT_MEMBERS(ForInterface);
inline auto rootSteps(PtrId ptrId) const; template <SizeT L, class Xpr>
class For : public XprInterface<For<L,Xpr>>
template <SizeT L>
constexpr auto staticRootSteps(PtrId ptrId) const;
};
*/
template <class Xpr>
class For : public XprInterface<For<Xpr>>
{ {
public: public:
DEFAULT_MEMBERS(For); DEFAULT_MEMBERS(For);
constexpr For(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr); constexpr For(SizeT size, const IndexId<L>& id, SizeT step, const Xpr& xpr);
template <class PosT1, class PosT2> template <class PosT1, class PosT2>
inline SizeT operator()(const PosT1& mlast, const PosT2& last) const; inline SizeT operator()(const PosT1& mlast, const PosT2& last) const;
inline SizeT operator()() const; inline SizeT operator()() const;
inline auto rootSteps(PtrId ptrId) const; template <SizeT I>
inline decltype(auto) rootSteps(const IndexId<I>& id) const;
template <SizeT L>
constexpr auto staticRootSteps(PtrId ptrId) const;
private: private:
SizeT mSize = 0; SizeT mSize = 0;
PtrId mIptrId = 0; IndexId<L> mId;
Xpr mXpr; Xpr mXpr;
UPos mStep; UPos mStep;
typedef decltype(mXpr.rootSteps(0)) PosT; typedef decltype(mXpr.rootSteps(mId)) PosT;
PosT mExt; PosT mExt;
}; };
@ -55,17 +41,15 @@ namespace CNORXZ
public: public:
DEFAULT_MEMBERS(SLFor); DEFAULT_MEMBERS(SLFor);
constexpr SLFor(PtrId iptrId, const Xpr& xpr); constexpr SLFor(const IndexId<L>& id, const Xpr& xpr);
template <class PosT1, class PosT2> template <class PosT1, class PosT2>
constexpr SizeT operator()(const PosT1& mlast, const PosT2& last) const; constexpr SizeT operator()(const PosT1& mlast, const PosT2& last) const;
constexpr SizeT operator()() const; constexpr SizeT operator()() const;
constexpr auto rootSteps(PtrId ptrId) const; template <SizeT I>
constexpr decltype(auto) rootSteps(const IndexId<I>& id) const;
template <SizeT L2>
constexpr auto staticRootSteps(PtrId ptrId) const;
private: private:
@ -75,39 +59,37 @@ namespace CNORXZ
template <SizeT I> template <SizeT I>
constexpr SizeT exec() const; constexpr SizeT exec() const;
PtrId mIptrId = 0; IndexId<L> mId;
Xpr mXpr; Xpr mXpr;
typedef decltype(mXpr.template staticRootSteps<L>(0)) PosT; typedef decltype(mXpr.RootSteps(mId)) PosT;
PosT mExt; PosT mExt;
}; };
// multi-threading // multi-threading
template <class Xpr> template <SizeT L, class Xpr>
class TFor : public XprInterface<TFor<Xpr>> class TFor : public XprInterface<TFor<L,Xpr>>
{ {
public: public:
DEFAULT_MEMBERS(TFor); DEFAULT_MEMBERS(TFor);
constexpr TFor(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr); constexpr TFor(SizeT size, const IndexId<L>& id, SizeT step, const Xpr& xpr);
template <class PosT1, class PosT2> template <class PosT1, class PosT2>
inline SizeT operator()(const PosT1& mlast, const PosT2& last) const; inline SizeT operator()(const PosT1& mlast, const PosT2& last) const;
inline SizeT operator()() const; inline SizeT operator()() const;
inline auto rootSteps(PtrId ptrId) const; template <SizeT I>
inline decltype(auto) rootSteps(const IndexId<I>& id) const;
template <SizeT L2>
inline auto staticRootSteps(PtrId ptrId) const;
private: private:
SizeT mSize = 0; SizeT mSize = 0;
PtrId mIptrId = 0; IndexId<L> mId = 0;
Xpr mXpr; Xpr mXpr;
UPos mStep; UPos mStep;
typedef decltype(mXpr.rootSteps(0)) PosT; typedef decltype(mXpr.rootSteps(mId)) PosT;
PosT mExt; PosT mExt;
}; };

View file

@ -0,0 +1,65 @@
#ifndef __cxz_index_id_cc_h__
#define __cxz_index_id_cc_h__
#include "index_id.h"
namespace CNORXZ
{
template <SizeT I>
explicit constexpr IndexId<I>::IndexId(PtrId id) :
mId(id)
{}
template <SizeT I>
constexpr const PtrId& IndexId<I>::id() const
{
return mId;
}
template <SizeT I>
template <SizeT J>
constexpr decltype(auto) IndexId<I>::operator==(const IndexId<J>& 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 <SizeT I>
template <SizeT J>
constexpr decltype(auto) IndexId<I>::operator!=(const IndexId<J>& 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

View file

@ -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 <SizeT I = 0>
class IndexId
{
private:
PtrId mId = 0;
public:
DEFAULT_MEMBERS(IndexId);
explicit constexpr IndexId(PtrId id);
constexpr const PtrId& id() const;
template <SizeT J>
constexpr decltype(auto) operator==(const IndexId<J>& a) const;
template <SizeT J>
constexpr decltype(auto) operator!=(const IndexId<J>& a) const;
};
}
#endif

View file

@ -35,7 +35,7 @@ namespace CNORXZ
} }
template <class Xpr> template <class Xpr>
DPos VXpr<Xpr>::vrootSteps(PtrId ptrId) const DPos VXpr<Xpr>::vrootSteps(const IndexId<0>& id) const
{ {
return DPos(this->rootSteps(ptrId)); return DPos(this->rootSteps(ptrId));
} }
@ -61,15 +61,10 @@ namespace CNORXZ
return mC->vexec(); return mC->vexec();
} }
inline DPos DXpr::rootSteps(PtrId ptrId) const template <SizeT I>
inline DPos DXpr::rootSteps(const IndexId<I>& id) const
{ {
return mC->rootSteps(ptrId); return mC->rootSteps(IndexId<0>(id.id()));
}
template <SizeT N>
inline DPos DXpr::staticRootSteps(PtrId ptrId) const
{
return this->rootSteps(ptrId);
} }
} }

View file

@ -4,6 +4,7 @@
#include "base/base.h" #include "base/base.h"
#include "pos_type.h" #include "pos_type.h"
#include "index_id.h"
namespace CNORXZ namespace CNORXZ
{ {
@ -22,10 +23,8 @@ namespace CNORXZ
inline SizeT operator()() const { return THIS()(); } inline SizeT operator()() const { return THIS()(); }
inline auto rootSteps(PtrId ptrId) const { return THIS().rootSteps(ptrId); } template <SizeT I>
inline decltype(auto) rootSteps(const IndexId<I>& id) const { return THIS().rootSteps(id); }
template <SizeT N>
constexpr auto staticRootSteps(PtrId ptrId) const { return THIS().template staticRootSteps<N>(ptrId); }
}; };
class VXprBase class VXprBase
@ -38,7 +37,7 @@ namespace CNORXZ
virtual SizeT vexec(const UPos& mlast, const DPos& last) const = 0; virtual SizeT vexec(const UPos& mlast, const DPos& last) const = 0;
virtual SizeT vexec() const = 0; virtual SizeT vexec() const = 0;
virtual DPos vrootSteps(PtrId ptrId) const = 0; virtual DPos vrootSteps(const IndexId<0>& id) const = 0;
}; };
template <class Xpr> template <class Xpr>
@ -53,7 +52,7 @@ namespace CNORXZ
virtual SizeT vexec(const UPos& mlast, const DPos& last) const override final; virtual SizeT vexec(const UPos& mlast, const DPos& last) const override final;
virtual SizeT vexec() 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<VXprBase>, class DXpr : public ObjHandle<VXprBase>,
@ -69,10 +68,8 @@ namespace CNORXZ
inline SizeT operator()(const UPos& mlast, const PosT& last) const; inline SizeT operator()(const UPos& mlast, const PosT& last) const;
inline SizeT operator()() const; inline SizeT operator()() const;
inline DPos rootSteps(PtrId ptrId) const; template <SizeT I>
inline DPos rootSteps(const IndexId<I>& id) const;
template <SizeT N>
inline DPos staticRootSteps(PtrId ptrId) const; // fallback to rootSteps
}; };
} }