Skip to content

Commit

Permalink
Use OncePerProcess when possible (JuliaGPU#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangnrd authored Dec 2, 2024
1 parent 8ccd416 commit 3fd6c06
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 33 deletions.
16 changes: 11 additions & 5 deletions lib/mtl/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ function MTLBuffer(dev::MTLDevice, bytesize::Integer, ptr::Ptr;
return MTLBuffer(ptr)
end

const _page_size::Ref{Int} = Ref{Int}(0)
function page_size()
if _page_size[] == 0
_page_size[] = Int(ccall(:getpagesize, Cint, ()))
@static if isdefined(Base, :OncePerProcess) # VERSION >= v"1.12.0-DEV.1421"
const page_size = OncePerProcess{Int}() do
Int(ccall(:getpagesize, Cint, ()))
end
else
const _page_size::Ref{Int} = Ref{Int}(0)
function page_size()
if _page_size[] == 0
_page_size[] = Int(ccall(:getpagesize, Cint, ()))
end
_page_size[]
end
_page_size[]
end

function can_alloc_nocopy(ptr::Ptr, bytesize::Integer)
Expand Down
34 changes: 24 additions & 10 deletions src/initialization.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
# Starts at `nothing`. Only false when it's determined
const _functional = Ref{Union{Nothing,Bool}}(false)
@static if isdefined(Base, :OncePerProcess) # VERSION >= v"1.12.0-DEV.1421"
const functional = OncePerProcess{Bool}() do
try
dev = device()
return supports_family(dev, MTL.MTLGPUFamilyApple7) &&
supports_family(dev, MTL.MTLGPUFamilyMetal3)
catch
return false
end
end
else
# Becomes `nothing` once it has been determined that the device is on macOS
const _functional = Ref{Union{Nothing,Bool}}(false)

function functional()
if isnothing(_functional[])
dev = device()
function functional()
if isnothing(_functional[])
dev = device()

_functional[] =
supports_family(dev, MTL.MTLGPUFamilyApple7) &&
supports_family(dev, MTL.MTLGPUFamilyMetal3)
_functional[] =
supports_family(dev, MTL.MTLGPUFamilyApple7) &&
supports_family(dev, MTL.MTLGPUFamilyMetal3)
end
_functional[]
end
_functional[]
end

function __init__()
Expand Down Expand Up @@ -51,7 +63,9 @@ function __init__()

# Successful loading of CoreGraphics means there's a
# chance the graphics device is supported
_functional[] = nothing
if @isdefined _functional
_functional[] = nothing # VERSION <= v"1.12.0-DEV.1421"
end
catch err
@error "Failed to load Metal" exception=(err,catch_backtrace())
return
Expand Down
47 changes: 29 additions & 18 deletions src/version.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,46 @@
parse(VersionNumber, verstr)
end

const _darwin_version = Ref{VersionNumber}()
"""
@static if isdefined(Base, :OncePerProcess) # VERSION >= v"1.12.0-DEV.1421"
const darwin_version = OncePerProcess{VersionNumber}() do
_syscall_version("kern.osrelease")
end
const macos_version = OncePerProcess{VersionNumber}() do
_syscall_version("kern.osproductversion")
end
else
const _darwin_version = Ref{VersionNumber}()
function darwin_version()
if !isassigned(_darwin_version)
_darwin_version[] = _syscall_version("kern.osrelease")
end
_darwin_version[]
end

const _macos_version = Ref{VersionNumber}()
function macos_version()
if !isassigned(_macos_version)
_macos_version[] = _syscall_version("kern.osproductversion")
end
_macos_version[]
end
end

@doc """
Metal.darwin_version() -> VersionNumber
Returns the host Darwin kernel version.
See also [`Metal.macos_version`](@ref).
"""
function darwin_version()
if !isassigned(_darwin_version)
_darwin_version[] = _syscall_version("kern.osrelease")
end
_darwin_version[]
end
""" darwin_version

const _macos_version = Ref{VersionNumber}()
"""
@doc """
Metal.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")
end
_macos_version[]
end
""" macos_version


## support queries
Expand Down

0 comments on commit 3fd6c06

Please sign in to comment.