Skip to content

Commit

Permalink
Fixed numerous memory leaks and removed unused fix
Browse files Browse the repository at this point in the history
  • Loading branch information
abkein committed Dec 27, 2024
1 parent dc33743 commit a6c4789
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 345 deletions.
16 changes: 8 additions & 8 deletions src/compute_cluster_ke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ ComputeClusterKE::ComputeClusterKE(LAMMPS* lmp, int narg, char** arg) : Compute(
auto computes = lmp->modify->get_compute_by_style("ke/atom");
if (computes.empty()) { error->all(FLERR, "compute {}: Cannot find compute with style 'ke/atom'", style); }
compute_ke_atom = computes[0];

size_local_rows = size_cutoff + 1;
local_kes.create(memory, size_local_rows, "compute:ke/cluster:local_kes");
vector_local = local_kes.data();

size_vector = size_cutoff + 1;
kes.create(memory, size_vector, "compute:ke/cluster:kes");
vector = kes.data();
}

/* ---------------------------------------------------------------------- */
Expand All @@ -95,14 +103,6 @@ ComputeClusterKE::~ComputeClusterKE() noexcept(true)
void ComputeClusterKE::init()
{
if ((modify->get_compute_by_style(style).size() > 1) && (comm->me == 0)) { error->warning(FLERR, "More than one compute {}", style); }

size_local_rows = size_cutoff + 1;
local_kes.create(memory, size_local_rows, "compute:ke/cluster:local_kes");
vector_local = local_kes.data();

size_vector = size_cutoff + 1;
kes.create(memory, size_vector, "compute:ke/cluster:kes");
vector = kes.data();
}

/* ---------------------------------------------------------------------- */
Expand Down
16 changes: 8 additions & 8 deletions src/compute_cluster_pe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ ComputeClusterPE::ComputeClusterPE(LAMMPS* lmp, int narg, char** arg) : Compute(
auto computes = lmp->modify->get_compute_by_style("pe/atom");
if (computes.empty()) { error->all(FLERR, "compute {}: Cannot find compute with style 'pe/atom'", style); }
compute_pe_atom = computes[0];

size_local_rows = size_cutoff + 1;
local_pes.create(memory, size_local_rows, "compute:pe/cluster:local_pes");
vector_local = local_pes.data();

size_vector = size_cutoff + 1;
pes.create(memory, size_vector, "compute:pe/cluster:pes");
vector = pes.data();
}

/* ---------------------------------------------------------------------- */
Expand All @@ -77,14 +85,6 @@ ComputeClusterPE::~ComputeClusterPE() noexcept(true)
void ComputeClusterPE::init()
{
if ((modify->get_compute_by_style(style).size() > 1) && (comm->me == 0)) { error->warning(FLERR, "More than one compute {}", style); }

size_local_rows = size_cutoff + 1;
local_pes.create(memory, size_local_rows, "compute:pe/cluster:local_pes");
vector_local = local_pes.data();

size_vector = size_cutoff + 1;
pes.create(memory, size_vector, "compute:pe/cluster:pes");
vector = pes.data();
}

/* ---------------------------------------------------------------------- */
Expand Down
77 changes: 43 additions & 34 deletions src/compute_cluster_size_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ ComputeClusterSizeExt::ComputeClusterSizeExt(LAMMPS* lmp, int narg, char** arg)
if (size_cutoff < 1) { error->all(FLERR, "{}: size_cutoff must be greater than 0", style); }

size_vector = size_cutoff + 1;

// keeper1 = new MemoryKeeper(memory);
// cluster_map_allocator = new MapAlloc_t<int, int>(keeper1);
// cluster_map = new Map_t<int, int>(*cluster_map_allocator);

// keeper2 = new MemoryKeeper(memory);
// alloc_map_vec1 = new MapAlloc_t<int, Vec_t<int>>(keeper2);
// cIDs_by_size = new Map_t<int, Vec_t<int>>(*alloc_map_vec1);

// keeper3 = new MemoryKeeper(memory);
// alloc_map_vec2 = new MapAlloc_t<int, Vec_t<int>>(keeper3);
// cIDs_by_size_all = new Map_t<int, Vec_t<int>>(*alloc_map_vec2);

