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

204 lines
6.3 KiB
C
Raw Normal View History

2015-03-04 05:31:44 +00:00
#ifndef GRID_LATTICE_H
#define GRID_LATTICE_H
2015-04-03 05:29:54 +01:00
namespace Grid {
2015-03-04 03:12:19 +00:00
2015-04-18 20:44:19 +01:00
// TODO:
2015-04-18 14:36:01 +01:00
// mac,real,imag
2015-04-18 20:44:19 +01:00
// Functionality:
2015-04-18 14:36:01 +01:00
// -=,+=,*=,()
// add,+,sub,-,mult,mac,*
// adj,conj
// real,imag
// transpose,transposeIndex
// trace,traceIndex
// peekIndex
// innerProduct,outerProduct,
// localNorm2
// localInnerProduct
extern int GridCshiftPermuteMap[4][16];
////////////////////////////////////////////////
// Basic expressions used in Expression Template
////////////////////////////////////////////////
class LatticeBase {};
class LatticeExpressionBase {};
template <typename Op, typename T1>
class LatticeUnaryExpression : public std::pair<Op,std::tuple<T1> > , public LatticeExpressionBase {
public:
LatticeUnaryExpression(const std::pair<Op,std::tuple<T1> > &arg): std::pair<Op,std::tuple<T1> >(arg) {};
};
template <typename Op, typename T1, typename T2>
class LatticeBinaryExpression : public std::pair<Op,std::tuple<T1,T2> > , public LatticeExpressionBase {
public:
LatticeBinaryExpression(const std::pair<Op,std::tuple<T1,T2> > &arg): std::pair<Op,std::tuple<T1,T2> >(arg) {};
};
template <typename Op, typename T1, typename T2, typename T3>
class LatticeTrinaryExpression :public std::pair<Op,std::tuple<T1,T2,T3> >, public LatticeExpressionBase {
public:
LatticeTrinaryExpression(const std::pair<Op,std::tuple<T1,T2,T3> > &arg): std::pair<Op,std::tuple<T1,T2,T3> >(arg) {};
};
2015-03-04 03:12:19 +00:00
template<class vobj>
class Lattice : public LatticeBase
2015-03-04 03:12:19 +00:00
{
public:
GridBase *_grid;
2015-03-04 03:12:19 +00:00
int checkerboard;
std::vector<vobj,alignedAllocator<vobj> > _odata;
2015-04-10 04:53:09 +01:00
public:
typedef typename vobj::scalar_type scalar_type;
typedef typename vobj::vector_type vector_type;
typedef vobj vector_object;
////////////////////////////////////////////////////////////////////////////////
// Expression Template closure support
////////////////////////////////////////////////////////////////////////////////
template <typename Op, typename T1> inline Lattice<vobj> & operator=(const LatticeUnaryExpression<Op,T1> &expr)
{
#pragma omp parallel for
for(int ss=0;ss<_grid->oSites();ss++){
vobj tmp= eval(ss,expr);
vstream(_odata[ss] ,tmp);
}
return *this;
}
template <typename Op, typename T1,typename T2> inline Lattice<vobj> & operator=(const LatticeBinaryExpression<Op,T1,T2> &expr)
{
#pragma omp parallel for
for(int ss=0;ss<_grid->oSites();ss++){
vobj tmp= eval(ss,expr);
vstream(_odata[ss] ,tmp);
}
return *this;
}
template <typename Op, typename T1,typename T2,typename T3> inline Lattice<vobj> & operator=(const LatticeTrinaryExpression<Op,T1,T2,T3> &expr)
{
#pragma omp parallel for
for(int ss=0;ss<_grid->oSites();ss++){
vobj tmp= eval(ss,expr);
vstream(_odata[ss] ,tmp);
}
return *this;
}
//GridFromExpression is tricky to do
template<class Op,class T1>
Lattice(const LatticeUnaryExpression<Op,T1> & expr): _grid(nullptr){
GridFromExpression(_grid,expr);
assert(_grid!=nullptr);
_odata.resize(_grid->oSites());
#pragma omp parallel for
for(int ss=0;ss<_grid->oSites();ss++){
_odata[ss] = eval(ss,expr);
}
};
template<class Op,class T1, class T2>
Lattice(const LatticeBinaryExpression<Op,T1,T2> & expr): _grid(nullptr){
GridFromExpression(_grid,expr);
assert(_grid!=nullptr);
_odata.resize(_grid->oSites());
#pragma omp parallel for
for(int ss=0;ss<_grid->oSites();ss++){
_odata[ss] = eval(ss,expr);
}
};
template<class Op,class T1, class T2, class T3>
Lattice(const LatticeTrinaryExpression<Op,T1,T2,T3> & expr): _grid(nullptr){
GridFromExpression(_grid,expr);
assert(_grid!=nullptr);
_odata.resize(_grid->oSites());
#pragma omp parallel for
for(int ss=0;ss<_grid->oSites();ss++){
_odata[ss] = eval(ss,expr);
}
};
2015-04-18 20:44:19 +01:00
//////////////////////////////////////////////////////////////////
// Constructor requires "grid" passed.
// what about a default grid?
//////////////////////////////////////////////////////////////////
2015-05-03 09:44:47 +01:00
Lattice(GridBase *grid) : _grid(grid), _odata(_grid->oSites()) {
// _odata.reserve(_grid->oSites());
2015-05-03 09:44:47 +01:00
// _odata.resize(_grid->oSites());
2015-04-03 22:54:13 +01:00
assert((((uint64_t)&_odata[0])&0xF) ==0);
2015-03-04 03:12:19 +00:00
checkerboard=0;
}
2015-04-18 20:44:19 +01:00
template<class sobj> inline Lattice<vobj> & operator = (const sobj & r){
2015-03-04 03:12:19 +00:00
#pragma omp parallel for
for(int ss=0;ss<_grid->oSites();ss++){
this->_odata[ss]=r;
}
return *this;
}
template<class robj> inline Lattice<vobj> & operator = (const Lattice<robj> & r){
conformable(*this,r);
std::cout<<"Lattice operator ="<<std::endl;
#pragma omp parallel for
for(int ss=0;ss<_grid->oSites();ss++){
this->_odata[ss]=r._odata[ss];
}
return *this;
}
2015-03-04 03:12:19 +00:00
// *=,+=,-= operators inherit behvour from correspond */+/- operation
2015-04-18 20:44:19 +01:00
template<class T> inline Lattice<vobj> &operator *=(const T &r) {
2015-03-04 03:12:19 +00:00
*this = (*this)*r;
return *this;
}
2015-04-18 20:44:19 +01:00
template<class T> inline Lattice<vobj> &operator -=(const T &r) {
2015-03-04 03:12:19 +00:00
*this = (*this)-r;
return *this;
}
2015-04-18 20:44:19 +01:00
template<class T> inline Lattice<vobj> &operator +=(const T &r) {
2015-03-04 03:12:19 +00:00
*this = (*this)+r;
return *this;
}
2015-04-18 20:44:19 +01:00
inline friend Lattice<vobj> operator / (const Lattice<vobj> &lhs,const Lattice<vobj> &rhs){
2015-03-04 03:12:19 +00:00
conformable(lhs,rhs);
2015-04-18 20:44:19 +01:00
Lattice<vobj> ret(lhs._grid);
2015-03-04 03:12:19 +00:00
#pragma omp parallel for
for(int ss=0;ss<lhs._grid->oSites();ss++){
2015-04-18 20:44:19 +01:00
ret._odata[ss] = lhs._odata[ss]/rhs._odata[ss];
2015-04-18 14:36:01 +01:00
}
return ret;
};
2015-04-18 20:44:19 +01:00
}; // class Lattice
}
2015-04-18 14:36:01 +01:00
#undef GRID_LATTICE_EXPRESSION_TEMPLATES
2015-04-18 21:23:32 +01:00
#include <lattice/Grid_lattice_conformable.h>
#ifdef GRID_LATTICE_EXPRESSION_TEMPLATES
#include <lattice/Grid_lattice_ET.h>
#else
#include <lattice/Grid_lattice_overload.h>
#endif
2015-04-18 21:23:32 +01:00
#include <lattice/Grid_lattice_arith.h>
2015-04-18 21:23:32 +01:00
#include <lattice/Grid_lattice_trace.h>
#include <lattice/Grid_lattice_transpose.h>
#include <lattice/Grid_lattice_local.h>
#include <lattice/Grid_lattice_reduction.h>
#include <lattice/Grid_lattice_peekpoke.h>
#include <lattice/Grid_lattice_reality.h>
#include <Grid_extract.h>
2015-04-18 21:23:32 +01:00
#include <lattice/Grid_lattice_coordinate.h>
#include <lattice/Grid_lattice_rng.h>
#include <lattice/Grid_lattice_transfer.h>
2015-04-14 22:40:40 +01:00
2015-03-04 05:31:44 +00:00
#endif