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

vslCopyStream bug?

eddy_alonso
Beginner
372 Views

Dear all:

What am I doing wrong? The following very short code crashes

#define CACHESIZE 100

int main()

{
double r1[CACHESIZE];

unsigned int seed = 45;

VSLStreamStatePtr stream1;
VSLStreamStatePtr stream2;
VSLStreamStatePtr stream3;

int errcode = vslNewStream(&stream1, VSL_BRNG_MT19937, seed);
errcode = vdRngGaussian( VSL_METHOD_DGAUSSIAN_BOXMULLER2, stream1, CACHESIZE, r1, 0.0, 1.0 );
errcode = vslCopyStream(&stream2,stream1);
errcode = vslCopyStream(&stream3,stream2);
errcode = vslDeleteStream(&stream1);
errcode = vslDeleteStream(&stream2); // crash here
errcode = vslDeleteStream(&stream3);

return 0;
}

In other words, if I make a copy from a stream copy, it crashes when I delete the second stream. Thiscomes aboutin the context of writing a random number generator class, its copy constructor and the destructor. In the copy constructor, I copy the stream, which is a member variable of the class, and the destructor calls vslDeleteStream. When I call a function, which in turn calls another function, if both functions take the random number generator as argument (passed by value), the copy constructor is called twice in a row, and the destructor as well when I exit the scope -- the same sequence of shown above: two calls to CopyStream and two calls to DeleteStream. Am I doing something wrong? Is it a bug? About the stacked copy of streams, I am very constrained by the program where I am doing the development.

Thanks a lot in advance

Eduardo

0 Kudos
2 Replies
Ilya_B_Intel
Employee
372 Views

Eduardo,

Thank you for reporting this issue. We have also found this bug and prepared a fix which will be available in the nearest MKL release.

As for the current workaround, we could recommend the following:

- save RNG initialization parameters in private members of the class;
- save information about the number of generated numbers (say, num_generated);

- use vslNewStream function with the same initialization parameters in copy constructor;
- use vslSkipAheadStream function with num_generated variable asthe secondparameter to start generating random numbers in the new class from the same point as in the class which is being copied.

Sorry for inconvenience.

Software engineer,
Ilya

0 Kudos
eddy_alonso
Beginner
372 Views

Dear Ilya:

Thanks for your quick answer. Unfortunately, your workaround is valid only for basic RNG's for which the skip-ahead method is supported, which is not the case in any of the mersenne-twister flavors provided by theMKL (MT19937, MT2203) according to the vslnotes.pdf. I tried and it didn't work (it returned and invalid parameter error). This is very unfortunate, because I bought the MKL almost exclusively for the random number generators. I only hope it's fixed in the next release.

Regards

Ed

0 Kudos
Reply