get_state_size#

Returns the size in bytes which is needed to store the state of a given random number engine.

Description#

The get_state_size function allows you to know the amount of memory needed for storing the engine’s state in memory.

API#

Syntax#

namespace oneapi::mkl::rng {
  template<typename Engine>
  std::int64_t get_state_size (Engine& engine);
}

Include Files#

  • oneapi/mkl/rng.hpp

Input Parameters#

Name

Type

Description

engine

Engine&

Random number engine object, which state size would be calculated.

Output Parameters#

Type

Description

std::int64_t

Size of the given engine’s state in bytes.

The following code illustrates how to use the get_state_size function to store the engine’s state to memory:

Code for Save/Load state functionality#

 1   // Creating GPU engine
 2   oneapi::mkl::rng::default_engine engine(gpu_queue);
 3
 4   // Checking how much memory is required to save
 5   auto mem_size = oneapi::mkl::rng::get_state_size(engine);
 6
 7   // Allocating memory buffer
 8   std::uint8_t* mem_buf = new std::uint8_t[mem_size];
 9
10   // Saving state of engine in the file
11   oneapi::mkl::rng::save_state(engine, mem_buf);
12
13   // Generating random numbers from the GPU engine
14   oneapi::mkl::rng::generate(oneapi::mkl::rng::uniform{}, engine, n, r1);
15
16   // Loading state for the CPU queue
17   auto engine_2 = oneapi::mkl::rng::load_state<oneapi::mkl::rng::default_engine>(cpu_queue, mem_buf);
18
19   // Generating random numbers from the CPU engine
20   oneapi::mkl::rng::generate(oneapi::mkl::rng::uniform{}, engine_2, n, r2);
21
22   // Cleaning up memory buffer
23   delete[] mem_buf;