Skip to content

Commit f66b441

Browse files
committed
Standardize error handling and output across all examples (#1678)
- Remove success/completion fluff messages ("done!", "passed", etc.) - Use sys.exit(1) instead of sys.exit(0) for unsupported configurations - Send skip/warning/error messages to stderr - Replace user-facing assert with proper checks - Standardize sys.exit(-1) to sys.exit(1) Made-with: Cursor
1 parent 7a36d70 commit f66b441

23 files changed

+132
-186
lines changed

cuda_bindings/examples/0_Introduction/clock_nvrtc_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ def elems_to_bytes(nelems, dt):
5858

5959

6060
def main():
61-
print("CUDA Clock sample")
61+
import pytest
6262

6363
if platform.machine() == "armv7l":
64-
print("clock_nvrtc is not supported on ARMv7 - waiving sample")
65-
return
64+
pytest.skip("clock_nvrtc is not supported on ARMv7")
6665

6766
timer = np.empty(NUM_BLOCKS * 2, dtype="int64")
6867
hinput = np.empty(NUM_THREADS * 2, dtype="float32")

cuda_bindings/examples/0_Introduction/simpleCubemapTexture_test.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ def main():
9090
f"CUDA device [{deviceProps.name}] has {deviceProps.multiProcessorCount} Multi-Processors SM {deviceProps.major}.{deviceProps.minor}"
9191
)
9292
if deviceProps.major < 2:
93-
print("Test requires SM 2.0 or higher for support of Texture Arrays. Test will exit...")
94-
sys.exit()
93+
import pytest
94+
95+
pytest.skip("Test requires SM 2.0 or higher for support of Texture Arrays.")
9596

9697
# Generate input data for layered texture
9798
width = 64
@@ -208,12 +209,10 @@ def main():
208209
checkCudaErrors(cudart.cudaFree(d_data))
209210
checkCudaErrors(cudart.cudaFreeArray(cu_3darray))
210211

211-
print("Comparing kernel output to expected data")
212212
MIN_EPSILON_ERROR = 5.0e-3
213213
if np.max(np.abs(h_odata - h_data_ref)) > MIN_EPSILON_ERROR:
214-
print("Failed")
215-
sys.exit(-1)
216-
print("Passed")
214+
print("Failed", file=sys.stderr)
215+
sys.exit(1)
217216

218217

219218
if __name__ == "__main__":

cuda_bindings/examples/0_Introduction/simpleP2P_test.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,27 @@
2424

2525

2626
def main():
27-
print("Starting...")
27+
import pytest
2828

2929
if platform.system() == "Darwin":
30-
print("simpleP2P is not supported on Mac OSX - waiving sample")
31-
return
30+
pytest.skip("simpleP2P is not supported on Mac OSX")
3231

3332
if platform.machine() == "armv7l":
34-
print("simpleP2P is not supported on ARMv7 - waiving sample")
35-
return
33+
pytest.skip("simpleP2P is not supported on ARMv7")
3634

3735
if platform.machine() == "aarch64":
38-
print("simpleP2P is not supported on aarch64 - waiving sample")
39-
return
36+
pytest.skip("simpleP2P is not supported on aarch64")
4037

4138
if platform.machine() == "sbsa":
42-
print("simpleP2P is not supported on sbsa - waiving sample")
43-
return
39+
pytest.skip("simpleP2P is not supported on sbsa")
4440

4541
# Number of GPUs
4642
print("Checking for multiple GPUs...")
4743
gpu_n = checkCudaErrors(cudart.cudaGetDeviceCount())
4844
print(f"CUDA-capable device count: {gpu_n}")
4945

5046
if gpu_n < 2:
51-
print("Two or more GPUs with Peer-to-Peer access capability are required")
52-
return
47+
pytest.skip("Two or more GPUs with Peer-to-Peer access capability are required")
5348

5449
prop = [checkCudaErrors(cudart.cudaGetDeviceProperties(i)) for i in range(gpu_n)]
5550
# Check possibility for peer access
@@ -80,9 +75,7 @@ def main():
8075
break
8176

