-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include <mpi/core/communicators/communicator.hpp> | ||
#include <mpi/core/type/type_traits.hpp> | ||
#include <mpi/core/window.hpp> | ||
|
||
namespace mpi | ||
{ | ||
// The type must be compliant. | ||
// Requires .synchronize() to be called explicitly to ensure synchronization. | ||
template <typename type> | ||
class manual_shared_variable | ||
{ | ||
public: | ||
explicit manual_shared_variable (const communicator& communicator, const std::int32_t root = 0) | ||
: communicator_(communicator), root_(root), window_(communicator_, communicator_.rank() == root_ ? type_traits<type>::get_data_type().size() : 0) | ||
{ | ||
|
||
} | ||
manual_shared_variable (const manual_shared_variable& that) = delete ; | ||
manual_shared_variable ( manual_shared_variable&& temp) = default; | ||
virtual ~manual_shared_variable () = default; | ||
manual_shared_variable& operator=(const manual_shared_variable& that) = delete ; | ||
manual_shared_variable& operator=( manual_shared_variable&& temp) = default; | ||
manual_shared_variable& operator=(const type& value) | ||
{ | ||
set(value); | ||
return *this; | ||
} | ||
operator type() const | ||
{ | ||
return get(); | ||
} | ||
|
||
void set (const type& value) | ||
{ | ||
window_.lock (root_, false); | ||
window_.put (value , root_, 0, 1, type_traits<type>::get_data_type()); | ||
window_.unlock(root_); | ||
} | ||
[[nodiscard]] | ||
type get () const | ||
{ | ||
type result {}; | ||
window_.lock (root_, true); | ||
window_.get (result, root_, 0, 1, type_traits<type>::get_data_type()); | ||
window_.unlock(root_); | ||
return result; | ||
} | ||
|
||
void synchronize() const | ||
{ | ||
window_.fence(); | ||
} | ||
|
||
protected: | ||
const communicator& communicator_; | ||
std::int32_t root_ ; | ||
window window_ ; | ||
}; | ||
|
||
// The type must be compliant. | ||
// Calls .synchronize() globally after a .set_if_rank(...), ensuring synchronization. | ||
// Note that .set_if_rank(...) must be called on all processes in the communicator as if its a collective. | ||
template <typename type> | ||
class automatic_shared_variable : public manual_shared_variable<type> | ||
{ | ||
public: | ||
using base = manual_shared_variable<type>; | ||
|
||
explicit automatic_shared_variable (const communicator& communicator, const std::int32_t root = 0) | ||
: base(communicator, root) | ||
{ | ||
|
||
} | ||
automatic_shared_variable (const automatic_shared_variable& that) = delete ; | ||
automatic_shared_variable ( automatic_shared_variable&& temp) = default; | ||
~automatic_shared_variable () override = default; | ||
automatic_shared_variable& operator=(const automatic_shared_variable& that) = delete ; | ||
automatic_shared_variable& operator=( automatic_shared_variable&& temp) = default; | ||
automatic_shared_variable& operator=(const type& value) = delete ; | ||
operator type() const | ||
{ | ||
return base::get(); | ||
} | ||
|
||
void set_if_rank(const type& value, const std::int32_t rank) | ||
{ | ||
if (base::communicator_.rank() == rank) | ||
base::set(value); | ||
base::synchronize(); | ||
} | ||
}; | ||
|
||
template <typename type> | ||
using shared_variable = automatic_shared_variable<type>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
#include <iostream> | ||
|
||
#include "internal/doctest.h" | ||
|
||
#define MPI_USE_EXCEPTIONS | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "internal/doctest.h" | ||
|
||
#define MPI_USE_EXCEPTIONS | ||
|
||
#include <mpi/all.hpp> | ||
|
||
TEST_CASE("Shared Variable Test") | ||
{ | ||
mpi::environment environment ; | ||
const auto& communicator = mpi::world_communicator; | ||
|
||
{ | ||
mpi::manual_shared_variable<std::int32_t> variable(communicator); | ||
|
||
if (communicator.rank() == 0) | ||
variable = 42; | ||
|
||
variable.synchronize(); // :( | ||
|
||
if (communicator.rank() != 0) | ||
REQUIRE(variable == 42); | ||
} | ||
|
||
{ | ||
mpi::manual_shared_variable<std::array<std::int32_t, 3>> variable(communicator); | ||
if (communicator.rank() == 0) | ||
variable = {1, 2, 3}; | ||
|
||
variable.synchronize(); // :( | ||
|
||
if (communicator.rank() != 0) | ||
{ | ||
auto value = variable.get(); | ||
REQUIRE(value[0] == 1); | ||
REQUIRE(value[1] == 2); | ||
REQUIRE(value[2] == 3); | ||
} | ||
} | ||
|
||
{ | ||
mpi::shared_variable<std::int32_t> variable(communicator); | ||
variable.set_if_rank(42, 0); | ||
if (communicator.rank() != 0) | ||
REQUIRE(variable == 42); | ||
} | ||
|
||
{ | ||
mpi::shared_variable<std::array<std::int32_t, 3>> variable(communicator); | ||
variable.set_if_rank({1, 2, 3}, 0); | ||
if (communicator.rank() != 0) | ||
{ | ||
auto value = variable.get(); | ||
REQUIRE(value[0] == 1); | ||
REQUIRE(value[1] == 2); | ||
REQUIRE(value[2] == 3); | ||
} | ||
} | ||
} |