Skip to main content

Variable Block Compressed Sparse Row Matrix with MPI support

Project description

Logo

Distributed variable-block sparse matrices with fast block kernels + thresholded SpMM. Python-first, MPI-scalable, designed for localized-orbital Hamiltonians and multi-DOF PDE systems.

Why VBCSR?

  • Hardware-Accelerated Performance: Leveraging SIMD (AVX/AVX2) instructions, precaching and threading, VBCSR delivers state-of-the-art performance for block-sparse matrix operations.
  • Easy Integration: Header-only C++ core for easy inclusion in both Python and C++ projects.
  • Pythonic & Intuitive: Perform complex linear algebra using natural Python syntax (A * x, A + B, A @ B) and standard NumPy arrays.
  • Scalable & Distributed: Built on MPI to handle massive datasets across distributed computing clusters.
  • Seamless Integration: Drop-in compatibility with SciPy solvers (scipy.sparse.linalg) for easy integration into existing workflows.

Real-World Applications

VBCSR is designed for complex physics and engineering simulations where block-sparse structures are natural.

1. Linear-Scaling Quantum Chemistry (McWeeny Purification)

Compute the density matrix of a large-scale system using McWeeny Purification. VBCSR's filtered SpMM ensures the matrix remains sparse throughout the iteration. See /examples/ex07_graphene_purification.py for a full implementation.

# 1. Create a large-scale tight-binding Hamiltonian (e.g., 5000 blocks)
H = VBCSR.create_random(global_blocks=5000, block_size_min=4, block_size_max=8)

# 2. Simple spectral scaling
H.scale(0.1); H.shift(0.5)

# 3. McWeeny Purification: P_{n+1} = 3P_n^2 - 2P_n^3
P = H.copy()
for _ in range(10):
    P2 = P.spmm(P, threshold=1e-5)
    P = 3.0 * P2 - 2.0 * P2.spmm(P, threshold=1e-5)
# P is now the density matrix, distributed across your MPI cluster!

2. Stochastic Trace Estimation & DOS (KPM)

Compute the Density of States (DOS) using the Kernel Polynomial Method (KPM) with stochastic trace estimation. See examples/ex08_graphene_dos.py for a full implementation.

Graphene DOS

energies, dos = compute_kpm_dos(H, num_moments=200, num_random_vecs=10)

3. Quantum Dynamics & PDE Solving

  • Quantum Dynamics: Propagate wavepackets in time using Chebyshev expansion (examples/ex09_graphene_dynamics.py).

Graphene Dynamics

  • Variable-Block PDEs: Solve heterogeneous systems (e.g., mixed Truss/Beam elements) with variable DOFs per node (examples/ex10_variable_pde.py).

Installation

VBCSR requires a C++ compiler, MPI, and BLAS/LAPACK (OpenBLAS or MKL).

Quick Start (Recommended)

conda install -c conda-forge openblas/mkl openmpi mpi4py
pip install vbcsr

From Source

git clone https://github.com/yourusername/vbcsr.git
cd vbcsr
pip install .

For advanced installation options (MKL, OpenMP, etc.), see doc/advanced_installation.md.

Documentation

Performance at a Glance

VBCSR is designed for high-performance block-sparse operations, significantly outperforming standard CSR implementations for block-structured matrices.

Blocks Approx Rows VBCSR Time (s) SciPy Time (s) Speedup
500 ~9000 0.0049 0.0209 4.21x
1000 ~18000 0.0096 0.0392 4.07x
5000 ~90000 0.0468 0.2029 4.33x
10000 ~180000 0.0931 0.4151 4.46x
20000 ~360000 0.1866 0.8377 4.49x

Benchmark Plot

Pythonic Usage

VBCSR matrices behave like first-class Python objects, supporting standard arithmetic and seamless integration with the SciPy ecosystem.

1. Easy Initialization from SciPy

Convert any SciPy sparse matrix (BSR or CSR) to a distributed VBCSR matrix in one line.

import scipy.sparse as sp
from vbcsr import VBCSR

# Create a SciPy BSR matrix
A_scipy = sp.bsr_matrix(np.random.rand(100, 100), blocksize=(10, 10))

# Convert to VBCSR (automatically distributed if MPI is initialized)
A = VBCSR.from_scipy(A_scipy)

2. Natural Arithmetic & Operators

Use standard Python operators for matrix-vector and matrix-matrix operations.

# Matrix-Vector Multiplication (SpMV)
y = A @ x  # or A.mult(x)

# Filtered Sparse Matrix-Matrix Multiplication (SpMM)
C = A @ B  # or A.spmm(B, threshold=1e-4)

# Matrix Arithmetic
D = 2.0 * A + B - 0.5 * C

3. Distributed Computing Made Simple

VBCSR handles the complexity of MPI communication behind the scenes.

from mpi4py import MPI
comm = MPI.COMM_WORLD

