Grid 0.7.0
Lattice_peekpoke.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/lattice/Lattice_peekpoke.h
6
7 Copyright (C) 2015
8
9Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
10Author: Peter Boyle <paboyle@ph.ed.ac.uk>
11Author: Peter Boyle <peterboyle@Peters-MacBook-Pro-2.local>
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License along
24 with this program; if not, write to the Free Software Foundation, Inc.,
25 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26
27 See the full license in the file "LICENSE" in the top level distribution directory
28*************************************************************************************/
29/* END LEGAL */
30#ifndef GRID_LATTICE_PEEK_H
31#define GRID_LATTICE_PEEK_H
32
34// Peeking and poking around
36
38
39
40// FIXME accelerator_loop and accelerator_inline these
42// Peek internal indices of a Lattice object
44template<int Index,class vobj>
45auto PeekIndex(const Lattice<vobj> &lhs,int i) -> Lattice<decltype(peekIndex<Index>(vobj(),i))>
46{
47 Lattice<decltype(peekIndex<Index>(vobj(),i))> ret(lhs.Grid());
48 ret.Checkerboard()=lhs.Checkerboard();
49 autoView( ret_v, ret, AcceleratorWrite);
50 autoView( lhs_v, lhs, AcceleratorRead);
51 accelerator_for( ss, lhs_v.size(), 1, {
52 ret_v[ss] = peekIndex<Index>(lhs_v[ss],i);
53 });
54 return ret;
55};
56template<int Index,class vobj>
57auto PeekIndex(const Lattice<vobj> &lhs,int i,int j) -> Lattice<decltype(peekIndex<Index>(vobj(),i,j))>
58{
59 Lattice<decltype(peekIndex<Index>(vobj(),i,j))> ret(lhs.Grid());
60 ret.Checkerboard()=lhs.Checkerboard();
61 autoView( ret_v, ret, AcceleratorWrite);
62 autoView( lhs_v, lhs, AcceleratorRead);
63 accelerator_for( ss, lhs_v.size(), 1, {
64 ret_v[ss] = peekIndex<Index>(lhs_v[ss],i,j);
65 });
66 return ret;
67};
68
70// Poke internal indices of a Lattice object
72template<int Index,class vobj>
73void PokeIndex(Lattice<vobj> &lhs,const Lattice<decltype(peekIndex<Index>(vobj(),0))> & rhs,int i)
74{
75 autoView( rhs_v, rhs, AcceleratorRead);
76 autoView( lhs_v, lhs, AcceleratorWrite);
77 accelerator_for( ss, lhs_v.size(), 1, {
78 pokeIndex<Index>(lhs_v[ss],rhs_v[ss],i);
79 });
80}
81template<int Index,class vobj>
82void PokeIndex(Lattice<vobj> &lhs,const Lattice<decltype(peekIndex<Index>(vobj(),0,0))> & rhs,int i,int j)
83{
84 autoView( rhs_v, rhs, AcceleratorRead);
85 autoView( lhs_v, lhs, AcceleratorWrite);
86 accelerator_for( ss, lhs_v.size(), 1, {
87 pokeIndex<Index>(lhs_v[ss],rhs_v[ss],i,j);
88 });
89}
90
92// Poke a scalar object into the SIMD array
94template<class vobj,class sobj>
95void pokeSite(const sobj &s,Lattice<vobj> &l,const Coordinate &site){
96
97 GridBase *grid=l.Grid();
98
99 int Nsimd = grid->Nsimd();
100
101 assert( l.Checkerboard()== l.Grid()->CheckerBoard(site));
102 assert( sizeof(sobj)*Nsimd == sizeof(vobj));
103
104 int rank,odx,idx;
105 // Optional to broadcast from node 0.
106 grid->GlobalCoorToRankIndex(rank,odx,idx,site);
107 grid->Broadcast(grid->BossRank(),s);
108
109 // extract-modify-merge cycle is easiest way and this is not perf critical
110 ExtractBuffer<sobj> buf(Nsimd);
111 autoView( l_v , l, CpuWrite);
112 if ( rank == grid->ThisRank() ) {
113 extract(l_v[odx],buf);
114 buf[idx] = s;
115 merge(l_v[odx],buf);
116 }
117
118 return;
119};
120
121
123// Peek a scalar object from the SIMD array
125template<class vobj>
126typename vobj::scalar_object peekSite(const Lattice<vobj> &l,const Coordinate &site){
127 typename vobj::scalar_object s;
128 peekSite(s,l,site);
129 return s;
130}
131template<class vobj,class sobj>
132void peekSite(sobj &s,const Lattice<vobj> &l,const Coordinate &site){
133
134 GridBase *grid=l.Grid();
135
136 int Nsimd = grid->Nsimd();
137
138 assert( l.Checkerboard() == l.Grid()->CheckerBoard(site));
139
140 int rank,odx,idx;
141 grid->GlobalCoorToRankIndex(rank,odx,idx,site);
142
143 ExtractBuffer<sobj> buf(Nsimd);
144 autoView( l_v , l, CpuWrite);
145 extract(l_v[odx],buf);
146
147 s = buf[idx];
148
149 grid->Broadcast(rank,s);
150
151 return;
152};
153
155// Peek a scalar object from the SIMD array
157// Must be CPU read view
158template<class vobj,class sobj>
159inline void peekLocalSite(sobj &s,const LatticeView<vobj> &l,Coordinate &site)
160{
161 GridBase *grid = l.getGrid();
162 assert(l.mode==CpuRead);
163 typedef typename vobj::scalar_type scalar_type;
164 typedef typename vobj::vector_type vector_type;
165
166 int Nsimd = grid->Nsimd();
167
168 // assert( l.Checkerboard()== grid->CheckerBoard(site));
169 assert( sizeof(sobj)*Nsimd == sizeof(vobj));
170
171 static const int words=sizeof(vobj)/sizeof(vector_type);
172 int odx,idx;
173 idx= grid->iIndex(site);
174 odx= grid->oIndex(site);
175
176 const vector_type *vp = (const vector_type *) &l[odx];
177 scalar_type * pt = (scalar_type *)&s;
178
179 for(int w=0;w<words;w++){
180 pt[w] = getlane(vp[w],idx);
181 }
182 // std::cout << "peekLocalSite "<<site<<" "<<odx<<","<<idx<<" "<<s<<std::endl;
183 return;
184};
185template<class vobj,class sobj>
186inline void peekLocalSite(sobj &s,const Lattice<vobj> &l,Coordinate &site)
187{
188 autoView(lv,l,CpuRead);
189 peekLocalSite(s,lv,site);
190 return;
191};
192
193// Must be CPU write view
194template<class vobj,class sobj>
195inline void pokeLocalSite(const sobj &s,LatticeView<vobj> &l,Coordinate &site)
196{
197 GridBase *grid=l.getGrid();
198 assert(l.mode==CpuWrite);
199
200 typedef typename vobj::scalar_type scalar_type;
201 typedef typename vobj::vector_type vector_type;
202
203 int Nsimd = grid->Nsimd();
204
205 // assert( l.Checkerboard()== grid->CheckerBoard(site));
206 assert( sizeof(sobj)*Nsimd == sizeof(vobj));
207
208 static const int words=sizeof(vobj)/sizeof(vector_type);
209 int odx,idx;
210 idx= grid->iIndex(site);
211 odx= grid->oIndex(site);
212
213 vector_type * vp = (vector_type *)&l[odx];
214 scalar_type * pt = (scalar_type *)&s;
215 for(int w=0;w<words;w++){
216 putlane(vp[w],pt[w],idx);
217 }
218 return;
219};
220
221template<class vobj,class sobj>
222inline void pokeLocalSite(const sobj &s, Lattice<vobj> &l,Coordinate &site)
223{
224 autoView(lv,l,CpuWrite);
225 pokeLocalSite(s,lv,site);
226 return;
227};
228
230#endif
231
#define accelerator_for(iterator, num, nsimd,...)
AcceleratorVector< int, MaxDims > Coordinate
Definition Coordinate.h:95
accelerator_inline S getlane(const Grid_simd< S, V > &in, int lane)
accelerator_inline void putlane(Grid_simd< S, V > &vec, const S &_S, int lane)
void PokeIndex(Lattice< vobj > &lhs, const Lattice< decltype(peekIndex< Index >(vobj(), 0))> &rhs, int i)
auto PeekIndex(const Lattice< vobj > &lhs, int i) -> Lattice< decltype(peekIndex< Index >(vobj(), i))>
void pokeSite(const sobj &s, Lattice< vobj > &l, const Coordinate &site)
void peekLocalSite(sobj &s, const LatticeView< vobj > &l, Coordinate &site)
void pokeLocalSite(const sobj &s, LatticeView< vobj > &l, Coordinate &site)
vobj::scalar_object peekSite(const Lattice< vobj > &l, const Coordinate &site)
#define autoView(l_v, l, mode)
@ AcceleratorRead
@ CpuRead
@ AcceleratorWrite
@ CpuWrite
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
AcceleratorVector< __T,GRID_MAX_SIMD > ExtractBuffer
accelerator void extract(const vobj &vec, ExtractBuffer< sobj > &extracted)
accelerator void merge(vobj &vec, ExtractBuffer< sobj > &extracted)
accelerator_inline auto peekIndex(const vtype &arg, int i) -> RemoveCRV(TensorIndexRecursion< Level >::peekIndex(arg, 0))
void Broadcast(int root, void *data, int bytes)
virtual int oIndex(Coordinate &coor)
void GlobalCoorToRankIndex(int &rank, int &o_idx, int &i_idx, const Coordinate &gcoor)
virtual int CheckerBoard(const Coordinate &site)=0
virtual int iIndex(Coordinate &lcoor)
int Nsimd(void) const
accelerator_inline int Checkerboard(void) const
GridBase * getGrid(void) const
ViewMode mode
GridBase * Grid(void) const