Dynamic ScaLAPACK wrapper for Python and Numpy-like ScaLAPACK Interface
Summary
scalapack4py is a library designed to provide a Pythonic interface
to ScaLAPACK and BLACS functionality. This library also includes
NPScal, an interface to the distributed memory linear algebra
routines of ScaLAPACK with matching call signatures to Numpy.
Full scalapack4py documentation.
Package layout
| Module | Contents |
|---|---|
scalapack4py.core |
ctypes bindings: load_scalapack finds and loads the library, ScaLAPACKLib exposes its routines |
scalapack4py.blacs |
BLACS process grids (BLACSGrid, init, finalize) and distribution descriptors (BLACSDescriptor, descriptor_registry) |
scalapack4py.npscal |
NPScal, the numpy-like distributed array, built on the two layers above |
ScaLAPACKLib, load_scalapack and BLACSGrid can also be imported
from scalapack4py directly. The names used before version 1.1
(ScaLAPACK4py, scalapack4py.library_loader,
scalapack4py.npscal.blacs_ctxt_management, ...) still work, but raise
a DeprecationWarning.
Requirements
ScaLAPACK Shared Object Library
A ScaLAPACK shared-object library linked to the BLACS, BLAS, and
LAPACK libraries. The Open MPI and Intel oneAPI packages from apt
(Debian, Ubuntu) and dnf (Fedora) below are found automatically.
Any other ScaLAPACK shared library, such as one you built yourself, can
be used by giving its path in the SCALAPACK_LIB_PATH environment
variable or as npsc.init(lib="/path/to/libscalapack.so").
Debian/Ubuntu OpenMPI
sudo apt update
sudo apt install libscalapack-openmpi-dev
Fedora OpenMPI
sudo dnf install openmpi openmpi-devel scalapack-openmpi
Debian/Ubuntu OneAPI
wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \
| gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" \
| sudo tee /etc/apt/sources.list.d/oneAPI.list
sudo apt update
sudo apt install intel-oneapi-mkl-devel intel-oneapi-mpi-devel
source /opt/intel/oneapi/setvars.sh
Fedora OneAPI
sudo tee /etc/yum.repos.d/oneAPI.repo << EOF
[oneAPI]
name=Intel oneAPI repository
baseurl=https://yum.repos.intel.com/oneapi
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://yum.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
EOF
sudo dnf install intel-oneapi-mkl-devel intel-oneapi-mpi-devel
source /opt/intel/oneapi/setvars.sh
setvars.sh sets MKLROOT, from which load_scalapack() finds the
MKL ScaLAPACK and BLACS libraries. With Intel MPI, mpi4py must also
be built against it (pip install --no-binary mpi4py mpi4py after
sourcing setvars.sh), as mpirun, mpi4py and ScaLAPACK must all
use the same MPI.
Python dependencies
python>=3.9numpy>=2.0.0mpi4py>=3.1.3
Installation
scalapack4py may be installed through the pip package manager:
pip install scalapack4py
A supported version of ScaLAPACK, BLACS, and BLAS are also required. At present, OpenMPI and OneAPI implementations are supported.
The Fundamentals of scalapack4py
The core layer, scalapack4py.core, provides a Pythonic interface to
the symbols stored in the dynamic ScaLAPACK and BLACS shared-object
libraries.
This functionality supports the creation of BLACS grids, BLACS descriptors, and the distribution of serialised Numpy arrays to block distributed ScaLAPACK arrays.
# run as: mpiexec -n 2 python3 -u scatter_gather.py
import numpy as np
from mpi4py import MPI
from ctypes import POINTER, c_int, c_double
from scalapack4py import ScaLAPACKLib, load_scalapack
sl = ScaLAPACKLib(load_scalapack())
n = 5
dtype = np.complex128
a = np.arange(n*n, dtype=dtype).reshape((n, n), order='F') if MPI.COMM_WORLD.rank == 0 else None
nprow, npcol = 2, 1
ctx = sl.make_blacs_context(sl.get_default_system_context(), nprow, npcol)
descr = sl.make_blacs_desc(ctx, n, n)
# Scatter the matrix held on rank 0 into each rank's local block ...
b = np.zeros((descr.locrow, descr.loccol), dtype=dtype)
sl.scatter_numpy(a, POINTER(c_int)(descr), b.ctypes.data_as(POINTER(c_double)), b.dtype)
# ... and gather it back to rank 0 (None on the other ranks)
c = sl.gather_numpy(POINTER(c_int)(descr), b, (n, n))
load_scalapack() searches the usual system locations, or the path
in SCALAPACK_LIB_PATH when it is set.
scalapack4py can also take over a distributed array that an
external program has already set up, through a pointer to its local
block. Below, libmatexport.so is a small C library from
examples/matrix_export that creates a
block-cyclic n x n matrix on a 2 x 2 BLACS grid; npsc.cptr_to_array
copies each rank's local block into an NPScal array, given the
matrix's global shape and block size:
# run as: mpiexec -n 4 python3 -u import_matrix.py
import numpy as np
from ctypes import CDLL, RTLD_GLOBAL, POINTER, c_int, c_double
import scalapack4py.npscal as npsc
from scalapack4py.blacs import BLACSDescriptor
grid = npsc.init(nprow=2, npcol=2)
lib = CDLL("./libmatexport.so", mode=RTLD_GLOBAL)
lib.matexport_init.argtypes = [c_int, c_int, c_int, c_int]
lib.matexport_data.restype = POINTER(c_double)
n, nb = 7, 2
lib.matexport_init(n, nb, grid.nprow, grid.npcol)
desc = BLACSDescriptor(m=n, n=n, mb=nb, nb=nb)
a = npsc.cptr_to_array(lib.matexport_data(), desc, grid=grid)
full = npsc.gather_to_global(a) # the whole matrix, on every rank
lib.matexport_finalize()
examples/matrix_export/export_demo.py
does the same with the core layer alone, reading the C library's own
ScaLAPACK descriptor with ScaLAPACKLib.wrap_blacs_desc.
The Fundamentals of NPScal
NPScal provides an interface to parallelised, distributed memory
linear algebra operations with a Numpy-like syntax. This library
simplifies much of the functionality of the scalapack4py.core and
scalapack4py.blacs layers, including the:
- Initialisation and finalisation of BLACS grids and descriptors
- Managing block sizes and BLACS grids across complex code bases
- Creation of BLACS distributed arrays
- Execution of ScaLAPACK routines through the Numpy dispatch mechanism
The full list of supported operations is given in the Supported Functionality section of the documentation.
A simple workflow for matrix-matrix multiplication of a distributed array:
# run as: mpiexec -n 4 python3 -u matmul.py
import numpy as np
import scalapack4py.npscal as npsc
# Creates a BLACS grid over all processes and makes it the default
npsc.init()
n = 2500
b = np.arange(n*n, dtype=np.float64).reshape((n, n)) / n**2
dist_b = npsc.array(b) # distributed from rank 0
dist_b_matmul = dist_b @ dist_b # parallel matrix product (p?gemm)
result = npsc.gather_to_global(dist_b_matmul)
assert np.allclose(result, b @ b)
Contributing Guidelines
Bug reports, questions and feature requests are welcome on the issue tracker.
Code contributions are made as merge requests against main on
GitLab. Please add tests for
new functionality and check that the existing suite still passes:
pip install -e .[test]
pytest tests/unit
mpirun -n 4 python -m pytest --with-mpi tests/integration tests/npscal
License
scalapack4py is released under the MIT License; see LICENSE.
Metadata
Release files for scalapack4py 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| scalapack4py-1.1.0.tar.gz | 99.0 kB | Without provenance | Details for scalapack4py-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| scalapack4py-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for scalapack4py-1.1.0-py3-none-any.whl |
Total release size: 206.8 kB
Release files / scalapack4py-1.1.0.tar.gz
| Download URL | scalapack4py-1.1.0.tar.gz |
|---|---|
| Size | 99.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2db67b69e4fab343c1e297d047faac5ebd25ec76f52bbc252a0a60e4c75cf6cb
|
|
BLAKE2b-256 checksum How to use checksums |
5a1f095ebeccc1526265f8c0b1872b6783b2bbf09e31f73815b674a805e7edc7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Release files / scalapack4py-1.1.0-py3-none-any.whl
| Download URL | scalapack4py-1.1.0-py3-none-any.whl |
|---|---|
| Size | 107.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b424537b1e66211b579383d60cd4f8d12dbd3637c75e7817d00692a88ad18e42
|
|
BLAKE2b-256 checksum How to use checksums |
76d81dd4958e854ecacee3295ea965e9fec978d699aa35de2a1e4f425cc5de2d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Security for scalapack4py 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.