Skip to content

Commit

Permalink
start to restructure graph_algorithm plugin (disfunctional)
Browse files Browse the repository at this point in the history
  • Loading branch information
SJulianS committed May 6, 2024
1 parent 683dca5 commit 422e7ef
Show file tree
Hide file tree
Showing 14 changed files with 499 additions and 687 deletions.
2 changes: 1 addition & 1 deletion plugins/graph_algorithm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
option(PL_GRAPH_ALGORITHM "PL_GRAPH_ALGORITHM" OFF)
option(PL_GRAPH_ALGORITHM "PL_GRAPH_ALGORITHM" ON)

if(PL_GRAPH_ALGORITHM OR BUILD_ALL_PLUGINS)
file(GLOB_RECURSE GRAPH_ALGORITHM_INC ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// MIT License
//
// Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
// Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
// Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
// Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "hal_core/defines.h"
#include "hal_core/utilities/result.h"

#include <igraph/igraph.h>
#include <set>

namespace hal
{
namespace graph_algorithm
{
class NetlistGraph;

Result<std::set<std::set<u32>>> get_connected_components(const NetlistGraph* graph, bool strong);
} // namespace graph_algorithm
} // namespace hal
81 changes: 81 additions & 0 deletions plugins/graph_algorithm/include/graph_algorithm/netlist_graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// MIT License
//
// Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
// Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
// Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
// Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "hal_core/defines.h"
#include "hal_core/utilities/result.h"

#include <functional>
#include <igraph/igraph.h>
#include <set>
#include <unordered_map>

namespace hal
{
class Netlist;
class Gate;
class Net;

namespace graph_algorithm
{
class NetlistGraph
{
public:
~NetlistGraph();

static Result<std::unique_ptr<NetlistGraph>> from_netlist(Netlist* nl, bool create_dummy_nodes = false, const std::function<bool(const Net*)>& filter = nullptr);

// TODO implement
static Result<std::unique_ptr<NetlistGraph>> from_netlist_no_edges(Netlist* nl);

Netlist* get_netlist() const;
igraph_t* get_graph() const;

Result<Gate*> get_gate_of_vertex(const u32 node) const;
Result<std::vector<Gate*>> get_gates_of_vertices(const std::set<u32>& nodes) const;
Result<std::vector<Gate*>> get_gates_of_vertices(const std::vector<u32>& nodes) const;

Result<u32> get_vertex_of_gate(Gate* g) const;

// TODO implement 4 below
void add_edges(const std::vector<std::pair<Gate*, Gate*>>& edges);
void add_edges(const std::vector<std::pair<u32, u32>>& edges);
void add_edges(const std::vector<Net*>& edges);
void delete_edges(const std::vector<std::pair<Gate*, Gate*>>& edges);
void delete_edges(const std::vector<std::pair<u32, u32>>& edges);
void delete_edges(const std::vector<Net*>& edges);

private:
NetlistGraph() = delete;
NetlistGraph(Netlist* nl);

Netlist* m_nl;
igraph_t* m_graph;
std::unordered_map<u32, Gate*> m_nodes_to_gates;
std::unordered_map<Gate*, u32> m_gates_to_nodes;
};
} // namespace graph_algorithm
} // namespace hal
Original file line number Diff line number Diff line change
Expand Up @@ -62,84 +62,5 @@ namespace hal
* @returns The version of the plugin.
*/
std::string get_version() const override;

/*
* clustering function
*/

/**
* Get a map of community IDs to communities. Each community is represented by a set of gates.
*
* @param[in] netlist - The netlist to operate on.
* @returns A map from community IDs to communities.
*/
std::map<int, std::set<Gate*>> get_communities(Netlist* const netlist);

/**
* Get a map of community IDs to communities running the spinglass clustering algorithm. Each community is represented by a set of gates.
*
* @param[in] netlist - The netlist to operate on.
* @param[in] spins - The number of spins.
* @returns A map from community IDs to communities.
*/
std::map<int, std::set<Gate*>> get_communities_spinglass(Netlist* const netlist, u32 const spins);

/**
* Get a map of community IDs to communities running the fast greedy clustering algorithm from igraph. Each community is represented by a set of gates.
*
* @param[in] netlist - The netlist to operate on.
* @returns A map from community IDs to communities.
*/
std::map<int, std::set<Gate*>> get_communities_fast_greedy(Netlist* const netlist);

/**
* Get a vector of strongly connected components (SCC) with each SSC being represented by a vector of gates.
*
* @param[in] netlist - The netlist to operate on.
* @returns A vector of SCCs.
*/
std::vector<std::vector<Gate*>> get_strongly_connected_components(Netlist* netlist);

/**
* Get a graph cut for a specific gate and depth. Further, a set of gates can be specified that limit the graph cut, i.e., flip-flops and memory cells.<br>
* The graph cut is returned as a vector of sets of gates with the vector's index representing the distance of each set to the starting point.
*
* @param[in] netlist - The netlist to operate on.
* @param[in] gate - The gate that is the starting point for the graph cut.
* @param[in] depth - The depth of the graph cut.
* @param[in] terminal_gate_type - A set of gates at which to terminate the graph cut.
* @returns The graph cut as a vector of sets of gates.
*/
std::vector<std::set<Gate*>>
get_graph_cut(Netlist* const netlist, Gate* gate, const u32 depth = std::numeric_limits<u32>::max(), const std::set<std::string> terminal_gate_type = std::set<std::string>());

/*
* igraph specific functions
*/

/**
* Generates an directed graph based on the current netlist. Each gate is transformed to a node while each
* net is transformed to an edge. The function returns the mapping from igraph node ids to HAL gates. Note
* that for each global input and output dummy nodes are generated in the igraph representation.
*
* @param[in] netlist - The netlist to operate on.
* @param[in] igraph - igraph object
* @returns map from igraph node id to HAL gate ID, to be able to match back any graph operations.
*/
std::map<int, Gate*> get_igraph_directed(Netlist* const netlist, igraph_t* igraph);

/**
* Uses the mapping provided by the the get_igraph_directed() function to generate sets of HAL gates
* that were generated by the clustering algorithms of igraph. The igraph membership vector contains
* the generated clusters from the igraph framework, which is used to generate the sets of gates in HAL.
* The sets are stored in a map with the regarding cluster ID from igraph, since these can contain information
* generated by the clustering algorithm.
*
* @param[in] graph - igraph graph object
* @param[in] membership - membership vector
* @param[in] vertex_to_gate - map from node ID in igraph to HAL gate
* @returns map from membership id to set of gates that have the membership.
*/
std::map<int, std::set<Gate*>> get_memberships_for_hal(igraph_t* graph, igraph_vector_int_t *membership, std::map<int, Gate*> vertex_to_gate);
};
} // namespace hal
109 changes: 55 additions & 54 deletions plugins/graph_algorithm/python/python_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,60 +60,61 @@ namespace hal
:returns: Plugin version.
:rtype: str
)")
.def("get_communities", &GraphAlgorithmPlugin::get_communities, py::arg("netlist"), R"(
Get a dict of community IDs to communities. Each community is represented by a set of gates.
:param hal_py.Netlist netlist: The netlist to operate on.
:returns: A dict from community IDs to communities.
:rtype: dict[int,set[hal_py.get_gate()]]
)")
.def("get_communities_spinglass", &GraphAlgorithmPlugin::get_communities_spinglass, py::arg("netlist"), py::arg("spins"), R"(
Get a dict of community IDs to communities running the spinglass clustering algorithm. Each community is represented by a set of gates.
:param hal_py.Netlist netlist: The netlist to operate on.
:param int spins: The number of spins.
:returns: A dict from community IDs to communities.
:rtype: dict[int,set[hal_py.get_gate()]]
)")
.def("get_communities_fast_greedy", &GraphAlgorithmPlugin::get_communities_fast_greedy, py::arg("netlist"), R"(
Get a dict of community IDs to communities running the fast greedy clustering algorithm from igraph. Each community is represented by a set of gates.
:param hal_py.Netlist netlist: The netlist to operate on.
:returns: A dict from community IDs to communities.
:rtype: dict[set[hal_py.get_gate()]]
)")
/*
.def("get_communities_multilevel", &GraphAlgorithmPlugin::get_communities_multilevel, py::arg("netlist"), R"(
Get a dict of community IDs to communities running the multilevel clustering algorithm from igraph. Each community is represented by a set of gates.
:param hal_py.Netlist netlist: The netlist to operate on.
:returns: A dict from community IDs to communities.
:rtype: dict[int,set[hal_py.get_gate()]]
)") */
.def("get_strongly_connected_components", &GraphAlgorithmPlugin::get_strongly_connected_components, py::arg("netlist"), R"(
Get a list of strongly connected components (SCC) with each SSC being represented by a list of gates.
:param hal_py.Netlist netlist: The netlist to operate on.
:returns: A list of SCCs.
:rtype: list[list[hal_py.get_gate()]]
)")
.def("get_graph_cut",
&GraphAlgorithmPlugin::get_graph_cut,
py::arg("netlist"),
py::arg("gate"),
py::arg("depth") = std::numeric_limits<u32>::max(),
py::arg("terminal_gate_type") = std::set<std::string>(),
R"(
Get a graph cut for a specific gate and depth. Further, a set of gates can be specified that limit the graph cut, i.e., flip-flops and memory cells.
The graph cut is returned as a list of sets of gates with the list's index representing the distance of each set to the starting point.
:param hal_py.Netlist netlist: The netlist to operate on.
:param hal_py.get_gate() gate: The gate that is the starting point for the graph cut.
:param int depth: The depth of the graph cut.
:param set[str] terminal_gate_type: A set of gates at which to terminate the graph cut.
:returns: The graph cut as a list of sets of gates.
:rtype: list[set[hal_py.get_gate()]]
)");
// .def("get_communities", &GraphAlgorithmPlugin::get_communities, py::arg("netlist"), R"(
// Get a dict of community IDs to communities. Each community is represented by a set of gates.

