Skip to content

Commit

Permalink
Merge pull request lammps#4088 from sakibmatin/debug
Browse files Browse the repository at this point in the history
Fix for force calculation and memory bug (atom name definition) in mliap.
  • Loading branch information
akohlmey authored Feb 26, 2024
2 parents 35db949 + f864963 commit e9deaa8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
18 changes: 14 additions & 4 deletions src/KOKKOS/mliap_unified_couple_kokkos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ from libc.stdint cimport uintptr_t
cimport cython
from cpython.ref cimport PyObject
from libc.stdlib cimport malloc, free
from libc.string cimport memcpy


cdef extern from "lammps.h" namespace "LAMMPS_NS":
Expand Down Expand Up @@ -451,15 +452,24 @@ cdef public object mliap_unified_connect_kokkos(char *fname, MLIAPDummyModel * m

cdef int nelements = <int>len(unified.element_types)
cdef char **elements = <char**>malloc(nelements * sizeof(char*))
cdef char * c_str
cdef char * s
cdef ssize_t slen

if not elements:
raise MemoryError("failed to allocate memory for element names")

cdef char *elem_name
for i, elem in enumerate(unified.element_types):
elem_name_bytes = elem.encode('UTF-8')
elem_name = elem_name_bytes
elements[i] = &elem_name[0]
py_str = elem.encode('UTF-8')
s = py_str
slen = len(py_str)
c_str = <char *>malloc((slen+1)*sizeof(char))
if not c_str:
raise MemoryError("failed to allocate memory for element names")
memcpy(c_str, s, slen)
c_str[slen] = 0
elements[i] = c_str

unified_int.descriptor.set_elements(elements, nelements)
unified_int.model.nelements = nelements

Expand Down
10 changes: 3 additions & 7 deletions src/KOKKOS/mliap_unified_kokkos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ void LAMMPS_NS::update_pair_forces(MLIAPDataKokkosDevice *data, double *fij)
int i = pair_i[ii];
int j = j_atoms[ii];
// must not count any contribution where i is not a local atom
if (i < nlocal) {
Kokkos::atomic_add(&f[i*3+0], fij[ii3+0]);
Kokkos::atomic_add(&f[i*3+1], fij[ii3+1]);
Kokkos::atomic_add(&f[i*3+2], fij[ii3+2]);
Expand Down Expand Up @@ -352,7 +351,6 @@ void LAMMPS_NS::update_pair_forces(MLIAPDataKokkosDevice *data, double *fij)
Kokkos::atomic_add(&d_vatom(j,3), 0.5*v[3]);
Kokkos::atomic_add(&d_vatom(j,4), 0.5*v[4]);
Kokkos::atomic_add(&d_vatom(j,5), 0.5*v[5]);
}
}
}
});
Expand Down Expand Up @@ -382,11 +380,9 @@ void LAMMPS_NS::update_atom_energy(MLIAPDataKokkosDevice *data, double *ei)

Kokkos::parallel_reduce(nlocal, KOKKOS_LAMBDA(int i, double &local_sum){
double e = ei[i];
// must not count any contribution where i is not a local atom
if (i < nlocal) {
d_eatoms[i] = e;
local_sum += e;
}

d_eatoms[i] = e;
local_sum += e;
},*data->energy);
}

Expand Down
25 changes: 10 additions & 15 deletions src/ML-IAP/mliap_unified.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,8 @@ void LAMMPS_NS::update_pair_energy(MLIAPData *data, double *eij)
double e = 0.5 * eij[ii];

// must not count any contribution where i is not a local atom
if (i < nlocal) {
data->eatoms[i] += e;
e_total += e;
}
data->eatoms[i] += e;
e_total += e;
}
data->energy = e_total;
}
Expand All @@ -277,17 +275,14 @@ void LAMMPS_NS::update_pair_forces(MLIAPData *data, double *fij)
int i = data->pair_i[ii];
int j = data->jatoms[ii];

// must not count any contribution where i is not a local atom
if (i < nlocal) {
f[i][0] += fij[ii3];
f[i][1] += fij[ii3 + 1];
f[i][2] += fij[ii3 + 2];
f[j][0] -= fij[ii3];
f[j][1] -= fij[ii3 + 1];
f[j][2] -= fij[ii3 + 2];

if (data->vflag) data->pairmliap->v_tally(i, j, &fij[ii3], data->rij[ii]);
}
f[i][0] += fij[ii3];
f[i][1] += fij[ii3 + 1];
f[i][2] += fij[ii3 + 2];
f[j][0] -= fij[ii3];
f[j][1] -= fij[ii3 + 1];
f[j][2] -= fij[ii3 + 2];

if (data->vflag) data->pairmliap->v_tally(i, j, &fij[ii3], data->rij[ii]);
}
}

Expand Down
20 changes: 16 additions & 4 deletions src/ML-IAP/mliap_unified_couple.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import lammps.mliap
cimport cython
from cpython.ref cimport PyObject
from libc.stdlib cimport malloc, free
from libc.string cimport memcpy


cdef extern from "lammps.h" namespace "LAMMPS_NS":
Expand Down Expand Up @@ -387,15 +388,26 @@ cdef public object mliap_unified_connect(char *fname, MLIAPDummyModel * model,

cdef int nelements = <int>len(unified.element_types)
cdef char **elements = <char**>malloc(nelements * sizeof(char*))
cdef char * c_str
cdef char * s
cdef ssize_t slen

if not elements:
raise MemoryError("failed to allocate memory for element names")

cdef char *elem_name
for i, elem in enumerate(unified.element_types):
elem_name_bytes = elem.encode('UTF-8')
elem_name = elem_name_bytes
elements[i] = &elem_name[0]
py_str = elem.encode('UTF-8')

s = py_str
slen = len(py_str)
c_str = <char *>malloc((slen+1)*sizeof(char))
if not c_str:
raise MemoryError("failed to allocate memory for element names")
memcpy(c_str, s, slen)
c_str[slen] = 0

elements[i] = c_str

unified_int.descriptor.set_elements(elements, nelements)
unified_int.model.nelements = nelements

Expand Down

0 comments on commit e9deaa8

Please sign in to comment.