Skip to content

MAINT: align random double_fill to float_fill, simplify distributions.c #14611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion numpy/random/common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ cdef extern from "src/aligned_malloc/aligned_malloc.h":
cdef void *PyArray_calloc_aligned(size_t n, size_t s)
cdef void PyArray_free_aligned(void *p)

ctypedef double (*random_double_fill)(bitgen_t *state, np.npy_intp count, double* out) nogil
ctypedef double (*random_double_0)(void *state) nogil
ctypedef double (*random_double_1)(void *state, double a) nogil
ctypedef double (*random_double_2)(void *state, double a, double b) nogil
Expand Down
8 changes: 4 additions & 4 deletions numpy/random/common.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,15 @@ cdef check_output(object out, object dtype, object size):


cdef object double_fill(void *func, bitgen_t *state, object size, object lock, object out):
cdef random_double_fill random_func = (<random_double_fill>func)
cdef random_double_0 random_func = (<random_double_0>func)
cdef double out_val
cdef double *out_array_data
cdef np.ndarray out_array
cdef np.npy_intp i, n

if size is None and out is None:
with lock:
random_func(state, 1, &out_val)
return out_val
return random_func(state)

if out is not None:
check_output(out, np.float64, size)
Expand All @@ -258,7 +257,8 @@ cdef object double_fill(void *func, bitgen_t *state, object size, object lock, o
n = np.PyArray_SIZE(out_array)
out_array_data = <double *>np.PyArray_DATA(out_array)
with lock, nogil:
random_func(state, n, out_array_data)
for i in range(n):
out_array_data[i] = random_func(state)
return out_array

cdef object float_fill(void *func, bitgen_t *state, object size, object lock, object out):
Expand Down
4 changes: 0 additions & 4 deletions numpy/random/distributions.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ cdef extern from "src/distributions/distributions.h":
ctypedef s_binomial_t binomial_t

double random_double(bitgen_t *bitgen_state) nogil
void random_double_fill(bitgen_t* bitgen_state, np.npy_intp cnt, double *out) nogil
double random_standard_exponential(bitgen_t *bitgen_state) nogil
void random_standard_exponential_fill(bitgen_t *bitgen_state, np.npy_intp cnt, double *out) nogil
double random_standard_exponential_zig(bitgen_t *bitgen_state) nogil
void random_standard_exponential_zig_fill(bitgen_t *bitgen_state, np.npy_intp cnt, double *out) nogil
double random_gauss_zig(bitgen_t* bitgen_state) nogil
void random_gauss_zig_fill(bitgen_t *bitgen_state, np.npy_intp count, double *out) nogil
double random_standard_gamma_zig(bitgen_t *bitgen_state, double shape) nogil

float random_float(bitgen_t *bitgen_state) nogil
Expand Down
8 changes: 4 additions & 4 deletions numpy/random/generator.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ cdef class Generator:
cdef double temp
key = np.dtype(dtype).name
if key == 'float64':
return double_fill(&random_double_fill, &self._bitgen, size, self.lock, out)
return double_fill(&random_double, &self._bitgen, size, self.lock, out)
elif key == 'float32':
return float_fill(&random_float, &self._bitgen, size, self.lock, out)
else:
Expand Down Expand Up @@ -336,9 +336,9 @@ cdef class Generator:
key = np.dtype(dtype).name
if key == 'float64':
if method == u'zig':
return double_fill(&random_standard_exponential_zig_fill, &self._bitgen, size, self.lock, out)
return double_fill(&random_standard_exponential_zig, &self._bitgen, size, self.lock, out)
else:
return double_fill(&random_standard_exponential_fill, &self._bitgen, size, self.lock, out)
return double_fill(&random_standard_exponential, &self._bitgen, size, self.lock, out)
elif key == 'float32':
if method == u'zig':
return float_fill(&random_standard_exponential_zig_f, &self._bitgen, size, self.lock, out)
Expand Down Expand Up @@ -920,7 +920,7 @@ cdef class Generator:
"""
key = np.dtype(dtype).name
if key == 'float64':
return double_fill(&random_gauss_zig_fill, &self._bitgen, size, self.lock, out)
return double_fill(&random_gauss_zig, &self._bitgen, size, self.lock, out)
Copy link
Contributor

@bashtage bashtage Sep 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can go further if the fill-methods are being removed and you can remove double_fill and just use

cont(&random_gauss_zig, &self._bitgen, size, self.lock, 0,
     0.0, '', CONS_NONE, None,0.0, '', CONS_NONE, None, 0.0, '', CONS_NONE, None
     out)

elif key == 'float32':
return float_fill(&random_gauss_zig_f, &self._bitgen, size, self.lock, out)

Expand Down
2 changes: 1 addition & 1 deletion numpy/random/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ cdef class RandomState:

"""
cdef double temp
return double_fill(&random_double_fill, &self._bitgen, size, self.lock, None)
return double_fill(&random_double, &self._bitgen, size, self.lock, None)

def random(self, size=None):
"""
Expand Down
29 changes: 0 additions & 29 deletions numpy/random/src/distributions/distributions.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,10 @@ double random_standard_exponential(bitgen_t *bitgen_state) {
return next_standard_exponential(bitgen_state);
}

void random_standard_exponential_fill(bitgen_t *bitgen_state, npy_intp cnt,
double *out) {
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = next_standard_exponential(bitgen_state);
}
}

float random_standard_exponential_f(bitgen_t *bitgen_state) {
return -logf(1.0f - next_float(bitgen_state));
}

void random_double_fill(bitgen_t *bitgen_state, npy_intp cnt, double *out) {
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = next_double(bitgen_state);
}
}
#if 0
double random_gauss(bitgen_t *bitgen_state) {
if (bitgen_state->has_gauss) {
Expand Down Expand Up @@ -124,14 +110,6 @@ double random_standard_exponential_zig(bitgen_t *bitgen_state) {
return standard_exponential_zig(bitgen_state);
}

void random_standard_exponential_zig_fill(bitgen_t *bitgen_state, npy_intp cnt,
double *out) {
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = standard_exponential_zig(bitgen_state);
}
}

static NPY_INLINE float standard_exponential_zig_f(bitgen_t *bitgen_state);

static float standard_exponential_zig_unlikely_f(bitgen_t *bitgen_state,
Expand Down Expand Up @@ -206,13 +184,6 @@ double random_gauss_zig(bitgen_t *bitgen_state) {
return next_gauss_zig(bitgen_state);
}

void random_gauss_zig_fill(bitgen_t *bitgen_state, npy_intp cnt, double *out) {
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = next_gauss_zig(bitgen_state);
}
}

float random_gauss_zig_f(bitgen_t *bitgen_state) {
uint32_t r;
int sign;
Expand Down
7 changes: 0 additions & 7 deletions numpy/random/src/distributions/distributions.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,15 @@ DECLDIR double loggam(double x);

DECLDIR float random_float(bitgen_t *bitgen_state);
DECLDIR double random_double(bitgen_t *bitgen_state);
DECLDIR void random_double_fill(bitgen_t *bitgen_state, npy_intp cnt, double *out);

DECLDIR int64_t random_positive_int64(bitgen_t *bitgen_state);
DECLDIR int32_t random_positive_int32(bitgen_t *bitgen_state);
DECLDIR int64_t random_positive_int(bitgen_t *bitgen_state);
DECLDIR uint64_t random_uint(bitgen_t *bitgen_state);

DECLDIR double random_standard_exponential(bitgen_t *bitgen_state);
DECLDIR void random_standard_exponential_fill(bitgen_t *bitgen_state, npy_intp cnt,
double *out);
DECLDIR float random_standard_exponential_f(bitgen_t *bitgen_state);
DECLDIR double random_standard_exponential_zig(bitgen_t *bitgen_state);
DECLDIR void random_standard_exponential_zig_fill(bitgen_t *bitgen_state,
npy_intp cnt, double *out);
DECLDIR float random_standard_exponential_zig_f(bitgen_t *bitgen_state);

/*
Expand All @@ -102,8 +97,6 @@ DECLDIR float random_gauss_f(bitgen_t *bitgen_state);
*/
DECLDIR double random_gauss_zig(bitgen_t *bitgen_state);
DECLDIR float random_gauss_zig_f(bitgen_t *bitgen_state);
DECLDIR void random_gauss_zig_fill(bitgen_t *bitgen_state, npy_intp cnt,
double *out);

/*
DECLDIR double random_standard_gamma(bitgen_t *bitgen_state, double shape);
Expand Down