From ScientificComputing
Jump to: navigation, search




Wolfram Mathematica (sometimes referred to as Mathematica) is a symbolic mathematical computation program, sometimes called a computer algebra program, used in many scientific, engineering, mathematical, and computing fields. It was conceived by Stephen Wolfram and is developed by Wolfram Research of Champaign, Illinois. The Wolfram Language is the programming language used in Mathematica.

Available versions

Legacy versions Supported versions New versions
9.0.1 10.0.2, 10.2.0, 11.1.1

Environment modules

Version Module load command Additional modules loaded automatically
9.0.1 module load legacy mathematica/9.0.1
10.0.2 module load mathematica/10.0.2
10.2.0 module load mathematica/10.2.0
11.1.1 module load mathematica/11.1.1

Interactive session

For starting an interactive Mathematica session on the login node, you first need to load the Mathematica module. Afterwards you can start the application with the command math
[leonhard@euler04 ~]$ module load mathematica/10.2.0
[leonhard@euler04 ~]$ math
Mathematica 10.2.0 for Linux x86 (64-bit)
Copyright 1988-2015 Wolfram Research, Inc.

In[1]:= N[E,100]                                                                                                              

Out[1]= 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427
Please only do small tests in interactive sessions. Larger computations must be submitted as a batch job.

How to submit a job

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

bsub [LSF options] "math -script input.m > output.out"

Here you need to replace [LSF options] with LSF parameters for the resource requirements of the job. Please find a documentation about the parameters of bsub 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:

bsub [LSF parameters] "gmath -script input.m > output.out"
LSF parameters are documented on the wiki page about using the batch system.

Parallel jobs

Note: If you start multiple instances of Mathematica, you have to request the same amount of cores from the batch system with the bsub option -n

Note2: Do not request more than the maximal number of cores of a single compute node, since Mathematica will start all Kernels on a single node. And before requesting the maximal number of cores, please test the scaling behavior of your code on 4 cores to see if it scales well.

With Mathematica it is possible to run a job in parallel, furthermore you can decide on your own, which functions are going to be parallelized. The standard procedure to run mathematica in parallel, invokes that you start at the beginning of your script a distinct number of Mathematica instances with the LaunchKernel[] command. Please do not forget to kill these Mathematica instances at the end of your script with the CloseKernels[] command. A typical parallel Mathematica script could look as follows (here we would like to run on 4 cores):

Parallel Mathematica code

In Mathematica, some commands can be run in parallel by using the Parallelize[] command. As a test example to demonstrate the parallel computing abilities of Mathematica, we calculate Mersenne prime numbers. With

Select[Range[60000],PrimeQ[2^# - 1]&]

Mathematica checks if 2^x-1 is a prime number for x running from 1 to 60000 (don't try this on a single cpu machine, it could take about 18 hours). For a parallel evaluation of the command, one needs to wrapp a Parallelize[] command around the code. This looks in the test case like:

a=Parallelize[Select[Range[60000],PrimeQ[2^# - 1]&]]

But, be aware that not all functions in Mathematica can be parallelized ! So please check first if the function you would like to use in a parallel Mathematica script can be parallelized at all. This can be achieved by a simple test script as it is shown for NDSolve[]. Enclose the function in a Parallelize[] command (for the test, you do not even have to put all the arguments of the function)

sol = Parallelize[NDSolve[]]

gives the following error message:

Parallelize::nopar1: NDSolve[] cannot be parallelized; proceeding with sequential evaluation.

If you receive this error message for the function that you would like evaluate in parallel, use only a single core.

For running parameter sweeps in parallel, you can also use the ParallelMap[] command instead of using the Parallelize[] command. As an example, we define a function that factorizes integers of the form (10^n-1)/9

f1[n_] = FactorInteger[(10^n-1)/9]
Here the function f1[n] is evaluated in parallel for n running from 1 to 1000


As an example for running a Mathematica job on the cluster, we look at numbers of the form 2^x-1 and check if they are prime numbers.
[leonhard@euler04 mathematica]$ cat test.m
a=Parallelize[Select[Range[500],PrimeQ[2^# - 1]&]]
[leonhard@euler04 mathematica]$ module load mathematica/10.2.0
[leonhard@euler04 mathematica]$ bsub -n 2 -W 4:00 -R "rusage[mem=512]" "math -script test.m > output.out"
Mathematica job.
Job <25719610> is submitted to queue <normal.4h>.
[leonhard@euler04 mathematica]$ bjobs
25719610   leonhard    PEND  normal.4h  euler04
[leonhard@euler04 mathematica]$ bjobs
25719610   leonhard    RUN   normal.4h  euler04     2*e2248     *utput.out Aug 25 08:31
[leonhard@euler04 mathematica]$ bjobs
No unfinished job found
[leonhard@euler04 mathematica]$ cat output.out 
{2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127}
The output lists the exponents of all Mersenne prime number smaller than 2^{500}.

License information

Commercial (centrally provided by IT shop)


Note that in batch mode, Mathematica notebooks can not be used, because the Mathematica kernel expects a plain text file with Mathematica commands. These plain text files are called script file and their name ends with .m (for instance input.m). The output has then to be redirected to an output file. In interactive mode, you type an expression, and the result is immediately displayed on your screen. In batch mode you have ensure that Mathematica writes labels and results to your output file. Therefore you need to assign your expression to a variable and use afterwards the Print[] command.
a = ... [expression] ...
Print["a = "]           -> prints a label
Print[a]                -> prints the value of a
You can also use graphical commands as Plot or Display in the batch mode. This is discussed below, but first it is explained, how to use Mathematica in batch mode, when no graphical commands are involved.