1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-09-20 01:05:38 +01:00

Improving efficiency of the force term

This commit is contained in:
Guido Cossu 2017-03-15 15:16:16 +09:00
parent 831ca4e3bf
commit 38806343a8
3 changed files with 104 additions and 41 deletions

View File

@ -30,17 +30,34 @@ directory
#ifndef SCALAR_INT_ACTION_H
#define SCALAR_INT_ACTION_H
// Note: this action can completely absorb the ScalarAction for real float fields
// use the scalarObjs to generalise the structure
namespace Grid {
// FIXME drop the QCD namespace everywhere here
template <class Impl>
class ScalarInteractionAction : public QCD::Action<typename Impl::Field> {
public:
INHERIT_FIELD_TYPES(Impl);
private:
RealD mass_square;
RealD lambda;
typedef typename Field::vector_object vobj;
typedef CartesianStencil<vobj,vobj> Stencil;
SimpleCompressor<vobj> compressor;
int npoint = 8;
std::vector<int> directions = {0,1,2,3,0,1,2,3}; // forcing 4 dimensions
std::vector<int> displacements = {1,1,1,1, -1,-1,-1,-1};
public:
INHERIT_FIELD_TYPES(Impl);
ScalarInteractionAction(RealD ms, RealD l) : mass_square(ms), lambda(l) {}
ScalarInteractionAction(RealD ms, RealD l) : mass_square(ms), lambda(l){}
virtual std::string LogParameters() {
std::stringstream sstream;
@ -51,27 +68,75 @@ class ScalarInteractionAction : public QCD::Action<typename Impl::Field> {
virtual std::string action_name() {return "ScalarAction";}
virtual void refresh(const Field &U,
GridParallelRNG &pRNG) {} // noop as no pseudoferms
virtual void refresh(const Field &U, GridParallelRNG &pRNG) {}
virtual RealD S(const Field &p) {
Field action(p._grid);
Field pshift(p._grid);
Field phisquared(p._grid);
static Stencil phiStencil(p._grid, npoint, 0, directions, displacements);
phiStencil.HaloExchange(p, compressor);
Field action(p._grid), pshift(p._grid), phisquared(p._grid);
phisquared = p*p;
action = (2.0*QCD::Nd + mass_square)*phisquared + lambda*phisquared*phisquared;
for (int mu = 0; mu < QCD::Nd; mu++) {
pshift = Cshift(p, mu, +1); // not efficient implement with stencils
action -= pshift*p + p*pshift;
// pshift = Cshift(p, mu, +1); // not efficient, implement with stencils
PARALLEL_FOR_LOOP
for (int i = 0; i < p._grid->oSites(); i++) {
int permute_type;
StencilEntry *SE;
vobj temp2;
vobj *temp;
vobj *t_p;
SE = phiStencil.GetEntry(permute_type, mu, i);
t_p = &p._odata[i];
if ( SE->_is_local ) {
temp = &p._odata[SE->_offset];
if ( SE->_permute ) {
permute(temp2, *temp, permute_type);
action._odata[i] -= temp2*(*t_p) + (*t_p)*temp2;
} else {
action._odata[i] -= *temp*(*t_p) + (*t_p)*(*temp);
}
} else {
action._odata[i] -= phiStencil.CommBuf()[SE->_offset]*(*t_p) + (*t_p)*phiStencil.CommBuf()[SE->_offset];
}
}
// action -= pshift*p + p*pshift;
}
// NB the trace in the algebra is normalised to 1/2
// minus sign coming from the antihermitian fields
return -(TensorRemove(sum(trace(action)))).real();
};
virtual void deriv(const Field &p,
Field &force) {
virtual void deriv(const Field &p, Field &force) {
force = (2.0*QCD::Nd + mass_square)*p + 2.0*lambda*p*p*p;
// following is inefficient
for (int mu = 0; mu < QCD::Nd; mu++) force -= Cshift(p, mu, -1) + Cshift(p, mu, 1);
// move this outside
static Stencil phiStencil(p._grid, npoint, 0, directions, displacements);
phiStencil.HaloExchange(p, compressor);
//for (int mu = 0; mu < QCD::Nd; mu++) force -= Cshift(p, mu, -1) + Cshift(p, mu, 1);
for (int point = 0; point < npoint; point++) {
PARALLEL_FOR_LOOP
for (int i = 0; i < p._grid->oSites(); i++) {
vobj *temp;
vobj temp2;
int permute_type;
StencilEntry *SE;
SE = phiStencil.GetEntry(permute_type, point, i);
if ( SE->_is_local ) {
temp = &p._odata[SE->_offset];
if ( SE->_permute ) {
permute(temp2, *temp, permute_type);
force._odata[i] -= temp2;
} else {
force._odata[i] -= *temp;
}
} else {
force._odata[i] -= phiStencil.CommBuf()[SE->_offset];
}
}
}
}
};

View File

@ -33,9 +33,8 @@ using namespace std;
using namespace Grid;
using namespace Grid::QCD;
int main (int argc, char ** argv)
{
Grid_init(&argc,&argv);
int main(int argc, char ** argv) {
Grid_init(&argc, &argv);
// typedef LatticeColourMatrix Field;
typedef LatticeComplex Field;

View File

@ -26,7 +26,7 @@ See the full license in the file "LICENSE" in the top level distribution directo
*************************************************************************************/
/* END LEGAL */
#include <Grid/Grid.h>
namespace Grid{
namespace Grid {
class ScalarActionParameters : Serializable {
public:
GRID_SERIALIZABLE_CLASS_MEMBERS(ScalarActionParameters,
@ -44,7 +44,7 @@ int main(int argc, char **argv) {
// here make a routine to print all the relevant information on the run
std::cout << GridLogMessage << "Grid is setup to use " << threads << " threads" << std::endl;
// Typedefs to simplify notation
// Typedefs to simplify notation
typedef ScalarAdjGenericHMCRunner HMCWrapper; // Uses the default minimum norm, real scalar fields
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@ -52,7 +52,7 @@ int main(int argc, char **argv) {
// Grid from the command line
GridModule ScalarGrid;
ScalarGrid.set_full( SpaceTimeGrid::makeFourDimGrid(
ScalarGrid.set_full(SpaceTimeGrid::makeFourDimGrid(
GridDefaultLatt(), GridDefaultSimd(Nd, vComplex::Nsimd()),
GridDefaultMpi()));
ScalarGrid.set_rb(SpaceTimeGrid::makeFourDimRedBlackGrid(ScalarGrid.get_full()));
@ -89,12 +89,11 @@ int main(int argc, char **argv) {
/////////////////////////////////////////////////////////////
// HMC parameters are serialisable
TheHMC.Parameters.MD.MDsteps = 10;
TheHMC.Parameters.MD.MDsteps = 20;
TheHMC.Parameters.MD.trajL = 1.0;
TheHMC.ReadCommandLine(argc, argv);
TheHMC.Run();
Grid_finalize();
} // main
} // main