Skip to content

Commit

Permalink
Merge pull request #6401 from nikwit/singleton-change-phase
Browse files Browse the repository at this point in the history
Allow Singletons to use `ExecutePhaseChange`
  • Loading branch information
knelli2 authored Dec 13, 2024
2 parents 9f25567 + 093595a commit cbe1362
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 36 deletions.
2 changes: 1 addition & 1 deletion docs/DevGuide/Parallelization.md
Original file line number Diff line number Diff line change
Expand Up @@ -866,4 +866,4 @@ also be run. Which functions are run on each node and core is set by calling
`Parallel::charmxx::register_init_node_and_proc` in `CkRegisterMainModule()`
with function pointers to the functions to be called. For example:

\snippet Test_AlgorithmPhaseControl.cpp charm_init_funcs_example
\snippet Test_AlgorithmPhaseControlNodegroup.cpp charm_init_funcs_example
38 changes: 21 additions & 17 deletions src/Parallel/PhaseControl/ContributeToPhaseChangeReduction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,27 @@ void contribute_to_phase_change_reduction(
PhaseControl::TaggedTupleCombine, Ts...>(nullptr),
cache.get_main_proxy().value());
reduction_data_type reduction_data{data_for_reduction};
// Note that Singletons could be supported by directly calling the main
// entry function, but due to this and other peculiarities with
// Singletons, it is best to discourage their use.
static_assert(
not std::is_same_v<typename SenderComponent::chare_type,
Parallel::Algorithms::Singleton>,
"Phase change reduction is not supported for singleton chares. "
"Consider constructing your chare as a length-1 array chare if you "
"need to contribute to phase change data");
Parallel::local_branch(
Parallel::get_parallel_component<SenderComponent>(cache))
->contribute(static_cast<int>(reduction_data.size()),
reduction_data.packed().get(),
Parallel::charmxx::charm_reducer_functions.at(
std::hash<Parallel::charmxx::ReducerFunctions>{}(
&reduction_data_type::combine)),
callback);

if constexpr (std::is_same_v<typename SenderComponent::chare_type,
Parallel::Algorithms::Singleton>) {
Parallel::local(Parallel::get_parallel_component<SenderComponent>(cache))
->contribute(static_cast<int>(reduction_data.size()),
reduction_data.packed().get(),
Parallel::charmxx::charm_reducer_functions.at(
std::hash<Parallel::charmxx::ReducerFunctions>{}(
&reduction_data_type::combine)),
callback);

} else {
Parallel::local_branch(
Parallel::get_parallel_component<SenderComponent>(cache))
->contribute(static_cast<int>(reduction_data.size()),
reduction_data.packed().get(),
Parallel::charmxx::charm_reducer_functions.at(
std::hash<Parallel::charmxx::ReducerFunctions>{}(
&reduction_data_type::combine)),
callback);
}
}
/// @}
} // namespace Parallel
7 changes: 5 additions & 2 deletions tests/Unit/Parallel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,11 @@ add_algorithm_test(
"AlgorithmGlobalCache"
INPUT_FILE "Test_AlgorithmGlobalCache.yaml")
add_algorithm_test(
"AlgorithmPhaseControl"
INPUT_FILE "Test_AlgorithmPhaseControl.yaml")
"AlgorithmPhaseControlSingleton"
INPUT_FILE "Test_AlgorithmPhaseControlSingleton.yaml")
add_algorithm_test(
"AlgorithmPhaseControlNodegroup"
INPUT_FILE "Test_AlgorithmPhaseControlNodegroup.yaml")
add_algorithm_test(
"DynamicInsertionState"
INPUT_FILE "Test_DynamicInsertionState.yaml")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#pragma once

#include "Framework/TestingFramework.hpp"

