When writing a CUDA kernel to access memory on NVLink-connected GPUs, you need to take into account the Unified Memory (UM) and Peer-to-Peer (P2P) memory access features provided by CUDA. Starting from CUDA 6.0 and onward, Unified Memory simplifies memory management by allowing automatic data migration to the GPU that needs it. In multi-GPU systems connected by NVLink, P2P enables direct memory access between GPUs.
Here's how you might typically access memory on NVLink-connected GPUs:
Enable Peer Access:
To allow one GPU to directly access memory of another GPU, you need to enable peer-to-peer access using cudaDeviceEnablePeerAccess(). This is not strictly necessary for Unified Memory, but it can be useful for explicit device memory allocations.
Allocate Memory:
Use cudaMallocManaged() to allocate Unified Memory that is accessible from any GPU or the CPU. This memory is automatically migrated by the CUDA driver to the GPU that requires it for computation.
Write the Kernel:
Write your CUDA kernel as usual. With Unified Memory, there is no need to explicitly transfer data between GPUs. The CUDA runtime takes care of migrating data to the GPU where it's needed when the kernel is running.
Launch the Kernel:
Launch your kernel on a specific GPU using cudaSetDevice() to set the active device, and then call your kernel function. If you're using streams, you can also specify the stream associated with a specific GPU.
Synchronize:
Use cudaDeviceSynchronize() after kernel execution to ensure that all operations have completed and that the data is consistent across devices.
Here's an example of how this might look in code:
c
#include <cuda_runtime.h>
// A simple CUDA kernel
__global__ void myKernel(int *data, int N) {
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx < N) {
data[idx] += 1; // Some computation
}
}
int main() {
const int N = 1024;
int *data;
// Allocate Unified Memory – accessible from CPU or GPU
cudaMallocManaged(&data, N * sizeof(int));
// Initialize data on the CPU
for (int i = 0; i < N; ++i) {
data[i] = 0;
}
// Enable peer access between GPUs
int numDevices;
cudaGetDeviceCount(&numDevices);
for (int i = 0; i < numDevices; ++i) {
cudaSetDevice(i);
for (int j = 0; j < numDevices; ++j) {
if (i != j) {
int canAccessPeer;
cudaDeviceCanAccessPeer(&canAccessPeer, i, j);
if (canAccessPeer) {
cudaDeviceEnablePeerAccess(j, 0);
}
}
}
}
// Run kernel on GPU 0
cudaSetDevice(0);
myKernel<<<(N + 255) / 256, 256>>>(data, N);
// Wait for GPU to finish before accessing on host
cudaDeviceSynchronize();
// Free memory
cudaFree(data);
return 0;
}
Keep in mind that not all GPUs may support P2P access, and even if they do, it might be limited to certain pairs of devices depending on the topology of the NVLink connections. Always check for support using cudaDeviceCanAccessPeer() before enabling peer access.
In more complex scenarios where you need to control data placement manually, you might use cudaMemcpyPeer() or cudaMemcpyPeerAsync() to transfer data between GPUs explicitly.
Moreover, when using NVLink, it's also important to consider the topology to optimize memory access patterns and kernel execution. NVLink provides a high-bandwidth, low-latency path between GPUs, but the best performance is achieved when data locality is carefully managed.