Array Programming

oneAPI.jl provides an array type, oneArray, which lives on the GPU. It implements the interface defined by GPUArrays.jl, allowing for high-level array operations.

The oneArray Type

The oneArray{T,N} type represents an N-dimensional array with elements of type T stored on the GPU.

using oneAPI# Allocate an uninitialized arraya = oneArray{Float32}(undef, 1024)# Initialize from a CPU arrayb = oneArray([1, 2, 3, 4])# Initialize with zeros/onesz = oneAPI.zeros(Float32, 100)o = oneAPI.ones(Float32, 100)

Array Operations

Since oneArray implements the AbstractArray interface, you can use standard Julia array operations.

a = oneArray(rand(Float32, 10))b = oneArray(rand(Float32, 10))c = a .+ b        # Element-wise additiond = sum(a)        # Reductione = map(sin, a)   # Map

Data Transfer

To move data between the host (CPU) and the device (GPU), use the constructors or copyto!.

# CPU to GPUd_a = oneArray(h_a)# GPU to CPUh_a = Array(d_a)

Copies to the GPU return as soon as the host array can be modified again. On integrated GPUs, small ones, up to 1 MiB, are staged in host memory and don't wait for work queued earlier. Copies to the host wait for the data to arrive.

Backend Agnostic Programming

To write code that works on both CPU and GPU (and other backends like CUDA), use the generic array interfaces provided by GPUArrays.jl. Avoid hardcoding oneArray in your functions; instead, accept AbstractArray and let the dispatch system handle the specific implementation.

function generic_add!(a::AbstractArray, b::AbstractArray)    a .+= b    return aend# Works on CPUgeneric_add!(rand(10), rand(10))# Works on Intel GPUgeneric_add!(oneArray(rand(10)), oneArray(rand(10)))