1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-09-20 17:25:37 +01:00
Grid/lib/lattice/Lattice_base.h

342 lines
9.1 KiB
C
Raw Normal View History

/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/lattice/Lattice_base.h
Copyright (C) 2015
Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
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
*************************************************************************************/
2018-01-15 00:03:49 +00:00
/* END LEGAL */
2018-01-24 13:35:13 +00:00
#pragma once
2015-05-11 19:09:49 +01:00
2015-05-21 06:47:05 +01:00
#define STREAMING_STORES
2018-01-15 00:03:49 +00:00
NAMESPACE_BEGIN(Grid);
2015-05-11 19:09:49 +01:00
// TODO:
// mac,real,imag
// Functionality:
// -=,+=,*=,()
// add,+,sub,-,mult,mac,*
// adj,conjugate
2015-05-11 19:09:49 +01:00
// real,imag
// transpose,transposeIndex
// trace,traceIndex
// peekIndex
// innerProduct,outerProduct,
// localNorm2
// localInnerProduct
extern int GridCshiftPermuteMap[4][16];
////////////////////////////////////////////////
// Basic expressions used in Expression Template
////////////////////////////////////////////////
class LatticeBase
{
public:
2018-01-15 00:03:49 +00:00
virtual ~LatticeBase(void) = default;
GridBase *_grid;
};
2015-05-11 19:09:49 +01:00
class LatticeExpressionBase {};
template <typename Op, typename T1>
class LatticeUnaryExpression : public std::pair<Op,std::tuple<T1> > , public LatticeExpressionBase {
2018-01-15 00:03:49 +00:00
public:
LatticeUnaryExpression(const std::pair<Op,std::tuple<T1> > &arg): std::pair<Op,std::tuple<T1> >(arg) {};
2015-05-11 19:09:49 +01:00
};
template <typename Op, typename T1, typename T2>
class LatticeBinaryExpression : public std::pair<Op,std::tuple<T1,T2> > , public LatticeExpressionBase {
2018-01-15 00:03:49 +00:00
public:
LatticeBinaryExpression(const std::pair<Op,std::tuple<T1,T2> > &arg): std::pair<Op,std::tuple<T1,T2> >(arg) {};
2015-05-11 19:09:49 +01:00
};
template <typename Op, typename T1, typename T2, typename T3>
class LatticeTrinaryExpression :public std::pair<Op,std::tuple<T1,T2,T3> >, public LatticeExpressionBase {
2018-01-15 00:03:49 +00:00
public:
LatticeTrinaryExpression(const std::pair<Op,std::tuple<T1,T2,T3> > &arg): std::pair<Op,std::tuple<T1,T2,T3> >(arg) {};
2015-05-11 19:09:49 +01:00
};
void inline conformable(GridBase *lhs,GridBase *rhs)
{
assert(lhs == rhs);
}
2015-05-11 19:09:49 +01:00
template<class vobj>
class Lattice : public LatticeBase
{
2018-01-24 13:35:13 +00:00
private:
// vobj * _odata_p;
// uint64_t _odata_size;
2015-05-11 19:09:49 +01:00
public:
2018-01-15 00:03:49 +00:00
int checkerboard;
Vector<vobj> _odata;
2018-01-24 13:35:13 +00:00
2018-01-15 00:03:49 +00:00
// to pthread need a computable loop where loop induction is not required
2018-01-24 13:35:13 +00:00
accelerator int begin(void) const { return 0;};
accelerator int end(void) const { return _odata.size(); }
accelerator_inline vobj & operator[](int i) { return _odata[i]; };
accelerator_inline const vobj & operator[](int i) const { return _odata[i]; };
2015-05-11 19:09:49 +01:00
public:
2018-01-15 00:03:49 +00:00
typedef typename vobj::scalar_type scalar_type;
typedef typename vobj::vector_type vector_type;
typedef vobj vector_object;
2018-01-24 13:35:13 +00:00
void resize(uint64_t size)
{
_odata.resize(size);
// _odata_p = &_odata[0];
// _odata_size = size;
}
2015-05-11 19:09:49 +01:00
////////////////////////////////////////////////////////////////////////////////
// Expression Template closure support
////////////////////////////////////////////////////////////////////////////////
2018-01-24 13:35:13 +00:00
template <typename Op, typename T1> inline Lattice<vobj> & operator=(const LatticeUnaryExpression<Op,T1> &expr)
2015-05-11 19:09:49 +01:00
{
2015-05-25 13:45:08 +01:00
GridBase *egrid(nullptr);
GridFromExpression(egrid,expr);
assert(egrid!=nullptr);
conformable(_grid,egrid);
int cb=-1;
CBFromExpression(cb,expr);
assert( (cb==Odd) || (cb==Even));
checkerboard=cb;
2015-05-21 06:47:05 +01:00
#ifdef STREAMING_STORES
2018-01-24 13:35:13 +00:00
accelerator_loop(ss,(*this),{
2015-05-21 06:47:05 +01:00
vobj tmp = eval(ss,expr);
2015-05-11 19:09:49 +01:00
vstream(_odata[ss] ,tmp);
2018-01-24 13:35:13 +00:00
});
2015-05-21 06:47:05 +01:00
#else
2018-01-24 13:35:13 +00:00
accelerator_loop(ss,(*this),{
2015-05-21 06:47:05 +01:00
_odata[ss]=eval(ss,expr);
2018-01-24 13:35:13 +00:00
});
2015-05-21 06:47:05 +01:00
#endif
2015-05-11 19:09:49 +01:00
return *this;
}
2018-01-24 13:35:13 +00:00
template <typename Op, typename T1,typename T2> inline Lattice<vobj> & operator=(const LatticeBinaryExpression<Op,T1,T2> &expr)
2015-05-11 19:09:49 +01:00
{
2015-05-25 13:45:08 +01:00
GridBase *egrid(nullptr);
GridFromExpression(egrid,expr);
assert(egrid!=nullptr);
conformable(_grid,egrid);
int cb=-1;
CBFromExpression(cb,expr);
assert( (cb==Odd) || (cb==Even));
checkerboard=cb;
2015-05-21 06:47:05 +01:00
#ifdef STREAMING_STORES
2018-01-24 13:35:13 +00:00
accelerator_loop(ss,(*this),{
2015-05-21 06:47:05 +01:00
vobj tmp = eval(ss,expr);
2015-05-11 19:09:49 +01:00
vstream(_odata[ss] ,tmp);
2018-01-24 13:35:13 +00:00
});
2015-05-21 06:47:05 +01:00
#else
2018-01-24 13:35:13 +00:00
accelerator_loop(ss,(*this),{
2015-05-21 06:47:05 +01:00
_odata[ss]=eval(ss,expr);
2018-01-24 13:35:13 +00:00
});
2015-05-21 06:47:05 +01:00
#endif
2015-05-11 19:09:49 +01:00
return *this;
}
2018-01-24 13:35:13 +00:00
template <typename Op, typename T1,typename T2,typename T3> inline Lattice<vobj> & operator=(const LatticeTrinaryExpression<Op,T1,T2,T3> &expr)
2015-05-11 19:09:49 +01:00
{
2015-05-25 13:45:08 +01:00
GridBase *egrid(nullptr);
GridFromExpression(egrid,expr);
assert(egrid!=nullptr);
conformable(_grid,egrid);
int cb=-1;
CBFromExpression(cb,expr);
assert( (cb==Odd) || (cb==Even));
checkerboard=cb;
2015-05-21 06:47:05 +01:00
#ifdef STREAMING_STORES
2018-01-24 13:35:13 +00:00
accelerator_loop(ss,(*this),{
2015-06-14 01:06:56 +01:00
//vobj tmp = eval(ss,expr);
vstream(_odata[ss] ,eval(ss,expr));
2018-01-24 13:35:13 +00:00
});
2015-05-21 06:47:05 +01:00
#else
2018-01-24 13:35:13 +00:00
accelerator_loop(ss,(*this),{
2015-05-21 06:47:05 +01:00
_odata[ss] = eval(ss,expr);
2018-01-24 13:35:13 +00:00
});
2015-05-21 06:47:05 +01:00
#endif
2015-05-11 19:09:49 +01:00
return *this;
}
//GridFromExpression is tricky to do
template<class Op,class T1>
2018-01-15 00:03:49 +00:00
Lattice(const LatticeUnaryExpression<Op,T1> & expr) {
_grid = nullptr;
2015-05-11 19:09:49 +01:00
GridFromExpression(_grid,expr);
assert(_grid!=nullptr);
2015-05-25 13:45:08 +01:00
int cb=-1;
CBFromExpression(cb,expr);
assert( (cb==Odd) || (cb==Even));
checkerboard=cb;
2018-01-24 13:35:13 +00:00
resize(_grid->oSites());
2015-05-21 06:47:05 +01:00
#ifdef STREAMING_STORES
2018-01-24 13:35:13 +00:00
cpu_loop(ss,(*this),{
2015-05-21 06:47:05 +01:00
vobj tmp = eval(ss,expr);
vstream(_odata[ss] ,tmp);
2018-01-24 13:35:13 +00:00
});
2015-05-21 06:47:05 +01:00
#else
2018-01-24 13:35:13 +00:00
cpu_loop(ss,(*this),{
2015-06-16 14:06:31 +01:00
_odata[ss]=eval(ss,expr);
2018-01-24 13:35:13 +00:00
});
2015-05-21 06:47:05 +01:00
#endif
2018-01-24 13:35:13 +00:00
}
2015-05-11 19:09:49 +01:00
template<class Op,class T1, class T2>
Lattice(const LatticeBinaryExpression<Op,T1,T2> & expr) {
_grid = nullptr;
2015-05-11 19:09:49 +01:00
GridFromExpression(_grid,expr);
assert(_grid!=nullptr);
2015-05-25 13:45:08 +01:00
int cb=-1;
CBFromExpression(cb,expr);
assert( (cb==Odd) || (cb==Even));
checkerboard=cb;
2018-01-24 13:35:13 +00:00
resize(_grid->oSites());
2015-05-25 13:45:08 +01:00
2015-05-21 06:47:05 +01:00
#ifdef STREAMING_STORES
2018-01-24 13:35:13 +00:00
cpu_loop(ss,(*this),{
2015-06-01 12:26:20 +01:00
vobj tmp = eval(ss,expr);
2015-05-21 06:47:05 +01:00
vstream(_odata[ss] ,tmp);
2018-01-24 13:35:13 +00:00
});
2015-05-21 06:47:05 +01:00
#else
2018-01-24 13:35:13 +00:00
cpu_loop(ss,(*this),{
2015-05-21 06:47:05 +01:00
_odata[ss]=eval(ss,expr);
2018-01-24 13:35:13 +00:00
});
2015-05-21 06:47:05 +01:00
#endif
2018-01-24 13:35:13 +00:00
}
2015-05-11 19:09:49 +01:00
template<class Op,class T1, class T2, class T3>
Lattice(const LatticeTrinaryExpression<Op,T1,T2,T3> & expr) {
_grid = nullptr;
2015-05-11 19:09:49 +01:00
GridFromExpression(_grid,expr);
assert(_grid!=nullptr);
2015-05-25 13:45:08 +01:00
int cb=-1;
CBFromExpression(cb,expr);
assert( (cb==Odd) || (cb==Even));
checkerboard=cb;
2018-01-24 13:35:13 +00:00
resize(_grid->oSites());
cpu_loop(ss,(*this),{
2015-06-16 14:06:31 +01:00
vstream(_odata[ss] ,eval(ss,expr));
2018-01-24 13:35:13 +00:00
});
}
2015-05-11 19:09:49 +01:00
//////////////////////////////////////////////////////////////////
// Constructor requires "grid" passed.
// what about a default grid?
//////////////////////////////////////////////////////////////////
2018-01-24 13:35:13 +00:00
Lattice(GridBase *grid) {
_grid = grid;
2018-01-24 13:35:13 +00:00
resize(grid->oSites());
assert((((uint64_t)&_odata[0])&0xF) ==0);
checkerboard=0;
}
Lattice(const Lattice& r){ // copy constructor
_grid = r._grid;
checkerboard = r.checkerboard;
2018-01-24 13:35:13 +00:00
resize(_grid->oSites());// essential
cpu_loop(ss,(*this),{
_odata[ss]=r._odata[ss];
2018-01-24 13:35:13 +00:00
});
}
virtual ~Lattice(void) = default;
void reset(GridBase* grid) {
if (_grid != grid) {
_grid = grid;
2018-01-24 13:35:13 +00:00
resize(grid->oSites());
checkerboard = 0;
2015-05-11 19:09:49 +01:00
}
}
2015-05-11 19:09:49 +01:00
2018-01-24 13:35:13 +00:00
template<class sobj> inline Lattice<vobj> & operator = (const sobj & r){
accelerator_loop(ss,(*this),{
this->_odata[ss]=r;
2018-01-24 13:35:13 +00:00
});
return *this;
}
2018-01-24 13:35:13 +00:00
template<class robj> inline Lattice<vobj> & operator = (const Lattice<robj> & r){
this->checkerboard = r.checkerboard;
conformable(*this,r);
2018-01-24 13:35:13 +00:00
accelerator_loop(ss,(*this),{
this->_odata[ss]=r._odata[ss];
2018-01-24 13:35:13 +00:00
});
return *this;
}
// *=,+=,-= operators inherit behvour from correspond */+/- operation
2018-01-24 13:35:13 +00:00
template<class T> inline Lattice<vobj> &operator *=(const T &r) {
*this = (*this)*r;
return *this;
}
2018-01-24 13:35:13 +00:00
template<class T> inline Lattice<vobj> &operator -=(const T &r) {
*this = (*this)-r;
return *this;
}
2018-01-24 13:35:13 +00:00
template<class T> inline Lattice<vobj> &operator +=(const T &r) {
*this = (*this)+r;
return *this;
}
}; // class Lattice
2018-01-15 00:03:49 +00:00
template<class vobj> std::ostream& operator<< (std::ostream& stream, const Lattice<vobj> &o){
std::vector<int> gcoor;
typedef typename vobj::scalar_object sobj;
sobj ss;
for(int g=0;g<o._grid->_gsites;g++){
o._grid->GlobalIndexToGlobalCoor(g,gcoor);
peekSite(ss,o,gcoor);
stream<<"[";
for(int d=0;d<gcoor.size();d++){
stream<<gcoor[d];
if(d!=gcoor.size()-1) stream<<",";
2015-05-13 09:24:10 +01:00
}
2018-01-15 00:03:49 +00:00
stream<<"]\t";
stream<<ss<<std::endl;
2015-05-13 09:24:10 +01:00
}
2018-01-15 00:03:49 +00:00
return stream;
2015-05-11 19:09:49 +01:00
}
2018-01-15 00:03:49 +00:00
NAMESPACE_END(Grid);
2015-05-11 19:09:49 +01:00