uniform_bits#

Generates uniformly distributed bits in 32/64-bit chunks.

Description#

The uniform_bits class object is used to generate uniformly distributed bits in 32/64-bit chunks. It is designed to ensure each bit in the 32/64-bit chunk is uniformly distributed. This distribution is supported for philox4x32x10 and mcg59 engines. When generating 64-bit chunks, twice as much engine offset needs to be provided.

UIntType denotes the chunk size and can be std::uint32_t, std::uint64_t. See VS Notes for details.

API#

Syntax#

namespace oneapi::mkl::rng::device {
  template<typename UIntType = std::uint32_t>
  class uniform_bits {
    using result_type = UIntType;
  };
}

Include Files#

  • oneapi/mkl/rng/device.hpp

Template Parameters#

typename UIntType = std::uint32_t

Type of the produced values. The specific values are as follows:

std::uint32_t std::uint64_t

Examples Of Generating Small Types#

Example 1#

uniform_bits returns uniformly distributed generator output, so to get small types such as std::int8_t, std::uint8_t, std::int16_t, and std::uint16_t, convert the generated numbers to the desired type as shown below:

queue.submit([&](sycl::handler& cgh) {
  cgh.parallel_for(sycl::range<1>(n / VecSize), [=](sycl::item<1> item) {
      size_t item_id = item.get_id(0);
      ...
      oneapi::mkl::rng::device::uniform_bits<Type> distr;

      auto res = oneapi::mkl::rng::device::generate(distr, engine);
      if constexpr(VecSize == 1) {
          r_acc[item_id] = static_cast<DesiredType>(res);
      }
      else {
          sycl::vec<DesiredType, VecSize> res_;
          res_ = res.template convert<DesiredType>();
          res_.store(item_id, r_acc);
      }
  });
});

Example 2#

You also can get several numbers of small type from one generated number. The following example demonstrates how to obtain four std::uint8_t numbers from one std::uint32_t:

oneapi::mkl::rng::device::uniform_bits<std::uint32_t> distr;

std::uint32_t res = oneapi::mkl::rng::device::generate(distr, engine);
sycl::vec<std::uint8_t, 4> res_small;
for (int i = 0; i < 4; ++i) {
    res_small[i] = static_cast<std::uint8_t>(res & 0xff);
    res >>= 8;
}