Running the same pseudorandom numbers on mexed files with parfor loops /omp pragma

1 view (last 30 days)
Carlos Larriba-Quest
Carlos Larriba-Quest on 28 Mar 2020
I have been mexing some of my parfor loop subroutines to make them a little bit faster. With parpool, I was able to create the same RandStream to send to the workers so that I get the same solution everytime. I have been trying to do the same thing with my mexed files but I am unable to do so.
I have found that one can "clear mex" out of memory and this will repeat the same pseudorandom numbers everytime which is fine for debugging.
However, what i would like to do is to be able to get different results for different runs by using different seed values. But if I choose the same seed after clearing mex, it would give always the same result, but different from another seed. rng() does not seem to work. I have no been able to find a way yet.
My attempt at the mex code:
function throwawayrngmextest(seed,threadc)
%Parpool to mex rng test
%#codegen
randomtest = zeros(threadc,1);
rng(seed);
%parfor loop
parfor ii=1:threadc
randomtest(ii) = rand(1)
end
for i=1:threadc
fprintf('%0.5f \n',randomtest(i));
end
end

Answers (2)

Wilson A N
Wilson A N on 1 Apr 2020
Hi Carlos,
You can use the time function from the 'time.h' header file to generate a random number based on the current time. A crude implementation is given below:
function throwawayrngmextest(threadc)
%Parpool to mex rng test
%#codegen
coder.cinclude('"time.h"')
k = 0;
k = coder.ceval('time',[]);
randomtest = zeros(threadc,1);
%parfor loop
parfor ii=1:threadc
rng(k+ii,'twister');
randomtest(ii) = rand(1);
end
for i=1:threadc
fprintf('%0.5f \n',randomtest(i));
end
end
I was able to generate different random numbers in each of the different threads. Even after performing a clear mex I obtained different random numbers for each thread (there was no repeat).
The only catch is that this code cannot be executed in MATLAB due to the ceval call. Hence you may need to provide a branch using coder.target('MATLAB') to provide a MATLAB execution path.
I hope this helps.
-Wilson

Carlos Larriba-Quest
Carlos Larriba-Quest on 1 Apr 2020
Thank you Wilson,
I did something very similar by creating my own chaotic function for each threadc. It seems to work for everything I tried. I am afraid that adding a small change in the seed will lead to incorrect pseudorandom numbers so I did not want to go that route. With the code below, from all the tests performed with any seed and any number of threads, I always got what seemed like accurate random generation numbers that can be repeated if needed. FYI, the 4.2948e9 is a value short of 2^32 which is the max value for seeds.
function rngvalues=throwawayrngmextest3(seed,threadc)
%Parpool to mex rng test
%#codegen
randomtest = zeros(threadc,1);
%parfor loop
parfor ii=1:threadc
rng(round(abs((sin(ii*seed)*4.2948e9))));
randomtest(ii) = rand(1)
end
rngvalues = randomtest;
for i=1:threadc
fprintf('%0.5f \n',randomtest(i));
end
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by