Skip to content

Unary Operations ​

Prefer `@.` for multi-op elementwise expressions so every operator is dotted (especially unary negation). This makes eligible CUDA broadcasts fusion-friendly. See [Kernel Fusion](perf/kernel_fusion.md).

The following unary operations are supported and can be broadcast over NDArray:

  • -, !, ~, abs, acos, acosh, asin, asinh, atan, atanh, cbrt, ceil, conj, cos, cosh, deg2rad, exp, exp2, expm1, floor, imag, isfinite, isinf, isnan, log, log10, log1p, log2, rad2deg, real, round, sign, signbit, sin, sinh, sqrt, tan, tanh, trunc, ^2, ^-1 or inv

Reductions, with optional dims except argmax / argmin:

  • sum, prod, maximum, minimum, mean, var, std, all, any, argmax, argmin

Differences from Base Julia ​

  • The acosh function in Julia will error on inputs outside of the domain (x >= 1), but cuNumeric.jl will return NaN.

  • round.(A) uses IEEE round-to-nearest-even (the RINT kernel), matching Julia round(x) / RoundNearest. Only the single-argument form is supported. digits, sigdigits, and RoundingMode are not supported (round.(A; digits=n) errors).

  • floor, ceil, trunc, and signbit are float-only kernels. round also supports complex values. Bool and integer inputs are not accepted (Julia's floor/ceil/trunc/round return integer inputs unchanged).

  • ~ is bitwise not on integers. On Bool it matches ! (the invert kernel rejects Bool, so we use logical not).

  • Full reductions (sum, mean, var, std, …) return a CNScalar backed by a 0D NDArray. Use fetch to read a native Julia scalar.

  • var / std match Julia / StatsBase sample statistics (corrected=true, divisor n-1). Complex inputs are not supported.

  • argmax / argmin are 1D fetch (matching Base's Int return, not CartesianIndex). The result is a 0D NDArray{Int64} of the 1-based index. Complex inputs are not supported.

For mapreduce and mapped sum, prod, minimum, and maximum, see Map and Reduce.

cuNumeric.unary_reduction_map Constant

Supported Unary Reduction Operations

The following unary reduction operations are supported and can be applied directly to NDArray values:

• all • any • maximum • minimum • prod • sum • mean • var / std (sample / corrected=true; real types only) • argmax / argmin (1-d only)

Full reductions return an CNScalar backed by a 0D NDArray. Use fetch or only when you need a host value.

Reduction over specific dimensions is supported via the dims keyword argument, following the same keepdims semantics as Julia's base reduction functions. Multi-axis dims=(1,2) is implemented as sequential single-axis reductions (the C++ kernel accepts only one axis at a time). argmax/argmin are 1-d only (Base's N-d / dims= path returns CartesianIndex).

Examples

julia
A = cuNumeric.ones(5)

maximum(A)
sum(A)
mean(A)

# Reduce over a specific dimension
B = cuNumeric.ones(3, 4)
sum(B, dims=1)    # 1×4 result
sum(B, dims=2)    # 3×1 result

# Reduce over multiple dimensions
sum(B, dims=(1,2))  # 1×1 result
source
Base.argmax Method
julia
argmax(A::NDArray{<:Any,1})

1-based index of the first extremum, as a 0-d NDArray{Int64} (not a Julia Int). 1-d only. Complex arrays are not supported.

source
Base.argmin Method
julia
argmin(A::NDArray{<:Any,1})

1-based index of the first extremum, as a 0-d NDArray{Int64} (not a Julia Int). 1-d only. Complex arrays are not supported.

source
Statistics.mean Method
julia
mean(A::NDArray; dims=:)

Arithmetic mean of A. Full reduction returns an CNScalar, not a host scalar. With dims, the reduced axes are kept as size 1, matching Base.

source
Statistics.var Method
julia
var(A::NDArray; corrected=true, mean=nothing, dims=:)
std(A::NDArray; corrected=true, mean=nothing, dims=:)

Sample variance and standard deviation (corrected=true, divisor n-1), matching Julia / StatsBase. Real types only. Returns an CNScalar or dimension-preserving NDArray, not a Julia scalar.

source