MapReduce

Equivalent to reduce(op, map(f, iterable)), without saving the intermediate mapped collection; can be used to e.g. split documents into words (map) and count the frequency thereof (reduce).

  • Other names: transform_reduce, some fold implementations include the mapping function too.

Function signature:

mapreduce(f, op, src::AbstractGPUVector; init,
          block_size::Int=256, temp::Union{Nothing, AbstractGPUVector}=nothing,
          switch_below::Int=0)

Example computing the minimum of absolute values:

import AcceleratedKernels as AK
using Metal

v = MtlArray{Int32}(rand(-5:5, 100_000))
AK.mapreduce(abs, (x, y) -> x < y ? x : y, v, init=typemax(Int32))

As for reduce, when there are fewer than switch_below elements left to reduce, they can be copied back to the host and we switch to a CPU reduction. The init initialiser has to be a neutral element for op, i.e. same type as returned from f (f can change the type of the collection, see the "Custom Structs" section below for an example). The temporary array temp needs to have at least (length(src) + 2 * block_size - 1) ÷ (2 * block_size) elements and have eltype(src) === typeof(init).