counts_global.create(memory, comm->nprocs, "size/cluster/ext:counts_global");
displs.create(memory, comm->nprocs, "size/cluster/ext:displs");

dist_local.create(memory, size_vector, "size/cluster/ext:dist_local");
dist.create(memory, size_vector, "size/cluster/ext:dist");
vector = dist.data();

cIDs_by_size.reserve(size_cutoff);
cIDs_by_size_all.reserve(size_cutoff);
}

/* ---------------------------------------------------------------------- */
Expand Down Expand Up @@ -92,43 +114,31 @@ void ComputeClusterSizeExt::init()
{
if ((modify->get_compute_by_style(style).size() > 1) && (comm->me == 0)) { error->warning(FLERR, "More than one compute {}", style); }

// keeper1 = new MemoryKeeper(memory);
// cluster_map_allocator = new MapAlloc_t<int, int>(keeper1);
// cluster_map = new Map_t<int, int>(*cluster_map_allocator);

// keeper2 = new MemoryKeeper(memory);
// alloc_map_vec1 = new MapAlloc_t<int, Vec_t<int>>(keeper2);
// cIDs_by_size = new Map_t<int, Vec_t<int>>(*alloc_map_vec1);

// keeper3 = new MemoryKeeper(memory);
// alloc_map_vec2 = new MapAlloc_t<int, Vec_t<int>>(keeper3);
// cIDs_by_size_all = new Map_t<int, Vec_t<int>>(*alloc_map_vec2);

counts_global.create(memory, comm->nprocs, "size/cluster/ext:counts_global");
displs.create(memory, comm->nprocs, "size/cluster/ext:displs");

dist_local.create(memory, size_vector, "size/cluster/ext:dist_local");
dist.create(memory, size_vector, "size/cluster/ext:dist");
vector = dist.data();
if ((nloc < atom->nlocal) || (ns.empty() && clusters.empty() && monomers.empty())) {
nloc = static_cast<int>(atom->nlocal * LMP_NUCC_ALLOC_COEFF);
cluster_map.reserve(nloc);

nloc = static_cast<int>(atom->nlocal * LMP_NUCC_ALLOC_COEFF);
// keeper1->pool_size<MapMember_t<int, int>>(nloc);
// keeper2->pool_size<MapMember_t<int, Vec_t<int>>>(nloc);
// keeper3->pool_size<MapMember_t<int, Vec_t<int>>>(nloc);
// keeper1->pool_size<MapMember_t<int, int>>(nloc);
// keeper2->pool_size<MapMember_t<int, Vec_t<int>>>(nloc);
// keeper3->pool_size<MapMember_t<int, Vec_t<int>>>(nloc);

cluster_map.reserve(nloc);
cIDs_by_size.reserve(size_cutoff);
cIDs_by_size_all.reserve(size_cutoff);
clusters.grow(memory, nloc, "size/cluster/ext:clusters");
ns.grow(memory, 2 * nloc, "size/cluster/ext:ns");
monomers.grow(memory, nloc, "size/cluster/ext:monomers");
}

clusters.create(memory, nloc, "size/cluster/ext:clusters");
ns.create(memory, 2 * nloc, "size/cluster/ext:ns");
monomers.create(memory, nloc, "size/cluster/ext:monomers");
if (ns.empty() || clusters.empty() || monomers.empty()) { error->one(FLERR, "{}: Inconsistent arrays state", style); }

natom_loc = static_cast<bigint>(static_cast<long double>(2 * atom->natoms) * LMP_NUCC_ALLOC_COEFF);
gathered.create(memory, natom_loc, "size/cluster/ext:gathered");
if ((gathered.empty()) || (natom_loc < 2 * atom->natoms)) {
natom_loc = static_cast<bigint>(static_cast<long double>(2 * atom->natoms) * LMP_NUCC_ALLOC_COEFF);
gathered.grow(memory, natom_loc, "size/cluster/ext:gathered");
}

nloc_peratom = static_cast<int>(atom->nlocal * LMP_NUCC_ALLOC_COEFF);
peratom_size.create(memory, nloc_peratom, "size/cluster/ext:peratom");
if ((peratom_size.empty()) || (nloc_peratom < atom->nlocal)) {
nloc_peratom = static_cast<int>(atom->nlocal * LMP_NUCC_ALLOC_COEFF);
peratom_size.grow(memory, nloc_peratom, "size/cluster/ext:peratom");
vector_atom = peratom_size.data();
}

initialized_flag = 1;
}
Expand All @@ -142,7 +152,6 @@ void ComputeClusterSizeExt::compute_vector()
auto& cmap = cluster_map;
auto& cbs = cIDs_by_size;
auto& cbs_all = cIDs_by_size_all;
// auto &cmap = cluster_map;

