Skip to content

Add Python graph algorithms with classic/SPLA comparison - #282

Open
vkutuev wants to merge 14 commits into
mainfrom
graph-algorithms
Open

vkutuev wants to merge 14 commits into
mainfrom
graph-algorithms

Conversation

@vkutuev

@vkutuev vkutuev commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Add Python graph algorithms with classic/SPLA comparison

Supersedes #243 (rebased onto main, cleaned up, and extended with the
maintainer fixes and Python infrastructure below).

The C++ bug fixes found along the way (LIL values counter,
set_fill_value data loss) were extracted to #285 and are already in
main.

Graph algorithms (from #243)

  • python/algorithms/: BFS, SSSP, PageRank and triangle counting on SPLA
    matrices, compared against classic pure-Python implementations on
    Matrix Market fixtures (push/pull variants where applicable).
  • MtxLoader: edge-weight support for Matrix Market input.
  • pyspla bindings: Descriptor creation/setters, Vector.fill_with,
    Vector.set_fill_value.
  • OpenCL: validate the vector format in cl_v_count_mf.

Maintainer fixes

  • Triangle counting rewritten (the original counted weighted triangles and
    failed its own test); main_spla fixed; lint cleanup.
  • fix(python): Scalar.__sub__ performed addition.

Python infrastructure

  • uv: PEP 621 pyproject.toml (setuptools, dynamic version from
    VERSION.md), dev tools as a PEP 735 group in uv.lock; setup.py /
    MANIFEST.in removed.
  • ruff: lint + format config (E/F/I/W, line-length 120) with the whole
    python/ tree brought to a clean baseline.
  • tests: new pytest suite under python/tests/ — pyspla API coverage
    (containers, sparse ops, format conversion, descriptors, error paths) and
    algorithm tests against classic references on small committed fixtures,
    with a NetworkX cross-check for triangle counting. Replaces the ad-hoc
    algorithms/test_compare.py.
  • CI: .github/workflows/python.yml — lint job (ruff check + format)
    and test job (CPU build of libspla, staged into the package, full pytest
    run).

Verification

  • C++ test suite: green on CPU and OpenCL builds.
  • Python: 51 tests green on both the CPU and OpenCL .so; ruff clean.

Expose the new spla_Vector_fill_with C API and wrap the existing
spla_Vector_set_fill_value in the Python Vector class. Also add
Python wrappers for the existing spla_Exec_v_eadd_fdb and
spla_Exec_v_count_mf C functions, which the graph algorithms rely on.
Add spla_Descriptor_make, spla_Descriptor_set_struct_only and
spla_Descriptor_set_early_exit to the C API (src/binding/c_descriptor.cpp)
and expose them in Python: Descriptor() now creates a real descriptor
instead of a null handle, plus set_struct_only()/set_early_exit().
MtxLoader now parses edge weights from Matrix Market files and
detects the "symmetric" attribute from the header. The default of
make_undirected is flipped to false: symmetrization now happens
automatically only for files declared symmetric in the header.
The sssp example reads edge weights and the tc example uses the
upper triangle.
The sp and dn paths of v_count_mf accessed the device vector without
calling validate_rw first, crashing when the vector was not already
in the required format. Add the missing validate_rw calls
(AccCoo/AccDense) to match the other kernels.
Add BFS, PageRank, SSSP and transitive closure as both classic
(pure-Python) and SPLA-backed implementations under
python/algorithms/, together with a comparison harness
(test_compare.py), a sample graph (graph.txt), a README with usage
instructions, and CPU/GPU result plots.
- tc_spla: default to the Sandia algorithm (L*L masked by L) — exact
  for any edge orientation and immune to edge weights; keep the
  mxmT-based variant (mirrors C++ spla::tc, requires upper-triangle
  unit-weight input) as tc_spla_mxmT
- test_compare: TC tests read with unit weights and cross-check
  against NetworkX (skipped when not installed); dataset paths point
  to python/algorithms/data/ (local fixtures, not committed)
- main_spla: fix IndentationError in the sssp vectors branch
- ruff: fix import sorting and unused imports, apply ruff format
Replace the legacy setup.py/MANIFEST.in with a PEP 621 pyproject.toml
(setuptools backend) and manage the dev environment with uv:

- version/readme now come from VERSION.md/README.md via
  [tool.setuptools.dynamic]
- dev tools (pytest, scipy, networkx, ruff) declared as a PEP 735
  dependency group, locked in uv.lock
- gitignore .venv/ and staged native libraries (build artifacts that
  get packaged into wheels)
- document the uv-based dev workflow in python/README.md
Add a [tool.ruff] configuration (E/F/I/W rules, line-length 120) and
bring the whole python/ tree to a clean baseline:

- fix import sorting (I001) and unused imports (F401)
- apply ruff format across the tree
- E741 ignored: I/J/V is standard COO notation for sparse matrices
- per-file ignores for the star-import re-export pattern in
  pyspla/__init__.py, the documented star import in example.py, and
  sys.path setup in test files
The subtraction operator was implemented with addition, so a - b
returned a + b.
Replace the ad-hoc algorithms/test_compare.py with a proper test
suite under python/tests/:

- test_pyspla.py exercises the installed package: scalar/vector/matrix
  containers, element-wise and sparse operations, format conversion,
  descriptors and wrapper-level error handling.
- test_algorithms.py checks the SPLA graph algorithms (BFS, SSSP,
  PageRank, triangle counting) against classic pure-Python references
  on small committed Matrix Market fixtures, with a NetworkX
  cross-check for triangle counting.

Wire pytest into pyproject.toml and point the algorithms README at
the new suite.
bridge.initialize() relied on the import system attaching the
pyspla.bridge submodule to the package namespace as a side effect of
the star import. After a failed first import (e.g. missing native
library), the submodule stays in sys.modules but is not re-attached
to the fresh package object, so a retry raised a misleading NameError
instead of the real 'no compiled spla file' error.
Two jobs on pushes/PRs touching the Python side (and the C++ sources
it builds against):

- lint: ruff check + ruff format --check via uv
- test: build libspla (CPU config), stage the shared library into the
  package and run the pytest suite
Document every algorithm module and function: the classic reference
implementations, the spla frontier-based versions (bfs, sssp), PageRank,
triangle counting (Sandia and masked mxmT variants), the graph loaders,
and the CLI driver. The tc_spla.py docstrings previously shipped inside
the triangle-counting fix commit are moved here so all documentation
lives in a single commit.
- drop the stale venv/pip installation section (the project uses uv;
  ../README.md documents the current workflow)
- correct the 'on GPU' claim: spla is OpenCL-based (GPU, PoCL CPU, or
  pure-CPU builds)
- extend the undirected-graph warning from SSSP to BFS and explain why:
  the pull step (mxv) walks each edge i -> j backwards, so it is only
  correct for symmetric graphs; push-only mode is directed-safe
- point the examples at files that actually ship (graph.txt,
  ../tests/data/undirected.mtx) instead of a nonexistent graph.mtx
- document the index-base difference (vectors 0-based, Matrix Market
  1-based) and the square-graph assumption
- replace the doctored --help dump with the real one
@@ -0,0 +1,113 @@
"""Graph loaders for the spla algorithm examples.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope, Spla (C++ core) has own loaders. May they be shared?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants