|
| 1 | +#include <cmath> |
| 2 | +#include <cstdio> |
| 3 | +#include <cstdlib> |
| 4 | +#include <cstring> |
| 5 | +#include <iostream> |
| 6 | +#include <limits> |
| 7 | + |
| 8 | +#include <Kokkos_Core.hpp> |
| 9 | + |
| 10 | +int main(int argc, char *argv[]) { |
| 11 | + Kokkos::initialize(argc, argv); |
| 12 | + { |
| 13 | + |
| 14 | + int provided; |
| 15 | + int initialized; |
| 16 | + MPI_Initialized(&initialized); |
| 17 | + if (!initialized) { |
| 18 | + MPI_Init_thread(nullptr, nullptr, MPI_THREAD_FUNNELED, &provided); |
| 19 | + } |
| 20 | + |
| 21 | + int rank, world_size; |
| 22 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 23 | + MPI_Comm_size(MPI_COMM_WORLD, &world_size); |
| 24 | + |
| 25 | + try { |
| 26 | + Kokkos::InitializationSettings settings; |
| 27 | + settings.set_device_id(rank % Kokkos::HIP::detect_device_count()); |
| 28 | + if (!Kokkos::is_initialized()) { |
| 29 | + //settings.set_num_threads(2); // if you want ... or more parameters |
| 30 | + Kokkos::initialize(settings); |
| 31 | + } |
| 32 | + |
| 33 | + { |
| 34 | + int n = 10; |
| 35 | + Kokkos::View<double*, Kokkos::HIP::memory_space> data("data", n); |
| 36 | + |
| 37 | + Kokkos::parallel_for(Kokkos::RangePolicy<Kokkos::HIP::execution_space>(0, n), |
| 38 | + KOKKOS_LAMBDA(const int i) { |
| 39 | + data(i) = rank * 1.0 + i; |
| 40 | + }); |
| 41 | + |
| 42 | + Kokkos::fence(); |
| 43 | + |
| 44 | + double local_sum = 0.0; |
| 45 | + Kokkos::parallel_reduce(Kokkos::RangePolicy<Kokkos::HIP::execution_space>(0, n), |
| 46 | + KOKKOS_LAMBDA(const int i, double& sum) { |
| 47 | + sum += data(i); |
| 48 | + }, local_sum); |
| 49 | + |
| 50 | + Kokkos::fence(); |
| 51 | + |
| 52 | + double global_sum; |
| 53 | + MPI_Allreduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); |
| 54 | + |
| 55 | + std::cout <<"rank["<<rank<< "] Lobale Sum : " << local_sum << std::endl; |
| 56 | + |
| 57 | + if (rank == 0) { |
| 58 | + std::cout << "Globale sum : " << global_sum << std::endl; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + //Kokkos::finalize(); |
| 63 | + |
| 64 | + if (Kokkos::is_initialized()) { |
| 65 | + Kokkos::finalize(); |
| 66 | + } |
| 67 | + } |
| 68 | + catch (std::exception& e) { |
| 69 | + std::cerr << "Exception caught on rank " << rank << ": " << e.what() << std::endl; |
| 70 | + MPI_Abort(MPI_COMM_WORLD, 1); |
| 71 | + } |
| 72 | + |
| 73 | + MPI_Initialized(&initialized); |
| 74 | + if (initialized) { MPI_Finalize(); } |
| 75 | + |
| 76 | + } |
| 77 | + Kokkos::finalize(); |
| 78 | + return 0; |
| 79 | +} |
0 commit comments