Distributed computing
Engee provides capabilities for implementing distributed computing using a library called Distributed. The tools of this library help to explore the possibilities of performing tasks in separate threads that may reside on different computing cores.
Importing a library for distributed computing:
Pkg.add(["LinearAlgebra", "Distributed"])
using Distributed
Using the nworkers function of the Distributed library, you can find out how many processes are currently available.:
nworkers()
Using addprocs, you can add a number of workflows.:
addprocs(2)
nworkers()
Using pmap
One example of how all available processes work is using the pmap function. The pmap function transforms a collection of c by applying f to each item **using the available workflows and tasks.
pmap(x -> x*2, [1,2,3])
Any mistake can stop pmap. This may result in the specified function not
being applied to all items in the collection. However, these errors can be handled using
the on_error argument.
pmap(x -> iseven(x) ? error("foo") : x, 1:4; on_error=identity)
Errors can also be handled by returning values instead.:
even_or_zero = pmap(x->iseven(x) ? error("foo") : x, 1:4; on_error=ex->0)
Using the @distributed macro
The macro @distributed in the Julia programming language provides the ability to parallelize operations in a loop. It allows you to iterate the loop on different processes, which can speed up code execution, especially when working with large amounts of data.
Example of using a macro @distributed:
@elapsed @sync @distributed for _ in 1:2
sleep(2)
end
In this case, the loop iterations are executed in parallel, without using the macro, the execution time of the cycle would take 4 seconds.
Using the @everywhere macro
The macro @everywhere is used to execute code on all available processors in parallel computing without having to explicitly specify each process.
This example will be executed on every processor available for parallel computing and will output the eigenvalues of a random matrix A:
@everywhere begin
using LinearAlgebra
A = rand()
println((A))
end
Conclusions:
In this example, the use of the Distributed library for implementing distributed computing was demonstrated. Examples of using the pmap function and macros @distributed and @everywhere for parallel execution of tasks and operations in a loop were given.