From bd896ddd13021753561a89745af4d76c59acb486 Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Sat, 13 Jul 2024 16:57:58 -0300 Subject: [PATCH 01/19] Unexport `MTL` --- lib/mps/MPS.jl | 1 + src/Metal.jl | 3 ++- test/capturing.jl | 1 + test/device/intrinsics.jl | 1 + test/execution.jl | 2 ++ test/metal.jl | 2 ++ test/mps/copy.jl | 4 ++-- test/mps/matrix.jl | 2 +- test/profiling.jl | 1 + 9 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/mps/MPS.jl b/lib/mps/MPS.jl index 2a4766504..5a15788da 100644 --- a/lib/mps/MPS.jl +++ b/lib/mps/MPS.jl @@ -9,6 +9,7 @@ refer to the [official Apple documentation](https://developer.apple.com/document module MPS using ..Metal +using Metal.MTL using CEnum using ObjectiveC, .Foundation diff --git a/src/Metal.jl b/src/Metal.jl index ad8adede2..a44f5f63a 100644 --- a/src/Metal.jl +++ b/src/Metal.jl @@ -16,7 +16,8 @@ using ObjectiveC, .CoreFoundation, .Foundation, .Dispatch, .OS # core library include("../lib/mtl/MTL.jl") -@reexport using .MTL +using .MTL +export MTL, Shared, Private, Managed # essential stuff include("state.jl") diff --git a/test/capturing.jl b/test/capturing.jl index 91df77673..76a3ad989 100644 --- a/test/capturing.jl +++ b/test/capturing.jl @@ -1,3 +1,4 @@ +using Metal.MTL @testset "capturing" begin mktempdir() do tmpdir cd(tmpdir) do diff --git a/test/device/intrinsics.jl b/test/device/intrinsics.jl index 4f31a5912..d02504145 100644 --- a/test/device/intrinsics.jl +++ b/test/device/intrinsics.jl @@ -1,4 +1,5 @@ using SpecialFunctions +using Metal: metal_support @testset "arguments" begin @on_device dispatch_quadgroups_per_threadgroup() diff --git a/test/execution.jl b/test/execution.jl index 75cd75755..9937a343a 100644 --- a/test/execution.jl +++ b/test/execution.jl @@ -1,3 +1,5 @@ +using Metal: macos_version, MTLCommandQueue + dummy() = return @testset "@metal" begin diff --git a/test/metal.jl b/test/metal.jl index 64614686a..7d15f97c4 100644 --- a/test/metal.jl +++ b/test/metal.jl @@ -1,3 +1,5 @@ +using Metal.MTL + @testset "MTL" begin @autoreleasepool begin diff --git a/test/mps/copy.jl b/test/mps/copy.jl index fd2456cd8..570b6e423 100644 --- a/test/mps/copy.jl +++ b/test/mps/copy.jl @@ -18,7 +18,7 @@ function copytest(src, srctrans, dsttrans) cpcols,cprows = size(dst) end - cmdbuf = MTLCommandBuffer(queue) do cbuf + cmdbuf = MTL.MTLCommandBuffer(queue) do cbuf srcMPS = MPS.MPSMatrix(src) dstMPS = MPS.MPSMatrix(dst) @@ -26,7 +26,7 @@ function copytest(src, srctrans, dsttrans) copykern = MPS.MPSMatrixCopy(dev, cprows, cpcols, srctrans, dsttrans) MPS.encode!(cbuf, copykern, copydesc) end - wait_completed(cmdbuf) + MTL.wait_completed(cmdbuf) return dst end diff --git a/test/mps/matrix.jl b/test/mps/matrix.jl index 37da8d0b2..c7546d236 100644 --- a/test/mps/matrix.jl +++ b/test/mps/matrix.jl @@ -1,7 +1,7 @@ # # matrix descriptor # - +using .MTL: MTLOrigin using .MPS: MPSMatrixDescriptor, MPSDataType @testset "MPSMatrixDescriptor" begin T = Float32 diff --git a/test/profiling.jl b/test/profiling.jl index f280c2302..e6949e8bc 100644 --- a/test/profiling.jl +++ b/test/profiling.jl @@ -1,3 +1,4 @@ +using Metal: macos_version @testset "profiling" begin # determine if we can even run these tests From 00b1044071d69f90067a5c715a2d43874ffc1467 Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Sat, 13 Jul 2024 18:02:19 -0300 Subject: [PATCH 02/19] Export `encode!` from MPS --- lib/mps/MPS.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/mps/MPS.jl b/lib/mps/MPS.jl index 5a15788da..8004ee72d 100644 --- a/lib/mps/MPS.jl +++ b/lib/mps/MPS.jl @@ -23,6 +23,7 @@ is_supported(dev::MTLDevice) = ccall(:MPSSupportsMTLDevice, Bool, (id{MTLDevice} include("size.jl") # high-level wrappers +export encode! # Defined in multiple files include("command_buf.jl") include("kernel.jl") include("images.jl") From f22e52b24cdbd4e0efca25ec711e17ea905283a3 Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Thu, 18 Jul 2024 10:53:04 -0300 Subject: [PATCH 03/19] Feedback --- lib/mps/MPS.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mps/MPS.jl b/lib/mps/MPS.jl index 8004ee72d..9ec965efe 100644 --- a/lib/mps/MPS.jl +++ b/lib/mps/MPS.jl @@ -9,7 +9,7 @@ refer to the [official Apple documentation](https://developer.apple.com/document module MPS using ..Metal -using Metal.MTL +using .MTL using CEnum using ObjectiveC, .Foundation From 75ee43575cfd4929073b4c5dd76bf13bd5ee6e32 Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:06:06 -0300 Subject: [PATCH 04/19] Move versions.jl to `Metal` module. Add docstrings --- docs/src/api/essentials.md | 8 ++++++++ lib/mtl/MTL.jl | 3 ++- src/Metal.jl | 2 ++ {lib/mtl => src}/version.jl | 40 +++++++++++++++++++++++++++++++++---- test/execution.jl | 3 +-- 5 files changed, 49 insertions(+), 7 deletions(-) rename {lib/mtl => src}/version.jl (77%) diff --git a/docs/src/api/essentials.md b/docs/src/api/essentials.md index d1fc0da6f..0536da2d3 100644 --- a/docs/src/api/essentials.md +++ b/docs/src/api/essentials.md @@ -1,5 +1,13 @@ # Essentials +## Versions and Support +```@docs +macos_version +Metal.darwin_version +Metal.metal_support +Metal.metallib_support +Metal.air_support +``` ## Global State diff --git a/lib/mtl/MTL.jl b/lib/mtl/MTL.jl index 125d096e2..e8d77898c 100644 --- a/lib/mtl/MTL.jl +++ b/lib/mtl/MTL.jl @@ -11,6 +11,8 @@ module MTL using CEnum using ObjectiveC, .Foundation, .Dispatch +using ..Metal + # Metal APIs generally expect to be running under an autorelease pool. # In most cases, we handle this in the code calling into the MTL module, # however, finalizers are out of the caller's control, so we need to @@ -20,7 +22,6 @@ release(obj) = @autoreleasepool unsafe=true Foundation.release(obj) ## source code includes -include("version.jl") include("size.jl") include("device.jl") include("resource.jl") diff --git a/src/Metal.jl b/src/Metal.jl index a44f5f63a..c8af0d2b2 100644 --- a/src/Metal.jl +++ b/src/Metal.jl @@ -14,6 +14,8 @@ using ExprTools: splitdef, combinedef using Artifacts using ObjectiveC, .CoreFoundation, .Foundation, .Dispatch, .OS +include("version.jl") + # core library include("../lib/mtl/MTL.jl") using .MTL diff --git a/lib/mtl/version.jl b/src/version.jl similarity index 77% rename from lib/mtl/version.jl rename to src/version.jl index f95562db0..89db5876a 100644 --- a/lib/mtl/version.jl +++ b/src/version.jl @@ -1,6 +1,6 @@ # version and support queries -export darwin_version, macos_version, metallib_support, air_support, metal_support +export macos_version @noinline function _syscall_version(name) size = Ref{Csize_t}() @@ -18,6 +18,13 @@ export darwin_version, macos_version, metallib_support, air_support, metal_suppo end const _darwin_version = Ref{VersionNumber}() +""" + Metal.darwin_version() -> VersionNumber + +Returns the host Darwin kernel version. + +See also [`macos_version`](@ref). +""" function darwin_version() if !isassigned(_darwin_version) _darwin_version[] = _syscall_version("kern.osrelease") @@ -26,6 +33,13 @@ function darwin_version() end const _macos_version = Ref{VersionNumber}() +""" + macos_version() -> VersionNumber + +Returns the host macOS version. + +See also [`Metal.darwin_version`](@ref). +""" function macos_version() if !isassigned(_macos_version) _macos_version[] = _syscall_version("kern.osproductversion") @@ -46,7 +60,13 @@ end # - metal support: last 2 bytes of the VERS tag in the function list, # or air.language_version in the embedded bitcode -# support for the metallib file format +""" + Metal.metallib_support() -> VersionNumber + +Returns the highest supported version for the metallib file format. + +See also [`Metal.air_support`](@ref) and [`Metal.metal_support`](@ref). +""" function metallib_support() macos = macos_version() if macos >= v"15" @@ -68,7 +88,13 @@ function metallib_support() end end -# support for the embedded AIR bitcode format +""" + Metal.air_support() -> VersionNumber + +Returns the highest supported version for the embedded AIR bitcode format. + +See also [`Metal.metallib_support`](@ref) and [`Metal.metal_support`](@ref). +""" function air_support() macos = macos_version() if macos >= v"15" @@ -92,7 +118,13 @@ function air_support() end end -# support for the Metal language +""" + Metal.metal_support() -> VersionNumber + +Returns the highest supported version for the Metal Shading Language. + +See also [`Metal.metallib_support`](@ref) and [`Metal.air_support`](@ref). +""" function metal_support() macos = macos_version() if macos >= v"15" diff --git a/test/execution.jl b/test/execution.jl index 9937a343a..d0bd2f2c9 100644 --- a/test/execution.jl +++ b/test/execution.jl @@ -1,4 +1,3 @@ -using Metal: macos_version, MTLCommandQueue dummy() = return @@ -143,7 +142,7 @@ end vecA .= 0 dev = device() - queue = MTLCommandQueue(dev) + queue = MTL.MTLCommandQueue(dev) @metal threads=(3) queue=queue tester(bufferA) synchronize(queue) @test all(vecA == Int.([5, 5, 5, 0, 0, 0, 0, 0])) From 8092a5f3a8e2cc6bf7f23a5d1165cf56ff76bbc4 Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:40:27 -0300 Subject: [PATCH 05/19] Don't export `macos_version` --- docs/src/api/essentials.md | 2 +- lib/mtl/library.jl | 2 +- src/version.jl | 6 ++---- test/execution.jl | 6 +++--- test/profiling.jl | 3 +-- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/docs/src/api/essentials.md b/docs/src/api/essentials.md index 0536da2d3..175c6680f 100644 --- a/docs/src/api/essentials.md +++ b/docs/src/api/essentials.md @@ -2,7 +2,7 @@ ## Versions and Support ```@docs -macos_version +Metal.macos_version Metal.darwin_version Metal.metal_support Metal.metallib_support diff --git a/lib/mtl/library.jl b/lib/mtl/library.jl index f9037b535..60d11faa8 100644 --- a/lib/mtl/library.jl +++ b/lib/mtl/library.jl @@ -23,7 +23,7 @@ end function MTLLibraryFromFile(dev::MTLDevice, path::String) err = Ref{id{NSError}}(nil) - handle = if macos_version() >= v"13" + handle = if Metal.macos_version() >= v"13" url = NSFileURL(path) @objc [dev::id{MTLDevice} newLibraryWithURL:url::id{NSURL} error:err::Ptr{id{NSError}}]::id{MTLLibrary} diff --git a/src/version.jl b/src/version.jl index 89db5876a..91108b52c 100644 --- a/src/version.jl +++ b/src/version.jl @@ -1,7 +1,5 @@ # version and support queries -export macos_version - @noinline function _syscall_version(name) size = Ref{Csize_t}() err = @ccall sysctlbyname(name::Cstring, C_NULL::Ptr{Cvoid}, size::Ptr{Csize_t}, @@ -23,7 +21,7 @@ const _darwin_version = Ref{VersionNumber}() Returns the host Darwin kernel version. -See also [`macos_version`](@ref). +See also [`Metal.macos_version`](@ref). """ function darwin_version() if !isassigned(_darwin_version) @@ -34,7 +32,7 @@ end const _macos_version = Ref{VersionNumber}() """ - macos_version() -> VersionNumber + Metal.macos_version() -> VersionNumber Returns the host macOS version. diff --git a/test/execution.jl b/test/execution.jl index d0bd2f2c9..9ab3aa31f 100644 --- a/test/execution.jl +++ b/test/execution.jl @@ -52,7 +52,7 @@ end Metal.code_typed(dummy, Tuple{}) Metal.code_warntype(devnull, dummy, Tuple{}) Metal.code_llvm(devnull, dummy, Tuple{}) - if macos_version() >= v"13" + if Metal.macos_version() >= v"13" Metal.code_agx(devnull, dummy, Tuple{}) end @@ -60,7 +60,7 @@ end @device_code_typed @metal dummy() @device_code_warntype io=devnull @metal dummy() @device_code_llvm io=devnull @metal dummy() - if macos_version() >= v"13" + if Metal.macos_version() >= v"13" @device_code_agx io=devnull @metal dummy() end @@ -73,7 +73,7 @@ end # make sure kernel name aliases are preserved in the generated code @test occursin("dummy", sprint(io->(@device_code_llvm io=io optimize=false @metal dummy()))) @test occursin("dummy", sprint(io->(@device_code_llvm io=io @metal dummy()))) - if macos_version() >= v"13" + if Metal.macos_version() >= v"13" @test occursin("dummy", sprint(io->(@device_code_agx io=io @metal dummy()))) end diff --git a/test/profiling.jl b/test/profiling.jl index e6949e8bc..72be12ea2 100644 --- a/test/profiling.jl +++ b/test/profiling.jl @@ -1,4 +1,3 @@ -using Metal: macos_version @testset "profiling" begin # determine if we can even run these tests @@ -14,7 +13,7 @@ else error("Could not parse xctrace version output:\n$version_output") else xcode_version = VersionNumber(parse(Int, m.captures[1]), parse(Int, m.captures[2])) - if MTL.is_m1(device()) && macos_version() >= v"14.4" && xcode_version < v"15.3" + if MTL.is_m1(device()) && Metal.macos_version() >= v"14.4" && xcode_version < v"15.3" @warn "Skipping profiling tests because of an M1-related bug on macOS 14.4 and Xcode < 15.3; please upgrade Xcode first" else run_tests = true From 2b864c76fb0a33269dc5dd197076f09703ff9a2b Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Fri, 19 Jul 2024 09:29:39 -0300 Subject: [PATCH 06/19] Fix tests? --- bin/metallib-as | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/metallib-as b/bin/metallib-as index 4bbe7048d..d6207775a 100755 --- a/bin/metallib-as +++ b/bin/metallib-as @@ -14,8 +14,8 @@ function main(args) force = false output = nothing downgrade = false - air_version = air_support() - metal_version = metal_support() + air_version = Metal.air_support() + metal_version = Metal.metal_support() usage = "metallib-as [-h] [-f] [-d] [-l] [-o output.metallib] inputs.(bc|ll)..." while !isempty(args) arg = popfirst!(args) From 73a6315cb08832c41746879e4da2b5895e75f2cf Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 19 Jul 2024 14:54:13 +0200 Subject: [PATCH 07/19] Fix script tests. --- bin/metallib-load | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/metallib-load b/bin/metallib-load index 637866925..3b17bc5c6 100755 --- a/bin/metallib-load +++ b/bin/metallib-load @@ -5,7 +5,7 @@ DIR="$(dirname "$(cd "$(dirname "$0")" && pwd)")" exec julia --project="$DIR" "$0" "$@" =# -using Metal +using Metal, .MTL function main(args) # parse arguments From 407df4193595b570731c60a6248ef2eff77f6a7d Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 19 Jul 2024 15:32:57 +0200 Subject: [PATCH 08/19] MPS: Repeat encode! in export lists. --- lib/mps/MPS.jl | 1 - lib/mps/copy.jl | 2 +- lib/mps/decomposition.jl | 4 ++-- lib/mps/images.jl | 15 ++++++++++++++- lib/mps/matrix.jl | 6 ++++-- lib/mps/vector.jl | 2 +- 6 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/mps/MPS.jl b/lib/mps/MPS.jl index 9ec965efe..2bb794187 100644 --- a/lib/mps/MPS.jl +++ b/lib/mps/MPS.jl @@ -23,7 +23,6 @@ is_supported(dev::MTLDevice) = ccall(:MPSSupportsMTLDevice, Bool, (id{MTLDevice} include("size.jl") # high-level wrappers -export encode! # Defined in multiple files include("command_buf.jl") include("kernel.jl") include("images.jl") diff --git a/lib/mps/copy.jl b/lib/mps/copy.jl index 1a6b1893d..2aa4a9053 100644 --- a/lib/mps/copy.jl +++ b/lib/mps/copy.jl @@ -21,7 +21,7 @@ end ## kernel -export MPSMatrixCopy +export MPSMatrixCopy, encode! @objcwrapper immutable=false MPSMatrixCopy <: MPSKernel diff --git a/lib/mps/decomposition.jl b/lib/mps/decomposition.jl index 8bf4f67ce..ce241cf27 100644 --- a/lib/mps/decomposition.jl +++ b/lib/mps/decomposition.jl @@ -8,7 +8,7 @@ end ## lu -export MPSMatrixDecompositionLU +export MPSMatrixDecompositionLU, encode! @objcwrapper immutable=false MPSMatrixDecompositionLU <: MPSMatrixUnaryKernel @@ -33,7 +33,7 @@ end ## cholesky -export MPSMatrixDecompositionCholesky +export MPSMatrixDecompositionCholesky, encode! @objcwrapper immutable=false MPSMatrixDecompositionCholesky <: MPSMatrixUnaryKernel diff --git a/lib/mps/images.jl b/lib/mps/images.jl index c1b6d6f17..7b8cc250c 100644 --- a/lib/mps/images.jl +++ b/lib/mps/images.jl @@ -1,3 +1,5 @@ +## kernels + @cenum MPSImageEdgeMode::NSUInteger begin MPSImageEdgeModeZero = 0 MPSImageEdgeModeClamp = 1 @@ -37,6 +39,11 @@ end @autoproperty clipRect::MTLRegion end + +## gaussian blur + +export MPSImageGaussianBlur, encode! + @objcwrapper immutable=false MPSImageGaussianBlur <: MPSUnaryImageKernel function MPSImageGaussianBlur(dev, sigma) @@ -49,6 +56,10 @@ function MPSImageGaussianBlur(dev, sigma) end +## image box + +export MPSImageBox + @objcwrapper immutable=false MPSImageBox <: MPSUnaryImageKernel function MPSImageBox(dev, kernelWidth, kernelHeight) @@ -62,7 +73,9 @@ function MPSImageBox(dev, kernelWidth, kernelHeight) end -# High-level functions for image blurring +## high-level blurring functionality + +export blur, gaussianblur, boxblur function blur(image, kernel; pixelFormat=MTL.MTLPixelFormatRGBA8Unorm) res = copy(image) diff --git a/lib/mps/matrix.jl b/lib/mps/matrix.jl index 2e650241f..9626b762c 100644 --- a/lib/mps/matrix.jl +++ b/lib/mps/matrix.jl @@ -167,7 +167,7 @@ end ## matrix multiplication -export MPSMatrixMultiplication, matmul! +export MPSMatrixMultiplication, encode!, matmul! @objcwrapper immutable=false MPSMatrixMultiplication <: MPSKernel @@ -242,7 +242,7 @@ end ## topk -export MPSMatrixFindTopK, topk, topk! +export MPSMatrixFindTopK, encode!, topk, topk! @objcwrapper immutable=false MPSMatrixFindTopK <: MPSMatrixUnaryKernel @@ -333,6 +333,8 @@ end ## softmax +export MPSMatrixSoftMax, MPSMatrixLogSoftMax, encode! + @objcwrapper immutable=false MPSMatrixSoftMax <: MPSMatrixUnaryKernel @objcwrapper immutable=false MPSMatrixLogSoftMax <: MPSMatrixSoftMax diff --git a/lib/mps/vector.jl b/lib/mps/vector.jl index 4de641dae..7e016bc81 100644 --- a/lib/mps/vector.jl +++ b/lib/mps/vector.jl @@ -85,7 +85,7 @@ end ## matrix vector multiplication -export MPSMatrixVectorMultiplication, matvecmul! +export MPSMatrixVectorMultiplication, encode!, matvecmul! @objcwrapper immutable=false MPSMatrixVectorMultiplication <: MPSMatrixBinaryKernel From 35cfdf42d09ac4b0fe0c55c666caebe27a919680 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 19 Jul 2024 15:33:06 +0200 Subject: [PATCH 09/19] Clean-ups. --- test/capturing.jl | 5 ++++- test/execution.jl | 1 - test/metal.jl | 6 ++++-- test/mps/copy.jl | 2 -- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/test/capturing.jl b/test/capturing.jl index 76a3ad989..da30b801a 100644 --- a/test/capturing.jl +++ b/test/capturing.jl @@ -1,5 +1,7 @@ -using Metal.MTL @testset "capturing" begin + +using .MTL + mktempdir() do tmpdir cd(tmpdir) do @@ -79,4 +81,5 @@ end end end + end diff --git a/test/execution.jl b/test/execution.jl index 9ab3aa31f..5a8a6d195 100644 --- a/test/execution.jl +++ b/test/execution.jl @@ -1,4 +1,3 @@ - dummy() = return @testset "@metal" begin diff --git a/test/metal.jl b/test/metal.jl index 7d15f97c4..fe7ded3c3 100644 --- a/test/metal.jl +++ b/test/metal.jl @@ -1,6 +1,7 @@ -using Metal.MTL - @testset "MTL" begin + +using .MTL + @autoreleasepool begin @testset "devices" begin @@ -465,4 +466,5 @@ end # TODO: continue adding tests end + end diff --git a/test/mps/copy.jl b/test/mps/copy.jl index 570b6e423..ac20f533e 100644 --- a/test/mps/copy.jl +++ b/test/mps/copy.jl @@ -1,5 +1,3 @@ -using Metal, Test - # XXX: Why 64-bit Integers broken? Same behaviour with Swift const IGNORE_UNION = Union{Complex, Int64, UInt64} From 61ab374644d535677b8b953a109ca777402143c1 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 19 Jul 2024 17:07:55 +0200 Subject: [PATCH 10/19] Fix. --- test/capturing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/capturing.jl b/test/capturing.jl index da30b801a..d39357ce0 100644 --- a/test/capturing.jl +++ b/test/capturing.jl @@ -1,7 +1,7 @@ -@testset "capturing" begin - using .MTL +@testset "capturing" begin + mktempdir() do tmpdir cd(tmpdir) do From fd7060dfcc1c7c78d6e4323e0e2fa789d655a024 Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Fri, 19 Jul 2024 12:20:41 -0300 Subject: [PATCH 11/19] Don't export storage modes from `Metal` --- lib/mtl/storage_type.jl | 14 +++++++------- src/Metal.jl | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/mtl/storage_type.jl b/lib/mtl/storage_type.jl index 7e5762064..c7592e4f7 100644 --- a/lib/mtl/storage_type.jl +++ b/lib/mtl/storage_type.jl @@ -10,35 +10,35 @@ export ReadUsage, WriteUsage, ReadWriteUsage abstract type StorageMode end """ - struct Shared <: MTL.StorageMode + struct Shared <: Metal.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 [`Private`](@ref) and [`Managed`](@ref). +See also [`Metal.Private`](@ref) and [`Metal.Managed`](@ref). """ struct Shared <: StorageMode end """ - struct Managed <: MTL.StorageMode + struct Managed <: Metal.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 [`Shared`](@ref) and [`Private`](@ref). +See also [`Metal.Shared`](@ref) and [`Metal.Private`](@ref). """ struct Managed <: StorageMode end """ - struct Private <: MTL.StorageMode + struct Private <: Metal.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 [`Shared`](@ref) and [`Managed`](@ref). +See also [`Metal.Shared`](@ref) and [`Metal.Managed`](@ref). """ struct Private <: StorageMode end struct Memoryless <: StorageMode end @@ -46,7 +46,7 @@ struct Memoryless <: StorageMode end """ CPUStorage -Union type of [`Shared`](@ref) and [`Managed`](@ref) storage modes. +Union type of [`Metal.Shared`](@ref) and [`Metal.Managed`](@ref) storage modes. Represents storage modes where the resource is accessible via the CPU. """ diff --git a/src/Metal.jl b/src/Metal.jl index c8af0d2b2..57a3600b8 100644 --- a/src/Metal.jl +++ b/src/Metal.jl @@ -19,7 +19,7 @@ include("version.jl") # core library include("../lib/mtl/MTL.jl") using .MTL -export MTL, Shared, Private, Managed +export MTL # essential stuff include("state.jl") From a04627702f47e92e1e88005ab1bf59385168a3dc Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Fri, 19 Jul 2024 15:04:20 -0300 Subject: [PATCH 12/19] Upgrade Documenter to version 1 --- docs/Manifest.toml | 212 ++++++++++++++++++++++++++++++++++++++++----- docs/Project.toml | 4 + docs/make.jl | 2 +- 3 files changed, 194 insertions(+), 24 deletions(-) diff --git a/docs/Manifest.toml b/docs/Manifest.toml index 906f60049..829105f93 100644 --- a/docs/Manifest.toml +++ b/docs/Manifest.toml @@ -1,58 +1,142 @@ # This file is machine-generated - editing it directly is not advised -julia_version = "1.8.5" +julia_version = "1.10.4" manifest_format = "2.0" -project_hash = "2f8db419dfdb71e751be08e523f22250f9afa480" +project_hash = "547b38d4fef962d28135073fdff2e2cad080b03d" [[deps.ANSIColoredPrinters]] git-tree-sha1 = "574baf8110975760d391c710b6341da1afa48d8c" uuid = "a4c015fc-c6ff-483c-b24f-f7ea428134e9" version = "0.0.1" +[[deps.AbstractTrees]] +git-tree-sha1 = "2d9c9a55f9c93e8887ad391fbae72f8ef55e1177" +uuid = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" +version = "0.4.5" + +[[deps.ArgTools]] +uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" +version = "1.1.1" + +[[deps.Artifacts]] +uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" + [[deps.Base64]] uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" +[[deps.CodecZlib]] +deps = ["TranscodingStreams", "Zlib_jll"] +git-tree-sha1 = "b8fe8546d52ca154ac556809e10c75e6e7430ac8" +uuid = "944b1d66-785c-5afd-91f1-9de20f533193" +version = "0.7.5" + [[deps.Dates]] deps = ["Printf"] uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" [[deps.DocStringExtensions]] deps = ["LibGit2"] -git-tree-sha1 = "c36550cb29cbe373e95b3f40486b9a4148f89ffd" +git-tree-sha1 = "2fb1e02f2b635d0845df5d7c167fec4dd739b00d" uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" -version = "0.9.2" +version = "0.9.3" [[deps.Documenter]] -deps = ["ANSIColoredPrinters", "Base64", "Dates", "DocStringExtensions", "IOCapture", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "REPL", "Test", "Unicode"] -git-tree-sha1 = "6030186b00a38e9d0434518627426570aac2ef95" +deps = ["ANSIColoredPrinters", "AbstractTrees", "Base64", "CodecZlib", "Dates", "DocStringExtensions", "Downloads", "Git", "IOCapture", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "MarkdownAST", "Pkg", "PrecompileTools", "REPL", "RegistryInstances", "SHA", "TOML", "Test", "Unicode"] +git-tree-sha1 = "76deb8c15f37a3853f13ea2226b8f2577652de05" uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4" -version = "0.27.23" +version = "1.5.0" + +[[deps.Downloads]] +deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"] +uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" +version = "1.6.0" + +[[deps.Expat_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl"] +git-tree-sha1 = "1c6317308b9dc757616f0b5cb379db10494443a7" +uuid = "2e619515-83b5-522b-bb60-26c02a35a201" +version = "2.6.2+0" + +[[deps.FileWatching]] +uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee" + +[[deps.Git]] +deps = ["Git_jll"] +git-tree-sha1 = "04eff47b1354d702c3a85e8ab23d539bb7d5957e" +uuid = "d7ba0133-e1db-5d97-8f8c-041e4b3a1eb2" +version = "1.3.1" + +[[deps.Git_jll]] +deps = ["Artifacts", "Expat_jll", "JLLWrappers", "LibCURL_jll", "Libdl", "Libiconv_jll", "OpenSSL_jll", "PCRE2_jll", "Zlib_jll"] +git-tree-sha1 = "d18fb8a1f3609361ebda9bf029b60fd0f120c809" +uuid = "f8c6e375-362e-5223-8a59-34ff63f689eb" +version = "2.44.0+2" [[deps.IOCapture]] deps = ["Logging", "Random"] -git-tree-sha1 = "f7be53659ab06ddc986428d3a9dcc95f6fa6705a" +git-tree-sha1 = "b6d6bfdd7ce25b0f9b2f6b3dd56b2673a66c8770" uuid = "b5f81e59-6552-4d32-b1f0-c071b021bf89" -version = "0.2.2" +version = "0.2.5" [[deps.InteractiveUtils]] deps = ["Markdown"] uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +[[deps.JLLWrappers]] +deps = ["Artifacts", "Preferences"] +git-tree-sha1 = "7e5d6779a1e09a36db2a7b6cff50942a0a7d0fca" +uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" +version = "1.5.0" + [[deps.JSON]] deps = ["Dates", "Mmap", "Parsers", "Unicode"] -git-tree-sha1 = "3c837543ddb02250ef42f4738347454f95079d4e" +git-tree-sha1 = "31e996f0a15c7b280ba9f76636b3ff9e2ae58c9a" uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" -version = "0.21.3" +version = "0.21.4" + +[[deps.LazilyInitializedFields]] +git-tree-sha1 = "8f7f3cabab0fd1800699663533b6d5cb3fc0e612" +uuid = "0e77f7df-68c5-4e49-93ce-4cd80f5598bf" +version = "1.2.2" + +[[deps.LibCURL]] +deps = ["LibCURL_jll", "MozillaCACerts_jll"] +uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" +version = "0.6.4" + +[[deps.LibCURL_jll]] +deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] +uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" +version = "8.4.0+0" [[deps.LibGit2]] -deps = ["Base64", "NetworkOptions", "Printf", "SHA"] +deps = ["Base64", "LibGit2_jll", "NetworkOptions", "Printf", "SHA"] uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" +[[deps.LibGit2_jll]] +deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll"] +uuid = "e37daf67-58a4-590a-8e99-b0245dd2ffc5" +version = "1.6.4+0" + +[[deps.LibSSH2_jll]] +deps = ["Artifacts", "Libdl", "MbedTLS_jll"] +uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" +version = "1.11.0+1" + +[[deps.Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[deps.Libiconv_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl"] +git-tree-sha1 = "f9557a255370125b405568f9767d6d195822a175" +uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531" +version = "1.17.0+0" + [[deps.Literate]] deps = ["Base64", "IOCapture", "JSON", "REPL"] -git-tree-sha1 = "1c4418beaa6664041e0f9b48f0710f57bff2fcbe" +git-tree-sha1 = "eef2e1fc1dc38af90a18eb16e519e06d1fd10c2a" uuid = "98b081ad-f1c9-55d3-8b20-4c87d4299306" -version = "2.14.0" +version = "2.19.0" [[deps.Logging]] uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" @@ -61,18 +145,61 @@ uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" deps = ["Base64"] uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" +[[deps.MarkdownAST]] +deps = ["AbstractTrees", "Markdown"] +git-tree-sha1 = "465a70f0fc7d443a00dcdc3267a497397b8a3899" +uuid = "d0879d2d-cac2-40c8-9cee-1863dc0c7391" +version = "0.1.2" + +[[deps.MbedTLS_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" +version = "2.28.2+1" + [[deps.Mmap]] uuid = "a63ad114-7e13-5084-954f-fe012c677804" +[[deps.MozillaCACerts_jll]] +uuid = "14a3606d-f60d-562e-9121-12d972cd8159" +version = "2023.1.10" + [[deps.NetworkOptions]] uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" version = "1.2.0" +[[deps.OpenSSL_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl"] +git-tree-sha1 = "a028ee3cb5641cccc4c24e90c36b0a4f7707bdf5" +uuid = "458c3c95-2e84-50aa-8efc-19380b2a3a95" +version = "3.0.14+0" + +[[deps.PCRE2_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "efcefdf7-47ab-520b-bdef-62a2eaa19f15" +version = "10.42.0+1" + [[deps.Parsers]] -deps = ["Dates", "SnoopPrecompile"] -git-tree-sha1 = "b64719e8b4504983c7fca6cc9db3ebc8acc2a4d6" +deps = ["Dates", "PrecompileTools", "UUIDs"] +git-tree-sha1 = "8489905bcdbcfac64d1daa51ca07c0d8f0283821" uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" -version = "2.5.1" +version = "2.8.1" + +[[deps.Pkg]] +deps = ["Artifacts", "Dates", "Downloads", "FileWatching", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] +uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" +version = "1.10.0" + +[[deps.PrecompileTools]] +deps = ["Preferences"] +git-tree-sha1 = "5aa36f7049a63a1528fe8f7c3f2113413ffd4e1f" +uuid = "aea7be01-6a6a-4083-8856-8a6e6704d82a" +version = "1.2.1" + +[[deps.Preferences]] +deps = ["TOML"] +git-tree-sha1 = "9306f6085165d270f7e3db02af26a400d580f5c6" +uuid = "21216c6a-2e73-6563-6e65-726566657250" +version = "1.4.3" [[deps.Printf]] deps = ["Unicode"] @@ -83,9 +210,15 @@ deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" [[deps.Random]] -deps = ["SHA", "Serialization"] +deps = ["SHA"] uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +[[deps.RegistryInstances]] +deps = ["LazilyInitializedFields", "Pkg", "TOML", "Tar"] +git-tree-sha1 = "ffd19052caf598b8653b99404058fce14828be51" +uuid = "2792f1a3-b283-48e8-9a74-f99dce5104f3" +version = "0.1.0" + [[deps.SHA]] uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" version = "0.7.0" @@ -93,17 +226,50 @@ version = "0.7.0" [[deps.Serialization]] uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" -[[deps.SnoopPrecompile]] -git-tree-sha1 = "f604441450a3c0569830946e5b33b78c928e1a85" -uuid = "66db9d55-30c0-4569-8b51-7e840670fc0c" -version = "1.0.1" - [[deps.Sockets]] uuid = "6462fe0b-24de-5631-8697-dd941f90decc" +[[deps.TOML]] +deps = ["Dates"] +uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" +version = "1.0.3" + +[[deps.Tar]] +deps = ["ArgTools", "SHA"] +uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" +version = "1.10.0" + [[deps.Test]] deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +[[deps.TranscodingStreams]] +git-tree-sha1 = "60df3f8126263c0d6b357b9a1017bb94f53e3582" +uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" +version = "0.11.0" +weakdeps = ["Random", "Test"] + + [deps.TranscodingStreams.extensions] + TestExt = ["Test", "Random"] + +[[deps.UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + [[deps.Unicode]] uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" + +[[deps.Zlib_jll]] +deps = ["Libdl"] +uuid = "83775a58-1f1d-513f-b197-d71354ab007a" +version = "1.2.13+1" + +[[deps.nghttp2_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" +version = "1.52.0+1" + +[[deps.p7zip_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" +version = "17.4.0+2" diff --git a/docs/Project.toml b/docs/Project.toml index ed63661e6..83d2355da 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -2,3 +2,7 @@ Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" + +[compat] +Documenter = "1" +Literate = "2" diff --git a/docs/make.jl b/docs/make.jl index ba3cecd27..1edd729b5 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -21,7 +21,7 @@ function main() analytics = "", # TODO ), doctest = true, - #strict = true, + warnonly = true, modules = [Metal], pages = Any[ "Home" => "index.md", From 86c2cc4f748b31a6d28df36e1968b272f5c4b3e7 Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Fri, 19 Jul 2024 15:35:38 -0300 Subject: [PATCH 13/19] Rename storage modes --- .buildkite/pipeline.yml | 4 +-- LocalPreferences.toml | 4 +-- docs/src/api/array.md | 10 +++---- docs/src/api/essentials.md | 12 ++++---- docs/src/usage/array.md | 20 +++++++------- examples/gtk.jl | 2 +- examples/unified_memory.jl | 4 +-- lib/mps/linalg.jl | 4 +-- lib/mtl/buffer.jl | 8 +++--- lib/mtl/storage_type.jl | 42 ++++++++++++++-------------- src/array.jl | 56 +++++++++++++++++++------------------- src/broadcast.jl | 2 +- src/compiler/execution.jl | 2 +- src/memory.jl | 6 ++-- src/pool.jl | 8 +++--- test/array.jl | 42 ++++++++++++++-------------- test/capturing.jl | 2 +- test/device/intrinsics.jl | 10 +++---- test/execution.jl | 2 +- test/metal.jl | 2 +- test/runtests.jl | 2 +- test/scripts.jl | 8 +++--- 22 files changed, 126 insertions(+), 126 deletions(-) diff --git a/.buildkite/pipeline.yml b/.buildkite/pipeline.yml index f347b5ff1..254672495 100644 --- a/.buildkite/pipeline.yml +++ b/.buildkite/pipeline.yml @@ -54,8 +54,8 @@ steps: matrix: setup: storage: - - "Shared" - - "Managed" + - "SharedStorage" + - "ManagedStorage" commands: | echo -e "[Metal]\ndefault_storage = \"{{matrix.storage}}\"" >LocalPreferences.toml - label: "API validation" diff --git a/LocalPreferences.toml b/LocalPreferences.toml index 4e4d2854c..7f1c333c4 100644 --- a/LocalPreferences.toml +++ b/LocalPreferences.toml @@ -1,4 +1,4 @@ [Metal] # which storage mode unspecified allocations should default to. -# possible values: "Private", "Shared", "Managed" -#default_storage = "Private" +# possible values: "PrivateStorage", "SharedStorage", "ManagedStorage" +#default_storage = "PrivateStorage" diff --git a/docs/src/api/array.md b/docs/src/api/array.md index e8a4677f5..408286b3a 100644 --- a/docs/src/api/array.md +++ b/docs/src/api/array.md @@ -15,13 +15,13 @@ MtlVecOrMat ## Storage modes -The Metal API has various storage modes that dictate how a resource can be accessed. `MtlArray`s are `Private` by default, but they can also be `Shared` or `Managed`. For more information on storage modes, see the official [Metal documentation](https://developer.apple.com/documentation/metal/resource_fundamentals/setting_resource_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](https://developer.apple.com/documentation/metal/resource_fundamentals/setting_resource_storage_modes). ```@docs -Private -Shared -Managed -CPUStorage +Metal.PrivateStorage +Metal.SharedStorage +Metal.ManagedStorage +Metal.CPUStorage ``` There also exist the following convenience functions to check if an MtlArray is using a specific storage mode: diff --git a/docs/src/api/essentials.md b/docs/src/api/essentials.md index 175c6680f..122b9d2f2 100644 --- a/docs/src/api/essentials.md +++ b/docs/src/api/essentials.md @@ -12,10 +12,10 @@ Metal.air_support ## Global State ```@docs -device! -devices -device -global_queue -synchronize -device_synchronize +Metal.device! +Metal.devices +Metal.device +Metal.global_queue +Metal.synchronize +Metal.device_synchronize ``` diff --git a/docs/src/usage/array.md b/docs/src/usage/array.md index e7ae4b016..2a85d99f0 100644 --- a/docs/src/usage/array.md +++ b/docs/src/usage/array.md @@ -28,16 +28,16 @@ can construct `MtlArray`s in the same way as regular `Array` objects: ```jldoctest julia> MtlArray{Int}(undef, 2) -2-element MtlVector{Int64, Private}: +2-element MtlVector{Int64, Metal.MTL.PrivateStorage}: 0 0 julia> MtlArray{Int}(undef, (1,2)) -1×2 MtlMatrix{Int64, Private}: +1×2 MtlMatrix{Int64, Metal.MTL.PrivateStorage}: 0 0 julia> similar(ans) -1×2 MtlMatrix{Int64, Private}: +1×2 MtlMatrix{Int64, Metal.MTL.PrivateStorage}: 0 0 ``` @@ -46,7 +46,7 @@ Copying memory to or from the GPU can be expressed using constructors as well, o ```jldoctest julia> a = MtlArray([1,2]) -2-element MtlVector{Int64, Private}: +2-element MtlVector{Int64, Metal.MTL.PrivateStorage}: 1 2 @@ -73,11 +73,11 @@ perform simple element-wise operations you can use `map` or `broadcast`: julia> a = MtlArray{Float32}(undef, (1,2)); julia> a .= 5 -1×2 MtlMatrix{Float32, Private}: +1×2 MtlMatrix{Float32, Metal.MTL.PrivateStorage}: 5.0 5.0 julia> map(sin, a) -1×2 MtlMatrix{Float32, Private}: +1×2 MtlMatrix{Float32, Metal.MTL.PrivateStorage}: -0.958924 -0.958924 ``` @@ -86,7 +86,7 @@ To reduce the dimensionality of arrays, Metal.jl implements the various flavours ```jldoctest julia> a = Metal.ones(2,3) -2×3 MtlMatrix{Float32, Private}: +2×3 MtlMatrix{Float32, Metal.MTL.PrivateStorage}: 1.0 1.0 1.0 1.0 1.0 1.0 @@ -94,15 +94,15 @@ julia> reduce(+, a) 6.0f0 julia> mapreduce(sin, *, a; dims=2) -2×1 MtlMatrix{Float32, Private}: +2×1 MtlMatrix{Float32, Metal.MTL.PrivateStorage}: 0.59582335 0.59582335 julia> b = Metal.zeros(1) -1-element MtlVector{Float32, Private}: +1-element MtlVector{Float32, Metal.MTL.PrivateStorage}: 0.0 julia> Base.mapreducedim!(identity, +, b, a) -1×1 MtlMatrix{Float32, Private}: +1×1 MtlMatrix{Float32, Metal.MTL.PrivateStorage}: 6.0 ``` diff --git a/examples/gtk.jl b/examples/gtk.jl index 83c4b3916..853db16c3 100644 --- a/examples/gtk.jl +++ b/examples/gtk.jl @@ -26,7 +26,7 @@ function generate(img, pos) return end -img = MtlMatrix{RGB{N0f8},Shared}(undef, 800,600) +img = MtlMatrix{RGB{N0f8},Metal.SharedStorage}(undef, 800,600) host = unsafe_wrap(Array{RGB{N0f8}}, img, size(img)) ## initial image diff --git a/examples/unified_memory.jl b/examples/unified_memory.jl index 30eacd530..dea44d027 100644 --- a/examples/unified_memory.jl +++ b/examples/unified_memory.jl @@ -26,7 +26,7 @@ end # be allocated, then wrapped by a CPU array...not the other way around. # Create a Metal array with a storage mode of shared (both CPU and GPU get access) -arr_mtl = Metal.zeros(Float32, (16,16); storage=Shared) +arr_mtl = Metal.zeros(Float32, (16,16); storage=Metal.SharedStorage) # Unsafe wrap the contents of the Metal array with a CPU array arr_cpu = unsafe_wrap(Array{Float32}, arr_mtl, size(arr_mtl)) @@ -71,7 +71,7 @@ end # Make larger arrays to make the kernel take non-trivial time # Create a Metal array with a default storage mode of shared (both CPU and GPU get access) -arr_mtl = Metal.zeros(Float32, 1024*1024; storage=Shared) +arr_mtl = Metal.zeros(Float32, 1024*1024; storage=Metal.SharedStorage) # Unsafe wrap the contents of the Metal array with a CPU array arr_cpu = unsafe_wrap(Array{Float32}, arr_mtl, size(arr_mtl)) dummy_mtl = MtlArray{Float32}(undef, 1) diff --git a/lib/mps/linalg.jl b/lib/mps/linalg.jl index c025ff501..1c29e75c0 100644 --- a/lib/mps/linalg.jl +++ b/lib/mps/linalg.jl @@ -118,7 +118,7 @@ LinearAlgebra.ipiv2perm(v::MtlVector{T}, maxi::Integer) where T = dev = device() queue = global_queue(dev) - At = MtlMatrix{T,Private}(undef, (N, M)) + At = MtlMatrix{T,PrivateStorage}(undef, (N, M)) mps_a = MPSMatrix(A) mps_at = MPSMatrix(At) @@ -176,7 +176,7 @@ end dev = device() queue = global_queue(dev) - At = MtlMatrix{T,Private}(undef, (N, M)) + At = MtlMatrix{T,PrivateStorage}(undef, (N, M)) mps_a = MPSMatrix(A) mps_at = MPSMatrix(At) diff --git a/lib/mtl/buffer.jl b/lib/mtl/buffer.jl index aad8fd14d..bf0b8d637 100644 --- a/lib/mtl/buffer.jl +++ b/lib/mtl/buffer.jl @@ -23,7 +23,7 @@ end ## allocation function MTLBuffer(dev::Union{MTLDevice,MTLHeap}, bytesize::Integer; - storage=Private, hazard_tracking=DefaultTracking, + storage=PrivateStorage, hazard_tracking=DefaultTracking, cache_mode=DefaultCPUCache) opts = convert(MTLResourceOptions, storage) | hazard_tracking | cache_mode @@ -34,9 +34,9 @@ function MTLBuffer(dev::Union{MTLDevice,MTLHeap}, bytesize::Integer; end function MTLBuffer(dev::MTLDevice, bytesize::Integer, ptr::Ptr; - nocopy=false, storage=Shared, hazard_tracking=DefaultTracking, + nocopy=false, storage=SharedStorage, hazard_tracking=DefaultTracking, cache_mode=DefaultCPUCache) - storage == Private && error("Cannot allocate-and-initialize a Private buffer") + storage == PrivateStorage && error("Cannot allocate-and-initialize a PrivateStorage buffer") opts = convert(MTLResourceOptions, storage) | hazard_tracking | cache_mode @assert 0 < bytesize <= dev.maxBufferLength @@ -92,7 +92,7 @@ alloc_buffer(dev::MTLHeap, bytesize, opts) = Notifies the GPU that the range of bytes specified by `range` have been modified on the CPU, and that they should be transferred to the device before executing any following command. -Only valid for `Managed` buffers. +Only valid for `ManagedStorage` buffers. """ function DidModifyRange!(buf::MTLBuffer, range) @objc [buf::id{MTLBuffer} didModifyRange:range::NSRange]::Nothing diff --git a/lib/mtl/storage_type.jl b/lib/mtl/storage_type.jl index c7592e4f7..c1d20b2d0 100644 --- a/lib/mtl/storage_type.jl +++ b/lib/mtl/storage_type.jl @@ -1,64 +1,64 @@ -export Shared, Managed, Private, CPUStorage +export SharedStorage, ManagedStorage, PrivateStorage, CPUStorage export ReadUsage, WriteUsage, ReadWriteUsage # Metal Has 4 storage types -# Shared -> Buffer in Host memory, accessed by the GPU. Requires no sync -# Managed -> Mirrored memory buffers in host and GPU. Requires syncing -# Private -> Memory in Device, not accessible by Host. +# SharedStorage -> Buffer in Host memory, accessed by the GPU. Requires no sync +# ManagedStorage -> Mirrored memory buffers in host and GPU. Requires syncing +# PrivateStorage -> Memory in Device, not accessible by Host. # Memoryless -> iOS stuff. ignore it abstract type StorageMode end """ - struct Shared <: Metal.StorageMode + struct SharedStorage <: Metal.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.Private`](@ref) and [`Metal.Managed`](@ref). +See also [`Metal.PrivateStorage`](@ref) and [`Metal.ManagedStorage`](@ref). """ -struct Shared <: StorageMode end +struct SharedStorage <: StorageMode end """ - struct Managed <: Metal.StorageMode + struct ManagedStorage <: Metal.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.Shared`](@ref) and [`Metal.Private`](@ref). +See also [`Metal.SharedStorage`](@ref) and [`Metal.PrivateStorage`](@ref). """ -struct Managed <: StorageMode end +struct ManagedStorage <: StorageMode end """ - struct Private <: Metal.StorageMode + struct PrivateStorage <: Metal.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.Shared`](@ref) and [`Metal.Managed`](@ref). +See also [`Metal.SharedStorage`](@ref) and [`Metal.ManagedStorage`](@ref). """ -struct Private <: StorageMode end +struct PrivateStorage <: StorageMode end struct Memoryless <: StorageMode end """ CPUStorage -Union type of [`Metal.Shared`](@ref) and [`Metal.Managed`](@ref) storage modes. +Union type of [`Metal.SharedStorage`](@ref) and [`Metal.ManagedStorage`](@ref) storage modes. Represents storage modes where the resource is accessible via the CPU. """ -const CPUStorage = Union{Shared,Managed} -Base.convert(::Type{MTLStorageMode}, ::Type{Shared}) = MTLStorageModeShared -Base.convert(::Type{MTLStorageMode}, ::Type{Managed}) = MTLStorageModeManaged -Base.convert(::Type{MTLStorageMode}, ::Type{Private}) = MTLStorageModePrivate +const CPUStorage = Union{SharedStorage,ManagedStorage} +Base.convert(::Type{MTLStorageMode}, ::Type{SharedStorage}) = MTLStorageModeShared +Base.convert(::Type{MTLStorageMode}, ::Type{ManagedStorage}) = MTLStorageModeManaged +Base.convert(::Type{MTLStorageMode}, ::Type{PrivateStorage}) = MTLStorageModePrivate Base.convert(::Type{MTLStorageMode}, ::Type{Memoryless}) = MTLStorageModeMemoryless -Base.convert(::Type{MTLResourceOptions}, ::Type{Shared}) = MTLResourceStorageModeShared -Base.convert(::Type{MTLResourceOptions}, ::Type{Managed}) = MTLResourceStorageModeManaged -Base.convert(::Type{MTLResourceOptions}, ::Type{Private}) = MTLResourceStorageModePrivate +Base.convert(::Type{MTLResourceOptions}, ::Type{SharedStorage}) = MTLResourceStorageModeShared +Base.convert(::Type{MTLResourceOptions}, ::Type{ManagedStorage}) = MTLResourceStorageModeManaged +Base.convert(::Type{MTLResourceOptions}, ::Type{PrivateStorage}) = MTLResourceStorageModePrivate Base.convert(::Type{MTLResourceOptions}, ::Type{Memoryless}) = MTLResourceStorageModeMemoryless Base.convert(::Type{MTLResourceOptions}, SM::MTLStorageMode) = MTLResourceOptions(UInt(SM) << 4) diff --git a/src/array.jl b/src/array.jl index ee4b1f14c..8fbdbbe73 100644 --- a/src/array.jl +++ b/src/array.jl @@ -39,7 +39,7 @@ end `N`-dimensional Metal array with storage mode `S` and elements of type `T`. -`S` can be `Private` (default), `Shared`, or `Managed`. +`S` can be `Metal.PrivateStorage` (default), `Metal.SharedStorage`, or `Metal.ManagedStorage`. See the Array Programming section of the Metal.jl docs for more details. """ @@ -92,11 +92,11 @@ mutable struct MtlArray{T,N,S} <: AbstractGPUArray{T,N} check_eltype(T) storagemode = data[].storageMode obj = if storagemode == MTL.MTLStorageModeShared - new{T,N,Shared}(copy(data), maxsize, offset, dims) + new{T,N,SharedStorage}(copy(data), maxsize, offset, dims) elseif storagemode == MTL.MTLStorageModeManaged - new{T,N,Managed}(copy(data), maxsize, offset, dims) + new{T,N,ManagedStorage}(copy(data), maxsize, offset, dims) elseif storagemode == MTL.MTLStorageModePrivate - new{T,N,Private}(copy(data), maxsize, offset, dims) + new{T,N,PrivateStorage}(copy(data), maxsize, offset, dims) elseif storagemode == MTL.MTLStorageModeMemoryless new{T,N,Memoryless}(copy(data), maxsize, offset, dims) end @@ -127,29 +127,29 @@ storagemode(::Type{<:MtlArray{<:Any,<:Any,S}}) where {S} = S """ is_shared(A::MtlArray) -> Bool -Returns true if `A` has storage mode [`Shared`](@ref). +Returns true if `A` has storage mode [`Metal.SharedStorage`](@ref). See also [`is_private`](@ref) and [`is_managed`](@ref). """ -is_shared(A::MtlArray) = storagemode(A) == Shared +is_shared(A::MtlArray) = storagemode(A) == SharedStorage """ is_managed(A::MtlArray) -> Bool -Returns true if `A` has storage mode [`Managed`](@ref). +Returns true if `A` has storage mode [`Metal.ManagedStorage`](@ref). See also [`is_shared`](@ref) and [`is_private`](@ref). """ -is_managed(A::MtlArray) = storagemode(A) == Managed +is_managed(A::MtlArray) = storagemode(A) == ManagedStorage """ is_private(A::MtlArray) -> Bool -Returns true if `A` has storage mode [`Private`](@ref). +Returns true if `A` has storage mode [`Metal.PrivateStorage`](@ref). See also [`is_shared`](@ref) and [`is_managed`](@ref). """ -is_private(A::MtlArray) = storagemode(A) == Private +is_private(A::MtlArray) = storagemode(A) == PrivateStorage is_memoryless(A::MtlArray) = storagemode(A) == Memoryless @@ -185,13 +185,13 @@ See also `VecOrMat`(@ref) for examples. const MtlVecOrMat{T,S} = Union{MtlVector{T,S},MtlMatrix{T,S}} # default to private memory -const DefaultStorageMode = let str = @load_preference("default_storage", "Private") - if str == "Private" - Private - elseif str == "Shared" - Shared - elseif str == "Managed" - Managed +const DefaultStorageMode = let str = @load_preference("default_storage", "PrivateStorage") + if str == "PrivateStorage" + PrivateStorage + elseif str == "SharedStorage" + SharedStorage + elseif str == "ManagedStorage" + ManagedStorage else error("unknown default storage mode: $default_storage") end @@ -242,10 +242,10 @@ Base.elsize(::Type{<:MtlArray{T}}) where {T} = sizeof(T) Base.size(x::MtlArray) = x.dims Base.sizeof(x::MtlArray) = Base.elsize(x) * length(x) -@inline function Base.pointer(x::MtlArray{T}, i::Integer=1; storage=Private) where {T} - PT = if storage == Private +@inline function Base.pointer(x::MtlArray{T}, i::Integer=1; storage=PrivateStorage) where {T} + PT = if storage == PrivateStorage MtlPtr{T} - elseif storage == Shared || storage == Managed + elseif storage == SharedStorage || storage == ManagedStorage Ptr{T} else error("unknown memory type") @@ -270,12 +270,12 @@ end ## indexing -function Base.getindex(x::MtlArray{T,N,S}, I::Int) where {T,N,S<:Union{Shared,Managed}} +function Base.getindex(x::MtlArray{T,N,S}, I::Int) where {T,N,S<:Union{SharedStorage,ManagedStorage}} @boundscheck checkbounds(x, I) unsafe_load(pointer(x, I; storage=S)) end -function Base.setindex!(x::MtlArray{T,N,S}, v, I::Int) where {T,N,S<:Union{Shared,Managed}} +function Base.setindex!(x::MtlArray{T,N,S}, v, I::Int) where {T,N,S<:Union{SharedStorage,ManagedStorage}} @boundscheck checkbounds(x, I) unsafe_store!(pointer(x, I; storage=S), v) end @@ -462,9 +462,9 @@ Adapt.adapt_storage(::MtlArrayAdaptor{S}, xs::AbstractArray{T,N}) where {T<:Comp isbits(xs) ? xs : MtlArray{ComplexF32,N,S}(xs) """ - mtl(A; storage=Private) + mtl(A; storage=Metal.PrivateStorage) -`storage` can be `Private` (default) or `Shared`. +`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 @@ -480,18 +480,18 @@ Uses Adapt.jl to act inside some wrapper structs. ```jldoctests julia> mtl(ones(3)') -1×3 adjoint(::MtlVector{Float32, Private}) with eltype Float32: +1×3 adjoint(::MtlVector{Float32, Metal.MTL.PrivateStorage}) with eltype Float32: 1.0 1.0 1.0 -julia> mtl(zeros(1,3); storage=Shared) -1×3 MtlMatrix{Float32, Shared}: +julia> mtl(zeros(1,3); storage=Metal.SharedStorage) +1×3 MtlMatrix{Float32, Metal.MTL.SharedStorage}: 0.0 0.0 0.0 julia> mtl(1:3) 1:3 julia> MtlArray(1:3) -3-element MtlVector{Int64, Private}: +3-element MtlVector{Int64, Metal.MTL.PrivateStorage}: 1 2 3 diff --git a/src/broadcast.jl b/src/broadcast.jl index ed0eb734f..23efb801a 100644 --- a/src/broadcast.jl +++ b/src/broadcast.jl @@ -14,7 +14,7 @@ BroadcastStyle(W::Type{<:WrappedMtlArray{T,N}}) where {T,N} = # which one is better, so use shared memory BroadcastStyle(::MtlArrayStyle{N, S1}, ::MtlArrayStyle{N, S2}) where {N,S1,S2} = - MtlArrayStyle{N, Shared}() + MtlArrayStyle{N, SharedStorage}() # allocation of output arrays Base.similar(bc::Broadcasted{MtlArrayStyle{N,S}}, ::Type{T}, dims) where {T,N,S} = diff --git a/src/compiler/execution.jl b/src/compiler/execution.jl index 4808c6224..1fd4f3b9d 100644 --- a/src/compiler/execution.jl +++ b/src/compiler/execution.jl @@ -253,7 +253,7 @@ end end # pass by reference, in an argument buffer - argument_buffer = alloc(kernel.pipeline.device, sizeof(argtyp); storage=Shared) + argument_buffer = alloc(kernel.pipeline.device, sizeof(argtyp); storage=SharedStorage) argument_buffer.label = "MTLBuffer for kernel argument" unsafe_store!(convert(Ptr{argtyp}, argument_buffer), arg) return argument_buffer diff --git a/src/memory.jl b/src/memory.jl index 74bce49f8..2bdfbdf70 100644 --- a/src/memory.jl +++ b/src/memory.jl @@ -36,7 +36,7 @@ function Base.unsafe_copyto!(dev::MTLDevice, dst::MtlPtr{T}, src::Ptr{T}, N::Int if storage_type == MTL.MTLStorageModePrivate # stage through a shared buffer nocopy = MTL.can_alloc_nocopy(src, N*sizeof(T)) - tmp_buf = alloc(dev, N*sizeof(T), src; storage=Shared, nocopy) + tmp_buf = alloc(dev, N*sizeof(T), src; storage=SharedStorage, nocopy) # copy to the private buffer unsafe_copyto!(dev, MtlPtr{T}(dst.buffer, dst.offset), MtlPtr{T}(tmp_buf, 0), N; @@ -59,9 +59,9 @@ function Base.unsafe_copyto!(dev::MTLDevice, dst::Ptr{T}, src::MtlPtr{T}, N::Int # stage through a shared buffer nocopy = MTL.can_alloc_nocopy(dst, N*sizeof(T)) tmp_buf = if nocopy - alloc(dev, N*sizeof(T), dst; storage=Shared, nocopy) + alloc(dev, N*sizeof(T), dst; storage=SharedStorage, nocopy) else - alloc(dev, N*sizeof(T); storage=Shared) + alloc(dev, N*sizeof(T); storage=SharedStorage) end unsafe_copyto!(dev, MtlPtr{T}(tmp_buf, 0), MtlPtr{T}(src.buffer, src.offset), N; queue, async=(nocopy && async)) diff --git a/src/pool.jl b/src/pool.jl index f313871b2..4392e5234 100644 --- a/src/pool.jl +++ b/src/pool.jl @@ -42,13 +42,13 @@ otherwise it's zero-initialized. ! Note: You are responsible for freeing the returned buffer The storage kwarg controls where the buffer is stored. Possible values are: - - Private : Residing on the device - - Shared : Residing on the host - - Managed : Keeps two copies of the buffer, on device and on host. Explicit calls must be + - PrivateStorage : Residing on the device + - SharedStorage : Residing on the host + - ManagedStorage : Keeps two copies of the buffer, on device and on host. Explicit calls must be given to syncronize the two - Memoryless : an iOS specific thing that won't work on Mac. -Note that `Private` buffers can't be directly accessed from the CPU, therefore you cannot +Note that `PrivateStorage` buffers can't be directly accessed from the CPU, therefore you cannot use this option if you pass a ptr to initialize the memory. """ function alloc(dev::Union{MTLDevice,MTLHeap}, sz::Integer, args...; kwargs...) diff --git a/test/array.jl b/test/array.jl index 607e78be3..e44e1dbd7 100644 --- a/test/array.jl +++ b/test/array.jl @@ -1,4 +1,4 @@ -STORAGEMODES = [Private, Shared, Managed] +STORAGEMODES = [Metal.PrivateStorage, Metal.SharedStorage, Metal.ManagedStorage] @testset "array" begin @@ -51,8 +51,8 @@ end @test Adapt.adapt(MtlArray, [1 2;3 4]) isa MtlArray{Int, 2, Metal.DefaultStorageMode} @test Adapt.adapt(MtlArray{Float32}, [1 2;3 4]) isa MtlArray{Float32, 2, Metal.DefaultStorageMode} @test Adapt.adapt(MtlArray{Float32, 2}, [1 2;3 4]) isa MtlArray{Float32, 2, Metal.DefaultStorageMode} - @test Adapt.adapt(MtlArray{Float32, 2, Shared}, [1 2;3 4]) isa MtlArray{Float32, 2, Shared} - @test Adapt.adapt(MtlMatrix{ComplexF32, Shared}, [1 2;3 4]) isa MtlArray{ComplexF32, 2, Shared} + @test Adapt.adapt(MtlArray{Float32, 2, Metal.SharedStorage}, [1 2;3 4]) isa MtlArray{Float32, 2, Metal.SharedStorage} + @test Adapt.adapt(MtlMatrix{ComplexF32, Metal.SharedStorage}, [1 2;3 4]) isa MtlArray{ComplexF32, 2, Metal.SharedStorage} @test Adapt.adapt(MtlArray{Float16}, Float64[1]) isa MtlArray{Float16} # Test a few explicitly unsupported types @@ -132,35 +132,35 @@ check_storagemode(arr, smode) = Metal.storagemode(arr) == smode end # private storage errors. - if SM == Metal.Private - let arr_mtl = Metal.zeros(Float32, dim...; storage=Private) + if SM == Metal.PrivateStorage + let arr_mtl = Metal.zeros(Float32, dim...; storage=Metal.PrivateStorage) @test is_private(arr_mtl) && !is_shared(arr_mtl) && !is_managed(arr_mtl) @test_throws "Cannot access the contents of a private buffer" arr_cpu = unsafe_wrap(Array{Float32}, arr_mtl, dim) end let b = rand(Float32, 10) - arr_mtl = mtl(b; storage=Private) + arr_mtl = mtl(b; storage=Metal.PrivateStorage) @test_throws ErrorException arr_mtl[1] @test Metal.@allowscalar arr_mtl[1] == b[1] end - elseif SM == Metal.Shared - let arr_mtl = Metal.zeros(Float32, dim...; storage=Shared) + elseif SM == Metal.SharedStorage + let arr_mtl = Metal.zeros(Float32, dim...; storage=Metal.SharedStorage) @test !is_private(arr_mtl) && is_shared(arr_mtl) && !is_managed(arr_mtl) @test unsafe_wrap(Array{Float32}, arr_mtl) isa Array{Float32} end let b = rand(Float32, 10) - arr_mtl = mtl(b; storage=Shared) + arr_mtl = mtl(b; storage=Metal.SharedStorage) @test arr_mtl[1] == b[1] end - elseif SM == Metal.Managed - let arr_mtl = Metal.zeros(Float32, dim...; storage=Managed) + elseif SM == Metal.ManagedStorage + let arr_mtl = Metal.zeros(Float32, dim...; storage=Metal.ManagedStorage) @test !is_private(arr_mtl) && !is_shared(arr_mtl) && is_managed(arr_mtl) @test unsafe_wrap(Array{Float32}, arr_mtl) isa Array{Float32} end let b = rand(Float32, 10) - arr_mtl = mtl(b; storage=Managed) + arr_mtl = mtl(b; storage=Metal.ManagedStorage) @test arr_mtl[1] == b[1] end end @@ -177,8 +177,8 @@ end n1 = length(dim1) dim2 = dim1[1:2] n2 = length(dim2) - sm1 = Shared - sm2 = Private + sm1 = Metal.SharedStorage + sm2 = Metal.PrivateStorage arr = MtlArray{typ1, n1, sm1}(undef, dim1) @@ -283,18 +283,18 @@ end # https://github.com/JuliaGPU/CUDA.jl/issues/2191 @testset "preserving storage mode" begin - a = mtl([1]; storage=Shared) - @test Metal.storagemode(a) == Shared + a = mtl([1]; storage=Metal.SharedStorage) + @test Metal.storagemode(a) == Metal.SharedStorage # storage mode should be preserved b = a .+ 1 - @test Metal.storagemode(b) == Shared + @test Metal.storagemode(b) == Metal.SharedStorage # when there's a conflict, we should defer to shared memory - c = mtl([1]; storage=Private) - d = mtl([1]; storage=Shared) + c = mtl([1]; storage=Metal.PrivateStorage) + d = mtl([1]; storage=Metal.SharedStorage) e = c .+ d - @test Metal.storagemode(e) == Shared + @test Metal.storagemode(e) == Metal.SharedStorage end @testset "resizing" begin @@ -348,7 +348,7 @@ end @test all(marr1 .== 2) @test all(arr1 .== 2) - marr2 = Metal.zeros(Float32, 18000; storage=Shared); + marr2 = Metal.zeros(Float32, 18000; storage=Metal.SharedStorage); arr2 = unsafe_wrap(Vector{Float32}, marr2); @test all(arr2 .== 0) diff --git a/test/capturing.jl b/test/capturing.jl index d39357ce0..d6b4a330c 100644 --- a/test/capturing.jl +++ b/test/capturing.jl @@ -58,7 +58,7 @@ manager.defaultCaptureScope = new_scope @test manager.defaultCaptureScope == new_scope # Capturing -bufferA = MtlArray{Float32,1,Shared}(undef, tuple(4)) +bufferA = MtlArray{Float32,1,SharedStorage}(undef, tuple(4)) @test !isdir(path) @test manager.isCapturing == false diff --git a/test/device/intrinsics.jl b/test/device/intrinsics.jl index d02504145..b6070b701 100644 --- a/test/device/intrinsics.jl +++ b/test/device/intrinsics.jl @@ -106,7 +106,7 @@ end @testset "math" begin a = ones(Float32,1) a .* Float32(3.14) - bufferA = MtlArray{eltype(a),length(size(a)),Shared}(a) + bufferA = MtlArray{eltype(a),length(size(a)),Metal.SharedStorage}(a) vecA = unsafe_wrap(Vector{Float32}, pointer(bufferA), 1) function intr_test(arr) @@ -126,7 +126,7 @@ end @metal intr_test2(bufferA) synchronize() - bufferB = MtlArray{eltype(a),length(size(a)),Shared}(a) + bufferB = MtlArray{eltype(a),length(size(a)),Metal.SharedStorage}(a) vecB = unsafe_wrap(Vector{Float32}, pointer(bufferB), 1) function intr_test3(arr_sin, arr_cos) @@ -189,7 +189,7 @@ end @inbounds buf[idx] += 1 return nothing end - buf = Metal.zeros(Int, 1024; storage=Shared) + buf = Metal.zeros(Int, 1024; storage=Metal.SharedStorage) vec = unsafe_wrap(Vector{Int}, pointer(buf), size(buf)) @metal threads=length(buf) sync_test_kernel(buf) synchronize() @@ -333,8 +333,8 @@ end return end - dev_a = Metal.zeros(typ, 32; storage=Shared) - dev_b = Metal.zeros(typ, 32; storage=Shared) + dev_a = Metal.zeros(typ, 32; storage=Metal.SharedStorage) + dev_b = Metal.zeros(typ, 32; storage=Metal.SharedStorage) a = unsafe_wrap(Array{typ}, dev_a, 32) b = unsafe_wrap(Array{typ}, dev_b, 32) diff --git a/test/execution.jl b/test/execution.jl index 5a8a6d195..0a1b0dd4b 100644 --- a/test/execution.jl +++ b/test/execution.jl @@ -110,7 +110,7 @@ function tester(A) end bufferSize = 8 -bufferA = MtlArray{Int,1,Shared}(undef, tuple(bufferSize)) +bufferA = MtlArray{Int,1,Metal.SharedStorage}(undef, tuple(bufferSize)) vecA = unsafe_wrap(Vector{Int}, pointer(bufferA), tuple(bufferSize)) @testset "synchronization" begin diff --git a/test/metal.jl b/test/metal.jl index fe7ded3c3..065784409 100644 --- a/test/metal.jl +++ b/test/metal.jl @@ -236,7 +236,7 @@ end dev = first(devices()) -buf = MTLBuffer(dev, 8; storage=Shared) +buf = MTLBuffer(dev, 8; storage=SharedStorage) @test buf.length == 8 @test sizeof(buf) == 8 diff --git a/test/runtests.jl b/test/runtests.jl index 0dc553f99..187b5b9b8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -75,7 +75,7 @@ for (rootpath, dirs, files) in walkdir(@__DIR__) end ## GPUArrays testsuite for name in keys(TestSuite.tests) - if Metal.DefaultStorageMode != Private && name == "indexing scalar" + if Metal.DefaultStorageMode != Metal.PrivateStorage && name == "indexing scalar" # GPUArrays' scalar indexing tests assume that indexing is not supported continue end diff --git a/test/scripts.jl b/test/scripts.jl index 3f443face..21d68709c 100644 --- a/test/scripts.jl +++ b/test/scripts.jl @@ -4,11 +4,11 @@ using LLVM # for some reason, the environment shenanigans done by the scripts only work when # invoked from the Metal.jl CI, and not from GPUArrays.jl' reverse CI -if get(ENV, "BUILDKITE_PIPELINE_NAME", "") != "Metal.jl" +# if get(ENV, "BUILDKITE_PIPELINE_NAME", "") != "Metal.jl" -@warn "Skipping script tests" +# @warn "Skipping script tests" -else +# else mktempdir() do dir metallib_as = joinpath(dirname(@__DIR__), "bin", "metallib-as") @@ -50,4 +50,4 @@ end end -end +# end From b094602e2b8d2905cb8ea6b454de6567a458af59 Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:07:55 -0300 Subject: [PATCH 14/19] Remove MTL from MtlArray printing --- docs/src/usage/array.md | 20 ++++++++++---------- lib/mtl/storage_type.jl | 5 +++++ src/array.jl | 6 +++--- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/docs/src/usage/array.md b/docs/src/usage/array.md index 2a85d99f0..42e27db7b 100644 --- a/docs/src/usage/array.md +++ b/docs/src/usage/array.md @@ -28,16 +28,16 @@ can construct `MtlArray`s in the same way as regular `Array` objects: ```jldoctest julia> MtlArray{Int}(undef, 2) -2-element MtlVector{Int64, Metal.MTL.PrivateStorage}: +2-element MtlVector{Int64, Metal.PrivateStorage}: 0 0 julia> MtlArray{Int}(undef, (1,2)) -1×2 MtlMatrix{Int64, Metal.MTL.PrivateStorage}: +1×2 MtlMatrix{Int64, Metal.PrivateStorage}: 0 0 julia> similar(ans) -1×2 MtlMatrix{Int64, Metal.MTL.PrivateStorage}: +1×2 MtlMatrix{Int64, Metal.PrivateStorage}: 0 0 ``` @@ -46,7 +46,7 @@ Copying memory to or from the GPU can be expressed using constructors as well, o ```jldoctest julia> a = MtlArray([1,2]) -2-element MtlVector{Int64, Metal.MTL.PrivateStorage}: +2-element MtlVector{Int64, Metal.PrivateStorage}: 1 2 @@ -73,11 +73,11 @@ perform simple element-wise operations you can use `map` or `broadcast`: julia> a = MtlArray{Float32}(undef, (1,2)); julia> a .= 5 -1×2 MtlMatrix{Float32, Metal.MTL.PrivateStorage}: +1×2 MtlMatrix{Float32, Metal.PrivateStorage}: 5.0 5.0 julia> map(sin, a) -1×2 MtlMatrix{Float32, Metal.MTL.PrivateStorage}: +1×2 MtlMatrix{Float32, Metal.PrivateStorage}: -0.958924 -0.958924 ``` @@ -86,7 +86,7 @@ To reduce the dimensionality of arrays, Metal.jl implements the various flavours ```jldoctest julia> a = Metal.ones(2,3) -2×3 MtlMatrix{Float32, Metal.MTL.PrivateStorage}: +2×3 MtlMatrix{Float32, Metal.PrivateStorage}: 1.0 1.0 1.0 1.0 1.0 1.0 @@ -94,15 +94,15 @@ julia> reduce(+, a) 6.0f0 julia> mapreduce(sin, *, a; dims=2) -2×1 MtlMatrix{Float32, Metal.MTL.PrivateStorage}: +2×1 MtlMatrix{Float32, Metal.PrivateStorage}: 0.59582335 0.59582335 julia> b = Metal.zeros(1) -1-element MtlVector{Float32, Metal.MTL.PrivateStorage}: +1-element MtlVector{Float32, Metal.PrivateStorage}: 0.0 julia> Base.mapreducedim!(identity, +, b, a) -1×1 MtlMatrix{Float32, Metal.MTL.PrivateStorage}: +1×1 MtlMatrix{Float32, Metal.PrivateStorage}: 6.0 ``` diff --git a/lib/mtl/storage_type.jl b/lib/mtl/storage_type.jl index c1d20b2d0..1ff0f0b50 100644 --- a/lib/mtl/storage_type.jl +++ b/lib/mtl/storage_type.jl @@ -43,6 +43,11 @@ See also [`Metal.SharedStorage`](@ref) and [`Metal.ManagedStorage`](@ref). struct PrivateStorage <: StorageMode end struct Memoryless <: StorageMode end +# Remove the ".MTL" when printing +Base.show(io::IO, ::Type{<:PrivateStorage}) = print(io, "Metal.PrivateStorage") +Base.show(io::IO, ::Type{<:SharedStorage}) = print(io, "Metal.SharedStorage") +Base.show(io::IO, ::Type{<:ManagedStorage}) = print(io, "Metal.ManagedStorage") + """ CPUStorage diff --git a/src/array.jl b/src/array.jl index 8fbdbbe73..bee444cf1 100644 --- a/src/array.jl +++ b/src/array.jl @@ -480,18 +480,18 @@ Uses Adapt.jl to act inside some wrapper structs. ```jldoctests julia> mtl(ones(3)') -1×3 adjoint(::MtlVector{Float32, Metal.MTL.PrivateStorage}) with eltype Float32: +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.MTL.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.MTL.PrivateStorage}: +3-element MtlVector{Int64, Metal.PrivateStorage}: 1 2 3 From b68e36574b45c267ebf1c1c882420bd14fa69dac Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:23:48 -0300 Subject: [PATCH 15/19] Revert accidental change --- test/scripts.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/scripts.jl b/test/scripts.jl index 21d68709c..3f443face 100644 --- a/test/scripts.jl +++ b/test/scripts.jl @@ -4,11 +4,11 @@ using LLVM # for some reason, the environment shenanigans done by the scripts only work when # invoked from the Metal.jl CI, and not from GPUArrays.jl' reverse CI -# if get(ENV, "BUILDKITE_PIPELINE_NAME", "") != "Metal.jl" +if get(ENV, "BUILDKITE_PIPELINE_NAME", "") != "Metal.jl" -# @warn "Skipping script tests" +@warn "Skipping script tests" -# else +else mktempdir() do dir metallib_as = joinpath(dirname(@__DIR__), "bin", "metallib-as") @@ -50,4 +50,4 @@ end end -# end +end From 9e084de8a01b4c90a6d9900c6b07d8f65b1be8d3 Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:23:58 -0300 Subject: [PATCH 16/19] Update LocalPreference strings --- .buildkite/pipeline.yml | 4 ++-- LocalPreferences.toml | 4 ++-- src/array.jl | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.buildkite/pipeline.yml b/.buildkite/pipeline.yml index 254672495..eebfba141 100644 --- a/.buildkite/pipeline.yml +++ b/.buildkite/pipeline.yml @@ -54,8 +54,8 @@ steps: matrix: setup: storage: - - "SharedStorage" - - "ManagedStorage" + - "shared" + - "managed" commands: | echo -e "[Metal]\ndefault_storage = \"{{matrix.storage}}\"" >LocalPreferences.toml - label: "API validation" diff --git a/LocalPreferences.toml b/LocalPreferences.toml index 7f1c333c4..a2eaba4fc 100644 --- a/LocalPreferences.toml +++ b/LocalPreferences.toml @@ -1,4 +1,4 @@ [Metal] # which storage mode unspecified allocations should default to. -# possible values: "PrivateStorage", "SharedStorage", "ManagedStorage" -#default_storage = "PrivateStorage" +# possible values: "private", "shared", "managed" +#default_storage = "private" diff --git a/src/array.jl b/src/array.jl index bee444cf1..7edf8cf47 100644 --- a/src/array.jl +++ b/src/array.jl @@ -185,12 +185,12 @@ See also `VecOrMat`(@ref) for examples. const MtlVecOrMat{T,S} = Union{MtlVector{T,S},MtlMatrix{T,S}} # default to private memory -const DefaultStorageMode = let str = @load_preference("default_storage", "PrivateStorage") - if str == "PrivateStorage" +const DefaultStorageMode = let str = @load_preference("default_storage", "private") + if str == "private" PrivateStorage - elseif str == "SharedStorage" + elseif str == "shared" SharedStorage - elseif str == "ManagedStorage" + elseif str == "managed" ManagedStorage else error("unknown default storage mode: $default_storage") From eaa8215f415979de7ccefdeee1370bfc8dd8b497 Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Mon, 22 Jul 2024 12:11:05 -0300 Subject: [PATCH 17/19] Fix docstrings --- lib/mtl/storage_type.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mtl/storage_type.jl b/lib/mtl/storage_type.jl index 1ff0f0b50..850951573 100644 --- a/lib/mtl/storage_type.jl +++ b/lib/mtl/storage_type.jl @@ -10,7 +10,7 @@ export ReadUsage, WriteUsage, ReadWriteUsage abstract type StorageMode end """ - struct SharedStorage <: Metal.StorageMode + struct Metal.SharedStorage <: MTL.StorageMode Used to indicate that the resource is stored using `MTLStorageModeShared` in memory. @@ -21,7 +21,7 @@ See also [`Metal.PrivateStorage`](@ref) and [`Metal.ManagedStorage`](@ref). struct SharedStorage <: StorageMode end """ - struct ManagedStorage <: Metal.StorageMode + struct Metal.ManagedStorage <: MTL.StorageMode Used to indicate that the resource is stored using `MTLStorageModeManaged` in memory. @@ -32,7 +32,7 @@ See also [`Metal.SharedStorage`](@ref) and [`Metal.PrivateStorage`](@ref). struct ManagedStorage <: StorageMode end """ - struct PrivateStorage <: Metal.StorageMode + struct Metal.PrivateStorage <: MTL.StorageMode Used to indicate that the resource is stored using `MTLStorageModePrivate` in memory. From df4cf6842178d50e7f668d6aa0fb28a019c42b7b Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Mon, 22 Jul 2024 13:51:48 -0300 Subject: [PATCH 18/19] Remove unused dependency --- Project.toml | 2 -- src/Metal.jl | 1 - 2 files changed, 3 deletions(-) diff --git a/Project.toml b/Project.toml index a21ce9269..920857d78 100644 --- a/Project.toml +++ b/Project.toml @@ -21,7 +21,6 @@ Preferences = "21216c6a-2e73-6563-6e65-726566657250" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" Python_jll = "93d3a430-8e7c-50da-8e8d-3dfcfb3baf05" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" -Reexport = "189a3867-3050-52da-a836-e630ba90ab69" SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" @@ -50,7 +49,6 @@ ObjectFile = "0.4" ObjectiveC = "2.1, 3" PrecompileTools = "1" Preferences = "1" -Reexport = "1.0" SHA = "0.7" SpecialFunctions = "2" StaticArrays = "1" diff --git a/src/Metal.jl b/src/Metal.jl index 57a3600b8..08eba6039 100644 --- a/src/Metal.jl +++ b/src/Metal.jl @@ -1,6 +1,5 @@ module Metal -using Reexport using GPUArrays using Adapt using GPUCompiler From e9c59e204357bc46cac7d58b481db0c4bdd7782a Mon Sep 17 00:00:00 2001 From: Christian Guinard <28689358+christiangnrd@users.noreply.github.com> Date: Tue, 23 Jul 2024 10:00:31 -0300 Subject: [PATCH 19/19] Don't export `CPUStorage` from `MTL` --- docs/src/api/array.md | 1 - lib/mtl/storage_type.jl | 13 +++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/src/api/array.md b/docs/src/api/array.md index 408286b3a..7d238d677 100644 --- a/docs/src/api/array.md +++ b/docs/src/api/array.md @@ -21,7 +21,6 @@ The Metal API has various storage modes that dictate how a resource can be acces Metal.PrivateStorage Metal.SharedStorage Metal.ManagedStorage -Metal.CPUStorage ``` There also exist the following convenience functions to check if an MtlArray is using a specific storage mode: diff --git a/lib/mtl/storage_type.jl b/lib/mtl/storage_type.jl index 850951573..638600e59 100644 --- a/lib/mtl/storage_type.jl +++ b/lib/mtl/storage_type.jl @@ -1,4 +1,4 @@ -export SharedStorage, ManagedStorage, PrivateStorage, CPUStorage +export SharedStorage, ManagedStorage, PrivateStorage export ReadUsage, WriteUsage, ReadWriteUsage # Metal Has 4 storage types @@ -18,7 +18,7 @@ For more information on Metal storage modes, refer to the official Metal documen See also [`Metal.PrivateStorage`](@ref) and [`Metal.ManagedStorage`](@ref). """ -struct SharedStorage <: StorageMode end +struct SharedStorage <: StorageMode end """ struct Metal.ManagedStorage <: MTL.StorageMode @@ -29,7 +29,7 @@ For more information on Metal storage modes, refer to the official Metal documen See also [`Metal.SharedStorage`](@ref) and [`Metal.PrivateStorage`](@ref). """ -struct ManagedStorage <: StorageMode end +struct ManagedStorage <: StorageMode end """ struct Metal.PrivateStorage <: MTL.StorageMode @@ -40,8 +40,8 @@ For more information on Metal storage modes, refer to the official Metal documen See also [`Metal.SharedStorage`](@ref) and [`Metal.ManagedStorage`](@ref). """ -struct PrivateStorage <: StorageMode end -struct Memoryless <: StorageMode end +struct PrivateStorage <: StorageMode end +struct Memoryless <: StorageMode end # Remove the ".MTL" when printing Base.show(io::IO, ::Type{<:PrivateStorage}) = print(io, "Metal.PrivateStorage") @@ -49,13 +49,14 @@ Base.show(io::IO, ::Type{<:SharedStorage}) = print(io, "Metal.SharedStorage") Base.show(io::IO, ::Type{<:ManagedStorage}) = print(io, "Metal.ManagedStorage") """ - CPUStorage + MTL.CPUStorage Union type of [`Metal.SharedStorage`](@ref) and [`Metal.ManagedStorage`](@ref) storage modes. Represents storage modes where the resource is accessible via the CPU. """ const CPUStorage = Union{SharedStorage,ManagedStorage} + Base.convert(::Type{MTLStorageMode}, ::Type{SharedStorage}) = MTLStorageModeShared Base.convert(::Type{MTLStorageMode}, ::Type{ManagedStorage}) = MTLStorageModeManaged Base.convert(::Type{MTLStorageMode}, ::Type{PrivateStorage}) = MTLStorageModePrivate