// :param hal_py.Netlist netlist: The netlist to operate on.
// :returns: A dict from community IDs to communities.
// :rtype: dict[int,set[hal_py.get_gate()]]
// )")
// .def("get_communities_spinglass", &GraphAlgorithmPlugin::get_communities_spinglass, py::arg("netlist"), py::arg("spins"), R"(
// Get a dict of community IDs to communities running the spinglass clustering algorithm. Each community is represented by a set of gates.

// :param hal_py.Netlist netlist: The netlist to operate on.
// :param int spins: The number of spins.
// :returns: A dict from community IDs to communities.
// :rtype: dict[int,set[hal_py.get_gate()]]
// )")
// .def("get_communities_fast_greedy", &GraphAlgorithmPlugin::get_communities_fast_greedy, py::arg("netlist"), R"(
// Get a dict of community IDs to communities running the fast greedy clustering algorithm from igraph. Each community is represented by a set of gates.

// :param hal_py.Netlist netlist: The netlist to operate on.
// :returns: A dict from community IDs to communities.
// :rtype: dict[set[hal_py.get_gate()]]
// )")
// /*
// .def("get_communities_multilevel", &GraphAlgorithmPlugin::get_communities_multilevel, py::arg("netlist"), R"(
// Get a dict of community IDs to communities running the multilevel clustering algorithm from igraph. Each community is represented by a set of gates.

