Mathematica/Parallel

From ScientificComputing
Revision as of 07:47, 25 August 2016 by Sfux (talk | contribs) (Created page with "<font color="red">Note: If you start multiple instances of Mathematica, you have to request the same amount of cores from the batch system with the bsub option -n</font> <fon...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Note: If you start multiple instances of Mathematica, you have to request the same amount of cores from the batch system with the bsub option -n

Note2: Do not request more than the maximal number of cores of a single compute node, since Mathematica will start all Kernels on a single node. And before requesting the maximal number of cores, please test the scaling behavior of your code on 4 cores to see if it scales well.

With Mathematica it is possible to run a job in parallel, furthermore you can decide on your own, which functions are going to be parallelized. The standard procedure to run mathematica in parallel, invokes that you start at the beginning of your script a distinct number of Mathematica instances with the LaunchKernel[] command. Please do not forget to kill these Mathematica instances at the end of your script with the CloseKernels[] command. A typical parallel Mathematica script could look as follows (here we would like to run on 4 cores):

LaunchKernels[4];
...
Parallel Mathematica code
...
CloseKernels[]

In Mathematica, some commands can be run in parallel by using the Parallelize[] command. As a test example to demonstrate the parallel computing abilities of Mathematica, we calculate Mersenne prime numbers. With

Select[Range[60000],PrimeQ[2^# - 1]&]

Mathematica checks if 2^x-1 is a prime number for x running from 1 to 60000 (don't try this on a single cpu machine, it could take about 18 hours). For a parallel evaluation of the command, one needs to wrapp a Parallelize[] command around the code. This looks in the test case like:

LaunchKernels[4];
a=Parallelize[Select[Range[60000],PrimeQ[2^# - 1]&]]
Print[a]
CloseKernels[];

But, be aware that not all functions in Mathematica can be parallelized ! So please check first if the function you would like to use in a parallel Mathematica script can be parallelized at all. This can be achieved by a simple test script as it is shown for NDSolve[]. Enclose the function in a Parallelize[] command (for the test, you do not even have to put all the arguments of the function)

LaunchKernels[2];
sol = Parallelize[NDSolve[]]
CloseKernels[];

gives the following error message:

Parallelize::nopar1: NDSolve[] cannot be parallelized; proceeding with sequential evaluation.

If you receive this error message for the function that you would like evaluate in parallel, use only a single core.

For running parameter sweeps in parallel, you can also use the ParallelMap[] command instead of using the Parallelize[] command. As an example, we define a function that factorizes integers of the form (10^n-1)/9

LaunchKernels[2];
f1[n_] = FactorInteger[(10^n-1)/9]
ParallelMap[f1,Range[1,1000]]
CloseKernels[];

Here the function f1[n] is evaluated in parallel for n running from 1 to 1000