if (compute_cluster_atom->invoked_peratom != update->ntimestep) { compute_cluster_atom->compute_peratom(); }

Expand Down Expand Up @@ -228,7 +237,7 @@ void ComputeClusterSizeExt::compute_vector()
}

if (nloc_peratom < atom->nlocal) {
nloc_peratom = atom->nlocal;
nloc_peratom = static_cast<int>(atom->nlocal * LMP_NUCC_ALLOC_COEFF);
peratom_size.grow(memory, nloc_peratom, "size/cluster/ext:peratom");
peratom_size.reset();
vector_atom = peratom_size.data();
Expand Down
12 changes: 6 additions & 6 deletions src/compute_cluster_size_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ class ComputeClusterSizeExt : public Compute {
// NUCC::Map_t<int, NUCC::Vec_t<int>> *cIDs_by_size_all;
std::unordered_map<int, std::vector<int>> cIDs_by_size_all;

int nloc; // number of reserved elements in atoms_by_cID and cIDs_by_size
int nloc = 0; // number of reserved elements in atoms_by_cID and cIDs_by_size
NUCC::cspan<double> dist; // cluster size distribution (vector == dist)
NUCC::cspan<double> dist_local; // local cluster size distribution
int nc_global; // number of clusters total
int nc_global = 0; // number of clusters total
NUCC::cspan<int> counts_global;
NUCC::cspan<int> displs;
NUCC::cspan<NUCC::cluster_data> clusters;
NUCC::cspan<int> ns;
NUCC::cspan<int> gathered;
NUCC::cspan<double> peratom_size;
bigint natom_loc;
int nonexclusive;
int nloc_peratom;
bigint natom_loc = 0;
int nonexclusive = 0;
int nloc_peratom = 0;

NUCC::cspan<int> monomers;
int nmono;
int nmono = 0;

Compute *compute_cluster_atom = nullptr;
};
Expand Down
16 changes: 8 additions & 8 deletions src/compute_cluster_te.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ ComputeClusterTE::ComputeClusterTE(LAMMPS* lmp, int narg, char** arg) : Compute(
style);
}
}

size_local_rows = size_cutoff + 1;
local_tes.create(memory, size_local_rows, "compute:te/cluster:local_tes");
vector_local = local_tes.data();

size_vector = size_cutoff + 1;
tes.create(memory, size_vector, "compute:te/cluste:tes");
vector = tes.data();
}

/* ---------------------------------------------------------------------- */
Expand All @@ -80,14 +88,6 @@ ComputeClusterTE::~ComputeClusterTE() noexcept(true)
void ComputeClusterTE::init()
{
if ((modify->get_compute_by_style(style).size() > 1) && (comm->me == 0)) { error->warning(FLERR, "More than one compute {}", style); }

size_local_rows = size_cutoff + 1;
local_tes.create(memory, size_local_rows, "compute:te/cluster:local_tes");
vector_local = local_tes.data();

size_vector = size_cutoff + 1;
tes.create(memory, size_vector, "compute:te/cluste:tes");
vector = tes.data();
}

/* ---------------------------------------------------------------------- */
Expand Down
8 changes: 4 additions & 4 deletions src/compute_cluster_temps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ ComputeClusterTemp::ComputeClusterTemp(LAMMPS *lmp, int narg, char **arg) : Comp
}
size_cutoff = MIN(size_cutoff, t_size_cutoff);
}

