|
16 | 16 |
|
17 | 17 | #pragma once
|
18 | 18 |
|
| 19 | +#include "KokkosComm_include_mpi.hpp" |
19 | 20 | #include "KokkosComm_collective.hpp"
|
20 | 21 | #include "KokkosComm_version.hpp"
|
21 | 22 | #include "KokkosComm_isend.hpp"
|
|
28 | 29 |
|
29 | 30 | #include <Kokkos_Core.hpp>
|
30 | 31 |
|
| 32 | +#include <algorithm> |
| 33 | +#include <cstdio> |
| 34 | +#include <string_view> |
| 35 | + |
31 | 36 | namespace KokkosComm {
|
32 | 37 |
|
| 38 | +inline void initialize(int &argc, char ***argv) { |
| 39 | + int flag; |
| 40 | + MPI_Initialized(&flag); |
| 41 | + // Eagerly abort if MPI has already been initialized |
| 42 | + if (0 != flag) { |
| 43 | + int rank; |
| 44 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 45 | + if (0 == rank) { |
| 46 | + fprintf(stderr, "error: MPI must not be initialized prior to initializing KokkosComm\n"); |
| 47 | + } |
| 48 | + MPI_Abort(MPI_COMM_WORLD, -1); |
| 49 | + } |
| 50 | + |
| 51 | + int provided; |
| 52 | + MPI_Init_thread(&argc, argv, MPI_THREAD_MULTIPLE, &provided); |
| 53 | + int rank; |
| 54 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 55 | + |
| 56 | + // Abort if MPI failed to provide Thread Multiple |
| 57 | + if (MPI_THREAD_MULTIPLE != provided) { |
| 58 | + if (0 == rank) { |
| 59 | + fprintf(stderr, "error: failed to initialized with required thread support\n"); |
| 60 | + } |
| 61 | + MPI_Abort(MPI_COMM_WORLD, -1); |
| 62 | + } |
| 63 | + |
| 64 | + // Strip "--help" and "--kokkos-help" from the flags passed to Kokkos if we are not on rank 0 to prevent Kokkos |
| 65 | + // from printing the help message multiple times. |
| 66 | + if (0 != rank) { |
| 67 | + auto *help_it = std::find_if(*argv, *argv + argc, |
| 68 | + [](std::string_view const &x) { return x == "--help" || x == "--kokkos-help"; }); |
| 69 | + if (help_it != *argv + argc) { |
| 70 | + std::swap(*help_it, *(*argv + argc - 1)); |
| 71 | + --argc; |
| 72 | + } |
| 73 | + } |
| 74 | + Kokkos::initialize(argc, *argv); |
| 75 | +} |
| 76 | + |
| 77 | +inline void finalize() { |
| 78 | + Kokkos::finalize(); |
| 79 | + MPI_Finalize(); |
| 80 | +} |
| 81 | + |
33 | 82 | template <CommMode SendMode = CommMode::Default, KokkosExecutionSpace ExecSpace, KokkosView SendView>
|
34 | 83 | Req isend(const ExecSpace &space, const SendView &sv, int dest, int tag, MPI_Comm comm) {
|
35 | 84 | return Impl::isend<SendMode>(space, sv, dest, tag, comm);
|
|
0 commit comments