2016-01-02 14:51:32 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./lib/cshift/Cshift_mpi.h
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
|
|
|
|
|
|
|
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|
|
|
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 */
|
2015-04-18 18:36:48 +01:00
|
|
|
#ifndef _GRID_CSHIFT_MPI_H_
|
|
|
|
#define _GRID_CSHIFT_MPI_H_
|
2015-03-29 20:35:37 +01:00
|
|
|
|
|
|
|
|
2015-04-18 20:44:19 +01:00
|
|
|
namespace Grid {
|
|
|
|
|
2015-06-01 12:25:59 +01:00
|
|
|
template<class vobj> Lattice<vobj> Cshift(const Lattice<vobj> &rhs,int dimension,int shift)
|
2015-03-29 20:35:37 +01:00
|
|
|
{
|
|
|
|
typedef typename vobj::vector_type vector_type;
|
|
|
|
typedef typename vobj::scalar_type scalar_type;
|
|
|
|
|
2015-11-04 09:59:27 +00:00
|
|
|
Lattice<vobj> ret(rhs._grid);
|
2015-03-29 20:35:37 +01:00
|
|
|
|
|
|
|
int fd = rhs._grid->_fdimensions[dimension];
|
|
|
|
int rd = rhs._grid->_rdimensions[dimension];
|
|
|
|
|
|
|
|
// Map to always positive shift modulo global full dimension.
|
|
|
|
shift = (shift+fd)%fd;
|
|
|
|
|
2015-05-31 22:50:03 +01:00
|
|
|
ret.checkerboard = rhs._grid->CheckerBoardDestination(rhs.checkerboard,shift,dimension);
|
2015-03-29 20:35:37 +01:00
|
|
|
|
|
|
|
// the permute type
|
|
|
|
int simd_layout = rhs._grid->_simd_layout[dimension];
|
|
|
|
int comm_dim = rhs._grid->_processors[dimension] >1 ;
|
|
|
|
int splice_dim = rhs._grid->_simd_layout[dimension]>1 && (comm_dim);
|
|
|
|
|
2015-04-03 04:52:53 +01:00
|
|
|
|
|
|
|
if ( !comm_dim ) {
|
2015-11-04 09:59:27 +00:00
|
|
|
// std::cout << "Cshift_local" <<std::endl;
|
2015-04-03 04:52:53 +01:00
|
|
|
Cshift_local(ret,rhs,dimension,shift); // Handles checkerboarding
|
|
|
|
} else if ( splice_dim ) {
|
2015-11-04 09:59:27 +00:00
|
|
|
// std::cout << "Cshift_comms_simd" <<std::endl;
|
2015-04-03 04:52:53 +01:00
|
|
|
Cshift_comms_simd(ret,rhs,dimension,shift);
|
|
|
|
} else {
|
2015-11-04 09:59:27 +00:00
|
|
|
// std::cout << "Cshift_comms" <<std::endl;
|
2015-04-03 04:52:53 +01:00
|
|
|
Cshift_comms(ret,rhs,dimension,shift);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:25:59 +01:00
|
|
|
template<class vobj> void Cshift_comms(Lattice<vobj>& ret,const Lattice<vobj> &rhs,int dimension,int shift)
|
2015-04-03 04:52:53 +01:00
|
|
|
{
|
|
|
|
int sshift[2];
|
|
|
|
|
2015-05-31 15:09:02 +01:00
|
|
|
sshift[0] = rhs._grid->CheckerBoardShiftForCB(rhs.checkerboard,dimension,shift,Even);
|
|
|
|
sshift[1] = rhs._grid->CheckerBoardShiftForCB(rhs.checkerboard,dimension,shift,Odd);
|
2015-04-03 04:52:53 +01:00
|
|
|
|
2015-11-04 09:59:27 +00:00
|
|
|
// std::cout << "Cshift_comms dim "<<dimension<<"cb "<<rhs.checkerboard<<"shift "<<shift<<" sshift " << sshift[0]<<" "<<sshift[1]<<std::endl;
|
2015-04-03 04:52:53 +01:00
|
|
|
if ( sshift[0] == sshift[1] ) {
|
2015-11-04 09:59:27 +00:00
|
|
|
// std::cout << "Single pass Cshift_comms" <<std::endl;
|
2015-04-03 04:52:53 +01:00
|
|
|
Cshift_comms(ret,rhs,dimension,shift,0x3);
|
|
|
|
} else {
|
2015-11-04 09:59:27 +00:00
|
|
|
// std::cout << "Two pass Cshift_comms" <<std::endl;
|
2015-04-03 04:52:53 +01:00
|
|
|
Cshift_comms(ret,rhs,dimension,shift,0x1);// if checkerboard is unfavourable take two passes
|
|
|
|
Cshift_comms(ret,rhs,dimension,shift,0x2);// both with block stride loop iteration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:25:59 +01:00
|
|
|
template<class vobj> void Cshift_comms_simd(Lattice<vobj>& ret,const Lattice<vobj> &rhs,int dimension,int shift)
|
2015-04-03 04:52:53 +01:00
|
|
|
{
|
|
|
|
int sshift[2];
|
|
|
|
|
2015-05-31 15:09:02 +01:00
|
|
|
sshift[0] = rhs._grid->CheckerBoardShiftForCB(rhs.checkerboard,dimension,shift,Even);
|
|
|
|
sshift[1] = rhs._grid->CheckerBoardShiftForCB(rhs.checkerboard,dimension,shift,Odd);
|
2015-04-03 04:52:53 +01:00
|
|
|
|
|
|
|
if ( sshift[0] == sshift[1] ) {
|
|
|
|
Cshift_comms_simd(ret,rhs,dimension,shift,0x3);
|
|
|
|
} else {
|
|
|
|
Cshift_comms_simd(ret,rhs,dimension,shift,0x1);// if checkerboard is unfavourable take two passes
|
|
|
|
Cshift_comms_simd(ret,rhs,dimension,shift,0x2);// both with block stride loop iteration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:25:59 +01:00
|
|
|
template<class vobj> void Cshift_comms(Lattice<vobj> &ret,const Lattice<vobj> &rhs,int dimension,int shift,int cbmask)
|
2015-04-03 04:52:53 +01:00
|
|
|
{
|
|
|
|
typedef typename vobj::vector_type vector_type;
|
|
|
|
typedef typename vobj::scalar_type scalar_type;
|
|
|
|
|
2015-04-06 06:30:48 +01:00
|
|
|
GridBase *grid=rhs._grid;
|
2015-04-03 04:52:53 +01:00
|
|
|
Lattice<vobj> temp(rhs._grid);
|
|
|
|
|
|
|
|
int fd = rhs._grid->_fdimensions[dimension];
|
|
|
|
int rd = rhs._grid->_rdimensions[dimension];
|
2015-04-27 13:45:07 +01:00
|
|
|
int pd = rhs._grid->_processors[dimension];
|
2015-04-03 04:52:53 +01:00
|
|
|
int simd_layout = rhs._grid->_simd_layout[dimension];
|
|
|
|
int comm_dim = rhs._grid->_processors[dimension] >1 ;
|
|
|
|
assert(simd_layout==1);
|
|
|
|
assert(comm_dim==1);
|
|
|
|
assert(shift>=0);
|
|
|
|
assert(shift<fd);
|
|
|
|
|
|
|
|
int buffer_size = rhs._grid->_slice_nblock[dimension]*rhs._grid->_slice_block[dimension];
|
2016-10-20 16:59:16 +01:00
|
|
|
commVector<vobj> send_buf(buffer_size);
|
|
|
|
commVector<vobj> recv_buf(buffer_size);
|
2015-04-03 04:52:53 +01:00
|
|
|
|
2015-05-31 15:09:02 +01:00
|
|
|
int cb= (cbmask==0x2)? Odd : Even;
|
|
|
|
int sshift= rhs._grid->CheckerBoardShiftForCB(rhs.checkerboard,dimension,shift,cb);
|
2015-04-03 04:52:53 +01:00
|
|
|
|
|
|
|
for(int x=0;x<rd;x++){
|
|
|
|
|
2015-04-27 13:45:07 +01:00
|
|
|
int sx = (x+sshift)%rd;
|
|
|
|
int comm_proc = ((x+sshift)/rd)%pd;
|
2015-04-03 04:52:53 +01:00
|
|
|
|
2015-04-27 13:45:07 +01:00
|
|
|
if (comm_proc==0) {
|
2015-04-03 22:54:13 +01:00
|
|
|
|
2015-04-03 04:52:53 +01:00
|
|
|
Copy_plane(ret,rhs,dimension,x,sx,cbmask);
|
2015-04-03 22:54:13 +01:00
|
|
|
|
2015-04-03 04:52:53 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
int words = send_buf.size();
|
|
|
|
if (cbmask != 0x3) words=words>>1;
|
|
|
|
|
|
|
|
int bytes = words * sizeof(vobj);
|
|
|
|
|
|
|
|
Gather_plane_simple (rhs,send_buf,dimension,sx,cbmask);
|
|
|
|
|
|
|
|
int rank = grid->_processor;
|
|
|
|
int recv_from_rank;
|
|
|
|
int xmit_to_rank;
|
|
|
|
grid->ShiftedRanks(dimension,comm_proc,xmit_to_rank,recv_from_rank);
|
|
|
|
|
2015-11-04 09:59:27 +00:00
|
|
|
|
2015-04-03 04:52:53 +01:00
|
|
|
grid->SendToRecvFrom((void *)&send_buf[0],
|
|
|
|
xmit_to_rank,
|
|
|
|
(void *)&recv_buf[0],
|
|
|
|
recv_from_rank,
|
|
|
|
bytes);
|
2017-02-07 06:01:15 +00:00
|
|
|
grid->Barrier();
|
2017-04-20 13:17:55 +01:00
|
|
|
|
2015-04-03 04:52:53 +01:00
|
|
|
Scatter_plane_simple (ret,recv_buf,dimension,x,cbmask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:25:59 +01:00
|
|
|
template<class vobj> void Cshift_comms_simd(Lattice<vobj> &ret,const Lattice<vobj> &rhs,int dimension,int shift,int cbmask)
|
2015-04-03 04:52:53 +01:00
|
|
|
{
|
2015-04-06 06:30:48 +01:00
|
|
|
GridBase *grid=rhs._grid;
|
|
|
|
const int Nsimd = grid->Nsimd();
|
2015-04-03 04:52:53 +01:00
|
|
|
typedef typename vobj::vector_type vector_type;
|
2015-04-28 08:11:59 +01:00
|
|
|
typedef typename vobj::scalar_object scalar_object;
|
2015-04-03 04:52:53 +01:00
|
|
|
typedef typename vobj::scalar_type scalar_type;
|
|
|
|
|
|
|
|
int fd = grid->_fdimensions[dimension];
|
|
|
|
int rd = grid->_rdimensions[dimension];
|
|
|
|
int ld = grid->_ldimensions[dimension];
|
2015-04-27 13:45:07 +01:00
|
|
|
int pd = grid->_processors[dimension];
|
2015-04-03 04:52:53 +01:00
|
|
|
int simd_layout = grid->_simd_layout[dimension];
|
|
|
|
int comm_dim = grid->_processors[dimension] >1 ;
|
|
|
|
|
|
|
|
assert(comm_dim==1);
|
|
|
|
assert(simd_layout==2);
|
|
|
|
assert(shift>=0);
|
|
|
|
assert(shift<fd);
|
|
|
|
|
2015-04-06 11:26:24 +01:00
|
|
|
int permute_type=grid->PermuteType(dimension);
|
2015-03-29 20:35:37 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////
|
|
|
|
// Simd direction uses an extract/merge pair
|
|
|
|
///////////////////////////////////////////////
|
2015-04-03 04:52:53 +01:00
|
|
|
int buffer_size = grid->_slice_nblock[dimension]*grid->_slice_block[dimension];
|
2015-03-29 20:35:37 +01:00
|
|
|
int words = sizeof(vobj)/sizeof(vector_type);
|
|
|
|
|
2016-10-20 16:59:16 +01:00
|
|
|
std::vector<commVector<scalar_object> > send_buf_extract(Nsimd,commVector<scalar_object>(buffer_size) );
|
|
|
|
std::vector<commVector<scalar_object> > recv_buf_extract(Nsimd,commVector<scalar_object>(buffer_size) );
|
2016-02-14 20:24:38 +00:00
|
|
|
|
2015-04-28 08:11:59 +01:00
|
|
|
int bytes = buffer_size*sizeof(scalar_object);
|
2015-03-29 20:35:37 +01:00
|
|
|
|
2015-12-30 19:29:48 +00:00
|
|
|
std::vector<scalar_object *> pointers(Nsimd); //
|
2015-04-28 08:11:59 +01:00
|
|
|
std::vector<scalar_object *> rpointers(Nsimd); // received pointers
|
2015-03-29 20:35:37 +01:00
|
|
|
|
2015-04-03 04:52:53 +01:00
|
|
|
///////////////////////////////////////////
|
|
|
|
// Work out what to send where
|
|
|
|
///////////////////////////////////////////
|
2015-05-31 15:09:02 +01:00
|
|
|
int cb = (cbmask==0x2)? Odd : Even;
|
|
|
|
int sshift= grid->CheckerBoardShiftForCB(rhs.checkerboard,dimension,shift,cb);
|
2015-04-03 04:52:53 +01:00
|
|
|
|
2015-04-27 13:45:07 +01:00
|
|
|
// loop over outer coord planes orthog to dim
|
2015-04-03 04:52:53 +01:00
|
|
|
for(int x=0;x<rd;x++){
|
|
|
|
|
2015-04-28 08:11:59 +01:00
|
|
|
// FIXME call local permute copy if none are offnode.
|
2015-04-27 13:45:07 +01:00
|
|
|
for(int i=0;i<Nsimd;i++){
|
2015-04-28 08:11:59 +01:00
|
|
|
pointers[i] = &send_buf_extract[i][0];
|
2015-04-03 04:52:53 +01:00
|
|
|
}
|
|
|
|
int sx = (x+sshift)%rd;
|
2015-04-27 13:45:07 +01:00
|
|
|
Gather_plane_extract(rhs,pointers,dimension,sx,cbmask);
|
2015-03-29 20:35:37 +01:00
|
|
|
|
2015-04-27 13:45:07 +01:00
|
|
|
for(int i=0;i<Nsimd;i++){
|
|
|
|
|
|
|
|
int inner_bit = (Nsimd>>(permute_type+1));
|
|
|
|
int ic= (i&inner_bit)? 1:0;
|
2015-03-29 20:35:37 +01:00
|
|
|
|
2015-04-27 13:45:07 +01:00
|
|
|
int my_coor = rd*ic + x;
|
|
|
|
int nbr_coor = my_coor+sshift;
|
|
|
|
int nbr_proc = ((nbr_coor)/ld) % pd;// relative shift in processors
|
2015-03-29 20:35:37 +01:00
|
|
|
|
2015-04-27 13:45:07 +01:00
|
|
|
int nbr_ic = (nbr_coor%ld)/rd; // inner coord of peer
|
|
|
|
int nbr_ox = (nbr_coor%rd); // outer coord of peer
|
|
|
|
int nbr_lane = (i&(~inner_bit));
|
2015-03-29 20:35:37 +01:00
|
|
|
|
2015-04-27 13:45:07 +01:00
|
|
|
int recv_from_rank;
|
|
|
|
int xmit_to_rank;
|
2015-03-29 20:35:37 +01:00
|
|
|
|
2015-04-27 13:45:07 +01:00
|
|
|
if (nbr_ic) nbr_lane|=inner_bit;
|
2015-03-29 20:35:37 +01:00
|
|
|
|
2015-04-27 13:45:07 +01:00
|
|
|
assert (sx == nbr_ox);
|
2015-03-29 20:35:37 +01:00
|
|
|
|
2015-04-27 13:45:07 +01:00
|
|
|
if(nbr_proc){
|
|
|
|
grid->ShiftedRanks(dimension,nbr_proc,xmit_to_rank,recv_from_rank);
|
2015-04-03 04:52:53 +01:00
|
|
|
|
2015-04-27 13:45:07 +01:00
|
|
|
grid->SendToRecvFrom((void *)&send_buf_extract[nbr_lane][0],
|
|
|
|
xmit_to_rank,
|
|
|
|
(void *)&recv_buf_extract[i][0],
|
|
|
|
recv_from_rank,
|
|
|
|
bytes);
|
2017-02-07 06:01:15 +00:00
|
|
|
grid->Barrier();
|
2015-04-28 08:11:59 +01:00
|
|
|
rpointers[i] = &recv_buf_extract[i][0];
|
2015-04-27 13:45:07 +01:00
|
|
|
} else {
|
2015-04-28 08:11:59 +01:00
|
|
|
rpointers[i] = &send_buf_extract[nbr_lane][0];
|
2015-03-29 20:35:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-04-27 13:45:07 +01:00
|
|
|
Scatter_plane_merge(ret,rpointers,dimension,x,cbmask);
|
2015-03-29 20:35:37 +01:00
|
|
|
}
|
2015-04-27 13:45:07 +01:00
|
|
|
|
|
|
|
}
|
2015-04-18 20:44:19 +01:00
|
|
|
}
|
2015-03-29 20:35:37 +01:00
|
|
|
#endif
|