![]() |
![]() |
||||
|
Submitting Multiple Batch Jobs
If you need to run multiple instances of the same program with different initial parameter values you do not need to write a separate shell script for each run. Instead you can write just one shell script that will read the different sets of parameters from a text file and submit separate jobs with each set of parameters. The examples below are for MATLAB and Stata programs. For other programs you will need to modify the line of the script that calls the program.
Multiple job shell script for MATLAB programsSuppose you have a simulation in Matlab called "my_simulation.m" which is a function that requires 4 parameters and you would like to run this program for a few different sets of parameter values: my_simulation.m function my_simulation(P1,P2,P3,P4) . . . The following shell script reads the parameter values from a text file and submits a separate batch job for each set of parameters: submit.txt (for a MATLAB program) #!/bin/bash while read P1 P2 P3 P4 do JOB=`qsub -m abe -N Big_run - << EOJ cd ~/matlabprograms matlab << M_PROG my_simulation(${P1},${P2},${P3},${P4}); M_PROG EOJ ` echo "JobID = ${JOB} for parameters ${P1} ${P2} ${P3} ${P4} submitted on `date`" done < params.txt exit The sample script above requires a text file called "params.txt" that contains the sets of parameter values, one set per line, each parameter separated from the next by a space: params.txt 0.01 5000 0.9 1 0.02 5000 0.7 2 0.03 5000 0.6 3 The script will read each set of values and create, on the fly, the equivalent of a single job shell script. This is done in each iteration of the This examples assumes that "params.txt" is located in the same directory as the shell script. You can also specify another location of the parameters file by replacing Note that the definition of the job starts and ends with a "back tick" (the reverse quote, found usually in the key under the Esc in a PC keyboard). In the script you see Look at the my_simulation(0.01,5000,0.9,1); Keep in mind that you will need to distinguish output files from runs with different parameter values. One simple way is to print out parameter values in the beginning of your MATLAB code. You can also redirect the output of each job into a file that has the parameter values in its title to do that, replace the line with the matlab command in the example with: matlab > "sim_${P1}_${P2}_${P3}_${P4}.log" << M_PROG
In this case matlab output for the jobs in this examples will be saved in files named
sim_0.01_5000_0.9_1.log, sim_0.02_5000_0.7_2.log and sim_0.03_5000_0.6_3.log so matching parameter values and output files will be trivial.
You can add additional lines of MATLAB code between
matlab << M_PROG
my_simulation(${P1},${P2},${P3},${P4});
secondprogram(${P1},${P2});
M_PROG
Similar redirection is done for the The JobID = 3220.seldon for parameters 0.01 5000 0.9 1 submitted on Wed Aug 10 14:29:55 CDT 2005 JobID = 3221.seldon for parameters 0.02 5000 0.7 2 submitted on Wed Aug 10 14:29:55 CDT 2005 JobID = 3222.seldon for parameters 0.03 5000 0.6 3 submitted on Wed Aug 10 14:29:55 CDT 2005 Multiple job shell script for Stata programsBelow is a shell script for Stata that works in a similar fashion:
submit.txt (for a Stata program)
#!/bin/bash
while read P1 P2 P3
do
JOB=`qsub -m abe -N Big_run - << EOJ
cd ~/statacode
stata -b do simulation ${P1} ${P2} ${P3}
EOJ
`
echo "JobID = ${JOB} for parameters ${P1} ${P2} ${P3} ${P4} submitted on `date`"
done < params.txt
exit
This script also reads parameter values (3 on each line, separated by spaces) from a file called "params.txt" located in the same directory as the shell script. It passes the parameters to a stata program called
simulation.do
args P1 P2 P3
log using sim_`P1'_`P2'_`P3'.log
di `P1'
di `P2'
di `P3'
di `P1'+`P3'
...
log close
The first line of this do-file copies the text of the three arguments into Stata "macros" called P1, P2 and P3. Wherever Stata encounters `P1' or `P2' in the code it replaces it with the text of the first or second argument. Note that the name of the macro is enclosed in different quotes - use the "back tick" before the name and the single quote after the name.
The second line of the do-file saves the output to a log file that contains the parameters P1, P2 and P3 in its name. By default, on each run Stata would try saving the output to a file called "simulation.log" and you will lose output from all but one job if you do not instruct Stata to do otherwise. Running the shell scriptBefore running the script you need to make it executable. If you named the shell script "submit.txt" then type in To run the shell script which will submit your jobs type in Debugging shell scriptsWhen writing these scripts, make sure to test your script by using the JOB=`echo "qsub -m abe -N Big_run - << EOJ cd ~/statacode stata -b do simulation ${P1} ${P2} ${P3} EOJ " ` When this modified script is executed, the operating system simply will show the Monitoring and stopping jobsYou can monitor the status of your jobs in the queue by typing in Writing more advanced scriptsLearn more about writing shell scripts from these sources:
Any SSCC user: contact bef@northwestern.edu (Bruce Foster) for help. This page is a modification of instructions prepared by Patricia Ledesma Liébana at Kellogg Research Computing. |
![]() |
Services |
Get Connected |
Support |
Educational Resources |
NUIT
|
|