Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
expose pathfinder
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Nov 8, 2024
1 parent 420856e commit 7b6c1d9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
25 changes: 25 additions & 0 deletions src/faebryk/core/cpp/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ import enum
from collections.abc import Callable, Sequence, Set
from typing import overload

class Counter:
@property
def in_cnt(self) -> int: ...
@property
def weak_in_cnt(self) -> int: ...
@property
def out_weaker(self) -> int: ...
@property
def out_stronger(self) -> int: ...
@property
def out_cnt(self) -> int: ...
@property
def time_spent_s(self) -> float: ...
@property
def hide(self) -> bool: ...
@property
def name(self) -> str: ...
@property
def multi(self) -> bool: ...
@property
def total_counter(self) -> bool: ...

class Graph:
def __init__(self) -> None: ...
def get_edges(self, arg: GraphInterface, /) -> dict[GraphInterface, Link]: ...
Expand Down Expand Up @@ -149,5 +171,8 @@ def add(i: int, j: int = 1) -> int:
"""A function that adds two numbers"""

def call_python_function(func: Callable[[], int]) -> int: ...
def find_paths(
src: Node, dst: Sequence[Node]
) -> tuple[list[list[GraphInterface]], list[Counter]]: ...
def print_obj(obj: object) -> None: ...
def set_leak_warnings(value: bool) -> None: ...
4 changes: 2 additions & 2 deletions src/faebryk/core/cpp/include/pathfinder/pathfinder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include "graph/graph.hpp"
#include "pathfinder/bfs.hpp"
#include "pathfinder/pathcounter.hpp"
#include "perf.hpp"
#include <any>
Expand All @@ -27,7 +28,6 @@ struct Filter {
};

class PathFinder {
Graph &g;
std::vector<BFSPath> multi_paths;
size_t path_cnt = 0;

Expand All @@ -44,7 +44,7 @@ class PathFinder {
std::vector<BFSPath> _filter_paths_by_split_join(std::vector<BFSPath> &paths);

public:
PathFinder(Graph &g);
PathFinder();

std::vector<Filter> filters;
bool run_filters(BFSPath &p);
Expand Down
27 changes: 27 additions & 0 deletions src/faebryk/core/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "graph/graphinterfaces.hpp"
#include "graph/links.hpp"
#include "nano.hpp"
#include "pathfinder/pathfinder.hpp"
#include <nanobind/nanobind.h>

// check if c++20 is used
Expand Down Expand Up @@ -46,6 +47,17 @@ void print_obj_pyptr(PyObject *pyobj) {
print_obj(obj);
}

std::pair<std::vector<Path>, std::vector<Counter>>
find_paths(Node_ref src, std::vector<Node_ref> dst) {
PerfCounter pc;

PathFinder pf;
auto res = pf.find_paths(src, dst);

printf("TIME: %3.2lf ms C++ find paths\n", pc.ms());
return res;
}

PYMOD(m) {
m.doc() = "faebryk core c++ module";

Expand All @@ -54,6 +66,8 @@ PYMOD(m) {
m.def("set_leak_warnings", &nb::set_leak_warnings, "value"_a);
m.def("print_obj", &print_obj, "obj"_a);

m.def("find_paths", &find_paths, "src"_a, "dst"_a);

// Graph
using GI = GraphInterface;

Expand Down Expand Up @@ -158,4 +172,17 @@ PYMOD(m) {

nb::exception<Node::NodeException>(m, "NodeException");
nb::exception<Node::NodeNoParent>(m, "NodeNoParent");

// Pathfinder
nb::class_<Counter>(m, "Counter")
.def_ro("in_cnt", &Counter::in_cnt)
.def_ro("weak_in_cnt", &Counter::weak_in_cnt)
.def_ro("out_weaker", &Counter::out_weaker)
.def_ro("out_stronger", &Counter::out_stronger)
.def_ro("out_cnt", &Counter::out_cnt)
.def_ro("time_spent_s", &Counter::time_spent_s)
.def_ro("hide", &Counter::hide)
.def_ro("name", &Counter::name)
.def_ro("multi", &Counter::multi)
.def_ro("total_counter", &Counter::total_counter);
}
7 changes: 3 additions & 4 deletions src/faebryk/core/cpp/src/pathfinder/pathfinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
#include <unordered_set>

// PathFinder implementations
PathFinder::PathFinder(Graph &g)
: g(g) {
filters = {
PathFinder::PathFinder()
: filters{
Filter{
.filter = &PathFinder::_count,
.discovery = true,
Expand Down Expand Up @@ -85,7 +84,7 @@ PathFinder::PathFinder(Graph &g)
.name = "stack",
},
},
};
} {
}

// Filter implementations
Expand Down

0 comments on commit 7b6c1d9

Please sign in to comment.