From cba8e8237ae754cd842e4440a1d555c7be3fbbfa Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 13 Aug 2021 06:25:21 -0400 Subject: [PATCH] avoid memory leak with MPI-IO --- src/read_restart.cpp | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/read_restart.cpp b/src/read_restart.cpp index faf9fd1620..e42bb59272 100644 --- a/src/read_restart.cpp +++ b/src/read_restart.cpp @@ -44,7 +44,7 @@ using namespace LAMMPS_NS; /* ---------------------------------------------------------------------- */ -ReadRestart::ReadRestart(LAMMPS *lmp) : Command(lmp) {} +ReadRestart::ReadRestart(LAMMPS *lmp) : Command(lmp), mpiio(nullptr) {} /* ---------------------------------------------------------------------- */ @@ -110,8 +110,7 @@ void ReadRestart::command(int narg, char **arg) } fp = fopen(hfile.c_str(),"rb"); if (fp == nullptr) - error->one(FLERR,"Cannot open restart file {}: {}", - hfile, utils::getsyserror()); + error->one(FLERR,"Cannot open restart file {}: {}", hfile, utils::getsyserror()); } // read magic string, endian flag, format revision @@ -336,8 +335,7 @@ void ReadRestart::command(int narg, char **arg) procfile.replace(procfile.find("%"),1,fmt::format("{}",icluster)); fp = fopen(procfile.c_str(),"rb"); if (fp == nullptr) - error->one(FLERR,"Cannot open restart file {}: {}", - procfile, utils::getsyserror()); + error->one(FLERR,"Cannot open restart file {}: {}", procfile, utils::getsyserror()); } int procsperfile; @@ -401,7 +399,7 @@ void ReadRestart::command(int narg, char **arg) // clean-up memory - delete [] file; + delete[] file; memory->destroy(buf); // for multiproc or MPI-IO files: @@ -525,8 +523,9 @@ void ReadRestart::command(int narg, char **arg) MPI_Barrier(world); if (comm->me == 0) - utils::logmesg(lmp," read_restart CPU = {:.3f} seconds\n", - MPI_Wtime()-time1); + utils::logmesg(lmp," read_restart CPU = {:.3f} seconds\n",MPI_Wtime()-time1); + + delete mpiio; } /* ---------------------------------------------------------------------- @@ -599,7 +598,7 @@ void ReadRestart::header() if (me == 0) utils::logmesg(lmp," restart file = {}, LAMMPS = {}\n", version,lmp->version); - delete [] version; + delete[] version; // we have no forward compatibility, thus exit with error @@ -638,7 +637,7 @@ void ReadRestart::header() } else if (flag == UNITS) { char *style = read_string(); if (strcmp(style,update->unit_style) != 0) update->set_units(style); - delete [] style; + delete[] style; } else if (flag == NTIMESTEP) { update->ntimestep = read_bigint(); @@ -772,9 +771,9 @@ void ReadRestart::header() atom->create_avec(style,nargcopy,argcopy,1); if (comm->me ==0) utils::logmesg(lmp," restoring atom style {} from restart\n",style); - for (int i = 0; i < nargcopy; i++) delete [] argcopy[i]; - delete [] argcopy; - delete [] style; + for (int i = 0; i < nargcopy; i++) delete[] argcopy[i]; + delete[] argcopy; + delete[] style; } else if (flag == NATOMS) { atom->natoms = read_bigint(); @@ -889,7 +888,7 @@ void ReadRestart::type_arrays() double *mass = new double[atom->ntypes+1]; read_double_vec(atom->ntypes,&mass[1]); atom->set_mass(mass); - delete [] mass; + delete[] mass; } else error->all(FLERR, "Invalid flag in type arrays section of restart file"); @@ -910,7 +909,7 @@ void ReadRestart::force_fields() if (flag == PAIR) { style = read_string(); force->create_pair(style,1); - delete [] style; + delete[] style; if (comm->me ==0) utils::logmesg(lmp," restoring pair style {} from restart\n", force->pair_style); @@ -926,7 +925,7 @@ void ReadRestart::force_fields() } else if (flag == BOND) { style = read_string(); force->create_bond(style,1); - delete [] style; + delete[] style; if (comm->me ==0) utils::logmesg(lmp," restoring bond style {} from restart\n", force->bond_style); @@ -935,7 +934,7 @@ void ReadRestart::force_fields() } else if (flag == ANGLE) { style = read_string(); force->create_angle(style,1); - delete [] style; + delete[] style; if (comm->me ==0) utils::logmesg(lmp," restoring angle style {} from restart\n", force->angle_style); @@ -944,7 +943,7 @@ void ReadRestart::force_fields() } else if (flag == DIHEDRAL) { style = read_string(); force->create_dihedral(style,1); - delete [] style; + delete[] style; if (comm->me ==0) utils::logmesg(lmp," restoring dihedral style {} from restart\n", force->dihedral_style); @@ -953,7 +952,7 @@ void ReadRestart::force_fields() } else if (flag == IMPROPER) { style = read_string(); force->create_improper(style,1); - delete [] style; + delete[] style; if (comm->me ==0) utils::logmesg(lmp," restoring improper style {} from restart\n", force->improper_style); @@ -1117,7 +1116,7 @@ void ReadRestart::magic_string() MPI_Bcast(str,n,MPI_CHAR,0,world); if (strcmp(str,MAGIC_STRING) != 0) error->all(FLERR,"Invalid LAMMPS restart file"); - delete [] str; + delete[] str; } /* ---------------------------------------------------------------------- @@ -1164,7 +1163,7 @@ void ReadRestart::check_eof_magic() if (strcmp(str,MAGIC_STRING) != 0) error->all(FLERR,"Incomplete or corrupted LAMMPS restart file"); - delete [] str; + delete[] str; } /* ----------------------------------------------------------------------