From 61993d16432314055cc263779095f550c7d7a1db Mon Sep 17 00:00:00 2001 From: Leidy Lorena Alzate Vargas - 373576 Date: Wed, 22 Nov 2023 14:36:59 -0700 Subject: [PATCH 1/4] MLIAP Unified fix for multi layer models CPU only --- src/ML-IAP/mliap_unified.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/ML-IAP/mliap_unified.cpp b/src/ML-IAP/mliap_unified.cpp index de1d0bcb7d..6dcdf94c2d 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; } @@ -278,16 +276,14 @@ void LAMMPS_NS::update_pair_forces(MLIAPData *data, double *fij) 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]); } } From 469358cbf4951c51472777bcd51055de9aaf2ed7 Mon Sep 17 00:00:00 2001 From: Leidy Lorena Alzate Vargas - 373576 Date: Wed, 22 Nov 2023 14:38:13 -0700 Subject: [PATCH 2/4] UPDATE KOKKOS --- src/KOKKOS/mliap_unified_kokkos.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/KOKKOS/mliap_unified_kokkos.cpp b/src/KOKKOS/mliap_unified_kokkos.cpp index 4c38e4f1d6..4a8c14d723 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]); - } } } }); @@ -383,10 +381,8 @@ 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); } From 8e3b5bf32730d43373881a9a06364a975399522c Mon Sep 17 00:00:00 2001 From: Ben Nebgen Date: Fri, 5 Jan 2024 13:05:27 -0700 Subject: [PATCH 3/4] Fixed memory bug in ML-IAP unified with atom name definitions Added explict memory allocation for type string in ML-IAP unified. Corrected bug where atom types were not properly alligned with the potential. --- src/KOKKOS/mliap_unified_couple_kokkos.pyx | 18 ++++++++++++++---- src/ML-IAP/mliap_unified_couple.pyx | 20 ++++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) 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/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 From f864963ab94ba88828cc77ffd0ecd1902313b1e3 Mon Sep 17 00:00:00 2001 From: sakibmatin Date: Thu, 22 Feb 2024 22:03:29 -0700 Subject: [PATCH 4/4] removed old comments. --- src/KOKKOS/mliap_unified_kokkos.cpp | 2 +- src/ML-IAP/mliap_unified.cpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/KOKKOS/mliap_unified_kokkos.cpp b/src/KOKKOS/mliap_unified_kokkos.cpp index 4a8c14d723..68caf035e9 100644 --- a/src/KOKKOS/mliap_unified_kokkos.cpp +++ b/src/KOKKOS/mliap_unified_kokkos.cpp @@ -380,7 +380,7 @@ 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 + 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 6dcdf94c2d..7697204e44 100644 --- a/src/ML-IAP/mliap_unified.cpp +++ b/src/ML-IAP/mliap_unified.cpp @@ -275,7 +275,6 @@ 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 f[i][0] += fij[ii3]; f[i][1] += fij[ii3 + 1]; f[i][2] += fij[ii3 + 2];