Mathematica

Wolfram Mathmatica is a powerful technical computing software system used for mathematical, symbolic, and numerical calculations.  Mathematica is available in the cluster as a module or an OnDemand application.

Accessing as a module

To view the different versions of Mathematica available use the spidercommand.  

$ module spider mathematica
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 mathematica:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Versions:
       mathematica/13.0.1
       mathematica/14.1
       mathematica/14.3.0
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 For detailed information about a specific "mathematica" package (including how to load the modules) use the module's full name.
 Note that names that have a trailing (E) are extensions provided by other modules.
 For example:
    $ module spider mathematica/14.3.0

Accessing with Open OnDemand

To use Mathematica in a GUI environment, spin up an Interactive Desktop Session in OnDemand and then visit the menu in the top left.

Mathematica can be run in Interactive mode or batch mode.  To use Interactive mode the simplest solution is just starting it in a Nova OnDemand session.

To start Mathematica in a sbatch script use the following template:

#!/bin/bash 
#SBATCH --job-name=math_batch # Name of your job 
#SBATCH --output=math_output_%j.txt # Output log file (%j inserts the job ID) 
#SBATCH --error=math_error_%j.err # Error log file 
#SBATCH --nodes=1 # Number of nodes requested 
#SBATCH --ntasks-per-node=1 # Number of tasks 
#SBATCH --cpus-per-task=8 # Number of CPU cores for parallel subkernels 
#SBATCH --mem=16G # Total memory requested 
#SBATCH --time=02:00:00 # Time limit (hh:mm:ss) 

# 1. Load the Mathematica module (exact name varies by cluster) 
module load mathematica # 2. Run Mathematica in batch mode 

# Use -script to execute a Wolfram language script file 
math -noprompt -script my_script.wl

Using GPUs

First request a GPU

--gres=gpu:1

Next you'll need to load the CUDA module:

module load cuda

Use the following code snippet to verify that the GPU has been detected:

Needs["CUDALink`"] 
(* Check if your GPU is detected and supported (Should return True) *) 
CUDAQ[] 
(* Get detailed diagnostic information about your hardware and driver status *) 
CUDAInformation[] 
(* Test if the GPU can perform a basic computation *) 
CUDADot[Range[10], Range[10]]

Parallel Programming

Mathematica provides a comprehensive, built-in parallel computing micro-architecture that launches a single "master" kernel to control multiple background "subkernels". 

Parallel Computing Documentation

A common beginner trap is trying to use custom variables or functions inside a parallel loop and getting errors. Because subkernels run as completely separate processes, they do not know about variables defined in the master notebook unless you explicitly share them. 

An example program:

myFactor = 42;
myFunction[x_] := x * myFactor;

(* This ensures subkernels recognize 'myFunction' and 'myFactor' *)
DistributeDefinitions[myFunction, myFactor]; 

ParallelTable[myFunction[i], {i, 1, 10}]
Another example program:
(* 1. Define a heavy mathematical function on the master kernel *) 
heavyCalculation[x_] := Total[FactorInteger[x^5 + 3x^2 + 17]]; 

(* 2. CRITICAL: Distribute the function definition to all subkernels *) 
DistributeDefinitions[heavyCalculation]; 

(* 3. Run the computation in parallel *) 
parallelResult = AbsoluteTiming[ ParallelTable[heavyCalculation[i], {i, 10^5, 10^5 + 5000}] ]; 

(* 4. Compare it against standard sequential evaluation *) 
sequentialResult = AbsoluteTiming[ Table[heavyCalculation[i], {i, 10^5, 10^5 + 5000}] ]; 
Print["Parallel Time: ", parallelResult[[1]], " seconds."]; 
Print["Sequential Time: ", sequentialResult[[1]], " seconds."];