Grid 0.7.0
Lattice_base.h
Go to the documentation of this file.
1/*************************************************************************************
2
3Grid physics library, www.github.com/paboyle/Grid
4
5Source file: ./lib/lattice/Lattice_base.h
6
7Copyright (C) 2015
8
9Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
10Author: Peter Boyle <paboyle@ph.ed.ac.uk>
11Author: paboyle <paboyle@ph.ed.ac.uk>
12Author: Christoph Lehner <christoph@lhnr.de>
13
14This program is free software; you can redistribute it and/or modify
15it under the terms of the GNU General Public License as published by
16the Free Software Foundation; either version 2 of the License, or
17(at your option) any later version.
18
19This program is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License along
25with this program; if not, write to the Free Software Foundation, Inc.,
2651 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27
28See the full license in the file "LICENSE" in the top level distribution
29directory
30*************************************************************************************/
31 /* END LEGAL */
32
33#pragma once
34
35#define STREAMING_STORES
36
38
39extern int GridCshiftPermuteMap[4][16];
40
42// The real lattice class, with normal copy and assignment semantics.
43// This contains extra (host resident) grid pointer data that may be accessed by host code
45template<class vobj>
46class Lattice : public LatticeAccelerator<vobj>
47{
48public:
49 GridBase *Grid(void) const { return this->_grid; }
51 // Member types
53 typedef typename vobj::scalar_type scalar_type;
54 typedef typename vobj::vector_type vector_type;
55 typedef typename vobj::scalar_object scalar_object;
56 typedef vobj vector_object;
57
58private:
59 void dealloc(void)
60 {
61 if( this->_odata_size ) {
63 alloc.deallocate(this->_odata,this->_odata_size);
64 this->_odata=nullptr;
65 this->_odata_size=0;
66 }
67 }
68 void resize(uint64_t size)
69 {
70 if ( this->_odata_size != size ) {
72
73 dealloc();
74
75 this->_odata_size = size;
76 if ( size )
77 this->_odata = alloc.allocate(this->_odata_size);
78 else
79 this->_odata = nullptr;
80 }
81 }
82public:
83
85 // Can use to make accelerator dirty without copy from host ; useful for temporaries "dont care" prev contents
87 void SetViewMode(ViewMode mode) {
88 LatticeView<vobj> accessor(*( (LatticeAccelerator<vobj> *) this),mode);
89 accessor.ViewClose();
90 }
91
92 // Helper function to print the state of this object in the AccCache
93 void PrintCacheState(void)
94 {
96 }
97
99 // Return a view object that may be dereferenced in site loops.
100 // The view is trivially copy constructible and may be copied to an accelerator device
101 // in device lambdas
103
105 {
106 LatticeView<vobj> accessor(*( (LatticeAccelerator<vobj> *) this),mode);
107 return accessor;
108 }
109
111 if ( this->_odata_size ) {
112 dealloc();
113 }
114 }
115
116 // Expression Template closure support
118 template <typename Op, typename T1> inline Lattice<vobj> & operator=(const LatticeUnaryExpression<Op,T1> &expr)
119 {
120 GRID_TRACE("ExpressionTemplateEval");
121 GridBase *egrid(nullptr);
122 GridFromExpression(egrid,expr);
123 assert(egrid!=nullptr);
124 conformable(this->_grid,egrid);
125
126 int cb=-1;
127 CBFromExpression(cb,expr);
128 assert( (cb==Odd) || (cb==Even));
129 this->checkerboard=cb;
130
131 auto exprCopy = expr;
132 ExpressionViewOpen(exprCopy);
133 auto me = View(AcceleratorWriteDiscard);
134 accelerator_for(ss,me.size(),vobj::Nsimd(),{
135 auto tmp = eval(ss,exprCopy);
136 coalescedWrite(me[ss],tmp);
137 });
138 me.ViewClose();
139 ExpressionViewClose(exprCopy);
140 return *this;
141 }
142 template <typename Op, typename T1,typename T2> inline Lattice<vobj> & operator=(const LatticeBinaryExpression<Op,T1,T2> &expr)
143 {
144 GRID_TRACE("ExpressionTemplateEval");
145 GridBase *egrid(nullptr);
146 GridFromExpression(egrid,expr);
147 assert(egrid!=nullptr);
148 conformable(this->_grid,egrid);
149
150 int cb=-1;
151 CBFromExpression(cb,expr);
152 assert( (cb==Odd) || (cb==Even));
153 this->checkerboard=cb;
154
155 auto exprCopy = expr;
156 ExpressionViewOpen(exprCopy);
157 auto me = View(AcceleratorWriteDiscard);
158 accelerator_for(ss,me.size(),vobj::Nsimd(),{
159 auto tmp = eval(ss,exprCopy);
160 coalescedWrite(me[ss],tmp);
161 });
162 me.ViewClose();
163 ExpressionViewClose(exprCopy);
164 return *this;
165 }
166 template <typename Op, typename T1,typename T2,typename T3> inline Lattice<vobj> & operator=(const LatticeTrinaryExpression<Op,T1,T2,T3> &expr)
167 {
168 GRID_TRACE("ExpressionTemplateEval");
169 GridBase *egrid(nullptr);
170 GridFromExpression(egrid,expr);
171 assert(egrid!=nullptr);
172 conformable(this->_grid,egrid);
173
174 int cb=-1;
175 CBFromExpression(cb,expr);
176 assert( (cb==Odd) || (cb==Even));
177 this->checkerboard=cb;
178 auto exprCopy = expr;
179 ExpressionViewOpen(exprCopy);
180 auto me = View(AcceleratorWriteDiscard);
181 accelerator_for(ss,me.size(),vobj::Nsimd(),{
182 auto tmp = eval(ss,exprCopy);
183 coalescedWrite(me[ss],tmp);
184 });
185 me.ViewClose();
186 ExpressionViewClose(exprCopy);
187 return *this;
188 }
189 //GridFromExpression is tricky to do
190 template<class Op,class T1>
192 this->_grid = nullptr;
193 GridFromExpression(this->_grid,expr);
194 assert(this->_grid!=nullptr);
195
196 int cb=-1;
197 CBFromExpression(cb,expr);
198 assert( (cb==Odd) || (cb==Even));
199 this->checkerboard=cb;
200
201 resize(this->_grid->oSites());
202
203 *this = expr;
204 }
205 template<class Op,class T1, class T2>
207 this->_grid = nullptr;
208 GridFromExpression(this->_grid,expr);
209 assert(this->_grid!=nullptr);
210
211 int cb=-1;
212 CBFromExpression(cb,expr);
213 assert( (cb==Odd) || (cb==Even));
214 this->checkerboard=cb;
215
216 resize(this->_grid->oSites());
217
218 *this = expr;
219 }
220 template<class Op,class T1, class T2, class T3>
222 this->_grid = nullptr;
223 GridFromExpression(this->_grid,expr);
224 assert(this->_grid!=nullptr);
225
226 int cb=-1;
227 CBFromExpression(cb,expr);
228 assert( (cb==Odd) || (cb==Even));
229 this->checkerboard=cb;
230
231 resize(this->_grid->oSites());
232
233 *this = expr;
234 }
235
236 template<class sobj> inline Lattice<vobj> & operator = (const sobj & r){
237 vobj vtmp;
238 vtmp = r;
239#if 1
240 deviceVector<vobj> vvtmp(1);
241 acceleratorPut(vvtmp[0],vtmp);
242 vobj *vvtmp_p = & vvtmp[0];
243 auto me = View(AcceleratorWrite);
244 accelerator_for(ss,me.size(),vobj::Nsimd(),{
245 auto stmp=coalescedRead(*vvtmp_p);
246 coalescedWrite(me[ss],stmp);
247 });
248#else
249 auto me = View(CpuWrite);
250 thread_for(ss,me.size(),{
251 me[ss]= r;
252 });
253#endif
254 me.ViewClose();
255 return *this;
256 }
257
259 // Follow rule of five, with Constructor requires "grid" passed
260 // to user defined constructor
262 // user defined constructor
265 this->_grid = grid;
266 resize(this->_grid->oSites());
267 assert((((uint64_t)&this->_odata[0])&0xF) ==0);
268 this->checkerboard=0;
269 SetViewMode(mode);
270 }
271
272 // virtual ~Lattice(void) = default;
273
274 void reset(GridBase* grid) {
275 if (this->_grid != grid) {
276 this->_grid = grid;
277 this->resize(grid->oSites());
278 this->checkerboard = 0;
279 }
280 }
281
282 // copy constructor
284 Lattice(const Lattice& r){
285 this->_grid = r.Grid();
286 resize(this->_grid->oSites());
287 *this = r;
288 }
289
290 // move constructor
293 this->_grid = r.Grid();
294 this->_odata = r._odata;
295 this->_odata_size = r._odata_size;
296 this->checkerboard= r.Checkerboard();
297 r._odata = nullptr;
298 r._odata_size = 0;
299 }
300
301 // assignment template
303 template<class robj> inline Lattice<vobj> & operator = (const Lattice<robj> & r){
304 typename std::enable_if<!std::is_same<robj,vobj>::value,int>::type i=0;
305 conformable(*this,r);
306 this->checkerboard = r.Checkerboard();
307 auto him= r.View(AcceleratorRead);
308 auto me = View(AcceleratorWriteDiscard);
309 accelerator_for(ss,me.size(),vobj::Nsimd(),{
310 coalescedWrite(me[ss],him(ss));
311 });
312 me.ViewClose(); him.ViewClose();
313 return *this;
314 }
315
317 // Copy assignment
320 this->checkerboard = r.Checkerboard();
321 conformable(*this,r);
322 auto him= r.View(AcceleratorRead);
323 auto me = View(AcceleratorWriteDiscard);
324 accelerator_for(ss,me.size(),vobj::Nsimd(),{
325 coalescedWrite(me[ss],him(ss));
326 });
327 me.ViewClose(); him.ViewClose();
328 return *this;
329 }
330
331 // Move assignment possible if same type
334
335 resize(0); // deletes if appropriate
336 this->_grid = r.Grid();
337 this->_odata = r._odata;
338 this->_odata_size = r._odata_size;
339 this->checkerboard= r.Checkerboard();
340
341 r._odata = nullptr;
342 r._odata_size = 0;
343
344 return *this;
345 }
346
348 // *=,+=,-= operators inherit behvour from correspond */+/- operation
350 template<class T> inline Lattice<vobj> &operator *=(const T &r) {
351 *this = (*this)*r;
352 return *this;
353 }
354
355 template<class T> inline Lattice<vobj> &operator -=(const T &r) {
356 *this = (*this)-r;
357 return *this;
358 }
359 template<class T> inline Lattice<vobj> &operator +=(const T &r) {
360 *this = (*this)+r;
361 return *this;
362 }
363
364 friend inline void swap(Lattice &l, Lattice &r) {
365 conformable(l,r);
369 tmp = *lp; *lp=*rp; *rp=tmp;
370 }
371
372}; // class Lattice
373
374template<class vobj> std::ostream& operator<< (std::ostream& stream, const Lattice<vobj> &o){
375 typedef typename vobj::scalar_object sobj;
376 for(int64_t g=0;g<o.Grid()->_gsites;g++){
377
378 Coordinate gcoor;
379 o.Grid()->GlobalIndexToGlobalCoor(g,gcoor);
380
381 sobj ss;
382 peekSite(ss,o,gcoor);
383 stream<<"[";
384 for(int d=0;d<gcoor.size();d++){
385 stream<<gcoor[d];
386 if(d!=gcoor.size()-1) stream<<",";
387 }
388 stream<<"]\t";
389 stream<<ss<<std::endl;
390 }
391 return stream;
392}
393
395
void acceleratorPut(T &dev, const T &host)
#define accelerator_for(iterator, num, nsimd,...)
std::vector< T, devAllocator< T > > deviceVector
static const int Even
static const int Odd
AcceleratorVector< int, MaxDims > Coordinate
Definition Coordinate.h:95
accelerator_inline void GridFromExpression(GridBase *&grid, const T1 &lat)
Definition Lattice_ET.h:207
void ExpressionViewOpen(T1 &lat)
Definition Lattice_ET.h:278
void ExpressionViewClose(T1 &lat)
Definition Lattice_ET.h:309
void CBFromExpression(int &cb, const T1 &lat)
Definition Lattice_ET.h:245
std::ostream & operator<<(std::ostream &stream, const Lattice< vobj > &o)
int GridCshiftPermuteMap[4][16]
void conformable(const Lattice< obj1 > &lhs, const Lattice< obj2 > &rhs)
vobj::scalar_object peekSite(const Lattice< vobj > &l, const Coordinate &site)
ViewMode
@ AcceleratorRead
@ AcceleratorWrite
@ CpuWrite
@ AcceleratorWriteDiscard
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
#define thread_for(i, num,...)
Definition Threads.h:60
#define GRID_TRACE(name)
Definition Tracing.h:68
accelerator_inline size_type size(void) const
Definition Coordinate.h:52
int oSites(void) const
void GlobalIndexToGlobalCoor(int64_t gidx, Coordinate &gcoor)
CoarseSiteVector * _odata
accelerator_inline LatticeAccelerator()
accelerator_inline int Checkerboard(void) const
void ViewClose(void)
Lattice(Lattice &&r)
Lattice(const Lattice &r)
CoarseSiteVector::scalar_type scalar_type
Lattice< vobj > & operator+=(const T &r)
CoarseSiteVector::scalar_object scalar_object
void PrintCacheState(void)
Lattice(const LatticeTrinaryExpression< Op, T1, T2, T3 > &expr)
Lattice< vobj > & operator*=(const T &r)
Lattice(const LatticeUnaryExpression< Op, T1 > &expr)
void resize(uint64_t size)
Lattice(GridBase *grid, ViewMode mode=AcceleratorWriteDiscard)
CoarseSiteVector vector_object
void reset(GridBase *grid)
Lattice< vobj > & operator=(const LatticeUnaryExpression< Op, T1 > &expr)
friend void swap(Lattice &l, Lattice &r)
Lattice< vobj > & operator=(const LatticeBinaryExpression< Op, T1, T2 > &expr)
Lattice< vobj > & operator-=(const T &r)
Lattice(const LatticeBinaryExpression< Op, T1, T2 > &expr)
Lattice< vobj > & operator=(const LatticeTrinaryExpression< Op, T1, T2, T3 > &expr)
void SetViewMode(ViewMode mode)
LatticeView< vobj > View(ViewMode mode) const
CoarseSiteVector::vector_type vector_type
GridBase * Grid(void) const
void dealloc(void)
static void PrintState(void *CpuPtr)
void deallocate(pointer __p, size_type __n)
pointer allocate(size_type __n, const void *_p=0)