From b6e4c40b57700e0ed32e437260eb44d70d49980b Mon Sep 17 00:00:00 2001 From: Yu Zhang Date: Tue, 16 Jan 2024 20:36:00 -0600 Subject: [PATCH 01/11] Add finalize callbacks into RuntimeView --- CMakeLists.txt | 2 +- .../runtime/detail_/runtime_view_pimpl.hpp | 7 +++++++ .../runtime/detail_/runtime_view_pimpl.ipp | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b281f79b..a6c4afe2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,8 +30,8 @@ set( CACHE STRING "" FORCE ) -include(get_cmaize) include(nwx_versions) +include(get_cmaize) ### Options ### # These are the default values, used only if the user hasn't already set them. diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp index 2026b61c..1de2409f 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp @@ -126,6 +126,13 @@ struct RuntimeViewPIMPL { /// Pointer to the logger (pointer to allow logging with const ResourceSets) logger_pointer m_plogger; + /// Stacks of initialize and finalize callback functions + //std::stack> m_callbacks_init; + std::stack> m_callbacks_final; + + /// Register callback functions to stacks + void callback_register_stack(std::function cb_func, std::stack> m_stack); + private: /** @brief Wraps the process of instantiating a ResourceSet. * diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp index 200e59c9..441de64c 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp @@ -25,6 +25,12 @@ namespace parallelzone::runtime::detail_ { +void callback_register_stack(std::function cb_func, std::stack> m_stack) { + m_stack.push(cb_func); +} + +void mpi_finalize_wrapper() { MPI_Finalize(); } + inline RuntimeViewPIMPL::RuntimeViewPIMPL(bool did_i_start_mpi, comm_type comm, logger_type logger) : m_did_i_start_mpi(did_i_start_mpi), @@ -33,11 +39,20 @@ inline RuntimeViewPIMPL::RuntimeViewPIMPL(bool did_i_start_mpi, comm_type comm, m_resource_sets_() { // Pre-populate the current rank's resource set. instantiate_resource_set_(m_comm.me()); + + /// Register the finalize callbacks + callback_register_stack(std::function{&mpi_finalize_wrapper}, m_callbacks_final); } inline RuntimeViewPIMPL::~RuntimeViewPIMPL() noexcept { if(!m_did_i_start_mpi) return; - MPI_Finalize(); + //MPI_Finalize(); + + /// call the initialize callback functions + while(!m_callbacks_final.empty()) { + m_callbacks_final.top()(); + m_callbacks_final.pop(); + } } inline RuntimeViewPIMPL::const_resource_set_reference RuntimeViewPIMPL::at( From 0f542984eef7348d3c8fcdf19ad27eb12d300384 Mon Sep 17 00:00:00 2001 From: yzhang Date: Wed, 17 Jan 2024 16:32:53 -0600 Subject: [PATCH 02/11] Fix the scope issue of callback_register_stack --- src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp index 441de64c..086d6d41 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp @@ -25,7 +25,7 @@ namespace parallelzone::runtime::detail_ { -void callback_register_stack(std::function cb_func, std::stack> m_stack) { +inline void RuntimeViewPIMPL::callback_register_stack(std::function cb_func, std::stack> m_stack) { m_stack.push(cb_func); } From 5599af9bf58668eb1f964d13f5bead8ee6c215e0 Mon Sep 17 00:00:00 2001 From: yzhang Date: Thu, 18 Jan 2024 12:35:33 -0600 Subject: [PATCH 03/11] Add functional and stack head files and change function name --- src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp | 4 +++- src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp index 1de2409f..9af65af4 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp @@ -16,6 +16,8 @@ #pragma once #include +#include +#include namespace parallelzone::runtime::detail_ { @@ -131,7 +133,7 @@ struct RuntimeViewPIMPL { std::stack> m_callbacks_final; /// Register callback functions to stacks - void callback_register_stack(std::function cb_func, std::stack> m_stack); + void stack_callback(std::function cb_func, std::stack> m_stack); private: /** @brief Wraps the process of instantiating a ResourceSet. diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp index 086d6d41..9a22123d 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp @@ -25,7 +25,7 @@ namespace parallelzone::runtime::detail_ { -inline void RuntimeViewPIMPL::callback_register_stack(std::function cb_func, std::stack> m_stack) { +inline void RuntimeViewPIMPL::stack_callback(std::function cb_func, std::stack> m_stack) { m_stack.push(cb_func); } @@ -41,7 +41,7 @@ inline RuntimeViewPIMPL::RuntimeViewPIMPL(bool did_i_start_mpi, comm_type comm, instantiate_resource_set_(m_comm.me()); /// Register the finalize callbacks - callback_register_stack(std::function{&mpi_finalize_wrapper}, m_callbacks_final); + stack_callback(std::function{&mpi_finalize_wrapper}, m_callbacks_final); } inline RuntimeViewPIMPL::~RuntimeViewPIMPL() noexcept { From 9502688e572a92a4a04dd621b5e71bf9cd07a642 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 18 Jan 2024 18:40:59 +0000 Subject: [PATCH 04/11] Committing clang-format changes --- .../runtime/detail_/runtime_view_pimpl.hpp | 7 ++++--- .../runtime/detail_/runtime_view_pimpl.ipp | 18 ++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp index 9af65af4..7afcddb0 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp @@ -15,8 +15,8 @@ */ #pragma once -#include #include +#include #include namespace parallelzone::runtime::detail_ { @@ -129,11 +129,12 @@ struct RuntimeViewPIMPL { logger_pointer m_plogger; /// Stacks of initialize and finalize callback functions - //std::stack> m_callbacks_init; + // std::stack> m_callbacks_init; std::stack> m_callbacks_final; /// Register callback functions to stacks - void stack_callback(std::function cb_func, std::stack> m_stack); + void stack_callback(std::function cb_func, + std::stack> m_stack); private: /** @brief Wraps the process of instantiating a ResourceSet. diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp index 9a22123d..96d612e1 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp @@ -25,11 +25,12 @@ namespace parallelzone::runtime::detail_ { -inline void RuntimeViewPIMPL::stack_callback(std::function cb_func, std::stack> m_stack) { +inline void RuntimeViewPIMPL::stack_callback( + std::function cb_func, std::stack> m_stack) { m_stack.push(cb_func); } -void mpi_finalize_wrapper() { MPI_Finalize(); } +void mpi_finalize_wrapper() { MPI_Finalize(); } inline RuntimeViewPIMPL::RuntimeViewPIMPL(bool did_i_start_mpi, comm_type comm, logger_type logger) : @@ -40,19 +41,20 @@ inline RuntimeViewPIMPL::RuntimeViewPIMPL(bool did_i_start_mpi, comm_type comm, // Pre-populate the current rank's resource set. instantiate_resource_set_(m_comm.me()); - /// Register the finalize callbacks - stack_callback(std::function{&mpi_finalize_wrapper}, m_callbacks_final); + /// Register the finalize callbacks + stack_callback(std::function{&mpi_finalize_wrapper}, + m_callbacks_final); } inline RuntimeViewPIMPL::~RuntimeViewPIMPL() noexcept { if(!m_did_i_start_mpi) return; - //MPI_Finalize(); + // MPI_Finalize(); /// call the initialize callback functions while(!m_callbacks_final.empty()) { - m_callbacks_final.top()(); - m_callbacks_final.pop(); - } + m_callbacks_final.top()(); + m_callbacks_final.pop(); + } } inline RuntimeViewPIMPL::const_resource_set_reference RuntimeViewPIMPL::at( From 57cebfd5cb416368b11cd19290a4e18cc2c8fbee Mon Sep 17 00:00:00 2001 From: yzhang Date: Thu, 18 Jan 2024 13:22:51 -0600 Subject: [PATCH 05/11] Fix the signature of stack_callback() --- src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp | 2 +- src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp index 9af65af4..85be09b6 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp @@ -133,7 +133,7 @@ struct RuntimeViewPIMPL { std::stack> m_callbacks_final; /// Register callback functions to stacks - void stack_callback(std::function cb_func, std::stack> m_stack); + void stack_callback(std::function cb_func, std::stack>& m_stack); private: /** @brief Wraps the process of instantiating a ResourceSet. diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp index 9a22123d..f36cfc83 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp @@ -25,11 +25,13 @@ namespace parallelzone::runtime::detail_ { -inline void RuntimeViewPIMPL::stack_callback(std::function cb_func, std::stack> m_stack) { +inline void RuntimeViewPIMPL::stack_callback(std::function cb_func, std::stack>& m_stack) { m_stack.push(cb_func); } -void mpi_finalize_wrapper() { MPI_Finalize(); } +inline void mpi_finalize_wrapper() { + MPI_Finalize(); +} inline RuntimeViewPIMPL::RuntimeViewPIMPL(bool did_i_start_mpi, comm_type comm, logger_type logger) : @@ -46,8 +48,6 @@ inline RuntimeViewPIMPL::RuntimeViewPIMPL(bool did_i_start_mpi, comm_type comm, inline RuntimeViewPIMPL::~RuntimeViewPIMPL() noexcept { if(!m_did_i_start_mpi) return; - //MPI_Finalize(); - /// call the initialize callback functions while(!m_callbacks_final.empty()) { m_callbacks_final.top()(); From b5855320d1b79103bfb9618386ad98dfcb0d7f3a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 18 Jan 2024 19:27:08 +0000 Subject: [PATCH 06/11] Committing clang-format changes --- src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp | 3 ++- src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp | 7 +++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp index 1f134958..18dbb25f 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp @@ -133,7 +133,8 @@ struct RuntimeViewPIMPL { std::stack> m_callbacks_final; /// Register callback functions to stacks - void stack_callback(std::function cb_func, std::stack>& m_stack); + void stack_callback(std::function cb_func, + std::stack>& m_stack); private: /** @brief Wraps the process of instantiating a ResourceSet. diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp index b91032d4..321a64bb 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp @@ -25,13 +25,12 @@ namespace parallelzone::runtime::detail_ { -inline void RuntimeViewPIMPL::stack_callback(std::function cb_func, std::stack>& m_stack) { +inline void RuntimeViewPIMPL::stack_callback( + std::function cb_func, std::stack>& m_stack) { m_stack.push(cb_func); } -inline void mpi_finalize_wrapper() { - MPI_Finalize(); -} +inline void mpi_finalize_wrapper() { MPI_Finalize(); } inline RuntimeViewPIMPL::RuntimeViewPIMPL(bool did_i_start_mpi, comm_type comm, logger_type logger) : From df514862bf58ce7454d3fb8200571d0e47b22b9a Mon Sep 17 00:00:00 2001 From: "Jonathan M. Waldrop" Date: Thu, 18 Jan 2024 15:12:07 -0600 Subject: [PATCH 07/11] stack_callback work with Yu --- include/parallelzone/runtime/runtime_view.hpp | 14 ++++++++++++ .../runtime/detail_/runtime_view_pimpl.hpp | 22 ++++++++++++------- .../runtime/detail_/runtime_view_pimpl.ipp | 11 +++++----- src/parallelzone/runtime/runtime_view.cpp | 4 ++++ src/python/runtime/runtime_view.cpp | 2 ++ .../runtime/detail_/runtime_view_pimpl.cpp | 15 +++++++++++++ .../parallelzone/runtime/runtime_view.cpp | 15 +++++++++++++ .../unit_tests/runtime/test_runtime_view.py | 12 ++++++++++ 8 files changed, 81 insertions(+), 14 deletions(-) diff --git a/include/parallelzone/runtime/runtime_view.hpp b/include/parallelzone/runtime/runtime_view.hpp index 5b8d255e..87aac29d 100644 --- a/include/parallelzone/runtime/runtime_view.hpp +++ b/include/parallelzone/runtime/runtime_view.hpp @@ -97,6 +97,9 @@ class RuntimeView { /// Type of a pointer to the PIMPL using pimpl_pointer = std::shared_ptr; + /// Type of a callback function + using callback_function_type = std::function; + // ------------------------------------------------------------------------- // -- Ctors, Assignment, Dtor // ------------------------------------------------------------------------- @@ -490,6 +493,17 @@ class RuntimeView { // -- Utility methods // ------------------------------------------------------------------------- + /** @brief Adds callback function to call when destructed. + * + * Adds functions to a stack of callback functions that will be called + * upone the destruction of *this. + * + * @param[in] cb_func The callback function to add to the stack + * + * @throw None No throw guarantee. + */ + void stack_callback(callback_function_type cb_func); + /** @brief Swaps the state of *this with @p other. * * This method simply swaps the pointers of the PIMPLs. As such all diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp index 18dbb25f..667bec71 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp @@ -64,6 +64,9 @@ struct RuntimeViewPIMPL { /// Ultimately a typedef of RuntimeView::argv_type using argv_type = parent_type::argv_type; + /// Type of a callback function + using callback_function_type = parent_type::callback_function_type; + /** @brief Initializes *this from the provided MPI communicator. * * Constructor for the RuntimeViewPIMPL class. @@ -119,6 +122,14 @@ struct RuntimeViewPIMPL { */ bool operator==(const RuntimeViewPIMPL& rhs) const noexcept; + /** @brief Adds callback function to call when destructed. + * + * @param[in] cb_func The callback function to add to the stack + * + * @throw None No throw guarantee. + */ + void stack_callback(callback_function_type cb_func); + /// Did this PIMPL start MPI? bool m_did_i_start_mpi; @@ -128,14 +139,6 @@ struct RuntimeViewPIMPL { /// Pointer to the logger (pointer to allow logging with const ResourceSets) logger_pointer m_plogger; - /// Stacks of initialize and finalize callback functions - // std::stack> m_callbacks_init; - std::stack> m_callbacks_final; - - /// Register callback functions to stacks - void stack_callback(std::function cb_func, - std::stack>& m_stack); - private: /** @brief Wraps the process of instantiating a ResourceSet. * @@ -168,6 +171,9 @@ struct RuntimeViewPIMPL { * ResourceSet in a const function we can do that. */ mutable resource_set_container m_resource_sets_; + + /// Stacks of initialize and finalize callback functions + std::stack> m_callbacks_final; }; } // namespace parallelzone::runtime::detail_ diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp index 321a64bb..b3dd6a0c 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp @@ -25,9 +25,8 @@ namespace parallelzone::runtime::detail_ { -inline void RuntimeViewPIMPL::stack_callback( - std::function cb_func, std::stack>& m_stack) { - m_stack.push(cb_func); +inline void RuntimeViewPIMPL::stack_callback(callback_function_type cb_func) { + m_callbacks_final.push(cb_func); } inline void mpi_finalize_wrapper() { MPI_Finalize(); } @@ -42,12 +41,12 @@ inline RuntimeViewPIMPL::RuntimeViewPIMPL(bool did_i_start_mpi, comm_type comm, instantiate_resource_set_(m_comm.me()); /// Register the finalize callbacks - stack_callback(std::function{&mpi_finalize_wrapper}, - m_callbacks_final); + if(m_did_i_start_mpi) { + stack_callback(std::function{&mpi_finalize_wrapper}); + } } inline RuntimeViewPIMPL::~RuntimeViewPIMPL() noexcept { - if(!m_did_i_start_mpi) return; /// call the initialize callback functions while(!m_callbacks_final.empty()) { m_callbacks_final.top()(); diff --git a/src/parallelzone/runtime/runtime_view.cpp b/src/parallelzone/runtime/runtime_view.cpp index ac9558a9..d88218f7 100644 --- a/src/parallelzone/runtime/runtime_view.cpp +++ b/src/parallelzone/runtime/runtime_view.cpp @@ -120,6 +120,10 @@ RuntimeView::logger_reference RuntimeView::logger() const { // -- Utility methods // ----------------------------------------------------------------------------- +void RuntimeView::stack_callback(callback_function_type cb_func) { + pimpl_().stack_callback(cb_func); +} + void RuntimeView::swap(RuntimeView& other) noexcept { m_pimpl_.swap(other.m_pimpl_); } diff --git a/src/python/runtime/runtime_view.cpp b/src/python/runtime/runtime_view.cpp index 6a78a33e..ee0a4325 100644 --- a/src/python/runtime/runtime_view.cpp +++ b/src/python/runtime/runtime_view.cpp @@ -16,6 +16,7 @@ #include "runtime.hpp" #include +#include #include namespace parallelzone::runtime { @@ -31,6 +32,7 @@ void export_runtime_view(pybind11::module_& m) { .def("my_resource_set", &RuntimeView::my_resource_set) .def("count", &RuntimeView::count) .def("logger", &RuntimeView::logger) + .def("stack_callback", &RuntimeView::stack_callback) .def(pybind11::self == pybind11::self) .def(pybind11::self != pybind11::self); } diff --git a/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp b/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp index 6c6fd4a5..290abc80 100644 --- a/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp +++ b/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp @@ -73,4 +73,19 @@ TEST_CASE("RuntimeViewPIMPL") { REQUIRE_FALSE(pimpl == other); } } + + SECTION("stack_callback") { + // Simulate initialization + bool is_running = true; + + // Simulate finalize callback + auto turn_off = [&is_running](){ is_running = false; }; + + // RuntimeViewPIMPL will fall off, call the turn_off lambda + { + RuntimeViewPIMPL falls_off(false, comm, log); + falls_off.stack_callback(turn_off); + } + REQUIRE(is_running == false); + } } diff --git a/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp b/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp index ba296eed..c7678bf1 100644 --- a/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp +++ b/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp @@ -208,6 +208,21 @@ TEST_CASE("RuntimeView") { } } + SECTION("stack_callback") { + // Simulate initialization + bool is_running = true; + + // Simulate finalize callback + auto turn_off = [&is_running](){ is_running = false; }; + + // RuntimeView will fall off, call the turn_off lambda + { + RuntimeView falls_off; + falls_off.stack_callback(turn_off); + } + REQUIRE(is_running == false); + } + SECTION("gather") { using data_type = std::vector; data_type local_data(3, "Hello"); diff --git a/tests/python/unit_tests/runtime/test_runtime_view.py b/tests/python/unit_tests/runtime/test_runtime_view.py index 9c11bcc4..14c340db 100644 --- a/tests/python/unit_tests/runtime/test_runtime_view.py +++ b/tests/python/unit_tests/runtime/test_runtime_view.py @@ -79,6 +79,18 @@ def test_logger(self): self.assertIsNotNone(self.defaulted.logger()) self.defaulted.logger().log("Hello").log("world") + def test_stack_callback(self): + is_running = [True] + + def turn_off(val=is_running): + val[0] = False + + falls_off = pz.runtime.RuntimeView() + falls_off.stack_callback(turn_off) + del falls_off + + self.assertFalse(is_running[0]) + def test_comparisons(self): self.assertEqual(self.defaulted, pz.runtime.RuntimeView()) self.assertFalse(self.defaulted != pz.runtime.RuntimeView()) From d7de6bebb30f177bf0ddd6c3c826beca64474e9a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 18 Jan 2024 21:13:19 +0000 Subject: [PATCH 08/11] Committing clang-format changes --- include/parallelzone/runtime/runtime_view.hpp | 2 +- .../parallelzone/runtime/detail_/runtime_view_pimpl.cpp | 2 +- tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/parallelzone/runtime/runtime_view.hpp b/include/parallelzone/runtime/runtime_view.hpp index 87aac29d..19f651f9 100644 --- a/include/parallelzone/runtime/runtime_view.hpp +++ b/include/parallelzone/runtime/runtime_view.hpp @@ -494,7 +494,7 @@ class RuntimeView { // ------------------------------------------------------------------------- /** @brief Adds callback function to call when destructed. - * + * * Adds functions to a stack of callback functions that will be called * upone the destruction of *this. * diff --git a/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp b/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp index 290abc80..55be0d96 100644 --- a/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp +++ b/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp @@ -79,7 +79,7 @@ TEST_CASE("RuntimeViewPIMPL") { bool is_running = true; // Simulate finalize callback - auto turn_off = [&is_running](){ is_running = false; }; + auto turn_off = [&is_running]() { is_running = false; }; // RuntimeViewPIMPL will fall off, call the turn_off lambda { diff --git a/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp b/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp index c7678bf1..3e3c5a33 100644 --- a/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp +++ b/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp @@ -213,7 +213,7 @@ TEST_CASE("RuntimeView") { bool is_running = true; // Simulate finalize callback - auto turn_off = [&is_running](){ is_running = false; }; + auto turn_off = [&is_running]() { is_running = false; }; // RuntimeView will fall off, call the turn_off lambda { From 2e66ecfd3c8625d8a6d1f871bf2e206d0cb67c09 Mon Sep 17 00:00:00 2001 From: yzhang Date: Fri, 19 Jan 2024 16:25:50 -0600 Subject: [PATCH 09/11] Add stack tests --- include/parallelzone/runtime/runtime_view.hpp | 8 ++++--- .../runtime/detail_/runtime_view_pimpl.hpp | 5 +++-- .../runtime/detail_/runtime_view_pimpl.ipp | 4 ++-- src/parallelzone/runtime/runtime_view.cpp | 2 +- .../runtime/detail_/runtime_view_pimpl.cpp | 21 ++++++++++++++++++- .../parallelzone/runtime/runtime_view.cpp | 21 ++++++++++++++++++- .../unit_tests/runtime/test_runtime_view.py | 18 +++++++++++++++- 7 files changed, 68 insertions(+), 11 deletions(-) diff --git a/include/parallelzone/runtime/runtime_view.hpp b/include/parallelzone/runtime/runtime_view.hpp index 19f651f9..39a17d52 100644 --- a/include/parallelzone/runtime/runtime_view.hpp +++ b/include/parallelzone/runtime/runtime_view.hpp @@ -496,11 +496,13 @@ class RuntimeView { /** @brief Adds callback function to call when destructed. * * Adds functions to a stack of callback functions that will be called - * upone the destruction of *this. + * upone the destruction of *this. N.b., the functions are called LIFO. * - * @param[in] cb_func The callback function to add to the stack + * @param[in] cb_func The callback function with the signature 'void()' + * to add to the stack. * - * @throw None No throw guarantee. + * @throw std::bad_alloc if there is problem adding the function to the + * stack. Strong throw guarantee. */ void stack_callback(callback_function_type cb_func); diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp index 667bec71..2fbcf67d 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp @@ -126,7 +126,8 @@ struct RuntimeViewPIMPL { * * @param[in] cb_func The callback function to add to the stack * - * @throw None No throw guarantee. + * @throw std::bad_alloc if there is problem adding the function to the + * stack. Strong throw guarantee. */ void stack_callback(callback_function_type cb_func); @@ -173,7 +174,7 @@ struct RuntimeViewPIMPL { mutable resource_set_container m_resource_sets_; /// Stacks of initialize and finalize callback functions - std::stack> m_callbacks_final; + std::stack m_callbacks_final; }; } // namespace parallelzone::runtime::detail_ diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp index b3dd6a0c..31f8cd8f 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp @@ -26,7 +26,7 @@ namespace parallelzone::runtime::detail_ { inline void RuntimeViewPIMPL::stack_callback(callback_function_type cb_func) { - m_callbacks_final.push(cb_func); + m_callbacks_final.push(std::move(cb_func)); } inline void mpi_finalize_wrapper() { MPI_Finalize(); } @@ -42,7 +42,7 @@ inline RuntimeViewPIMPL::RuntimeViewPIMPL(bool did_i_start_mpi, comm_type comm, /// Register the finalize callbacks if(m_did_i_start_mpi) { - stack_callback(std::function{&mpi_finalize_wrapper}); + stack_callback(callback_function_type{&mpi_finalize_wrapper}); } } diff --git a/src/parallelzone/runtime/runtime_view.cpp b/src/parallelzone/runtime/runtime_view.cpp index d88218f7..dbd31583 100644 --- a/src/parallelzone/runtime/runtime_view.cpp +++ b/src/parallelzone/runtime/runtime_view.cpp @@ -121,7 +121,7 @@ RuntimeView::logger_reference RuntimeView::logger() const { // ----------------------------------------------------------------------------- void RuntimeView::stack_callback(callback_function_type cb_func) { - pimpl_().stack_callback(cb_func); + pimpl_().stack_callback(std::move(cb_func)); } void RuntimeView::swap(RuntimeView& other) noexcept { diff --git a/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp b/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp index 55be0d96..81e993fa 100644 --- a/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp +++ b/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp @@ -74,7 +74,7 @@ TEST_CASE("RuntimeViewPIMPL") { } } - SECTION("stack_callback") { + SECTION("stack_callback I") { // Simulate initialization bool is_running = true; @@ -88,4 +88,23 @@ TEST_CASE("RuntimeViewPIMPL") { } REQUIRE(is_running == false); } + + SECTION("stack_callback II") { + // Testing the stack + int func_no = 1; + + // Two lambdas to be pushed into the stack + auto call_back_1 = [&func_no]() { func_no += 1; }; + auto call_back_2 = [&func_no]() { func_no *= 2; }; + + // RuntimeViewPIMPL will fall off, call the turn_off lambda + { + RuntimeViewPIMPL rt_pimpl(false, comm, log); + rt_pimpl.stack_callback(call_back_1); + rt_pimpl.stack_callback(call_back_2); + } + + REQUIRE(func_no == 3); + + } } diff --git a/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp b/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp index 3e3c5a33..037c4bb6 100644 --- a/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp +++ b/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp @@ -208,7 +208,7 @@ TEST_CASE("RuntimeView") { } } - SECTION("stack_callback") { + SECTION("stack_callback I") { // Simulate initialization bool is_running = true; @@ -223,6 +223,25 @@ TEST_CASE("RuntimeView") { REQUIRE(is_running == false); } + SECTION("stack_callback II") { + // Testing the stack + int func_no = 1; + + // Two lambdas to be pushed into the stack + auto call_back_1 = [&func_no]() { func_no += 1; }; + auto call_back_2 = [&func_no]() { func_no *= 2; }; + + // RuntimeView will fall off, call the turn_off lambda + { + RuntimeView rt; + rt.stack_callback(call_back_1); + rt.stack_callback(call_back_2); + } + + REQUIRE(func_no == 3); + + } + SECTION("gather") { using data_type = std::vector; data_type local_data(3, "Hello"); diff --git a/tests/python/unit_tests/runtime/test_runtime_view.py b/tests/python/unit_tests/runtime/test_runtime_view.py index 14c340db..6c72a569 100644 --- a/tests/python/unit_tests/runtime/test_runtime_view.py +++ b/tests/python/unit_tests/runtime/test_runtime_view.py @@ -79,7 +79,7 @@ def test_logger(self): self.assertIsNotNone(self.defaulted.logger()) self.defaulted.logger().log("Hello").log("world") - def test_stack_callback(self): + def test_stack_callback_1(self): is_running = [True] def turn_off(val=is_running): @@ -91,6 +91,22 @@ def turn_off(val=is_running): self.assertFalse(is_running[0]) + def test_stack_callback_2(self): + func_no = [1] + + def call_back_1(val=func_no): + val[0] = val[0] + 1 + + def call_back_2(val=func_no): + val[0] = val[0] * 2 + + rt = pz.runtime.RuntimeView() + rt.stack_callback(call_back_1) + rt.stack_callback(call_back_2) + del rt + + self.assertEqual(func_no[0], 3) + def test_comparisons(self): self.assertEqual(self.defaulted, pz.runtime.RuntimeView()) self.assertFalse(self.defaulted != pz.runtime.RuntimeView()) From 04b6a2dfeb468c3d85ca95a0f426de2825cda1b2 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 19 Jan 2024 22:26:53 +0000 Subject: [PATCH 10/11] Committing clang-format changes --- include/parallelzone/runtime/runtime_view.hpp | 2 +- .../runtime/detail_/runtime_view_pimpl.cpp | 17 ++++++++--------- .../parallelzone/runtime/runtime_view.cpp | 1 - 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/include/parallelzone/runtime/runtime_view.hpp b/include/parallelzone/runtime/runtime_view.hpp index 39a17d52..14164b54 100644 --- a/include/parallelzone/runtime/runtime_view.hpp +++ b/include/parallelzone/runtime/runtime_view.hpp @@ -502,7 +502,7 @@ class RuntimeView { * to add to the stack. * * @throw std::bad_alloc if there is problem adding the function to the - * stack. Strong throw guarantee. + * stack. Strong throw guarantee. */ void stack_callback(callback_function_type cb_func); diff --git a/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp b/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp index 81e993fa..4375d9c1 100644 --- a/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp +++ b/tests/cxx/unit_tests/parallelzone/runtime/detail_/runtime_view_pimpl.cpp @@ -88,23 +88,22 @@ TEST_CASE("RuntimeViewPIMPL") { } REQUIRE(is_running == false); } - + SECTION("stack_callback II") { - // Testing the stack - int func_no = 1; + // Testing the stack + int func_no = 1; - // Two lambdas to be pushed into the stack - auto call_back_1 = [&func_no]() { func_no += 1; }; - auto call_back_2 = [&func_no]() { func_no *= 2; }; + // Two lambdas to be pushed into the stack + auto call_back_1 = [&func_no]() { func_no += 1; }; + auto call_back_2 = [&func_no]() { func_no *= 2; }; - // RuntimeViewPIMPL will fall off, call the turn_off lambda + // RuntimeViewPIMPL will fall off, call the turn_off lambda { RuntimeViewPIMPL rt_pimpl(false, comm, log); rt_pimpl.stack_callback(call_back_1); - rt_pimpl.stack_callback(call_back_2); + rt_pimpl.stack_callback(call_back_2); } REQUIRE(func_no == 3); - } } diff --git a/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp b/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp index 037c4bb6..53b6304c 100644 --- a/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp +++ b/tests/cxx/unit_tests/parallelzone/runtime/runtime_view.cpp @@ -239,7 +239,6 @@ TEST_CASE("RuntimeView") { } REQUIRE(func_no == 3); - } SECTION("gather") { From 773d7c3974e3910882195e1df7f8bbaa624a5d83 Mon Sep 17 00:00:00 2001 From: yzhang Date: Mon, 22 Jan 2024 10:22:38 -0600 Subject: [PATCH 11/11] Change m_callbacks_final to m_callbacks_final_ --- src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp | 2 +- src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp index 2fbcf67d..1de271cc 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.hpp @@ -174,7 +174,7 @@ struct RuntimeViewPIMPL { mutable resource_set_container m_resource_sets_; /// Stacks of initialize and finalize callback functions - std::stack m_callbacks_final; + std::stack m_callbacks_final_; }; } // namespace parallelzone::runtime::detail_ diff --git a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp index 31f8cd8f..9d8ec7f7 100644 --- a/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp +++ b/src/parallelzone/runtime/detail_/runtime_view_pimpl.ipp @@ -26,7 +26,7 @@ namespace parallelzone::runtime::detail_ { inline void RuntimeViewPIMPL::stack_callback(callback_function_type cb_func) { - m_callbacks_final.push(std::move(cb_func)); + m_callbacks_final_.push(std::move(cb_func)); } inline void mpi_finalize_wrapper() { MPI_Finalize(); } @@ -48,9 +48,9 @@ inline RuntimeViewPIMPL::RuntimeViewPIMPL(bool did_i_start_mpi, comm_type comm, inline RuntimeViewPIMPL::~RuntimeViewPIMPL() noexcept { /// call the initialize callback functions - while(!m_callbacks_final.empty()) { - m_callbacks_final.top()(); - m_callbacks_final.pop(); + while(!m_callbacks_final_.empty()) { + m_callbacks_final_.top()(); + m_callbacks_final_.pop(); } }