A newer version of this document is available. Customers should click here to go to the newest version.
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
   // Creating GPU engine
   oneapi::mkl::rng::default_engine engine(gpu_queue);
   // Checking how much memory is required to save
   auto mem_size = oneapi::mkl::rng::get_state_size(engine);
   // Allocating memory buffer
   std::uint8_t* mem_buf = new std::uint8_t[mem_size];
   // Saving state of engine in the file
   oneapi::mkl::rng::save_state(engine, mem_buf);
   // Generating random numbers from the GPU engine
   oneapi::mkl::rng::generate(oneapi::mkl::rng::uniform{}, engine, n, r1);
   // Loading state for the CPU queue
   auto engine_2 = oneapi::mkl::rng::load_state<oneapi::mkl::rng::default_engine>(cpu_queue, mem_buf);
   // Generating random numbers from the CPU engine
   oneapi::mkl::rng::generate(oneapi::mkl::rng::uniform{}, engine_2, n, r2);
   // Cleaning up memory buffer
   delete[] mem_buf;