#include <cstddef>
Expand Down Expand Up @@ -165,7 +167,7 @@ struct ComponentAlpha {

template <typename Metavariables>
struct ComponentBeta {
using chare_type = Parallel::Algorithms::Nodegroup;
using chare_type = typename Metavariables::component_beta_chare_type;
using metavariables = Metavariables;
using array_index = size_t;

Expand Down Expand Up @@ -274,7 +276,7 @@ struct TerminateAndRestart {
const ArrayIndex& /*array_index*/, const ActionList /*meta*/,
const ParallelComponent* const /*meta*/) {
if (db::get<Tags::Step>(box) % interval == 0) {
if(db::get<Tags::Step>(box) < 15) {
if (db::get<Tags::Step>(box) < 15) {
Parallel::simple_action<Actions::RestartMe<ParallelComponent>>(
Parallel::get_parallel_component<OtherComponent>(cache));

Expand Down Expand Up @@ -335,9 +337,12 @@ struct Testing {
// 7: Register | none
// 10: none | Solve
// 14: none | Register and Solve
template <typename ComponentBetaChareType>
struct TestMetavariables {
using component_list = tmpl::list<ComponentAlpha<TestMetavariables>,
ComponentBeta<TestMetavariables>>;
using component_beta_chare_type = ComponentBetaChareType;
using component_list =
tmpl::list<ComponentAlpha<TestMetavariables<ComponentBetaChareType>>,
ComponentBeta<TestMetavariables<ComponentBetaChareType>>>;

struct factory_creation
: tt::ConformsTo<Options::protocols::FactoryCreation> {
Expand All @@ -364,7 +369,8 @@ struct TestMetavariables {
}

static std::string expected_log(
tmpl::type_<ComponentAlpha<TestMetavariables>> /*meta*/) {
tmpl::type_<
ComponentAlpha<TestMetavariables<ComponentBetaChareType>>> /*meta*/) {
return "Running phase: Initialization\n" +
repeat("Running phase: Evolve\n", 2_st) +
"Terminate and Restart\n"
Expand Down Expand Up @@ -397,7 +403,8 @@ struct TestMetavariables {
}

static std::string expected_log(
tmpl::type_<ComponentBeta<TestMetavariables>> /*meta*/) {
tmpl::type_<
ComponentBeta<TestMetavariables<ComponentBetaChareType>>> /*meta*/) {
return "Running phase: Initialization\n" +
repeat("Running phase: Evolve\n", 3_st) + // steps 1-3 -> Solve
"Running phase: Solve\n"
Expand Down Expand Up @@ -429,11 +436,3 @@ struct TestMetavariables {
// NOLINTNEXTLINE(google-runtime-references)
void pup(PUP::er& /*p*/) {}
};

// [charm_init_funcs_example]
extern "C" void CkRegisterMainModule() {
Parallel::charmxx::register_main_module<TestMetavariables>();
Parallel::charmxx::register_init_node_and_proc(
{&register_factory_classes_with_charm<TestMetavariables>}, {});
}
// [charm_init_funcs_example]
17 changes: 17 additions & 0 deletions tests/Unit/Parallel/Test_AlgorithmPhaseControlNodegroup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#include "Parallel/Test_AlgorithmPhaseControl.hpp"

#include "Parallel/Algorithms/AlgorithmNodegroup.hpp"

// [charm_init_funcs_example]
extern "C" void CkRegisterMainModule() {
Parallel::charmxx::register_main_module<
TestMetavariables<Parallel::Algorithms::Nodegroup>>();
Parallel::charmxx::register_init_node_and_proc(
{&register_factory_classes_with_charm<
TestMetavariables<Parallel::Algorithms::Nodegroup>>},
{});
}
// [charm_init_funcs_example]
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
---
---

# note that this is a vector of pairs, so has the peculiar '- -' for the
# elements
PhaseChangeAndTriggers:
- Trigger: RegisterTrigger
PhaseChanges:
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Parallel/Test_AlgorithmPhaseControlSingleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#include "Parallel/Test_AlgorithmPhaseControl.hpp"

#include "Parallel/Algorithms/AlgorithmSingleton.hpp"

extern "C" void CkRegisterMainModule() {
Parallel::charmxx::register_main_module<
TestMetavariables<Parallel::Algorithms::Singleton>>();
Parallel::charmxx::register_init_node_and_proc(
{&register_factory_classes_with_charm<
TestMetavariables<Parallel::Algorithms::Singleton>>},
{});
}
17 changes: 17 additions & 0 deletions tests/Unit/Parallel/Test_AlgorithmPhaseControlSingleton.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

---
---

PhaseChangeAndTriggers:
- Trigger: RegisterTrigger
PhaseChanges:
- VisitAndReturn(Register)
- Trigger: SolveTrigger
PhaseChanges:
- VisitAndReturn(Solve)

ResourceInfo:
AvoidGlobalProc0: false
Singletons: Auto

0 comments on commit cbe1362

Please sign in to comment.