Creating parallel applications with the Julia programming language
Initialization
In addition to the functions for creating arrays, Julia supports complicated initialization functions, such as:
DArray(init, dims[, procs, dist])
Programmers need to supply the init
function, which is given a tuple of index ranges for which the array is then initialized. dims
specifies the array's dimensions.
Programmers can use dist
to determine how the array will be distributed across the various processes. However, this manual declaration isn't generally necessary because Julia sorts it automatically. The example in Listing 1 creates a distributed random matrix and then returns the part for which Process 2 is responsible.
Listing 1
Distributed Arrays
The practical thing about these distributed arrays is that Julia hides all communications from the user. If, for example, a user wants to calculate the sum of all the elements of the d
matrix, that user just needs to call sum(d)
. sum(d)
delivers the result directly. Julia adds all the elements of the subarray with each individual process. All partial results to the total are then added.
Julia also offers structures for easily distributing loops. The following few lines, for example, add generated random numbers using rand()
:
julia> r = @parallel (+) for i=1:200000000 rand() end
The @parallel
macro distributes the loop to all the available processes. The return value for each loop is the last expression in the loop, in this case, the rand()
statement for generating a random number. The example above then adds the results for each loop run. Programmers can also omit the reduction operator (in this case, the addition). Julia then distributes the loop in parallel without reducing the results at the end.
The pmap()
function is useful for such cases. pmap()
simply executes a function for a specific object (i.e., a typical map task). The following example uses the pmap()
statement to compute the rank of matrices:
julia> M = [rand(1000,1000) for i=1:4]; julia> pmap(Rang,M) 4element Array{Any,1}: 1000 1000 1000 1000
The first statement here creates four 1000x1000 matrices. pmap()
then calculates the rank for each matrix. Because Julia is started with p 4
, a worker process is responsible for each matrix. It is just as easy to calculate the characteristics of other matrices. For example, the det()
function calculates the determinant, and the inv()
function calculates the inverse of a matrix.
Monte Carlo Pi Calculation
Monte Carlo calculations are generally very easy to parallelize because the individual calculation steps are mostly independent of each other.
For Monte Carlobased calculations with the number pi, programmers need to generate a lot of random numbers in a square surrounding the unit circle (radius 1). These random numbers are generated uniformly between 1 and +1 for the x and ycoordinates.
The ratio of the area of the unit circle to the square is now just pi*1*1/(2*2). This means that exactly the proportion pi/4 of the random numbers should be in the circle. If you now count them in a loop, you will get the Monte Carlo estimate for pi. In the following example:
pi_MC = N_in / N_tot * 4
N_in
is the number of random numbers in the unit circle, and N_tot
the total number of random numbers generated in the xy plane. The higher N_tot
, the closer pi_MC
is to pi. The best thing is, therefore, to generate loads of random numbers to obtain a very precise value for pi.
Listing 2 shows an implementation of this method in Julia. The program executes the central loop using @parallel
and adds the loop result (1 or 0) at the end in a reduction step. This code runs on a processor in less than two minutes on the test system:
time = 116.90136814117432 seconds pi estimate = 3.14162308
Anyone who uses two cores will speed up the example by nearly a factor of 2:
time = 73.63914084434509 seconds pi estimate = 3.141607444
The method converges very slowly, but the first two digits of pi (3.14) have already been calculated correctly.
Listing 2
Monte Carlo Estimation
A Second Example: Julia with Julia
The parallel calculation of a Julia fractal with Julia is a more complicated example. Martin Rupp originally wrote the program based on the official Julia Mandelbrot example [2]. However, the version shown in Listing 3 is modified because the syntax has changed.
Listing 3
Julia Set
The mathematical idea of a Julia fractal is very simple. Consider the sequence of complex numbers zn+1 = zn + c for an arbitrary complex constant c. Each c yields a particular Julia set. Typical Julia images are created by using a counter. The more iterations are needed in the Znseries to reach a certain threshold value, the brighter the associated item appears. The twodimensional complex plane can be easily mapped to the x and ycoordinate of a pixel grid through which the known pictures are created.
Julia can easily deal with complex numbers, which is why the implementation is particularly simple. The code is complicated by parallelization because distributed arrays are used. Depending on the process ID, part of the Julia image is calculated by another process. An advantage of parallelization is that the calculation of a certain pixel is independent of its surroundings. The situation would be more complicated if the characteristic of one pixel also depended on the environment of the pixel. So, using
~/julia/julia p 4 juliaset.jl
it is possible to easily execute code (see Figure 2).
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
News

Nvidia and Valve Collaborate to Bring DLSS to Linux
Both powerhouses in the gaming industry are trying to make the experience on Linux much improved, by way of DLSS.

Kali Linux 2021.2 Official Release Now Available
The latest iteration of security fanfavorite Kali Linux has been released with new tools, themes, and plenty of improvements.

Entroware Unleashes a Beast of a Linux Laptop
If you're looking for a portable workhorse, look no further than the Entroware Proteus laptop.

System76 Unveils its “Launch” Keyboard
The opensource darling, System76, is about to launch the Launch keyboard and you can preorder yours now.

Armbian 21.05 Now Available
The Armbian developers have released the latest update to the Debianbased Linux distribution geared for ARM and embedded devices.

StarLabs has Released Another Linux Laptop
A new 14" Linux laptop has been released by the company that created the 11" Star Lite and can be purchased with your choice of Linux distribution.

Ubuntu 21.04 Adds Support for Active Directory and Other Major Changes
In a bid to help make Linux more viable for the business desktop, Canonical adds support for Active Directory and a few other notable changes.

GNOME 40 Available on openSUSE
The rolling release edition of openSUSE, Tumbleweed, now offers the latest version of the GNOME desktop.

Apple M1 Hardware Support to be Merged into Linux Kernel 5.13
Linux users will be able to install their favorite distribution on Apple’s M1based hardware.

KDE Launches the Qt 5 Patch Collection
To support and maintain a stable Qt 5 for KDE Gears and Frameworks, KDE will maintain a patch collection.