/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: ./lib/lattice/Lattice_reduction.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 */ #ifndef GRID_LATTICE_REDUCTION_H #define GRID_LATTICE_REDUCTION_H #ifdef GRID_WARN_SUBOPTIMAL #warning "Optimisation alert all these reduction loops are NOT threaded " #endif NAMESPACE_BEGIN(Grid); //////////////////////////////////////////////////////////////////////////////////////////////////// // Deterministic Reduction operations //////////////////////////////////////////////////////////////////////////////////////////////////// template inline RealD norm2(const Lattice &arg){ ComplexD nrm = innerProduct(arg,arg); return real(nrm); } // Double inner product template inline ComplexD innerProduct(const Lattice &left,const Lattice &right) { typedef typename vobj::scalar_type scalar_type; typedef typename vobj::vector_typeD vector_type; scalar_type nrm; GridBase *grid = left.Grid(); std::vector > sumarray(grid->SumArraySize()); auto left_v = left.View(); auto right_v=right.View(); thread_loop( (int thr=0;thrSumArraySize();thr++),{ int mywork, myoff; GridThread::GetWork(left.Grid()->oSites(),thr,mywork,myoff); decltype(innerProductD(left_v[0],right_v[0])) vnrm=Zero(); // private to thread; sub summation for(int ss=myoff;ssSumArraySize();i++){ vvnrm = vvnrm+sumarray[i]; } nrm = Reduce(vvnrm);// sum across simd right.Grid()->GlobalSum(nrm); return nrm; } template inline auto sum(const LatticeUnaryExpression & expr) ->typename decltype(expr.op.func(eval(0,expr.arg1)))::scalar_object { return sum(closure(expr)); } template inline auto sum(const LatticeBinaryExpression & expr) ->typename decltype(expr.op.func(eval(0,expr.arg1,eval(0,expr.arg2))))::scalar_object { return sum(closure(expr)); } template inline auto sum(const LatticeTrinaryExpression & expr) ->typename decltype(expr.op.func(eval(0,expr.arg1), eval(0,expr.arg2), eval(0,expr.arg3) ))::scalar_object { return sum(closure(expr)); } template inline typename vobj::scalar_object sum(const Lattice &arg) { GridBase *grid=arg.Grid(); int Nsimd = grid->Nsimd(); std::vector > sumarray(grid->SumArraySize()); for(int i=0;iSumArraySize();i++){ sumarray[i]=Zero(); } auto arg_v = arg.View(); thread_loop( (int thr=0;thrSumArraySize();thr++),{ int mywork, myoff; GridThread::GetWork(grid->oSites(),thr,mywork,myoff); vobj vvsum=Zero(); for(int ss=myoff;ssSumArraySize();i++){ vsum = vsum+sumarray[i]; } typedef typename vobj::scalar_object sobj; sobj ssum; zeroit(ssum); ExtractBuffer buf(Nsimd); extract(vsum,buf); for(int i=0;iGlobalSum(ssum); return ssum; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////// // sliceSum, sliceInnerProduct, sliceAxpy, sliceNorm etc... ////////////////////////////////////////////////////////////////////////////////////////////////////////////// template inline void sliceSum(const Lattice &Data,std::vector &result,int orthogdim) { /////////////////////////////////////////////////////// // FIXME precision promoted summation // may be important for correlation functions // But easily avoided by using double precision fields /////////////////////////////////////////////////////// typedef typename vobj::scalar_object sobj; GridBase *grid = Data.Grid(); assert(grid!=NULL); const int Nd = grid->_ndimension; const int Nsimd = grid->Nsimd(); assert(orthogdim >= 0); assert(orthogdim < Nd); int fd=grid->_fdimensions[orthogdim]; int ld=grid->_ldimensions[orthogdim]; int rd=grid->_rdimensions[orthogdim]; std::vector > lvSum(rd); // will locally sum vectors first std::vector lsSum(ld,Zero()); // sum across these down to scalars ExtractBuffer extracted(Nsimd); // splitting the SIMD result.resize(fd); // And then global sum to return the same vector to every node for(int r=0;r_slice_nblock[orthogdim]; int e2= grid->_slice_block [orthogdim]; int stride=grid->_slice_stride[orthogdim]; // sum over reduced dimension planes, breaking out orthog dir auto Data_v = Data.View(); thread_loop( (int r=0;r_ostride[orthogdim]; // base offset for start of plane for(int n=0;niCoorFromIindex(icoor,idx); int ldx =rt+icoor[orthogdim]*rd; lsSum[ldx]=lsSum[ldx]+extracted[idx]; } } // sum over nodes. sobj gsum; for(int t=0;t_processor_coor[orthogdim] ) { gsum=lsSum[lt]; } else { gsum=Zero(); } grid->GlobalSum(gsum); result[t]=gsum; } } template static void sliceInnerProductVector( std::vector & result, const Lattice &lhs,const Lattice &rhs,int orthogdim) { typedef typename vobj::vector_type vector_type; typedef typename vobj::scalar_type scalar_type; GridBase *grid = lhs.Grid(); assert(grid!=NULL); conformable(grid,rhs.Grid()); const int Nd = grid->_ndimension; const int Nsimd = grid->Nsimd(); assert(orthogdim >= 0); assert(orthogdim < Nd); int fd=grid->_fdimensions[orthogdim]; int ld=grid->_ldimensions[orthogdim]; int rd=grid->_rdimensions[orthogdim]; std::vector > lvSum(rd); // will locally sum vectors first std::vector lsSum(ld,scalar_type(0.0)); // sum across these down to scalars ExtractBuffer > extracted(Nsimd); // splitting the SIMD result.resize(fd); // And then global sum to return the same vector to every node for IO to file for(int r=0;r_slice_nblock[orthogdim]; int e2= grid->_slice_block [orthogdim]; int stride=grid->_slice_stride[orthogdim]; auto lhs_v = lhs.View(); auto rhs_v = rhs.View(); thread_loop( (int r=0;r_ostride[orthogdim]; // base offset for start of plane for(int n=0;n temp; temp._internal = lvSum[rt]; extract(temp,extracted); for(int idx=0;idxiCoorFromIindex(icoor,idx); int ldx =rt+icoor[orthogdim]*rd; lsSum[ldx]=lsSum[ldx]+extracted[idx]._internal; } } // sum over nodes. scalar_type gsum; for(int t=0;t_processor_coor[orthogdim] ) { gsum=lsSum[lt]; } else { gsum=scalar_type(0.0); } grid->GlobalSum(gsum); result[t]=gsum; } } template static void sliceNorm (std::vector &sn,const Lattice &rhs,int Orthog) { typedef typename vobj::scalar_object sobj; typedef typename vobj::scalar_type scalar_type; typedef typename vobj::vector_type vector_type; int Nblock = rhs.Grid()->GlobalDimensions()[Orthog]; std::vector ip(Nblock); sn.resize(Nblock); sliceInnerProductVector(ip,rhs,rhs,Orthog); for(int ss=0;ss static void sliceMaddVector(Lattice &R,std::vector &a,const Lattice &X,const Lattice &Y, int orthogdim,RealD scale=1.0) { typedef typename vobj::scalar_object sobj; typedef typename vobj::scalar_type scalar_type; typedef typename vobj::vector_type vector_type; typedef typename vobj::tensor_reduced tensor_reduced; scalar_type zscale(scale); GridBase *grid = X.Grid(); int Nsimd =grid->Nsimd(); int Nblock =grid->GlobalDimensions()[orthogdim]; int fd =grid->_fdimensions[orthogdim]; int ld =grid->_ldimensions[orthogdim]; int rd =grid->_rdimensions[orthogdim]; int e1 =grid->_slice_nblock[orthogdim]; int e2 =grid->_slice_block [orthogdim]; int stride =grid->_slice_stride[orthogdim]; Coordinate icoor; for(int r=0;r_ostride[orthogdim]; // base offset for start of plane vector_type av; for(int l=0;liCoorFromIindex(icoor,l); int ldx =r+icoor[orthogdim]*rd; scalar_type *as =(scalar_type *)&av; as[l] = scalar_type(a[ldx])*zscale; } tensor_reduced at; at=av; auto X_v = X.View(); auto Y_v = Y.View(); auto R_v = R.View(); thread_loop_collapse2( (int n=0;n