Evaluating Mathematica notebooks
Mathematica scripts (files with extension .m
can be easily evaluated with the command
math -script file.m
On the other hand, evaluating notebooks in batch mode is more complicated.
The following instructions somehow allow to evaluate a notebook using sbatch
.
It is necessary to prepare the script and the notebook in the proper way to have
it working correctly.
Preparing the notebook
For some reason the output saved in batch mode is not readable unless the following modifications are made to the notebook:
-
At the top of the notebook, define the function
Report = CellPrint[ExpressionCell[#, "Output"]]&
-
Postpone the function to each cell that produces output. For instance
Plot[Sin[x],{x, 0, 2 Pi}] // Report
Alternatively, it is possible to export plots and data using the Export[]
function.
Parallel kernels
WARNING Parallel evaluation in batch mode can have problems. Please test your notebook with a reasonably-sized problem to make sure that everything is working, before running long computations.
If the notebook employs parallel functions like Parallelize
or ParallelTable
.
The following steps must be taken.
-
Add these lines to the head of the notebook. This will launch as many kernels as the number of cores requested for the job with the
-c
argument.NumberOfKernels = ToExpression@Environment["SLURM_CPUS_PER_TASK"]; LaunchKernels[NumberOfKernels]
-
At the end of the notebook, add the following code to close the kernels:
CloseKernels[];
The script
Here is an example script that allows to run a job that evaluates a Mathematica notebook.
Please save the file as, e.g. mathematica_script.sh
, and run it with the command
sbatch mathematica_script.sh
Please modify it as needed, following the instructions in the comments.
#!/bin/bash
# This script evaluates a Mathematica notebook in batch mode using SLURM queue manager
#
# Modify the following lines as instructed in the comments, then run this script with
# the command `sbatch mathematica_script.sh`
#
#
#############################################
# PLEASE WRITE <username> and <jobname>
#############################################
#
#SBATCH -A <username>
#############################################
# Adjust number of cores (equal to the number of parallel kernels)
#############################################
#SBATCH -c 1
#SBATCH --job-name=<jobname>
#############################################
# Adjust mail info
#############################################
#SBATCH --mail-user=nome.cognome@dominio.it
#SBATCH --mail-type=ALL # Possible values BEGIN,END,FAIL
#############################################
# PLEASE WRITE the notebook name
#############################################
NOTEBOOK_FILE=test.nb
#############################################
# Do not change from here
#############################################
TEMP_SCRIPT_NAME=mathematica_batch.m
# Redirect to the Xvfb virtual display
export DISPLAY=:666
# Create a temporary Mathematica script that issues the evaluation of the notebook
echo "UsingFrontEnd[\$Messages = Append[\$Messages, Streams[][[2]]];NotebookEvaluate[\"$NOTEBOOK_FILE\", InsertResults -> True]]" > mathematica_batch.m
echo "Evaluating notebook. Error messages will be written to stderr"
# Launch the script
srun math -script "$TEMP_SCRIPT_NAME"
echo ""
echo "Notebook evalution complete..."
# Once the execution is complete, delete the script
rm mathematica_batch.m
When the job is complete, the notebook will be saved with the output evaluated.