How can I save the stream state (i.e to a file) of VSL generators such that I can later reload it and start generating where I stoped?
I would recommend two approaches to save/restore simulation runs.
Suppose that you want to perform a simulation run with N random numbers generated. Further you want to store the current state after N numbers have been generated. And finally you want to continue simulation starting from (N+1)-th number.
1. Create the stream with the vslNewStream function (you will pass basic generator brng and the seed seed). Perform the first run. By the end of the run you know how many numbers N were generated.
2. Save this N to a file, delete the stream stream and finish the application.
In the second run you should do following:
1. Create the stream with the vslNewStream function (you will pass the same basic generator brng and the same seed seed as in the first run).
2. Call vslSkipAheadStream subroutine to skip N numbers generated in the first run. This subroutine works much faster than if you would simply generate these N numbers. (Please see the VSL Notes document for details, which basic generators support the skip-ahead method).
3. Perform the second run. It will start with (N+1)-th number.
4. Delete the stream and finish the application.
You can apply this idea to perform multiple saves/restores of N.
In the second approach you will save/restore whole stream state rather than quantity of numbers have been generated. To save the structure into a file you will need to determine the size of the stream state structure for the basic generator is being used. To do this you need to call the vslGetBrngProperties routine:
vslGetBrngProperties(brng,properties) - fills the fields of the structure properties with the properties of the basic generator brng.
Among other fields, the properties structure has the StreamStateSize field (the first field in the structure of size of 4 bytes) -- this is what you need. For further information I would recommend the Advanced Service Subroutines chapter of the MKL Reference Manual.
To implement the save/restore to a file I suggest the following scheme:
1. Perform the first run of a simulation.
2. Determine the size in bytes of the stream state structure.
3. Get an access to the memory, where the stream state data is stored, and save this data to a file (you know number of bytes to store from Step 2).
4. Delete all streams and finish the application.
For the second run.
1.Allocate the memoryrestored_stream in which you will load thestream statefrom the file.To do thisyou should determine the size of the stream state structure again to determine how many bytes toload.
2. Load the data into restored_stream.
I would not recommend using restored stream state directly in random number generators because therewill besome internal fields in the structure which are not initialized properly after the load. To avoidthis you should do the following:
3. Create one more stream stream by the vslNewStream function. No matter which seed you will pass, the only important is brng - should be the same as you saved/restored.
4. Use thevslCopyStreamState to copy the state of restored_stream to the stream stream. This routine will copy only the stream state related information, and other internal fields in stream are already initialized in the step 3.
5. Perform the second run.
6. Delete the stream stream. Deallocate the memory for restored_stream.
Let me know if it works for you.We also canwrite a sample program for you if you need this.
We are also considering for future MKL releases what can be done on VSL side to simplify stream state save/restore mechanisms.