1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-09 21:50:45 +01:00

Solving a memory leak in Communicator_mpi

This commit is contained in:
Guido Cossu 2017-09-18 14:39:04 +01:00
parent b542d349b8
commit 999c623590
5 changed files with 135 additions and 99 deletions

View File

@ -50,6 +50,9 @@ public:
GridBase(const std::vector<int> & processor_grid) : CartesianCommunicator(processor_grid) {}; GridBase(const std::vector<int> & processor_grid) : CartesianCommunicator(processor_grid) {};
virtual ~GridBase() = default;
// Physics Grid information. // Physics Grid information.
std::vector<int> _simd_layout;// Which dimensions get relayed out over simd lanes. std::vector<int> _simd_layout;// Which dimensions get relayed out over simd lanes.
std::vector<int> _fdimensions;// (full) Global dimensions of array prior to cb removal std::vector<int> _fdimensions;// (full) Global dimensions of array prior to cb removal

View File

@ -93,6 +93,7 @@ public:
// Use a reduced simd grid // Use a reduced simd grid
_ldimensions[d] = _gdimensions[d] / _processors[d]; //local dimensions _ldimensions[d] = _gdimensions[d] / _processors[d]; //local dimensions
//std::cout << _ldimensions[d] << " " << _gdimensions[d] << " " << _processors[d] << std::endl;
assert(_ldimensions[d] * _processors[d] == _gdimensions[d]); assert(_ldimensions[d] * _processors[d] == _gdimensions[d]);
_rdimensions[d] = _ldimensions[d] / _simd_layout[d]; //overdecomposition _rdimensions[d] = _ldimensions[d] / _simd_layout[d]; //overdecomposition
@ -137,6 +138,8 @@ public:
block = block * _rdimensions[d]; block = block * _rdimensions[d];
} }
}; };
virtual ~GridCartesian() = default;
}; };
} }
#endif #endif

View File

@ -152,6 +152,7 @@ class CartesianCommunicator {
// Constructor of any given grid // Constructor of any given grid
//////////////////////////////////////////////// ////////////////////////////////////////////////
CartesianCommunicator(const std::vector<int> &pdimensions_in); CartesianCommunicator(const std::vector<int> &pdimensions_in);
virtual ~CartesianCommunicator();
//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////
// Wraps MPI_Cart routines, or implements equivalent on other impls // Wraps MPI_Cart routines, or implements equivalent on other impls

View File

@ -75,6 +75,13 @@ CartesianCommunicator::CartesianCommunicator(const std::vector<int> &processors)
assert(Size==_Nprocessors); assert(Size==_Nprocessors);
} }
CartesianCommunicator::~CartesianCommunicator(){
if (communicator && !MPI::Is_finalized())
MPI_Comm_free(&communicator);
}
void CartesianCommunicator::GlobalSum(uint32_t &u){ void CartesianCommunicator::GlobalSum(uint32_t &u){
int ierr=MPI_Allreduce(MPI_IN_PLACE,&u,1,MPI_UINT32_T,MPI_SUM,communicator); int ierr=MPI_Allreduce(MPI_IN_PLACE,&u,1,MPI_UINT32_T,MPI_SUM,communicator);
assert(ierr==0); assert(ierr==0);

View File

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