In Linux, you could let a bash script write out MATLAB scripts which can then be executed in parallel. You can just use the ampersand (&
) for that after each MATLAB call, but the GNU parallel software is better: you can then specify how many jobs will run in parallel.
This bash script
#!/bin/bash
# command line argument: how many scripts (jobs) in parallel?
if [[ ${1} == "" ]]; then
echo "${0} needs a parameter: N == how many scripts are made 0,1,2 ..."
exit 1
fi
N=${1};
echo "creating and running ${N} scripts ..."
# some constants
c_dir=$(pwd)
ml_ex=$(which matlab)
# create the scripts
for (( i=1; i <= ${N}; i++ )); do
cat << EOF > ${c_dir}/script${i}.m
a = ones (${i}) * $i
EOF
done
# list them, then pass this list to parallel
for f in ${c_dir}/script*.m; do
echo "${ml_ex} < $f"
done | parallel -j ${N};
# tidy up
rm -f ${c_dir}/script*.m
makes N
MATLAB scripts (N
is the command line parameter) and executes them in MATLAB in parallel. Each script should show a M
xM
matrix filled with the number M
(for M
= 1,2, ... N
). So the command runsN.sh 5
runs 5 copies of MATLAB at the same time.
Instead of ${ml_ex}
in the script, ${ml_ex} -nodesktop -nosplash
shows more clearly what happens. I have an alias to always use those options.
This maybe worth trying if you have a number of time-consuming, not very resource-demanding, completely independent jobs. I have used it for image processing.