Array programming

The Metal array type, MtlArray, generally implements the Base array interface and all of its expected methods.

However, there is the special function mtl for transferring an array over to the gpu. For compatibility reasons, it will automatically convert arrays of Float64 to Float32.

Metal.mtlFunction
mtl(A; storage=Metal.PrivateStorage)

storage can be Metal.PrivateStorage (default), Metal.SharedStorage, or Metal.ManagedStorage.

Opinionated GPU array adaptor, which may alter the element type T of arrays:

  • For T<:AbstractFloat, it makes a MtlArray{Float32} for performance and compatibility reasons (except for Float16).
  • For T<:Complex{<:AbstractFloat} it makes a MtlArray{ComplexF32}.
  • For other isbitstype(T), it makes a MtlArray{T}.

By contrast, MtlArray(A) never changes the element type.

Uses Adapt.jl to act inside some wrapper structs.

Examples

julia> mtl(ones(3)')
1×3 adjoint(::MtlVector{Float32, Metal.PrivateStorage}) with eltype Float32:
 1.0  1.0  1.0

julia> mtl(zeros(1,3); storage=Metal.SharedStorage)
1×3 MtlMatrix{Float32, Metal.SharedStorage}:
 0.0  0.0  0.0

julia> mtl(1:3)
1:3

julia> MtlArray(1:3)
3-element MtlVector{Int64, Metal.PrivateStorage}:
 1
 2
 3
source
Metal.MtlArrayType
MtlArray{T,N,S} <: AbstractGPUArray{T,N}

N-dimensional Metal array with storage mode S and elements of type T.

S can be Metal.PrivateStorage (default), Metal.SharedStorage, or Metal.ManagedStorage.

See the Array Programming section of the Metal.jl docs for more details.

source
Metal.MtlVectorType
MtlVector{T,S} <: AbstractGPUVector{T}

One-dimensional array with elements of type T for use with Apple Metal-compatible GPUs. Alias for MtlArray{T,1,S}.

See also Vector(@ref), and the Array Programming section of the Metal.jl docs for more details.

source
Metal.MtlMatrixType
MtlMatrix{T,S} <: AbstractGPUMatrix{T}

Two-dimensional array with elements of type T for use with Apple Metal-compatible GPUs. Alias for MtlArray{T,2,S}.

See also Matrix(@ref), and the Array Programming section of the Metal.jl docs for more details.

source
Metal.MtlVecOrMatType
MtlVecOrMat{T,S}

Union type of MtlVector{T,S} and MtlMatrix{T,S} which allows functions to accept either an MtlMatrix or an MtlVector.

See also VecOrMat(@ref) for examples.

source

Storage modes

The Metal API has various storage modes that dictate how a resource can be accessed. MtlArrays are Metal.PrivateStorage by default, but they can also be Metal.SharedStorage or Metal.ManagedStorage. For more information on storage modes, see the official Metal documentation.

There also exist the following convenience functions to check if an MtlArray is using a specific storage mode: