Backends and dispatching

NetworkX · Expert · 12 min

Backends let existing NetworkX code run on GPUs or many cores, often 10 to 100 times faster, with a configuration change.

Since NetworkX 3.2 many functions are dispatchable: when called, NetworkX can hand the work to an installed backend package that implements the same function signature with faster code.

Notable backends:

  • nx-cugraph (NVIDIA RAPIDS): GPU implementations of PageRank, betweenness, Louvain, Leiden, shortest paths and more.
  • nx-parallel: CPU multiprocessing with joblib for algorithms such as betweenness and all-pairs computations.
  • graphblas-algorithms: sparse linear-algebra implementations using GraphBLAS.

Ways to use one:

  • Per call: nx.betweenness_centrality(G, backend="parallel").
  • Automatically: set nx.config.backend_priority.algos = ["cugraph"] or the environment variable NX_CUGRAPH_AUTOCONFIG=True, and existing code dispatches to the backend where supported, falling back to NetworkX otherwise.
  • With backend graph objects: functions called on a backend's own graph type dispatch to that backend automatically.

Converting a NetworkX graph to a backend's format costs time, so NetworkX can cache converted graphs on G.__networkx_cache__ (nx.config.cache_converted_graphs). Mutating the graph through the normal API clears the cache.

nx.config.backends lists installed backends, and the docs mark which functions each backend supports. Logging (networkx logger at DEBUG) shows which implementation actually ran.

Example

import networkx as nx

print(sorted(nx.config.backends))       # e.g. ['cugraph', 'parallel'] when installed; [] otherwise
print(nx.config.cache_converted_graphs)  # True

G = nx.karate_club_graph()
# Explicitly ask for the built-in implementation ("networkx" is always available)
pr = nx.pagerank(G, backend="networkx")
print(max(pr, key=pr.get))               # 33

# With nx-parallel installed, the same call could run on all cores:
#   nx.betweenness_centrality(G, backend="parallel")
# With nx-cugraph installed, route supported calls to the GPU automatically:
#   nx.config.backend_priority.algos = ["cugraph"]
try:
    nx.pagerank(G, backend="no-such-backend")
except ImportError as e:
    print(type(e).__name__)              # ImportError (unknown backend)

Remember

  • Dispatchable functions can run on installed backends unchanged
  • Choose per call with backend= or globally via nx.config.backend_priority
  • Converted graphs are cached; check logs for which backend ran

Watch out for

  • Assuming every function is accelerated: unsupported ones silently fall back to pure Python.

Check yourself

  1. You set nx.config.backend_priority.algos = ['cugraph'] and call a function nx-cugraph doesn't implement. What happens?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: networkx.org/documentation/stable/reference/backends.html

Builds on

Unlocks

Nothing builds on this yet.