Engee documentation
Notebook

Parallel computing in Julia language

This example discusses the implementation of parallel computing in the Julia programming language using threads.

Introduction

What is parallel computing?

Parallel computing is a way of performing multiple tasks simultaneously when they may partially overlap in time. This is especially useful if the operations are not directly related and can be performed in parallel.

This approach is often used for:

  • Performance optimization
  • Processing multiple tasks simultaneously
  • Work with network requests or I/O operations

The main part

Creating an array of words that will be output asynchronously

In [ ]:
words = ["Julia", "the best", "language", "Programming"]
Out[0]:
4-element Vector{String}:
 "Julia"
 "лучший"
 "язык"
 "программирования"

Creating a function that will fire every random number of seconds

In [ ]:
function sleepprint(s)
    # We generate a delay from 0 to 1 second (by default)
    sleep(rand())
    # Output the line to the console
    println(s)
end
Out[0]:
sleepprint (generic function with 1 method)

The interesting point here is the use of two macros:

  • @async runs the task asynchronously in the background.
  • @sync ensures that the main process waits for all asynchronous tasks to complete before continuing execution.

The next loop runs each word from the list in a separate task (Task) through a function call sleepprint.

In [ ]:
# We run all asynchronous tasks five times.:
for i in 1:5
    println("\iTeration No.: $i")
    
    @sync for word in words
        # For each iteration, we create an async task
        @async sleepprint(word)
    end
end
Итерация №: 1
лучший
Julia
программирования
язык

Итерация №: 2
язык
программирования
лучший
Julia

Итерация №: 3
язык
программирования
Julia
лучший

Итерация №: 4
язык
Julia
лучший
программирования

Итерация №: 5
язык
программирования
Julia
лучший

Because sleep(rand()) generates different time intervals, and the line output order may change each time the script is run.

The principle of operation is as follows:

  1. We're talking @sync that we want to wait for the end of all actions inside the block;
  2. Then an independent map is created for each word. @async the task that will launch sleepprint;
  3. Because sleep(rand()) it pauses for various reasons, and tasks are performed randomly;
  4. But thanks to @sync most importantly, the thread continues to run only after completing ALL such tasks.

Conclusion

We have reviewed a simple demonstration of parallel computing in the Julia language. Using macros@async and @sync we were able to organize the competitive execution of the function sleepprint, which simulated some kind of long-term operation (artificial delay).

This shows how easy it is to parallelize the simplest tasks in Julia without having to use external libraries. This method is suitable for situations where it is necessary to perform several independent operations (for example, accessing the database, reading files, API calls) more efficiently than sequentially, especially if these operations require waiting for external resources.

The example was developed using materials from Rosetta Code