-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
fcece40
commit ddd29dc
Showing
4 changed files
with
92 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include <mpi/core/utility/container_adapter.hpp> | ||
|
||
namespace mpi | ||
{ | ||
// The type must satisfy compliant. | ||
template <typename type> | ||
class sync_variable | ||
{ | ||
public: | ||
explicit sync_variable (const communicator& communicator, const std::int32_t root = 0) | ||
: communicator_(communicator), window_(communicator, sizeof type), root_(root) | ||
{ | ||
|
||
} | ||
sync_variable (const sync_variable& that) = delete ; | ||
sync_variable ( sync_variable&& temp) = default; | ||
virtual ~sync_variable () = default; | ||
sync_variable& operator=(const sync_variable& that) = delete ; | ||
sync_variable& operator=( sync_variable&& temp) = default; | ||
|
||
void put(const type& value) | ||
{ | ||
window_.lock (root_, false); | ||
window_.put (value , root_, 0, 1, container_adapter<type>::data_type()); | ||
window_.unlock(root_); | ||
} | ||
[[nodiscard]] | ||
type get() const | ||
{ | ||
type result; | ||
window_.lock (root_, false); | ||
window_.get (result, root_, 0, 1, container_adapter<type>::data_type()); | ||
window_.unlock(root_); | ||
return result; | ||
} | ||
|
||
void synchronize() const | ||
{ | ||
window_.fence(); | ||
} | ||
|
||
protected: | ||
const communicator& communicator_; | ||
window window_ ; | ||
std::int32_t root_ ; | ||
}; | ||
} |
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,40 @@ | ||
#include "internal/doctest.h" | ||
|
||
#define MPI_USE_EXCEPTIONS | ||
|
||
#include <mpi/all.hpp> | ||
|
||
TEST_CASE("Sync Variable Test") | ||
{ | ||
mpi::environment environment ; | ||
const auto& communicator = mpi::world_communicator; | ||
|
||
for (auto i = 0; i < 1000; ++i) | ||
{ | ||
mpi::sync_variable<std::int32_t> variable(communicator); | ||
if (communicator.rank() == 0) | ||
variable.put(i); | ||
|
||
variable.synchronize(); // Why is this necessary :( | ||
|
||
if (communicator.rank() != 0) | ||
REQUIRE(variable.get() == i); | ||
} | ||
|
||
for (auto i = 0; i < 1000; ++i) | ||
{ | ||
mpi::sync_variable<std::array<std::int32_t, 3>> variable(communicator); | ||
if (communicator.rank() == 0) | ||
variable.put({i, i + 1, i + 2}); | ||
|
||
variable.synchronize(); // Why is this necessary :( | ||
|
||
if (communicator.rank() != 0) | ||
{ | ||
auto value = variable.get(); | ||
REQUIRE(value[0] == i ); | ||
REQUIRE(value[1] == i + 1); | ||
REQUIRE(value[2] == i + 2); | ||
} | ||
} | ||
} |