From 096a70363bc0486d2fab6339b81e911bd5109623 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 17 Nov 2022 11:31:34 -0500 Subject: [PATCH] allow to check if Kokkos is active and retrieve number of threads and gpus --- src/library.cpp | 16 +++++++++-- .../c-library/test_library_properties.cpp | 28 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/library.cpp b/src/library.cpp index a1cc67cfa5..e7f3471d64 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -983,7 +983,7 @@ be called without a valid LAMMPS object handle (it is ignored). **Image masks** These settings are related to how LAMMPS stores and interprets periodic images. The values are used -internally by the Fortran interface and are not likely to be useful to users. +internally by the :doc:`Fortran interface ` and are not likely to be useful to users. .. list-table:: :header-rows: 1 @@ -1015,8 +1015,17 @@ internally by the Fortran interface and are not likely to be useful to users. * - box_exist - 1 if the simulation box is defined, 0 if not. See :doc:`create_box`. + * - kokkos_active + - 1 if the KOKKOS package is compiled in **and** activated, 0 if not. + See :doc:``. + * - kokkos_nthreads + - Number of Kokkos threads per MPI process, 0 if Kokkos is not active. + See :doc:``. + * - kokkos_ngpus + - Number of Kokkos gpus per MPI process, 0 if Kokkos is not active or no GPU support. + See :doc:``. * - nthreads - - Number of requested OpenMP threads for LAMMPS' execution + - Number of requested OpenMP threads per MPI process for LAMMPS' execution * - newton_bond - 1 if Newton's 3rd law is applied to bonded interactions, 0 if not. * - newton_pair @@ -1126,6 +1135,9 @@ int lammps_extract_setting(void *handle, const char *keyword) if (strcmp(keyword,"dimension") == 0) return lmp->domain->dimension; if (strcmp(keyword,"box_exist") == 0) return lmp->domain->box_exist; + if (strcmp(keyword,"kokkos_active") == 0) return (lmp->kokkos) ? 1 : 0; + if (strcmp(keyword,"kokkos_nthreads") == 0) return (lmp->kokkos) ? lmp->kokkos->nthreads : 0; + if (strcmp(keyword,"kokkos_ngpus") == 0) return (lmp->kokkos) ? lmp->kokkos->ngpus : 0; if (strcmp(keyword,"newton_bond") == 0) return lmp->force->newton_bond; if (strcmp(keyword,"newton_pair") == 0) return lmp->force->newton_pair; if (strcmp(keyword,"triclinic") == 0) return lmp->domain->triclinic; diff --git a/unittest/c-library/test_library_properties.cpp b/unittest/c-library/test_library_properties.cpp index bbb363dfab..56486fba6c 100644 --- a/unittest/c-library/test_library_properties.cpp +++ b/unittest/c-library/test_library_properties.cpp @@ -209,6 +209,10 @@ TEST_F(LibraryProperties, setting) lammps_command(lmp, "dimension 3"); if (!verbose) ::testing::internal::GetCapturedStdout(); + EXPECT_EQ(lammps_extract_setting(lmp, "kokkos_active"), 0); + EXPECT_EQ(lammps_extract_setting(lmp, "kokkos_nthreads"), 0); + EXPECT_EQ(lammps_extract_setting(lmp, "kokkos_ngpus"), 0); + EXPECT_EQ(lammps_extract_setting(lmp, "world_size"), 1); EXPECT_EQ(lammps_extract_setting(lmp, "world_rank"), 0); EXPECT_EQ(lammps_extract_setting(lmp, "universe_size"), 1); @@ -545,3 +549,27 @@ TEST_F(AtomProperties, position) EXPECT_DOUBLE_EQ(x[1][1], 0.1); EXPECT_DOUBLE_EQ(x[1][2], 0.1); } + +TEST(SystemSettings, kokkos) +{ + if (!lammps_config_has_package("KOKKOS")) GTEST_SKIP(); + if (!lammps_config_accelerator("KOKKOS", "api", "openmp")) GTEST_SKIP(); + + // clang-format off + const char *args[] = {"SystemSettings", "-log", "none", "-echo", "screen", "-nocite", + "-k", "on", "t", "4", "-sf", "kk"}; + // clang-format on + char **argv = (char **)args; + int argc = sizeof(args) / sizeof(char *); + + ::testing::internal::CaptureStdout(); + void *lmp = lammps_open_no_mpi(argc, argv, nullptr); + std::string output = ::testing::internal::GetCapturedStdout(); + if (verbose) std::cout << output; + EXPECT_THAT(output, StartsWith("LAMMPS (")); + + EXPECT_EQ(lammps_extract_setting(lmp, "kokkos_active"), 1); + EXPECT_EQ(lammps_extract_setting(lmp, "kokkos_nthreads"), 4); + EXPECT_EQ(lammps_extract_setting(lmp, "kokkos_ngpus"), 0); + lammps_close(lmp); +}