Skip to main content

Parallel random number generation that produces the same result, regardless of the number of threads

Project description

parallel-numpy-rng

tests

A multi-threaded random number generator backed by Numpy RNG, with parallelism provided by Numba.

Overview

Uses the "fast-forward" capability of the PCG-family of RNG, as exposed by the new-style Numpy RNG API, to generate random numbers in a multi-threaded manner. The key is that you get the same random numbers regardless of how many threads were used.

Only a two types of random numbers are supported right now: uniform and normal. More could be added if there is demand, although some kinds, like bounded random ints, are hard to parallelize in the approach used here.

Example + Performance

import numpy as np
import parallel_numpy_rng

seed = 123
parallel_rng = parallel_numpy_rng.default_rng(seed)
numpy_rng = np.random.default_rng(seed)

%timeit numpy_rng.random(size=10**9, dtype=np.float32)                           # 2.85 s
%timeit parallel_rng.random(size=10**9, dtype=np.float32, nthread=1)             # 3.34 s
%timeit parallel_rng.random(size=10**9, dtype=np.float32, nthread=128)           # 67.8 ms

%timeit numpy_rng.standard_normal(size=10**8, dtype=np.float32)                  # 1.12 s
%timeit parallel_rng.standard_normal(size=10**8,dtype=np.float32, nthread=1)     # 1.85 s
%timeit parallel_rng.standard_normal(size=10**8, dtype=np.float32, nthread=128)  # 43.5 ms

Note that the parallel_rng is slower than Numpy when using a single thread, because the parallel implementation requires a slower algorithm in some cases (this is especially noticeable for float64 and normals)

Installation

The code works and is reasonably well tested, so it's probably ready for use. It can be installed from PyPI:

$ pip install parallel-numpy-rng

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

parallel-numpy-rng-0.1.2.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

parallel_numpy_rng-0.1.2-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file parallel-numpy-rng-0.1.2.tar.gz.

File metadata

  • Download URL: parallel-numpy-rng-0.1.2.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for parallel-numpy-rng-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8d7b101820e2def248cd0b6f30f4171886d98f8291c65821c41a7109cef52c36
MD5 72586aca7f9e74f2a90349a15db6b6c1
BLAKE2b-256 333c8638aac5698be7ecf84763b53146df6bf6bcd84f77a1c444f35817c0b4ee

See more details on using hashes here.

File details

Details for the file parallel_numpy_rng-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: parallel_numpy_rng-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for parallel_numpy_rng-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e5840dc6dc44a0efde1c73345bfa672e6bc1e01e0ad07cbee3113381f5702b30
MD5 2165256f0312b93b9ac28df6f651ad8f
BLAKE2b-256 73c9fa70fe0e08b054ea10579110704491e76cd93318b82232f31444d274dd2c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page