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

Allow Singletons to use ExecutePhaseChange #6401

Merged
merged 1 commit into from
Dec 13, 2024
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
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()),
Comment on lines +76 to +79
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that technically this could have been put into the other contribute_to_phase_change_reduction function that already uses Parallel::local, but then every phase change would have to be updated to call the other function for both arrays and singletons. I think this current way is cleaner.

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
Loading