# Mathematica

To submit Mathematica jobs to compute nodes using SLURM, the Mathematica command to be executed must be contained in a single `.m` script. The `.m` script will then be passed to the`math`command in a batch script. Please note that, currently, we support only in a batch mode, not an interactive mode.

It is also important to note that we currently have 2 licenses of Mathematica for a cluster. We then limit the total job to run to 2. You must choose `qos=cu_math` and `partition=math` in your SLURM script.

An example of `math-1core.m`,

```markup
Pause[600];
A = Sum[i, {i,1,100}]
B = Mean[{25, 36, 22, 16, 8, 42}]
Answer = A + B
Quit[];
```

And here is an example of the SLURM script (`example7a.slurm`),

```bash
#!/bin/bash
#
#SBATCH --qos=cu_math
#SBATCH --partition=math
#SBATCH --job-name=example7a
#SBATCH --output=example7a.txt
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --time=00:10:00
#SBATCH --cpus-per-task=1
#SBATCH --mem-per-cpu=10G

module purge

#To run python script
/usr/local/bin/math -run < math-1core.m
```

Job submission is done in the same way as other SLURM jobs, e.g. using`sbatch example7a.slurm`.

#### Multiple CPUs

For multiple CPUs in a machine, Mathematica can be run in parallel using the built in Parallel commands or by utilizing the parallel API. Note that, the Parallel Mathematica jobs are limited to one node, but can utilize all CPU cores on the node. You can use 16 CPUs maximum.

\
Here we request and use 16 cores:

```markup
(*Limits Mathematica to requested resources*)
Unprotect[$ProcessorCount];$ProcessorCount = 16;

(*Prints the machine name that each kernel is running on*)
Print[ParallelEvaluate[$MachineName]];

(*Prints all Mersenne Prime numbers less than 2000*)
Print[Parallelize[Select[Range[2000],PrimeQ[2^#-1]&]]];
```

and an example of submission script,

```bash
#!/bin/bash
#
#SBATCH --qos=cu_math
#SBATCH --partition=math
#SBATCH --job-name=example7b
#SBATCH --output=example7b.txt
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --time=00:10:00
#SBATCH --cpus-per-task=16
#SBATCH --mem-per-cpu=2G

module purge

#To run python script
/usr/local/bin/math -run < math-parallel.m
```

#### Using Lightweight grid mathematica

Under construction.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://esciencecu-twiki.sc.chula.ac.th/slurm/slurm-examples/slurm-mathematica.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
