/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: ./lib/lattice/Lattice_base.h Copyright (C) 2015 Author: Azusa Yamaguchi Author: Peter Boyle Author: paboyle 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 */ #pragma once #define STREAMING_STORES NAMESPACE_BEGIN(Grid); // TODO: // mac,real,imag // Functionality: // -=,+=,*=,() // add,+,sub,-,mult,mac,* // adj,conjugate // real,imag // transpose,transposeIndex // trace,traceIndex // peekIndex // innerProduct,outerProduct, // localNorm2 // localInnerProduct extern int GridCshiftPermuteMap[4][16]; //////////////////////////////////////////////// // Basic expressions used in Expression Template //////////////////////////////////////////////// // The data inside the lattice class class LatticeBase {}; template class LatticeAccelerator : public LatticeBase { protected: int checkerboard; vobj *_odata; // A managed pointer uint64_t _odata_size; // virtual ~LatticeBase(void) = default; public: accelerator_inline LatticeAccelerator() : checkerboard(0), _odata(nullptr), _odata_size(0) { // std::cout << " Lattice accelerator object "<_odata<<" size "<_odata_size< class LatticeUnaryExpression : public std::pair > , public LatticeExpressionBase { public: LatticeUnaryExpression(const std::pair > &arg): std::pair >(arg) {}; }; template class LatticeBinaryExpression : public std::pair > , public LatticeExpressionBase { public: LatticeBinaryExpression(const std::pair > &arg): std::pair >(arg) {}; }; template class LatticeTrinaryExpression :public std::pair >, public LatticeExpressionBase { public: LatticeTrinaryExpression(const std::pair > &arg): std::pair >(arg) {}; }; void inline conformable(GridBase *lhs,GridBase *rhs) { assert(lhs == rhs); } template class Lattice : public LatticeAccelerator { protected: // Move to private and fix GridBase *_grid; public: GridBase *Grid(void) const { return _grid; } /////////////////////////////////////////////////// // Member types /////////////////////////////////////////////////// typedef typename vobj::scalar_type scalar_type; typedef typename vobj::vector_type vector_type; typedef vobj vector_object; private: void dealloc(void) { alignedAllocator alloc; // std::cout << " deallocating lattice "<_odata<<" size "<_odata_size<_odata_size ) { alloc.deallocate(this->_odata,this->_odata_size); this->_odata=nullptr; this->_odata_size=0; } } void resize(uint64_t size) { alignedAllocator alloc; if ( this->_odata_size != size ) { dealloc(); } this->_odata_size = size; if ( size ) this->_odata = alloc.allocate(this->_odata_size); else this->_odata = nullptr; // std::cout << " allocated lattice "<_odata<<" size "<_odata_size<_odata = ptr; assert(this->_odata_size == count); // std::cout << " copied lattice "<_odata<<" size "<_odata_size<_odata_size ) { // std::cout << " deleting lattice this"<_odata<<" size "<_odata_size<_odata<<" size "<_odata_size< inline Lattice & operator=(const LatticeUnaryExpression &expr) { GridBase *egrid(nullptr); GridFromExpression(egrid,expr); assert(egrid!=nullptr); conformable(_grid,egrid); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; #ifdef STREAMING_STORES accelerator_loop(ss,(*this),{ vobj tmp = eval(ss,expr); vstream(this->_odata[ss] ,tmp); }); #else accelerator_loop(ss,(*this),{ this->_odata[ss]=eval(ss,expr); }); #endif return *this; } template inline Lattice & operator=(const LatticeBinaryExpression &expr) { GridBase *egrid(nullptr); GridFromExpression(egrid,expr); assert(egrid!=nullptr); conformable(_grid,egrid); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; #ifdef STREAMING_STORES accelerator_loop(ss,(*this),{ vobj tmp = eval(ss,expr); vstream(this->_odata[ss] ,tmp); }); #else accelerator_loop(ss,(*this),{ this->_odata[ss]=eval(ss,expr); }); #endif return *this; } template inline Lattice & operator=(const LatticeTrinaryExpression &expr) { GridBase *egrid(nullptr); GridFromExpression(egrid,expr); assert(egrid!=nullptr); conformable(_grid,egrid); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; #ifdef STREAMING_STORES accelerator_loop(ss,(*this),{ vobj tmp = eval(ss,expr); vstream(this->_odata[ss] ,tmp); }); #else accelerator_loop(ss,(*this),{ this->_odata[ss] = eval(ss,expr); }); #endif return *this; } //GridFromExpression is tricky to do template Lattice(const LatticeUnaryExpression & expr) { _grid = nullptr; GridFromExpression(_grid,expr); assert(_grid!=nullptr); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; resize(_grid->oSites()); #ifdef STREAMING_STORES accelerator_loop(ss,(*this),{ vstream(this->_odata[ss] ,eval(ss,expr)); }); #else accelerator_loop(ss,(*this),{ this->_odata[ss]=eval(ss,expr); }); #endif } template Lattice(const LatticeBinaryExpression & expr) { _grid = nullptr; GridFromExpression(_grid,expr); assert(_grid!=nullptr); resize(_grid->oSites()); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; #ifdef STREAMING_STORES accelerator_loop(ss,(*this),{ vobj tmp = eval(ss,expr); vstream(this->_odata[ss] ,tmp); }); #else accelerator_loop(ss,(*this),{ this->_odata[ss]=eval(ss,expr); }); #endif } template Lattice(const LatticeTrinaryExpression & expr) { _grid = nullptr; GridFromExpression(_grid,expr); assert(_grid!=nullptr); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; resize(_grid->oSites()); accelerator_loop(ss,(*this),{ vstream(this->_odata[ss] ,eval(ss,expr)); }); } // virtual ~Lattice(void) = default; /* void reset(GridBase* grid) { if (_grid != grid) { _grid = grid; resize(grid->oSites()); this->checkerboard = 0; } } */ template inline Lattice & operator = (const sobj & r){ accelerator_loop(ss,(*this),{ this->_odata[ss]=r; }); return *this; } ////////////////////////////////////////////////////////////////// // Follow rule of five, with Constructor requires "grid" passed // to user defined constructor ////////////////////////////////////////////////////////////////// Lattice(GridBase *grid) { // user defined constructor _grid = grid; // std::cout << "Lattice constructor(grid)"<oSites()); assert((((uint64_t)&this->_odata[0])&0xF) ==0); this->checkerboard=0; } Lattice(const Lattice& r){ // copy constructor // std::cout << "Lattice constructor(const Lattice &) "<checkerboard = r.Checkerboard(); accelerator_loop(ss,(*this),{ this->_odata[ss]=r[ss]; }); } Lattice(Lattice && r){ // move constructor // std::cout << "Lattice move constructor(Lattice &) "<_odata = r._odata; this->_odata_size = r._odata_size; this->checkerboard= r.Checkerboard(); r._odata = nullptr; r._odata_size = 0; } // assignment template template inline Lattice & operator = (const Lattice & r){ // std::cout << "Lattice = (Lattice &)"<::value,int>::type i=0; this->checkerboard = r.Checkerboard(); conformable(*this,r); accelerator_loop(ss,(*this),{ this->_odata[ss]=r[ss]; }); return *this; } // Copy assignment inline Lattice & operator = (const Lattice & r){ // std::cout << "Lattice = (Lattice &)"<checkerboard = r.Checkerboard(); conformable(*this,r); accelerator_loop(ss,(*this),{ this->_odata[ss]=r[ss]; }); return *this; } // Move assignment if same type inline Lattice & operator = (Lattice && r){ // std::cout << "Lattice = (Lattice &&)"<_grid = r.Grid(); this->checkerboard = r.Checkerboard(); this->_odata = r._odata; this->_odata_size = r._odata_size; this->checkerboard= r.Checkerboard(); r._odata = nullptr; r._odata_size = 0; return *this; } // *=,+=,-= operators inherit behvour from correspond */+/- operation template inline Lattice &operator *=(const T &r) { *this = (*this)*r; return *this; } template inline Lattice &operator -=(const T &r) { *this = (*this)-r; return *this; } template inline Lattice &operator +=(const T &r) { *this = (*this)+r; return *this; } friend inline void swap(Lattice &l, Lattice &r) { conformable(l,r); LatticeAccelerator tmp; LatticeAccelerator *lp = (LatticeAccelerator *)&l; LatticeAccelerator *rp = (LatticeAccelerator *)&r; tmp = *lp; *lp=*rp; *rp=tmp; } }; // class Lattice template std::ostream& operator<< (std::ostream& stream, const Lattice &o){ std::vector gcoor; typedef typename vobj::scalar_object sobj; sobj ss; for(int g=0;g_gsites;g++){ o.Grid()->GlobalIndexToGlobalCoor(g,gcoor); peekSite(ss,o,gcoor); stream<<"["; for(int d=0;d