Skip to content

Commit

Permalink
Merge 3de519b into ac58287
Browse files Browse the repository at this point in the history
  • Loading branch information
bashtage committed May 11, 2019
2 parents ac58287 + 3de519b commit 3f17332
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 110 deletions.
21 changes: 9 additions & 12 deletions randomgen/dsfmt.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,18 @@ cdef class DSFMT:
Sequences and Their Applications - SETA, 290--298, 2008.
"""
cdef dsfmt_state *rng_state
cdef brng_t *_brng
cdef brng_t _brng
cdef public object capsule
cdef public object _cffi
cdef public object _ctypes
cdef public object _generator
cdef object _cffi
cdef object _ctypes
cdef object _generator
cdef public object lock

def __init__(self, seed=None):
self.rng_state = <dsfmt_state *>malloc(sizeof(dsfmt_state))
self.rng_state.state = <dsfmt_t *>PyArray_malloc_aligned(sizeof(dsfmt_t))
self.rng_state.buffered_uniforms = <double *>PyArray_calloc_aligned(DSFMT_N64, sizeof(double))
self.rng_state.buffer_loc = DSFMT_N64
self._brng = <brng_t *>malloc(sizeof(brng_t))
self.seed(seed)
self.lock = Lock()

Expand All @@ -162,7 +161,7 @@ cdef class DSFMT:
self._brng.next_double = &dsfmt_double
self._brng.next_raw = &dsfmt_raw
cdef const char *name = "BasicRNG"
self.capsule = PyCapsule_New(<void *>self._brng, name, NULL)
self.capsule = PyCapsule_New(<void *>&self._brng, name, NULL)

self._cffi = None
self._ctypes = None
Expand All @@ -184,8 +183,6 @@ cdef class DSFMT:
PyArray_free_aligned(self.rng_state.state)
PyArray_free_aligned(self.rng_state.buffered_uniforms)
free(self.rng_state)
if self._brng:
free(self._brng)

cdef _reset_state_variables(self):
self.rng_state.buffer_loc = DSFMT_N64
Expand Down Expand Up @@ -219,10 +216,10 @@ cdef class DSFMT:
See the class docstring for the number of bits returned.
"""
return random_raw(self._brng, self.lock, size, output)
return random_raw(&self._brng, self.lock, size, output)

def _benchmark(self, Py_ssize_t cnt, method=u'uint64'):
return benchmark(self._brng, self.lock, cnt, method)
return benchmark(&self._brng, self.lock, cnt, method)

def seed(self, seed=None):
"""
Expand Down Expand Up @@ -363,7 +360,7 @@ cdef class DSFMT:
* brng - pointer to the Basic RNG struct
"""
if self._ctypes is None:
self._ctypes = prepare_ctypes(self._brng)
self._ctypes = prepare_ctypes(&self._brng)

return self._ctypes

Expand All @@ -386,7 +383,7 @@ cdef class DSFMT:
"""
if self._cffi is not None:
return self._cffi
self._cffi = prepare_cffi(self._brng)
self._cffi = prepare_cffi(&self._brng)
return self._cffi

@property
Expand Down
17 changes: 7 additions & 10 deletions randomgen/mt19937.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ cdef class MT19937:
"""
cdef mt19937_state *rng_state
cdef brng_t *_brng
cdef brng_t _brng
cdef public object capsule
cdef object _ctypes
cdef object _cffi
Expand All @@ -126,7 +126,6 @@ cdef class MT19937:

def __init__(self, seed=None):
self.rng_state = <mt19937_state *>malloc(sizeof(mt19937_state))
self._brng = <brng_t *>malloc(sizeof(brng_t))
self.seed(seed)
self.lock = Lock()

Expand All @@ -141,13 +140,11 @@ cdef class MT19937:
self._generator = None

cdef const char *name = "BasicRNG"
self.capsule = PyCapsule_New(<void *>self._brng, name, NULL)
self.capsule = PyCapsule_New(<void *>&self._brng, name, NULL)

def __dealloc__(self):
if self.rng_state:
free(self.rng_state)
if self._brng:
free(self._brng)

