MCPcopy Create free account
hub / github.com/NVIDIA/cuda-samples / run

Function run

python/4_DistributedComputing/simpleP2P/simpleP2P.py:68–353  ·  view source on GitHub ↗

Demonstrates peer-to-peer (P2P) memory access between multiple GPUs using cuda.core. This function shows how to: 1. Detect and verify multiple GPUs with P2P capability 2. Enable peer access between GPUs 3. Perform direct GPU-to-GPU memory transfers 4. Launch kernels that ac

(num_elements=1024 * 1024 * 16)

Source from the content-addressed store, hash-verified

66
67
68def run(num_elements=1024 * 1024 * 16):
69 """
70 Demonstrates peer-to-peer (P2P) memory access between multiple GPUs using cuda.core.
71
72 This function shows how to:
73 1. Detect and verify multiple GPUs with P2P capability
74 2. Enable peer access between GPUs
75 3. Perform direct GPU-to-GPU memory transfers
76 4. Launch kernels that access memory from other GPUs
77 5. Measure P2P bandwidth
78 6. Validate results
79
80 Parameters
81 ----------
82 num_elements : int
83 Number of elements in arrays (default: 16M elements = 64MB)
84 """
85
86 print("\n" + "=" * 70)
87 print("simpleP2P - CUDA Python Sample")
88 print("=" * 70)
89 print("\nStarting...")
90
91 # Check for multiple GPUs
92 print("\nChecking for multiple GPUs...")
93 devices = Device.get_all_devices()
94 num_devices = len(devices)
95 print(f"CUDA-capable device count: {num_devices}")
96
97 if num_devices < 2:
98 print(
99 "Two or more GPUs with Peer-to-Peer access capability are "
100 "required, waiving this sample."
101 )
102 return 2
103
104 # Check for P2P capability
105 print("\nChecking GPU(s) for support of peer to peer memory access...")
106
107 p2p_capable_gpus = [-1, -1]
108
109 for i in range(num_devices):
110 p2p_capable_gpus[0] = i
111 for j in range(num_devices):
112 if i == j:
113 continue
114
115 # Check peer access capability using cuda.core
116 i_access_j = devices[i].can_access_peer(devices[j])
117 j_access_i = devices[j].can_access_peer(devices[i])
118
119 print(
120 f"> Peer access from {devices[i].name} (GPU{i}) -> "
121 f"{devices[j].name} (GPU{j}): {'Yes' if i_access_j else 'No'}"
122 )
123 print(
124 f"> Peer access from {devices[j].name} (GPU{j}) -> "
125 f"{devices[i].name} (GPU{i}): {'Yes' if j_access_i else 'No'}"

Callers 1

mainFunction · 0.70

Calls 1

verify_array_resultFunction · 0.90

Tested by

no test coverage detected