-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathrandom.rs
More file actions
293 lines (265 loc) · 9.87 KB
/
random.rs
File metadata and controls
293 lines (265 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Safe interface for NumPy's random [`BitGenerator`][bg].
//!
//! Using the patterns described in [“Extending `numpy.random`”][ext],
//! you can generate random numbers without holding the GIL,
//! by [acquiring][`PyBitGeneratorMethods::lock`] a lock [guard][`PyBitGeneratorGuard`] for the [`PyBitGenerator`]:
//!
//! ```
//! use pyo3::prelude::*;
//! use numpy::random::{PyBitGenerator, PyBitGeneratorMethods as _};
//!
//! fn default_bit_gen<'py>(py: Python<'py>) -> PyResult<Bound<'py, PyBitGenerator>> {
//! let default_rng = py.import("numpy.random")?.call_method0("default_rng")?;
//! let bit_generator = default_rng.getattr("bit_generator")?.downcast_into()?;
//! Ok(bit_generator)
//! }
//!
//! let random_number = Python::with_gil(|py| -> PyResult<_> {
//! let mut bitgen = default_bit_gen(py)?.lock()?;
//! // use bitgen without holding the GIL
//! Ok(py.allow_threads(|| bitgen.next_uint64()))
//! })?;
//! # Ok::<(), PyErr>(())
//! ```
//!
//! With the [`rand`] crate installed, you can also use the [`rand::Rng`] APIs from the [`PyBitGeneratorGuard`]:
//!
//! ```
//! # use pyo3::prelude::*;
//! use rand::Rng as _;
//! # use numpy::random::{PyBitGenerator, PyBitGeneratorMethods as _};
//! # // TODO: reuse function definition from above?
//! # fn default_bit_gen<'py>(py: Python<'py>) -> PyResult<Bound<'py, PyBitGenerator>> {
//! # let default_rng = py.import("numpy.random")?.call_method0("default_rng")?;
//! # let bit_generator = default_rng.getattr("bit_generator")?.downcast_into()?;
//! # Ok(bit_generator)
//! # }
//!
//! Python::with_gil(|py| -> PyResult<_> {
//! let mut bitgen = default_bit_gen(py)?.lock()?;
//! if bitgen.random_ratio(1, 1_000_000) {
//! println!("a sure thing");
//! }
//! Ok(())
//! })?;
//! # Ok::<(), PyErr>(())
//! ```
//!
//! [bg]: https://numpy.org/doc/stable//reference/random/bit_generators/generated/numpy.random.BitGenerator.html
//! [ext]: https://numpy.org/doc/stable/reference/random/extending.html
use std::ptr::NonNull;
use pyo3::{
exceptions::PyRuntimeError,
ffi,
prelude::*,
sync::GILOnceCell,
types::{DerefToPyAny, PyCapsule, PyType},
PyTypeInfo,
};
use crate::npyffi::npy_bitgen;
/// Wrapper for [`np.random.BitGenerator`][bg].
///
/// See also [`PyBitGeneratorMethods`].
///
/// [bg]: https://numpy.org/doc/stable//reference/random/bit_generators/generated/numpy.random.BitGenerator.html
#[repr(transparent)]
pub struct PyBitGenerator(PyAny);
impl DerefToPyAny for PyBitGenerator {}
unsafe impl PyTypeInfo for PyBitGenerator {
const NAME: &'static str = "PyBitGenerator";
const MODULE: Option<&'static str> = Some("numpy.random");
fn type_object_raw<'py>(py: Python<'py>) -> *mut ffi::PyTypeObject {
static CLS: GILOnceCell<Py<PyType>> = GILOnceCell::new();
let cls = CLS
.get_or_try_init::<_, PyErr>(py, || {
Ok(py
.import("numpy.random")?
.getattr("BitGenerator")?
.downcast_into::<PyType>()?
.unbind())
})
.expect("Failed to get BitGenerator type object")
.clone_ref(py)
.into_bound(py);
cls.as_type_ptr()
}
}
/// Methods for [`PyBitGenerator`].
pub trait PyBitGeneratorMethods {
/// Acquire a lock on the BitGenerator to allow calling its methods in.
fn lock(&self) -> PyResult<PyBitGeneratorGuard>;
}
impl<'py> PyBitGeneratorMethods for Bound<'py, PyBitGenerator> {
fn lock(&self) -> PyResult<PyBitGeneratorGuard> {
let capsule = self.getattr("capsule")?.downcast_into::<PyCapsule>()?;
let lock = self.getattr("lock")?;
// we’re holding the GIL, so there’s no race condition checking the lock and acquiring it later.
if lock.call_method0("locked")?.extract()? {
return Err(PyRuntimeError::new_err("BitGenerator is already locked"));
}
lock.call_method0("acquire")?;
assert_eq!(capsule.name()?, Some(c"BitGenerator"));
let ptr = capsule.pointer() as *mut npy_bitgen;
let Some(non_null) = NonNull::new(ptr) else {
lock.call_method0("release")?;
return Err(PyRuntimeError::new_err("Invalid BitGenerator capsule"));
};
Ok(PyBitGeneratorGuard {
raw_bitgen: non_null,
_capsule: capsule.unbind(),
lock: lock.unbind(),
})
}
}
impl<'py> TryFrom<&Bound<'py, PyBitGenerator>> for PyBitGeneratorGuard {
type Error = PyErr;
fn try_from(value: &Bound<'py, PyBitGenerator>) -> Result<Self, Self::Error> {
value.lock()
}
}
/// [`PyBitGenerator`] lock allowing to access its methods without holding the GIL.
pub struct PyBitGeneratorGuard {
raw_bitgen: NonNull<npy_bitgen>,
/// This field makes sure the `raw_bitgen` inside the capsule doesn’t get deallocated.
_capsule: Py<PyCapsule>,
/// This lock makes sure no other threads try to use the BitGenerator while we do.
lock: Py<PyAny>,
}
// SAFETY: 1. We don’t hold the GIL, so we can’t access the Python objects.
// 2. We only access `raw_bitgen` from `&mut self`, which protects it from parallel access.
unsafe impl Send for PyBitGeneratorGuard {}
impl Drop for PyBitGeneratorGuard {
fn drop(&mut self) {
// ignore errors. This includes when `try_release` was called manually.
let _ = Python::with_gil(|py| -> PyResult<_> {
self.lock.bind(py).call_method0("release")?;
Ok(())
});
}
}
// SAFETY: 1. We hold the `BitGenerator.lock`, so nothing apart from us is allowed to change its state.
// 2. We hold the `BitGenerator.capsule`, so it can’t be deallocated.
impl<'py> PyBitGeneratorGuard {
/// Release the lock, allowing for checking for errors.
pub fn release(self, py: Python<'py>) -> PyResult<()> {
self.lock.bind(py).call_method0("release")?;
Ok(())
}
/// Returns the next random unsigned 64 bit integer.
pub fn next_u64(&mut self) -> u64 {
unsafe {
let bitgen = self.raw_bitgen.as_ptr();
((*bitgen).next_uint64)((*bitgen).state)
}
}
/// Returns the next random unsigned 32 bit integer.
pub fn next_u32(&mut self) -> u32 {
unsafe {
let bitgen = self.raw_bitgen.as_ptr();
((*bitgen).next_uint32)((*bitgen).state)
}
}
/// Returns the next random double.
pub fn next_double(&mut self) -> libc::c_double {
unsafe {
let bitgen = self.raw_bitgen.as_ptr();
((*bitgen).next_double)((*bitgen).state)
}
}
/// Returns the next raw value (can be used for testing).
pub fn next_raw(&mut self) -> u64 {
unsafe {
let bitgen = self.raw_bitgen.as_ptr();
((*bitgen).next_raw)((*bitgen).state)
}
}
}
#[cfg(feature = "rand")]
impl rand::RngCore for PyBitGeneratorGuard {
fn next_u32(&mut self) -> u32 {
PyBitGeneratorGuard::next_u32(self)
}
fn next_u64(&mut self) -> u64 {
PyBitGeneratorGuard::next_u64(self)
}
fn fill_bytes(&mut self, dst: &mut [u8]) {
rand::rand_core::impls::fill_bytes_via_next(self, dst)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn get_bit_generator<'py>(py: Python<'py>) -> PyResult<Bound<'py, PyBitGenerator>> {
let default_rng = py.import("numpy.random")?.call_method0("default_rng")?;
let bit_generator = default_rng
.getattr("bit_generator")?
.downcast_into::<PyBitGenerator>()?;
Ok(bit_generator)
}
/// Test the primary use case: acquire the lock, release the GIL, then use the lock
#[test]
fn use_outside_gil() -> PyResult<()> {
Python::with_gil(|py| {
let mut bitgen = get_bit_generator(py)?.lock()?;
py.allow_threads(|| {
let _ = bitgen.next_raw();
});
assert!(bitgen.release(py).is_ok());
Ok(())
})
}
/// More complex version of primary use case: use from multiple threads
#[cfg(feature = "rand")]
#[test]
fn use_parallel() -> PyResult<()> {
use crate::array::{PyArray2, PyArrayMethods as _};
use ndarray::Dimension;
use rand::Rng;
use std::sync::{Arc, Mutex};
Python::with_gil(|py| -> PyResult<_> {
let mut arr = PyArray2::<u32>::zeros(py, (2, 300), false).readwrite();
let bitgen = get_bit_generator(py)?.lock()?;
let bitgen = Arc::new(Mutex::new(bitgen));
let (_n_threads, chunk_size) = arr.dims().into_pattern();
let slice = arr.as_slice_mut()?;
py.allow_threads(|| {
std::thread::scope(|s| {
for chunk in slice.chunks_exact_mut(chunk_size) {
let bitgen = Arc::clone(&bitgen);
s.spawn(move || {
let mut bitgen = bitgen.lock().unwrap();
chunk.fill_with(|| bitgen.random_range(10..200));
});
}
})
});
std::mem::drop(bitgen);
Ok(())
})
}
/// Test that the `rand::Rng` APIs work
#[cfg(feature = "rand")]
#[test]
fn rand() -> PyResult<()> {
use rand::Rng as _;
Python::with_gil(|py| {
let mut bitgen = get_bit_generator(py)?.lock()?;
py.allow_threads(|| {
assert!(bitgen.random_ratio(1, 1));
assert!(!bitgen.random_ratio(0, 1));
});
assert!(bitgen.release(py).is_ok());
Ok(())
})
}
#[test]
fn double_lock_fails() -> PyResult<()> {
Python::with_gil(|py| {
let generator = get_bit_generator(py)?;
let bitgen = generator.lock()?;
assert!(generator.lock().is_err());
assert!(bitgen.release(py).is_ok());
Ok(())
})
}
}