# Pickling support:
def __getstate__(self):
Expand Down Expand Up @@ -189,10 +186,10 @@ cdef class MT19937:
See the class docstring for the number of bits returned.
"""
return random_raw(self._brng, self.lock, size, output)
return random_raw(&self._brng, self.lock, size, output)

def _benchmark(self, Py_ssize_t cnt, method=u'uint64'):
return benchmark(self._brng, self.lock, cnt, method)
return benchmark(&self._brng, self.lock, cnt, method)

def seed(self, seed=None):
"""
Expand Down Expand Up @@ -319,7 +316,7 @@ cdef class MT19937:
* brng - pointer to the Basic RNG struct
"""
if self._ctypes is None:
self._ctypes = prepare_ctypes(self._brng)
self._ctypes = prepare_ctypes(&self._brng)

return self._ctypes

Expand All @@ -342,7 +339,7 @@ cdef class MT19937:
"""
if self._cffi is not None:
return self._cffi
self._cffi = prepare_cffi(self._brng)
self._cffi = prepare_cffi(&self._brng)
return self._cffi

@property
Expand All @@ -353,7 +350,7 @@ cdef class MT19937:
Returns
-------
gen : randomgen.generator.RandomGenerator
Random generator used this instance as the core PRNG
Random generator used by this instance as the core PRNG
"""
if self._generator is None:
from .generator import RandomGenerator
Expand Down
17 changes: 7 additions & 10 deletions randomgen/pcg32.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ cdef class PCG32:
Statistically Good Algorithms for Random Number Generation"
"""
cdef pcg32_state *rng_state
cdef brng_t *_brng
cdef brng_t _brng
cdef public object capsule
cdef object _ctypes
cdef object _cffi
Expand All @@ -132,7 +132,6 @@ cdef class PCG32:
def __init__(self, seed=None, inc=0):
self.rng_state = <pcg32_state *>malloc(sizeof(pcg32_state))
self.rng_state.pcg_state = <pcg32_random_t *>malloc(sizeof(pcg32_random_t))
self._brng = <brng_t *>malloc(sizeof(brng_t))
self.seed(seed, inc)
self.lock = Lock()

Expand All @@ -147,7 +146,7 @@ cdef class PCG32:
self._generator = None

cdef const char *name = "BasicRNG"
self.capsule = PyCapsule_New(<void *>self._brng, name, NULL)
self.capsule = PyCapsule_New(<void *>&self._brng, name, NULL)

# Pickling support:
def __getstate__(self):
Expand All @@ -163,8 +162,6 @@ cdef class PCG32:
def __dealloc__(self):
if self.rng_state:
free(self.rng_state)
if self._brng:
free(self._brng)

def random_raw(self, size=None, output=True):
"""
Expand Down Expand Up @@ -195,10 +192,10 @@ cdef class PCG32:
See the class docstring for the number of bits returned.
"""
return random_raw(self._brng, self.lock, size, output)
return random_raw(&self._brng, self.lock, size, output)

def _benchmark(self, Py_ssize_t cnt, method=u'uint64'):
return benchmark(self._brng, self.lock, cnt, method)
return benchmark(&self._brng, self.lock, cnt, method)

def seed(self, seed=None, inc=0):
"""
Expand Down Expand Up @@ -346,7 +343,7 @@ cdef class PCG32:
* brng - pointer to the Basic RNG struct
"""
if self._ctypes is None:
self._ctypes = prepare_ctypes(self._brng)
self._ctypes = prepare_ctypes(&self._brng)

return self._ctypes

Expand All @@ -369,7 +366,7 @@ cdef class PCG32:
"""
if self._cffi is not None:
return self._cffi
self._cffi = prepare_cffi(self._brng)
self._cffi = prepare_cffi(&self._brng)
return self._cffi

@property
Expand All @@ -380,7 +377,7 @@ cdef class PCG32:
Returns
-------
gen : randomgen.generator.RandomGenerator
Random generator used this instance as the core PRNG
Random generator used by this instance as the core PRNG
"""
if self._generator is None:
from .generator import RandomGenerator
Expand Down
15 changes: 6 additions & 9 deletions randomgen/pcg64.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ cdef class PCG64:
Statistically Good Algorithms for Random Number Generation"
"""
cdef pcg64_state *rng_state
cdef brng_t *_brng
cdef brng_t _brng
cdef public object capsule
cdef object _ctypes
cdef object _cffi
Expand All @@ -126,7 +126,6 @@ cdef class PCG64:
def __init__(self, seed=None, inc=0):
self.rng_state = <pcg64_state *>malloc(sizeof(pcg64_state))
self.rng_state.pcg_state = <pcg64_random_t *>malloc(sizeof(pcg64_random_t))
self._brng = <brng_t *>malloc(sizeof(brng_t))
self.seed(seed, inc)
self.lock = Lock()

Expand All @@ -141,7 +140,7 @@ cdef class PCG64:
self._generator = None

cdef const char *name = "BasicRNG"
self.capsule = PyCapsule_New(<void *>self._brng, name, NULL)
self.capsule = PyCapsule_New(<void *>&self._brng, name, NULL)

# Pickling support:
def __getstate__(self):
Expand All @@ -157,8 +156,6 @@ cdef class PCG64:
def __dealloc__(self):
if self.rng_state:
free(self.rng_state)
if self._brng:
free(self._brng)

cdef _reset_state_variables(self):
self.rng_state.has_uint32 = 0
Expand Down Expand Up @@ -193,10 +190,10 @@ cdef class PCG64:
See the class docstring for the number of bits returned.
"""
return random_raw(self._brng, self.lock, size, output)
return random_raw(&self._brng, self.lock, size, output)

