1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 15:55:37 +00:00
Grid/lib/lattice/Lattice_peekpoke.h

208 lines
6.1 KiB
C
Raw Normal View History

2018-01-14 23:58:44 +00:00
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/lattice/Lattice_peekpoke.h
Copyright (C) 2015
Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
Author: Peter Boyle <peterboyle@Peters-MacBook-Pro-2.local>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
See the full license in the file "LICENSE" in the top level distribution directory
2018-01-14 23:58:44 +00:00
*************************************************************************************/
/* END LEGAL */
2015-04-18 20:44:19 +01:00
#ifndef GRID_LATTICE_PEEK_H
#define GRID_LATTICE_PEEK_H
///////////////////////////////////////////////
// Peeking and poking around
///////////////////////////////////////////////
2018-01-14 23:58:44 +00:00
NAMESPACE_BEGIN(Grid);
2018-01-24 13:36:31 +00:00
// FIXME accelerator_loop and accelerator_inline these
2018-01-14 23:58:44 +00:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// Peek internal indices of a Lattice object
////////////////////////////////////////////////////////////////////////////////////////////////////
2018-01-24 13:36:31 +00:00
template<int Index,class vobj>
2018-01-26 23:06:03 +00:00
auto PeekIndex(const Lattice<vobj> &lhs,int i) -> Lattice<decltype(peekIndex<Index>(lhs[0],i))>
2018-01-14 23:58:44 +00:00
{
2018-01-27 00:04:12 +00:00
Lattice<decltype(peekIndex<Index>(lhs[0],i))> ret(lhs.Grid());
2018-01-26 23:06:03 +00:00
ret.Checkerboard()=lhs.Checkerboard();
2018-01-24 13:36:31 +00:00
cpu_loop( ss, lhs, {
2018-01-26 23:06:03 +00:00
ret[ss] = peekIndex<Index>(lhs[ss],i);
2018-01-24 13:36:31 +00:00
});
2018-01-14 23:58:44 +00:00
return ret;
};
2018-01-24 13:36:31 +00:00
template<int Index,class vobj>
2018-01-26 23:06:03 +00:00
auto PeekIndex(const Lattice<vobj> &lhs,int i,int j) -> Lattice<decltype(peekIndex<Index>(lhs[0],i,j))>
2018-01-14 23:58:44 +00:00
{
2018-01-27 00:04:12 +00:00
Lattice<decltype(peekIndex<Index>(lhs[0],i,j))> ret(lhs.Grid());
2018-01-26 23:06:03 +00:00
ret.Checkerboard()=lhs.Checkerboard();
2018-01-24 13:36:31 +00:00
cpu_loop( ss, lhs, {
2018-01-26 23:06:03 +00:00
ret[ss] = peekIndex<Index>(lhs[ss],i,j);
2018-01-24 13:36:31 +00:00
});
2018-01-14 23:58:44 +00:00
return ret;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// Poke internal indices of a Lattice object
////////////////////////////////////////////////////////////////////////////////////////////////////
2018-01-24 13:36:31 +00:00
template<int Index,class vobj>
2018-01-26 23:06:03 +00:00
void PokeIndex(Lattice<vobj> &lhs,const Lattice<decltype(peekIndex<Index>(lhs[0],0))> & rhs,int i)
2018-01-14 23:58:44 +00:00
{
2018-01-24 13:36:31 +00:00
cpu_loop( ss, lhs, {
2018-01-26 23:06:03 +00:00
pokeIndex<Index>(lhs[ss],rhs[ss],i);
2018-01-24 13:36:31 +00:00
});
2018-01-14 23:58:44 +00:00
}
2018-01-24 13:36:31 +00:00
template<int Index,class vobj>
2018-01-26 23:06:03 +00:00
void PokeIndex(Lattice<vobj> &lhs,const Lattice<decltype(peekIndex<Index>(lhs[0],0,0))> & rhs,int i,int j)
2018-01-14 23:58:44 +00:00
{
2018-01-24 13:36:31 +00:00
cpu_loop( ss, lhs, {
2018-01-26 23:06:03 +00:00
pokeIndex<Index>(lhs[ss],rhs[ss],i,j);
2018-01-24 13:36:31 +00:00
});
2018-01-14 23:58:44 +00:00
}
//////////////////////////////////////////////////////
// Poke a scalar object into the SIMD array
//////////////////////////////////////////////////////
2018-01-24 13:36:31 +00:00
template<class vobj,class sobj>
2018-01-14 23:58:44 +00:00
void pokeSite(const sobj &s,Lattice<vobj> &l,const std::vector<int> &site){
2018-01-27 00:04:12 +00:00
GridBase *grid=l.Grid();
2018-01-14 23:58:44 +00:00
typedef typename vobj::scalar_type scalar_type;
typedef typename vobj::vector_type vector_type;
int Nsimd = grid->Nsimd();
2018-01-27 00:04:12 +00:00
assert( l.Checkerboard()== l.Grid()->CheckerBoard(site));
2018-01-14 23:58:44 +00:00
assert( sizeof(sobj)*Nsimd == sizeof(vobj));
int rank,odx,idx;
// Optional to broadcast from node 0.
grid->GlobalCoorToRankIndex(rank,odx,idx,site);
grid->Broadcast(grid->BossRank(),s);
std::vector<sobj> buf(Nsimd);
// extract-modify-merge cycle is easiest way and this is not perf critical
if ( rank == grid->ThisRank() ) {
2018-01-26 23:06:03 +00:00
extract(l[odx],buf);
2018-01-14 23:58:44 +00:00
buf[idx] = s;
2018-01-26 23:06:03 +00:00
merge(l[odx],buf);
2018-01-14 23:58:44 +00:00
}
return;
};
//////////////////////////////////////////////////////////
// Peek a scalar object from the SIMD array
//////////////////////////////////////////////////////////
template<class vobj,class sobj>
void peekSite(sobj &s,const Lattice<vobj> &l,const std::vector<int> &site){
2015-04-18 20:44:19 +01:00
2018-01-27 00:04:12 +00:00
GridBase *grid=l.Grid();
2015-04-18 20:44:19 +01:00
2018-01-14 23:58:44 +00:00
typedef typename vobj::scalar_type scalar_type;
typedef typename vobj::vector_type vector_type;
2015-04-18 20:44:19 +01:00
2018-01-14 23:58:44 +00:00
int Nsimd = grid->Nsimd();
2015-04-18 20:44:19 +01:00
2018-01-27 00:04:12 +00:00
assert( l.Checkerboard() == l.Grid()->CheckerBoard(site));
2018-01-14 23:58:44 +00:00
int rank,odx,idx;
grid->GlobalCoorToRankIndex(rank,odx,idx,site);
2018-01-14 23:58:44 +00:00
std::vector<sobj> buf(Nsimd);
2018-01-26 23:06:03 +00:00
extract(l[odx],buf);
2015-04-18 20:44:19 +01:00
2018-01-14 23:58:44 +00:00
s = buf[idx];
2018-01-14 23:58:44 +00:00
grid->Broadcast(rank,s);
2015-04-18 20:44:19 +01:00
2018-01-14 23:58:44 +00:00
return;
};
2015-04-18 20:44:19 +01:00
2018-01-14 23:58:44 +00:00
//////////////////////////////////////////////////////////
// Peek a scalar object from the SIMD array
//////////////////////////////////////////////////////////
template<class vobj,class sobj>
void peekLocalSite(sobj &s,const Lattice<vobj> &l,std::vector<int> &site){
2018-01-27 00:04:12 +00:00
GridBase *grid = l.Grid();
2018-01-14 23:58:44 +00:00
typedef typename vobj::scalar_type scalar_type;
typedef typename vobj::vector_type vector_type;
2018-01-14 23:58:44 +00:00
int Nsimd = grid->Nsimd();
2018-01-27 00:04:12 +00:00
assert( l.Checkerboard()== l.Grid()->CheckerBoard(site));
2018-01-14 23:58:44 +00:00
assert( sizeof(sobj)*Nsimd == sizeof(vobj));
2018-01-14 23:58:44 +00:00
static const int words=sizeof(vobj)/sizeof(vector_type);
int odx,idx;
idx= grid->iIndex(site);
odx= grid->oIndex(site);
2018-01-26 23:06:03 +00:00
scalar_type * vp = (scalar_type *)&l[odx];
2018-01-14 23:58:44 +00:00
scalar_type * pt = (scalar_type *)&s;
2016-10-24 19:24:21 +01:00
2018-01-14 23:58:44 +00:00
for(int w=0;w<words;w++){
pt[w] = vp[idx+w*Nsimd];
}
2018-01-14 23:58:44 +00:00
return;
};
2018-01-14 23:58:44 +00:00
template<class vobj,class sobj>
void pokeLocalSite(const sobj &s,Lattice<vobj> &l,std::vector<int> &site){
2018-01-27 00:04:12 +00:00
GridBase *grid=l.Grid();
2018-01-14 23:58:44 +00:00
typedef typename vobj::scalar_type scalar_type;
typedef typename vobj::vector_type vector_type;
2018-01-14 23:58:44 +00:00
int Nsimd = grid->Nsimd();
2018-01-27 00:04:12 +00:00
assert( l.Checkerboard()== l.Grid()->CheckerBoard(site));
2018-01-14 23:58:44 +00:00
assert( sizeof(sobj)*Nsimd == sizeof(vobj));
2018-01-14 23:58:44 +00:00
static const int words=sizeof(vobj)/sizeof(vector_type);
int odx,idx;
idx= grid->iIndex(site);
odx= grid->oIndex(site);
2018-01-26 23:06:03 +00:00
scalar_type * vp = (scalar_type *)&l[odx];
2018-01-14 23:58:44 +00:00
scalar_type * pt = (scalar_type *)&s;
2018-01-14 23:58:44 +00:00
for(int w=0;w<words;w++){
vp[idx+w*Nsimd] = pt[w];
}
2018-01-14 23:58:44 +00:00
return;
};
2018-01-14 23:58:44 +00:00
NAMESPACE_END(Grid);
2015-04-18 20:44:19 +01:00
#endif