assign parameters for cluster job with slurm sbatch - matlab

I am using slurm sbatch to parralel launch a matlab function on a cluster.
What is the proper syntax in my sbatch file in order to assign numeric parameters to the matlab function?
I have tried the following (and alike s):
#!/bin/bash
#SBATCH --partition=debug
#SBATCH --time=0-00:15:00
#SBATCH --cpus-per-task=12
#SBATCH -n1
VAR1=50
VAR2=40
BASE_MFILE_NAME=RUNAGT
MATLAB_MFILE=.m
srun --exclusive --cpus-per-task=12 matlab2013b/bin/matlab -nodesktop -nosplash -nodisplay -r "RUNAGT(${SLURM_ARRAY_TASK_ID},VAR1,VAR2);exit" -logfile testV${SLURM_ARRAY_TASK_ID}.log &
wait
${SLURM_ARRAY_TASK_ID} is working but matlab does not recognize VAR1 and VAR2.
error: Undefined function or variable 'VAR1'.

I believe it is not reading your variables because you are not putting a $ in front of them. You srun line should be:
srun --exclusive --cpus-per-task=12 matlab2013b/bin/matlab -nodesktop -nosplash -nodisplay -r "RUNAGT(${SLURM_ARRAY_TASK_ID},${VAR1},${VAR2});exit" -logfile testV${SLURM_ARRAY_TASK_ID}.log &

Related

Passing a parameter into a slurm script (matlab)

I am using slurm to submit jobs to the university supercomputer. My matlab function has one parameter:
function test(variable_1)
and my slurm file is (I am not sure if it is correct. I know how to define the value of the parameter in the slurm file, but I would like to pass the value to the slurm file as I need to run the matlab function many times with different values for the parameter):
#!/bin/bash -l
#SBATCH --time=2-00:00:00
#SBATCH --job-name="test"
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --mem=4gb
#SBATCH -p small
module load matlab/R2021a
matlab -nodisplay -nodesktop -nosplash -r "test variable_1"
I tried to use the following codes (for example, I would like to set variable_1=12) to submit the job but it did not work.
sbatch test.slurm 12
Can anyone help me with this issue? Thanks!
The first argument of the Bash script is stored in an environment variable named $1. So the last line of the script should be
matlab -nodisplay -nodesktop -nosplash -r "test $1"
Beware that if the argument is a number, you might need to first cast it to integer from string with str2num in your Matlab script.

Slurm task id as Matlab's function argument

I want to create a job array in slurm in a way such that it is called a Matlab function that depends on the array task id. I tried
#!/bin/bash
#SBATCH -J TEST
#SBATCH -p slims
#SBATCH -o o
#SBATCH -e e
matlab -r "test(${SLURM_ARRAY_TASK_ID})"
where test.m is the matlab function that I want to run. This throw the error "Not enough arguments in line 7 test.m ..."
How should I do it?
It looks like $SLURM_ARRAY_TASK_ID was not defined, and there is no --array parameter in your submission file. So unless you provided that argument on the command line
sbatch --array ... <yourscript.sh>
you did not tell Slurm to create an array.
Either add #SBATCH --array ... to your submission script or specify it on the command line.

Call sequential m-files batch file?

I want to write a batch file that calls sequentially 2 m-files. I want the second m-file (f.m) not to start before the end of the first m-file (main.m). The second m-file is repeated 4 times (4 tasks).
I have tried to use the wait option as below but it does not work.
#$ -S /bin/bash
#$ -l h_vmem=2G
#$ -l tmem=2G
#$ -cwd
#$ -j y
#$ -N example
#$ -t 1-4
echo "Task ID is $SGE_TASK_ID"
matlab -wait -nodisplay -nodesktop -nojvm -nosplash < main.m
matlab -nodisplay -nodesktop -nojvm -nosplash < f.m
What am I doing wrong?
You can either concatenate the contents of main.m and f.m together prior to piping them to MATLAB
cat main.m f.m | matlab -nodisplay -nodesktop -nojvm -nosplash
Or you can call the scripts by passing a command to MATLAB using the -r flag
matlab -nodisplay -nodesktop -nojvm -nosplash -r 'main; f; exit'
These are both going to be more performant than your approach since here we're only launching one instance of MATLAB.

running matlab script from command prompt not taking all parameters for startup.m

I run a script in folder
C:\SVN\myscripts\tst1.m
with following command on cmd.exe( Win 7)
"C:\Program Files\MATLAB\R2013b\bin\matlab.exe" -nosplash -nodesktop -wait -minimize -r "tst1"
This executes fine. However, as mentioned in the "help" document, I can specify multiple scripts/functions like
"C:\Program Files\MATLAB\R2013b\bin\matlab.exe" -nosplash -nodesktop -wait -minimize -r "tst1, exit"
This worked too.
However, if the same is done on startup.m file( my own file which sets some paths before calling some other Matlab Scripts) that is located as
C:\SVN\myscripts\StartupScript\startup.m
in following manner( after changing pwd to above folder):
"C:\Program Files\MATLAB\R2013b\bin\matlab.exe" -nosplash -nodesktop -wait -minimize -r "startup, exit"
The Startup.m does execute all its contents sucessfully, but at the end of it, I get following message :
Undefined function or variable 'startup'.
and Matlab returns the control to command prompt with non zero exit code.
Any reasons for this kind of behavior.
Thanks
sedy

How to call MATLAB from command-line and print to stdout before exiting

I'm trying to run MATLAB scripts from command-line and am having problems getting MATLAB to return results to stdout.
When running the following command, MATLAB prints the license banner and exits without printing the message.
matlab -nodisplay -nojvm -r "fprintf(1, 'value: %f\n', 2.0); quit;"
*note: I am currently running Version 7.10.0.499 (R2010a) 64-bit (maci64)
As was shown in this related post, you can use the -logfile option to make a copy of all outputs to a file.
matlab -nodisplay -nojvm -logfile out.txt -r "fprintf(1, 'value: %f\n', 2.0); quit;"
On Windows, use the -wait command-line options to block the execution of your script until MATLAB closes.
On Unix, you can use sleep 5s to sleep for 5 seconds, or use the wait command to pause execution until the process finishes:
#!/bin/sh
matlab -nodisplay -logfile out.txt -r "rand(3), quit"
wait $(ps | grep matlab | awk '{print $2}') && cat out.txt