# Mathematica

Mathematica is a Wolfram product designed for modern technical computing. It’s built with robust algorithms capable of handling large-scale problems. Mathematica excels in areas such as neural networks, machine learning, and image processing.

### 0.0.1 Using Mathematica on CARC systems

Begin by logging in. You can find instructions for this in the Getting Started with Discovery or Getting Started with Endeavour user guides.

Mathematica can be used in either interactive or batch modes. In either mode, first load a corresponding software module:

`module load mathematica`

The Mathematica module is based on pre-build binaries provided by Wolfram so it has no prerequisite modules.

#### 0.0.1.1 Requesting newer versions of Mathematica

As Mathematica is licensed software, only CARC staff can install new versions. If you require a new version, please submit a help ticket and we will install it for you.

### 0.0.2 Running Mathematica in interactive mode

A common mistake for new users of HPC clusters is to run heavy workloads directly on a login node (e.g., `discovery.usc.edu`

or`endeavour.usc.edu`

). Unless you are only running a small test, please make sure to run your program as a job interactively on a compute node. Processes left running on login nodes may be terminated without warning. For more information on jobs, see our Running Jobs user guide.

There are multiple ways to run Mathematica interactively:

- Using the command line
- Using Open OnDemand to get the “familiar” Mathematica GUI
- Using Open OnDemand to run Mathematica in a Jupyter notebook.

#### 0.0.2.1 Using the command line

Before you can run Mathematica, you will need to request compute resources using `salloc`

.

```
[user@discovery2 ~]$ salloc --time=1:00:00 --ntasks=1 --cpus-per-task=8 --mem=16G --account=<project_id>
salloc: Pending job allocation 16888646
salloc: job 16888646 queued and waiting for resources
salloc: job 16888646 has been allocated resources
salloc: Granted job allocation 16888646
salloc: Waiting for resource configuration
salloc: Nodes e13-48 are ready for job
```

Make sure to change the resource requests (the `--time=1:00:00 --ntasks=1 --cpus-per-task=8 --mem=16G --account=<project_id>`

part after your salloc command) as needed, such as the number of cores and memory required. Also make sure to substitute your project ID; enter `myaccount`

to view your available project IDs.

Once you are granted the resources and logged in to a compute node, load the modules and then enter `math`

```
[user@e13-48 ~]$ module load mathematica
[user@e13-48 ~]$ math
Mathematica 12.3.0 Kernel for Linux x86 (64-bit)
Copyright 1988-2021 Wolfram Research, Inc.
In[1]:= Table[Sin[i],{i,-Pi,Pi,Pi/6}]
1 1 1 1 1 Sqrt[3]
Out[1]= {0, -(-), -(-) Sqrt[3], -1, -(-) Sqrt[3], -(-), 0, -, -------, 1,
2 2 2 2 2 2
Sqrt[3] 1
> -------, -, 0}
2 2
```

Note that the shell prompt changes from `user@discovery1`

to `user@<nodename>`

to indicate that you are now on a compute node (e.g., `e13-48`

).

To end the session, enter `Exit`

in the Mathematica interpreter and enter `exit`

in the shell. This will return you to the login node:

```
In[2]:= Exit
[user@e13-48 ~]$ exit
exit
salloc: Relinquishing job allocation 16888646
```

#### 0.0.2.2 Using Open OnDemand to get the “familiar” Mathematica GUI

Mathematica is available as an interactive app on Open OnDemand. Read more about running an interactive app here.

#### 0.0.2.3 Using Open OnDemand to run Mathematica in a Jupyter notebook

Although Mathematica natively runs as a notebook when used interactively, it can be desireable to run it within a Jupyter notebook. One of the main benefits is that it is more responsive.

To set up, you will need to install a Mathematica kernel which Jupyter can use to interpret and run Mathematica commands. This process is described on our Jupyter kernels page.

After the kernel has been installed successfully, follow our guide on starting up a Jupyter notebook in CARC OnDemand.

When the JupyterLab launches, create a new notebook and you should see a Mathematica option under the “Select Other Kernel” section.

### 0.0.3 Running Mathematica in batch mode

To use Mathematica in batch mode, there are a few steps to follow:

- Create a Mathematica script
- Create a Slurm job script that runs the Mathematica script
- Submit the job script to the job scheduler using
`sbatch`

Your Mathematica script should consist of the sequence of Mathematica commands needed for your analysis.

A Slurm job script is a special type of Bash shell script that the Slurm job scheduler recognizes as a job. For a job running Mathematica, a Slurm job script should look something like the following:

```
#!/bin/bash
#SBATCH --account=<project_id>
#SBATCH --partition=main
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=8
#SBATCH --mem=16G
#SBATCH --time=1:00:00
module purge
module load mathematica
./script.m
```

Each line is described below:

Command or Slurm argument | Meaning |
---|---|

`#!/bin/bash` |
Use Bash to execute this script |

`#SBATCH` |
Syntax that allows Slurm to read your requests (ignored by Bash) |

`--account=<project_id>` |
Charge compute resources used to <project_id>; enter `myaccount` to view your available project IDs |

`--partition=main` |
Submit job to the main partition |

`--nodes=1` |
Use 1 compute node |

`--ntasks=1` |
Run 1 task (e.g., running a Gurobi model) |

`--cpus-per-task=8` |
Reserve 8 CPUs for your exclusive use |

`--mem=16G` |
Reserve 16 GB of memory for your exclusive use |

`--time=1:00:00` |
Reserve resources described for 1 hour |

`module purge` |
Clear environment modules |

`module load mathematica` |
Load the `mathematica` environment module |

`./script.m` |
Run `script.m` |

Here is an example Mathematica script that will generate an image.

```
#!/usr/bin/env wolframscript
colorFunction[x_,y_,t_]:=Round[Mod[(x/10)^2y/10+(y/10)^2x/10+t,1],1/256]
size=50;
imgData[t_]:=Table[colorFunction[i,j,t],{j,1/2 size, 3.5/2 size,1},{i,-size 2/3, size 2/3}];
dt=1/5
img=ArrayPlot[imgData[dt*24], ImageSize -> 500, Frame -> None,
ColorFunction -> "Pastel"]
Export["plot.jpg",img]
```

You will need to enable “execute permissions” to this script like so:

`chmod +x script.m`

Make sure to adjust the resources requested based on your needs, but remember that fewer resources requested leads to less queue time for your job. Note that to fully utilize the resources, especially the number of cores, you may need to explicitly change your Mathematica code to do so.

You can develop Mathematica scripts and job scripts on your local machine and then transfer them to the cluster, or you can use one of the available text editor modules (e.g., `micro`

) to develop them directly on the cluster.

Save the job script as `mathematica.job`

, for example, and then submit it to the job scheduler with Slurm’s `sbatch`

command:

```
[user@discovery1 ~]$ sbatch mathematica.job
Submitted batch job 13587
```

To check the status of your job, enter `myqueue`

. If there is no job status listed, then this means the job has completed.

The results of the job will be logged and, by default, saved to a file of the form `slurm-<jobid>.out`

in the same directory where the job script is located. To view the contents of this file, enter `less slurm-<jobid>.out`

, and then enter `q`

to exit the viewer.

For more information on job status and running jobs, see the Running Jobs user guide.

### 0.0.4 Additional resources

If you have questions about or need help with Mathematica, please submit a help ticket and we will assist you.