2016-01-02 14:51:32 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./lib/cartesian/Cartesian_red_black.h
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
|
|
|
|
|
|
|
Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
|
|
|
|
Author: Peter Boyle <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_CARTESIAN_RED_BLACK_H
|
|
|
|
#define GRID_CARTESIAN_RED_BLACK_H
|
|
|
|
|
|
|
|
|
|
|
|
namespace Grid {
|
|
|
|
|
2016-07-08 17:16:08 +01:00
|
|
|
static const int CbRed =0;
|
|
|
|
static const int CbBlack=1;
|
|
|
|
static const int Even =CbRed;
|
|
|
|
static const int Odd =CbBlack;
|
|
|
|
|
2015-04-18 18:36:48 +01:00
|
|
|
// Specialise this for red black grids storing half the data like a chess board.
|
|
|
|
class GridRedBlackCartesian : public GridBase
|
|
|
|
{
|
|
|
|
public:
|
2015-05-31 15:09:02 +01:00
|
|
|
std::vector<int> _checker_dim_mask;
|
|
|
|
int _checker_dim;
|
2016-10-11 22:21:07 +01:00
|
|
|
std::vector<int> _checker_board;
|
2015-05-31 15:09:02 +01:00
|
|
|
|
2015-04-18 18:36:48 +01:00
|
|
|
virtual int CheckerBoarded(int dim){
|
2015-05-31 15:09:02 +01:00
|
|
|
if( dim==_checker_dim) return 1;
|
2015-04-18 18:36:48 +01:00
|
|
|
else return 0;
|
|
|
|
}
|
2017-02-20 21:49:06 +00:00
|
|
|
virtual int CheckerBoard(const std::vector<int> &site){
|
2015-05-31 15:09:02 +01:00
|
|
|
int linear=0;
|
|
|
|
assert(site.size()==_ndimension);
|
|
|
|
for(int d=0;d<_ndimension;d++){
|
|
|
|
if(_checker_dim_mask[d])
|
|
|
|
linear=linear+site[d];
|
|
|
|
}
|
|
|
|
return (linear&0x1);
|
2015-04-18 18:36:48 +01:00
|
|
|
}
|
|
|
|
|
2015-05-31 15:09:02 +01:00
|
|
|
|
2015-04-18 18:36:48 +01:00
|
|
|
// Depending on the cb of site, we toggle source cb.
|
|
|
|
// for block #b, element #e = (b, e)
|
|
|
|
// we need
|
2015-05-31 15:09:02 +01:00
|
|
|
virtual int CheckerBoardShiftForCB(int source_cb,int dim,int shift,int ocb){
|
|
|
|
if(dim != _checker_dim) return shift;
|
2015-04-18 18:36:48 +01:00
|
|
|
|
2015-05-31 15:09:02 +01:00
|
|
|
int fulldim =_fdimensions[dim];
|
2015-04-18 18:36:48 +01:00
|
|
|
shift = (shift+fulldim)%fulldim;
|
|
|
|
|
|
|
|
// Probably faster with table lookup;
|
|
|
|
// or by looping over x,y,z and multiply rather than computing checkerboard.
|
|
|
|
|
|
|
|
if ( (source_cb+ocb)&1 ) {
|
|
|
|
return (shift)/2;
|
|
|
|
} else {
|
|
|
|
return (shift+1)/2;
|
|
|
|
}
|
|
|
|
}
|
2016-10-11 22:21:07 +01:00
|
|
|
virtual int CheckerBoardFromOindexTable (int Oindex) {
|
|
|
|
return _checker_board[Oindex];
|
|
|
|
}
|
|
|
|
virtual int CheckerBoardFromOindex (int Oindex)
|
|
|
|
{
|
|
|
|
std::vector<int> ocoor;
|
|
|
|
oCoorFromOindex(ocoor,Oindex);
|
|
|
|
return CheckerBoard(ocoor);
|
|
|
|
}
|
2015-05-31 15:09:02 +01:00
|
|
|
virtual int CheckerBoardShift(int source_cb,int dim,int shift,int osite){
|
|
|
|
|
|
|
|
if(dim != _checker_dim) return shift;
|
2015-04-18 18:36:48 +01:00
|
|
|
|
2015-05-31 15:09:02 +01:00
|
|
|
int ocb=CheckerBoardFromOindex(osite);
|
|
|
|
|
|
|
|
return CheckerBoardShiftForCB(source_cb,dim,shift,ocb);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int CheckerBoardDestination(int source_cb,int shift,int dim){
|
|
|
|
if ( _checker_dim_mask[dim] ) {
|
|
|
|
// If _fdimensions[checker_dim] is odd, then shifting by 1 in other dims
|
|
|
|
// does NOT cause a parity hop.
|
|
|
|
int add=(dim==_checker_dim) ? 0 : _fdimensions[_checker_dim];
|
|
|
|
if ( (shift+add) &0x1) {
|
2015-04-18 18:36:48 +01:00
|
|
|
return 1-source_cb;
|
|
|
|
} else {
|
|
|
|
return source_cb;
|
|
|
|
}
|
2015-05-31 15:09:02 +01:00
|
|
|
} else {
|
|
|
|
return source_cb;
|
|
|
|
|
|
|
|
}
|
2015-04-18 18:36:48 +01:00
|
|
|
};
|
2015-05-25 13:44:35 +01:00
|
|
|
|
2017-06-22 08:14:34 +01:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Create Redblack from original grid; require full grid pointer ?
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
GridRedBlackCartesian(const GridBase *base) : GridBase(base->_processors,*base)
|
|
|
|
{
|
|
|
|
int dims = base->_ndimension;
|
|
|
|
std::vector<int> checker_dim_mask(dims,1);
|
|
|
|
int checker_dim = 0;
|
|
|
|
Init(base->_fdimensions,base->_simd_layout,base->_processors,checker_dim_mask,checker_dim);
|
|
|
|
};
|
2015-05-25 13:44:35 +01:00
|
|
|
|
2017-06-22 08:14:34 +01:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Create redblack from original grid, with non-trivial checker dim mask
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
GridRedBlackCartesian(const GridBase *base,
|
|
|
|
const std::vector<int> &checker_dim_mask,
|
|
|
|
int checker_dim
|
|
|
|
) : GridBase(base->_processors,*base)
|
|
|
|
{
|
|
|
|
Init(base->_fdimensions,base->_simd_layout,base->_processors,checker_dim_mask,checker_dim) ;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Create redblack grid ;; deprecate these. Should not
|
|
|
|
// need direct creation of redblack without a full grid to base on
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
GridRedBlackCartesian(const GridBase *base,
|
|
|
|
const std::vector<int> &dimensions,
|
2015-06-02 16:57:12 +01:00
|
|
|
const std::vector<int> &simd_layout,
|
|
|
|
const std::vector<int> &processor_grid,
|
|
|
|
const std::vector<int> &checker_dim_mask,
|
2015-05-31 15:09:02 +01:00
|
|
|
int checker_dim
|
2017-06-22 08:14:34 +01:00
|
|
|
) : GridBase(processor_grid,*base)
|
2015-05-31 15:09:02 +01:00
|
|
|
{
|
|
|
|
Init(dimensions,simd_layout,processor_grid,checker_dim_mask,checker_dim);
|
|
|
|
}
|
2017-06-22 08:14:34 +01:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Create redblack grid
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
GridRedBlackCartesian(const GridBase *base,
|
|
|
|
const std::vector<int> &dimensions,
|
2015-06-02 16:57:12 +01:00
|
|
|
const std::vector<int> &simd_layout,
|
2017-06-22 08:14:34 +01:00
|
|
|
const std::vector<int> &processor_grid) : GridBase(processor_grid,*base)
|
2015-05-31 15:09:02 +01:00
|
|
|
{
|
|
|
|
std::vector<int> checker_dim_mask(dimensions.size(),1);
|
2017-06-22 08:14:34 +01:00
|
|
|
int checker_dim = 0;
|
|
|
|
Init(dimensions,simd_layout,processor_grid,checker_dim_mask,checker_dim);
|
2015-05-31 15:09:02 +01:00
|
|
|
}
|
2017-06-22 08:14:34 +01:00
|
|
|
#endif
|
|
|
|
|
2015-06-02 16:57:12 +01:00
|
|
|
void Init(const std::vector<int> &dimensions,
|
2017-08-08 11:36:20 +01:00
|
|
|
const std::vector<int> &simd_layout,
|
|
|
|
const std::vector<int> &processor_grid,
|
|
|
|
const std::vector<int> &checker_dim_mask,
|
|
|
|
int checker_dim)
|
2015-04-18 18:36:48 +01:00
|
|
|
{
|
2017-10-24 16:51:14 +01:00
|
|
|
|
2017-08-04 16:08:07 +01:00
|
|
|
_isCheckerBoarded = true;
|
2015-05-31 15:09:02 +01:00
|
|
|
_checker_dim = checker_dim;
|
2017-08-08 11:36:20 +01:00
|
|
|
assert(checker_dim_mask[checker_dim] == 1);
|
2015-05-31 15:09:02 +01:00
|
|
|
_ndimension = dimensions.size();
|
2017-08-08 11:36:20 +01:00
|
|
|
assert(checker_dim_mask.size() == _ndimension);
|
|
|
|
assert(processor_grid.size() == _ndimension);
|
|
|
|
assert(simd_layout.size() == _ndimension);
|
|
|
|
|
2015-05-31 15:09:02 +01:00
|
|
|
_fdimensions.resize(_ndimension);
|
|
|
|
_gdimensions.resize(_ndimension);
|
|
|
|
_ldimensions.resize(_ndimension);
|
|
|
|
_rdimensions.resize(_ndimension);
|
|
|
|
_simd_layout.resize(_ndimension);
|
2017-06-11 23:14:10 +01:00
|
|
|
_lstart.resize(_ndimension);
|
|
|
|
_lend.resize(_ndimension);
|
2017-08-08 11:36:20 +01:00
|
|
|
|
2015-05-31 15:09:02 +01:00
|
|
|
_ostride.resize(_ndimension);
|
|
|
|
_istride.resize(_ndimension);
|
2017-08-08 11:36:20 +01:00
|
|
|
|
2015-05-31 15:09:02 +01:00
|
|
|
_fsites = _gsites = _osites = _isites = 1;
|
2016-10-11 22:21:07 +01:00
|
|
|
|
2017-08-08 11:36:20 +01:00
|
|
|
_checker_dim_mask = checker_dim_mask;
|
2016-10-11 22:21:07 +01:00
|
|
|
|
2017-08-08 11:36:20 +01:00
|
|
|
for (int d = 0; d < _ndimension; d++)
|
|
|
|
{
|
|
|
|
_fdimensions[d] = dimensions[d];
|
|
|
|
_gdimensions[d] = _fdimensions[d];
|
|
|
|
_fsites = _fsites * _fdimensions[d];
|
|
|
|
_gsites = _gsites * _gdimensions[d];
|
|
|
|
|
|
|
|
if (d == _checker_dim)
|
|
|
|
{
|
|
|
|
assert((_gdimensions[d] & 0x1) == 0);
|
|
|
|
_gdimensions[d] = _gdimensions[d] / 2; // Remove a checkerboard
|
|
|
|
}
|
|
|
|
_ldimensions[d] = _gdimensions[d] / _processors[d];
|
|
|
|
assert(_ldimensions[d] * _processors[d] == _gdimensions[d]);
|
|
|
|
_lstart[d] = _processor_coor[d] * _ldimensions[d];
|
|
|
|
_lend[d] = _processor_coor[d] * _ldimensions[d] + _ldimensions[d] - 1;
|
|
|
|
|
|
|
|
// Use a reduced simd grid
|
|
|
|
_simd_layout[d] = simd_layout[d];
|
|
|
|
_rdimensions[d] = _ldimensions[d] / _simd_layout[d]; // this is not checking if this is integer
|
|
|
|
assert(_rdimensions[d] * _simd_layout[d] == _ldimensions[d]);
|
|
|
|
assert(_rdimensions[d] > 0);
|
|
|
|
|
|
|
|
// all elements of a simd vector must have same checkerboard.
|
|
|
|
// If Ls vectorised, this must still be the case; e.g. dwf rb5d
|
|
|
|
if (_simd_layout[d] > 1)
|
|
|
|
{
|
|
|
|
if (checker_dim_mask[d])
|
|
|
|
{
|
|
|
|
assert((_rdimensions[d] & 0x1) == 0);
|
|
|
|
}
|
|
|
|
}
|
2016-10-11 22:21:07 +01:00
|
|
|
|
2017-08-08 11:36:20 +01:00
|
|
|
_osites *= _rdimensions[d];
|
|
|
|
_isites *= _simd_layout[d];
|
|
|
|
|
|
|
|
// Addressing support
|
|
|
|
if (d == 0)
|
|
|
|
{
|
|
|
|
_ostride[d] = 1;
|
|
|
|
_istride[d] = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_ostride[d] = _ostride[d - 1] * _rdimensions[d - 1];
|
|
|
|
_istride[d] = _istride[d - 1] * _simd_layout[d - 1];
|
|
|
|
}
|
2015-05-31 15:09:02 +01:00
|
|
|
}
|
2017-08-08 11:36:20 +01:00
|
|
|
|
2015-05-31 15:09:02 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// subplane information
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
_slice_block.resize(_ndimension);
|
|
|
|
_slice_stride.resize(_ndimension);
|
|
|
|
_slice_nblock.resize(_ndimension);
|
2017-08-08 11:36:20 +01:00
|
|
|
|
|
|
|
int block = 1;
|
|
|
|
int nblock = 1;
|
|
|
|
for (int d = 0; d < _ndimension; d++)
|
|
|
|
nblock *= _rdimensions[d];
|
|
|
|
|
|
|
|
for (int d = 0; d < _ndimension; d++)
|
|
|
|
{
|
|
|
|
nblock /= _rdimensions[d];
|
|
|
|
_slice_block[d] = block;
|
|
|
|
_slice_stride[d] = _ostride[d] * _rdimensions[d];
|
|
|
|
_slice_nblock[d] = nblock;
|
|
|
|
block = block * _rdimensions[d];
|
2015-05-31 15:09:02 +01:00
|
|
|
}
|
2016-10-11 22:21:07 +01:00
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
// Create a checkerboard lookup table
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
int rvol = 1;
|
2017-08-08 11:36:20 +01:00
|
|
|
for (int d = 0; d < _ndimension; d++)
|
|
|
|
{
|
|
|
|
rvol = rvol * _rdimensions[d];
|
2016-10-11 22:21:07 +01:00
|
|
|
}
|
|
|
|
_checker_board.resize(rvol);
|
2017-08-08 11:36:20 +01:00
|
|
|
for (int osite = 0; osite < _osites; osite++)
|
|
|
|
{
|
|
|
|
_checker_board[osite] = CheckerBoardFromOindex(osite);
|
2016-10-11 22:21:07 +01:00
|
|
|
}
|
2015-04-18 18:36:48 +01:00
|
|
|
};
|
2017-08-08 11:36:20 +01:00
|
|
|
|
|
|
|
protected:
|
2015-04-18 18:36:48 +01:00
|
|
|
virtual int oIndex(std::vector<int> &coor)
|
|
|
|
{
|
2017-08-08 11:36:20 +01:00
|
|
|
int idx = 0;
|
|
|
|
for (int d = 0; d < _ndimension; d++)
|
|
|
|
{
|
|
|
|
if (d == _checker_dim)
|
|
|
|
{
|
|
|
|
idx += _ostride[d] * ((coor[d] / 2) % _rdimensions[d]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
idx += _ostride[d] * (coor[d] % _rdimensions[d]);
|
|
|
|
}
|
2015-05-31 15:09:02 +01:00
|
|
|
}
|
2016-07-11 22:15:22 +01:00
|
|
|
return idx;
|
2015-04-18 18:36:48 +01:00
|
|
|
};
|
2017-08-08 11:36:20 +01:00
|
|
|
|
2016-07-14 23:44:48 +01:00
|
|
|
virtual int iIndex(std::vector<int> &lcoor)
|
|
|
|
{
|
2017-08-08 11:36:20 +01:00
|
|
|
int idx = 0;
|
|
|
|
for (int d = 0; d < _ndimension; d++)
|
|
|
|
{
|
|
|
|
if (d == _checker_dim)
|
|
|
|
{
|
|
|
|
idx += _istride[d] * (lcoor[d] / (2 * _rdimensions[d]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
idx += _istride[d] * (lcoor[d] / _rdimensions[d]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx;
|
2016-07-14 23:44:48 +01:00
|
|
|
}
|
2015-04-18 18:36:48 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|