summaryrefslogtreecommitdiff
path: root/numpy/random/_examples/numba/extending_distributions.py
diff options
context:
space:
mode:
authorDongHun Kwak <dh0128.kwak@samsung.com>2020-12-31 09:38:55 +0900
committerDongHun Kwak <dh0128.kwak@samsung.com>2020-12-31 09:38:55 +0900
commit92fba4b9b454bc82b27770074a248dd685053832 (patch)
tree6ee53dfa05d52fbd824da6abc7d190485505f646 /numpy/random/_examples/numba/extending_distributions.py
parent295fa02e974b890f98bb7bf6a94045c2cd3f5f68 (diff)
downloadpython-numpy-92fba4b9b454bc82b27770074a248dd685053832.tar.gz
python-numpy-92fba4b9b454bc82b27770074a248dd685053832.tar.bz2
python-numpy-92fba4b9b454bc82b27770074a248dd685053832.zip
Imported Upstream version 1.18.0upstream/1.18.0
Diffstat (limited to 'numpy/random/_examples/numba/extending_distributions.py')
-rw-r--r--numpy/random/_examples/numba/extending_distributions.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/numpy/random/_examples/numba/extending_distributions.py b/numpy/random/_examples/numba/extending_distributions.py
new file mode 100644
index 000000000..7cf8bf0b0
--- /dev/null
+++ b/numpy/random/_examples/numba/extending_distributions.py
@@ -0,0 +1,67 @@
+r"""
+Building the required library in this example requires a source distribution
+of NumPy or clone of the NumPy git repository since distributions.c is not
+included in binary distributions.
+
+On *nix, execute in numpy/random/src/distributions
+
+export ${PYTHON_VERSION}=3.8 # Python version
+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 and PYTHON_VERSION are setup dependent, this is an example
+set PYTHON_HOME=c:\Anaconda
+set PYTHON_VERSION=38
+cl.exe /LD .\distributions.c -DDLL_EXPORT \
+ -I%PYTHON_HOME%\lib\site-packages\numpy\core\include \
+ -I%PYTHON_HOME%\include %PYTHON_HOME%\libs\python%PYTHON_VERSION%.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_standard_normal(void *bitgen_state);
+""")
+x = PCG64()
+xffi = x.cffi
+bit_generator = xffi.bit_generator
+
+random_standard_normal = lib.random_standard_normal
+
+
+def normals(n, bit_generator):
+ out = np.empty(n)
+ for i in range(n):
+ out[i] = random_standard_normal(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])