:@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
end

Examples

"""
    @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:

  1. Parallel sum computation:

    julia> @parallel for i = 1:10
               @show i
               i
           end

    In this example, the loop executes in parallel across available workers, printing and returning the values of i. No reducer function is specified.

  2. 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
           end

    Here, the loop executes in parallel across workers, and each worker performs a local reduction using the custom reducer function. The final reduction is done on the calling process.

  3. Waiting for completion using @sync:

    julia> @sync @parallel for i = 1:10
               @show i
               i
           end

    This example uses @sync to 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.

*Required Field
Details

Checking you are not a robot: