Skip to content

Commit 0cb31f7

Browse files
committed
feat(init/fini): add basic initialization and finalization
KokkosComm should now be initialized (and finalized) these new functions. Users now must _not_ initialize/finalize MPI, nor Kokkos by themselves.
1 parent 2e34aef commit 0cb31f7

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/KokkosComm.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#pragma once
1818

19+
#include "KokkosComm_include_mpi.hpp"
1920
#include "KokkosComm_collective.hpp"
2021
#include "KokkosComm_version.hpp"
2122
#include "KokkosComm_isend.hpp"
@@ -28,8 +29,56 @@
2829

2930
#include <Kokkos_Core.hpp>
3031

32+
#include <algorithm>
33+
#include <cstdio>
34+
#include <string_view>
35+
3136
namespace KokkosComm {
3237

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+
3382
template <CommMode SendMode = CommMode::Default, KokkosExecutionSpace ExecSpace, KokkosView SendView>
3483
Req isend(const ExecSpace &space, const SendView &sv, int dest, int tag, MPI_Comm comm) {
3584
return Impl::isend<SendMode>(space, sv, dest, tag, comm);

0 commit comments

Comments
 (0)