From a3f8b237723fd52be4d51bf4b355259dbab21916 Mon Sep 17 00:00:00 2001 From: haykh Date: Thu, 11 Sep 2025 15:57:14 -0700 Subject: [PATCH 1/5] kokkos_rand minimal --- minimal/CMakeLists.txt | 13 +++++++++++++ minimal/kokkos_rand.cpp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 minimal/kokkos_rand.cpp diff --git a/minimal/CMakeLists.txt b/minimal/CMakeLists.txt index b21dd0fe..9578bbcb 100644 --- a/minimal/CMakeLists.txt +++ b/minimal/CMakeLists.txt @@ -82,6 +82,19 @@ if("KOKKOS" IN_LIST MODES) target_link_libraries(${exec} ${libs}) endif() +if("KOKKOS_RAND" IN_LIST MODES) + set(libs "") + set(exec kokkos_rand.xc) + set(src ${CMAKE_CURRENT_SOURCE_DIR}/kokkos_rand.cpp) + + find_kokkos() + list(APPEND libs Kokkos::kokkos) + + add_executable(${exec} ${src}) + + target_link_libraries(${exec} ${libs}) +endif() + if("ADIOS2_NOMPI" IN_LIST MODES) set(libs stdc++fs) set(exec adios2-nompi.xc) diff --git a/minimal/kokkos_rand.cpp b/minimal/kokkos_rand.cpp new file mode 100644 index 00000000..72e23844 --- /dev/null +++ b/minimal/kokkos_rand.cpp @@ -0,0 +1,36 @@ +#include + +#include +#include + +auto main(int argc, char** argv) -> int { + try { + Kokkos::initialize(argc, argv); + Kokkos::DefaultExecutionSpace {}.print_configuration(std::cout); + + Kokkos::Random_XorShift64_Pool<> random_pool(12345); + + const auto sz = (std::size_t)(1e8); + + Kokkos::View view { "rand_view", sz }; + Kokkos::parallel_for( + "fill_random", + Kokkos::RangePolicy<>(0, sz), + KOKKOS_LAMBDA(std::size_t i) { + auto gen = random_pool.get_state(); + view(i) = gen.frand(); + random_pool.free_state(gen); + }); + Kokkos::fence(); + + } catch (const std::exception& e) { + if (Kokkos::is_initialized()) { + Kokkos::finalize(); + } + std::cerr << "Error: " << e.what() << std::endl; + return 1; + } + + Kokkos::finalize(); + return 0; +} From 42dc7e54054c4ed95072ea3f8eb3bb514c705dc8 Mon Sep 17 00:00:00 2001 From: haykh Date: Thu, 11 Sep 2025 16:10:03 -0700 Subject: [PATCH 2/5] mpi + kokkos_rand min --- minimal/CMakeLists.txt | 3 ++- minimal/kokkos_rand.cpp | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/minimal/CMakeLists.txt b/minimal/CMakeLists.txt index 9578bbcb..5eeb17ac 100644 --- a/minimal/CMakeLists.txt +++ b/minimal/CMakeLists.txt @@ -87,8 +87,9 @@ if("KOKKOS_RAND" IN_LIST MODES) set(exec kokkos_rand.xc) set(src ${CMAKE_CURRENT_SOURCE_DIR}/kokkos_rand.cpp) + find_package(MPI REQUIRED) find_kokkos() - list(APPEND libs Kokkos::kokkos) + list(APPEND libs MPI::MPI_CXX Kokkos::kokkos) add_executable(${exec} ${src}) diff --git a/minimal/kokkos_rand.cpp b/minimal/kokkos_rand.cpp index 72e23844..cae9dea9 100644 --- a/minimal/kokkos_rand.cpp +++ b/minimal/kokkos_rand.cpp @@ -1,36 +1,55 @@ #include +#include +#include +#include #include #include auto main(int argc, char** argv) -> int { try { + MPI_Init(&argc, &argv); + int rank, size; + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + Kokkos::initialize(argc, argv); + Kokkos::DefaultExecutionSpace {}.print_configuration(std::cout); - Kokkos::Random_XorShift64_Pool<> random_pool(12345); + std::uint64_t RandomSeed = 0x123456789abcdef0; + Kokkos::Random_XorShift64_Pool<> random_pool( + RandomSeed + static_cast(rank)); const auto sz = (std::size_t)(1e8); Kokkos::View view { "rand_view", sz }; - Kokkos::parallel_for( - "fill_random", - Kokkos::RangePolicy<>(0, sz), - KOKKOS_LAMBDA(std::size_t i) { - auto gen = random_pool.get_state(); - view(i) = gen.frand(); - random_pool.free_state(gen); - }); + + for (auto i = 0; i < 100; ++i) { + Kokkos::parallel_for( + "fill_random", + Kokkos::RangePolicy<>(0, sz), + KOKKOS_LAMBDA(std::size_t i) { + auto gen = random_pool.get_state(); + view(i) = gen.frand(); + random_pool.free_state(gen); + }); + Kokkos::fence(); + } + + std::cout << "Rank " << rank << " finished\n" << std::flush; Kokkos::fence(); } catch (const std::exception& e) { if (Kokkos::is_initialized()) { Kokkos::finalize(); + MPI_Finalize(); } std::cerr << "Error: " << e.what() << std::endl; return 1; } Kokkos::finalize(); + MPI_Finalize(); return 0; } From 463eb49c4818baacb198d59b10578f6b9c0f8034 Mon Sep 17 00:00:00 2001 From: haykh Date: Thu, 11 Sep 2025 16:26:34 -0700 Subject: [PATCH 3/5] histogram in kokkos rand minimal test --- minimal/kokkos_rand.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/minimal/kokkos_rand.cpp b/minimal/kokkos_rand.cpp index cae9dea9..81f3e1b4 100644 --- a/minimal/kokkos_rand.cpp +++ b/minimal/kokkos_rand.cpp @@ -6,6 +6,29 @@ #include #include +template +void create_histogram(Kokkos::View& histogram, + T min, + T max, + Kokkos::View values) { + deep_copy(histogram, static_cast(0)); + Kokkos::View> histogram_atomic = histogram; + Kokkos::parallel_for( + "histogram", + values.extent(0), + KOKKOS_LAMBDA(std::size_t i) { + if (values(i) < min || values(i) >= max) { + return; + } + const std::size_t index = (1.0 * (values(i) - min) / (max - min)) * + histogram.extent(0); + if (index >= histogram.extent(0)) { + return; + } + histogram_atomic(index)++; + }); +} + auto main(int argc, char** argv) -> int { try { MPI_Init(&argc, &argv); @@ -39,6 +62,21 @@ auto main(int argc, char** argv) -> int { std::cout << "Rank " << rank << " finished\n" << std::flush; Kokkos::fence(); + MPI_Barrier(MPI_COMM_WORLD); + if (rank == 0) { + const int histogram_bins = 10; + float min = 0.0f; + float max = 0.1f; + Kokkos::View histogram("histogram", histogram_bins); + create_histogram(histogram, min, max, view); + auto h_histogram = Kokkos::create_mirror_view(histogram); + Kokkos::deep_copy(h_histogram, histogram); + std::cout << "hist: (" << min << ", " << max << ")\n"; + for (int i = 0; i < histogram_bins; ++i) { + std::cout << h_histogram(i) << " "; + } + std::cout << std::endl; + } } catch (const std::exception& e) { if (Kokkos::is_initialized()) { From 9ead72a96f36adb9e0ffea5e3e876400e171a8cd Mon Sep 17 00:00:00 2001 From: haykh Date: Thu, 11 Sep 2025 17:02:28 -0700 Subject: [PATCH 4/5] maxnpart check in uniform inject --- src/archetypes/particle_injector.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/archetypes/particle_injector.h b/src/archetypes/particle_injector.h index 6313031d..ff0a74be 100644 --- a/src/archetypes/particle_injector.h +++ b/src/archetypes/particle_injector.h @@ -482,6 +482,13 @@ namespace arch { const auto xi_min = std::get<2>(result); const auto xi_max = std::get<3>(result); + for (const auto sp : { injector.species.first, injector.species.second }) { + rase::ErrorIf(domain.species[sp - 1].npart() + nparticles >= + domain.species[sp - 1].maxnpart(), + "Trying to inject more particles than maxnpart", + HERE); + } + Kokkos::parallel_for( "InjectUniform", nparticles, @@ -596,6 +603,13 @@ namespace arch { const auto xi_min = std::get<2>(result); const auto xi_max = std::get<3>(result); + for (const auto sp : { injector.species.first, injector.species.second }) { + rase::ErrorIf(domain.species[sp - 1].npart() + nparticles >= + domain.species[sp - 1].maxnpart(), + "Trying to inject more particles than maxnpart", + HERE); + } + Kokkos::parallel_for( "InjectUniform", nparticles, From 77fc3bc66563e82009eaafcc18aa15646ac23348 Mon Sep 17 00:00:00 2001 From: LudwigBoess Date: Tue, 7 Oct 2025 17:37:22 +0000 Subject: [PATCH 5/5] bugfix --- src/archetypes/particle_injector.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/archetypes/particle_injector.h b/src/archetypes/particle_injector.h index ff0a74be..58919e38 100644 --- a/src/archetypes/particle_injector.h +++ b/src/archetypes/particle_injector.h @@ -483,7 +483,7 @@ namespace arch { const auto xi_max = std::get<3>(result); for (const auto sp : { injector.species.first, injector.species.second }) { - rase::ErrorIf(domain.species[sp - 1].npart() + nparticles >= + raise::ErrorIf(domain.species[sp - 1].npart() + nparticles >= domain.species[sp - 1].maxnpart(), "Trying to inject more particles than maxnpart", HERE); @@ -604,7 +604,7 @@ namespace arch { const auto xi_max = std::get<3>(result); for (const auto sp : { injector.species.first, injector.species.second }) { - rase::ErrorIf(domain.species[sp - 1].npart() + nparticles >= + raise::ErrorIf(domain.species[sp - 1].npart() + nparticles >= domain.species[sp - 1].maxnpart(), "Trying to inject more particles than maxnpart", HERE);