fix memory allocation runtime error

This commit is contained in:
Megan J. McCarthy
2023-06-10 07:54:40 -06:00
parent 0d875d4f8f
commit 9eb32fc6b0
4 changed files with 69 additions and 52 deletions

View File

@ -42,8 +42,6 @@ ComputeLocalCompAtom::ComputeLocalCompAtom(LAMMPS *lmp, int narg, char **arg) :
{
if (narg < 3 || narg > 5) error->all(FLERR, "Illegal compute local/comp/atom command");
int ntypes = atom->ntypes;
cutoff = 0.0;
int iarg = 3;
@ -59,6 +57,7 @@ ComputeLocalCompAtom::ComputeLocalCompAtom(LAMMPS *lmp, int narg, char **arg) :
peratom_flag = 1;
ntypes = atom->ntypes;
size_peratom_cols = 1 + ntypes;
nmax = 0;
@ -71,6 +70,7 @@ ComputeLocalCompAtom::~ComputeLocalCompAtom()
if (copymode) return;
memory->destroy(result);
memory->destroy(lcomp);
}
/* ---------------------------------------------------------------------- */
@ -108,6 +108,7 @@ void ComputeLocalCompAtom::init()
auto req = neighbor->add_request(this, NeighConst::REQ_FULL | NeighConst::REQ_OCCASIONAL);
if (cutflag) req->set_cutoff(cutoff);
}
/* ---------------------------------------------------------------------- */
@ -125,6 +126,9 @@ void ComputeLocalCompAtom::compute_peratom()
double xtmp, ytmp, ztmp, delx, dely, delz, rsq;
int *ilist, *jlist, *numneigh, **firstneigh;
int count, itype, jtype;
// int lcomp[ntypes];
memory->create(lcomp, ntypes, "local/comp/atom:lcomp");
invoked_peratom = update->ntimestep;
@ -153,20 +157,14 @@ void ComputeLocalCompAtom::compute_peratom()
int *type = atom->type;
int *mask = atom->mask;
int typeone_i, typeone_j;
int ntypes = atom->ntypes;
int lcomp[ntypes];
// get per-atom local compositions
for (ii = 0; ii < inum; ii++) {
for (int i = 0; i < ntypes; i++) lcomp[i] = 0;
i = ilist[ii];
for (int m = 0; m < ntypes; m++) lcomp[m] = 0.0;
if (mask[i] & groupbit) {
xtmp = x[i][0];

View File

@ -34,11 +34,13 @@ class ComputeLocalCompAtom : public Compute {
double memory_usage() override;
protected:
int nmax;
int nmax, ntypes;
double cutoff; // global cutoff distance
double cutsq; // cutoff**2
class NeighList *list; // neighbor list
int *lcomp; // peratom array to track local compositions
double **result; // peratom array of local compositions
};

View File

@ -12,7 +12,7 @@
------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
Contributing author: Stan Moore (SNL), Megan McCarthy (SNL)
Contributing authors: Stan Moore (SNL), Megan McCarthy (SNL)
------------------------------------------------------------------------- */
#include "compute_local_comp_atom_kokkos.h"
@ -42,11 +42,13 @@ template<class DeviceType>
ComputeLocalCompAtomKokkos<DeviceType>::ComputeLocalCompAtomKokkos(LAMMPS *lmp, int narg, char **arg) :
ComputeLocalCompAtom(lmp, narg, arg)
{
kokkosable = 1;
atomKK = (AtomKokkos *) atom;
execution_space = ExecutionSpaceFromDevice<DeviceType>::space;
datamask_read = EMPTY_MASK;
datamask_modify = EMPTY_MASK;
}
/* ---------------------------------------------------------------------- */
@ -57,6 +59,7 @@ ComputeLocalCompAtomKokkos<DeviceType>::~ComputeLocalCompAtomKokkos()
if (copymode) return;
memoryKK->destroy_kokkos(k_result,result);
memoryKK->destroy_kokkos(k_lcomp,lcomp);
}
/* ---------------------------------------------------------------------- */
@ -79,6 +82,7 @@ void ComputeLocalCompAtomKokkos<DeviceType>::init()
template<class DeviceType>
void ComputeLocalCompAtomKokkos<DeviceType>::compute_peratom()
{
invoked_peratom = update->ntimestep;
// grow result array if necessary
@ -90,8 +94,16 @@ void ComputeLocalCompAtomKokkos<DeviceType>::compute_peratom()
memoryKK->create_kokkos(k_result,result,nmax,size_peratom_cols,"local/comp/atom:result");
d_result = k_result.view<DeviceType>();
array_atom = result;
memoryKK->destroy_kokkos(k_lcomp,lcomp);
nmax = atom->nmax;
memoryKK->create_kokkos(k_lcomp,lcomp,nmax,"local/comp/atom:result");
d_lcomp = k_lcomp.view<DeviceType>();
}
memoryKK->create_kokkos(k_lcomp,lcomp,size_peratom_cols,"local/comp/atom:lcomp");
d_lcomp = k_lcomp.view<DeviceType>();
// invoke full neighbor list (will copy or build if necessary)
neighbor->build_one(list);
@ -105,12 +117,14 @@ void ComputeLocalCompAtomKokkos<DeviceType>::compute_peratom()
// compute properties for each atom in group
// use full neighbor list to count atoms less than cutoff
atomKK->sync(execution_space,X_MASK|V_MASK|RMASS_MASK|TYPE_MASK|MASK_MASK);
atomKK->sync(execution_space,X_MASK|TYPE_MASK|MASK_MASK);
x = atomKK->k_x.view<DeviceType>();
type = atomKK->k_type.view<DeviceType>();
mask = atomKK->k_mask.view<DeviceType>();
ntypes = atom->ntypes;
Kokkos::deep_copy(d_result,0.0);
Kokkos::deep_copy(d_lcomp,0.0);
copymode = 1;
typename Kokkos::RangePolicy<DeviceType, TagComputeLocalCompAtom> policy(0,inum);
@ -119,66 +133,67 @@ void ComputeLocalCompAtomKokkos<DeviceType>::compute_peratom()
k_result.modify<DeviceType>();
k_result.sync_host();
k_lcomp.modify<DeviceType>();
k_lcomp.sync_host();
}
template<class DeviceType>
KOKKOS_INLINE_FUNCTION
void ComputeLocalCompAtomKokkos<DeviceType>::operator()(TagComputeLocalCompAtom, const int &ii) const
{
double typeone_i, typeone_j;
int ntypes = atom->ntypes;
int lcomp[ntypes];
const int i = d_ilist[ii];
// get per-atom local compositions
// initialize / reset lcomp
int inum = list->inum;
for (int ii = 0; ii < inum; ii++) {
for (int m = 0; m < ntypes; m++) d_lcomp(m) = 0;
// for (int m = 0; m < ntypes; m++) d_result(i,m) = 0;
for (int i = 0; i < ntypes; i++) lcomp[i] = 0;
if (mask[i] & groupbit) {
const int i = d_ilist[ii];
const X_FLOAT xtmp = x(i,0);
const X_FLOAT ytmp = x(i,1);
const X_FLOAT ztmp = x(i,2);
const int jnum = d_numneigh[i];
if (mask[i] & groupbit) {
// i atom contribution
const X_FLOAT xtmp = x(i,0);
const X_FLOAT ytmp = x(i,1);
const X_FLOAT ztmp = x(i,2);
const int jnum = d_numneigh[i];
int count = 1;
int itype = type[i];
d_lcomp(itype-1)++;
// d_result(i,itype-1)++;
// i atom contribution
for (int jj = 0; jj < jnum; jj++) {
int count = 1;
int j = d_neighbors(i,jj);
j &= NEIGHMASK;
int itype = type[i];
lcomp[itype-1]++;
int jtype = type[j];
for (int jj = 0; jj < jnum; jj++) {
int j = d_neighbors(i,jj);
j &= NEIGHMASK;
int jtype = type[j];
const F_FLOAT delx = x(j,0) - xtmp;
const F_FLOAT dely = x(j,1) - ytmp;
const F_FLOAT delz = x(j,2) - ztmp;
const F_FLOAT rsq = delx*delx + dely*dely + delz*delz;
if (rsq < cutsq) {
count++;
lcomp[jtype-1]++;
}
const F_FLOAT delx = x(j,0) - xtmp;
const F_FLOAT dely = x(j,1) - ytmp;
const F_FLOAT delz = x(j,2) - ztmp;
const F_FLOAT rsq = delx*delx + dely*dely + delz*delz;
if (rsq < cutsq) {
count++;
d_lcomp(jtype-1)++;
// d_result(i,jtype-1)++;
}
// total count of atoms found in sampled radius range
// total count of atoms found in sampled radius range
d_result(i,0) = count;
d_result(i,0) = count;
// local comp fractions per element
// local comp fractions per
double lfac = 1.0 / count;
for (int n = 0; n < ntypes; n++) {
d_result(i,n+1) = d_lcomp(n+1) * lfac;
// d_result(i,n+1) = d_result(i,n+1) * lfac;
}
double lfac = 1.0 / count;
for (int n = 0; n < ntypes; n++)
d_result(i,n+1) = lcomp[n] * lfac;
}
}
}

View File

@ -55,7 +55,9 @@ template <class DeviceType> class ComputeLocalCompAtomKokkos : public ComputeLoc
typename AT::t_int_1d d_ilist;
typename AT::t_int_1d d_numneigh;
DAT::tdual_int_1d k_lcomp;
DAT::tdual_float_2d k_result;
typename AT::t_int_1d d_lcomp;
typename AT::t_float_2d d_result;
};