Skip to content
Closed
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
31 changes: 31 additions & 0 deletions src/shamrock/include/shamrock/solvergraph/HypergraphLog.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// -------------------------------------------------------//
//
// SHAMROCK code for hydrodynamics
// Copyright (c) 2021-2025 Timothée David--Cléris <[email protected]>
// 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 ([email protected])
* @brief
*
*/

#include "shambase/aliases_int.hpp"
#include <memory>

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<INode> &ptr);

} // namespace shamrock::solvergraph
51 changes: 47 additions & 4 deletions src/shamrock/include/shamrock/solvergraph/INode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,48 @@

#include "shambase/WithUUID.hpp"
#include "shambase/memory.hpp"
#include "shamrock/solvergraph/HypergraphLog.hpp"
#include "shamrock/solvergraph/IEdge.hpp"
#include <memory>
#include <vector>

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<INode>,
public MoveAware,
public shambase::WithUUID<INode, u64> {

/// Read only edges
Expand All @@ -34,15 +68,20 @@ namespace shamrock::solvergraph {
std::vector<std::shared_ptr<IEdge>> 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
Expand Down Expand Up @@ -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({});
}
Expand Down
45 changes: 45 additions & 0 deletions src/shamrock/src/solvergraph/HypergraphLog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// -------------------------------------------------------//
//
// SHAMROCK code for hydrodynamics
// Copyright (c) 2021-2025 Timothée David--Cléris <[email protected]>
// 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 ([email protected])
* @brief
*
*/

#include "shamrock/solvergraph/HypergraphLog.hpp"
#include "shamcomm/logs.hpp"
#include "shamrock/solvergraph/INode.hpp"
#include <unordered_map>
#include <memory>

namespace shamrock::solvergraph {

std::unordered_map<u64, std::shared_ptr<INode>> 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<INode> &ptr) {
if (!bool(inode_labels.at(uuid))) {
if (inode_labels.at(uuid).get() != ptr.get()) {
inode_labels[uuid] = ptr;
}
}
}

} // namespace shamrock::solvergraph
Loading