Register and respond to external GPU notifications initiated by a user.
A user can connect or disconnect external GPUs from a Mac at any time. The sequence of events that occurs when a user properly adds and removes an external GPU from the system is:
The user connects an external GPU to the Mac. The GPU is added to the system, and Metal makes it available to your app.
While one or more external GPUs are connected to the system, macOS presents an item in the menu bar that allows the user to safely disconnect any external GPUs.
The user selects an external GPU for a safe disconnect.
Metal notifies your app of the request, and you migrate all current and future work off of the external GPU.
After all apps (including yours) have stopped using the external GPU, macOS notifies the user that it's safe to disconnect the GPU.
The external GPU is removed from the system, and the user can disconnect the GPU from the Mac.
To support external GPUs effectively, you must ensure that your app responds to these events appropriately.
Set a GPU Eject Policy
You can modify your app's
Info file to control how your app responds to a safe disconnect request for an external GPU. This response is determined by the
GPUEject key, which takes the following values:
wait. Set this value to manually respond to the safe disconnect request. Your app must register and respond to the
removalnotification posted by Metal. macOS waits for your app to remove all references to the external GPU before notifying the user that it's safe to disconnect the GPU.
relaunch. Set this value to allow macOS to quit and relaunch your app with another GPU. Your app can implement the
application(_:method to save any state before it quits, and it can implement the
will Encode Restorable State:)
application(_:method to restore any saved state after it relaunches.
did Decode Restorable State:)
kill. Set this value to allow macOS to force your app to quit.
To support external GPUs effectively, set a
wait value and respond to a safe disconnect request appropriately.
Register for External GPU Notifications
MTLCopy 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).
To deregister the observer, call the
Respond to External GPU Notifications
Metal notifies your app about these external GPU events:
was. 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.
removal. 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.
was. 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
removalnotification. If the user unexpectedly disconnected an external GPU, Metal posts this notification without first posting a
removalnotification. 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