/************************************************************************************* 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 Author: Christoph Lehner 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); extern int GridCshiftPermuteMap[4][16]; ///////////////////////////////////////////////////////////////////////////////////////// // The real lattice class, with normal copy and assignment semantics. // This contains extra (host resident) grid pointer data that may be accessed by host code ///////////////////////////////////////////////////////////////////////////////////////// template class Lattice : public LatticeAccelerator { public: GridBase *Grid(void) const { return this->_grid; } /////////////////////////////////////////////////// // Member types /////////////////////////////////////////////////// typedef typename vobj::scalar_type scalar_type; typedef typename vobj::vector_type vector_type; typedef typename vobj::scalar_object scalar_object; typedef vobj vector_object; private: void dealloc(void) { if( this->_odata_size ) { alignedAllocator alloc; alloc.deallocate(this->_odata,this->_odata_size); this->_odata=nullptr; this->_odata_size=0; } } void resize(uint64_t size) { if ( this->_odata_size != size ) { alignedAllocator alloc; dealloc(); this->_odata_size = size; if ( size ) this->_odata = alloc.allocate(this->_odata_size); else this->_odata = nullptr; } } public: ///////////////////////////////////////////////////////////////////////////////// // Can use to make accelerator dirty without copy from host ; useful for temporaries "dont care" prev contents ///////////////////////////////////////////////////////////////////////////////// void SetViewMode(ViewMode mode) { LatticeView accessor(*( (LatticeAccelerator *) this),mode); accessor.ViewClose(); } ///////////////////////////////////////////////////////////////////////////////// // Return a view object that may be dereferenced in site loops. // The view is trivially copy constructible and may be copied to an accelerator device // in device lambdas ///////////////////////////////////////////////////////////////////////////////// LatticeView View (ViewMode mode) const { LatticeView accessor(*( (LatticeAccelerator *) this),mode); return accessor; } ~Lattice() { if ( this->_odata_size ) { dealloc(); } } //////////////////////////////////////////////////////////////////////////////// // Expression Template closure support //////////////////////////////////////////////////////////////////////////////// template inline Lattice & operator=(const LatticeUnaryExpression &expr) { GridBase *egrid(nullptr); GridFromExpression(egrid,expr); assert(egrid!=nullptr); conformable(this->_grid,egrid); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; auto exprCopy = expr; ExpressionViewOpen(exprCopy); auto me = View(AcceleratorWriteDiscard); accelerator_for(ss,me.size(),vobj::Nsimd(),{ auto tmp = eval(ss,exprCopy); coalescedWrite(me[ss],tmp); }); me.ViewClose(); ExpressionViewClose(exprCopy); return *this; } template inline Lattice & operator=(const LatticeBinaryExpression &expr) { GridBase *egrid(nullptr); GridFromExpression(egrid,expr); assert(egrid!=nullptr); conformable(this->_grid,egrid); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; auto exprCopy = expr; ExpressionViewOpen(exprCopy); auto me = View(AcceleratorWriteDiscard); accelerator_for(ss,me.size(),vobj::Nsimd(),{ auto tmp = eval(ss,exprCopy); coalescedWrite(me[ss],tmp); }); me.ViewClose(); ExpressionViewClose(exprCopy); return *this; } template inline Lattice & operator=(const LatticeTrinaryExpression &expr) { GridBase *egrid(nullptr); GridFromExpression(egrid,expr); assert(egrid!=nullptr); conformable(this->_grid,egrid); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; auto exprCopy = expr; ExpressionViewOpen(exprCopy); auto me = View(AcceleratorWriteDiscard); accelerator_for(ss,me.size(),vobj::Nsimd(),{ auto tmp = eval(ss,exprCopy); coalescedWrite(me[ss],tmp); }); me.ViewClose(); ExpressionViewClose(exprCopy); return *this; } //GridFromExpression is tricky to do template Lattice(const LatticeUnaryExpression & expr) { this->_grid = nullptr; GridFromExpression(this->_grid,expr); assert(this->_grid!=nullptr); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; resize(this->_grid->oSites()); *this = expr; } template Lattice(const LatticeBinaryExpression & expr) { this->_grid = nullptr; GridFromExpression(this->_grid,expr); assert(this->_grid!=nullptr); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; resize(this->_grid->oSites()); *this = expr; } template Lattice(const LatticeTrinaryExpression & expr) { this->_grid = nullptr; GridFromExpression(this->_grid,expr); assert(this->_grid!=nullptr); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); this->checkerboard=cb; resize(this->_grid->oSites()); *this = expr; } template inline Lattice & operator = (const sobj & r){ auto me = View(CpuWrite); thread_for(ss,me.size(),{ me[ss]= r; }); me.ViewClose(); return *this; } ////////////////////////////////////////////////////////////////// // Follow rule of five, with Constructor requires "grid" passed // to user defined constructor /////////////////////////////////////////// // user defined constructor /////////////////////////////////////////// Lattice(GridBase *grid,ViewMode mode=AcceleratorWriteDiscard) { this->_grid = grid; resize(this->_grid->oSites()); assert((((uint64_t)&this->_odata[0])&0xF) ==0); this->checkerboard=0; SetViewMode(mode); } // virtual ~Lattice(void) = default; void reset(GridBase* grid) { if (this->_grid != grid) { this->_grid = grid; this->resize(grid->oSites()); this->checkerboard = 0; } } /////////////////////////////////////////// // copy constructor /////////////////////////////////////////// Lattice(const Lattice& r){ this->_grid = r.Grid(); resize(this->_grid->oSites()); *this = r; } /////////////////////////////////////////// // move constructor /////////////////////////////////////////// Lattice(Lattice && r){ this->_grid = r.Grid(); this->_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){ typename std::enable_if::value,int>::type i=0; conformable(*this,r); this->checkerboard = r.Checkerboard(); auto me = View(AcceleratorWriteDiscard); auto him= r.View(AcceleratorRead); accelerator_for(ss,me.size(),vobj::Nsimd(),{ coalescedWrite(me[ss],him(ss)); }); me.ViewClose(); him.ViewClose(); return *this; } /////////////////////////////////////////// // Copy assignment /////////////////////////////////////////// inline Lattice & operator = (const Lattice & r){ this->checkerboard = r.Checkerboard(); conformable(*this,r); auto me = View(AcceleratorWriteDiscard); auto him= r.View(AcceleratorRead); accelerator_for(ss,me.size(),vobj::Nsimd(),{ coalescedWrite(me[ss],him(ss)); }); me.ViewClose(); him.ViewClose(); return *this; } /////////////////////////////////////////// // Move assignment possible if same type /////////////////////////////////////////// inline Lattice & operator = (Lattice && r){ resize(0); // deletes if appropriate this->_grid = r.Grid(); 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){ typedef typename vobj::scalar_object sobj; for(int g=0;g_gsites;g++){ Coordinate gcoor; o.Grid()->GlobalIndexToGlobalCoor(g,gcoor); sobj ss; peekSite(ss,o,gcoor); stream<<"["; for(int d=0;d