Skip to content

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.

julia
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 Float64

Types ​

TypeSubtype of
CNFloatAbstractFloat
CNInt, CNUIntSigned, Unsigned
CNBoolInteger
CNComplexNumber

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:

julia
@allowfetch s > 0            # Bool
allowfetch() do
    Float64(s)
end
  • The 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 Float64 still needs f(Float64(s)).

  • Conversions that can lose information (float to int, complex to real) need allowfetch and throw InexactError like 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.

source
cuNumeric.CNComplex Type

A complex device scalar backed by a 0D NDArray; wrapping does not synchronize.

source
cuNumeric.CNFloat Type

A floating-point device scalar; wrapping its 0D storage does not synchronize.

source
cuNumeric.CNInt Type

A signed integer device scalar backed by a 0D NDArray.

source
cuNumeric.CNUInt Type

An unsigned integer device scalar backed by a 0D NDArray.

source
Base.fetch Method
julia
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.

source
cuNumeric.allowfetch Method
julia
allowfetch(f, allow=true)
allowfetch(allow::Bool=true)
@allowfetch expression

Permit 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