https://scicomp.ethz.ch/w/index.php?title=Mathematica/Parallel&feed=atom&action=historyMathematica/Parallel - Revision history2022-12-04T02:25:41ZRevision history for this page on the wikiMediaWiki 1.27.0https://scicomp.ethz.ch/w/index.php?title=Mathematica/Parallel&diff=518&oldid=prevSfux: 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..."2016-08-25T06:47:00Z<p>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..."</p>
<p><b>New page</b></p><div><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><br />
<br />
<font color="red">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.</font><br />
<br />
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 <tt>LaunchKernel[]</tt> command. Please do not forget to kill these Mathematica instances at the end of your script with the <tt>CloseKernels[]</tt> command. A typical parallel Mathematica script could look as follows (here we would like to run on 4 cores):<br />
<br />
LaunchKernels[4];<br />
...<br />
Parallel Mathematica code<br />
...<br />
CloseKernels[]<br />
<br />
In Mathematica, some commands can be run in parallel by using the <tt>Parallelize[]</tt> command. As a test example to demonstrate the parallel computing abilities of Mathematica, we calculate Mersenne prime numbers. With<br />
<br />
Select[Range[60000],PrimeQ[2^# - 1]&]<br />
<br />
Mathematica checks if <math>2^x-1</math> is a prime number for <math>x</math> 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 <tt>Parallelize[]</tt> command around the code. This looks in the test case like:<br />
<br />
LaunchKernels[4];<br />
a=Parallelize[Select[Range[60000],PrimeQ[2^# - 1]&]]<br />
Print[a]<br />
CloseKernels[];<br />
<br />
But, be aware that <font color="red">not all functions in Mathematica can be parallelized !</font> 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 <tt>NDSolve[]</tt>. Enclose the function in a <tt>Parallelize[]</tt> command (for the test, you do not even have to put all the arguments of the function)<br />
<br />
LaunchKernels[2];<br />
sol = Parallelize[NDSolve[]]<br />
CloseKernels[];<br />
<br />
gives the following error message:<br />
<br />
Parallelize::nopar1: NDSolve[] cannot be parallelized; proceeding with sequential evaluation.<br />
<br />
If you receive this error message for the function that you would like evaluate in parallel, use only a single core.<br />
<br />
For running parameter sweeps in parallel, you can also use the ParallelMap[] command instead of using the <tt>Parallelize[]</tt> command. As an example, we define a function that factorizes integers of the form <math>(10^n-1)/9</math><br />
<br />
LaunchKernels[2];<br />
f1[n_] = FactorInteger[(10^n-1)/9]<br />
ParallelMap[f1,Range[1,1000]]<br />
CloseKernels[];<br />
<br />
Here the function <tt>f1[n]</tt> is evaluated in parallel for n running from 1 to 1000</div>Sfux