BwUniCluster2.0/Software/Mathematica: Difference between revisions
(Created page with "<!--{| align="right" {{Table|width=40%}} --> {|{{Softwarebox}} |- ! colspan="2" style="text-align:center" | Mathematica |- | module load | math/mathematica <!-- Neben CIS auc…") |
m (M Janczyk moved page Mathematica to BwUniCluster2.0/Software/Mathematica) |
||
(39 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
{{Softwarepage|math/mathematica}} |
|||
<!--{| align="right" {{Table|width=40%}} --> |
|||
{|{{Softwarebox}} |
|||
{| width=600px class="wikitable" |
|||
|- |
|- |
||
! Description !! Content |
|||
! colspan="2" style="text-align:center" | Mathematica |
|||
|- |
|- |
||
| module load |
| module load |
||
| math/mathematica |
| math/mathematica |
||
<!-- Neben CIS auch bereits über Kategorien abgedeckt |
|||
|- |
|||
| Availability |
|||
| [[bwUniCluster]] [[bwForFreiburg]], [[bwForMannheim-Heidelberg]], [[bwForUlm]], [[bwForTübingen]] |
|||
--> |
|||
|- |
|- |
||
| License |
| License |
||
| Commercial. See [http://www.wolfram.com/mathematica/pricing/ Mathematica Pricing]. |
|||
| Commercial |
|||
|- |
|- |
||
|Citing |
| Citing |
||
| |
| n/a |
||
|- |
|- |
||
| Links |
| Links |
||
| [http://www.wolfram.com/mathematica/ Homepage]; [http://www.wolfram.com/support/?source=nav Documentation] |
| [http://www.wolfram.com/mathematica/ Homepage] | [http://www.wolfram.com/support/?source=nav Documentation] |
||
| |
|||
|- |
|- |
||
| Graphical Interface |
| Graphical Interface |
||
| Yes (See [[VNC]]) |
|||
| Yes <!-- Yes could link to some x11-forwarding/vnc instructions --> |
|||
|- |
|||
| Included in module |
|||
| <!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --> |
|||
|} |
|} |
||
'''Mathematica''' is a software from Wolfram for symbolic and numerical computation with many features |
|||
such as powerful visualization and application specific functions. |
|||
= Description = |
|||
To check which versions of Mathematica are available execute |
|||
'''Mathematica''' is a software from Wolfram for symbolic and numerical computation with many features such as powerful visualization and application specific functions. |
|||
module avail math/mathematica |
|||
<br> |
|||
<br> |
|||
= Licenses = |
|||
Mathematica uses [[network license]]s from the home university of the user, not of the site where the user is calculating |
|||
== Users from Ulm == |
|||
Using Mathematica network licenses on the clusters is not supported, because Ulm has a very limited number of licenses that have to be reserved for interactive use. You can still run Mathematica compilations on the cluster by using the Mathematica "compile" function to compile a binary that can only calculate your specific problem and does not need licenses. |
|||
Instructions and an example on how to compile your binary can be found here: |
|||
* https://www.wolfram.com/mathematica/new-in-8/integrated-c-workflow/create-standalone-executables-using-compiled-funct.html |
|||
== Users from Freiburg == |
|||
Freiburg has enough licenses, so that Mathematica does not need to be compiled. But if you do, you are independent of a license server. |
|||
Instructions and an example on how to compile your binary can be found here: |
|||
* https://www.wolfram.com/mathematica/new-in-8/integrated-c-workflow/create-standalone-executables-using-compiled-funct.html |
|||
= General Usage = |
|||
The default version can be loaded as usual with |
|||
module load math/mathematica |
|||
Mathematica can run a script in batch mode which is useful when submitting batch jobs to the cluster. Alternatively , Mathematica can be used interactively on the command line or with a graphical front-end. |
|||
== General Usage == |
|||
Mathematica can be used interactively on the command line or with a graphical front-end. |
|||
Alternatively Mathematica can run a script in batch mode which is useful when submitting batch jobs to the cluster. |
|||
After loading Mathematica the different modes can be used as follows. |
After loading Mathematica the different modes can be used as follows. |
||
Interactive with |
*Interactive with GUI (needs [[X11 forwarding]] or [[VNC]]):<br /><pre>$ mathematica</pre> |
||
./math |
|||
Interactive with |
*Interactive with command line:<br /><pre>$ math</pre> |
||
./mathematica |
|||
*Non-Interactive. Input is taken from script.m:<br /><pre>$ math < script.m</pre> |
|||
Non-Interatice: |
|||
./math < script.m |
|||
*Non-Interactive. You have to explicitly specify what you want to print<br /><pre>$ math -script script.m</pre>However, the output is done in InputForm which is suitable for input in other Mathematica calculations but if you want pretty output you have to change the output format to OutputForm like this<br /><pre>SetOptions[$Output, FormatType -> OutputForm]</pre> |
|||
For an introduction to Mathematica we refer to the online documentation. Specific information on the use in a compute cluster is in the next section. |
|||
For an introduction to Mathematica we refer to the online documentation ([http://reference.wolfram.com/language/ Mathematica Documentation Center]). Specific information on the use in a compute cluster is in the next section. |
|||
== Parallel Computation == |
|||
Obviously parallel computation can be useful to speed up a long running computation but it should also be used |
|||
when multiple computations with different input data are needed (e.g. for parametric studies). The reason for this |
|||
is the license model from Wolfram. |
|||
There are two types of licenses. Each time an instance of Mathematica starts, a so called MathKernel license is used up. |
|||
For each subprocesses Mathematica spawns, a license called SubMathKernel is used up. |
|||
Because we have only a small number of MathKernel licenses it is necessary to start many subprocesses instead of submitting many jobs. |
|||
Remember to request the right amount of processors in your job script but note that Mathematica will not automatically use these processors. |
|||
= Parallel Computation = |
|||
In general you have to change your code to benefit from more cores. |
|||
== Implicit Multithreading == |
|||
Some functions are already internally multithreaded by using the built-in MKL library. To benefit from this you have to set the environment variable MKL_NUM_THREADS to the number of cores you requested for your job. The effect can be seen in the following example. |
|||
<pre> |
|||
a = RandomReal[{1, 2}, {5000, 5000}] |
|||
b = RandomReal[1, {5000}] |
|||
Print[ First[AbsoluteTiming[LinearSolve[a, b]]] ] |
|||
</pre> |
|||
When you call this example in a job file with |
|||
<pre> |
|||
module load math/mathematica |
|||
export MKL_NUM_THREADS=2 |
|||
math -script example.m |
|||
</pre> |
|||
you should see that it executes about twice as fast as without the setting of MKL_NUM_THREADS. |
|||
If you want to turn on MKL threading just for a specific part of the code you can use the command (16-core example) |
|||
<pre> |
|||
SetSystemOptions["ParallelOptions" -> "MKLThreadNumber" -> 16] |
|||
</pre> |
|||
to turn on multithreading and later turn it off (e.g. because of memory requirements) with a corresponding call to SetSystemOptions. |
|||
== ParallelTable == |
|||
Obviously parallel computation can be useful to speed up time-consuming computations, but it should also be used for multiple computations with different input data files (e.g. for parametric studies). The reason for this |
|||
is the license model from Wolfram. There are two types of licenses. |
|||
* Each time an instance of Mathematica starts, a so called MathKernel license is used up. |
|||
* Each time Mathematica spawns a subprocess, a license called SubMathKernel is used up. |
|||
Because usually there are much more SubMathKernel licenses than MathKernel licenses it is recommended to start multiple subprocesses instead of submitting multiple jobs. |
|||
Remember to request the correct amount of processors in your [[Batch Jobs]] script but note that Mathematica will not automatically use these processors. |
|||
In general you have to adjust your code to benefit from more cores. |
|||
To do this you first have to start a number of kernels which are then used by ParallelTable to run the computations in parallel. |
To do this you first have to start a number of kernels which are then used by ParallelTable to run the computations in parallel. |
||
This basic example computes the first eight square numbers in parallel. |
This basic example computes the first eight square numbers in parallel. |
||
Line 75: | Line 107: | ||
Note that the use of DistributeDefinitions is necessary for f is a user defined function and the definition of this function must be available to all kernels. |
Note that the use of DistributeDefinitions is necessary for f is a user defined function and the definition of this function must be available to all kernels. |
||
The next example is the computation of |
The next example is the computation of a numerical solution for the following initial value problem |
||
x'(t) = x |
x'(t) = x(t)^2 - x(t)^3 |
||
x(0) = d |
x(0) = d |
||
It is difficult to solve this equation with high accuracy at the point 1/d. |
It is difficult to solve this equation with high accuracy at the point 1/d. |
||
Line 85: | Line 117: | ||
y[t_] := 1/(ProductLog[(1/d-1)*Exp[1/d-1-t]]+1) (* analytical solution *) |
y[t_] := 1/(ProductLog[(1/d-1)*Exp[1/d-1-t]]+1) (* analytical solution *) |
||
relerr[t_, s_] := Abs[(y[t] - x[t]/.s)/y[t]] (* relative error of solution s at time t*) |
relerr[t_, s_] := Abs[(y[t] - x[t]/.s)/y[t]] (* relative error of solution s at time t *) |
||
g[v_] := {v[[1]], v[[2]][[1]], relerr[1/d, v[[2]][[2 |
g[v_] := {v[[1]], v[[2]][[1]], relerr[1/d, v[[2]][[2]][[1]]]} (* helper function *) |
||
(* compute numerical solutions for 6 different step sizes *) |
(* compute numerical solutions for 6 different step sizes *) |
||
LaunchKernels[6] |
LaunchKernels[6] |
||
tbl = ParallelTable[{step, Timing[NDSolve[{x'[t] == x[t]^2 - x[t]^3, x[0] == d}, x, {t, 0, 2/d}, MaxStepSize->step, MaxSteps->10000000]]}, {step,5,3,-0. |
tbl = ParallelTable[{step, Timing[NDSolve[{x'[t] == x[t]^2 - x[t]^3, x[0] == d}, x, {t, 0, 2/d}, MaxStepSize->step, MaxSteps->10000000]]}, {step,5,3,-0.4}] |
||
CloseKernels[] |
CloseKernels[] |
||
SetOptions[$Output, FormatType -> OutputForm] (* better output when called with -script *) |
|||
Grid[Join[{{"Stepsize", "Time", "Error at 1/d"}}, Map[g, tbl]]] (* print the result *) |
|||
Print[ Grid[Join[{{"Stepsize", "Time", "Error at 1/d"}}, Map[g, tbl]]] ] (* print the result *) |
|||
</pre> |
|||
== ParallelSubmit == |
|||
Mathematica features some further functions similar to ParallelTable which are documented on the [http://reference.wolfram.com/mathematica/guide/ParallelComputing.html Mathematica parallel computing webpage] from Wolfram. One such function is ParallelSubmit which can be used to start two (or more) completely unrelated functions in parallel. But bear in mind that the functions should have similar running times because the scheduler reserves the requested resources for the runtime of the whole job. The following code shows how to pack two 1-core jobs into one 2-core job. |
|||
<pre> |
|||
f[r_] := ( |
|||
d = 0.000001; |
|||
{time, sol} = Timing[NDSolve[{x'[t] == r*x[t]^2 - x[t]^3, x[0] == d}, x, {t, 0, 2/d}, MaxStepSize->3, MaxSteps->10000000]]; |
|||
{time, x[1/d]/.sol[[1]]} |
|||
) |
|||
g[r_] := ( |
|||
{time, sol} = Timing[NDSolve[{y''[t] + (r+1)*y'[t] + r*y[t] == 0, y[0] == 1, y'[0] == 0}, y, {t, 0, 30}, MaxStepSize->0.000002, MaxSteps->10000000000]]; |
|||
{time, y[30]/.sol[[1]]} |
|||
) |
|||
kernel1 := ( |
|||
file = OpenWrite["out1.txt", FormatType -> OutputForm]; |
|||
$Output = {file}; |
|||
Print[f[1.2]]; |
|||
Close[file]; |
|||
) |
|||
kernel2 := ( |
|||
file = OpenWrite["out2.txt", FormatType -> OutputForm]; |
|||
$Output = {file}; |
|||
Print[g[1000]]; |
|||
Close[file]; |
|||
) |
|||
LaunchKernels[2] |
|||
DistributeDefinitions[kernel1, kernel2] |
|||
e = {ParallelSubmit[kernel1], ParallelSubmit[kernel2]} |
|||
WaitAll[e] |
|||
</pre> |
</pre> |
||
The helper functions kernel1 and kernel2 get started in different threads and then they execute f and g. The output of these functions is redirected to different files so the results can clearly be distinguished and examined as soon as one function is done. |
|||
There are other functions similar to ParallelTable which are documented on the site |
|||
http://reference.wolfram.com/mathematica/guide/ParallelComputing.html which provides general information on parallel computing with Mathematica. |
Latest revision as of 11:58, 14 March 2023
The main documentation is available via |
Description | Content |
---|---|
module load | math/mathematica |
License | Commercial. See Mathematica Pricing. |
Citing | n/a |
Links | Homepage | Documentation |
Graphical Interface | Yes (See VNC) |
Description
Mathematica is a software from Wolfram for symbolic and numerical computation with many features such as powerful visualization and application specific functions.
Licenses
Mathematica uses network licenses from the home university of the user, not of the site where the user is calculating
Users from Ulm
Using Mathematica network licenses on the clusters is not supported, because Ulm has a very limited number of licenses that have to be reserved for interactive use. You can still run Mathematica compilations on the cluster by using the Mathematica "compile" function to compile a binary that can only calculate your specific problem and does not need licenses.
Instructions and an example on how to compile your binary can be found here:
Users from Freiburg
Freiburg has enough licenses, so that Mathematica does not need to be compiled. But if you do, you are independent of a license server.
Instructions and an example on how to compile your binary can be found here:
General Usage
Mathematica can run a script in batch mode which is useful when submitting batch jobs to the cluster. Alternatively , Mathematica can be used interactively on the command line or with a graphical front-end. After loading Mathematica the different modes can be used as follows.
- Interactive with GUI (needs X11 forwarding or VNC):
$ mathematica
- Interactive with command line:
$ math
- Non-Interactive. Input is taken from script.m:
$ math < script.m
- Non-Interactive. You have to explicitly specify what you want to print
$ math -script script.m
However, the output is done in InputForm which is suitable for input in other Mathematica calculations but if you want pretty output you have to change the output format to OutputForm like thisSetOptions[$Output, FormatType -> OutputForm]
For an introduction to Mathematica we refer to the online documentation (Mathematica Documentation Center). Specific information on the use in a compute cluster is in the next section.
Parallel Computation
Implicit Multithreading
Some functions are already internally multithreaded by using the built-in MKL library. To benefit from this you have to set the environment variable MKL_NUM_THREADS to the number of cores you requested for your job. The effect can be seen in the following example.
a = RandomReal[{1, 2}, {5000, 5000}] b = RandomReal[1, {5000}] Print[ First[AbsoluteTiming[LinearSolve[a, b]]] ]
When you call this example in a job file with
module load math/mathematica export MKL_NUM_THREADS=2 math -script example.m
you should see that it executes about twice as fast as without the setting of MKL_NUM_THREADS.
If you want to turn on MKL threading just for a specific part of the code you can use the command (16-core example)
SetSystemOptions["ParallelOptions" -> "MKLThreadNumber" -> 16]
to turn on multithreading and later turn it off (e.g. because of memory requirements) with a corresponding call to SetSystemOptions.
ParallelTable
Obviously parallel computation can be useful to speed up time-consuming computations, but it should also be used for multiple computations with different input data files (e.g. for parametric studies). The reason for this is the license model from Wolfram. There are two types of licenses.
- Each time an instance of Mathematica starts, a so called MathKernel license is used up.
- Each time Mathematica spawns a subprocess, a license called SubMathKernel is used up.
Because usually there are much more SubMathKernel licenses than MathKernel licenses it is recommended to start multiple subprocesses instead of submitting multiple jobs.
Remember to request the correct amount of processors in your Batch Jobs script but note that Mathematica will not automatically use these processors. In general you have to adjust your code to benefit from more cores. To do this you first have to start a number of kernels which are then used by ParallelTable to run the computations in parallel. This basic example computes the first eight square numbers in parallel.
LaunchKernels[8] f[i_] := i^2 DistributeDefinitions[f] ParallelTable[f[i], {i,0,7}] CloseKernels[]
Note that the use of DistributeDefinitions is necessary for f is a user defined function and the definition of this function must be available to all kernels.
The next example is the computation of a numerical solution for the following initial value problem
x'(t) = x(t)^2 - x(t)^3 x(0) = d
It is difficult to solve this equation with high accuracy at the point 1/d. We decrease the step size of the algorithm to see how the execution time and the relative error at the point 1/d change.
d = 0.00001 y[t_] := 1/(ProductLog[(1/d-1)*Exp[1/d-1-t]]+1) (* analytical solution *) relerr[t_, s_] := Abs[(y[t] - x[t]/.s)/y[t]] (* relative error of solution s at time t *) g[v_] := {v[[1]], v[[2]][[1]], relerr[1/d, v[[2]][[2]][[1]]]} (* helper function *) (* compute numerical solutions for 6 different step sizes *) LaunchKernels[6] tbl = ParallelTable[{step, Timing[NDSolve[{x'[t] == x[t]^2 - x[t]^3, x[0] == d}, x, {t, 0, 2/d}, MaxStepSize->step, MaxSteps->10000000]]}, {step,5,3,-0.4}] CloseKernels[] SetOptions[$Output, FormatType -> OutputForm] (* better output when called with -script *) Print[ Grid[Join[{{"Stepsize", "Time", "Error at 1/d"}}, Map[g, tbl]]] ] (* print the result *)
ParallelSubmit
Mathematica features some further functions similar to ParallelTable which are documented on the Mathematica parallel computing webpage from Wolfram. One such function is ParallelSubmit which can be used to start two (or more) completely unrelated functions in parallel. But bear in mind that the functions should have similar running times because the scheduler reserves the requested resources for the runtime of the whole job. The following code shows how to pack two 1-core jobs into one 2-core job.
f[r_] := ( d = 0.000001; {time, sol} = Timing[NDSolve[{x'[t] == r*x[t]^2 - x[t]^3, x[0] == d}, x, {t, 0, 2/d}, MaxStepSize->3, MaxSteps->10000000]]; {time, x[1/d]/.sol[[1]]} ) g[r_] := ( {time, sol} = Timing[NDSolve[{y''[t] + (r+1)*y'[t] + r*y[t] == 0, y[0] == 1, y'[0] == 0}, y, {t, 0, 30}, MaxStepSize->0.000002, MaxSteps->10000000000]]; {time, y[30]/.sol[[1]]} ) kernel1 := ( file = OpenWrite["out1.txt", FormatType -> OutputForm]; $Output = {file}; Print[f[1.2]]; Close[file]; ) kernel2 := ( file = OpenWrite["out2.txt", FormatType -> OutputForm]; $Output = {file}; Print[g[1000]]; Close[file]; ) LaunchKernels[2] DistributeDefinitions[kernel1, kernel2] e = {ParallelSubmit[kernel1], ParallelSubmit[kernel2]} WaitAll[e]
The helper functions kernel1 and kernel2 get started in different threads and then they execute f and g. The output of these functions is redirected to different files so the results can clearly be distinguished and examined as soon as one function is done.