MATLAB Answers

Run scripts in parallel on multiple workers (distributed job)

12 views (last 30 days)
Igor Braverman
Igor Braverman on 31 Oct 2012
Commented: Dingqiao Zhu on 7 Mar 2017
Hello, I have 12 workers available in my local matlabpool. I'd like to run 3 scripts (doing different simulink simulations) in parallel on those workers. There is no need for those tasks to communicate to each other. What is the best way to go about this (programmatically)?
pctRunOnAll('cd C:\Users\Controls\prj\sunapee');
pctRunOnAll('sys = ''sunapee'';')
pctRunOnAll('load_system( sys)');
low_j = batch('low_pressure_tsts');
med_j = batch('mid_pressure_tsts');
hi_j = batch('high_pressure_tsts');
Doesn't seem to be doing the trick. Thank you, Igor


Sign in to comment.

Answers (2)

Ryan G
Ryan G on 31 Oct 2012
It sounds like there are multiple simulations in each script. So the number of simulations would be the sum of the three and you want to distribute this task.
From the doc examples for the batch command it looks like you would want something more like this:
j = batch('script1', 'matlabpool', 8, 'CaptureDiary', true);
wait(j); % Wait for the job to finish
diary(j) % Display the diary
load(j) % Load job workspace data into client workspace
Where you would replace script1 with your script name and replace 8 with your 12 workers.


Igor Braverman
Igor Braverman on 31 Oct 2012
Thank you for your reply, Ryan. Each script runs a number of simulations, that is correct, but I am not worried about distributing those yet. I just wanted to run script 1 in parallel with script 2 and script 3. If I wait for the job of script 1 to finish it kind of defeats the purpose. In pseudocode i'd like to do something like this:
  1. open up matlab pool of 12 workers
  2. run script 1 on worker 1, run script 2 on worker 2, run script 3 on worker 3.
  3. collect the data to the client
  4. close matlab pool
Ryan G
Ryan G on 1 Nov 2012
The names of the script implied they might each loop within the script, in which case running each one in this method would save time. If each script is more linear and can you want to run them each on a worker at the same time you could try something like this:
j = batch('script1', 'Profile', 'local','matlabpool', 3);
k = batch('script2', 'Profile', 'local','matlabpool', 3);
m = batch('script3', 'Profile', 'local','matlabpool', 3);
This would potentially use 4 workers for each job here. If this still doesn't work, I would suggest looking into the createJob function.

Sign in to comment.

Danilo Teran
Danilo Teran on 28 Sep 2016
Hello, How can I stop when I start both scripts.
My scripts is too long and I need to stop them.
Best Regards

Sign in to answer this question.