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

11 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Edric Ellis
Edric Ellis am 11 Jun. 2021
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 Kommentare
Sebastian Sas Brunser
Sebastian Sas Brunser am 11 Jun. 2021
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by