oneMath RNG Device Usage Model#

A typical usage model for device routines is the same as described in oneMath RNG Host Usage Model:

  1. Create and initialize the object for basic random number generator.

  2. Create and initialize the object for distribution generator.

  3. Call the generate routine to get random numbers with appropriate statistical distribution.

Example of Scalar Random Numbers Generation#

#include "oneapi/math/rng/device.hpp"

int main() {
    sycl::queue q;
    // Prepare a memory for random numbers
    // Submit a kernel to generate on device
    q.submit([&](sycl::handler& cgh) {
        // ...
        cgh.parallel_for(n, [=](size_t idx) {
            // Create an engine object
            oneapi::math::rng::device::philox4x32x10<> engine(seed, idx);
            // Create a distribution object
            oneapi::math::rng::device::uniform<float> distr;
            // Call generate function to obtain scalar random number
            float res = oneapi::math::rng::device::generate(distr, engine);
            // ...
        });
    });
    // ...
}

Example of Vector Random Numbers Generation#

#include "oneapi/math/rng/device.hpp"

int main() {
    sycl::queue q;
    // Prepare an array for random numbers
    // Submit a kernel to generate on device
    q.submit([&](sycl::handler& cgh) {
        // ...
        cgh.parallel_for((n / vec_size), [=](size_t idx) {
            // Create an engine object
            oneapi::math::rng::device::philox4x32x10<vec_size> engine(seed, idx * vec_size);
            // Create a distribution object
            oneapi::math::rng::device::uniform<float> distr;
            // Call generate function to obtain random numbers
            sycl::vec<float, vec_size> res = oneapi::math::rng::device::generate(distr, engine);
            // ...
        });
    });
    // ...
}

Parent topic: Random Number Generators Device Routines