def _benchmark(self, Py_ssize_t cnt, method=u'uint64'):
return benchmark(self._brng, self.lock, cnt, method)
return benchmark(&self._brng, self.lock, cnt, method)

def seed(self, seed=None, inc=0):
"""
Expand Down Expand Up @@ -382,7 +379,7 @@ cdef class PCG64:
* brng - pointer to the Basic RNG struct
"""
if self._ctypes is None:
self._ctypes = prepare_ctypes(self._brng)
self._ctypes = prepare_ctypes(&self._brng)

return self._ctypes

Expand All @@ -405,7 +402,7 @@ cdef class PCG64:
"""
if self._cffi is not None:
return self._cffi
self._cffi = prepare_cffi(self._brng)
self._cffi = prepare_cffi(&self._brng)
return self._cffi

@property
Expand Down
19 changes: 8 additions & 11 deletions randomgen/philox.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ cdef class Philox:
Networking, Storage and Analysis (SC11), New York, NY: ACM, 2011.
"""
cdef philox_state *rng_state
cdef brng_t *_brng
cdef brng_t _brng
cdef public object capsule
cdef object _ctypes
cdef object _cffi
Expand All @@ -170,11 +170,10 @@ cdef class Philox:
sizeof(philox4x64_ctr_t))
self.rng_state.key = <philox4x64_key_t *> malloc(
sizeof(philox4x64_key_t))
self._brng = <brng_t *> malloc(sizeof(brng_t))
self.seed(seed, counter, key)
self.lock = Lock()

self._brng.state = <void *> self.rng_state
self._brng.state = <void *>self.rng_state
self._brng.next_uint64 = &philox_uint64
self._brng.next_uint32 = &philox_uint32
self._brng.next_double = &philox_double
Expand All @@ -185,7 +184,7 @@ cdef class Philox:
self._generator = None

cdef const char *name = 'BasicRNG'
self.capsule = PyCapsule_New(<void *> self._brng, name, NULL)
self.capsule = PyCapsule_New(<void *>&self._brng, name, NULL)

# Pickling support:
def __getstate__(self):
Expand All @@ -203,8 +202,6 @@ cdef class Philox:
free(self.rng_state.ctr)
free(self.rng_state.key)
free(self.rng_state)
if self._brng:
free(self._brng)

cdef _reset_state_variables(self):
self.rng_state.has_uint32 = 0
Expand Down Expand Up @@ -242,10 +239,10 @@ cdef class Philox:
See the class docstring for the number of bits returned.
"""
return random_raw(self._brng, self.lock, size, output)
return random_raw(&self._brng, self.lock, size, output)

def _benchmark(self, Py_ssize_t cnt, method=u'uint64'):
return benchmark(self._brng, self.lock, cnt, method)
return benchmark(&self._brng, self.lock, cnt, method)

def seed(self, seed=None, counter=None, key=None):
"""
Expand Down Expand Up @@ -434,7 +431,7 @@ cdef class Philox:
* brng - pointer to the Basic RNG struct
"""
if self._ctypes is None:
self._ctypes = prepare_ctypes(self._brng)
self._ctypes = prepare_ctypes(&self._brng)

return self._ctypes

Expand All @@ -457,7 +454,7 @@ cdef class Philox:
"""
if self._cffi is not None:
return self._cffi
self._cffi = prepare_cffi(self._brng)
self._cffi = prepare_cffi(&self._brng)
return self._cffi

@property
Expand All @@ -468,7 +465,7 @@ cdef class Philox:
Returns
-------
gen : randomgen.generator.RandomGenerator
Random generator used this instance as the core PRNG
Random generator used by this instance as the core PRNG
"""
if self._generator is None:
from .generator import RandomGenerator
Expand Down

0 comments on commit 3f17332

Please sign in to comment.