Skip to content

Commit 4f4bdef

Browse files
Merge pull request #652 from christiangnrd/numcoresfix
2 parents 47aefc3 + feaff69 commit 4f4bdef

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/utilities.jl

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,45 @@ function versioninfo(io::IO=stdout)
6868
return
6969
end
7070

71+
const _iokitlib = Symbol("/System/Library/Frameworks/IOKit.framework/Resources/BridgeSupport/IOKit.dylib")
72+
const _cflib = Symbol("/System/Library/Frameworks/CoreFoundation.framework/Resources/BridgeSupport/CoreFoundation.dylib")
73+
7174
@static if isdefined(Base, :OncePerProcess) # VERSION >= v"1.12.0-DEV.1421"
7275
const num_gpu_cores = OncePerProcess{Int64}() do
73-
_num_cpu_cores = 0
76+
_num_gpu_cores = Ref{Int64}(0)
7477
try
75-
system_prof = read(`system_profiler SPDisplaysDataType`, String)
76-
_num_gpu_cores = parse(Int64, only(match(r"Total Number of Cores:\s*(\d+)", system_prof).captures))
78+
serv_match = @ccall _iokitlib.IOServiceMatching("AGXAccelerator"::Cstring)::Ptr{Cvoid}
79+
serv = @ccall _iokitlib.IOServiceGetMatchingService(C_NULL::Ptr{Nothing}, serv_match::Ptr{Cvoid})::UInt
80+
81+
gpuCoreCountNumber = @ccall _iokitlib.IORegistryEntrySearchCFProperty(serv::UInt, "IOService"::Ptr{Cchar}, "gpu-core-count"::id{NSString}, C_NULL::Ptr{Nothing}, UInt32(0)::UInt32)::id{Object}
82+
gpuCoreCountNumber != nil || error()
83+
84+
type = @ccall _cflib.CFNumberGetType(gpuCoreCountNumber::id{Object})::Int64
85+
type == 4 || error()
86+
87+
success = @ccall _cflib.CFNumberGetValue(gpuCoreCountNumber::id{Object}, type::Int64, _num_gpu_cores::Ptr{Int64})::Bool
88+
success || error()
7789
catch
7890
@warn "Could not determine number of GPU cores; some algorithms may not run optimally."
7991
end
80-
_num_cpu_cores
92+
_num_gpu_cores[]
8193
end
8294
else
8395
const _num_gpu_cores = Ref{Int64}(-1)
8496
function num_gpu_cores()
8597
if _num_gpu_cores[] == -1
8698
try
87-
system_prof = read(`system_profiler SPDisplaysDataType`, String)
88-
_num_gpu_cores[] = parse(Int64, only(match(r"Total Number of Cores:\s*(\d+)", system_prof).captures))
99+
serv_match = @ccall _iokitlib.IOServiceMatching("AGXAccelerator"::Cstring)::Ptr{Cvoid}
100+
serv = @ccall _iokitlib.IOServiceGetMatchingService(C_NULL::Ptr{Nothing}, serv_match::Ptr{Cvoid})::UInt
101+
102+
gpuCoreCountNumber = @ccall _iokitlib.IORegistryEntrySearchCFProperty(serv::UInt, "IOService"::Ptr{Cchar}, "gpu-core-count"::id{NSString}, C_NULL::Ptr{Nothing}, UInt32(0)::UInt32)::id{Object}
103+
gpuCoreCountNumber != nil || error()
104+
105+
type = @ccall _cflib.CFNumberGetType(gpuCoreCountNumber::id{Object})::Int64
106+
type == 4 || error()
107+
108+
success = @ccall _cflib.CFNumberGetValue(gpuCoreCountNumber::id{Object}, type::Int64, _num_gpu_cores::Ptr{Int64})::Bool
109+
success || error()
89110
catch
90111
@warn "Could not determine number of GPU cores; some algorithms may not run optimally."
91112
_num_gpu_cores[] = 0

test/version.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
@test Metal.air_support() isa VersionNumber
99
@test Metal.metal_support() isa VersionNumber
1010

11-
@test Metal.num_gpu_cores() isa Int64
11+
@test Metal.num_gpu_cores() > 0
1212

1313
end # testset "version"

0 commit comments

Comments
 (0)