diff --git a/src/KOKKOS/mliap_unified_couple_kokkos.pyx b/src/KOKKOS/mliap_unified_couple_kokkos.pyx index 97d807ac33..385a770bb3 100644 --- a/src/KOKKOS/mliap_unified_couple_kokkos.pyx +++ b/src/KOKKOS/mliap_unified_couple_kokkos.pyx @@ -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": @@ -451,15 +452,24 @@ cdef public object mliap_unified_connect_kokkos(char *fname, MLIAPDummyModel * m cdef int nelements = len(unified.element_types) cdef char **elements = 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 = 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 diff --git a/src/KOKKOS/mliap_unified_kokkos.cpp b/src/KOKKOS/mliap_unified_kokkos.cpp index 4c38e4f1d6..68caf035e9 100644 --- a/src/KOKKOS/mliap_unified_kokkos.cpp +++ b/src/KOKKOS/mliap_unified_kokkos.cpp @@ -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]); @@ -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]); - } } } }); @@ -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); } diff --git a/src/ML-IAP/mliap_unified.cpp b/src/ML-IAP/mliap_unified.cpp index de1d0bcb7d..7697204e44 100644 --- a/src/ML-IAP/mliap_unified.cpp +++ b/src/ML-IAP/mliap_unified.cpp @@ -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; } @@ -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]; + 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]); - } + if (data->vflag) data->pairmliap->v_tally(i, j, &fij[ii3], data->rij[ii]); } } diff --git a/src/ML-IAP/mliap_unified_couple.pyx b/src/ML-IAP/mliap_unified_couple.pyx index 3148b96b51..6c8331d0fa 100644 --- a/src/ML-IAP/mliap_unified_couple.pyx +++ b/src/ML-IAP/mliap_unified_couple.pyx @@ -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": @@ -387,15 +388,26 @@ cdef public object mliap_unified_connect(char *fname, MLIAPDummyModel * model, cdef int nelements = len(unified.element_types) cdef char **elements = 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 = 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