Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test all storage modes #314

Merged
merged 10 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ steps:
julia: "1.11"
soft_fail: true

# Test Storage modes Shared and Managed
- group: ":floppy_disk: Storage mode"
key: "storage"
steps:
- label: "MtlArray with {{matrix.storage}} storage"
plugins:
- JuliaCI/julia#v1:
version: "1.10"
- JuliaCI/julia-test#v1:
test_args: "--quickfail"
agents:
queue: "juliaecosystem"
os: "macos"
arch: "aarch64"
if: build.message !~ /\[skip tests\]/ && build.message !~ /\[skip storage\]/ && !build.pull_request.draft
timeout_in_minutes: 60
matrix:
setup:
storage:
- "Shared"
- "Managed"
commands: |
echo -e "[Metal]\ndefault_storage = \"{{matrix.storage}}\"" >LocalPreferences.toml


# special tests
- group: ":eyes: Special"
depends_on: "julia"
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Manifest.toml
LocalPreferences.toml
*.DS_Store
*.gputrace
*.trace
Expand Down
4 changes: 4 additions & 0 deletions LocalPreferences.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[Metal]
# which storage mode unspecified allocations should default to.
# possible values: "Private", "Shared", "Managed"
#default_storage = "Private"
13 changes: 7 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ LLVMDowngrader_jll = "f52de702-fb25-5922-94ba-81dd59b07444"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
ObjectFile = "d8793406-e978-5875-9003-1fc021f44a92"
ObjectiveC = "e86c9b32-1129-44ac-8ea0-90d5bb39ded9"
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"
Expand All @@ -26,6 +27,12 @@ SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[weakdeps]
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

[extensions]
SpecialFunctionsExt = "SpecialFunctions"

[compat]
Adapt = "4"
Artifacts = "1"
Expand All @@ -45,11 +52,5 @@ SHA = "0.7"
StaticArrays = "1"
julia = "1.8"

[extensions]
SpecialFunctionsExt = "SpecialFunctions"

[extras]
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

[weakdeps]
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
1 change: 1 addition & 0 deletions src/Metal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using GPUCompiler
using LLVM
using LLVM.Interop
import LLVMDowngrader_jll
using Preferences: @load_preference, load_preference
using Python_jll
using ObjectFile
using ExprTools: splitdef, combinedef
Expand Down
19 changes: 16 additions & 3 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,18 @@ const MtlMatrix{T,S} = MtlArray{T,2,S}
const MtlVecOrMat{T,S} = Union{MtlVector{T,S},MtlMatrix{T,S}}

# default to private memory
const DefaultStorageMode = Private
const DefaultStorageMode = let str = @load_preference("default_storage", "Private")
if str == "Private"
Private
elseif str == "Shared"
Shared
elseif str == "Managed"
Managed
else
error("unknown default storage mode: $default_storage")
end
end

MtlArray{T,N}(::UndefInitializer, dims::Dims{N}) where {T,N} =
MtlArray{T,N,DefaultStorageMode}(undef, dims)

Expand Down Expand Up @@ -170,14 +181,16 @@ end

function Base.unsafe_convert(::Type{MtlPointer{T}}, x::MtlArray) where {T}
buf = x.data[]
synchronize()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think we need to synchronize here. MtlPointers will only be used on device.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this the kernelabstractions copyto test fails.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before #305 it worked without a sync.

Base.pointer(x::MtlArray{T}) where {T} = Base.unsafe_convert(MtlPointer{T}, x)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the problem is, that pointer(A) might mean device or host pointer. #316 might fix that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this PR passes all tests, I suggest we merge this and then you can finish up the fix in #316.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, it's really not acceptable to sync on every pointer conversion, as that would remove almost all opportunity for asynchronous execution (as most GPU operations involve a conversion to a pointer).

MtlPointer{T}(buf, x.offset*Base.elsize(x))
end

function Base.unsafe_convert(::Type{Ptr{S}}, x::MtlArray{T}) where {S, T}
buf = x.data[]
if is_private(x)
throw(ArgumentError("cannot take the CPU address of a $(typeof(x))"))
end
synchronize()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be good to port the opportunistic synchronization from CUDA.jl

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can that be a separate PR? I don't have time at the moment to look into that and this PR in its current state seems to fix #312 and make CI more robust.

buf = x.data[]
convert(Ptr{T}, buf) + x.offset*Base.elsize(x)
end

Expand Down Expand Up @@ -237,7 +250,7 @@ Base.convert(::Type{T}, x::T) where T <: MtlArray = x
Base.unsafe_convert(::Type{<:Ptr}, x::MtlArray) =
throw(ArgumentError("cannot take the host address of a $(typeof(x))"))

Base.unsafe_convert(t::Type{MTL.MTLBuffer}, x::MtlArray) = x.data[]
Base.unsafe_convert(::Type{MTL.MTLBuffer}, x::MtlArray) = x.data[]


## interop with ObjC libraries
Expand Down
13 changes: 13 additions & 0 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ function versioninfo(io::IO=stdout)
println(io)
end

prefs = [
"default_storage" => load_preference(Metal, "default_storage"),
]
if any(x->!isnothing(x[2]), prefs)
println(io, "Preferences:")
for (key, val) in prefs
if !isnothing(val)
println(io, "- $key: $val")
end
end
println(io)
end

devs = devices()
if isempty(devs)
println(io, "No Metal devices.")
Expand Down
8 changes: 4 additions & 4 deletions test/array.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
STORAGEMODES = [Private, Shared]#, Managed]
STORAGEMODES = [Private, Shared, Managed]

@testset "array" begin

Expand Down Expand Up @@ -43,9 +43,9 @@ end
end

# test the regular adaptor
@test Adapt.adapt(MtlArray, [1 2;3 4]) isa MtlArray{Int, 2, Private}
@test Adapt.adapt(MtlArray{Float32}, [1 2;3 4]) isa MtlArray{Float32, 2, Private}
@test Adapt.adapt(MtlArray{Float32, 2}, [1 2;3 4]) isa MtlArray{Float32, 2, Private}
@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{Float16}, Float64[1]) isa MtlArray{Float16}
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ for (rootpath, dirs, files) in walkdir(@__DIR__)
end
## GPUArrays testsuite
for name in keys(TestSuite.tests)
if Metal.DefaultStorageMode != Private && name == "indexing scalar"
# GPUArrays' scalar indexing tests assume that indexing is not supported
continue
end
push!(tests, "gpuarrays$(Base.Filesystem.path_separator)$name")
test_runners["gpuarrays$(Base.Filesystem.path_separator)$name"] = ()->TestSuite.tests[name](MtlArray)
end
Expand Down