Skip to main content

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.

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)

Source distribution for contextshare 0.1.1
File Size Uploaded
contextshare-0.1.1.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for contextshare 0.1.1
File Interpreter ABI Platform
contextshare-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.8 kB

Release files / contextshare-0.1.1.tar.gz

Download URL contextshare-0.1.1.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
cc4fc581ad15838cce66530a6ab01241ac500e7e82a638b5ce6cfbb44562dffa
BLAKE2b-256 checksum
How to use checksums
95c1ff49210997caacc316ff47a70e1277f5bec1fe17c2b559039515cfa6c5b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.10

Release files / contextshare-0.1.1-py3-none-any.whl

Download URL contextshare-0.1.1-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f47bfaa7aff5212afdbcfa472e227e0045d40284d64f9318c9cd5cbababf8df1
BLAKE2b-256 checksum
How to use checksums
186fed2e1dd3e900b5573546ed2d482c32a8084a3e61582508ae512e37113137
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.10

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page