8277
if p2pCapableGPUs[0] == -1 or p2pCapableGPUs[1] == -1:
83-
print("Two or more GPUs with Peer-to-Peer access capability are required.")
84-
print("Peer to Peer access is not available amongst GPUs in the system, waiving test.")
85-
return
78+
pytest.skip("Peer to Peer access is not available amongst GPUs in the system")
8679

8780
# Use first pair of p2p capable GPUs detected
8881
gpuid = [p2pCapableGPUs[0], p2pCapableGPUs[1]]
@@ -239,9 +232,8 @@ def main():
239232
checkCudaErrors(cudart.cudaSetDevice(i))
240233

241234
if error_count != 0:
242-
print("Test failed!")
243-
sys.exit(-1)
244-
print("Test passed!")
235+
print("Test failed!", file=sys.stderr)
236+
sys.exit(1)
245237

246238

247239
if __name__ == "__main__":

cuda_bindings/examples/0_Introduction/simpleZeroCopy_test.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,26 @@ def main():
3232
idev = 0
3333
bPinGenericMemory = False
3434

35+
import pytest
36+
3537
if platform.system() == "Darwin":
36-
print("simpleZeroCopy is not supported on Mac OSX - waiving sample")
37-
return
38+
pytest.skip("simpleZeroCopy is not supported on Mac OSX")
3839

3940
if platform.machine() == "armv7l":
40-
print("simpleZeroCopy is not supported on ARMv7 - waiving sample")
41-
return
41+
pytest.skip("simpleZeroCopy is not supported on ARMv7")
4242

4343
if platform.machine() == "aarch64":
44-
print("simpleZeroCopy is not supported on aarch64 - waiving sample")
45-
return
44+
pytest.skip("simpleZeroCopy is not supported on aarch64")
4645

4746
if platform.machine() == "sbsa":
48-
print("simpleZeroCopy is not supported on sbsa - waiving sample")
49-
return
47+
pytest.skip("simpleZeroCopy is not supported on sbsa")
5048

5149
if checkCmdLineFlag("help"):
52-
print("Usage: simpleZeroCopy [OPTION]\n")
53-
print("Options:")
54-
print(" device=[device #] Specify the device to be used")
55-
print(" use_generic_memory (optional) use generic page-aligned for system memory")
56-
return
50+
print("Usage: simpleZeroCopy [OPTION]\n", file=sys.stderr)
51+
print("Options:", file=sys.stderr)
52+
print(" device=[device #] Specify the device to be used", file=sys.stderr)
53+
print(" use_generic_memory (optional) use generic page-aligned for system memory", file=sys.stderr)
54+
sys.exit(1)
5755

5856
# Get the device selected by the user or default to 0, and then set it.
5957
if checkCmdLineFlag("device="):
@@ -78,8 +76,7 @@ def main():
7876
deviceProp = checkCudaErrors(cudart.cudaGetDeviceProperties(idev))
7977

8078
if not deviceProp.canMapHostMemory:
81-
print(f"Device {idev} does not support mapping CPU host memory!")
82-
return
79+
pytest.skip(f"Device {idev} does not support mapping CPU host memory!")
8380

8481
checkCudaErrors(cudart.cudaSetDeviceFlags(cudart.cudaDeviceMapHost))
8582

@@ -177,9 +174,8 @@ def main():
177174
checkCudaErrors(cudart.cudaFreeHost(c))
178175

179176
if errorNorm / refNorm >= 1.0e-7:
180-
print("FAILED")
181-
sys.exit(-1)
182-
print("PASSED")
177+
print("FAILED", file=sys.stderr)
178+
sys.exit(1)
183179

184180

185181
if __name__ == "__main__":

cuda_bindings/examples/0_Introduction/systemWideAtomics_test.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,28 +165,24 @@ def verify(testData, length):
165165

166166

167167
def main():
168+
import pytest
169+
168170
if os.name == "nt":
169-
print("Atomics not supported on Windows")
170-
return
171+
pytest.skip("Atomics not supported on Windows")
171172

