10# include <IOKit/IOKitLib.h>
14string MetalInfo::get_device_name(id<MTLDevice> device)
16 string device_name = [device.name UTF8String];
19 int gpu_core_count = get_apple_gpu_core_count(device);
20 device_name +=
string_printf(gpu_core_count ?
" (GPU - %d cores)" :
" (GPU)", gpu_core_count);
25int MetalInfo::get_apple_gpu_core_count(id<MTLDevice> device)
28 io_service_t gpu_service = IOServiceGetMatchingService(
29 kIOMainPortDefault, IORegistryEntryIDMatching(device.registryID));
30 if (CFNumberRef numberRef = (CFNumberRef)IORegistryEntryCreateCFProperty(
31 gpu_service, CFSTR(
"gpu-core-count"),
nullptr, 0))
33 if (CFGetTypeID(numberRef) == CFNumberGetTypeID()) {
34 CFNumberGetValue(numberRef, kCFNumberSInt32Type, &core_count);
41AppleGPUArchitecture MetalInfo::get_apple_gpu_architecture(id<MTLDevice> device)
43 const char *device_name = [device.name UTF8String];
44 if (strstr(device_name,
"M1")) {
47 if (strstr(device_name,
"M2")) {
48 return get_apple_gpu_core_count(device) <= 10 ? APPLE_M2 : APPLE_M2_BIG;
50 if (strstr(device_name,
"M3")) {
56int MetalInfo::optimal_sort_partition_elements()
58 if (
auto *
str = getenv(
"CYCLES_METAL_SORT_PARTITION_ELEMENTS")) {
72 static bool already_enumerated =
false;
74 if (already_enumerated) {
75 return usable_devices;
78 metal_printf(
"Usable Metal devices:");
79 for (id<MTLDevice> device
in MTLCopyAllDevices()) {
80 string device_name = get_device_name(device);
84 if (@available(macos 13.0, *)) {
85 const char *device_name_char = [device.name UTF8String];
86 if (!(strstr(device_name_char,
"Intel") || strstr(device_name_char,
"AMD")) &&
87 strstr(device_name_char,
"Apple"))
92 usable = [device hasUnifiedMemory];
97 metal_printf(
"- %s", device_name.c_str());
99 usable_devices.push_back(device);
102 metal_printf(
" (skipping \"%s\")", device_name.c_str());
105 if (usable_devices.empty()) {
106 metal_printf(
" No usable Metal devices found");
108 already_enumerated =
true;
110 return usable_devices;
#define CCL_NAMESPACE_END
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)