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,^-1orinv
Reductions, with optional dims except argmax / argmin:
sum,prod,maximum,minimum,mean,var,std,all,any,argmax,argmin
Differences from Base Julia
The
acoshfunction in Julia will error on inputs outside of the domain (x >= 1), but cuNumeric.jl will returnNaN.round.(A)uses IEEE round-to-nearest-even (theRINTkernel), matching Juliaround(x)/RoundNearest. Only the single-argument form is supported.digits,sigdigits, andRoundingModeare not supported (round.(A; digits=n)errors).floor,ceil,trunc, andsignbitare float-only kernels.roundalso supports complex values. Bool and integer inputs are not accepted (Julia'sfloor/ceil/trunc/roundreturn integer inputs unchanged).~is bitwise not on integers. OnBoolit matches!(the invert kernel rejectsBool, so we use logical not).Full reductions (
sum,mean,var,std, …) return aCNScalarbacked by a 0D NDArray. Usefetchto read a native Julia scalar.var/stdmatch Julia / StatsBase sample statistics (corrected=true, divisorn-1). Complex inputs are not supported.argmax/argminare 1D fetch (matching Base'sIntreturn, notCartesianIndex). The result is a 0DNDArray{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
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 resultBase.argmax Method
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.
Base.argmin Method
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.
Statistics.mean Method
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.
Statistics.var Method
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.