2016-01-02 14:51:32 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
2017-03-15 06:16:16 +00:00
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
2016-01-02 14:51:32 +00:00
|
|
|
|
|
|
|
Source file: ./tests/Test_stencil.cc
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
|
|
|
|
|
|
|
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|
|
|
Author: Peter Boyle <peterboyle@Peters-MacBook-Pro-2.local>
|
|
|
|
Author: paboyle <paboyle@ph.ed.ac.uk>
|
|
|
|
|
|
|
|
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
|
|
|
|
*************************************************************************************/
|
|
|
|
/* END LEGAL */
|
2016-07-07 22:31:07 +01:00
|
|
|
#include <Grid/Grid.h>
|
2015-04-14 20:22:04 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Grid;
|
2018-01-15 09:37:58 +00:00
|
|
|
;
|
2015-04-14 20:22:04 +01:00
|
|
|
|
2017-03-15 06:16:16 +00:00
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
Grid_init(&argc, &argv);
|
2015-04-14 20:22:04 +01:00
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
// typedef LatticeColourMatrix Field;
|
|
|
|
typedef LatticeComplex Field;
|
|
|
|
typedef typename Field::vector_object vobj;
|
|
|
|
typedef typename vobj::scalar_object sobj;
|
2015-05-11 12:43:10 +01:00
|
|
|
|
2015-05-11 18:59:03 +01:00
|
|
|
std::vector<int> latt_size = GridDefaultLatt();
|
2015-07-01 22:45:15 +01:00
|
|
|
std::vector<int> simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd());
|
2015-05-11 18:59:03 +01:00
|
|
|
std::vector<int> mpi_layout = GridDefaultMpi();
|
2015-04-14 20:22:04 +01:00
|
|
|
|
2015-04-26 15:51:09 +01:00
|
|
|
double volume = latt_size[0]*latt_size[1]*latt_size[2]*latt_size[3];
|
2017-03-15 06:16:16 +00:00
|
|
|
|
2015-04-26 15:51:09 +01:00
|
|
|
GridCartesian Fine(latt_size,simd_layout,mpi_layout);
|
2017-06-22 08:14:34 +01:00
|
|
|
GridRedBlackCartesian rbFine(&Fine);
|
2015-04-26 15:51:09 +01:00
|
|
|
GridParallelRNG fRNG(&Fine);
|
2015-11-04 10:03:04 +00:00
|
|
|
|
2017-04-01 16:29:40 +01:00
|
|
|
// fRNG.SeedFixedIntegers(std::vector<int>({45,12,81,9});
|
2015-04-28 08:11:59 +01:00
|
|
|
std::vector<int> seeds({1,2,3,4});
|
|
|
|
fRNG.SeedFixedIntegers(seeds);
|
2017-03-15 06:16:16 +00:00
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
Field Foo(&Fine);
|
|
|
|
Field Bar(&Fine);
|
|
|
|
Field Check(&Fine);
|
|
|
|
Field Diff(&Fine);
|
|
|
|
LatticeComplex lex(&Fine);
|
|
|
|
|
2017-03-15 06:16:16 +00:00
|
|
|
lex = zero;
|
2015-04-26 15:51:09 +01:00
|
|
|
random(fRNG,Foo);
|
|
|
|
gaussian(fRNG,Bar);
|
2015-04-14 20:22:04 +01:00
|
|
|
|
2017-02-20 22:49:23 +00:00
|
|
|
for (int i=0;i<simd_layout.size();i++){
|
|
|
|
std::cout <<" simd layout "<<i<<" = "<<simd_layout[i]<<std::endl;
|
|
|
|
}
|
2015-11-04 10:03:04 +00:00
|
|
|
Integer stride =1000;
|
|
|
|
{
|
|
|
|
LatticeComplex coor(&Fine);
|
|
|
|
|
|
|
|
for(int d=0;d<Nd;d++){
|
|
|
|
LatticeCoordinate(coor,d);
|
|
|
|
lex = lex + coor*stride;
|
|
|
|
stride=stride/10;
|
|
|
|
}
|
|
|
|
Foo=lex;
|
|
|
|
}
|
2015-04-14 20:22:04 +01:00
|
|
|
|
2016-02-18 00:27:11 +00:00
|
|
|
typedef CartesianStencil<vobj,vobj> Stencil;
|
2015-04-14 20:22:04 +01:00
|
|
|
for(int dir=0;dir<4;dir++){
|
2015-04-28 08:11:59 +01:00
|
|
|
for(int disp=0;disp<Fine._fdimensions[dir];disp++){
|
2015-04-14 20:22:04 +01:00
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
std::cout<< std::fixed <<GridLogMessage << "Using stencil to shift dim "<<dir<< " by "<<disp<<std::endl;
|
2015-04-14 20:22:04 +01:00
|
|
|
// start to test the Cartesian npoint stencil infrastructure
|
|
|
|
int npoint=1;
|
|
|
|
std::vector<int> directions(npoint,dir);
|
|
|
|
std::vector<int> displacements(npoint,disp);
|
|
|
|
|
2015-11-29 00:31:02 +00:00
|
|
|
Stencil myStencil(&Fine,npoint,0,directions,displacements);
|
2015-04-14 20:22:04 +01:00
|
|
|
std::vector<int> ocoor(4);
|
|
|
|
for(int o=0;o<Fine.oSites();o++){
|
|
|
|
Fine.oCoorFromOindex(ocoor,o);
|
|
|
|
ocoor[dir]=(ocoor[dir]+disp)%Fine._rdimensions[dir];
|
|
|
|
}
|
2017-03-15 06:16:16 +00:00
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
SimpleCompressor<vobj> compress;
|
2016-01-10 19:20:16 +00:00
|
|
|
myStencil.HaloExchange(Foo,compress);
|
2015-04-14 20:22:04 +01:00
|
|
|
|
|
|
|
Bar = Cshift(Foo,dir,disp);
|
|
|
|
|
|
|
|
// Implement a stencil code that should agree with cshift!
|
2018-01-27 00:04:12 +00:00
|
|
|
for(int i=0;i<Check.Grid()->oSites();i++){
|
2015-08-14 00:01:04 +01:00
|
|
|
|
|
|
|
int permute_type;
|
|
|
|
StencilEntry *SE;
|
|
|
|
SE = myStencil.GetEntry(permute_type,0,i);
|
|
|
|
|
|
|
|
if ( SE->_is_local && SE->_permute )
|
2018-01-26 23:09:03 +00:00
|
|
|
permute(Check[i],Foo[SE->_offset],permute_type);
|
2015-08-14 00:01:04 +01:00
|
|
|
else if (SE->_is_local)
|
2018-01-26 23:09:03 +00:00
|
|
|
Check[i] = Foo[SE->_offset];
|
2017-02-20 22:49:23 +00:00
|
|
|
else {
|
2018-01-26 23:09:03 +00:00
|
|
|
Check[i] = myStencil.CommBuf()[SE->_offset];
|
|
|
|
// std::cout << " receive "<<i<<" " << Check[i]<<std::endl;
|
|
|
|
// std::cout << " Foo "<<i<<" " << Foo[i]<<std::endl;
|
2017-02-20 22:49:23 +00:00
|
|
|
}
|
2015-04-14 20:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Real nrmC = norm2(Check);
|
|
|
|
Real nrmB = norm2(Bar);
|
2015-05-10 15:30:29 +01:00
|
|
|
Diff = Check-Bar;
|
|
|
|
Real nrm = norm2(Diff);
|
2015-07-23 17:31:13 +01:00
|
|
|
std::cout<<GridLogMessage<<"N2diff ="<<nrm<<" "<<nrmC<<" " <<nrmB<<std::endl;
|
2015-04-14 20:22:04 +01:00
|
|
|
|
|
|
|
std::vector<int> coor(4);
|
|
|
|
for(coor[3]=0;coor[3]<latt_size[3]/mpi_layout[3];coor[3]++){
|
|
|
|
for(coor[2]=0;coor[2]<latt_size[2]/mpi_layout[2];coor[2]++){
|
|
|
|
for(coor[1]=0;coor[1]<latt_size[1]/mpi_layout[1];coor[1]++){
|
|
|
|
for(coor[0]=0;coor[0]<latt_size[0]/mpi_layout[0];coor[0]++){
|
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
RealD diff;
|
|
|
|
sobj check,bar;
|
2015-04-14 20:22:04 +01:00
|
|
|
peekSite(check,Check,coor);
|
|
|
|
peekSite(bar,Bar,coor);
|
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
sobj ddiff;
|
|
|
|
ddiff = check -bar;
|
|
|
|
diff =norm2(ddiff);
|
|
|
|
if ( diff > 0){
|
|
|
|
std::cout <<"Coor (" << coor[0]<<","<<coor[1]<<","<<coor[2]<<","<<coor[3]
|
|
|
|
<<") " <<check<<" vs "<<bar<<std::endl;
|
|
|
|
}
|
|
|
|
|
2017-03-15 06:16:16 +00:00
|
|
|
|
2015-04-14 20:22:04 +01:00
|
|
|
}}}}
|
|
|
|
|
2017-02-20 22:49:23 +00:00
|
|
|
if (nrm > 1.0e-4) {
|
2018-01-26 23:09:03 +00:00
|
|
|
for(int i=0;i<Check.size();i++){
|
|
|
|
std::cout << i<<" Check.odata "<<Check[i]<< "\n"<<i<<" Bar.odata "<<Bar[i]<<std::endl;
|
2017-02-20 22:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-07 05:58:34 +00:00
|
|
|
if (nrm > 1.0e-4) exit(-1);
|
2015-11-04 10:03:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout<<GridLogMessage<<"Testing RedBlack\n ";
|
|
|
|
|
|
|
|
|
|
|
|
Field EFoo(&rbFine);
|
|
|
|
Field OFoo(&rbFine);
|
|
|
|
Field ECheck(&rbFine);
|
|
|
|
Field OCheck(&rbFine);
|
|
|
|
pickCheckerboard(Even,EFoo,Foo);
|
|
|
|
pickCheckerboard(Odd ,OFoo,Foo);
|
|
|
|
|
|
|
|
for(int dir=0;dir<4;dir++){
|
|
|
|
for(int disp=0;disp<rbFine._fdimensions[dir];disp++){
|
|
|
|
|
|
|
|
std::cout<<GridLogMessage << "Using stencil to shift rb dim "<<dir<< " by "<<disp<<std::endl;
|
|
|
|
// start to test the Cartesian npoint stencil infrastructure
|
|
|
|
int npoint=1;
|
|
|
|
std::vector<int> directions(npoint,dir);
|
|
|
|
std::vector<int> displacements(npoint,disp);
|
|
|
|
|
2015-11-29 00:31:02 +00:00
|
|
|
Stencil EStencil(&rbFine,npoint,Even,directions,displacements);
|
|
|
|
Stencil OStencil(&rbFine,npoint,Odd,directions,displacements);
|
2015-11-04 10:03:04 +00:00
|
|
|
|
|
|
|
std::vector<int> ocoor(4);
|
|
|
|
for(int o=0;o<Fine.oSites();o++){
|
|
|
|
Fine.oCoorFromOindex(ocoor,o);
|
|
|
|
ocoor[dir]=(ocoor[dir]+disp)%Fine._rdimensions[dir];
|
|
|
|
}
|
2017-03-15 06:16:16 +00:00
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
SimpleCompressor<vobj> compress;
|
|
|
|
|
|
|
|
Bar = Cshift(Foo,dir,disp);
|
|
|
|
|
|
|
|
if ( disp & 0x1 ) {
|
2018-01-26 23:09:03 +00:00
|
|
|
ECheck.Checkerboard() = Even;
|
|
|
|
OCheck.Checkerboard() = Odd;
|
2017-03-15 06:16:16 +00:00
|
|
|
} else {
|
2018-01-26 23:09:03 +00:00
|
|
|
ECheck.Checkerboard() = Odd;
|
|
|
|
OCheck.Checkerboard() = Even;
|
2015-11-04 10:03:04 +00:00
|
|
|
}
|
2015-12-30 19:29:48 +00:00
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
// Implement a stencil code that should agree with that darn cshift!
|
2017-02-07 05:58:34 +00:00
|
|
|
EStencil.HaloExchange(EFoo,compress);
|
2018-01-27 00:04:12 +00:00
|
|
|
for(int i=0;i<OCheck.Grid()->oSites();i++){
|
2015-11-04 10:03:04 +00:00
|
|
|
int permute_type;
|
|
|
|
StencilEntry *SE;
|
|
|
|
SE = EStencil.GetEntry(permute_type,0,i);
|
2015-12-30 19:29:48 +00:00
|
|
|
// std::cout << "Even source "<< i<<" -> " <<SE->_offset << " "<< SE->_is_local<<std::endl;
|
2015-11-04 10:03:04 +00:00
|
|
|
|
|
|
|
if ( SE->_is_local && SE->_permute )
|
2018-01-26 23:09:03 +00:00
|
|
|
permute(OCheck[i],EFoo[SE->_offset],permute_type);
|
2015-11-04 10:03:04 +00:00
|
|
|
else if (SE->_is_local)
|
2018-01-26 23:09:03 +00:00
|
|
|
OCheck[i] = EFoo[SE->_offset];
|
2017-03-15 06:16:16 +00:00
|
|
|
else
|
2018-01-26 23:09:03 +00:00
|
|
|
OCheck[i] = EStencil.CommBuf()[SE->_offset];
|
2015-11-04 10:03:04 +00:00
|
|
|
}
|
2017-02-07 05:58:34 +00:00
|
|
|
OStencil.HaloExchange(OFoo,compress);
|
2018-01-27 00:04:12 +00:00
|
|
|
for(int i=0;i<ECheck.Grid()->oSites();i++){
|
2015-11-04 10:03:04 +00:00
|
|
|
int permute_type;
|
|
|
|
StencilEntry *SE;
|
|
|
|
SE = OStencil.GetEntry(permute_type,0,i);
|
2015-12-30 19:29:48 +00:00
|
|
|
// std::cout << "ODD source "<< i<<" -> " <<SE->_offset << " "<< SE->_is_local<<std::endl;
|
2017-03-15 06:16:16 +00:00
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
if ( SE->_is_local && SE->_permute )
|
2018-01-26 23:09:03 +00:00
|
|
|
permute(ECheck[i],OFoo[SE->_offset],permute_type);
|
2015-11-04 10:03:04 +00:00
|
|
|
else if (SE->_is_local)
|
2018-01-26 23:09:03 +00:00
|
|
|
ECheck[i] = OFoo[SE->_offset];
|
2017-03-15 06:16:16 +00:00
|
|
|
else
|
2018-01-26 23:09:03 +00:00
|
|
|
ECheck[i] = OStencil.CommBuf()[SE->_offset];
|
2015-11-04 10:03:04 +00:00
|
|
|
}
|
2017-03-15 06:16:16 +00:00
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
setCheckerboard(Check,ECheck);
|
|
|
|
setCheckerboard(Check,OCheck);
|
2017-03-15 06:16:16 +00:00
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
Real nrmC = norm2(Check);
|
|
|
|
Real nrmB = norm2(Bar);
|
|
|
|
Diff = Check-Bar;
|
|
|
|
Real nrm = norm2(Diff);
|
|
|
|
std::cout<<GridLogMessage<<"RB N2diff ="<<nrm<<" "<<nrmC<<" " <<nrmB<<std::endl;
|
|
|
|
|
|
|
|
std::vector<int> coor(4);
|
|
|
|
for(coor[3]=0;coor[3]<latt_size[3]/mpi_layout[3];coor[3]++){
|
|
|
|
for(coor[2]=0;coor[2]<latt_size[2]/mpi_layout[2];coor[2]++){
|
|
|
|
for(coor[1]=0;coor[1]<latt_size[1]/mpi_layout[1];coor[1]++){
|
|
|
|
for(coor[0]=0;coor[0]<latt_size[0]/mpi_layout[0];coor[0]++){
|
|
|
|
|
|
|
|
RealD diff;
|
|
|
|
sobj check,bar;
|
|
|
|
peekSite(check,Check,coor);
|
|
|
|
peekSite(bar,Bar,coor);
|
|
|
|
|
|
|
|
sobj ddiff;
|
|
|
|
ddiff = check -bar;
|
|
|
|
diff =norm2(ddiff);
|
|
|
|
if ( diff > 0){
|
|
|
|
std::cout <<"Coor (" << coor[0]<<","<<coor[1]<<","<<coor[2]<<","<<coor[3] <<") "
|
2017-03-15 06:16:16 +00:00
|
|
|
<<"shift "<<disp<<" dir "<< dir
|
2015-11-04 10:03:04 +00:00
|
|
|
<< " stencil impl " <<check<<" vs cshift impl "<<bar<<std::endl;
|
|
|
|
}
|
2017-03-15 06:16:16 +00:00
|
|
|
|
2015-11-04 10:03:04 +00:00
|
|
|
}}}}
|
2015-04-14 20:22:04 +01:00
|
|
|
|
2017-02-07 05:58:34 +00:00
|
|
|
if (nrm > 1.0e-4) exit(-1);
|
2015-04-14 20:22:04 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Grid_finalize();
|
|
|
|
}
|