From 89e3ce1b6f16e918476c249583c80660d98112bb Mon Sep 17 00:00:00 2001 From: Anders Johansson Date: Tue, 4 Jun 2024 11:58:29 -0400 Subject: [PATCH] attempt fix empty domains also in non-Kokkos pair and compute (#45), also force C++17 because of insert_or_assign. Compiles but untested. --- compute_allegro.cpp | 46 +++++++++++++++++++++++++++++---------------- pair_allegro.cpp | 3 +++ patch_lammps.sh | 2 +- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/compute_allegro.cpp b/compute_allegro.cpp index f8738e7..ed350d3 100644 --- a/compute_allegro.cpp +++ b/compute_allegro.cpp @@ -101,18 +101,27 @@ void ComputeAllegro::compute_vector() { invoked_vector = update->ntimestep; - const torch::Tensor &quantity_tensor = - ((PairAllegro *) force->pair)->custom_output.at(quantity).cpu().ravel(); + // empty domain, pair style won't store tensor + // note: assumes nlocal == inum + if (atom->nlocal == 0) { + for (int i = 0; i < size_vector; i++) { + vector[i] = 0.0; + } + } else { + const torch::Tensor &quantity_tensor = + ((PairAllegro *) force->pair)->custom_output.at(quantity).cpu().ravel(); - auto quantity = quantity_tensor.data_ptr(); + auto quantity = quantity_tensor.data_ptr(); - if (quantity_tensor.size(0) != size_vector) { - error->one(FLERR, "size {} of quantity tensor {} does not match expected {} on rank {}", - quantity_tensor.size(0), this->quantity, size_vector, comm->me); - } + if (quantity_tensor.size(0) != size_vector) { + error->one(FLERR, "size {} of quantity tensor {} does not match expected {} on rank {}", + quantity_tensor.size(0), this->quantity, size_vector, comm->me); + } - for (int i = 0; i < size_vector; i++) { vector[i] = quantity[i]; } + for (int i = 0; i < size_vector; i++) { vector[i] = quantity[i]; } + } + // even if empty domain MPI_Allreduce(MPI_IN_PLACE, vector, size_vector, MPI_DOUBLE, MPI_SUM, world); } @@ -128,18 +137,23 @@ void ComputeAllegro::compute_peratom() if (nperatom==1) vector_atom = &array_atom[0][0]; } - const torch::Tensor &quantity_tensor = - ((PairAllegro *) force->pair)->custom_output.at(quantity).cpu().contiguous().reshape({-1,nperatom}); + // guard against empty domain (pair style won't store tensor) + if (atom->nlocal > 0) { + const torch::Tensor &quantity_tensor = + ((PairAllegro *) force->pair)->custom_output.at(quantity).cpu().contiguous().reshape({-1,nperatom}); - auto quantity = quantity_tensor.accessor(); - quantityptr = quantity_tensor.data_ptr(); + auto quantity = quantity_tensor.accessor(); + quantityptr = quantity_tensor.data_ptr(); - int nlocal = atom->nlocal; - for (int i = 0; i < nlocal; i++) { - for (int j = 0; j < nperatom; j++) { - array_atom[i][j] = quantity[i][j]; + int nlocal = atom->nlocal; + for (int i = 0; i < nlocal; i++) { + for (int j = 0; j < nperatom; j++) { + array_atom[i][j] = quantity[i][j]; + } } } + + // even if empty domain if (newton) comm->reverse_comm(this); } diff --git a/pair_allegro.cpp b/pair_allegro.cpp index 57e0354..f5e1360 100644 --- a/pair_allegro.cpp +++ b/pair_allegro.cpp @@ -335,6 +335,9 @@ template void PairAllegro::compute(int eflag, i // Neighbor list per atom int **firstneigh = list->firstneigh; + // Skip calculation if empty domain + if (inum==0) return; + // Total number of bonds (sum of number of neighbors) int nedges = 0; diff --git a/patch_lammps.sh b/patch_lammps.sh index dd66e31..f063478 100755 --- a/patch_lammps.sh +++ b/patch_lammps.sh @@ -68,7 +68,7 @@ fi echo "Updating CMakeLists.txt..." -sed -i "s/set(CMAKE_CXX_STANDARD 11)/set(CMAKE_CXX_STANDARD 14)/" $lammps_dir/cmake/CMakeLists.txt +sed -i "s/set(CMAKE_CXX_STANDARD 11)/set(CMAKE_CXX_STANDARD 17)/" $lammps_dir/cmake/CMakeLists.txt # Add libtorch cat >> $lammps_dir/cmake/CMakeLists.txt << "EOF2"