Mathematica >
PARALLEL PACKAGE TUTORIAL

Getting Started

Mathematica comes with all the tools and configuration so you can immediately carry out parallel computing. Note that, to take advantage of parallel computing it is often better to have a multi-core machine or access to a grid of parallel Mathematica kernels. Luckily, multi-core machines have been common in many types of configuration for some time.
A first step that may just demonstrate that the system is running is a ParallelEvaluate. If this is the first parallel computation, it will launch the configured parallel kernels.
The following example should return the process id for each parallel kernel.
In[1]:=
Click for copyable input
Out[1]=
This returns the machine name for each kernel, it shows that everything is running on the same computer.
In[2]:=
Click for copyable input
Out[2]=
You might find it useful to open the Parallel Kernels Status monitor, it should look something like the following.
Now we can carry out an actual computation. One very simple type of parallel program is to do a search. In the following example, one is added to a factorial and the result is tested to see if it is a prime number. This is done by wrapping the regular Mathematica computation in Parallelize.
In[3]:=
Click for copyable input
This shows us that some of these numbers are prime.
In[4]:=
Click for copyable input
Out[4]=
Another example is to look for Mersenne prime numbers. This is done with the following, again wrapping the computation in Parallelize.
In[5]:=
Click for copyable input
Out[5]=
This shows that the first 15 Mersenne prime numbers have been found.
When you get to this stage you should be ready to start carrying out parallel computation in Mathematica.

Using Your Own Functions in Parallel Computations

The previous example worked by simply wrapping a parallelizable expression in Parallelize[...]. If the expressions involves not only built-in functions, but functions you defined yourself, some preparatory work is necessary.
Definitions for symbols to be evaluated on the parallel kernels, other than built-in ones, need to be distributed to all kernels before they can be used.
We define a predicate that tests whether is prime.
In[14]:=
Click for copyable input
We distribute the definition to all parallel kernels.
In[15]:=
Click for copyable input
Now we can use it as part of a parallel computation.
In[16]:=
Click for copyable input
Out[16]=
What happens if we forget to distribute definitions for a parallel computation?
This definition is the same as mersenneQ above.
In[17]:=
Click for copyable input
The parallel kernels do not know the definition, so it never returns True.
In[18]:=
Click for copyable input
Out[18]=
In many cases the computation seems to work anyway, but if you analyze its performance, you should see that it was not in fact evaluated as fast as it should.
This computation gives the right result, but it is not faster than a normal Table would be.
In[22]:=
Click for copyable input
Out[22]=
The reason it seems to work is that the unknown function m2 does not evaluate on the parallel kernels, so the expressions m2[10000], m2[10001], ... are sent back, and they then evaluate on the master kernel, where the definition is known.
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team
Format:   HTML  |  CDF