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

Basic Random Number generators

fabiocannizzo
Beginner
1,820 Views

// I do the following:

VSLStreamStatePtr streamPtr;

unsigned intbuffer[128];

unsigned intseed = 5489;

vslNewStream( streamPtr, VSL_BRNG_MT2203 + 5,seed ); // create & init

VSLBRngProperties properties;

vslGetBrngProperties( VSL_BRNG_MT2203 + 5, &properties ); // get properties

(*properties.iBRng)( streamPtr_, 128, buffer ); // generate some 32bit random numbers

// so far so good. Now I want to reset the generator to its initial state, so that it will refegenerate exactly the same sequence of random numbers

(*properties.InitStream)( 1, &streamPtr, 1, &seed ); // attempt to reset generator status

// unfortunately this does not work. The generator will not reproduce exactly the same sequence. What am I doing wrong?

Thanks

0 Kudos
7 Replies
Andrey_N_Intel
Employee
1,820 Views

VSLBRngProperties is VSL data type which describes structure of BRNG. You may want to use it to get properties of the given generator via call to vslGetBrngProperties routine. Another and main intention of the structure is to give opportunity to register own generator following the VSL conventions. Standard functions like vslNewStream or viRngUniformBits are recommended for work with a user-defined generator (or any other BRNG registered in the library).

Attempt to reset the generator in the test case fails as the second parameter of InitStreamPtr function is passed by value:

typedef int (*InitStreamPtr)( int method, VSLStreamStatePtr stream,int n, const unsigned int params[] );

So,second call to iBRng should return random numbers from the original random stream. If you want a copy of the original stream you could use standard vslCopyStream function.

0 Kudos
fabiocannizzo
Beginner
1,820 Views

Thanks a lot.

Now I have fixed this problem, but it still does not work. The call

(*properties.InitStream)( 1, streamPtr, 1, &seed ); // attempt to reset generator status to initial state

fails with error code return LeapFrog not supported.

0 Kudos
Andrey_N_Intel
Employee
1,820 Views

The way you useVSL routines (via fields of VSLBRngProperties structure) is error-prone and results in unpredicted work of the library. Direct call to *properties.InitStream has no effecton streamPtras the function does not return updated stateof the generator (you pass the streamPtr parameter by value, not by reference). To have things properly worked, please use standard VSL functions.

0 Kudos
fabiocannizzo
Beginner
1,820 Views

Thanks a lot for your reply.

In fact I am keen on doing that. Except I am having difficulties in undertsanding what is the correct way to call the routines. I could not find any VSL function which reset the state of a generator.

Let me please restate the definition of my problem:

I want N independent (uncorrelated) streams (to be used by separate consumer threads) of 32-bit integer random deviates with uniform discrete distribution in the interval [0, 0xFFFFFFFF], based on the Mersenne Twister.

I understand that this can be achieved using MT2203.

I also want each of these streams to be independently resettable to their initial state (same seed as previously used in the initialization).

Howwould you reccomendto achieve that?

Thanks a lot.

Regards,

Fabio

0 Kudos
Andrey_N_Intel
Employee
1,820 Views

Fabio,

VSL MT2203 BRNG allows creating up to 1024 independent random streams.
Their creation/use/deletion can be organized in the following way:

#define N 10 /* number of random streams */
#define SEED 44444 /* seed can be different for different MT2203 streams */

int main()
{
...
int k;
VSLStreamStatePtr stream;
int errcode;
unsigned int r[128];
...

/* Create N streams */
for ( k=0; k {
errcode = vslNewStream( &stream, VSL_BRNG_MT2203+k, SEED );
}
...
errcode = viRngUniformBits( method, stream[5], 128, r ); /* use 5th MT2203 stream*/
...

/* Delete streams */
for ( k=0; k {
errcode = vslDeleteStream( &stream );
}
...

}

Support of resetting is, for example,as follows:

...
VSLStreamStatePtr stream, streamCpy;

/* Create N streams and their copies */
for ( k=0; k {
errcode = vslNewStream( &stream, VSL_BRNG_MT2203+k, SEED );
errcode = vslCopyStream( &streamCpy, stream );
}
...

To reset any stream you would need to call vslCopyStreamState function in proper time:

vslCopyStreamState( stream, streamCpy );

or just use streamCpy instead of stream.

In the end of the calculations you would need to free memory allocated for
all streams:
for ( k=0; k {
errcode = vslDeleteStream( &stream );
errcode = vslDeleteStream( &streamCpy );
}

It isalso possible todelete and createthe streamforrequired MT2203 index and seed once more.

Please, let us know how this scheme works for you. Additional details re the VSL functions and their use can be found in MKL Manual and Vslnotes.pdf.

Thanks,
Andrey

0 Kudos
fabiocannizzo
Beginner
1,820 Views

Andrey,

Thanks a lot.

So summarizing, you do not have a method which just re-seed an existingstream. So I have the two choices:

1) Allocate stream A. Copy it into stream B. . Whenever I want to resetstream A I copyB onto A. So my extra cost is mainly"extra-memory" for stream B.

2) Allocatestream A. When I am done delete it and recreate it. Now I do not have any memory extra cost, but I have a performance cost for memory reallocationand re-initialization.

They both work for me.

Whichmethod do you think is faster?

What is the memory consumption associated withcreating of a stream of type MT2203?

Thanks a lot,

Fabio

0 Kudos
Andrey_N_Intel
Employee
1,820 Views

Fabio,

VSL does not have a function for re-seeding as existing VSL service routines cover this specific task. Answer to the question which method is faster depends on requirements of your application:
- how many MT2203 streams do you need to create?
- how often does your app re-seeds the streams and how many of them are re-seeded?
- how much time does the apps spend in generation of random numbers and in other calculations?
- what are the memory limitations? etc

Probably, generation of random numbers/other calculations take significant amount of time, and either approach for re-seeding of the streams will be negligible in terms of time. All analysis of the performance and relevant experiments can easily be done on your side. Intel provides tools for analysis of performance. VTune is the instrument which would help you to analyze bottlenecks of your app and to choose the most suitable approaches in developement of your algorithm.

To determine amount of memory which is required to store state of MT2203 random stream, please, call function vslGetBrngProperties( brng, &properties ). Field StreamStateSize of the structure properties gives idea about memory consumption.

Best,
Andrey

0 Kudos
Reply