:@parallel
@parallel
A parallel for loop of the form :
@parallel [reducer] for var = range
body
end
The specified range is partitioned and locally executed across all workers. In case an optional reducer function is specified, @parallel performs local reductions on each worker with a final reduction on the calling process.
Note that without a reducer function, @parallel executes asynchronously, i.e. it spawns independent tasks on all available workers and returns immediately without waiting for completion. To wait for completion, prefix the call with @sync, like :
@sync @parallel for var = range
body
endExamples
"""
@parallel [reducer] for var = range
A parallel for loop of the form:
@parallel [reducer] for var = range
body
end
The specified range is partitioned and locally executed across all workers. In case an optional reducer function is specified, @parallel performs local reductions on each worker with a final reduction on the calling process.
Note that without a reducer function, @parallel executes asynchronously, i.e. it spawns independent tasks on all available workers and returns immediately without waiting for completion. To wait for completion, prefix the call with @sync, like:
@sync @parallel for var = range
body
end
Examples:
-
Parallel sum computation:
julia> @parallel for i = 1:10 @show i i endIn this example, the loop executes in parallel across available workers, printing and returning the values of
i. No reducer function is specified. -
Parallel reduction using a custom reducer function:
julia> reducer = (x, y) -> x + y # Custom reducer function julia> @parallel reducer for i = 1:10 @show i i endHere, the loop executes in parallel across workers, and each worker performs a local reduction using the custom
reducerfunction. The final reduction is done on the calling process. -
Waiting for completion using
@sync:julia> @sync @parallel for i = 1:10 @show i i endThis example uses
@syncto wait for the completion of all parallel tasks before proceeding.
Note: Ensure that the necessary worker processes are available before using @parallel.```
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.