Random Number Generators#
oneAPI DPC++ Library (oneDPL) offers support of random number generation, including:
Random number engines, which generate unsigned integer sequences of random numbers.
Random number distributions (example:
uniform_real_distribution
), which converts the output of random number engines into various statistical distributions.
Random Number Engines#
Random number engines use seed data as an entropy source to generate pseudo-random numbers.
oneDPL provides several class templates for customizable engines, defined in the header
<oneapi/dpl/random>
under the oneapi::dpl::
namespace.
Engine |
Description |
---|---|
|
Implements a linear congruential algorithm |
|
Implements a subtract-with-carry algorithm |
|
Implements a discard block adaptor |
|
Implements a Philox algorithm |
Predefined Random Number Engines#
Predefined random number engines are instantiations of the random number engines class templates with selected engine parameters.
The types below are defined in the header <oneapi/dpl/random>
in the same namespaces as their
respective class templates.
Type |
Description |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The following predefined engines can efficiently generate vectors of random numbers.
They differ from the scalar engines above by using sycl::vec<T, N>
as the data type,
while other engine parameters remain the same.
Type |
Description |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Random Number Distributions#
Random number distributions process the output of random number engines in such a way that the
resulting output is distributed according to a defined statistical probability density function. They
are defined in the header <oneapi/dpl/random>
under the oneapi::dpl::
namespace.
Distribution |
Description |
---|---|
|
Produces integer values evenly distributed across a range |
|
Produces real values evenly distributed across a range |
|
Produces real values according to the Normal (Gaussian) distribution |
|
Produces real values according to the Exponential distribution |
|
Produces bool values according to the Bernoulli distribution |
|
Produces integer values according to the Geometric distribution |
|
Produces real values according to the Weibull distribution |
|
Produces real values according to the Lognormal distribution |
|
Produces real values according to the Extreme value (Gumbel) distribution |
|
Produces real values according to the Cauchy distribution |
Note
bernoulli_distribution
, geometric_distribution
, and uniform_int_distribution
can only be used on devices with FP64 support as they rely on double precision in their implementation (use sycl::aspect::fp64
to check if the device supports FP64).
Usage Model of oneDPL Random Number Generation Functionality#
Random number generation is available for SYCL* device-side and host-side code. For example:
#include <oneapi/dpl/random>
#include <sycl/sycl.hpp>
#include <iostream>
#include <vector>
int main() {
sycl::queue queue(sycl::default_selector_v);
std::int64_t nsamples = 100;
std::uint32_t seed = 777;
std::vector<float> x(nsamples);
{
sycl::buffer<float, 1> x_buf(x.data(), sycl::range<1>(x.size()));
queue.submit([&] (sycl::handler &cgh) {
auto x_acc =
x_buf.template get_access<sycl::access::mode::write>(cgh);
cgh.parallel_for<class count_kernel>(sycl::range<1>(nsamples),
[=](sycl::item<1> idx) {
std::uint64_t offset = idx.get_linear_id();
// Create minstd_rand engine
oneapi::dpl::minstd_rand engine(seed, offset);
// Create float uniform_real_distribution distribution
oneapi::dpl::uniform_real_distribution<float> distr;
// Generate float random number
auto res = distr(engine);
// Store results to x_acc
x_acc[idx] = res;
});
});
}
std::cout << "\nFirst 5 samples of minstd_rand with scalar generation" << std::endl;
for(int i = 0; i < 5; i++) {
std::cout << x.begin()[i] << std::endl;
}
std::cout << "\nLast 5 samples of minstd_rand with scalar generation" << std::endl;
for(int i = 0; i < 5; i++) {
std::cout << x.rbegin()[i] << std::endl;
}
return 0;
}