rand function is slow with a parfor loop

조회 수: 2 (최근 30일)
James
James 2011년 11월 18일
I'm trying to multithread a stochastic simulation using the parallel computing toolbox. I've profiled the program extensively to ensure that it is optimized. However, when I'm using PCT with 4 local workers, I find that the rand function consumes an inordinate amount of computational time far beyond what it uses in a single threaded program. Here is a representation of the code in question.
if(~(rand <= Pr))
I am stumped as to why this would be 10-15x slower than the single threaded case, unless rand is a synchronized function. I'm using a MacPro with 16 GB RAM and 2 quad core processors (MATLAB R2008b) for reference.

답변 (3개)

Alex
Alex 2011년 11월 18일
All computer based random number generators need a 'seed'. I don't know about Matlab, but this seed, if not provided directly by the user, is based off of the current system time. (by using the same 'seed', you can then get repeatable results, even in a 'random' environment).
I think, 2008b does not have the updated random generator methods.
http://www.mathworks.com/help/techdoc/math/bsn94u0-1.html This link talks about some of the older changes that have been made to the rand function.

Walter Roberson
Walter Roberson 2011년 11월 18일
2008b does have the updated random number generator methods.
MATLAB uses a constant seed for each session, but a different seed for each parallel worker. This is described more in Peter Perkin's posting at http://www.mathworks.com/matlabcentral/newsreader/view_thread/299841

James
James 2011년 11월 18일
Each worker uses a provided seed, not the default. Even if they used the worker ID as a seed, that wouldn't explain why calling rand is much slower in a parfor loop than a for loop.
  댓글 수: 1
Konrad Malkowski
Konrad Malkowski 2011년 11월 30일
James could you provide a more complete example that reproduces the issue?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by