From f7e4661ca0e2534438c372265d1cf3b366402653 Mon Sep 17 00:00:00 2001 From: Michael Marshall Date: Wed, 30 Jan 2019 21:16:09 +0000 Subject: [PATCH] Fixed grid3d leak in PerambLight --- Hadrons/Modules/MDistil/LapEvec.hpp | 3 +- Hadrons/Modules/MDistil/PerambLight.hpp | 56 ++++++++++++++++++------- tests/hadrons/Test_hadrons_distil.cc | 50 ++++++++++++++++++++++ 3 files changed, 92 insertions(+), 17 deletions(-) diff --git a/Hadrons/Modules/MDistil/LapEvec.hpp b/Hadrons/Modules/MDistil/LapEvec.hpp index b42ee4fd..5750ef95 100644 --- a/Hadrons/Modules/MDistil/LapEvec.hpp +++ b/Hadrons/Modules/MDistil/LapEvec.hpp @@ -144,9 +144,8 @@ protected: GridCartesian * gridLD; // Owned by me, so I must delete it GridCartesian * gridHD; // Owned by environment (so I won't delete it) int Nx, Ny, Nz, Nt; - protected: - void Cleanup(void); + virtual void Cleanup(void); }; MODULE_REGISTER_TMP(LapEvec, TLapEvec, MDistil); diff --git a/Hadrons/Modules/MDistil/PerambLight.hpp b/Hadrons/Modules/MDistil/PerambLight.hpp index 500b1bb3..532c007f 100644 --- a/Hadrons/Modules/MDistil/PerambLight.hpp +++ b/Hadrons/Modules/MDistil/PerambLight.hpp @@ -49,7 +49,7 @@ public: // constructor TPerambLight(const std::string name); // destructor - virtual ~TPerambLight(void) {}; + virtual ~TPerambLight(void); // dependency relation virtual std::vector getInput(void); virtual std::vector getOutput(void); @@ -57,6 +57,13 @@ public: virtual void setup(void); // execution virtual void execute(void); +protected: + // These variables are created in setup() and freed in Cleanup() + GridCartesian * grid3d; // Owned by me, so I must delete it + GridCartesian * grid4d; // Owned by environment (so I won't delete it) + +protected: + virtual void Cleanup(void); }; MODULE_REGISTER_TMP(PerambLight, TPerambLight, MDistil); @@ -67,9 +74,16 @@ MODULE_REGISTER_TMP(PerambLight, TPerambLight, MDistil); // constructor ///////////////////////////////////////////////////////////////// template TPerambLight::TPerambLight(const std::string name) -: Module(name) +: grid3d{nullptr}, grid4d{nullptr}, Module(name) {} +// destructor +template +TPerambLight::~TPerambLight(void) +{ + Cleanup(); +}; + // dependencies/products /////////////////////////////////////////////////////// template std::vector TPerambLight::getInput(void) @@ -94,31 +108,32 @@ std::vector TPerambLight::getOutput(void) template void TPerambLight::setup(void) { + Cleanup(); // auto &noise = envGet(std::vector>>, par().noise); - int LI=par().LI; - int SI=par().SI; - int TI=par().TI; - int nnoise=par().nnoise; - int Nt=par().Nt; - int Nt_inv=par().Nt_inv; - int nvec=par().nvec; + const int LI{par().LI}; + const int SI{par().SI}; + //const int TI{par().TI}; + const int nnoise{par().nnoise}; + const int Nt{par().Nt}; + const int Nt_inv{par().Nt_inv}; + const int nvec{par().nvec}; envCreate(Perambulator, getName() + "_perambulator_light", 1, Nt,nvec,LI,nnoise,Nt_inv,SI); envCreate(std::vector, getName() + "_noise", 1, nvec*Ns*Nt*nnoise); - GridCartesian * grid4d = env().getGrid(); - std::vector latt_size = GridDefaultLatt(); + grid4d = env().getGrid(); + /*std::vector latt_size = GridDefaultLatt(); std::vector simd_layout = GridDefaultSimd(Nd, vComplex::Nsimd()); std::vector mpi_layout = GridDefaultMpi(); std::vector simd_layout_3 = GridDefaultSimd(Nd-1, vComplex::Nsimd()); latt_size[Nd-1] = 1; simd_layout_3.push_back( 1 ); - mpi_layout[Nd-1] = 1; - GridCartesian * grid3d = new GridCartesian(latt_size,simd_layout_3,mpi_layout,*grid4d); + mpi_layout[Nd-1] = 1;*/ + grid3d = MakeLowerDimGrid(grid4d);//new GridCartesian(latt_size,simd_layout_3,mpi_layout,*grid4d); envTmp(LatticeSpinColourVector, "dist_source",1,LatticeSpinColourVector(grid4d)); envTmp(LatticeSpinColourVector, "tmp2",1,LatticeSpinColourVector(grid4d)); @@ -134,6 +149,17 @@ void TPerambLight::setup(void) } +// clean up any temporaries created by setup (that aren't stored in the environment) +template +void TPerambLight::Cleanup(void) +{ + if( grid3d != nullptr ) { + delete grid3d; + grid3d = nullptr; + } + grid4d = nullptr; +} + // execution /////////////////////////////////////////////////////////////////// template void TPerambLight::execute(void) @@ -144,7 +170,7 @@ void TPerambLight::execute(void) auto &perambulator = envGet(Perambulator, getName() + "_perambulator_light"); auto &epack = envGet(Grid::Hadrons::EigenPack, par().eigenPack); - GridCartesian * grid4d = env().getGrid(); + /*GridCartesian * grid4d = env().getGrid(); std::vector latt_size = GridDefaultLatt(); std::vector simd_layout = GridDefaultSimd(Nd, vComplex::Nsimd()); std::vector mpi_layout = GridDefaultMpi(); @@ -152,7 +178,7 @@ void TPerambLight::execute(void) latt_size[Nd-1] = 1; simd_layout_3.push_back( 1 ); mpi_layout[Nd-1] = 1; - GridCartesian * grid3d = new GridCartesian(latt_size,simd_layout_3,mpi_layout,*grid4d); + GridCartesian * grid3d = new GridCartesian(latt_size,simd_layout_3,mpi_layout,*grid4d);*/ LatticeGaugeField Umu(grid4d); FieldMetaData header; diff --git a/tests/hadrons/Test_hadrons_distil.cc b/tests/hadrons/Test_hadrons_distil.cc index 47939090..82a6e3d0 100644 --- a/tests/hadrons/Test_hadrons_distil.cc +++ b/tests/hadrons/Test_hadrons_distil.cc @@ -171,8 +171,58 @@ bool bNumber( int &ri, const char * & pstr, bool bGobbleWhiteSpace = true ) return true; } +#ifdef DEBUG + +typedef Eigen::Tensor MyTensor; + +bool DebugEigenTest() +{ + MyTensor x(2,3,4); + const MyTensor::Index s{x.size()}; + std::cout << "x.size() = " << s << std::endl; + std::cout << "x.NumDimensions = " << x.NumDimensions << " (TensorBase)" << std::endl; + std::cout << "x.NumIndices = " << x.NumIndices << std::endl; + const MyTensor::Dimensions & d{x.dimensions()}; + std::cout << "d.size() = " << d.size() << std::endl; + std::cout << "Dimensions are "; + for(auto i : d ) std::cout << "[" << i << "]"; + std::cout << std::endl; + MyTensor::Index SizeCalculated{1}; + std::cout << "Dimensions again"; + for(int i=0 ; i < d.size() ; i++ ) { + std::cout << " : [" << i << "]=" << d[i]; + SizeCalculated *= d[i]; + } + std::cout << std::endl; + std::cout << "SizeCalculated = " << SizeCalculated << std::endl;\ + assert( SizeCalculated == s ); + // Initialise + assert( d.size() == 3 ); + for( int i = 0 ; i < d[0] ; i++ ) + for( int j = 0 ; j < d[1] ; j++ ) + for( int k = 0 ; k < d[2] ; k++ ) { + x(i,j,k) = std::complex(SizeCalculated, SizeCalculated); + SizeCalculated--; + } + // Show raw data + std::cout << "Data follow : " << std::endl; + Complex * p = x.data(); + for( auto i = 0 ; i < s ; i++ ) { + if( i ) std::cout << ", "; + std::cout << "x.data()[" << i << "]=" << * p++; + } + std::cout << std::endl; + return true; +} +#endif + int main(int argc, char *argv[]) { +#ifdef DEBUG + // Debug only - test of Eigen::Tensor + //if( DebugEigenTest() ) return 0; +#endif + // Decode command-line parameters. 1st one is which test to run int iTestNum = -1;