Skip to content

Commit

Permalink
Fix loading on unsupported platforms (#459)
Browse files Browse the repository at this point in the history
* Gate getpagesize call behind function

* Make `functional` require a fully-supported GPU
  • Loading branch information
christiangnrd authored Oct 16, 2024
1 parent ddca5c4 commit 100f831
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
13 changes: 10 additions & 3 deletions lib/mtl/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,23 @@ function MTLBuffer(dev::MTLDevice, bytesize::Integer, ptr::Ptr;
return MTLBuffer(ptr)
end

const PAGESIZE = ccall(:getpagesize, Cint, ())
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

function can_alloc_nocopy(ptr::Ptr, bytesize::Integer)
# newBufferWithBytesNoCopy has several restrictions:
## the pointer has to be page-aligned
if Int64(ptr) % PAGESIZE != 0
if Int(ptr) % page_size() != 0
return false
end
## the new buffer needs to be page-aligned
## XXX: on macOS 14, this doesn't seem required; is this a documentation issue?
if bytesize % PAGESIZE != 0
if bytesize % page_size() != 0
return false
end
return true
Expand Down
20 changes: 17 additions & 3 deletions src/initialization.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const _functional = Ref{Bool}(false)
functional() = _functional[]
# Starts at `nothing`. Only false when it's determined
const _functional = Ref{Union{Nothing,Bool}}(false)

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

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

function __init__()
precompiling = ccall(:jl_generating_output, Cint, ()) != 0
Expand Down Expand Up @@ -39,7 +50,10 @@ function __init__()
load_framework("CoreGraphics")
ver = MTL.MTLCompileOptions().languageVersion
@debug "Successfully loaded Metal; targeting v$ver."
_functional[] = true

# Successful loading of CoreGraphics means there's a
# chance the graphics device is supported
_functional[] = nothing
catch err
@error "Failed to load Metal" exception=(err,catch_backtrace())
return
Expand Down

0 comments on commit 100f831

Please sign in to comment.