size_vector = size_cutoff + 1;
temp.create(memory, size_vector, "temp/cluster:temp");
vector = temp.data();
}

/* ---------------------------------------------------------------------- */
Expand All @@ -77,10 +81,6 @@ void ComputeClusterTemp::init()
if ((modify->get_compute_by_style(style).size() > 1) && (comm->me == 0)) {
error->warning(FLERR, "More than one compute {}", style);
}

size_vector = size_cutoff + 1;
temp.create(memory, size_vector, "temp/cluster:temp");
vector = temp.data();
}

/* ---------------------------------------------------------------------- */
Expand Down
30 changes: 14 additions & 16 deletions src/compute_cluster_volume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ ComputeClusterVolume::ComputeClusterVolume(LAMMPS *lmp, int narg, char **arg) :

size_local_rows = size_cutoff + 1;
size_vector = size_cutoff + 1;

dist_local.create(memory, size_local_rows, "compute:cluster/ke:local_kes");
vector_local = dist_local.data();
dist.create(memory, size_vector, "compute:cluster/ke:kes");
vector = dist.data();
dist_global.create(memory, size_vector * comm->nprocs, "dist_global");
}

/* ---------------------------------------------------------------------- */
Expand Down Expand Up @@ -137,22 +143,15 @@ void ComputeClusterVolume::init()
subbonds[4 + 0] = domain->sublo[2];
subbonds[4 + 1] = domain->subhi[2];

// memory->create(dist_local, size_local_rows, "compute:cluster/ke:local_kes");
dist_local.create(memory, size_local_rows, "compute:cluster/ke:local_kes");
vector_local = dist_local.data();
// memory->create(dist, size_vector, "compute:cluster/ke:kes");
dist.create(memory, size_vector, "compute:cluster/ke:kes");
vector = dist.data();
dist_global.create(memory, size_vector * comm->nprocs, "dist_global");

nloc = static_cast<int>(atom->nlocal * LMP_NUCC_ALLOC_COEFF);
// memory->create(volumes, nloc, "ComputeClusterVolume:volumes");
volumes.create(memory, nloc, "ComputeClusterVolume:volumes");
if (mode == VOLUMEMODE::RECTANGLE) {
// memory->grow(bboxes, 6 * nloc, "ComputeClusterVolume:bboxes");
bboxes.create(memory, 6 * nloc, "ComputeClusterVolume:bboxes");
if ((nloc < atom->nlocal) || volumes.empty()) {
nloc = static_cast<int>(atom->nlocal * LMP_NUCC_ALLOC_COEFF);
volumes.grow(memory, nloc, "ComputeClusterVolume:volumes");
if (mode == VOLUMEMODE::RECTANGLE) {
bboxes.create(memory, 6 * nloc, "ComputeClusterVolume:bboxes");
}
}


// memory->create(recv_comm_matrix_local, comm->nprocs, "recv_comm_matrix_local");
// memory->create(recv_comm_matrix_global, comm->nprocs * comm->nprocs, "recv_comm_matrix_global");
// memory->create(send_comm_matrix_local, comm->nprocs, "send_comm_matrix_local");
Expand All @@ -162,8 +161,7 @@ void ComputeClusterVolume::init()
const auto radius_voxels_int = static_cast<int>(std::ceil(overlap / voxel_size));
noff =
3 * 2 * (radius_voxels_int + 1) * 2 * (radius_voxels_int + 1) * 2 * (radius_voxels_int + 1);
// memory->create(offsets, noff, "ComputeClusterVolume:offsets");
offsets.create(memory, noff, "ComputeClusterVolume:offsets");
if (offsets.empty()) { offsets.create(memory, noff, "ComputeClusterVolume:offsets"); }

double const voxel_size_sq = voxel_size * voxel_size;
noffsets = 0;
Expand Down
9 changes: 5 additions & 4 deletions src/fix_capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ FixCapture::FixCapture(LAMMPS* lmp, int narg, char** arg) : Fix(lmp, narg, arg),
fmt::print(fp, "ntimestep,n\n");
::fflush(fp);
}