// :param hal_py.Netlist netlist: The netlist to operate on.
// :returns: A dict from community IDs to communities.
// :rtype: dict[int,set[hal_py.get_gate()]]
// )") */
// .def("get_strongly_connected_components", &GraphAlgorithmPlugin::get_strongly_connected_components, py::arg("netlist"), R"(
// Get a list of strongly connected components (SCC) with each SSC being represented by a list of gates.

// :param hal_py.Netlist netlist: The netlist to operate on.
// :returns: A list of SCCs.
// :rtype: list[list[hal_py.get_gate()]]
// )")
// .def("get_graph_cut",
// &GraphAlgorithmPlugin::get_graph_cut,
// py::arg("netlist"),
// py::arg("gate"),
// py::arg("depth") = std::numeric_limits<u32>::max(),
// py::arg("terminal_gate_type") = std::set<std::string>(),
// R"(
// Get a graph cut for a specific gate and depth. Further, a set of gates can be specified that limit the graph cut, i.e., flip-flops and memory cells.
// The graph cut is returned as a list of sets of gates with the list's index representing the distance of each set to the starting point.

// :param hal_py.Netlist netlist: The netlist to operate on.
// :param hal_py.get_gate() gate: The gate that is the starting point for the graph cut.
// :param int depth: The depth of the graph cut.
// :param set[str] terminal_gate_type: A set of gates at which to terminate the graph cut.
// :returns: The graph cut as a list of sets of gates.
// :rtype: list[set[hal_py.get_gate()]]
// )")
;

#ifndef PYBIND11_MODULE
return m.ptr();
Expand Down
Loading

0 comments on commit 422e7ef

Please sign in to comment.