gpt4 book ai didi

macos - 如何检测 macOS 上的外部 GPU (eGPU) 连接和断开连接?

转载 作者:行者123 更新时间:2023-12-05 00:47:56 24 4
gpt4 key购买 nike

我想编写一个 macOS 应用程序,它可以检测何时通过 Disconnect "GPU Name" Menu Extra 断开外部 GPU,然后采取一些措施。

  • 我使用什么 API 来检测 GPU 的存在?

  • 我能否在 GPU 断开连接并随后插入时收到通知?

最佳答案

来自 Apple 的 Metal docs :

Register for External GPU Notifications

Call the MTLCopyAllDevicesWithObserver function to get a list of all the Metal devices available to a system and register an observer that's called whenever this list changes (or may change due to a safe disconnect request).

id <NSObject> deviceObserver  = nil;
NSArray<id<MTLDevice>> *deviceList = nil;
deviceList = MTLCopyAllDevicesWithObserver(&deviceObserver,
^(id<MTLDevice> device, MTLDeviceNotificationName name) {
[self handleExternalGPUEventsForDevice:device notification:name];
});
_deviceObserver = deviceObserver;
_deviceList = deviceList;

To deregister the observer, call the MTLRemoveDeviceObserver function.

Respond to External GPU Notifications

Metal notifies your app about these external GPU events:

  • MTLDeviceWasAddedNotification. Metal posts this notification when an external GPU is added to the system. Evaluate the updated list of devices and consider using the new addition.

  • MTLDeviceRemovalRequestedNotification. Metal posts this notification when the user initiates a safe disconnect request for an external GPU. Your app has approximately one second to migrate work off the device and remove all references to it. If your app fails to do so, macOS notifies the user that your app is blocking the safe disconnect request.

  • MTLDeviceWasRemovedNotification. Metal posts this notification when an external GPU is removed from the system and your app still has references to that device. If the user safely disconnected an external GPU, Metal posts this notification after it posts a MTLDeviceRemovalRequestedNotification notification. If the user unexpectedly disconnected an external GPU, Metal posts this notification without first posting a MTLDeviceRemovalRequestedNotification notification. After an external GPU is removed, any command buffers queued for the device are completed with an error, and any new API calls that reference the device fail with an error.

Set up a method to respond to the notifications, and pass this method to the handler parameter of the MTLCopyAllDevicesWithObserver function.

- (void)handleExternalGPUEventsForDevice:(id<MTLDevice>)device notification:(MTLDeviceNotificationName)notification
{
if (notification == MTLDeviceWasAddedNotification) { }
else if (notification == MTLDeviceRemovalRequestedNotification) { }
else if (notification == MTLDeviceWasRemovedNotification) { }
}

关于macos - 如何检测 macOS 上的外部 GPU (eGPU) 连接和断开连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55913198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com