Search code examples
matlabrandomparallel-processingmatlabpool

How to run a MATLAB script multiple times in parallel


I have a matlab script (call that MyProcessing.m) that does some computations based on some random number. Right now I have a fixed seed to get the same sequence of random numbers. I would like to run this script multiple times in parallel to utilize the multiple cores that I have available on the system. I want each of the new "processes" to start with a different (but fixed for the moment) seed. Bellow is the processing file as it is right now.

There is a for loop inside the script but I cannot use parfor because each iteration depends on the previous one.

MyProcessing.m

rng(1);
A = rand(5,5);
x =[];
y = []

% for loop
%   that updates x and y when necessary
% end for

figure(1);
scatter(x, y);
savefig(filename);

I have access to the Parallel Computing Toolbox in MATLAB but I cannot think on what I should do. I believe that I have to write another script to call the processing script with a different random seed but I want also the different processes to be run in parallel so that I can run many experiments.

EDIT:

I want something like

for i = 1:numberOfParallelProcesses
  startANewRunOfTheScript();
end

where the for loop start the process and then it does not wait but it continues to start the next one.


Solution

  • You could use batch to achieve this. You can do:

    for idx = 1:n
      job(idx) = batch('MyProcessing');
    end
    

    You can then fetch the results later using the load method of each element of job:

    for idx = 1:n
      wait(job(idx)); % wait for results to arrive
      out{idx} = load(job(idx));
    end
    

    There's more about batch processing in the doc.