Stream: helpdesk (published)

Topic: FLoops and reductions over vectors


view this post on Zulip Daniel VandenHeuvel (Dec 06 2022 at 21:40):

Is it possible to build reductions over individual vector elements with FLoops.jl (or any other package? I only know of FLoops for now)? I'm trying to get a solution to a question I asked some time ago, https://discourse.julialang.org/t/problem-converting-serial-code-to-parallel-code-with-floops/87610, and this seems to be the issue with it. The actual example I'm interested in is for parallelising some finite volume code https://github.com/DanielVandH/FiniteVolumeMethod.jl/blob/main/src/fvm.jl#L793-L902.

view this post on Zulip jar (Dec 06 2022 at 22:22):

Is it possible to build reductions over individual vector elements

Can you give a minimal example of what this means?

view this post on Zulip Daniel VandenHeuvel (Dec 06 2022 at 22:26):

Well, actually I'm not 100% sure what I mean there other than that I wanted to use FLoops.@reduce for this, applying it to expressions like du[j] += 100 so that @reduce du[j] += 100 works.

In the linked discourse post, I have the code:

using FLoops, Random
function update_u!(du, u, k, q, j)
    v = [k, (k + 1) % length(u) + 1, (k + 7) % length(u) + 1]
    q .= [u[v[1]], u[v[3]]]
    du[v[j]] += q' * q
    du[v[(j%3)+1]] -= sin.(q)' * sin.(q)
    nothing
end
function do_parallel(du::AbstractVector{T}, u, elements) where {T}
    q = zeros(2, length(elements), 3)
    @floop for k  elements
        for j in 1:3
            @views update_u!(du, u, k, q[:, k, j], j)
        end
    end
    nothing
end

This somewhat captures what I want. @reduce would (I believe) avoid the data-race issues with updating du, but it doesn't seem to work on expressions like that.

view this post on Zulip jar (Dec 06 2022 at 23:07):

The floop macro is just a wrapper around the functional primitives, so if it's not working you should still be able to write it with Folds or Transducers

view this post on Zulip Daniel VandenHeuvel (Dec 06 2022 at 23:21):

I'll look into those packages. I had previously considered those but couldn't follow the documentation. Thanks


Last updated: Dec 28 2024 at 04:38 UTC