Skip to content

Map and Mapped Reductions ​

map and map! ​

map(f, A) and map(f, A, B) are the same as f.(A) and f.(A, B). map!(op, dest, A, B) writes into dest for built-in binary operators (+, *, max, …). All three arrays must have the same shape.

julia
A, B = cuNumeric.rand(1024), cuNumeric.rand(1024)
C = map(sin, A)
map!(*, C, A, B)
Base.map Method
julia
map(f, A::NDArray)

Apply f elementwise to A. Same as f.(A).

source
Base.map Method
julia
map(f, A::NDArray, B::NDArray)

Apply f elementwise to A and B. Same as f.(A, B).

source
Base.map! Method
julia
map!(op, dest::NDArray, A::NDArray, B::NDArray)

Write op.(A, B) into dest for built-in binary operators (+, *, max, …). All three arrays must have the same shape.

source

mapreduce ​

mapreduce(f, op, A; dims=:, init=...) maps and reduces an NDArray in one pass, without a mapped copy. op can be +, *, min or max. Mapped sum, prod, minimum and maximum use the same path.

julia
A = cuNumeric.ones(Float32, 1024, 512)
energy = sum(abs2, A)                     # CNScalar
columns = mapreduce(abs2, +, A; dims=1)   # 1 × 512
const α = 0.5f0                          # a non-const global would infer as Any
distance = sum(x -> abs2(x - α), A)
largest = maximum(abs, A; init=0f0)
  • Full reductions return a device-resident CNScalar.

  • Dimensional reductions keep reduced axes with size one. dims=() still applies the mapping.

Types and init ​

  • sum and prod widen integers like Base. mapreduce with + or * does not. Implicit widening is subject to allowpromotion.

  • init must be a neutral element. It is applied once and sets the output type. For dims reductions it must not narrow the accumulator.

  • Empty inputs follow Base, so arbitrary mappings may need init.

Limitations ​

  • GPU only. Unmapped reductions still run on CPU.

  • One input array. The mapping must be type-stable and GPU-compilable, and may capture only immutable scalars. At top level, captured globals must be const. No custom reducers.

  • No complex minimum/maximum, and no ComplexF64 prod.

  • Floating-point results are not bitwise reproducible. NaN propagation is kept.

Base.mapreduce Method
julia
mapreduce(f, op, A::NDArray; dims=:, init)

Fuse a scalar mapping with a distributed GPU reduction. Supported operators are +, *, min, and max. Full reductions return an CNScalar; explicit dimensions retain singleton axes. sum(f, A) and prod(f, A) use Base's integer widening rules, subject to allowpromotion.

Requires an active GPU target and a type-stable GPU-compilable callable with only isbits scalar captures. One input array is supported; complex results support only addition/product; ComplexF64 product accumulators are unsupported by Legate. Narrowing dimensional init types are unsupported. Floating-point results may differ in rounding with partitioning; extrema preserve NaNs and signed zeros, but not NaN payloads. See the mapped-reductions documentation.

source