memory->create(rmins, atom->ntypes + 1, atom->ntypes + 1, "fix_capture:rmins");
memory->create(vmax_coeffs, atom->ntypes + 1, "fix_capture:vmax_coeffs");
memory->create(sigmas, atom->ntypes + 1, "fix_capture:sigmas");

}

/* ---------------------------------------------------------------------- */
Expand Down Expand Up @@ -153,10 +158,6 @@ void FixCapture::init()
if (atom->mass_setflag[i] == 0) { error->all(FLERR, "fix capture: mass is not set for atom type {}.", i); }
}

memory->create(rmins, atom->ntypes + 1, atom->ntypes + 1, "fix_capture:rmins");
memory->create(vmax_coeffs, atom->ntypes + 1, "fix_capture:vmax_coeffs");
memory->create(sigmas, atom->ntypes + 1, "fix_capture:sigmas");

constexpr long double eight_over_pi_sqrt = 1.5957691216057307117597842397375L; // sqrt(8/pi)
constexpr long double ssdd = 0.6734396116428514837424685996751L; // sqrt(3-8/pi)
for (int i = 1; i <= atom->ntypes; ++i) {
Expand Down
8 changes: 4 additions & 4 deletions src/fix_capture_vel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ FixCaptureVel::FixCaptureVel(LAMMPS* lmp, int narg, char** arg) : Fix(lmp, narg,

ncaptured[0] = 0;
ncaptured[1] = 0;

memory->create(rmins, atom->ntypes + 1, atom->ntypes + 1, "fix_capture:rmins");
memory->create(vmax_coeffs, atom->ntypes + 1, "fix_capture:vmax_coeffs");
memory->create(sigmas, atom->ntypes + 1, "fix_capture:sigmas");
}

/* ---------------------------------------------------------------------- */
Expand Down Expand Up @@ -142,10 +146,6 @@ void FixCaptureVel::init()
if (atom->mass_setflag[i] == 0) { error->all(FLERR, "{}: mass is not set for atom type {}.", style, i); }
}

memory->create(rmins, atom->ntypes + 1, atom->ntypes + 1, "fix_capture:rmins");
memory->create(vmax_coeffs, atom->ntypes + 1, "fix_capture:vmax_coeffs");
memory->create(sigmas, atom->ntypes + 1, "fix_capture:sigmas");

constexpr long double eight_over_pi_sqrt = 1.5957691216057307117597842397375L; // sqrt(8/pi)
constexpr long double ssdd = 0.6734396116428514837424685996751L; // sqrt(3-8/pi)
for (int i = 1; i <= atom->ntypes; ++i) {
Expand Down
12 changes: 7 additions & 5 deletions src/fix_cluster_crush_delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ FixClusterCrushDelete::FixClusterCrushDelete(LAMMPS* lmp, int narg, char** arg)
fmt::print(fp, "ntimestep,ntotal,cc,ad,added,tr\n");
::fflush(fp);
}

pproc.create(memory, comm->nprocs, "cluster/crush/delete:pproc");
c2c.create(memory, comm->nprocs, "cluster/crush/delete:c2c");
}

/* ---------------------------------------------------------------------- */
Expand Down Expand Up @@ -324,11 +327,10 @@ void FixClusterCrushDelete::init()
{
if ((modify->get_fix_by_style(style).size() > 1) && (comm->me == 0)) { error->warning(FLERR, "More than one fix {}", style); }

pproc.create(memory, comm->nprocs, "cluster/crush/delete:pproc");
c2c.create(memory, comm->nprocs, "cluster/crush/delete:c2c");

nloc = atom->nlocal;
p2m.create(memory, nloc, "cluster/crush/delete:p2m");
if ((p2m.empty()) || (nloc < atom->nlocal)) {
nloc = atom->nlocal;
p2m.grow(memory, nloc, "cluster/crush/delete:p2m");
}
}

/* ---------------------------------------------------------------------- */
Expand Down
Loading

0 comments on commit a6c4789

Please sign in to comment.