1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 15:55:37 +00:00
Grid/lib/lattice/Lattice_arith.h

287 lines
8.0 KiB
C
Raw Normal View History

2018-01-15 00:04:43 +00:00
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/lattice/Lattice_arith.h
Copyright (C) 2015
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
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
2018-01-15 00:04:43 +00:00
*************************************************************************************/
/* END LEGAL */
2015-04-18 20:44:19 +01:00
#ifndef GRID_LATTICE_ARITH_H
#define GRID_LATTICE_ARITH_H
2018-01-15 00:04:43 +00:00
NAMESPACE_BEGIN(Grid);
2015-04-18 20:44:19 +01:00
2018-01-15 00:04:43 +00:00
//////////////////////////////////////////////////////////////////////////////////////////////////////
// avoid copy back routines for mult, mac, sub, add
//////////////////////////////////////////////////////////////////////////////////////////////////////
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void mult(Lattice<obj1> &ret,const Lattice<obj2> &lhs,const Lattice<obj3> &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = lhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,rhs);
conformable(lhs,rhs);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
mult(&tmp,&lhs[ss],&rhs[ss]);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-26 23:06:03 +00:00
mult(&ret[ss],&lhs[ss],&rhs[ss]);
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2015-04-18 20:44:19 +01:00
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void mac(Lattice<obj1> &ret,const Lattice<obj2> &lhs,const Lattice<obj3> &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = lhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,rhs);
conformable(lhs,rhs);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
mac(&tmp,&lhs[ss],&rhs[ss]);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-26 23:06:03 +00:00
mac(&ret[ss],&lhs[ss],&rhs[ss]);
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2015-05-03 09:44:47 +01:00
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void sub(Lattice<obj1> &ret,const Lattice<obj2> &lhs,const Lattice<obj3> &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = lhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,rhs);
conformable(lhs,rhs);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
sub(&tmp,&lhs[ss],&rhs[ss]);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-26 23:06:03 +00:00
sub(&ret[ss],&lhs[ss],&rhs[ss]);
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void add(Lattice<obj1> &ret,const Lattice<obj2> &lhs,const Lattice<obj3> &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = lhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,rhs);
conformable(lhs,rhs);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
add(&tmp,&lhs[ss],&rhs[ss]);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-26 23:06:03 +00:00
add(&ret[ss],&lhs[ss],&rhs[ss]);
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2015-04-18 20:44:19 +01:00
2018-01-15 00:04:43 +00:00
//////////////////////////////////////////////////////////////////////////////////////////////////////
// avoid copy back routines for mult, mac, sub, add
//////////////////////////////////////////////////////////////////////////////////////////////////////
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void mult(Lattice<obj1> &ret,const Lattice<obj2> &lhs,const obj3 &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = lhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(lhs,ret);
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
mult(&tmp,&lhs[ss],&rhs);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
2018-01-15 00:04:43 +00:00
}
2015-04-18 20:44:19 +01:00
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void mac(Lattice<obj1> &ret,const Lattice<obj2> &lhs,const obj3 &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = lhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,lhs);
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
mac(&tmp,&lhs[ss],&rhs);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
2018-01-15 00:04:43 +00:00
}
2015-04-18 20:44:19 +01:00
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void sub(Lattice<obj1> &ret,const Lattice<obj2> &lhs,const obj3 &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = lhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,lhs);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
sub(&tmp,&lhs[ss],&rhs);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-26 23:06:03 +00:00
sub(&ret[ss],&lhs[ss],&rhs);
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void add(Lattice<obj1> &ret,const Lattice<obj2> &lhs,const obj3 &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = lhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(lhs,ret);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
add(&tmp,&lhs[ss],&rhs);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,lhs,{
2018-01-26 23:06:03 +00:00
add(&ret[ss],&lhs[ss],&rhs);
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2015-05-03 09:44:47 +01:00
2018-01-15 00:04:43 +00:00
//////////////////////////////////////////////////////////////////////////////////////////////////////
// avoid copy back routines for mult, mac, sub, add
//////////////////////////////////////////////////////////////////////////////////////////////////////
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void mult(Lattice<obj1> &ret,const obj2 &lhs,const Lattice<obj3> &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = rhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,rhs);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,rhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
mult(&tmp,&lhs,&rhs[ss]);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,rhs,{
2018-01-26 23:06:03 +00:00
mult(&ret[ss],&lhs,&rhs[ss]);
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2015-04-18 20:44:19 +01:00
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void mac(Lattice<obj1> &ret,const obj2 &lhs,const Lattice<obj3> &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = rhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,rhs);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,rhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
mac(&tmp,&lhs,&rhs[ss]);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,rhs,{
2018-01-26 23:06:03 +00:00
mac(&ret[ss],&lhs,&rhs[ss]);
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2015-05-03 09:44:47 +01:00
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void sub(Lattice<obj1> &ret,const obj2 &lhs,const Lattice<obj3> &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = rhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,rhs);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,rhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
sub(&tmp,&lhs,&rhs[ss]);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,rhs,{
2018-01-26 23:06:03 +00:00
sub(&ret[ss],&lhs,&rhs[ss]);
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2018-01-24 13:34:46 +00:00
template<class obj1,class obj2,class obj3> inline
2018-01-15 00:04:43 +00:00
void add(Lattice<obj1> &ret,const obj2 &lhs,const Lattice<obj3> &rhs){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = rhs.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,rhs);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,rhs,{
2018-01-15 00:04:43 +00:00
obj1 tmp;
2018-01-26 23:06:03 +00:00
add(&tmp,&lhs,&rhs[ss]);
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,rhs,{
2018-01-26 23:06:03 +00:00
add(&ret[ss],&lhs,&rhs[ss]);
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2015-04-18 20:44:19 +01:00
2018-01-24 13:34:46 +00:00
template<class sobj,class vobj> inline
2018-01-15 00:04:43 +00:00
void axpy(Lattice<vobj> &ret,sobj a,const Lattice<vobj> &x,const Lattice<vobj> &y){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = x.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,x);
conformable(x,y);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,x,{
2018-01-26 23:06:03 +00:00
vobj tmp = a*x[ss]+y[ss];
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,x,{
2018-01-26 23:06:03 +00:00
ret[ss]=a*x[ss]+y[ss];
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2018-01-24 13:34:46 +00:00
template<class sobj,class vobj> inline
2018-01-15 00:04:43 +00:00
void axpby(Lattice<vobj> &ret,sobj a,sobj b,const Lattice<vobj> &x,const Lattice<vobj> &y){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = x.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,x);
conformable(x,y);
#ifdef STREAMING_STORES
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,x,{
2018-01-26 23:06:03 +00:00
vobj tmp = a*x[ss]+b*y[ss];
vstream(ret[ss],tmp);
2018-01-24 13:34:46 +00:00
});
#else
2018-01-24 13:34:46 +00:00
accelerator_loop(ss,x,{
2018-01-26 23:06:03 +00:00
ret[ss]=a*x[ss]+b*y[ss];
2018-01-24 13:34:46 +00:00
});
#endif
2018-01-15 00:04:43 +00:00
}
2015-05-03 09:44:47 +01:00
2018-01-24 13:34:46 +00:00
template<class sobj,class vobj> inline
2018-01-15 00:04:43 +00:00
RealD axpy_norm(Lattice<vobj> &ret,sobj a,const Lattice<vobj> &x,const Lattice<vobj> &y){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = x.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,x);
conformable(x,y);
axpy(ret,a,x,y);
return norm2(ret);
2015-04-18 20:44:19 +01:00
}
2018-01-24 13:34:46 +00:00
template<class sobj,class vobj> inline
2018-01-15 00:04:43 +00:00
RealD axpby_norm(Lattice<vobj> &ret,sobj a,sobj b,const Lattice<vobj> &x,const Lattice<vobj> &y){
2018-01-26 23:06:03 +00:00
ret.Checkerboard() = x.Checkerboard();
2018-01-15 00:04:43 +00:00
conformable(ret,x);
conformable(x,y);
axpby(ret,a,b,x,y);
return norm2(ret); // FIXME implement parallel norm in ss loop
}
NAMESPACE_END(Grid);
2015-04-18 20:44:19 +01:00
#endif