forked from networkx/nx-parallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshould_run_policies.py
More file actions
51 lines (37 loc) · 1.2 KB
/
should_run_policies.py
File metadata and controls
51 lines (37 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import nx_parallel as nxp
__all__ = [
"default_should_run",
"should_skip_parallel",
"should_run_if_large",
"should_run_if_nodes_none",
"should_run_if_sparse",
]
def should_skip_parallel(*_):
return "Fast algorithm; skip parallel execution"
def should_run_if_large(G, *_):
if len(G) <= 200:
return "Graph too small for parallel execution"
return True
def default_should_run(*_):
n_jobs = nxp.get_n_jobs()
print(f"{n_jobs=}")
if n_jobs in (None, 0, 1):
return "Parallel backend requires `n_jobs` > 1 to run"
return True
def should_run_if_nodes_none(G, nodes=None, *_):
if nodes is None:
return True
return "`nodes` should be None for parallel execution"
def should_run_if_sparse(G, *args, threshold=0.3, **kwargs):
if hasattr(G, "graph_object"):
G = G.graph_object
nodes = len(G)
# Handle trivial graphs separately to avoid division by zero
if nodes <= 1:
return "Empty graph" if nodes == 0 else "Single-node graph"
density = 2 * G.number_of_edges() / (nodes * (nodes - 1))
return (
True
if density <= threshold
else "Graph too dense to benefit from parallel execution"
)