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
words = ["Julia", "the best", "language", "Programming"]
Creating a function that will fire every random number of seconds
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
The interesting point here is the use of two macros:
@asyncruns the task asynchronously in the background.@syncensures 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.
# 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
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:
- We're talking
@syncthat we want to wait for the end of all actions inside the block; - Then an independent map is created for each word.
@asyncthe task that will launchsleepprint; - Because
sleep(rand())it pauses for various reasons, and tasks are performed randomly; - But thanks to
@syncmost 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