diff --git a/lib/lattice/Lattice_base.h b/lib/lattice/Lattice_base.h index 7cc7dc72..fa15ad33 100644 --- a/lib/lattice/Lattice_base.h +++ b/lib/lattice/Lattice_base.h @@ -55,11 +55,24 @@ extern int GridCshiftPermuteMap[4][16]; // Basic expressions used in Expression Template //////////////////////////////////////////////// -class LatticeBase +// The data inside the lattice class +class LatticeBase {}; + +template class LatticeAccelerator : public LatticeBase { public: - virtual ~LatticeBase(void) = default; - GridBase *_grid; + int checkerboard; + vobj *_odata; // A managed pointer + uint64_t _odata_size; + // virtual ~LatticeBase(void) = default; + accelerator_inline LatticeAccelerator() : checkerboard(0), _odata(nullptr), _odata_size(0) { + // std::cout << " Lattice accelerator object "<_odata<<" size "<_odata_size< -class Lattice : public LatticeBase +class Lattice : public LatticeAccelerator { -private: - // vobj * _odata_p; - // uint64_t _odata_size; -public: - int checkerboard; - Vector _odata; - - // to pthread need a computable loop where loop induction is not required - 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]; }; - +public: // Move to private and fix + GridBase *_grid; public: + /////////////////////////////////////////////////// + // 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) { - _odata.resize(size); - // _odata_p = &_odata[0]; - // _odata_size = 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<checkerboard=cb; #ifdef STREAMING_STORES accelerator_loop(ss,(*this),{ vobj tmp = eval(ss,expr); - vstream(_odata[ss] ,tmp); + vstream(this->_odata[ss] ,tmp); }); #else accelerator_loop(ss,(*this),{ - _odata[ss]=eval(ss,expr); + this->_odata[ss]=eval(ss,expr); }); #endif return *this; @@ -151,16 +193,16 @@ public: int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); - checkerboard=cb; + this->checkerboard=cb; #ifdef STREAMING_STORES accelerator_loop(ss,(*this),{ vobj tmp = eval(ss,expr); - vstream(_odata[ss] ,tmp); + vstream(this->_odata[ss] ,tmp); }); #else accelerator_loop(ss,(*this),{ - _odata[ss]=eval(ss,expr); + this->_odata[ss]=eval(ss,expr); }); #endif return *this; @@ -175,16 +217,16 @@ public: int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); - checkerboard=cb; + this->checkerboard=cb; #ifdef STREAMING_STORES accelerator_loop(ss,(*this),{ - //vobj tmp = eval(ss,expr); - vstream(_odata[ss] ,eval(ss,expr)); + vobj tmp = eval(ss,expr); + vstream(this->_odata[ss] ,tmp); }); #else accelerator_loop(ss,(*this),{ - _odata[ss] = eval(ss,expr); + this->_odata[ss] = eval(ss,expr); }); #endif return *this; @@ -199,17 +241,16 @@ public: int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); - checkerboard=cb; + this->checkerboard=cb; resize(_grid->oSites()); #ifdef STREAMING_STORES - cpu_loop(ss,(*this),{ - vobj tmp = eval(ss,expr); - vstream(_odata[ss] ,tmp); + accelerator_loop(ss,(*this),{ + vstream(this->_odata[ss] ,eval(ss,expr)); }); #else - cpu_loop(ss,(*this),{ - _odata[ss]=eval(ss,expr); + accelerator_loop(ss,(*this),{ + this->_odata[ss]=eval(ss,expr); }); #endif } @@ -218,22 +259,21 @@ public: _grid = nullptr; GridFromExpression(_grid,expr); assert(_grid!=nullptr); + resize(_grid->oSites()); int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); - checkerboard=cb; + this->checkerboard=cb; - resize(_grid->oSites()); - #ifdef STREAMING_STORES - cpu_loop(ss,(*this),{ + accelerator_loop(ss,(*this),{ vobj tmp = eval(ss,expr); - vstream(_odata[ss] ,tmp); + vstream(this->_odata[ss] ,tmp); }); #else - cpu_loop(ss,(*this),{ - _odata[ss]=eval(ss,expr); + accelerator_loop(ss,(*this),{ + this->_odata[ss]=eval(ss,expr); }); #endif } @@ -246,46 +286,24 @@ public: int cb=-1; CBFromExpression(cb,expr); assert( (cb==Odd) || (cb==Even)); - checkerboard=cb; + this->checkerboard=cb; resize(_grid->oSites()); - cpu_loop(ss,(*this),{ - vstream(_odata[ss] ,eval(ss,expr)); + accelerator_loop(ss,(*this),{ + vstream(this->_odata[ss] ,eval(ss,expr)); }); } - ////////////////////////////////////////////////////////////////// - // Constructor requires "grid" passed. - // what about a default grid? - ////////////////////////////////////////////////////////////////// - Lattice(GridBase *grid) { - _grid = grid; - resize(grid->oSites()); - assert((((uint64_t)&_odata[0])&0xF) ==0); - checkerboard=0; - } - - Lattice(const Lattice& r){ // copy constructor - _grid = r._grid; - checkerboard = r.checkerboard; - resize(_grid->oSites());// essential - cpu_loop(ss,(*this),{ - _odata[ss]=r._odata[ss]; - }); - } - - - - virtual ~Lattice(void) = default; - + // virtual ~Lattice(void) = default; + /* void reset(GridBase* grid) { if (_grid != grid) { _grid = grid; resize(grid->oSites()); - checkerboard = 0; + this->checkerboard = 0; } } - + */ template inline Lattice & operator = (const sobj & r){ accelerator_loop(ss,(*this),{ @@ -293,8 +311,40 @@ public: }); 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._odata[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),{ @@ -302,6 +352,33 @@ public: }); 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._odata[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) { @@ -318,7 +395,7 @@ public: return *this; } }; // class Lattice - + template std::ostream& operator<< (std::ostream& stream, const Lattice &o){ std::vector gcoor; typedef typename vobj::scalar_object sobj;