# Create a distributed matrix with 100 blocks
A = VBCSR.create_random(global_blocks=100, block_size_min=4, block_size_max=8, comm=comm)

# Perform distributed SpMV
x = A.create_vector()
x.set_constant(1.0)
y = A @ x

4. Custom Distributed Creation

For full control, define your own block distribution and connectivity pattern.

# Create the distributed matrix structure
A = VBCSR.create_distributed(owned_indices, block_sizes, adjacency, comm=comm)

# Fill blocks (can add local or remote blocks; VBCSR handles the exchange)
A.add_block(rank, rank, np.eye(2))
A.add_block(rank, (rank + 1) % comm.size, np.ones((2, 2)))

# Finalize assembly to synchronize remote data
A.assemble()

5. SciPy Solver Integration

VBCSR implements the LinearOperator interface, making it compatible with all SciPy iterative solvers.

from scipy.sparse.linalg import cg

# Solve Ax = b using Conjugate Gradient
x, info = cg(A, b, rtol=1e-6)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vbcsr-0.2.2.tar.gz (894.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

vbcsr-0.2.2-cp313-cp313-manylinux_2_28_x86_64.whl (16.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

vbcsr-0.2.2-cp312-cp312-manylinux_2_28_x86_64.whl (16.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

vbcsr-0.2.2-cp311-cp311-manylinux_2_28_x86_64.whl (16.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

vbcsr-0.2.2-cp310-cp310-manylinux_2_28_x86_64.whl (16.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

vbcsr-0.2.2-cp39-cp39-manylinux_2_28_x86_64.whl (16.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ x86-64

vbcsr-0.2.2-cp38-cp38-manylinux_2_28_x86_64.whl (16.3 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.28+ x86-64

File details

Details for the file vbcsr-0.2.2.tar.gz.

File metadata

  • Download URL: vbcsr-0.2.2.tar.gz
  • Upload date:
  • Size: 894.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for vbcsr-0.2.2.tar.gz
Algorithm Hash digest
SHA256 04e0d99fad262af85f084031be94713e20c6bdbc264f4503935967b125e31edc
MD5 6483f21dab754a255b06c2223a51d027
BLAKE2b-256 c48ffd913110765c9ab654a79b70637d9ea5bda607d4ae12ae524169c574d5df

See more details on using hashes here.

File details

Details for the file vbcsr-0.2.2-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for vbcsr-0.2.2-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0b277b73e8c3f158d6488793d726d35803c9339a4eb5eee3ceb309cb6d15dae6
MD5 c9d541226953683fd9cbf4d2ebc9881a
BLAKE2b-256 e342f41923737424d5cfe1ee50673861f781e40e581cfab6614634782109a822

See more details on using hashes here.

File details

Details for the file vbcsr-0.2.2-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for vbcsr-0.2.2-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7a50e336c5bd1fcc69dc724393fcf1bd8e4460e1a9e6266201973022a2cb5b1c
MD5 2d45d68585949205fdabc2c2ce9227a6
BLAKE2b-256 5ce6c2931daf8daa7aa2faa082dc4fca237963a6097a0989f5de2d37b76c900f

See more details on using hashes here.

File details

Details for the file vbcsr-0.2.2-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for vbcsr-0.2.2-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1993d24d065f8ccaec0f709de493e33b3a7d6884023ec9e83a5bd1ff852233af
MD5 537ae7304b67fe314adeea48cfc47276
BLAKE2b-256 45e89b9ecb3fa9fe6cc5958fec9988302ae888a37e8664f639bec5f614efc26f

See more details on using hashes here.

File details

Details for the file vbcsr-0.2.2-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for vbcsr-0.2.2-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5bfafe2c1299ba4c3693cccd4eded5c5cdbfdbcbaaf5e8f318a9179d90c3a309
MD5 e70f54d04bef708613b697db5fc999a2
BLAKE2b-256 07a4a970f1f847667a1d2742a68b4e39b44b8bbcfff0f4eebc95c11284a7efd6

See more details on using hashes here.

File details

Details for the file vbcsr-0.2.2-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for vbcsr-0.2.2-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 945a03d16fd56117f895b10751fbf73220dd1bfc5c0c01541564026b7abf3074
MD5 39b73cc4a459de4990cb937aa3f0f2f3
BLAKE2b-256 557255326573dffc4ef4a223e5499f50cfbc8b1fd94ab6a196fd6cf933da4fbf

See more details on using hashes here.

File details

Details for the file vbcsr-0.2.2-cp38-cp38-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for vbcsr-0.2.2-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 be4586e8297af9f83e7c86c9dabe95d04ab19a6ec30fc0d5b6e9eb8faaca427d
MD5 06dd89dc657047ab4e25ee945317247a
BLAKE2b-256 c6203052ea286b0724e81de9561df7e1a1b09806d5e92b8aa58eca48c941666e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page