Skip to content

Commit

Permalink
Prepare Python bindings for release
Browse files Browse the repository at this point in the history
  • Loading branch information
daskol committed Oct 18, 2019
1 parent 0d005fd commit e992f5f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ JIT compiler infrastructure.
- [x] Vectorization with AVX.
- [x] JIT compilation with LLVM.
- [x] Thread-safe.
- [ ] Python bindings.
- [x] Python bindings.

## Benchmarking

Expand Down Expand Up @@ -93,10 +93,18 @@ benchmarking. Optional dependency is (f) LLVM which provides JIT compiler
facility.
```bash
mkdir -p build/release
cmake ../.. -DCMAKE_BUILD_TYPE=Release -DUSE_LLVM=ON
cmake ../.. -DCMAKE_BUILD_TYPE=Release -DBUILD_WHEEL=ON -DUSE_LLVM=ON
make
```
LLVM is not required by default, so it could be turned on/off with option
`-DUSE_LLVM=ON/OFF` (as it is shown on the snippet above).
`-DUSE_LLVM=ON/OFF` (as it is shown on the snippet above). Option `BUILD_WHEEL`
triggers building of Python bindings which could be find in directory
`fast-bernoulli/python/`. Target for building Python bindings is not included
to a set of default targets, so one should to run building of target
`fast-bernoulli-python-wheel` in advance.
```bash
make fast-bernoulli-python-wheel
```

## Usage

Expand Down Expand Up @@ -125,8 +133,25 @@ could bring samples to common representation (vector of bools).
sampler(rng, ptr);

// Expand compressed representation of random values to vector of bools.
auto values = Expand(ptr);
auto values = Expand(ptr, nobits);
```
The function `CreateSampler()` is overrided in order to provide an advanced way
to instantiate sampler with structure `TSamplerOpts`.
Also, the library delivers Python bindings and an extremely simple usage
interface. Module `fast_bernoulli` provides function `sample()` which generates
random bits and returns memory view on resulting buffer of type `ui8`.
```python
import numpy as np
import fast_bernoulli as fb
# Assume 65536 bits with probability 0.6 should be generated.
probability = 0.6;
nobits = 65536;
# Draw samples from Bernoulli distribution. JIT is used.
res = fb.sample(probability, nobits, seed=42, jit=True) # memoryview
arr = np.array(res) # np.ndarray with dtype=np.uint8
```
2 changes: 1 addition & 1 deletion fast-bernoulli/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ add_custom_command(
"egg_info"
"-e${CMAKE_CURRENT_BINARY_DIR}"
COMMENT "Built python extension and make wheel distribution"
DEPENDS "fast-bernoulli-cc" "_fast_bernoulli.pyx"
DEPENDS "fast-bernoulli-cc" "fast_bernoulli.pyx"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf8
# filename: _fast_bernoulli.pyx
"""Module _fast_bernoulli provides bindings to native implementation of
# filename: fast_bernoulli.pyx
"""Module fast_bernoulli provides bindings to native implementation of
Bernoulli sampler.
"""

Expand Down
4 changes: 2 additions & 2 deletions fast-bernoulli/python/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ from setuptools import find_packages, setup
from setuptools.extension import Extension

ext = Extension(
name='_fast_bernoulli',
sources=['_fast_bernoulli.pyx'],
name='fast_bernoulli',
sources=['fast_bernoulli.pyx'],
extra_compile_args=['-std=c++17'],
language='c++',
)
Expand Down

0 comments on commit e992f5f

Please sign in to comment.