How do I differentiate the functions in parfeval?

I am trying to run a multiple functions parallel using parfeval, but I do not know how to destinguish between the programs because I can only input one function in the parfeval function. Is there a way to have changes between the programs when they are running parallel?
I have tried several things so far but these have been unsuccessful. (i) Include an input vector and use a different item in the vector in each iteration of the program. (ii) Collect the ID of the worker within the program and use this to differentiate between them. If there is a way to get the ID as a number to then use in an if-elseif statement, then I will be able to distinguish between the iterations of the program, but I could not figure out how to do this.
The output of the functions which are run using parfeval do not matter to me. During the program images and texts are saved which I use instead.

 Réponse acceptée

Walter Roberson
Walter Roberson le 27 Mar 2022

1 vote

Create a cell array of function handles. Index into it by the parfor index.
It is possible to get the id of the worker, but you should not be assuming that tasks are equally distributed over the workers. Workers are assigned chunks of indices at the beginning. When they finish that they are assigned the next available smaller chunk. When the smaller chunks have all been allocated, they are assigned individual remaining iterations. So you cannot assume that iteration #23 will be assigned to any particular worker and therefore you should seldom be using worker ID to determine what work is to be done.
SPMD is a bit more suitable for matching work and identifier, in that you can trade messages to decide which worker does what.

4 commentaires

Alternatively, you could use parfeval also with a cell array of function handles. Using either parfor or parfeval ought to alleviate the need for an if-else statement.
If you need the task ID to determine which task to run, getCurrentTask will return the task object and t.ID will store the task's ID. For example
parfor idx = 1:10
t = getCurrentTask;
t.ID
end
If you have a limited number of functions to use, each over a number of items, then parfeval() the function handle and a data item specific to what needs to be processed with the handle.
How would I be able to use psrfeval with a cell array of function handles?
Example:
N = 100;
candidates = {@shrink, @elongate, @twist1, @twist2};
what_to_run = candidates(randi(length(candidates), 1, N));
parfor K = 1 : N
thisfunction = what_to_run{K};
filename = sprintf("data_%03d.mat");
thisfunction(filename);
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by