Use high-speed connections between GPUs to transfer data quickly.
Starting with macOS 10.15, some Mac systems directly connect GPUs to each other, allowing you to quickly transfer data between them. These connections are not only faster, but they also avoid using the memory bus between the CPU and GPUs, leaving it available for other tasks. If your app uses multiple GPUs, test to see if they're connected, and when they are, use the transfer mechanism described here.
When GPUs are connected to each other, they're said to be in the same peer group. You determine whether a GPU is in a peer group by reading the device object’s
peer property. A nonzero value indicates that the GPU is in a peer group.
GPUs in the same peer group share the same peer group ID.
You can get the list of all devices associated with a peer group by filtering on this ID.
Copy Resources to the GPU that Needs to Access Them
In Metal, resources are created by device objects, and are always associated with the device object that created them. Peer groups don't change that association. If a resource is associated with a device object, and you want to access it on another device object, you need to copy the data to a resource associated with the second device object.
To copy data between members of a peer group, make a remote view on the second GPU that’s connected to the resource you want to copy. A remote view is a resource object that contains no storage of its own; it references the storage on the original GPU. You can only use remote views to copy data; using them in other Metal commands results in an error.
Create a Remote View of a Resource
To create a remote view of a resource, the device object you make the resource view on must share the same
peer as the device object that created the resource. In addition, the resource must use the
MTLStorage storage mode or be backed by an
To create a buffer view, call the
Similarly, to create a texture view, call the
Copy Data Between Connected GPUs
MTLBlit and encode a copy command. The source for this copy command is the remote view object:
As shown in the following illustration, there are three resource objects: the original resource that contains the data, a remote view that references the data, and a resource that receives the data.
Synchronize Access to Resources
Blit commands used in peer-to-peer transfers follow all of the usual synchronization rules on the GPU they're performed on. However, they don't automatically synchronize with any commands running on the source GPU. If you encode commands that modify the source resource, ensure that those commands are complete before executing the blit command to transfer the data to the other GPU. This is the same as what you do when transferring resources between GPUs through system memory.