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

11 views (last 30 days)
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!

Accepted Answer

Edric Ellis
Edric Ellis on 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:
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);
% Use GPLUS to find the total by communicating across
% all workers
totalValue = gplus(myValue);
% termination condition.
done = totalValue > thresh;
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by