Creating processes

To start with processes, add and make available the Distributed package with add Distributed, and using Distributed.

Julia can be started as a REPL or as a separate application with a number of workers, n, available. The following command starts n processes on the local machine (this command includes the Distributed package automatically):

// code in Chapter 8parallel.jl 
julia -p n   # starts REPL with n workers 

These workers are different processes, not threads, so they do not share memory.

To get the most out of a machine, set n equal to the number of processor cores. For example, when n is 8, you have, in fact, nine workers: one for the REPL shell itself, and eight others that are ready to do parallel tasks. Every worker has its own integer identifier, which we can see by calling the workers function, workers(). This returns the following:

8-element Array{Int64,1} containing:   2  3  4  5  6  7  8  9

Process 1 is the REPL worker. We can now iterate over the workers with the following command:

for pid in workers() 
  # do something with each process (pid = process id) 
end 

Each worker can get its own process ID with the myid() function. If you need more workers, adding new ones is easy:

addprocs(5)

This returns 5-element Array{Any,1}, which contains their process identifiers, 10 11 12 13 14. The default method adds workers on the local machine, but the addprocs method accepts arguments to start processes on remote machines via SSH. This is the secure shell protocol that enables you to execute commands on a remote computer via a shell in a totally encrypted manner.

The number of available workers is given by nprocs(); in our case, this is 14. A worker can be removed by calling rmprocs() with its identifier; for example, rmprocs(3) stops the worker with the ID 3.

All these workers communicate via TCP ports and run on the same machine, which is why it is called a local cluster. To activate workers on a cluster of computers, start Julia as follows:

julia --machine-file machines driver.jl

Here, machines is a file that contains the names of the computers you want to engage, as follows:

node01
node01
node02
node02
node03

Here node01, node02, and node03 are the three names of computers in the cluster, and we want to start two workers each on node01 and node02, and one worker on node03.

The driver.jl file is the script that runs the calculations and has a process identifier of 1. This command uses a password-less SSH login to start the worker processes on the specified machines. The following screenshot shows all the eight processors on an eight-core machine when engaged in a parallel operation:

The horizontal axis is time, and the vertical is the CPU usage. On each core, a worker process is engaged in a long-running Fibonacci calculation.

Processors can be dynamically added or removed to a master Julia process, locally on symmetric multiprocessor systems, remotely on a computer cluster, as well as in the cloud. If more versatility is needed, you can work with the ClusterManager type (see http://docs.julialang.org/en/latest/manual/parallel-computing/).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.126.199