Python wrapper for PT-Scotch graph partitioning library
Project description
PyScotch
WARNING: this is a vibe-engineering experiment - you probably shouldn't use this!
Python ctypes wrapper for the PT-Scotch graph partitioning library.
PyScotch provides a Pythonic interface to Scotch's graph/mesh partitioning, sparse matrix ordering, and distributed graph operations — without requiring mpi4py or Cython.
Features
- Graph partitioning — sequential and distributed (MPI)
- Mesh partitioning — with mesh-to-graph conversion
- Sparse matrix ordering — nested dissection for reduced fill-in
- Graph coloring — greedy heuristic coloring
- Distributed graph operations — coarsening, growing, band extraction, redistribution, induced subgraphs
- Multi-variant builds — 32/64-bit integer support with
_32/_64symbol suffixes - No mpi4py dependency — custom lightweight MPI ctypes wrapper
- Property-based testing — Hypothesis tests for stronger validation than Scotch's own C tests
Installation
Prerequisites
- Python 3.7+
- GCC or Clang
- An MPI implementation (OpenMPI or MPICH) for distributed operations
- Make, CMake, Git
Setup
git clone https://github.com/c4ffein/pyscotch.git
cd pyscotch
git submodule update --init --recursive
make build-all
uv pip install -e ".[dev]"
The submodule requires access to
gitlab.inria.fr. If it fails, check your authentication.
What build-all does
Builds 4 Scotch library variants into scotch-builds/:
| Directory | Contents |
|---|---|
lib32/ |
Sequential + parallel libraries, 32-bit SCOTCH_Num |
lib64/ |
Sequential + parallel libraries, 64-bit SCOTCH_Num |
inc32/ |
Headers for 32-bit variant |
inc64/ |
Headers for 64-bit variant |
Plus a libpyscotch_compat shared library that handles FILE* ABI compatibility between Python's C runtime and Scotch's.
Quick Start
Graph Partitioning
from pyscotch import Graph, Strategies
graph = Graph.from_edges([(0,1), (1,2), (2,3), (3,0)], num_vertices=4)
strategy = Strategies.partition_quality()
mapping = graph.partition(4, strategy)
print(mapping.get_partition_array()) # e.g. [0, 1, 2, 3]
Graph Coloring
from pyscotch import Graph
graph = Graph.from_edges([(0,1), (1,2), (2,0)], num_vertices=3)
colors, num_colors = graph.color()
print(f"{num_colors} colors: {colors}") # 3 colors: [0, 1, 2]
Sparse Matrix Ordering
from pyscotch import Graph, Strategies
graph = Graph()
graph.load("matrix.grf")
strategy = Strategies.ordering_quality()
ordering = graph.order(strategy)
print(ordering.get_permutation())
Distributed Graph (MPI)
from pyscotch import Dgraph
from pyscotch.mpi import mpi
mpi.init()
dgraph = Dgraph()
dgraph.load("graph.grf")
coarse, mult = dgraph.coarsen(coarrat=0.8)
if mult is not None:
print("Coarsened successfully")
coarse.exit()
dgraph.exit()
mpi.finalize()
Configuration
PyScotch selects which Scotch variant to load via environment variables:
| Variable | Values | Default | Description |
|---|---|---|---|
PYSCOTCH_INT_SIZE |
32, 64 |
64 |
Size of SCOTCH_Num integers |
PYSCOTCH_PARALLEL |
0, 1 |
0 |
Load PT-Scotch (parallel) or Scotch (sequential) |
PYSCOTCH_LIB_DIR |
path | unset | Explicit directory containing the Scotch libraries |
PYSCOTCH_SYSTEM |
0, 1 |
0 |
Force the system-installed Scotch (distro/conda packages) |
# Run with 32-bit sequential Scotch
PYSCOTCH_INT_SIZE=32 PYSCOTCH_PARALLEL=0 python my_script.py
Library discovery order: PYSCOTCH_LIB_DIR → libraries bundled in the
installed wheel → scotch-builds/ (development layout) → system-installed
Scotch. System packages (e.g. apt install libscotch-dev,
conda install scotch) ship unsuffixed symbols and a single integer width;
PyScotch detects this, verifies the width via SCOTCH_numSizeof(), and
refuses to load under a mismatched PYSCOTCH_INT_SIZE.
Testing
# Default: 64-bit parallel, no hypothesis
make test
# Full suite including hypothesis
make test-full
# All 4 variants with hypothesis (32/64 x seq/parallel)
make test-quadrant
Test categories:
| Directory | What |
|---|---|
tests/scotch_ports/ |
Direct ports of Scotch's C tests |
tests/scotch_ports_mpi/ |
MPI test ports (run via mpirun) |
tests/pyscotch_base/ |
PyScotch-specific tests (API completeness, int sizes, symbol prefixes) |
tests/hypothesis/ |
Property-based tests — stronger than Scotch's own C tests |
tests/pyscotch_integration/ |
End-to-end orchestrated workflows |
API Overview
Sequential
| Class | Key Methods |
|---|---|
Graph |
build(), load(), save(), partition(), order(), color(), induce_list(), induce_part(), stat(), base(), from_edges(), from_scipy_sparse(), to_scipy_sparse(), from_networkx(), to_networkx() |
Mesh |
build(), load(), save(), check(), to_graph(), partition() |
Architecture |
complete(), complete_weighted(), complete_graph(), name(), size() |
Strategy |
set(), set_default(), set_nested_dissection() |
Strategies |
partition_quality(), partition_speed(), ordering_quality(), ordering_speed() |
scotch_version() |
Returns (major, minor, patch) |
Distributed (MPI)
| Class | Key Methods |
|---|---|
Dgraph |
build(), build_grid_3d(), load(), save(), check(), stat(), coarsen(), ghst(), grow(), band(), redist(), induce_part(), part(), map(), map_compute(), map_save(), map_view(), order(), order_init()/order_compute()/order_perm()/order_exit(), order_save(), gather(), scatter(), free() |
Project Structure
pyscotch/
__init__.py # Public API exports
libscotch.py # ctypes bindings, library loading, type definitions
graph.py # Sequential graph operations
dgraph.py # Distributed graph operations (MPI)
mesh.py # Mesh operations
strategy.py # Strategy/preset management
arch.py # Target architecture definitions
mapping.py # Mapping result container
ordering.py # Ordering result container
mpi.py # Minimal MPI wrapper (OpenMPI, MPICH, Intel MPI)
api_decorators.py # @scotch_binding / @highlevel_api tracking
cli.py # Command-line interface
native/
file_compat.c # FILE* ABI compatibility layer
external/
scotch/ # Scotch submodule (gitlab.inria.fr)
scotchpy/ # Official Scotch Python bindings (reference only, not a dependency)
How It Works
PyScotch uses ctypes to call Scotch's C functions directly. Key design decisions:
- Dynamic struct sizing via
SCOTCH_*Sizeof()— never hardcodes structure sizes - Symbol suffixes (
_32/_64) viaSCOTCH_NAME_SUFFIX— allows loading multiple variants - FILE* compatibility layer — a small C shim (
libpyscotch_compat) that opens files with the same C runtime Scotch was compiled against, avoiding ABI mismatches @scotch_bindingdecorators — track which C functions each Python method wraps, enabling automated API completeness checks
Versioning
PyScotch versions are X.Y.z, where X.Y mirrors the Scotch series it is
built and tested against (e.g. PyScotch 7.0.* supports Scotch 7.0.x) and
z counts PyScotch's own releases within that series. Pin accordingly,
e.g. pyscotch~=7.0.0. Use pyscotch.scotch_version() to check the Scotch
actually loaded at runtime.
License
MIT License. See LICENSE.
PT-Scotch itself is distributed under the CeCILL-C license.
Acknowledgments
Built on PT-Scotch by Francois Pellegrini and the Scotch team at INRIA Bordeaux.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyscotch-7.0.0rc3.tar.gz.
File metadata
- Download URL: pyscotch-7.0.0rc3.tar.gz
- Upload date:
- Size: 139.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a876f13025c83f9a49b09502c5becaf7502882ed0090386c1c74d3b958022d8f
|
|
| MD5 |
0879f4f9052a605c65a242182c138198
|
|
| BLAKE2b-256 |
9ab85fa528a1e60952a9da20619488f3a11231484fbf18c5325b33cec8cc4dc0
|
Provenance
The following attestation bundles were made for pyscotch-7.0.0rc3.tar.gz:
Publisher:
wheels.yml on c4ffein/pyscotch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyscotch-7.0.0rc3.tar.gz -
Subject digest:
a876f13025c83f9a49b09502c5becaf7502882ed0090386c1c74d3b958022d8f - Sigstore transparency entry: 2171856898
- Sigstore integration time:
-
Permalink:
c4ffein/pyscotch@dd6e575232393f01ebb49a40489814d25e43bed7 -
Branch / Tag:
refs/tags/v7.0.0rc3 - Owner: https://github.com/c4ffein
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
wheels.yml@dd6e575232393f01ebb49a40489814d25e43bed7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyscotch-7.0.0rc3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pyscotch-7.0.0rc3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 706.9 kB
- Tags: Python 3, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbee8db09faa343af2c651db690f4b7556b60d93300a64a172d0342026831a3d
|
|
| MD5 |
b803f4cd87863a43dd5a3d64a32e690c
|
|
| BLAKE2b-256 |
84639e980bf963a812ae536eebe093f95c7f1fc8b51faf104967b77b18ed3739
|
Provenance
The following attestation bundles were made for pyscotch-7.0.0rc3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl:
Publisher:
wheels.yml on c4ffein/pyscotch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyscotch-7.0.0rc3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl -
Subject digest:
cbee8db09faa343af2c651db690f4b7556b60d93300a64a172d0342026831a3d - Sigstore transparency entry: 2171857129
- Sigstore integration time:
-
Permalink:
c4ffein/pyscotch@dd6e575232393f01ebb49a40489814d25e43bed7 -
Branch / Tag:
refs/tags/v7.0.0rc3 - Owner: https://github.com/c4ffein
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
wheels.yml@dd6e575232393f01ebb49a40489814d25e43bed7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyscotch-7.0.0rc3-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl.
File metadata
- Download URL: pyscotch-7.0.0rc3-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl
- Upload date:
- Size: 702.2 kB
- Tags: Python 3, manylinux: glibc 2.17+ ARM64, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b63557870fe197e28b77cf7833e2759919a8b3dd177f13d668c518a8bed4ba47
|
|
| MD5 |
2b679c6edee29929c3794c1000342e49
|
|
| BLAKE2b-256 |
621eb26a56df419f209bd3a3d63672dd08c6bf7373c1c8366cb73760a4498cde
|
Provenance
The following attestation bundles were made for pyscotch-7.0.0rc3-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl:
Publisher:
wheels.yml on c4ffein/pyscotch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyscotch-7.0.0rc3-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl -
Subject digest:
b63557870fe197e28b77cf7833e2759919a8b3dd177f13d668c518a8bed4ba47 - Sigstore transparency entry: 2171857044
- Sigstore integration time:
-
Permalink:
c4ffein/pyscotch@dd6e575232393f01ebb49a40489814d25e43bed7 -
Branch / Tag:
refs/tags/v7.0.0rc3 - Owner: https://github.com/c4ffein
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
wheels.yml@dd6e575232393f01ebb49a40489814d25e43bed7 -
Trigger Event:
push
-
Statement type: