Is it possible to build a parfor loop where workers share and edit (only) one variable array?

12 次查看(过去 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.

采纳的回答

Edric Ellis
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 个评论
Sebastian Sas Brunser
I will try implementing this and see how it goes.
The load will be roughly balanced, as the number of rays is large (>1e5), and the probabilities of each ray bouncing or being terminated are similar irrespective of its position.
I think this answers the original question, and now what's left is for me to do is the actual implementation of the methodology.
Thank you very much for your help!!!!!
Sebastian.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by