for.h
This commit is contained in:
parent
e2baceada7
commit
3eea7f2314
6 changed files with 339 additions and 19 deletions
198
src/include/ranges/xpr/for.cc.h
Normal file
198
src/include/ranges/xpr/for.cc.h
Normal file
|
@ -0,0 +1,198 @@
|
|||
|
||||
#ifndef __cxz_for_cc_h__
|
||||
#define __cxz_for_cc_h__
|
||||
|
||||
#include <omp.h>
|
||||
|
||||
#include "for.h"
|
||||
|
||||
namespace CNORXZ
|
||||
{
|
||||
/***********
|
||||
* For *
|
||||
***********/
|
||||
|
||||
template <class Xpr>
|
||||
constexpr For<Xpr>::For(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr) :
|
||||
mSize(size),
|
||||
mIptrId(iptrId),
|
||||
mStep(step),
|
||||
mXpr(xpr),
|
||||
mExt(mXpr.rootSteps(mIptrId))
|
||||
{}
|
||||
|
||||
template <class Xpr>
|
||||
template <class PosT1, class PosT2>
|
||||
inline SizeT For<Xpr>::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 <class Xpr>
|
||||
inline SizeT For<Xpr>::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 <class Xpr>
|
||||
inline auto For<Xpr>::rootSteps(PtrId ptrId) const
|
||||
{
|
||||
return mXpr.rootSteps(ptrId);
|
||||
}
|
||||
|
||||
template <class Xpr>
|
||||
template <SizeT L>
|
||||
constexpr auto For<Xpr>::staticRootSteps(PtrId ptrId) const
|
||||
{
|
||||
return mXpr.template staticRootSteps<L>(ptrId);
|
||||
}
|
||||
|
||||
|
||||
/*************
|
||||
* SLFor *
|
||||
*************/
|
||||
|
||||
template <SizeT N, SizeT L, SizeT S, class Xpr>
|
||||
constexpr SLFor<N,L,S,Xpr>::SLFor(PtrId iptrId, const Xpr& xpr) :
|
||||
mIptrId(iptrId),
|
||||
mXpr(xpr),
|
||||
mExt(mXpr.template staticRootSteps<L>(mIptrId))
|
||||
{}
|
||||
|
||||
template <SizeT N, SizeT L, SizeT S, class Xpr>
|
||||
template <class PosT1, class PosT2>
|
||||
constexpr SizeT SLFor<N,L,S,Xpr>::operator()(const PosT1& mlast, const PosT2& last) const
|
||||
{
|
||||
return exec<0>(mlast, last);
|
||||
}
|
||||
|
||||
template <SizeT N, SizeT L, SizeT S, class Xpr>
|
||||
constexpr SizeT SLFor<N,L,S,Xpr>::operator()() const
|
||||
{
|
||||
return exec<0>();
|
||||
}
|
||||
|
||||
template <SizeT N, SizeT L, SizeT S, class Xpr>
|
||||
constexpr auto SLFor<N,L,S,Xpr>::rootSteps(PtrId ptrId) const
|
||||
{
|
||||
return mXpr.rootSteps(ptrId);
|
||||
}
|
||||
|
||||
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 I, class PosT1, class PosT2>
|
||||
constexpr SizeT SLFor<N,L,S,Xpr>::exec(const PosT1& mlast, const PosT2& last) const
|
||||
{
|
||||
constexpr SPos<I> i;
|
||||
constexpr SPos<S> step;
|
||||
const auto mpos = mlast + step * i;
|
||||
const auto pos = last + mExt * i;
|
||||
mXpr(mpos, pos);
|
||||
if constexpr(I < N-1){
|
||||
return exec<I+1>(mlast, last);
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template <SizeT N, SizeT L, SizeT S, class Xpr>
|
||||
template <SizeT I>
|
||||
constexpr SizeT SLFor<N,L,S,Xpr>::exec() const
|
||||
{
|
||||
constexpr SPos<I> i;
|
||||
constexpr SPos<S> step;
|
||||
const auto mpos = step * i;
|
||||
const auto pos = mExt * i;
|
||||
mXpr(mpos, pos);
|
||||
if constexpr(I < N-1){
|
||||
return exec<I+1>();
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/************
|
||||
* TFor *
|
||||
************/
|
||||
|
||||
template <class Xpr>
|
||||
constexpr TFor<Xpr>::TFor(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr) :
|
||||
mSize(size),
|
||||
mIptrId(iptrId),
|
||||
mStep(step),
|
||||
mXpr(xpr),
|
||||
mExt(mXpr.rootSteps(mIptrId))
|
||||
{}
|
||||
|
||||
template <class Xpr>
|
||||
template <class PosT1, class PosT2>
|
||||
inline SizeT TFor<Xpr>::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 <class Xpr>
|
||||
inline SizeT TFor<Xpr>::operator()() const
|
||||
{
|
||||
int i = 0;
|
||||
#pragma omp parallel shared(mXpr) private(i)
|
||||
{
|
||||
auto xpr = mXpr;
|
||||
#pragma omp for
|
||||
for(i = 0; i < static_cast<int>(mSize); i++){
|
||||
const UPos I(i);
|
||||
const auto mpos = mStep * I;
|
||||
const auto pos = mExt * I;
|
||||
mXpr(mpos, pos);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <class Xpr>
|
||||
inline auto TFor<Xpr>::rootSteps(PtrId ptrId) const
|
||||
{
|
||||
return mXpr.rootSteps(ptrId);
|
||||
}
|
||||
|
||||
template <class Xpr>
|
||||
template <SizeT L2>
|
||||
inline auto TFor<Xpr>::staticRootSteps(PtrId ptrId) const
|
||||
{
|
||||
return mXpr.template staticRootSteps<L2>(ptrId);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
119
src/include/ranges/xpr/for.h
Normal file
119
src/include/ranges/xpr/for.h
Normal file
|
@ -0,0 +1,119 @@
|
|||
|
||||
#ifndef __cxz_for_h__
|
||||
#define __cxz_for_h__
|
||||
|
||||
#include "base/base.h"
|
||||
#include "xpr_base.h"
|
||||
|
||||
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>
|
||||
constexpr auto staticRootSteps(PtrId ptrId) const;
|
||||
};
|
||||
*/
|
||||
template <class Xpr>
|
||||
class For : public XprInterface<For<Xpr>>
|
||||
{
|
||||
public:
|
||||
DEFAULT_MEMBERS(For);
|
||||
|
||||
constexpr For(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr);
|
||||
|
||||
template <class PosT1, class PosT2>
|
||||
inline SizeT operator()(const PosT1& mlast, const PosT2& last) const;
|
||||
|
||||
inline SizeT operator()() const;
|
||||
|
||||
inline auto rootSteps(PtrId ptrId) const;
|
||||
|
||||
template <SizeT L>
|
||||
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 <SizeT N, SizeT L, SizeT S, class Xpr>
|
||||
class SLFor : public XprInterface<SLFor<N,L,S,Xpr>>
|
||||
{
|
||||
public:
|
||||
DEFAULT_MEMBERS(SLFor);
|
||||
|
||||
constexpr SLFor(PtrId iptrId, const Xpr& xpr);
|
||||
|
||||
template <class PosT1, class PosT2>
|
||||
constexpr SizeT operator()(const PosT1& mlast, const PosT2& last) const;
|
||||
|
||||
constexpr SizeT operator()() const;
|
||||
|
||||
constexpr auto rootSteps(PtrId ptrId) const;
|
||||
|
||||
template <SizeT L2>
|
||||
constexpr auto staticRootSteps(PtrId ptrId) const;
|
||||
|
||||
private:
|
||||
|
||||
template <SizeT I, class PosT1, class PosT2>
|
||||
constexpr SizeT exec(const PosT1& mlast, const PosT2& last) const;
|
||||
|
||||
template <SizeT I>
|
||||
constexpr SizeT exec() const;
|
||||
|
||||
PtrId mIptrId = 0;
|
||||
Xpr mXpr;
|
||||
typedef decltype(mXpr.template staticRootSteps<L>(0)) PosT;
|
||||
PosT mExt;
|
||||
|
||||
};
|
||||
|
||||
|
||||
// multi-threading
|
||||
template <class Xpr>
|
||||
class TFor : public XprInterface<TFor<Xpr>>
|
||||
{
|
||||
public:
|
||||
DEFAULT_MEMBERS(TFor);
|
||||
|
||||
constexpr TFor(SizeT size, PtrId iptrId, SizeT step, const Xpr& xpr);
|
||||
|
||||
template <class PosT1, class PosT2>
|
||||
inline SizeT operator()(const PosT1& mlast, const PosT2& last) const;
|
||||
|
||||
inline SizeT operator()() const;
|
||||
|
||||
inline auto rootSteps(PtrId ptrId) const;
|
||||
|
||||
template <SizeT L2>
|
||||
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
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
#include "vpos_type.cc.h"
|
||||
#include "pos_type.cc.h"
|
||||
#include "for.cc.h"
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
#include "vpos_type.h"
|
||||
#include "pos_type.h"
|
||||
#include "for.h"
|
||||
|
||||
#include "xpr.cc.h"
|
||||
|
|
|
@ -23,15 +23,15 @@ namespace CNORXZ
|
|||
}
|
||||
|
||||
template <class Xpr>
|
||||
SizeT VXpr<Xpr>::vexec(SizeT mlast, const DPos& last) const
|
||||
SizeT VXpr<Xpr>::vexec(const UPos& mlast, const DPos& last) const
|
||||
{
|
||||
return (*this)(mlast, last);
|
||||
}
|
||||
|
||||
template <class Xpr>
|
||||
SizeT VXpr<Xpr>::vexec(SizeT mlast) const
|
||||
SizeT VXpr<Xpr>::vexec() const
|
||||
{
|
||||
return (*this)(mlast);
|
||||
return (*this)();
|
||||
}
|
||||
|
||||
template <class Xpr>
|
||||
|
@ -50,15 +50,15 @@ namespace CNORXZ
|
|||
{}
|
||||
|
||||
template <class PosT>
|
||||
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
|
||||
|
|
|
@ -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<Xpr&>(*this); }
|
||||
const Xpr& THIS() const { return static_cast<const Xpr&>(*this); }
|
||||
inline Xpr& THIS() { return static_cast<Xpr&>(*this); }
|
||||
inline const Xpr& THIS() const { return static_cast<const Xpr&>(*this); }
|
||||
|
||||
template <class PosT>
|
||||
SizeT operator()(SizeT mlast, const PosT& last) const { return THIS()(mlast,last); }
|
||||
template <class PosT1, class PosT2>
|
||||
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 <SizeT N>
|
||||
constexpr auto staticRootSteps(PtrId ptrId) const { return THIS().staticRootSteps<N>(ptrId); }
|
||||
constexpr auto staticRootSteps(PtrId ptrId) const { return THIS().template staticRootSteps<N>(ptrId); }
|
||||
};
|
||||
|
||||
class VXprBase
|
||||
|
@ -34,8 +35,8 @@ namespace CNORXZ
|
|||
|
||||
virtual Uptr<VXprBase> 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<VXprBase> 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 <class PosT>
|
||||
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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue