Skip to content
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

Register derived parallel callbacks #5607

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/Parallel/Callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "Parallel/Invoke.hpp"
#include "Utilities/Serialization/CharmPupable.hpp"
#include "Utilities/Serialization/RegisterDerivedClassesWithCharm.hpp"

namespace Parallel {
/// An abstract base class, whose derived class holds a function that
Expand All @@ -28,6 +29,7 @@ class Callback : public PUP::able {
~Callback() override = default;
explicit Callback(CkMigrateMessage* msg) : PUP::able(msg) {}
virtual void invoke() = 0;
virtual void register_with_charm() = 0;
};

/// Wraps a call to a simple action and its arguments.
Expand All @@ -54,8 +56,17 @@ class SimpleActionCallback : public Callback {
p | args_;
}

void register_with_charm() override {
static bool done_registration{false};
if (done_registration) {
return;
}
done_registration = true;
register_classes_with_charm<SimpleActionCallback>();
}

private:
Proxy proxy_{};
std::decay_t<Proxy> proxy_{};
std::tuple<std::decay_t<Args>...> args_{};
};

Expand All @@ -65,15 +76,25 @@ class SimpleActionCallback<SimpleAction, Proxy> : public Callback {
public:
WRAPPED_PUPable_decl_template(SimpleActionCallback); // NOLINT
SimpleActionCallback() = default;
// NOLINTNEXTLINE(google-explicit-constructor)
SimpleActionCallback(Proxy proxy) : proxy_(proxy) {}
SimpleActionCallback(CkMigrateMessage* msg) : Callback(msg) {}
using PUP::able::register_constructor;
void invoke() override { Parallel::simple_action<SimpleAction>(proxy_); }

void pup(PUP::er& p) override { p | proxy_; }

void register_with_charm() override {
static bool done_registration{false};
if (done_registration) {
return;
}
done_registration = true;
register_classes_with_charm<SimpleActionCallback>();
}

private:
Proxy proxy_{};
std::decay_t<Proxy> proxy_{};
};

/// Wraps a call to perform_algorithm.
Expand All @@ -82,14 +103,24 @@ class PerformAlgorithmCallback : public Callback {
public:
WRAPPED_PUPable_decl_template(PerformAlgorithmCallback); // NOLINT
PerformAlgorithmCallback() = default;
// NOLINTNEXTLINE(google-explicit-constructor)
PerformAlgorithmCallback(Proxy proxy) : proxy_(proxy) {}
PerformAlgorithmCallback(CkMigrateMessage* msg) : Callback(msg) {}
using PUP::able::register_constructor;
void invoke() override { proxy_.perform_algorithm(); }
void pup(PUP::er& p) override { p | proxy_; }

void register_with_charm() override {
static bool done_registration{false};
if (done_registration) {
return;
}
done_registration = true;
register_classes_with_charm<PerformAlgorithmCallback>();
}

private:
Proxy proxy_{};
std::decay_t<Proxy> proxy_{};
};

/// \cond
Expand Down
1 change: 1 addition & 0 deletions src/Parallel/GlobalCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ bool GlobalCache<Metavariables>::mutable_cache_item_is_ready(
};

if (callback_was_registered()) {
optional_callback->register_with_charm();
// Second mutex is for vector of callbacks
std::mutex& mutex = tuples::get<MutexTag<tag>>(mutexes_).second;
{
Expand Down
31 changes: 17 additions & 14 deletions tests/Unit/Framework/ActionTesting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <algorithm>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong class name in the commit message. (Should be MockDistributedObjectProxy.)

#include <charm++.h>
#include <cstddef>
#include <memory>
#include <optional>
#include <tuple>
#include <unordered_map>
Expand Down Expand Up @@ -457,13 +458,15 @@ class MockDistributedObjectProxy : public CProxyElement_ArrayElement {
public:
using Inbox = tuples::tagged_tuple_from_typelist<InboxTagList>;

MockDistributedObjectProxy() = default;

MockDistributedObjectProxy(
size_t mock_node, size_t mock_local_core,
MockDistributedObject<Component>& mock_distributed_object, Inbox& inbox)
: mock_node_(mock_node),
mock_local_core_(mock_local_core),
mock_distributed_object_(mock_distributed_object),
inbox_(inbox) {}
mock_distributed_object_(&mock_distributed_object),
inbox_(&inbox) {}

template <typename InboxTag, typename Data>
void receive_data(const typename InboxTag::temporal_id& id, Data&& data,
Expand All @@ -472,49 +475,49 @@ class MockDistributedObjectProxy : public CProxyElement_ArrayElement {
// not needed now. However, it is required by the interface to be compliant
// with the Algorithm invocations.
(void)enable_if_disabled;
InboxTag::insert_into_inbox(make_not_null(&tuples::get<InboxTag>(inbox_)),
InboxTag::insert_into_inbox(make_not_null(&tuples::get<InboxTag>(*inbox_)),
id, std::forward<Data>(data));
}

template <typename InboxTag, typename MessageType>
void receive_data(MessageType* message) {
InboxTag::insert_into_inbox(make_not_null(&tuples::get<InboxTag>(inbox_)),
InboxTag::insert_into_inbox(make_not_null(&tuples::get<InboxTag>(*inbox_)),
message);
}

template <typename Action, typename... Args>
void simple_action(std::tuple<Args...> args) {
mock_distributed_object_.template simple_action<Action>(std::move(args));
mock_distributed_object_->template simple_action<Action>(std::move(args));
}

template <typename Action>
void simple_action() {
mock_distributed_object_.template simple_action<Action>();
mock_distributed_object_->template simple_action<Action>();
}

template <typename Action, typename... Args>
void threaded_action(std::tuple<Args...> args) {
mock_distributed_object_.template threaded_action<Action>(std::move(args));
mock_distributed_object_->template threaded_action<Action>(std::move(args));
}

template <typename Action>
void threaded_action() {
mock_distributed_object_.template threaded_action<Action>();
mock_distributed_object_->template threaded_action<Action>();
}

void set_terminate(bool t) { mock_distributed_object_.set_terminate(t); }
void set_terminate(bool t) { mock_distributed_object_->set_terminate(t); }

// Actions may call this, but since tests step through actions manually it has
// no effect.
void perform_algorithm() {}
void perform_algorithm(const bool /*restart_if_terminated*/) {}

MockDistributedObject<Component>* ckLocal() {
return (mock_distributed_object_.my_node() ==
return (mock_distributed_object_->my_node() ==
static_cast<int>(mock_node_) and
mock_distributed_object_.my_local_rank() ==
mock_distributed_object_->my_local_rank() ==
static_cast<int>(mock_local_core_))
? &mock_distributed_object_
? mock_distributed_object_
: nullptr;
}

Expand Down Expand Up @@ -549,8 +552,8 @@ class MockDistributedObjectProxy : public CProxyElement_ArrayElement {
// lives on.
size_t mock_node_{0};
size_t mock_local_core_{0};
MockDistributedObject<Component>& mock_distributed_object_;
Inbox& inbox_;
MockDistributedObject<Component>* mock_distributed_object_{nullptr};
Inbox* inbox_{nullptr};
};

template <typename ChareType>
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Parallel/Test_GlobalCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ class UseCkCallbackAsCallback : public Parallel::Callback {
using PUP::able::register_constructor;
void invoke() override { callback_.send(nullptr); }
void pup(PUP::er& p) override { p | callback_; }
// We shouldn't be pupping so registration doesn't matter
void register_with_charm() override {}

private:
CkCallback callback_;
Expand Down
Loading