Parallelizable reduction using @reduce

Warning

This page is still work-in-progress.

using FLoops

@reduce() do ... end syntax

@floop for x in 1:10
    y = 2x
    @reduce() do (acc; y)
        acc += y
    end
end
acc
110

Argument symbols must be unique within a @reduce block

@floop for x in 1:10
    @reduce() do (a; x), (b; x)
        a += x
        b *= x
    end
end
ERROR: LoadError: ArgumentError: Same input variable used multiple times.
* `x` used 2 times
in expression starting at reduction.md:32

The argument should be manually duplicated when using the same variable that would be merged into multiple accumulators:

@floop for x in 1:10
    y = x
    @reduce() do (a; x), (b; y)
        a += x
        b *= y
    end
end
(a, b)
(55, 3628800)

If two accumulators do not interact as in the case above, it is recommended to use two @reduce() do blocks to clarify that they are independent reductions:

@floop for x in 1:10
    @reduce() do (a; x)
        a += x
    end
    @reduce() do (b; x)
        b *= x
    end
end
(a, b)
(55, 3628800)

This page was generated using Literate.jl.