diff --git a/src/shamrock/include/shamrock/solvergraph/HypergraphLog.hpp b/src/shamrock/include/shamrock/solvergraph/HypergraphLog.hpp new file mode 100644 index 0000000000..6d48971d6d --- /dev/null +++ b/src/shamrock/include/shamrock/solvergraph/HypergraphLog.hpp @@ -0,0 +1,31 @@ +// -------------------------------------------------------// +// +// SHAMROCK code for hydrodynamics +// Copyright (c) 2021-2025 Timothée David--Cléris +// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1 +// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information +// +// -------------------------------------------------------// + +#pragma once + +/** + * @file HypergraphLog.hpp + * @author Timothée David--Cléris (tim.shamrock@proton.me) + * @brief + * + */ + +#include "shambase/aliases_int.hpp" +#include + +namespace shamrock::solvergraph { + + class INode; // forward declaration + + void log_new_inode(u64 uuid); + void log_del_inode(u64 uuid); + + void notify_inode_ptr(u64 uuid, std::shared_ptr &ptr); + +} // namespace shamrock::solvergraph diff --git a/src/shamrock/include/shamrock/solvergraph/INode.hpp b/src/shamrock/include/shamrock/solvergraph/INode.hpp index ba880c7afc..8abf112c35 100644 --- a/src/shamrock/include/shamrock/solvergraph/INode.hpp +++ b/src/shamrock/include/shamrock/solvergraph/INode.hpp @@ -18,14 +18,48 @@ #include "shambase/WithUUID.hpp" #include "shambase/memory.hpp" +#include "shamrock/solvergraph/HypergraphLog.hpp" #include "shamrock/solvergraph/IEdge.hpp" #include #include namespace shamrock::solvergraph { + inline bool log_enabled = true; + + /// class to check if the object was moved to somewhere else to avoid double deletion + class MoveAware { + public: + bool is_valid() const noexcept { + return sentinel_; // false was moved somewhere else + } + + protected: + MoveAware() : sentinel_(true) {} // intact by default + + MoveAware(const MoveAware &) = default; + MoveAware &operator=(const MoveAware &) = default; + + /// Move constructor - marks the source as moved-from + MoveAware(MoveAware &&other) noexcept : sentinel_(std::exchange(other.sentinel_, false)) {} + + /// Move assignment - marks the source as moved-from + MoveAware &operator=(MoveAware &&other) noexcept { + if (this != &other) { + sentinel_ = std::exchange(other.sentinel_, false); + } + return *this; + } + + virtual ~MoveAware() = default; + + private: + bool sentinel_; // true = intact, false = moved-from + }; + /// Inode is node between data edges, takes multiple inputs, multiple outputs class INode : public std::enable_shared_from_this, + public MoveAware, public shambase::WithUUID { /// Read only edges @@ -34,15 +68,20 @@ namespace shamrock::solvergraph { std::vector> rw_edges; public: - INode() = default; - - INode(const INode &) = delete; /// would violate shared_from_this() & unique UUID - INode &operator=(const INode &) = delete; /// would violate shared_from_this() & unique UUID + inline INode() { + if (log_enabled) { + // Can't use shared_from_this() in constructor - object not in shared_ptr yet + shamrock::solvergraph::log_new_inode(get_uuid()); + } + } /// Move constructor - automatically delegates to base classes and members + /// MoveAware's move constructor will be called automatically, handling sentinel + /// invalidation INode(INode &&) noexcept = default; /// Move assignment - automatically delegates to base classes and members + /// MoveAware's move assignment will be called automatically, handling sentinel invalidation INode &operator=(INode &&) noexcept = default; /// Get a shared pointer to this node @@ -70,6 +109,10 @@ namespace shamrock::solvergraph { /// Destructor (virtual) & reset the edges virtual ~INode() { + if (log_enabled && is_valid()) { + // Use UUID directly - shared_from_this() may not be safe in destructor + shamrock::solvergraph::log_del_inode(get_uuid()); + } __internal_set_ro_edges({}); __internal_set_rw_edges({}); } diff --git a/src/shamrock/src/solvergraph/HypergraphLog.cpp b/src/shamrock/src/solvergraph/HypergraphLog.cpp new file mode 100644 index 0000000000..3da35e2fe1 --- /dev/null +++ b/src/shamrock/src/solvergraph/HypergraphLog.cpp @@ -0,0 +1,45 @@ +// -------------------------------------------------------// +// +// SHAMROCK code for hydrodynamics +// Copyright (c) 2021-2025 Timothée David--Cléris +// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1 +// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information +// +// -------------------------------------------------------// + +/** + * @file HypergraphLog.cpp + * @author Timothée David--Cléris (tim.shamrock@proton.me) + * @brief + * + */ + +#include "shamrock/solvergraph/HypergraphLog.hpp" +#include "shamcomm/logs.hpp" +#include "shamrock/solvergraph/INode.hpp" +#include +#include + +namespace shamrock::solvergraph { + + std::unordered_map> inode_labels; + + void log_new_inode(u64 uuid) { + shamcomm::logs::raw_ln("New inode created: ", uuid); + inode_labels[uuid] = {}; + } + + void log_del_inode(u64 uuid) { + shamcomm::logs::raw_ln("Inode deleted: ", uuid); + inode_labels.erase(uuid); + } + + void notify_inode_ptr(u64 uuid, std::shared_ptr &ptr) { + if (!bool(inode_labels.at(uuid))) { + if (inode_labels.at(uuid).get() != ptr.get()) { + inode_labels[uuid] = ptr; + } + } + } + +} // namespace shamrock::solvergraph