Resolved memory management issue exposed by RCB in in.grid.test
This commit is contained in:
@ -60,13 +60,7 @@ ComputeGrid::ComputeGrid(LAMMPS *lmp, int narg, char **arg) :
|
||||
|
||||
ComputeGrid::~ComputeGrid()
|
||||
{
|
||||
memory->destroy(grid);
|
||||
memory->destroy(gridall);
|
||||
memory->destroy(local_flags);
|
||||
if (gridlocal_allocated) {
|
||||
gridlocal_allocated = 0;
|
||||
memory->destroy4d_offset(gridlocal,nzlo,nylo,nxlo);
|
||||
}
|
||||
deallocate();
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
@ -79,6 +73,7 @@ void ComputeGrid::init()
|
||||
|
||||
void ComputeGrid::setup()
|
||||
{
|
||||
deallocate();
|
||||
set_grid_global();
|
||||
set_grid_local();
|
||||
allocate();
|
||||
@ -195,21 +190,13 @@ void ComputeGrid::assign_local_flags()
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
free and reallocate arrays
|
||||
create arrays
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
void ComputeGrid::allocate()
|
||||
{
|
||||
// allocate arrays
|
||||
|
||||
memory->destroy(grid);
|
||||
memory->destroy(gridall);
|
||||
memory->destroy(local_flags);
|
||||
if (gridlocal_allocated) {
|
||||
gridlocal_allocated = 0;
|
||||
memory->destroy4d_offset(gridlocal,nzlo,nylo,nxlo);
|
||||
}
|
||||
|
||||
memory->create(grid,size_array_rows,size_array_cols,"grid:grid");
|
||||
memory->create(gridall,size_array_rows,size_array_cols,"grid:gridall");
|
||||
memory->create(local_flags,size_array_rows,"grid:local_flags");
|
||||
@ -222,6 +209,23 @@ void ComputeGrid::allocate()
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
free arrays
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
void ComputeGrid::deallocate()
|
||||
{
|
||||
memory->destroy(grid);
|
||||
memory->destroy(gridall);
|
||||
memory->destroy(local_flags);
|
||||
if (gridlocal_allocated) {
|
||||
gridlocal_allocated = 0;
|
||||
memory->destroy4d_offset(gridlocal,nzlo,nylo,nxlo);
|
||||
}
|
||||
array = nullptr;
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
set global grid
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
@ -49,7 +49,8 @@ class ComputeGrid : public Compute {
|
||||
int *local_flags; // local flag for each grid point
|
||||
int gridlocal_allocated; // shows if gridlocal allocated
|
||||
|
||||
void allocate();
|
||||
void allocate(); // create arrays
|
||||
void deallocate(); // free arrays
|
||||
void grid2x(int, double*); // convert grid point to coord
|
||||
void grid2ix(int, int&, int&, int&); // convert grid point to ix, iy, iz
|
||||
void assign_coords(); // assign coords for grid
|
||||
|
||||
@ -59,11 +59,7 @@ ComputeGridLocal::ComputeGridLocal(LAMMPS *lmp, int narg, char **arg) :
|
||||
|
||||
ComputeGridLocal::~ComputeGridLocal()
|
||||
{
|
||||
if (gridlocal_allocated) {
|
||||
gridlocal_allocated = 0;
|
||||
memory->destroy4d_offset(gridlocal,nzlo,nylo,nxlo);
|
||||
}
|
||||
memory->destroy(alocal);
|
||||
deallocate();
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
@ -76,9 +72,11 @@ void ComputeGridLocal::init()
|
||||
|
||||
void ComputeGridLocal::setup()
|
||||
{
|
||||
deallocate();
|
||||
set_grid_global();
|
||||
set_grid_local();
|
||||
allocate();
|
||||
assign_coords();
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
@ -95,25 +93,34 @@ void ComputeGridLocal::grid2x(int ix, int iy, int iz, double *x)
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
free and reallocate arrays
|
||||
create arrays
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
void ComputeGridLocal::allocate()
|
||||
{
|
||||
// allocate local array
|
||||
|
||||
if (gridlocal_allocated) {
|
||||
gridlocal_allocated = 0;
|
||||
memory->destroy4d_offset(gridlocal,nzlo,nylo,nxlo);
|
||||
}
|
||||
|
||||
if (nxlo <= nxhi && nylo <= nyhi && nzlo <= nzhi) {
|
||||
gridlocal_allocated = 1;
|
||||
memory->create4d_offset(gridlocal,size_local_cols,nzlo,nzhi,nylo,nyhi,
|
||||
nxlo,nxhi,"grid:gridlocal");
|
||||
memory->create(alocal, size_local_rows, size_local_cols, "compute/grid/local:alocal");
|
||||
array_local = alocal;
|
||||
}
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
free arrays
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
void ComputeGridLocal::deallocate()
|
||||
{
|
||||
if (gridlocal_allocated) {
|
||||
gridlocal_allocated = 0;
|
||||
memory->destroy4d_offset(gridlocal,nzlo,nylo,nxlo);
|
||||
memory->destroy(alocal);
|
||||
}
|
||||
array_local = nullptr;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
set global grid
|
||||
------------------------------------------------------------------------- */
|
||||
@ -201,11 +208,14 @@ void ComputeGridLocal::set_grid_local()
|
||||
|
||||
ngridlocal = (nxhi - nxlo + 1) * (nyhi - nylo + 1) * (nzhi - nzlo + 1);
|
||||
size_local_rows = ngridlocal;
|
||||
}
|
||||
|
||||
memory->destroy(alocal);
|
||||
memory->create(alocal, size_local_rows, size_local_cols, "compute/grid/local:alocal");
|
||||
array_local = alocal;
|
||||
/* ----------------------------------------------------------------------
|
||||
copy coords to local array
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
void ComputeGridLocal::assign_coords()
|
||||
{
|
||||
int igrid = 0;
|
||||
for (int iz = nzlo; iz <= nzhi; iz++)
|
||||
for (int iy = nylo; iy <= nyhi; iy++)
|
||||
|
||||
@ -46,10 +46,12 @@ class ComputeGridLocal : public Compute {
|
||||
int size_local_cols_base; // number of columns used for coords, etc.
|
||||
int gridlocal_allocated; // shows if gridlocal allocated
|
||||
|
||||
void allocate();
|
||||
void allocate(); // create arrays
|
||||
void deallocate(); // free arrays
|
||||
void grid2x(int, int, int, double*); // convert global indices to coordinates
|
||||
void set_grid_global(); // set global grid
|
||||
void set_grid_local(); // set bounds for local grid
|
||||
void assign_coords(); // assign coords for grid
|
||||
void copy_gridlocal_to_local_array();// copy 4d gridlocal array to 2d local array
|
||||
private:
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user