Grid 0.7.0
Lattice_view.h
Go to the documentation of this file.
1#pragma once
4// Base class which can be used by traits to pick up behaviour
6class LatticeBase {};
7
9// Conformable checks; same instance of Grid required
12{
13 assert(lhs == rhs);
14}
15
17// Minimal base class containing only data valid to access from accelerator
18// _odata will be a managed pointer in CUDA
20// Force access to lattice through a view object.
21// prevents writing of code that will not offload to GPU, but perhaps annoyingly
22// strict since host could could in principle direct access through the lattice object
23// Need to decide programming model.
24#define LATTICE_VIEW_STRICT
25template<class vobj> class LatticeAccelerator : public LatticeBase
26{
27protected:
28 //public:
31 vobj *_odata; // A managed pointer
32 uint64_t _odata_size;
34public:
36 accelerator_inline uint64_t oSites(void) const { return _odata_size; };
37 accelerator_inline int Checkerboard(void) const { return checkerboard; };
38 accelerator_inline int &Checkerboard(void) { return this->checkerboard; }; // can assign checkerboard on a container, not a view
39 accelerator_inline ViewAdvise Advise(void) const { return advise; };
40 accelerator_inline ViewAdvise &Advise(void) { return this->advise; }; // can assign advise on a container, not a view
42 {
43 if (grid) conformable(grid, _grid);
44 else grid = _grid;
45 };
46 // Host only
47 GridBase * getGrid(void) const { return _grid; };
48 vobj* getHostPointer(void) const { return _odata; };
49};
50
52// A View class which provides accessor to the data.
53// This will be safe to call from accelerator_for and is trivially copy constructible
54// The copy constructor for this will need to be used by device lambda functions
56template<class vobj>
57class LatticeView : public LatticeAccelerator<vobj>
58{
59public:
60 // Rvalue
62 void * cpu_ptr;
63#ifdef GRID_SIMT
64 accelerator_inline const typename vobj::scalar_object operator()(size_t i) const {
65 return coalescedRead(this->_odata[i]);
66 }
67#else
68 accelerator_inline const vobj & operator()(size_t i) const { return this->_odata[i]; }
69#endif
70
71#if 1
72 // accelerator_inline const vobj & operator[](size_t i) const { return this->_odata[i]; };
73 accelerator_inline vobj & operator[](size_t i) const { return this->_odata[i]; };
74#else
75 accelerator_inline const vobj & operator[](size_t i) const { return this->_odata[i]; };
76 accelerator_inline vobj & operator[](size_t i) { return this->_odata[i]; };
77#endif
78
79 accelerator_inline uint64_t begin(void) const { return 0;};
80 accelerator_inline uint64_t end(void) const { return this->_odata_size; };
81 accelerator_inline uint64_t size(void) const { return this->_odata_size; };
82
83 LatticeView(const LatticeAccelerator<vobj> &refer_to_me) : LatticeAccelerator<vobj> (refer_to_me){}
84 LatticeView(const LatticeView<vobj> &refer_to_me) = default; // Trivially copyable
85 LatticeView(const LatticeAccelerator<vobj> &refer_to_me,ViewMode mode) : LatticeAccelerator<vobj> (refer_to_me)
86 {
87 this->ViewOpen(mode);
88 }
89
90 // Host functions
92 { // Translate the pointer, could save a copy. Could use a "Handle" and not save _odata originally in base
93 // std::cout << "View Open"<<std::hex<<this->_odata<<std::dec <<std::endl;
94 this->cpu_ptr = (void *)this->_odata;
95 this->mode = mode;
96 this->_odata =(vobj *)
97 MemoryManager::ViewOpen(this->cpu_ptr,
98 this->_odata_size*sizeof(vobj),
99 mode,
100 this->advise);
101 }
102 void ViewClose(void)
103 { // Inform the manager
104 // std::cout << "View Close"<<std::hex<<this->cpu_ptr<<std::dec <<std::endl;
105 MemoryManager::ViewClose(this->cpu_ptr,this->mode);
106 }
107
108};
109// Little autoscope assister
110template<class View>
112{
113 View v; // Take a copy of view and call view close when I go out of scope automatically
114 public:
115 ViewCloser(View &_v) : v(_v) {};
116 ~ViewCloser() { v.ViewClose(); }
117};
118
119#define autoView(l_v,l,mode) \
120 auto l_v = l.View(mode); \
121 ViewCloser<decltype(l_v)> _autoView##l_v(l_v);
122
124// Lattice expression types used by ET to assemble the AST
125//
126// Need to be able to detect code paths according to the whether a lattice object or not
127// so introduce some trait type things
129
131
132template <typename T> using is_lattice = std::is_base_of<LatticeBase, T>;
133template <typename T> using is_lattice_expr = std::is_base_of<LatticeExpressionBase,T >;
134
135template<class T, bool isLattice> struct ViewMapBase { typedef T Type; };
136template<class T> struct ViewMapBase<T,true> { typedef LatticeView<typename T::vector_object> Type; };
138
139template <typename Op, typename _T1>
141{
142public:
143 typedef typename ViewMap<_T1>::Type T1;
144 Op op;
146 LatticeUnaryExpression(Op _op,const _T1 &_arg1) : op(_op), arg1(_arg1) {};
147};
148
149template <typename Op, typename _T1, typename _T2>
151{
152public:
153 typedef typename ViewMap<_T1>::Type T1;
154 typedef typename ViewMap<_T2>::Type T2;
155 Op op;
158 LatticeBinaryExpression(Op _op,const _T1 &_arg1,const _T2 &_arg2) : op(_op), arg1(_arg1), arg2(_arg2) {};
159};
160
161template <typename Op, typename _T1, typename _T2, typename _T3>
163{
164public:
165 typedef typename ViewMap<_T1>::Type T1;
166 typedef typename ViewMap<_T2>::Type T2;
167 typedef typename ViewMap<_T3>::Type T3;
168 Op op;
172 LatticeTrinaryExpression(Op _op,const _T1 &_arg1,const _T2 &_arg2,const _T3 &_arg3) : op(_op), arg1(_arg1), arg2(_arg2), arg3(_arg3) {};
173};
#define accelerator_inline
void accelerator_inline conformable(GridBase *lhs, GridBase *rhs)
std::is_base_of< LatticeExpressionBase, T > is_lattice_expr
ViewMapBase< T, std::is_base_of< LatticeBase, T >::value > ViewMap
std::is_base_of< LatticeBase, T > is_lattice
ViewMode
ViewAdvise
@ AdviseDefault
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
accelerator_inline vobj coalescedRead(const vobj &__restrict__ vec, int lane=0)
Definition Tensor_SIMT.h:61
vobj * getHostPointer(void) const
accelerator_inline LatticeAccelerator()
accelerator_inline int Checkerboard(void) const
accelerator_inline ViewAdvise Advise(void) const
accelerator_inline ViewAdvise & Advise(void)
accelerator_inline uint64_t oSites(void) const
accelerator_inline int & Checkerboard(void)
GridBase * getGrid(void) const
accelerator_inline void Conformable(GridBase *&grid) const
LatticeBinaryExpression(Op _op, const _T1 &_arg1, const _T2 &_arg2)
ViewMap< _T1 >::Type T1
ViewMap< _T2 >::Type T2
ViewMap< _T2 >::Type T2
LatticeTrinaryExpression(Op _op, const _T1 &_arg1, const _T2 &_arg2, const _T3 &_arg3)
ViewMap< _T3 >::Type T3
ViewMap< _T1 >::Type T1
LatticeUnaryExpression(Op _op, const _T1 &_arg1)
ViewMap< _T1 >::Type T1
accelerator_inline uint64_t begin(void) const
accelerator_inline uint64_t end(void) const
accelerator_inline vobj & operator[](size_t i) const
LatticeView(const LatticeAccelerator< vobj > &refer_to_me, ViewMode mode)
void ViewClose(void)
LatticeView(const LatticeAccelerator< vobj > &refer_to_me)
accelerator_inline const vobj & operator()(size_t i) const
LatticeView(const LatticeView< vobj > &refer_to_me)=default
void ViewOpen(ViewMode mode)
accelerator_inline uint64_t size(void) const
static void * ViewOpen(void *CpuPtr, size_t bytes, ViewMode mode, ViewAdvise hint)
static void ViewClose(void *CpuPtr, ViewMode mode)
ViewCloser(View &_v)
LatticeView< typename T::vector_object > Type