St-Ste-Ste-Stephen
St-Ste-Ste-Stephen

Reputation: 1076

Creating a pool of tasks/jobs with a limited amount of workers (ThreadPool) by using parallel toolbox

I would like to use the parallel computing toolbox to speed up a set of function calls that do not depend on each other. To make this more efficient I would like to use timer functions/callback functions to continually execute more of the functions after one of my function finishes executing. I don't know ahead of time which ones will be faster, so I can't just divide my set of functions into a few pools and set them up going in parallel.

In other words, I would like a few parallel executions to keep pulling from a pool of functions.

The only way I have this set up now I have a cell array of strings that I use str2fun on, is there a better way to do this?

Questions are welcome.

Upvotes: 3

Views: 1024

Answers (2)

Andrey Rubshtein
Andrey Rubshtein

Reputation: 20915

There is no need to write such code by yourself. The Matlab parallel toolbox has the ability to create a scheduler with multiple jobs. You can call createJob multiple times, and the scheduler will do the pulling.

 foos = [@foo1,@foo2,@foo3,@foo4]
 for i=1:numel(foos)
     obj = createJob();
     createTask(obj, foos(i), 1, {'your input'});
     submit(obj);
 end

enter image description here

Upvotes: 5

Oli
Oli

Reputation: 16065

Why not doing something simpler like:

matlabpool 3

parfor t=1:3
  if t==1
    a1=f1();
  end
  if t==2
    a2=f2();
  end
  if t==3
    a3=f3();
  end
end

Upvotes: 0

Related Questions