oneMKL RNG Usage Model

A typical algorithm for random number generators is as follows:

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

    • Use the skip_ahead or leapfrog function if it is required (used in parallel with random number generation for Host and CPU devices).

  2. Create and initialize the object for distribution generator.

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

The following example demonstrates generation of random numbers that is output from basic generator (engine) PHILOX4X32X10. The seed is equal to 777. The generator is used to generate 10,000 normally distributed random numbers with parameters a = 5 and sigma= 2. The purpose of the example is to calculate the sample mean for normal distribution with the given parameters.

Example of RNG Usage

Buffer API

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>


#include “CL/sycl.hpp”
#include “oneapi/mkl/rng.hpp”
#define SEED 777


int main() {
    sycl::queue queue;


    const size_t n = 10000;
    std::vector<double> r(n);


    // create basic random number generator object
    oneapi::mkl::rng::philox4x32x10 engine(queue, SEED);
    // create distribution object
    oneapi::mkl::rng::gaussian<double, oneapi::mkl::rng::gaussian_method::icdf> distr(5.0, 2.0);


    {
        // buffer for random numbers
        sycl::buffer<double, 1> r_buf(r.data(), r.size());
        // perform generation
        oneapi::mkl::rng::generate(distr, engine, n, r_buf);


    }


    double s = 0.0;
    for(int i = 0; i < n; i++) {
       s += r[i];
    }
    s /= n;


    std::cout << “Average = ” << s << std::endl;


    return 0;
}

USM API

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include “CL/sycl.hpp”
#include “oneapi/mkl/rng.hpp”
#define SEED 777


int main() {
  sycl::queue queue;


  const size_t n = 10000;


  // create USM allocator
  sycl::usm_allocator<double, sycl::usm::alloc::shared> allocator(queue);


  // create vector with USM allocator
  std::vector<double, decltype(allocator)> r(n, allocator);


  // create basic random number generator object
  oneapi::mkl::rng::philox4x32x10 engine(queue, SEED);
  // create distribution object
  oneapi::mkl::rng::gaussian<double, oneapi::mkl::rng::gaussian_method::icdf> distr(5.0, 2.0);


  // perform generation
  auto event = oneapi::mkl::rng::generate(distr, engine, n, r.data());


  // sycl::event object is returned by generate function for synchronization
  event.wait();  // synchronization can be also done by queue.wait()


  double s = 0.0;
  for(int i = 0; i < n; i++) {
      s += r[i];
  }
  s /= n;


  std::cout << “Average = ” << s << std::endl;


  return 0;
  }

You can also use USM with raw pointers by using the sycl::malloc_shared / sycl::malloc_device function.

Additionally, examples that demonstrate usage of random number generators functionality are available in:

${MKL}/examples/dpcpp/rng/source