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

Fix loading on unsupported platforms #459

Merged
merged 2 commits into from
Oct 16, 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
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)
christiangnrd marked this conversation as resolved.
Show resolved Hide resolved
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