172173
# set device
173174
dev_id = findCudaDevice()
174175
device_prop = checkCudaErrors(cudart.cudaGetDeviceProperties(dev_id))
175176

176177
if not device_prop.managedMemory:
177-
# This samples requires being run on a device that supports Unified Memory
178-
print("Unified Memory not supported on this device")
179-
return
178+
pytest.skip("Unified Memory not supported on this device")
180179

181180
computeMode = checkCudaErrors(cudart.cudaDeviceGetAttribute(cudart.cudaDeviceAttr.cudaDevAttrComputeMode, dev_id))
182181
if computeMode == cudart.cudaComputeMode.cudaComputeModeProhibited:
183-
# This sample requires being run with a default or process exclusive mode
184-
print("This sample requires a device in either default or process exclusive mode")
185-
return
182+
pytest.skip("This sample requires a device in either default or process exclusive mode")
186183

187184
if device_prop.major < 6:
188-
print("Requires a minimum CUDA compute 6.0 capability, waiving testing.")
189-
return
185+
pytest.skip("Requires a minimum CUDA compute 6.0 capability")
190186

191187
numThreads = 256
192188
numBlocks = 64
@@ -240,9 +236,9 @@ def main():
240236
else:
241237
checkCudaErrors(cudart.cudaFree(atom_arr))
242238

243-
print("systemWideAtomics completed, returned {}".format("OK" if testResult else "ERROR!"))
244239
if not testResult:
245-
sys.exit(-1)
240+
print("systemWideAtomics completed with errors", file=sys.stderr)
241+
sys.exit(1)
246242

247243

248244
if __name__ == "__main__":

cuda_bindings/examples/0_Introduction/vectorAddDrv_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232

3333
def main():
34-
print("Vector Addition (Driver API)")
3534
N = 50000
3635
nbytes = N * np.dtype(np.float32).itemsize
3736

@@ -45,8 +44,9 @@ def main():
4544
cuda.cuDeviceGetAttribute(cuda.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING, cuDevice)
4645
)
4746
if not uvaSupported:
48-
print("Accessing pageable memory directly requires UVA")
49-
return
47+
import pytest
48+
49+
pytest.skip("Accessing pageable memory directly requires UVA")
5050

5151
kernelHelper = common.KernelHelper(vectorAddDrv, int(cuDevice))
5252
_VecAdd_kernel = kernelHelper.getFunction(b"VecAdd_kernel")
@@ -106,9 +106,9 @@ def main():
106106
checkCudaErrors(cuda.cuMemFree(d_C))
107107

108108
checkCudaErrors(cuda.cuCtxDestroy(cuContext))
109-
print("{}".format("Result = PASS" if i + 1 == N else "Result = FAIL"))
110109
if i + 1 != N:
111-
sys.exit(-1)
110+
print("Result = FAIL", file=sys.stderr)
111+
sys.exit(1)
112112

113113

114114
if __name__ == "__main__":

cuda_bindings/examples/0_Introduction/vectorAddMMAP_test.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,23 +189,19 @@ def simpleFreeMultiDeviceMmap(dptr, size):
189189

190190

191191
def main():
192-
print("Vector Addition (Driver API)")
192+
import pytest
193193

194194
if platform.system() == "Darwin":
195-
print("vectorAddMMAP is not supported on Mac OSX - waiving sample")
196-
return
195+
pytest.skip("vectorAddMMAP is not supported on Mac OSX")
197196

198197
if platform.machine() == "armv7l":
199-
print("vectorAddMMAP is not supported on ARMv7 - waiving sample")
200-
return
198+
pytest.skip("vectorAddMMAP is not supported on ARMv7")
201199

202200
if platform.machine() == "aarch64":
203-
print("vectorAddMMAP is not supported on aarch64 - waiving sample")
204-
return
201+
pytest.skip("vectorAddMMAP is not supported on aarch64")
205202

