ParFor - how to rewrite For loop

Is it possible to rewrite the following code to that the main loop (a 1:NumData) use Parfor and allows the main loop to be processed in parallel. The core prpoblem is the need to access the 'a' variable which is state dependent and therefore excludes the use of Parfor.
Suggestions would be appreciated.
row = -1; high = 1;
NumData = 100000;
% start timer
tic
% pre allocate memory
RandData= ones(1:80,1:NumData);
% make random numbers
for a=1:NumData % <-- use ParFor ??
for b=1:80
RandData(a,b) = round(low + (high-low) * rand);
end
end
% end timer
toc

답변 (1개)

Daniel Shub
Daniel Shub 2011년 10월 4일

0 개 추천

Have you tried it? It works fine for me
parfor a=1:NumData
for b=1:80
RandData(a,b) = round(low + (high-low) * rand);
end
end

댓글 수: 2

adam cox
adam cox 2011년 10월 4일
I tried this, but it did not work as I think that in parallel mode (using matlabpool) the parallel processing has no way of what the variabel 'a' is globally when numerous instances are being run, unless they all just mimick oneanother or in fact the Parfor is not being run in parallel.
How many workers were involved in the matlabpool?
Walter Roberson
Walter Roberson 2011년 10월 4일
I don't see the state dependency? What you have posted looks like straight-forward use of parfor to me.

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

카테고리

도움말 센터File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

질문:

2011년 10월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by