contextshare
Simple context manager to share numpy arrays in python multiprocessing. Installation on Python >= 3.8:
pip install contextshare
Example
The following code example is complete and compares a serial() implementation with the parallel() version thereof implemented with contextshare.
#!/usr/bin/env python
import contextshare as shared
import numpy as np
def serial(sigmas):
alphas = []
for sigma in sigmas:
K = np.exp(-0.5 * D / (sigma * sigma))
alphas.append(np.linalg.solve(K, b))
return alphas
def parallel(sigmas):
with shared.SharedMemory({"D": D, "b": b}, nworkers=11) as sm:
@sm.register
def one_sigma(sigma):
K = np.exp(-0.5 * D / (sigma * sigma))
return np.linalg.solve(K, b)
for sigma in sigmas:
one_sigma(sigma)
return sm.evaluate(progress=True)
if __name__ == "__main__":
N = 2000
D = np.random.random((N, N))
b = np.random.random(N)
sigmas = 2.0 ** np.arange(1, 12)
s = serial(sigmas)
p = parallel(sigmas)
Documentation
The context manager shared.SharedMemory takes two arguments: the dictionary of numpy arrays to make available on all parallel workers and the number of workers to create.
# makes variables D and b available under the same name on 11 workers
with shared.SharedMemory({"D": D, "b": b}, nworkers=11) as sm:
The workers are spawned upon entering the context and are stopped upon exiting. Shared memory references are cleaned up automatically. Note that only numpy arrays are supported for sharing. Other arguments should be placed in the arguments of the function below. The function to call (i.e. the body of the serial for loop) needs to be placed in a function and either decorated or called explicitly:
# decorator
@sm.register
def one_sigma(sigma):
pass
# or, equivalently, an explicit call
sm.register(one_sigma)
Calling this decorated function returns immediately and enqueues a function call. Calling
sm.evaluate(progress=True)
starts the calculations and returns the results in order. With progress=True a progress bar is shown, default is to be silent.
Credits
This package was created with Cookiecutter using the audreyr/cookiecutter-pypackage` template.
======= History
0.1.0 (2021-02-23)
- First release on PyPI.
Metadata
Release files for contextshare 0.1.1
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 |
|---|---|---|---|---|
| contextshare-0.1.1.tar.gz | 10.3 kB | Without provenance | Details for contextshare-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| contextshare-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for contextshare-0.1.1-py3-none-any.whl |
Total release size: 14.8 kB
Security for contextshare 0.1.1
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.