Skip to content

Commit 7a4c5a9

Browse files
committed
Standardize error handling and output across all examples (NVIDIA#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 e923636 commit 7a4c5a9

23 files changed

Lines changed: 145 additions & 188 deletions

cuda_bindings/examples/0_Introduction/clock_nvrtc_test.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
33

44
import platform
5+
import sys
56

67
import numpy as np
78
from common import common
@@ -58,11 +59,9 @@ def elems_to_bytes(nelems, dt):
5859

5960

6061
def main():
61-
print("CUDA Clock sample")
62-
6362
if platform.machine() == "armv7l":
64-
print("clock_nvrtc is not supported on ARMv7 - waiving sample")
65-
return
63+
print("clock_nvrtc is not supported on ARMv7 - waiving sample", file=sys.stderr)
64+
sys.exit(1)
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: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ 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+
print("Test requires SM 2.0 or higher for support of Texture Arrays.", file=sys.stderr)
94+
sys.exit(1)
9595

9696
# Generate input data for layered texture
9797
width = 64
@@ -208,12 +208,10 @@ def main():
208208
checkCudaErrors(cudart.cudaFree(d_data))
209209
checkCudaErrors(cudart.cudaFreeArray(cu_3darray))
210210

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

218216

219217
if __name__ == "__main__":

cuda_bindings/examples/0_Introduction/simpleP2P_test.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,30 @@
2424

2525

2626
def main():
27-
print("Starting...")
28-
2927
if platform.system() == "Darwin":
30-
print("simpleP2P is not supported on Mac OSX - waiving sample")
31-
return
28+
print("simpleP2P is not supported on Mac OSX", file=sys.stderr)
29+
sys.exit(1)
3230

3331
if platform.machine() == "armv7l":
34-
print("simpleP2P is not supported on ARMv7 - waiving sample")
35-
return
32+
print("simpleP2P is not supported on ARMv7", file=sys.stderr)
33+
sys.exit(1)
3634

3735
if platform.machine() == "aarch64":
38-
print("simpleP2P is not supported on aarch64 - waiving sample")
39-
return
36+
print("simpleP2P is not supported on aarch64", file=sys.stderr)
37+
sys.exit(1)
4038

4139
if platform.machine() == "sbsa":
42-
print("simpleP2P is not supported on sbsa - waiving sample")
43-
return
40+
print("simpleP2P is not supported on sbsa", file=sys.stderr)
41+
sys.exit(1)
4442

4543
# Number of GPUs
4644
print("Checking for multiple GPUs...")
4745
gpu_n = checkCudaErrors(cudart.cudaGetDeviceCount())
4846
print(f"CUDA-capable device count: {gpu_n}")
4947

5048
if gpu_n < 2:
51-
print("Two or more GPUs with Peer-to-Peer access capability are required")
52-
return
49+
print("Two or more GPUs with Peer-to-Peer access capability are required", file=sys.stderr)
50+
sys.exit(1)
5351

5452
prop = [checkCudaErrors(cudart.cudaGetDeviceProperties(i)) for i in range(gpu_n)]
5553
# Check possibility for peer access
@@ -80,9 +78,9 @@ def main():
8078
break
8179

8280
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
81+
print("Two or more GPUs with Peer-to-Peer access capability are required.", file=sys.stderr)
82+
print("Peer to Peer access is not available amongst GPUs in the system, waiving test.", file=sys.stderr)
83+
sys.exit(1)
8684

8785
# Use first pair of p2p capable GPUs detected
8886
gpuid = [p2pCapableGPUs[0], p2pCapableGPUs[1]]
@@ -239,9 +237,8 @@ def main():
239237
checkCudaErrors(cudart.cudaSetDevice(i))
240238

241239
if error_count != 0:
242-
print("Test failed!")
243-
sys.exit(-1)
244-
print("Test passed!")
240+
print("Test failed!", file=sys.stderr)
241+
sys.exit(1)
245242

246243

247244
if __name__ == "__main__":

cuda_bindings/examples/0_Introduction/simpleZeroCopy_test.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,27 @@ def main():
3333
bPinGenericMemory = False
3434

3535
if platform.system() == "Darwin":
36-
print("simpleZeroCopy is not supported on Mac OSX - waiving sample")
37-
return
36+
print("simpleZeroCopy is not supported on Mac OSX", file=sys.stderr)
37+
sys.exit(1)
3838

3939
if platform.machine() == "armv7l":
40-
print("simpleZeroCopy is not supported on ARMv7 - waiving sample")
41-
return
40+
print("simpleZeroCopy is not supported on ARMv7", file=sys.stderr)
41+
sys.exit(1)
4242

4343
if platform.machine() == "aarch64":
44-
print("simpleZeroCopy is not supported on aarch64 - waiving sample")
45-
return
44+
print("simpleZeroCopy is not supported on aarch64", file=sys.stderr)
45+
sys.exit(1)
4646

4747
if platform.machine() == "sbsa":
48-
print("simpleZeroCopy is not supported on sbsa - waiving sample")
49-
return
48+
print("simpleZeroCopy is not supported on sbsa", file=sys.stderr)
49+
sys.exit(1)
5050

5151
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
52+
print("Usage: simpleZeroCopy [OPTION]\n", file=sys.stderr)
53+
print("Options:", file=sys.stderr)
54+
print(" device=[device #] Specify the device to be used", file=sys.stderr)
55+
print(" use_generic_memory (optional) use generic page-aligned for system memory", file=sys.stderr)
56+
sys.exit(1)
5757

5858
# Get the device selected by the user or default to 0, and then set it.
5959
if checkCmdLineFlag("device="):
@@ -78,8 +78,8 @@ def main():
7878
deviceProp = checkCudaErrors(cudart.cudaGetDeviceProperties(idev))
7979

8080
if not deviceProp.canMapHostMemory:
81-
print(f"Device {idev} does not support mapping CPU host memory!")
82-
return
81+
print(f"Device {idev} does not support mapping CPU host memory!", file=sys.stderr)
82+
sys.exit(1)
8383

8484
checkCudaErrors(cudart.cudaSetDeviceFlags(cudart.cudaDeviceMapHost))
8585

@@ -177,9 +177,8 @@ def main():
177177
checkCudaErrors(cudart.cudaFreeHost(c))
178178

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

184183

185184
if __name__ == "__main__":

cuda_bindings/examples/0_Introduction/systemWideAtomics_test.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,27 +166,25 @@ def verify(testData, length):
166166

167167
def main():
168168
if os.name == "nt":
169-
print("Atomics not supported on Windows")
170-
return
169+
print("Atomics not supported on Windows", file=sys.stderr)
170+
sys.exit(1)
171171

172172
# set device
173173
dev_id = findCudaDevice()
174174
device_prop = checkCudaErrors(cudart.cudaGetDeviceProperties(dev_id))
175175

176176
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
177+
print("Unified Memory not supported on this device", file=sys.stderr)
178+
sys.exit(1)
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+
print("This sample requires a device in either default or process exclusive mode", file=sys.stderr)
183+
sys.exit(1)
186184

187185
if device_prop.major < 6:
188-
print("Requires a minimum CUDA compute 6.0 capability, waiving testing.")
189-
return
186+
print("Requires a minimum CUDA compute 6.0 capability, waiving testing.", file=sys.stderr)
187+
sys.exit(1)
190188

191189
numThreads = 256
192190
numBlocks = 64
@@ -240,9 +238,9 @@ def main():
240238
else:
241239
checkCudaErrors(cudart.cudaFree(atom_arr))
242240

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

247245

248246
if __name__ == "__main__":

cuda_bindings/examples/0_Introduction/vectorAddDrv_test.py

Lines changed: 4 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,8 @@ 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+
print("Accessing pageable memory directly requires UVA", file=sys.stderr)
48+
sys.exit(1)
5049

5150
kernelHelper = common.KernelHelper(vectorAddDrv, int(cuDevice))
5251
_VecAdd_kernel = kernelHelper.getFunction(b"VecAdd_kernel")
@@ -106,9 +105,9 @@ def main():
106105
checkCudaErrors(cuda.cuMemFree(d_C))
107106

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

113112

114113
if __name__ == "__main__":

cuda_bindings/examples/0_Introduction/vectorAddMMAP_test.py

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

190190

191191
def main():
192-
print("Vector Addition (Driver API)")
193-
194192
if platform.system() == "Darwin":
195-
print("vectorAddMMAP is not supported on Mac OSX - waiving sample")
196-
return
193+
print("vectorAddMMAP is not supported on Mac OSX", file=sys.stderr)
194+
sys.exit(1)
197195

198196
if platform.machine() == "armv7l":
199-
print("vectorAddMMAP is not supported on ARMv7 - waiving sample")
200-
return
197+
print("vectorAddMMAP is not supported on ARMv7", file=sys.stderr)
198+
sys.exit(1)
201199

202200
if platform.machine() == "aarch64":
203-
print("vectorAddMMAP is not supported on aarch64 - waiving sample")
204-
return
201+
print("vectorAddMMAP is not supported on aarch64", file=sys.stderr)
202+
sys.exit(1)
205203

206204
if platform.machine() == "sbsa":
207-
print("vectorAddMMAP is not supported on sbsa - waiving sample")
208-
return
205+
print("vectorAddMMAP is not supported on sbsa", file=sys.stderr)
206+
sys.exit(1)
209207

210208
N = 50000
211209
size = N * np.dtype(np.float32).itemsize
@@ -224,8 +222,8 @@ def main():
224222
)
225223
print(f"Device {cuDevice} VIRTUAL ADDRESS MANAGEMENT SUPPORTED = {attributeVal}.")
226224
if not attributeVal:
227-
print(f"Device {cuDevice} doesn't support VIRTUAL ADDRESS MANAGEMENT.")
228-
return
225+
print(f"Device {cuDevice} doesn't support VIRTUAL ADDRESS MANAGEMENT.", file=sys.stderr)
226+
sys.exit(1)
229227

230228
# The vector addition happens on cuDevice, so the allocations need to be mapped there.
231229
mappingDevices = [cuDevice]
@@ -298,9 +296,9 @@ def main():
298296

299297
checkCudaErrors(cuda.cuCtxDestroy(cuContext))
300298

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

305303

306304
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,22 @@ 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():
200194
if platform.system() == "Darwin":
201-
print("streamOrderedAllocation is not supported on Mac OSX - waiving sample")
202-
return
195+
print("streamOrderedAllocation is not supported on Mac OSX", file=sys.stderr)
196+
sys.exit(1)
203197

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

211205
dev = findCudaDevice()
212206

@@ -218,8 +212,8 @@ def main():
218212
cudart.cudaDeviceGetAttribute(cuda.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_MEMORY_POOLS_SUPPORTED, dev)
219213
)
220214
if not isMemPoolSupported:
221-
print("Waiving execution as device does not support Memory Pools")
222-
return
215+
print("Waiving execution as device does not support Memory Pools", file=sys.stderr)
216+
sys.exit(1)
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)