summaryrefslogtreecommitdiff
path: root/numpy/random/examples/numba
diff options
context:
space:
mode:
authormattip <matti.picus@gmail.com>2019-11-19 22:23:00 -0800
committermattip <matti.picus@gmail.com>2019-11-19 22:23:00 -0800
commit521ea22552297345680c426c0204b17b644e6bd0 (patch)
tree883cc4a161230acc71692c71ff5fc69a98ffed35 /numpy/random/examples/numba
parent7d75171c4e6fbb2ed0aebda10908a6a0b01d0c7e (diff)
downloadnumpy-521ea22552297345680c426c0204b17b644e6bd0.tar.gz
MAINT: move numpy/random/examples -> numpy/random/_examples
Diffstat (limited to 'numpy/random/examples/numba')
-rw-r--r--numpy/random/examples/numba/extending.py84
-rw-r--r--numpy/random/examples/numba/extending_distributions.py61
2 files changed, 0 insertions, 145 deletions
diff --git a/numpy/random/examples/numba/extending.py b/numpy/random/examples/numba/extending.py
deleted file mode 100644
index 0d240596b..000000000
--- a/numpy/random/examples/numba/extending.py
+++ /dev/null
@@ -1,84 +0,0 @@
-import numpy as np
-import numba as nb
-
-from numpy.random import PCG64
-from timeit import timeit
-
-bit_gen = PCG64()
-next_d = bit_gen.cffi.next_double
-state_addr = bit_gen.cffi.state_address
-
-def normals(n, state):
- out = np.empty(n)
- for i in range((n + 1) // 2):
- x1 = 2.0 * next_d(state) - 1.0
- x2 = 2.0 * next_d(state) - 1.0
- r2 = x1 * x1 + x2 * x2
- while r2 >= 1.0 or r2 == 0.0:
- x1 = 2.0 * next_d(state) - 1.0
- x2 = 2.0 * next_d(state) - 1.0
- r2 = x1 * x1 + x2 * x2
- f = np.sqrt(-2.0 * np.log(r2) / r2)
- out[2 * i] = f * x1
- if 2 * i + 1 < n:
- out[2 * i + 1] = f * x2
- return out
-
-# Compile using Numba
-normalsj = nb.jit(normals, nopython=True)
-# Must use state address not state with numba
-n = 10000
-
-def numbacall():
- return normalsj(n, state_addr)
-
-rg = np.random.Generator(PCG64())
-
-def numpycall():
- return rg.normal(size=n)
-
-# Check that the functions work
-r1 = numbacall()
-r2 = numpycall()
-assert r1.shape == (n,)
-assert r1.shape == r2.shape
-
-t1 = timeit(numbacall, number=1000)
-print('{:.2f} secs for {} PCG64 (Numba/PCG64) gaussian randoms'.format(t1, n))
-t2 = timeit(numpycall, number=1000)
-print('{:.2f} secs for {} PCG64 (NumPy/PCG64) gaussian randoms'.format(t2, n))
-
-# example 2
-
-next_u32 = bit_gen.ctypes.next_uint32
-ctypes_state = bit_gen.ctypes.state
-
-@nb.jit(nopython=True)
-def bounded_uint(lb, ub, state):
- mask = delta = ub - lb
- mask |= mask >> 1
- mask |= mask >> 2
- mask |= mask >> 4
- mask |= mask >> 8
- mask |= mask >> 16
-
- val = next_u32(state) & mask
- while val > delta:
- val = next_u32(state) & mask
-
- return lb + val
-
-
-print(bounded_uint(323, 2394691, ctypes_state.value))
-
-
-@nb.jit(nopython=True)
-def bounded_uints(lb, ub, n, state):
- out = np.empty(n, dtype=np.uint32)
- for i in range(n):
- out[i] = bounded_uint(lb, ub, state)
-
-
-bounded_uints(323, 2394691, 10000000, ctypes_state.value)
-
-
diff --git a/numpy/random/examples/numba/extending_distributions.py b/numpy/random/examples/numba/extending_distributions.py
deleted file mode 100644
index 9233ccced..000000000
--- a/numpy/random/examples/numba/extending_distributions.py
+++ /dev/null
@@ -1,61 +0,0 @@
-r"""
-On *nix, execute in randomgen/src/distributions
-
-export PYTHON_INCLUDE=#path to Python's include folder, usually \
- ${PYTHON_HOME}/include/python${PYTHON_VERSION}m
-export NUMPY_INCLUDE=#path to numpy's include folder, usually \
- ${PYTHON_HOME}/lib/python${PYTHON_VERSION}/site-packages/numpy/core/include
-gcc -shared -o libdistributions.so -fPIC distributions.c \
- -I${NUMPY_INCLUDE} -I${PYTHON_INCLUDE}
-mv libdistributions.so ../../examples/numba/
-
-On Windows
-
-rem PYTHON_HOME is setup dependent, this is an example
-set PYTHON_HOME=c:\Anaconda
-cl.exe /LD .\distributions.c -DDLL_EXPORT \
- -I%PYTHON_HOME%\lib\site-packages\numpy\core\include \
- -I%PYTHON_HOME%\include %PYTHON_HOME%\libs\python36.lib
-move distributions.dll ../../examples/numba/
-"""
-import os
-
-import numba as nb
-import numpy as np
-from cffi import FFI
-
-from numpy.random import PCG64
-
-ffi = FFI()
-if os.path.exists('./distributions.dll'):
- lib = ffi.dlopen('./distributions.dll')
-elif os.path.exists('./libdistributions.so'):
- lib = ffi.dlopen('./libdistributions.so')
-else:
- raise RuntimeError('Required DLL/so file was not found.')
-
-ffi.cdef("""
-double random_gauss_zig(void *bitgen_state);
-""")
-x = PCG64()
-xffi = x.cffi
-bit_generator = xffi.bit_generator
-
-random_gauss_zig = lib.random_gauss_zig
-
-
-def normals(n, bit_generator):
- out = np.empty(n)
- for i in range(n):
- out[i] = random_gauss_zig(bit_generator)
- return out
-
-
-normalsj = nb.jit(normals, nopython=True)
-
-# Numba requires a memory address for void *
-# Can also get address from x.ctypes.bit_generator.value
-bit_generator_address = int(ffi.cast('uintptr_t', bit_generator))
-
-norm = normalsj(1000, bit_generator_address)
-print(norm[:12])