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.mtl
— Functionmtl(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 aMtlArray{Float32}
for performance and compatibility reasons (except forFloat16
). - For
T<:Complex{<:AbstractFloat}
it makes aMtlArray{ComplexF32}
. - For other
isbitstype(T)
, it makes aMtlArray{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
Metal.MtlArray
— TypeMtlArray{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.
Metal.MtlVector
— TypeMtlVector{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.
Metal.MtlMatrix
— TypeMtlMatrix{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.
Metal.MtlVecOrMat
— TypeMtlVecOrMat{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.
Storage modes
The Metal API has various storage modes that dictate how a resource can be accessed. MtlArray
s 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.
Metal.MTL.PrivateStorage
— Typestruct Metal.PrivateStorage <: MTL.StorageMode
Used to indicate that the resource is stored using MTLStorageModePrivate
in memory.
For more information on Metal storage modes, refer to the official Metal documentation.
See also Metal.SharedStorage
and Metal.ManagedStorage
.
Metal.MTL.SharedStorage
— Typestruct Metal.SharedStorage <: MTL.StorageMode
Used to indicate that the resource is stored using MTLStorageModeShared
in memory.
For more information on Metal storage modes, refer to the official Metal documentation.
See also Metal.PrivateStorage
and Metal.ManagedStorage
.
Metal.MTL.ManagedStorage
— Typestruct Metal.ManagedStorage <: MTL.StorageMode
Used to indicate that the resource is stored using MTLStorageModeManaged
in memory.
For more information on Metal storage modes, refer to the official Metal documentation.
See also Metal.SharedStorage
and Metal.PrivateStorage
.
There also exist the following convenience functions to check if an MtlArray is using a specific storage mode:
Metal.is_private
— Functionis_private(A::MtlArray) -> Bool
Returns true if A
has storage mode Metal.PrivateStorage
.
See also is_shared
and is_managed
.
Metal.is_shared
— Functionis_shared(A::MtlArray) -> Bool
Returns true if A
has storage mode Metal.SharedStorage
.
See also is_private
and is_managed
.
Metal.is_managed
— Functionis_managed(A::MtlArray) -> Bool
Returns true if A
has storage mode Metal.ManagedStorage
.
See also is_shared
and is_private
.