# Mathematica/Batch

When you run Mathematica on a HPC cluster, then there is a difference between commands that produce graphical output and commands that don't.

### Non-graphical commands in batch mode

In general you can submit a Mathematica batch job as

sbatch [Slurm options] --wrap="math -script input.m > output.out"

Here you need to replace *[Slurm options]* with Slurm parameters for the resource requirements of the job. Please find a documentation about the parameters of `sbatch` on the wiki page about the batch system. The single parts of the Mathematica command are explained in the table below.

Command | Description |
---|---|

math | calls the Mathematica kernel, which is used to execute jobs in batch mode |

-script | tells Mathematica that the job shall be executed in batch mode |

input.m | is the file that contains the Mathematica commands |

> output.out | redirects the output of the Mathematica kernel to output.out |

It is important that the whole mathematica command (red color) is enclosed within double quotes. Otherwise the redirection of the output fails and your output file will be empty, whereas the LSF file contains the output.

### Graphical commands in batch mode

The use of graphical commands as Plot or Display in batch mode is possible by exporting the plots as images. Since Mathematica expects a front end to display (even to export) graphics, we need to make Mathematica think that there is a graphical front end, by using a virtual frame buffer. The exporting of plots is done with the following commands:

b = Plot[....]; -> assign your plot to a variable and suppress output with a semicolone Export["my_nice_plot.eps", b] -> exports the image as .eps image (several other formats are also possible)

A batch job containing graphical commands is then submitted with gmath instead of math:

sbatch [Slurm parameters] --wrap="gmath -script input.m > output.out"

sbatch parameters are documented on the wiki page about using the batch system.