Device Scalars
Full reductions (sum(A), dot(x, y), norm(x)) return a CNScalar: a number that stays on the device, backed by a 0D NDArray. Further arithmetic needs no host transfer or synchronization.
using cuNumeric, LinearAlgebra
x = cuNumeric.NDArray([1.0, 2.0, 3.0])
s = sum(x) # CNFloat{Float64}
t = s^2 / 2 # still on device
y = x .* t # no host round trip
fetch(t) # 18.0, a Julia Float64Types
| Type | Subtype of |
|---|---|
CNFloat | AbstractFloat |
CNInt, CNUInt | Signed, Unsigned |
CNBool | Integer |
CNComplex | Number |
CNReal is the union of the real types, and CNScalar adds CNComplex. DeviceScalar{T} accepts either a CNScalar{T} or an NDArray{T,0}. cnscalar(a) wraps a 0D NDArray.
Getting a host value
fetch(s) always works. Implicit extraction (comparisons, Float64(s), if conditions, isapprox tolerances, a norm's p) needs allowfetch:
@allowfetch s > 0 # Bool
allowfetch() do
Float64(s)
endThe permission is task-local and also applies inside functions called in the scope.
It doesn't convert arguments for you: a method that only takes
Float64still needsf(Float64(s)).Conversions that can lose information (float to int, complex to real) need
allowfetchand throwInexactErrorlike Base.
Without allowfetch, device scalars work as broadcast operands, Ref(s), fill values, and coefficients for mul!, axpy!, axpby!, contract! and TensorOperations.
cuNumeric.CNBool Type
A Boolean device scalar. Bool is concrete, so the wrapper subtypes Integer.
sourcecuNumeric.CNComplex Type
A complex device scalar backed by a 0D NDArray; wrapping does not synchronize.
sourcecuNumeric.CNFloat Type
A floating-point device scalar; wrapping its 0D storage does not synchronize.
sourceBase.fetch Method
fetch(x::CNScalar)
fetch(x::NDArray)Retrieve a native Julia scalar, waiting for the result as needed. An NDArray must contain exactly one element. Explicit fetching does not require allowfetch or allowscalar permission.
cuNumeric.allowfetch Method
allowfetch(f, allow=true)
allowfetch(allow::Bool=true)
@allowfetch expressionPermit implicit host extraction of CNScalars for comparisons, predicates, and conversion to host numeric types. Arithmetic stays on the backend. The do-block and macro restore the calling task's previous permission, including on errors. Permission is task-local and separate from scalar indexing and promotion. This is not a fallback for arbitrary functions with unsupported argument types.
source