Randomness is lost in parfor loop on GPU?
이전 댓글 표시
I have a code with a following structure:
% my_code_seed#.m
myoutputs1 = my_function1(myinputs1);
seed = #;
rng(seed);
parfor iter = 1:50
myouputs2 = my_gpu_function2(myoutputs1, my_function_of_Gaussian);
result(iter).name = myoutputs2;
end
save(file_name, 'result');
What I want to do is to model some random behavior of something by using this code repeatedly. What I'm doing is to copy this code and change the value of the seed. For example, the "my_code_seed1.m" uses "seed = 1", and "my_code_seed2.m" uses "seed = 2". I run these codes in a parallel way on a GPU cluster to save the computation time. However, it seems like the results from the codes are very similar, even though I'm giving different values of seed.
Do you have an idea why rng(seed); is not working properly in this case? I appreciate any help.
답변 (1개)
Steven Lord
2022년 4월 13일
0 개 추천
What I'm doing is to copy this code and change the value of the seed. For example, the "my_code_seed1.m" uses "seed = 1", and "my_code_seed2.m" uses "seed = 2".
So if later on you find a bug in my_code_seed42.m you're going to go back and modify the previous 41 files? That's inefficient. Instead write the code once as a function that accepts a seed value and call that function with the various seeds as input.
As for the randomness in a parfor, see this documentation page on repeating random numbers in a parfor loop and the page on controlling randomness to which its first paragraph links.
댓글 수: 4
Edric Ellis
2022년 4월 13일
In the example you show, you're calling rng(seed) on the client, this does not affect the workers operating on the body of the parfor loop. If you're using gpuArray in the body of the parfor loop, then you'll need a combination of these two bits of documentation:
- https://www.mathworks.com/help/parallel-computing/repeat-random-numbers-in-parfor-loops.html - general page showing you how to get reproducible random numbers inside parfor. If you want to have each "job" get a different set, you'll need to adapt that to offset the streams per job
- https://www.mathworks.com/help/parallel-computing/gpurng.html describes how to set up rng state for gpuArray - it is separate from the CPU rng state.
Daigo
2022년 4월 13일
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!