Grid 0.7.0
GeneralLocalStencil.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/GeneralLocalStencil.h
6
7 Copyright (C) 2019
8
9 Author: Peter Boyle <paboyle@ph.ed.ac.uk>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along
22 with this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 See the full license in the file "LICENSE" in the top level distribution directory
26*************************************************************************************/
27/* END LEGAL */
28#pragma once
30
31// Share with Cartesian Stencil
33 uint64_t _offset; // 4 bytes
34 uint8_t _permute; // 1 bytes // Horrible alignment properties
35 uint8_t _wrap; // 1 bytes // Horrible alignment properties
36};
40
41// Could pack to 8 + 4 + 4 = 128 bit and use
42
44 public:
46 // Basic Grid and stencil info
48 int _npoints; // Move to template param?
50
51 accelerator_inline GeneralStencilEntry * GetEntry(int point,int osite) const {
52 return & this->_entries_p[point+this->_npoints*osite];
53 }
54 void ViewClose(void){};
55};
56
57// The Stencil Class itself
60public:
62
63protected:
65
66public:
67 GridBase *Grid(void) const { return _grid; }
68
69 View_type View(int mode) const {
70 View_type accessor(*( (View_type *) this));
71 return accessor;
72 }
73
74 // Resident in managed memory
76
77 GeneralLocalStencil(GridBase *grid, const std::vector<Coordinate> &shifts)
78 {
79 int npoints = shifts.size();
80 int osites = grid->oSites();
81
82 this->_grid = grid;
83 this->_npoints = npoints;
84 this->_entries.resize(npoints* osites);
85 this->_entries_p = &_entries[0];
86
87 thread_for(site, osites, {
88 Coordinate Coor;
89 Coordinate NbrCoor;
90
91 for(Integer ii=0;ii<npoints;ii++){
92 Integer lex = site*npoints+ii;
95 // Outer index of neighbour Offset calculation
97 grid->oCoorFromOindex(Coor,site);
98 for(int d=0;d<Coor.size();d++){
99 int rd = grid->_rdimensions[d];
100 NbrCoor[d] = (Coor[d] + shifts[ii][d] + rd )%rd;
101 }
102 SE._offset = grid->oIndexReduced(NbrCoor);
103
105 // Inner index permute calculation
106 // Simpler version using icoor calculation
108 SE._permute =0;
109 SE._wrap=0;
110 for(int d=0;d<Coor.size();d++){
111
112 int fd = grid->_fdimensions[d];
113 int rd = grid->_rdimensions[d];
114 int ld = grid->_ldimensions[d];
115 int ly = grid->_simd_layout[d];
116
117 assert((ly==1)||(ly==2)||(ly==grid->Nsimd()));
118
119 int shift = (shifts[ii][d]+fd)%fd; // make it strictly positive 0.. L-1
120 int x = Coor[d]; // x in [0... rd-1] as an oSite
121
122 if ( (x + shift)%fd != (x+shift)%ld ){
123 SE._wrap = 1;
124 }
125
126 int permute_dim = grid->PermuteDim(d);
127 int permute_slice=0;
128 if(permute_dim){
129 int num = shift%rd; // Slice within dest osite cell of slice zero
130 int wrap = shift/rd; // Number of osite local volume cells crossed through
131 // x+num < rd dictates whether we are in same permute state as slice 0
132 if ( x< rd-num ) permute_slice=wrap;
133 else permute_slice=(wrap+1)%ly;
134 }
135 if ( permute_slice ) {
136 int ptype =grid->PermuteType(d);
137 uint8_t mask =0x1<<ptype;
138 SE._permute |= mask;
139 }
140 }
142 // Store in look up table
144 acceleratorPut(this->_entries[lex],SE);
145 }
146 });
147 }
148
149};
150
151
153// Some machinery to streamline making a stencil
155
157public:
158 enum {
161 };
162};
163
164// TODO: put a check somewhere that BACKWARD_CONST > Nd!
165
167inline int Back(const int dir) {
168 // generalShift will use BACKWARD_CONST to determine whether we step forward or
169 // backward. Trick inspired by SIMULATeQCD.
170 return dir + shiftSignal::BACKWARD_CONST;
171}
172
174template<typename... Args>
175void generalShift(Coordinate& shift, int dir) {
176 if (dir >= shiftSignal::BACKWARD_CONST) {
178 shift[dir]+=-1;
179 } else if (dir == shiftSignal::NO_SHIFT) {
180 ; // do nothing
181 } else {
182 shift[dir]+=1;
183 }
184}
185
187template<typename... Args>
188void generalShift(Coordinate& shift, int dir, Args... args) {
189 if (dir >= shiftSignal::BACKWARD_CONST) {
191 shift[dir]+=-1;
192 } else if (dir == shiftSignal::NO_SHIFT) {
193 ; // do nothing
194 } else {
195 shift[dir]+=1;
196 }
197 generalShift(shift, args...);
198}
199
200
202
void acceleratorPut(T &dev, const T &host)
#define accelerator_inline
std::vector< T, devAllocator< T > > deviceVector
AcceleratorVector< int, MaxDims > Coordinate
Definition Coordinate.h:95
int Back(const int dir)
signals that you want to go backwards in direction dir
void generalShift(Coordinate &shift, int dir)
shift one unit in direction dir
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
uint32_t Integer
Definition Simd.h:58
#define thread_for(i, num,...)
Definition Threads.h:60
accelerator_inline size_type size(void) const
Definition Coordinate.h:52
accelerator_inline GeneralStencilEntry * GetEntry(int point, int osite) const
GeneralStencilEntry * _entries_p
GridBase * Grid(void) const
GeneralLocalStencilView View_type
deviceVector< GeneralStencilEntry > _entries
View_type View(int mode) const
GeneralLocalStencil(GridBase *grid, const std::vector< Coordinate > &shifts)
int PermuteDim(int dimension)
Coordinate _fdimensions
int oIndexReduced(Coordinate &ocoor)
int PermuteType(int dimension)
int oSites(void) const
void oCoorFromOindex(Coordinate &coor, int Oindex)
Coordinate _rdimensions
Coordinate _simd_layout
Coordinate _ldimensions
int Nsimd(void) const