Writing a shared variable at the end of a parfor loop

Hi,
I am using the parfor construct in Matlab. I need each worker to perform certain work and put the result in a matrix. Then I need to join together all the matrices into one. Something like in the pseudo code below
The idea is that each lab can perform a lot of computations and update its own result matrix, but at the end I want to join all the matrices together. There is no need to update the final matrix every time a lab does a computation.
EDIT: prefer a Matlab solution rather than a file exchange contribution
Thanks in advance
mat_for_all = [];
mat_local = [];
parfor i=1:100000
result = do_some_calc(i);
mat_local = [mat_local; result];
end
% join all the local matrices:
mat_for_all = [mat_for_all; mat_local];

 채택된 답변

Thomas
Thomas 2012년 3월 21일
This mat_for _all will have results of all the iteration in it, to check the result of each iteration use: mat_for_all(iteration_no). Dunno, where you are getting stuck..
mat_for_all=[];
parfor i=1:10000
mat_for_all(i)=randn;
end
mat_for_all

댓글 수: 4

Mark
Mark 2012년 3월 21일
Assuming it works (I haven't tried), wouldn't this cause access to mat_for_all to be blocked when one lab is writing to it? If so, that would have some performance penalty, wouldn't it? What I was asking is that each lab updates its own local matrix, then once all have finished doing so, join all the local matrices (a single data sending) per lab in a shared matrix (allocated locally)
Does this make sense of I am wrong about the above?
Mark, it's fine to do concatenations inside a PARFOR. MATLAB understands what you mean, and it collects the results from each worker together back at the client, and assembles the final result there. However, in this case assuming 'result' is a scalar, you may as well do
parfor i=1:100000
result(i) = do_some_calc(i);
end
The above parfor loop does work.. and MATLAB does the concatenation of the matrices.. That is the way the parallel computing toolbox has been designed and the there is little to no penalty for writing the elements of the matrix that way (Due to something internal in the PCT ..) I have run Monte Carlo simulations with thousand of runs the above way and it works fine..
Mark
Mark 2012년 3월 22일
Thank you

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

추가 답변 (0개)

카테고리

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

질문:

2012년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by