Mathematica

From ScientificComputing
Jump to: navigation, search

Please note that this application page is referring to the old CentOS software stack which is obsolete and does not work any more with the new Ubuntu setup. You can find an overview on the Ubuntu software stack on this wiki page.

Category

Mathematics

Description

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 (Euler, old software stack)

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

Please note that this page refers to installations from the old software stack. There are two software stacks on Euler. Newer versions of software are found in the new software stack.

Environment modules (Euler, old software stack)

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

Please note that this page refers to installations from the old software stack. There are two software stacks on Euler. Newer versions of software are found in the new software stack.

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
[sfux@eu-login-04 ~]$ module load mathematica/10.2.0
[sfux@eu-login-04 ~]$ 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

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.

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):

LaunchKernels[4];
...
Parallel Mathematica code
...
CloseKernels[]

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:

LaunchKernels[4];
a=Parallelize[Select[Range[60000],PrimeQ[2^# - 1]&]]
Print[a]
CloseKernels[];

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)

LaunchKernels[2];
sol = Parallelize[NDSolve[]]
CloseKernels[];

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

LaunchKernels[2];
f1[n_] = FactorInteger[(10^n-1)/9]
ParallelMap[f1,Range[1,1000]]
CloseKernels[];
Here the function f1[n] is evaluated in parallel for n running from 1 to 1000

Example

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
LaunchKernels[2];
a=Parallelize[Select[Range[500],PrimeQ[2^# - 1]&]]
Print[a]
CloseKernels[];
[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
JOBID      USER        STAT  QUEUE      FROM_HOST   EXEC_HOST   JOB_NAME   SUBMIT_TIME
25719610   leonhard    PEND  normal.4h  euler04
[leonhard@euler04 mathematica]$ bjobs
JOBID      USER        STAT  QUEUE      FROM_HOST   EXEC_HOST   JOB_NAME   SUBMIT_TIME
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)
https://www.wolfram.com/group-organization-licensing

Notes

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.

Links

http://www.wolfram.com/index.php

https://www.wolframalpha.com/
http://en.wikipedia.org/wiki/Mathematica