Skip to content

Commit

Permalink
Sync variable progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
acdemiralp committed Nov 11, 2021
1 parent fcece40 commit ddd29dc
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/mpi/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

#include <mpi/extensions/detach.hpp>
#include <mpi/extensions/future.hpp>
#include <mpi/extensions/sync_variable.hpp>

#include <mpi/io/enums/access_mode.hpp>
#include <mpi/io/enums/seek_mode.hpp>
Expand Down
51 changes: 51 additions & 0 deletions include/mpi/extensions/sync_variable.hpp
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_ ;
};
}
2 changes: 0 additions & 2 deletions tests/future_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <iostream>

#include "internal/doctest.h"

#define MPI_USE_EXCEPTIONS
Expand Down
40 changes: 40 additions & 0 deletions tests/sync_variable_test.cpp
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);
}
}
}

0 comments on commit ddd29dc

Please sign in to comment.