Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
6977 Discussions

Getting the "status" of a random number stream

cppcoder
Beginner
542 Views

Hi,

I have a program that is called several times as part of a simulation. When the simulation begins, the program is called from a script with parameterINITIAL_SEED (and other parameters). My program initializes a stream with that seed:

vslNewStream( &my_vsl_stream,VSL_BRNG_MT19937, INITIAL_SEED);

Thenproduces some results involving random numbers, writes results to disk, and ends.

The next time is called, I want it to start generating numbers from the point the stream was left during the previous execution, let's call it NEXT_SEED:

vslNewStream( &my_vsl_stream,VSL_BRNG_MT19937, NEXT_SEED);

How can I get that number before finishing execution so that it can be written to a file, and then passed by the script the next time and be used as the seed of the generator?

I found a functionGetStreamStateBrng but it doesn't seem to do what I want, or maybe it does. Any clue about it?

Thanks in advance

0 Kudos
6 Replies
Andrey_N_Intel
Employee
542 Views

Hello,

You might want tocheckservice RNG functionalityfor saving/restoring random stream descriptive data to/from file

vslSaveStreamF( stream, fname );
vslLoadStreamF( &stream, fname );

Its description is available inRandom Number Generators\Service Routines Section, which isinStatistical Functions Chapter of MKL Manual.

Please, let me know if this answers your question.

Best,
Andrey

0 Kudos
cppcoder
Beginner
542 Views
Hello,
Thanks for your answer.
The documentation only says that those functions "save /read stream descriptivedata". It doesn't say what descriptive data is that or if it saves the current state of the stream. Where can I find that info?
If the descriptive data saves also the initial SEED I passed and use it to initialize the stream, then those functions won't help me.
Thanks
0 Kudos
cppcoder
Beginner
542 Views

OK, it seems to be working.

I'm included a sample program to test this. It first initializes a stream with SEED = 10, and produces 5 numbers and saves the info of the stream. Then initializes a second stream with that info. Then it generates another 5 numbers which are different from the initial 5. Finally, it initializes a third stream with the same SEED=10 but generates 10 numbers. The first 5 are the same as the 5 generated with the first stream, while the remaining 5 are the same as the ones generated with the second stream.

Thus, it's working as expected

Thanks a lot for your answer

#include 
#include 

#include "mkl_vsl.h"
#include "errcheck.inc" // this is included somewhere in the MKL's examples directory 

#define SEED    10
#define BRNG    VSL_BRNG_MT19937
#define METHOD  0
#define N       5*2

double r;

int main()
{
    VSLStreamStatePtr stream1, stream2, stream3;
    int i, errcode;

	////////////////////////////////////
    errcode = vslNewStream( &stream1, BRNG,  SEED );
    CheckVslError( errcode );

	errcode = vdRngUniform( METHOD, stream1, (N / 2), r, 0.0, 1.0 );
    CheckVslError( errcode );

	std::copy (r, r + (N / 2), std::ostream_iterator (std::cout, "\t"));
	std::cout << std::endl;

	errcode = vslSaveStreamF( stream1, "C:\\temp\\somefile.bin" );
    CheckVslError( errcode );

    errcode = vslDeleteStream( &stream1 );
    CheckVslError( errcode );

	////////////////////////////////////
	errcode = vslLoadStreamF( &stream2, "C:\\temp\\somefile.bin" );
	CheckVslError( errcode );

	errcode = vdRngUniform( METHOD, stream2, (N / 2), r, 0.0, 1.0 );
    CheckVslError( errcode );

	std::copy (r, r + (N / 2), std::ostream_iterator (std::cout, "\t"));
	std::cout << std::endl;

    errcode = vslDeleteStream( &stream2 );
    CheckVslError( errcode );

	////////////////////////////////////
    errcode = vslNewStream( &stream3, BRNG,  SEED );
    CheckVslError( errcode );

	errcode = vdRngUniform( METHOD, stream3, N, r, 0.0, 1.0 );
    CheckVslError( errcode );

	std::copy (r, r + N, std::ostream_iterator (std::cout, "\t"));
	std::cout << std::endl;

    errcode = vslDeleteStream( &stream3 );
    CheckVslError( errcode );
	////////////////////////////////////

    return 0;
}
0 Kudos
Sergey_M_Intel2
Employee
542 Views
Hi cppcoder,

That's great that you find save/restore stream feature meeting your needs. Do you still feel that documentation for that feature needs to be improved?

Sergey
0 Kudos
cppcoder
Beginner
542 Views
Hi Sergey,
Yes, I still do. It was not very clear from reading the documentation if it achieved what I needed. I had to try it myself to see if it did. I don't think I would have found the functions I used useful unless I have got the reply on this forum.
Thanks
0 Kudos
Sergey_M_Intel2
Employee
542 Views
Hi cppcoder,

We'll take care of it and improve this part of documentation in coming releases of the MKL product.

Thank you for the feedback,
Sergey
0 Kudos
Reply