Is it possible to build a parfor loop where workers share and edit (only) one variable array?
조회 수: 11 (최근 30일)
이전 댓글 표시
Good day!
I am building a ray tracer to solve for thermal radiation within a solid.
Ray tracers are usually considered an exemplary algorithm for parallel computing, as you can "launch" rays on parallel workers. All the worker's computations are entirely independent, and by the end, you can sum up the results of the workers.
Nevertheless, I need to modify this slightly and make the computation of each worker dependent on a variable that needs to be accessible and editable by the other workers. In other words, most of the calculation is still independent but, in some intermediate steps, I need to check a "bookkeeping" or a "counter" (defined for the solid), and depending on the value stored there, the worker decides what to do. Each worker would also need to edit or update this counter after checking its value.
Is there a way to do this?
If not, I see a way of building a parfor inside a while loop, where only the independent calculation would be done in parallel workers. Nevertheless, given the number of iterations, I feel this method would most likely be slower than just a for loop.
Looking for your comments, and thank you very much!
Sebastian.
댓글 수: 0
채택된 답변
Edric Ellis
2021년 6월 11일
If I've understood correctly, you could use spmd to do something like this. spmd allows communication between the workers, so that you can exchange values on the workers. It's a bit like your idea of nesting parfor inside while, but everything stays on the workers. Here's a very rough sketch of how things might proceed:
spmd
done = false;
while ~done
% each worker has an independent copy of 'myValue', but
% we'll combine these later. For the sake of this simple
% example, I'm going to assume we're getting a numeric
% increment from each ray-trace, and we want to add them
% all up
myValue = 0;
% for-drange causes each worker to execute a different portion of
% the loop. Worker 1 gets 1:(numRays/numlabs), and so on.
for rIdx = drange(1:numRays)
myValue = myValue + traceRay(rIdx);
end
% Use GPLUS to find the total by communicating across
% all workers
totalValue = gplus(myValue);
% termination condition.
done = totalValue > thresh;
end
end
I hope this makes some sort of sense. Instead of gplus, you might need the more general gop or perhaps you might need to roll your own communication using labSend and labReceive.
댓글 수: 6
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!