Metadata-Version: 2.4
Name: scalapack4py
Version: 1.1.0
Summary: Python wrapper for dynamically loaded ScaLAPACK and BLACS libraries
Author-email: Pavel Stishenko <pstishenko@gmail.com>, Gabriel Bramley <BramleyG@cardiff.ac.uk>
License: MIT License
        
        Copyright (c) 2022 Pavel V Stishenko, Andrew Logsdail, Reinhard Maurer, 
        Volker Blum, Mariana Rossi, Ben Hourahine, Scott Woodley, Thomas Keal
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://gitlab.com/pvst/scalapack4py
Project-URL: Bug Tracker, https://gitlab.com/pvst/scalapack4py/-/issues
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: mpi4py>=3.1.3
Requires-Dist: numpy>=2.0.0
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == "test"
Requires-Dist: pytest-mpi>=0.6; extra == "test"
Dynamic: license-file

# 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](https://pvst.gitlab.io/scalapack4py/).

### 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
``` shell
	sudo apt update
	sudo apt install libscalapack-openmpi-dev
```

Fedora OpenMPI
``` shell
	sudo dnf install openmpi openmpi-devel scalapack-openmpi
```

Debian/Ubuntu OneAPI
``` shell
	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
``` shell
	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.9`
- `numpy>=2.0.0`
- `mpi4py>=3.1.3`

## Installation

`scalapack4py` may be installed through the pip package manager:

``` shell
	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.

``` python
# 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`](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:

``` python
# 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`](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](https://pvst.gitlab.io/scalapack4py/supported.html)
section of the documentation.

A simple workflow for matrix-matrix multiplication of a distributed
array:

```python
# 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](https://gitlab.com/pvst/scalapack4py/-/issues).

Code contributions are made as merge requests against `main` on
[GitLab](https://gitlab.com/pvst/scalapack4py). Please add tests for
new functionality and check that the existing suite still passes:

``` shell
	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](LICENSE).

