How to run a MATLAB script multiple times in parallel

1k views Asked by At

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.

2

There are 2 answers

2
Edric On BEST ANSWER

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.

0
learnvst On

For Embarrassingly parallel problems in matlab, by far the easiest solution is to start multiple instances of matlab and run the script on each instance (obviously itterating forwards on the random seed when you begin each instance). I used this simple technique to great effect on a 40 core server in the past. The only limit is your system memory. Use a little trial and error to find the number of instances required to get maximum throughput.