-
Notifications
You must be signed in to change notification settings - Fork 11
Draft kokkos-comm initialization/finalization using MPI_Session
s
#68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
5614211
45c695f
ea667db
bc31289
4852d9c
e37173b
aac626e
4bc55e2
afbb017
89e3ee3
dcc4584
de176b6
bb58138
c2812bd
4ceee5f
40ba7e7
822722d
8faf205
d064469
f0d5285
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//@HEADER | ||
// ************************************************************************ | ||
// | ||
// Kokkos v. 4.0 | ||
// Copyright (2022) National Technology & Engineering | ||
// Solutions of Sandia, LLC (NTESS). | ||
// | ||
// Under the terms of Contract DE-NA0003525 with NTESS, | ||
// the U.S. Government retains certain rights in this software. | ||
// | ||
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://kokkos.org/LICENSE for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//@HEADER | ||
|
||
#pragma once | ||
|
||
#include "KokkosComm_concepts.hpp" | ||
|
||
#include <Kokkos_Core_fwd.hpp> | ||
#include <cstdio> | ||
#include <mpi.h> | ||
|
||
namespace KokkosComm { | ||
|
||
using Color = int; | ||
using Key = int; | ||
|
||
template <KokkosExecutionSpace ExecSpace = Kokkos::DefaultExecutionSpace> | ||
class Communicator { | ||
private: | ||
MPI_Comm _comm; | ||
ExecSpace _exec_space; | ||
|
||
public: | ||
Communicator(MPI_Comm comm) : _comm(comm) {} | ||
Communicator(const Communicator& other) = delete; | ||
Communicator(const Communicator&& other) { _comm = std::move(other._comm); } | ||
~Communicator() { | ||
// Only free the communicator if it hasn't been set to `MPI_COMM_NULL` before. This is to prevent double freeing | ||
// when we explicitly call the communicator's dtor in the `Context` dtor. | ||
if (MPI_COMM_NULL != _comm) { | ||
MPI_Comm_free(&_comm); | ||
} | ||
} | ||
|
||
static auto dup_raw(MPI_Comm raw) -> Communicator { | ||
MPI_Comm new_comm; | ||
MPI_Comm_dup(raw, &new_comm); | ||
return Communicator(new_comm); | ||
} | ||
|
||
static auto dup(const Communicator& other) -> Communicator { return Communicator::dup_raw(other.as_raw()); } | ||
|
||
static auto split_raw(MPI_Comm raw, Color color, Key key) -> Communicator { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason why this isn't just an overload of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to make it explicit that we're splitting from a raw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that's a design decision that the Kokkos community should make (to be consistent with the other projects) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can drop the Raw. We typically are happy with overloads. |
||
MPI_Comm new_comm; | ||
MPI_Comm_split(raw, color, key, &new_comm); | ||
return Communicator(new_comm); | ||
} | ||
|
||
static auto split(const Communicator& other, Color color, Key key) -> Communicator { | ||
return Communicator::split_raw(other.as_raw(), color, key); | ||
} | ||
|
||
inline auto as_raw() const -> MPI_Comm { return _comm; } | ||
|
||
inline auto size(void) const -> int { | ||
int size; | ||
MPI_Comm_size(_comm, &size); | ||
return size; | ||
} | ||
|
||
inline auto rank(void) const -> int { | ||
int rank; | ||
MPI_Comm_rank(_comm, &rank); | ||
return rank; | ||
} | ||
}; | ||
|
||
} // namespace KokkosComm |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//@HEADER | ||
// ************************************************************************ | ||
// | ||
// Kokkos v. 4.0 | ||
// Copyright (2022) National Technology & Engineering | ||
// Solutions of Sandia, LLC (NTESS). | ||
// | ||
// Under the terms of Contract DE-NA0003525 with NTESS, | ||
// the U.S. Government retains certain rights in this software. | ||
// | ||
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://kokkos.org/LICENSE for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//@HEADER | ||
|
||
#pragma once | ||
|
||
#include "KokkosComm_communicator.hpp" | ||
#include "KokkosComm_concepts.hpp" | ||
|
||
#include <Kokkos_Core.hpp> | ||
#include <mpi.h> | ||
|
||
namespace KokkosComm { | ||
|
||
template <KokkosExecutionSpace ExecSpace> | ||
class Context { | ||
private: | ||
MPI_Session _shandle; | ||
Communicator<ExecSpace> _comm; | ||
|
||
public: | ||
Context(MPI_Session shandle, MPI_Comm comm) : _shandle(shandle), _comm(Communicator<ExecSpace>(comm)) {} | ||
|
||
~Context() { | ||
// Ensure the session-associated communicator is destroyed before the session is finalized. | ||
_comm.~Communicator(); | ||
MPI_Session_finalize(&_shandle); | ||
} | ||
|
||
auto comm(void) -> const Communicator<ExecSpace>& { return _comm; } | ||
}; | ||
|
||
template <KokkosExecutionSpace ExecSpace> | ||
auto initialize(void) -> Context<ExecSpace> { | ||
int rc; | ||
|
||
MPI_Session kokkoscomm_shandle = MPI_SESSION_NULL; | ||
MPI_Group kokkoscomm_group = MPI_GROUP_NULL; | ||
MPI_Comm kokkoscomm_comm = MPI_COMM_NULL; | ||
MPI_Info kokkoscomm_info = MPI_INFO_NULL; | ||
|
||
MPI_Info_create(&kokkoscomm_info); | ||
|
||
// Set threading level for our session | ||
constexpr char thrd_lvl_key[] = "thread_level"; | ||
constexpr char thrd_lvl_val[] = "MPI_THREAD_MULTIPLE"; | ||
MPI_Info_set(kokkoscomm_info, thrd_lvl_key, thrd_lvl_val); | ||
|
||
#ifdef KOKKOSCOMM_CUDA_AWARE_MPI | ||
// Disable CUDA pointer attribute checks from MPI | ||
constexpr char cu_ptr_attr_key[] = "mpi_communication_pattern"; | ||
constexpr char cu_ptr_attr_val[] = "MPI_CPU_TO_GPU"; | ||
MPI_Info_set(kokkoscomm_info, cu_ptr_attr_key, cu_ptr_attr_val); | ||
#endif | ||
|
||
rc = MPI_Session_init(kokkoscomm_info, MPI_ERRORS_RETURN, &kokkoscomm_shandle); | ||
|
||
constexpr char pset_name[] = "mpi://WORLD"; | ||
MPI_Group_from_session_pset(kokkoscomm_shandle, pset_name, &kokkoscomm_group); | ||
|
||
MPI_Comm_create_from_group(kokkoscomm_group, "kokkos-comm.default_session", MPI_INFO_NULL, MPI_ERRORS_RETURN, | ||
&kokkoscomm_comm); | ||
|
||
return Context<ExecSpace>(kokkoscomm_shandle, kokkoscomm_comm); | ||
} | ||
|
||
} // namespace KokkosComm |
Uh oh!
There was an error while loading. Please reload this page.