special case of using parallel computing toolbox in order to solve decomposed problems
Mostrar comentarios más antiguos
Hello,
How can I have a for loop with workers of parallel computing doing "1-some functions with different input for each worker, 2-gather the result of iteration from workers 3-doing computation with the result of all workers, 4-update the global input to each worker until stopping criteria and so on?
pseudonym code can be like this:
a=cell(number of workers,1); %first input in cell
b=cell(number of workers,1); %first input in cell
for i=1:maxiter % a loop in the client
tell the workers to find "X" based on some identical function and computations by different inputs (the cells of a and b)
do a global computation to find z with the resut of X gathered form each worker and update the input parameters (a and b)
if fcn(a,b,X,z)<eps
stop
end
end
Respuesta aceptada
Más respuestas (1)
Raymond Norris
el 30 de Oct. de 2020
Editada: Raymond Norris
el 30 de Oct. de 2020
Hi Bill,
There might be a couple of options
- parfeval*
- createJob/createTask
parfor won't do the trick because it doesn't allow for early exit (i.e. if fcn<eps then stop). parfeval provides the ability to run a collection of 'futures'. Each future could be the same function with different input arguments or different functions entirely. Once the futures are submitted, loop through and query their results. If you've found your threshhold, delete the remaining futures.
I'm putting an * next to parfeval because I don't fully under how you'd update the input parameters to something that's already been submitted, which would could cause issues for parfeval. I think you're suggesting something like:
a=cell(number of workers,1); %first input in cell
b=cell(number of workers,1); %first input in cell
for i=1:maxiter % a loop in the client
X(i) = identical_fcn(a{i},b{i})
z = X ...
% Would updating a and b effect how you're calling identical_fcn at the next iteration?
a{i} = z ... update some element of a, based on z before calling the next iteration of the identical_fcn
b{i} = z ... ditto
if fcn(a,b,X,z)<eps
break
end
end
A bit more code would help refine this.
Raymond
4 comentarios
Bill Masters
el 1 de Nov. de 2020
Bill Masters
el 1 de Nov. de 2020
Raymond Norris
el 1 de Nov. de 2020
Hi Bill,
So I don't make any assumptions, a couple of suggestions/thoughts
- I think you're missing an end for the for loop (before the definition of the tmp function)
- the tmp function should be returning (I assume f) not tmp
- the tmp function doesn't use the input argument i
- the call to tmp in the parfeval only passes in MAX_ITER (which maps to i). Nowhere are any of the variables rho, etc. defined. Even if they are global they won't be passed to each of the workers (for starters, you’d need to declare them as global in tmp as well, but this still won’t work).
- parfeval and fetchNext are (typically) called from within different for loops, because fetchNext is blocking until MATLAB fetches the results. This, along with updating z_old, x_hat, and u, all indicate that you'll have issues parallelizing this. This is a cascading waterfall algorithm, u(k) is dependent on u(k-1).
Based on #5, perhaps the best way to think of parallezing this is if you need to run this entire block of code multiple times. Then each worker can be given the entire code you have listed above and you can run 4 of these (for example). They wouldn't be tied together, but you could run multiple sets, all at the same time.
But perhaps I've misconstrued some things here.
Raymond
Bill Masters
el 3 de Nov. de 2020
Categorías
Más información sobre Parallel for-Loops (parfor) en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!