Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Python

on:
push:
branches:
- main
- release
- pre-release
paths:
- '.github/**'
- 'deps/**'
- 'include/**'
- 'src/**'
- 'python/**'
- 'CMakeLists.txt'
pull_request:
branches: [ main ]

env:
build-dir: "build"
config: "Release"
nt: "4"

jobs:
lint:
name: Lint
runs-on: ubuntu-24.04
defaults:
run:
working-directory: python
steps:
- uses: actions/checkout@v7

- uses: astral-sh/[email protected]
with:
enable-cache: true

- name: Check formatting
run: uv run ruff format --check .

- name: Lint
run: uv run ruff check .

test:
name: Test ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-24.04
lib: libspla_x64.so
- os: windows-2022
lib: spla_x64.dll
- os: macos-latest
lib: libspla_arm64.dylib
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v7

- uses: seanmiddleditch/gha-setup-ninja@master

- uses: ilammy/msvc-dev-cmd@v1
if: runner.os == 'Windows'
with:
arch: amd64

- uses: astral-sh/[email protected]
with:
enable-cache: true

- name: Configure CMake
run: |
cmake . -G Ninja -B ${{ env.build-dir }} \
-DCMAKE_BUILD_TYPE=${{ env.config }} \
-DSPLA_BUILD_TESTS=OFF \
-DSPLA_BUILD_EXAMPLES=OFF \
-DSPLA_BUILD_OPENCL=OFF

- name: Build library
run: |
cmake --build ${{ env.build-dir }} --verbose -j ${{ env.nt }}

- name: Stage native library
run: cp ${{ env.build-dir }}/${{ matrix.lib }} python/pyspla/

- name: Run tests
working-directory: python
run: uv run pytest tests/ -v
14 changes: 13 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,16 @@ dataset*
registry.txt

# Python cache files
/**/__pycache__
/**/__pycache__

# Python virtual environments (uv)
.venv/

# Staged native libraries (build artifacts packaged into wheels)
python/pyspla/libspla_*
python/pyspla/spla_*
# Python packaging artifacts
*.egg-info/

# Local wheel build output (uv build)
python/build/
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ add_library(spla SHARED
src/binding/c_scalar.cpp
src/binding/c_type.cpp
src/binding/c_vector.cpp
src/binding/c_descriptor.cpp
# C++ optional part
${SRC_OPENCL})

Expand Down
14 changes: 6 additions & 8 deletions examples/sssp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@

#include "common.hpp"
#include "options.hpp"

#include <spla.hpp>


