load_state#

Loads the state of the random number engine from the provided memory buffer or file, then creates new engine object.

Description#

The load_state function allows you to create a new random number engine object from the binary state of another engine, which was written in a memory buffer or file. You can use different sycl::queue objects for the new engine.

API#

Syntax#

Load from Memory Interface

namespace oneapi::mkl::rng {
  template<typename Engine>
  Engine load_state (const sycl::queue& queue,
                     const std::uint8_t* mem);
}

Load from File Interface

namespace oneapi::mkl::rng {
  template<typename Engine>
  Engine load_state (const sycl::queue& queue,
                     const std::string& filename); // deprecated since 2024.1 release
}

> Note: The Load from File function is deprecated. Use the Load from Memory function instead.

Include Files#

  • oneapi/mkl/rng.hpp

Input Parameters#

Load from Memory Interface

Name

Type

Description

queue

const sycl::queue& queue

sycl::queue object, which will be used for the newly-created engine.

mem

const std::uint8_t*

Memory, where engine's state was stored.

Load from File Interface

Name

Type

Description

queue

const sycl::queue& queue

sycl::queue object, which will be used for the newly-created engine.

filename

const std::string&

Name of the file where engine's state was written.

Output Parameters#

Type

Description

Engine

New random number engine object, which would be created from the given sycl::queue and loaded engine's state.

The following code illustrates how to store the engine's state to the

engine_state.dat file and load it back:

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;