BwUniCluster2.0/Software/Mathematica: Difference between revisions

From bwHPC Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
{| style="width:100%; vertical-align:top; background:#f5fffa;border:1px solid #e7aa01;"
| style="padding:2px;" | <div style="margin:3px; background:#cef2e0; font-size:120%; font-weight:bold; border:1px solid #e7aa01; text-align:left; color:#000; padding:0.2em 0.4em;">Mathematica might not be available on all locations.</div>
|}


<!--{| align="right" {{Table|width=40%}} -->
<!--{| align="right" {{Table|width=40%}} -->
{|{{Softwarebox}}
{|{{Softwarebox}}
Line 28: Line 33:
| <!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger -->
| <!-- 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
'''Mathematica''' is a software from Wolfram for symbolic and numerical computation with many features
such as powerful visualization and application specific functions.
such as powerful visualization and application specific functions.


== Loading ==
== Loading ==
To check which versions of Mathematica are available execute
To check if Mathematica is available execute
module avail math/mathematica
module avail math


The default version can be loaded as usual with
If Mathematica is available you can load a specific version or you can load the default version with
module load math/mathematica
module load math/mathematica


Line 57: Line 63:
Obviously parallel computation can be useful to speed up a long running computation but it should also be used
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
when multiple computations with different input data are needed (e.g. for parametric studies). The reason for this
is the license model from Wolfram.
is the license model from Wolfram. There are two types of licenses.
There are two types of licenses. Each time an instance of Mathematica starts, a so called MathKernel license is used up.
* 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.
* Each time Mathematica spawns a subprocess, 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.
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 right amount of processors in your job script but note that Mathematica will not automatically use these processors.
Remember to request the right amount of processors in your job script but note that Mathematica will not automatically use these processors.
Line 86: Line 92:
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]]][[1]]} (* helper function *)
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 *)
Line 97: Line 103:
There are other functions similar to ParallelTable which are documented on the site
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.
http://reference.wolfram.com/mathematica/guide/ParallelComputing.html which provides general information on parallel computing with Mathematica.

[[Category:Mathematics software]]

Revision as of 17:14, 7 July 2014

Mathematica might not be available on all locations.


Mathematica
module load math/mathematica
License Commercial
Citing
Links Homepage; Documentation;
Graphical Interface Yes
Included in module

Mathematica is a software from Wolfram for symbolic and numerical computation with many features such as powerful visualization and application specific functions.

Loading

To check if Mathematica is available execute

module avail math

If Mathematica is available you can load a specific version or you can load the default version with

module load math/mathematica

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.

Interactive with command line:

./math

Interactive with GUI (needs X11 forwarding):

./mathematica

Non-Interatice:

./math < script.m

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.

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.
  • 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 right amount of processors in your job script but note that Mathematica will not automatically use these processors. In general you have to change 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 the numerical solution of 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.2}]
CloseKernels[]

Grid[Join[{{"Stepsize", "Time", "Error at 1/d"}}, Map[g, tbl]]]	(* print the result *)

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.