diff options
Diffstat (limited to 'numpy/random/examples/cython/extending_distributions.pyx')
-rw-r--r-- | numpy/random/examples/cython/extending_distributions.pyx | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/numpy/random/examples/cython/extending_distributions.pyx b/numpy/random/examples/cython/extending_distributions.pyx index da584e941..89477eb6d 100644 --- a/numpy/random/examples/cython/extending_distributions.pyx +++ b/numpy/random/examples/cython/extending_distributions.pyx @@ -1,13 +1,16 @@ #!/usr/bin/env python #cython: language_level=3 - +""" +This file shows how the distributions that are accessed through +distributions.pxd can be used Cython code. +""" import numpy as np cimport numpy as np cimport cython from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer from numpy.random.common cimport * from numpy.random.distributions cimport random_gauss_zig -from numpy.random import Xoroshiro128 +from numpy.random import Xoshiro256 @cython.boundscheck(False) @@ -18,14 +21,16 @@ def normals_zig(Py_ssize_t n): cdef const char *capsule_name = "BitGenerator" cdef double[::1] random_values - x = Xoroshiro128() + x = Xoshiro256() capsule = x.capsule if not PyCapsule_IsValid(capsule, capsule_name): raise ValueError("Invalid pointer to anon_func_state") rng = <bitgen_t *> PyCapsule_GetPointer(capsule, capsule_name) random_values = np.empty(n) - for i in range(n): - random_values[i] = random_gauss_zig(rng) + # Best practice is to release GIL and acquire the lock + with x.lock, nogil: + for i in range(n): + random_values[i] = random_gauss_zig(rng) randoms = np.asarray(random_values) return randoms @@ -38,7 +43,7 @@ def uniforms(Py_ssize_t n): cdef const char *capsule_name = "BitGenerator" cdef double[::1] random_values - x = Xoroshiro128() + x = Xoshiro256() capsule = x.capsule # Optional check that the capsule if from a BitGenerator if not PyCapsule_IsValid(capsule, capsule_name): @@ -46,8 +51,9 @@ def uniforms(Py_ssize_t n): # Cast the pointer rng = <bitgen_t *> PyCapsule_GetPointer(capsule, capsule_name) random_values = np.empty(n) - for i in range(n): - # Call the function - random_values[i] = rng.next_double(rng.state) + with x.lock, nogil: + for i in range(n): + # Call the function + random_values[i] = rng.next_double(rng.state) randoms = np.asarray(random_values) return randoms |