Skip to content
Merged
1 change: 1 addition & 0 deletions dpnp/backend.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ cdef extern from "backend/backend_iface_fptr.hpp" namespace "DPNPFuncName": # n
DPNP_FN_FLOOR
DPNP_FN_FLOOR_DIVIDE
DPNP_FN_FMOD
DPNP_FN_GAMMA
DPNP_FN_GAUSSIAN
DPNP_FN_HYPOT
DPNP_FN_INVERT
Expand Down
1 change: 1 addition & 0 deletions dpnp/backend/backend_iface_fptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ enum class DPNPFuncName : size_t
DPNP_FN_FLOOR, /**< Used in numpy.floor() implementation */
DPNP_FN_FLOOR_DIVIDE, /**< Used in numpy.floor_divide() implementation */
DPNP_FN_FMOD, /**< Used in numpy.fmod() implementation */
DPNP_FN_GAMMA, /**< Used in numpy.random.gamma() implementation */
DPNP_FN_GAUSSIAN, /**< Used in numpy.random.randn() implementation */
DPNP_FN_HYPOT, /**< Used in numpy.hypot() implementation */
DPNP_FN_INVERT, /**< Used in numpy.invert() implementation */
Expand Down
22 changes: 22 additions & 0 deletions dpnp/backend/custom_kernels_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ void custom_rng_exponential_c(void* result, _DataType beta, size_t size)
event_out.wait();
}

template <typename _DataType>
void custom_rng_gamma_c(void* result, _DataType shape, _DataType scale, size_t size)
{
if (!size)
{
return;
}

// set displacement a
const _DataType a = (_DataType(0.0));

_DataType* result1 = reinterpret_cast<_DataType*>(result);

mkl_rng::gamma<_DataType> distribution(shape, a, scale);
// perform generation
auto event_out = mkl_rng::generate(distribution, DPNP_RNG_ENGINE, size, result1);
event_out.wait();
}

template <typename _DataType>
void custom_rng_gaussian_c(void* result, _DataType mean, _DataType stddev, size_t size)
{
Expand Down Expand Up @@ -107,6 +126,9 @@ void func_map_init_random(func_map_t& fmap)
fmap[DPNPFuncName::DPNP_FN_EXPONENTIAL][eft_DBL][eft_DBL] = {eft_DBL, (void*)custom_rng_exponential_c<double>};
fmap[DPNPFuncName::DPNP_FN_EXPONENTIAL][eft_FLT][eft_FLT] = {eft_FLT, (void*)custom_rng_exponential_c<float>};

fmap[DPNPFuncName::DPNP_FN_GAMMA][eft_DBL][eft_DBL] = {eft_DBL, (void*)custom_rng_gamma_c<double>};
fmap[DPNPFuncName::DPNP_FN_GAMMA][eft_FLT][eft_FLT] = {eft_FLT, (void*)custom_rng_gamma_c<float>};

fmap[DPNPFuncName::DPNP_FN_GAUSSIAN][eft_DBL][eft_DBL] = {eft_DBL, (void*)custom_rng_gaussian_c<double>};
fmap[DPNPFuncName::DPNP_FN_GAUSSIAN][eft_FLT][eft_FLT] = {eft_FLT, (void*)custom_rng_gaussian_c<float>};

Expand Down
38 changes: 38 additions & 0 deletions dpnp/random/_random.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ cimport numpy
__all__ = [
"dpnp_chisquare",
"dpnp_exponential",
"dpnp_gamma",
"dpnp_randn",
"dpnp_random",
"dpnp_srand",
Expand All @@ -54,6 +55,7 @@ __all__ = [

ctypedef void(*fptr_custom_rng_chi_square_c_1out_t)(void *, int, size_t)
ctypedef void(*fptr_custom_rng_exponential_c_1out_t)(void *, double, size_t)
ctypedef void(*fptr_custom_rng_gamma_c_1out_t)(void *, double, double, size_t)
ctypedef void(*fptr_custom_rng_gaussian_c_1out_t)(void *, double, double, size_t)
ctypedef void(*fptr_custom_rng_uniform_c_1out_t)(void *, long, long, size_t)

Expand Down Expand Up @@ -111,6 +113,42 @@ cpdef dparray dpnp_exponential(double beta, size):
return result


cpdef dparray dpnp_gamma(double shape, double scale, size):
"""
Returns an array populated with samples from gamma distribution.

`dpnp_gamma` generates a matrix filled with random floats sampled from a
univariate gamma distribution of `shape` and `scale`.

"""

dtype = numpy.float64
cdef dparray result
cdef DPNPFuncType param1_type
cdef DPNPFuncData kernel_data
cdef fptr_custom_rng_gamma_c_1out_t func

if shape == 0.0 or scale==0.0:
result = dparray(size, dtype=dtype)
result.fill(0.0)
else:
# convert string type names (dparray.dtype) to C enum DPNPFuncType
param1_type = dpnp_dtype_to_DPNPFuncType(dtype)

# get the FPTR data structure
kernel_data = get_dpnp_function_ptr(DPNP_FN_GAMMA, param1_type, param1_type)

result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
# ceate result array with type given by FPTR data
result = dparray(size, dtype=result_type)

func = <fptr_custom_rng_gamma_c_1out_t > kernel_data.ptr
# call FPTR function
func(result.get_data(), shape, scale, result.size)

return result


cpdef dparray dpnp_randn(dims):
"""
Returns an array populated with samples from standard normal distribution.
Expand Down
Loading