Running the same pseudorandom numbers on mexed files with parfor loops /omp pragma
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (2)
Wilson A N
am 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Parallel Computing Fundamentals finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!