Mathematica Parallel Computing Configuration

From mathpub
Jump to navigation Jump to search

This page contains information regarding how to setup nodes in the cluster as Remote Kernels in different methods to run parallel computation

First Method : Launch the kernels using commands in the notebook

Use the code given below in a Mathematica notebook. Change "kernels" variable to the number of kernels wanted for parallel computation

Needs["SubKernels`RemoteKernels`"]
kernels = 4; (*Number of kernels on each machine*)
configureKernel[host_] := 
 SubKernels`RemoteKernels`RemoteMachine[host, kernels];
LaunchKernels[
 Map[configureKernel, {"ssh://ramsey/", "ssh://fibonacci/", 
 "ssh://hopper/", "ssh://boole/", "ssh://heaviside/", 
 "ssh://squid1/", "ssh://squid2/"}]]; (*all the available machines, change to whatever is needed*)


Printing "Hello World" in parallel after setting up 4 kernels per machine with above code

Image1.png

Image.png


Second Method: Configure Remote Kernels in Mathematica Menus

In this method, we will set up the nodes in the cluster as Remote Kernels from the Mathematica Parallelize menu. This setup is required only once, every subsequent time, simply running LaunchKernels

command should set up the remote kernels for parallel evaluation.

Step 1

Open Mathematica and open Parallel Kernel Configuration from the Evaluation tab

Mathematica parallel.png

Step 2

Under Parallel, choose Remote Kernels, click on add hosts and add as shown below. Tick enable next to the number of kernels. For each, you can configure the number of kernels you want running on the machine.

Image2.png


Add all the machines and number of kernels you want to add. This is a one time setup, you won't need to do this again and again.


Image3.png

Step 3

Disable RemoteKernel Object here (if it is enabled)

Image4.png


If you don't disable this, subkernels will be created on the local kernel and not on the remote hosts.

Step 4

Once all these settings are done, running LaunchKernels[] from any notebook will launch the specification in the menu.

In this example 3 machines were configured in menu as shown above:

Image6.png

Image5.png


Third Method: Launching the kernels in a loop

For this method, the kernels will be launched by running the below code:

Needs["SubKernels`

RemoteKernels`"]

kernels = 1;(*number of kernels on each machine*)

configureKernel[host_] := 

SubKernels`RemoteKernels`RemoteMachine[host, kernels];

For[i = 0, i < 15, i++; 

If[i < 10, 

 LaunchKernels[

  Map[configureKernel, {StringJoin[{"ssh://pnode0", ToString[i], 

      "/"}]}]], 

 LaunchKernels[

   Map[configureKernel, {StringJoin[{"ssh://pnode", ToString[i], 

      "/"}]}]]]]

We are launching 15 kernels here. Change the number in the loop condition to launch that number of kernels.


Parallel Evaluation Example: Generating and Plotting Mandelbrot Set

In this section we will take a look at how the time for Mandelbrot set generation is greatly improved when we run on parallel remote kernels.

Code for generating Mandelbrot set without parallel kernels:

Image7.png


Output and Timing:

Image8.png


To parallelize this, we will launch 15 cores using the third method:

Image9.png


Output and Timing for Parallel Evaluation:

Image10.png


Code for running parallel evaluation of Mandelbrot set:

f[c_, maxiter_Integer : 10^4] := 

Abs[NestWhile[#^2 + c &, 0, Abs[#] < 2 &, 1, maxiter]] < 2

f[0]

(*True*)

f[1]

(*False*)

f[I]

(*True*)

f[I, 10^6]

(*True*)

ArrayPlot[

ParallelTable[

 Boole[f[N[x + I y], 10^3]], {y, -2, 2, 1/50}, {x, -2, 2, 1/50}]]