An OpenMP Implementation for Python
Project description
OMP4Py: a pure Python implementation of OpenMP
OMP4Py is a Python library that provides an implementation of OpenMP, which is widely recognized as the standard programming model for exploiting multithreading parallelism in HPC. OMP4Py brings OpenMP’s familiar directive-based parallelization paradigm to Python, allowing developers to write parallel code with the same level of control and flexibility as in C, C++, or Fortran.
OMP4Py, as a pure Python implementation, provides greater flexibility compared to the Numba-based PyOMP solution. Unlike PyOMP, it does not impose restrictions on using functions from non-Numba-optimized libraries or certain Python objects and data structures. Additionally, OMP4Py code can be combined with mpi4py to develop parallel applications that exploit both intra-node and inter-node parallelism.
Features
- Native Python library
- Simplified parallelization of loops and code sections
- Efficient thread management and synchronization
- API compliant with OpenMP 3.0 standard
Installation
You can install omp4py via pip:
pip install omp4py
Note: OMP4Py is compatible with Python versions 3.10 and later, which include the Global Interpreter Lock (GIL). However, to fully exploit multithreading for scaling applications, it is necessary to use Python 3.13 (free threading) or later, which offers a no-GIL option.
Usage
OMP4Py defines a function omp
that operates similarly to OpenMP directives in C/C++, maintaining the same syntax and
functionality. The function itself has no effect when executed; it serves solely as a container for the OpenMP
directives. Note that when a OpenMP directive must be used within structured blocks, the omp
function is used together
as part of a with
block; otherwise, it is used as a standalone function call. Note that functions or classes
containing the OpenMP directives must be decorated with the @omp
decorator.
Here's a basic example of how to use OMP4Py to calculate $\pi$:
from omp4py import *
import random
@omp
def pi(num_points):
count = 0
with omp("parallel for reduction(+:count)"):
for i in range(num_points):
x = random.random()
y = random.random()
if x * x + y * y <= 1.0:
count += 1
pi = count / num_points
return pi
print(pi(10000000))
Tests
To run the unit tests and check the coverage, you can use the following commands with Poetry*:
-
Run the unit tests:
poetry run coverage run
-
Generate a coverage report:
poetry run coverage html
* Test dependencies are required, and pip only installs project dependencies. Use poetry install
to install them.
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 Distribution
File details
Details for the file omp4py-0.1.tar.gz
.
File metadata
- Download URL: omp4py-0.1.tar.gz
- Upload date:
- Size: 38.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Windows/11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4af1988b9adb7fa95e83a9166750abdd3841d4aefc10650e040fb98043dfc5e |
|
MD5 | 8f905a321802d5247b9f00db27a57115 |
|
BLAKE2b-256 | 9f7280afce992fc685613e65f5d06baf3d064ffa8968a7e6da4d2f032b454f83 |
File details
Details for the file omp4py-0.1-py3-none-any.whl
.
File metadata
- Download URL: omp4py-0.1-py3-none-any.whl
- Upload date:
- Size: 52.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Windows/11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9b6894468d660ae72c0f4333a350f6721dba30648c15ce3c147aa7f0c0cf14a |
|
MD5 | 9574464f7a6b5016ebde3e106318880d |
|
BLAKE2b-256 | 00e8f6b79d4c3eb4859701ab3f5d6a32d3960c138b756f586c2807a3025401a9 |