int main(int argc, const char* const* argv) {
std::shared_ptr<cxxopts::Options> options = make_options("sssp", "sssp (single-source shortest path) algorithm with spla library");
cxxopts::ParseResult args;
Expand Down Expand Up @@ -73,12 +71,12 @@ int main(int argc, const char* const* argv) {
desc->set_traversal_mode(static_cast<spla::Descriptor::TraversalMode>(args[OPT_PUSH_PULL].as<int>() - 1));
desc->set_front_factor(args[OPT_FRONT_FACTOR].as<float>());

const spla::uint w = 1.0f;
const auto& Ai = loader.get_Ai();
const auto& Aj = loader.get_Aj();
const auto& Ai = loader.get_Ai();
const auto& Aj = loader.get_Aj();
const auto& Aw = loader.get_Aw();

for (std::size_t k = 0; k < loader.get_n_values(); ++k) {
A->set_float(Ai[k], Aj[k], w);
A->set_float(Ai[k], Aj[k], Aw[k]);
}

const int n_iters = args[OPT_NITERS].as<int>();
Expand Down Expand Up @@ -114,7 +112,7 @@ int main(int argc, const char* const* argv) {

for (std::size_t k = 0; k < loader.get_n_values(); ++k) {
ref_Ai[Ai[k]].push_back(Aj[k]);
ref_Ax[Ai[k]].push_back(w);
ref_Ax[Ai[k]].push_back(Aw[k]);
}

timer_ref.lap_begin();
Expand All @@ -141,4 +139,4 @@ int main(int argc, const char* const* argv) {
std::cout << std::endl;

return 0;
}
}
4 changes: 2 additions & 2 deletions examples/tc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int main(int argc, const char* const* argv) {
const auto& Aj = loader.get_Aj();

for (std::size_t k = 0; k < loader.get_n_values(); ++k) {
if (Ai[k] > Aj[k]) {
if (Ai[k] < Aj[k]) {
A->set_int(Ai[k], Aj[k], 1);
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ int main(int argc, const char* const* argv) {

// for tc it is important to have no duplicates
for (std::size_t k = 0; k < loader.get_n_values(); ++k) {
if (Ai[k] > Aj[k]) {
if (Ai[k] < Aj[k]) {
ref_A_set[Ai[k]].insert(Aj[k]);
}
}
Expand Down
8 changes: 8 additions & 0 deletions include/spla.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ SPLA_API spla_Status spla_Vector_get_float(spla_Vector v, spla_uint row_id, floa
SPLA_API spla_Status spla_Vector_build(spla_Vector v, spla_MemView keys, spla_MemView values);
SPLA_API spla_Status spla_Vector_read(spla_Vector v, spla_MemView* keys, spla_MemView* values);
SPLA_API spla_Status spla_Vector_clear(spla_Vector v);
SPLA_API spla_Status spla_Vector_fill_with(spla_Vector v, spla_Scalar value);

//////////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -389,6 +390,13 @@ SPLA_API spla_Status spla_Exec_v_count_mf(spla_Scalar r, spla_Vector v, spla_Des

//////////////////////////////////////////////////////////////////////////////////////

/* Descriptor object methods */

SPLA_API spla_Status spla_Descriptor_make(spla_Descriptor* descriptor);
SPLA_API spla_Status spla_Descriptor_set_struct_only(spla_Descriptor descriptor, spla_bool value);
SPLA_API spla_Status spla_Descriptor_set_early_exit(spla_Descriptor descriptor, spla_bool value);

//////////////////////////////////////////////////////////////////////////////////////
#if defined(__cplusplus)
}
#endif
Expand Down
8 changes: 5 additions & 3 deletions include/spla/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace spla {
*/
SPLA_API bool load(std::filesystem::path file_path,
bool offset_indices = true,
bool make_undirected = true,
bool make_undirected = false,
bool remove_loops = true);

/**
Expand All @@ -80,15 +80,17 @@ namespace spla {

[[nodiscard]] SPLA_API const std::vector<uint>& get_Ai() const;
[[nodiscard]] SPLA_API const std::vector<uint>& get_Aj() const;
[[nodiscard]] SPLA_API uint get_n_rows() const;
[[nodiscard]] SPLA_API uint get_n_cols() const;
[[nodiscard]] SPLA_API const std::vector<float>& get_Aw() const;
[[nodiscard]] SPLA_API uint get_n_rows() const;
[[nodiscard]] SPLA_API uint get_n_cols() const;
[[nodiscard]] SPLA_API std::size_t get_n_values() const;

private:
std::string m_name;
std::filesystem::path m_file_path;
std::vector<uint> m_Ai;
std::vector<uint> m_Aj;
std::vector<float> m_Aw;
bool m_base_is_zero = false;
uint m_n_rows = 0;
uint m_n_cols = 0;
Expand Down
5 changes: 0 additions & 5 deletions python/MANIFEST.in

This file was deleted.

27 changes: 27 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,33 @@ Delete package if no more required:
$ pip uninstall pyspla
```

## Development

The Python tooling is managed with [uv](https://docs.astral.sh/uv/).

```shell
# from the repository root: build the native library (CPU-only config)
cmake . -G Ninja -B build -DCMAKE_BUILD_TYPE=Release -DSPLA_BUILD_OPENCL=OFF
cmake --build build

# stage the library into the package
cp build/libspla_x64.so python/pyspla/

# from python/: create and sync the dev environment (installs pyspla + dev tools)
cd python
uv sync

# lint and format check
uv run ruff check .
uv run ruff format --check .

# run the test suite
uv run pytest tests/ -v

# build sdist + wheel (bundles the staged library)
uv build
```

## Contributors

- Egor Orachyov (Github: [@EgorOrachyov](https://github.com/EgorOrachyov))
Expand Down
115 changes: 115 additions & 0 deletions python/algorithms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Graph Algorithms with SPLA

This project provides a Python implementation of graph algorithms using the
sparse linear algebra primitives of SPLA (OpenCL-based: runs on a GPU, on CPU
via PoCL, or with a pure-CPU build).

The algorithms are tested in [`../tests/`](../tests/) against classic
(pure-Python) reference implementations; see the development instructions in
[`../README.md`](../README.md).

---

## Supported Algorithms

| Algorithm | Flag |
|-----------|------|
| BFS (Breadth-First Search) | `--algo bfs` |
| SSSP (Single-Source Shortest Paths) | `--algo sssp` |
| PageRank | `--algo pr` |
| Triangle Counting | `--algo tc` |

---

## Usage

```text
usage: main_spla.py [-h] --algo {bfs,sssp,pr,tc} [-m MATRIX] [-v VECTORS]
[-o OUTPUT] [-s START] [-a ALPHA] [-e EPS]

options:
-h, --help show this help message and exit
--algo {bfs,sssp,pr,tc}
Algorithm to run: bfs - Breadth-First Search, sssp -
Single-Source Shortest Paths, pr - PageRank, tc -
Triangle Counting
-m, --matrix MATRIX Path to graph in Matrix Market format (.mtx)
-v, --vectors VECTORS
Path to graph in vectors format (.txt)
-o, --output OUTPUT Output file path
-s, --start START Start vertex(default: 0)
-a, --alpha ALPHA Damping factor(default: 0.85)
-e, --eps EPS Convergence tolerance(default: 1e-4)
```

---

## Examples

```bash
# BFS (vectors format; graph.txt ships with this directory)
python main_spla.py --algo bfs -v graph.txt -s 0 -o bfs_result.txt

# SSSP
python main_spla.py --algo sssp -v graph.txt -s 0 -o sssp_result.txt

# PageRank
python main_spla.py --algo pr -v graph.txt -a 0.85 -e 1e-6 -o pr_result.txt

# Triangle Counting (Matrix Market format)
python main_spla.py --algo tc -m ../tests/data/undirected.mtx -o tc_result.txt
```

---

## Input format

Two options:

- `-v graph.txt` — vectors format, **0-based** indices
- `-m graph.mtx` — Matrix Market format, **1-based** indices per the
specification (converted to 0-based on load); see
[https://sparse.tamu.edu/](https://sparse.tamu.edu/)

Both readers assume a **square** graph: the size `n` is taken as the largest
index (or matrix dimension) seen, and an `n x n` matrix is built.

### graph.txt (vectors format)

Three lines of space-separated numbers: row indices, column indices, values.

```text
0 1 2 # I: source vertex indices
1 2 0 # J: target vertex indices
5 3 2 # V: edge weights
```

### graph.mtx (Matrix Market)

```text
%%MatrixMarket matrix coordinate real general
4 4 4
1 2 1.0
2 3 2.0
3 4 3.0
4 1 4.0
```

---

## Directed graphs

The frontier-based algorithms (`bfs`, `sssp`) advance the frontier with either
a **push** step (vector-matrix product) or a **pull** step (matrix-vector
product); in the default `push_pull` mode the cheaper one is picked per
iteration by frontier density.

The push step follows directed edges correctly, but the pull step walks each
edge `i -> j` backwards (it updates `i` from `j` via the entry `A[i, j]`), so
it is only correct for **undirected** (symmetric) graphs.

- **Undirected graphs:** all propagation modes work.
- **Directed graphs:** use push-only propagation. `main_spla.py` already does
this for the vectors format; for the Matrix Market format the default
`push_pull` mode is used, so directed graphs should be converted to the
vectors format (or the call changed to `push_only=True`).
Loading
Loading