Mathematica¶
Mathematica is a computational suite used for science, engineering, and mathematics and provides an array of technical functionality computing.
Mathematica is available as a module on Apocrita.
Usage¶
To run the default installed version of Mathematica, simply load the
mathematica
module:
module load mathematica
then run the math
command:
# Interactive
math
# Batch
math -script example.m
First use of package download
The first time a command is run from a package there may be additional
downloads required to use the package. If this is the case, Mathematica
will automatically download the required resources whilst displaying the
following message:
Loading from Wolfram Research server ...
Core usage¶
To ensure that Mathematica uses the correct number of cores, the following should be put at the top of your input file:
(* Get slot count and convert from string *)
Unprotect[$ProcessorCount];
$ProcessorCount = ToExpression[Environment["NSLOTS"]];
$ProcessorCount = $ProcessorCount - 1; (* Minus 1 for master process *)
Print["Processors: ", $ProcessorCount];
SetSystemOptions["ParallelOptions" -> "ParallelThreadNumber" -> $ProcessorCount];
SetSystemOptions["ParallelOptions" -> "MKLThreadNumber" -> $ProcessorCount];
LaunchKernels[$ProcessorCount];
Example jobs¶
Serial job¶
Here is an example job running on 4 cores:
#!/bin/bash
#$ -cwd
#$ -j y
#$ -pe smp 4
#$ -l h_rt=1:0:0
#$ -l h_vmem=1G
module load mathematica
math -script example.m
And the file example.m
:
(* Get slot count and convert from string *)
Unprotect[$ProcessorCount];
$ProcessorCount = ToExpression[Environment["NSLOTS"]];
$ProcessorCount = $ProcessorCount - 1; (* Minus 1 for master process *)
Print["Processors: ", $ProcessorCount];
SetSystemOptions["ParallelOptions" -> "ParallelThreadNumber" -> $ProcessorCount];
SetSystemOptions["ParallelOptions" -> "MKLThreadNumber" -> $ProcessorCount];
LaunchKernels[$ProcessorCount];
Print["Kernels: ", $KernelCount];
(* Generate table of primes *)
primeT = ParallelTable[Prime[i], {i, 10000000}];
Print["Size of Prime Table: ", ByteCount[primeT], " Bytes"];
(* Generate random matrix *)
randM = RandomReal[1, {4000, 4000}];
Print["Size of Random Matrix: ", ByteCount[randM], " Bytes"];
(* Multiply Matrix *)
Print["Multiplication Time: ", AbsoluteTiming[randM.randM;]];
GPU job¶
Here is an example job running on 8 cores and 1 GPU.
#!/bin/bash
#$ -cwd
#$ -j y
#$ -pe smp 12
#$ -l h_rt=240:0:0
#$ -l h_vmem=7.5G
#$ -l gpu=1
export CUDA_LIBRARY_PATH=/usr/lib64/nvidia/libcuda.so
module load mathematica
math -script gpu_example.m
And the file gpu_example.m
:
(* Get slot count and convert from string *)
Unprotect[$ProcessorCount];
$ProcessorCount = ToExpression[Environment["NSLOTS"]];
$ProcessorCount = $ProcessorCount - 1; (* Minus 1 for master process *)
Print["Processors: ", $ProcessorCount];
SetSystemOptions["ParallelOptions" -> "ParallelThreadNumber" -> $ProcessorCount];
SetSystemOptions["ParallelOptions" -> "MKLThreadNumber" -> $ProcessorCount];
LaunchKernels[$ProcessorCount];
Print["Kernels: ", $KernelCount];
Needs["CUDALink`"];
CUDAInformation[];
(* Generate random matrix *)
randM = RandomReal[1, {5000, 5000}];
Print["Size of Random Matrix: ", ByteCount[randM], " Bytes"];
(* Multiply Matrix (CPU) *)
Print["Multiplication Time: ", AbsoluteTiming[randM.randM;]];
(* Multiply Matrix (GPU) *)
Print["GPU Multiplication Time: ", AbsoluteTiming[CUDADot[randM, randM];]]
(* Load Matrix into GPU memory *)
randMG = CUDAMemoryLoad[randM]
(* Multiply Matrix (GPU in memory) *)
Print["GPU Multiplication Time (mem): ", AbsoluteTiming[CUDADot[randM, randM];]]