206203
if platform.machine() == "sbsa":
207-
print("vectorAddMMAP is not supported on sbsa - waiving sample")
208-
return
204+
pytest.skip("vectorAddMMAP is not supported on sbsa")
209205

210206
N = 50000
211207
size = N * np.dtype(np.float32).itemsize
@@ -224,8 +220,7 @@ def main():
224220
)
225221
print(f"Device {cuDevice} VIRTUAL ADDRESS MANAGEMENT SUPPORTED = {attributeVal}.")
226222
if not attributeVal:
227-
print(f"Device {cuDevice} doesn't support VIRTUAL ADDRESS MANAGEMENT.")
228-
return
223+
pytest.skip(f"Device {cuDevice} doesn't support VIRTUAL ADDRESS MANAGEMENT.")
229224

230225
# The vector addition happens on cuDevice, so the allocations need to be mapped there.
231226
mappingDevices = [cuDevice]
@@ -298,9 +293,9 @@ def main():
298293

299294
checkCudaErrors(cuda.cuCtxDestroy(cuContext))
300295

301-
print("{}".format("Result = PASS" if i + 1 == N else "Result = FAIL"))
302296
if i + 1 != N:
303-
sys.exit(-1)
297+
print("Result = FAIL", file=sys.stderr)
298+
sys.exit(1)
304299

305300

306301
if __name__ == "__main__":

cuda_bindings/examples/2_Concepts_and_Techniques/streamOrderedAllocation_test.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ def basicStreamOrderedAllocation(dev, nelem, a, b, c):
9292
errorNorm = math.sqrt(errorNorm)
9393
refNorm = math.sqrt(refNorm)
9494

95-
if errorNorm / refNorm < 1.0e-6:
96-
print("basicStreamOrderedAllocation PASSED")
97-
9895
checkCudaErrors(cudart.cudaStreamDestroy(stream))
9996

10097
return errorNorm / refNorm < 1.0e-6
@@ -188,25 +185,23 @@ def streamOrderedAllocationPostSync(dev, nelem, a, b, c):
188185
errorNorm = math.sqrt(errorNorm)
189186
refNorm = math.sqrt(refNorm)
190187

191-
if errorNorm / refNorm < 1.0e-6:
192-
print("streamOrderedAllocationPostSync PASSED")
193-
194188
checkCudaErrors(cudart.cudaStreamDestroy(stream))
195189

196190
return errorNorm / refNorm < 1.0e-6
197191

198192

199193
def main():
194+
import pytest
195+
200196
if platform.system() == "Darwin":
201-
print("streamOrderedAllocation is not supported on Mac OSX - waiving sample")
202-
return
197+
pytest.skip("streamOrderedAllocation is not supported on Mac OSX")
203198

204199
cuda.cuInit(0)
205200
if checkCmdLineFlag("help"):
206-
print("Usage: streamOrderedAllocation [OPTION]\n")
207-
print("Options:")
208-
print(" device=[device #] Specify the device to be used")
209-
return
201+
print("Usage: streamOrderedAllocation [OPTION]\n", file=sys.stderr)
202+
print("Options:", file=sys.stderr)
203+
print(" device=[device #] Specify the device to be used", file=sys.stderr)
204+
sys.exit(1)
210205

211206
dev = findCudaDevice()
212207

@@ -218,8 +213,7 @@ def main():
218213
cudart.cudaDeviceGetAttribute(cuda.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_MEMORY_POOLS_SUPPORTED, dev)
219214
)
220215
if not isMemPoolSupported:
221-
print("Waiving execution as device does not support Memory Pools")
222-
return
216+
pytest.skip("Waiving execution as device does not support Memory Pools")
223217

224218
global _vectorAddGPU
225219
kernelHelper = common.KernelHelper(streamOrderedAllocation, dev)
@@ -241,7 +235,7 @@ def main():
241235
ret2 = streamOrderedAllocationPostSync(dev, nelem, a, b, c)
242236

243237
if not ret1 or not ret2:
244-
sys.exit(-1)
238+
sys.exit(1)
245239

246240

247241
if __name__ == "__main__":

0 commit comments

Comments
 (0)