Skip to content

Commit bcba37c

Browse files
authored
Standardize error handling and output across all examples (#1678) (#1694)
- 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 eabaf4b commit bcba37c

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
@@ -59,11 +59,10 @@ def elems_to_bytes(nelems, dt):
5959

6060

6161
def main():
62-
print("CUDA Clock sample")
62+
import pytest
6363

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

6867
timer = np.empty(NUM_BLOCKS * 2, dtype="int64")
6968
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
@@ -91,8 +91,9 @@ def main():
9191
f"CUDA device [{deviceProps.name}] has {deviceProps.multiProcessorCount} Multi-Processors SM {deviceProps.major}.{deviceProps.minor}"
9292
)
9393
if deviceProps.major < 2:
94-
print("Test requires SM 2.0 or higher for support of Texture Arrays. Test will exit...")
95-
sys.exit()
94+
import pytest
95+
96+
pytest.skip("Test requires SM 2.0 or higher for support of Texture Arrays.")
9697

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

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

219218

220219
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
@@ -25,32 +25,27 @@
2525

2626

2727
def main():
28-
print("Starting...")
28+
import pytest
2929

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

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

3836
if platform.machine() == "aarch64":
39-
print("simpleP2P is not supported on aarch64 - waiving sample")
40-
return
37+
pytest.skip("simpleP2P is not supported on aarch64")
4138

4239
if platform.machine() == "sbsa":
43-
print("simpleP2P is not supported on sbsa - waiving sample")
44-
return
40+
pytest.skip("simpleP2P is not supported on sbsa")
4541

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

5147
if gpu_n < 2:
52-
print("Two or more GPUs with Peer-to-Peer access capability are required")
53-
return
48+
pytest.skip("Two or more GPUs with Peer-to-Peer access capability are required")
5449

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

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

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

242235
if error_count != 0:
243-
print("Test failed!")
244-
sys.exit(-1)
245-
print("Test passed!")
236+
print("Test failed!", file=sys.stderr)
237+
sys.exit(1)
246238

247239

248240
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
@@ -33,28 +33,26 @@ def main():
3333
idev = 0
3434
bPinGenericMemory = False
3535

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

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

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

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

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

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

8179
if not deviceProp.canMapHostMemory:
82-
print(f"Device {idev} does not support mapping CPU host memory!")
83-
return
80+
pytest.skip(f"Device {idev} does not support mapping CPU host memory!")
8481

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

@@ -178,9 +175,8 @@ def main():
178175
checkCudaErrors(cudart.cudaFreeHost(c))
179176

180177
if errorNorm / refNorm >= 1.0e-7:
181-
print("FAILED")
182-
sys.exit(-1)
183-
print("PASSED")
178+
print("FAILED", file=sys.stderr)
179+
sys.exit(1)
184180

185181

186182
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
@@ -166,28 +166,24 @@ def verify(testData, length):
166166

167167

168168
def main():
169+
import pytest
170+
169171
if os.name == "nt":
170-
print("Atomics not supported on Windows")
171-
return
172+
pytest.skip("Atomics not supported on Windows")
172173

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

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

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

188185
if device_prop.major < 6:
189-
print("Requires a minimum CUDA compute 6.0 capability, waiving testing.")
190-
return
186+
pytest.skip("Requires a minimum CUDA compute 6.0 capability")
191187

192188
numThreads = 256
193189
numBlocks = 64
@@ -241,9 +237,9 @@ def main():
241237
else:
242238
checkCudaErrors(cudart.cudaFree(atom_arr))
243239

244-
print("systemWideAtomics completed, returned {}".format("OK" if testResult else "ERROR!"))
245240
if not testResult:
246-
sys.exit(-1)
241+
print("systemWideAtomics completed with errors", file=sys.stderr)
242+
sys.exit(1)
247243

248244

249245
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
@@ -32,7 +32,6 @@
3232

3333

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

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

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

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

114114

115115
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
@@ -190,23 +190,19 @@ def simpleFreeMultiDeviceMmap(dptr, size):
190190

191191

192192
def main():
193-
print("Vector Addition (Driver API)")
193+
import pytest
194194

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

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

203201
if platform.machine() == "aarch64":
204-
print("vectorAddMMAP is not supported on aarch64 - waiving sample")
205-
return
202+
pytest.skip("vectorAddMMAP is not supported on aarch64")
206203

207204
if platform.machine() == "sbsa":
208-
print("vectorAddMMAP is not supported on sbsa - waiving sample")
209-
return
205+
pytest.skip("vectorAddMMAP is not supported on sbsa")
210206

211207
N = 50000
212208
size = N * np.dtype(np.float32).itemsize
@@ -225,8 +221,7 @@ def main():
225221
)
226222
print(f"Device {cuDevice} VIRTUAL ADDRESS MANAGEMENT SUPPORTED = {attributeVal}.")
227223
if not attributeVal:
228-
print(f"Device {cuDevice} doesn't support VIRTUAL ADDRESS MANAGEMENT.")
229-
return
224+
pytest.skip(f"Device {cuDevice} doesn't support VIRTUAL ADDRESS MANAGEMENT.")
230225

231226
# The vector addition happens on cuDevice, so the allocations need to be mapped there.
232227
mappingDevices = [cuDevice]
@@ -299,9 +294,9 @@ def main():
299294

300295
checkCudaErrors(cuda.cuCtxDestroy(cuContext))
301296

302-
print("{}".format("Result = PASS" if i + 1 == N else "Result = FAIL"))
303297
if i + 1 != N:
304-
sys.exit(-1)
298+
print("Result = FAIL", file=sys.stderr)
299+
sys.exit(1)
305300

306301

307302
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
@@ -93,9 +93,6 @@ def basicStreamOrderedAllocation(dev, nelem, a, b, c):
9393
errorNorm = math.sqrt(errorNorm)
9494
refNorm = math.sqrt(refNorm)
9595

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

10198
return errorNorm / refNorm < 1.0e-6
@@ -189,25 +186,23 @@ def streamOrderedAllocationPostSync(dev, nelem, a, b, c):
189186
errorNorm = math.sqrt(errorNorm)
190187
refNorm = math.sqrt(refNorm)
191188

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

197191
return errorNorm / refNorm < 1.0e-6
198192

199193

200194
def main():
195+
import pytest
196+
201197
if platform.system() == "Darwin":
202-
print("streamOrderedAllocation is not supported on Mac OSX - waiving sample")
203-
return
198+
pytest.skip("streamOrderedAllocation is not supported on Mac OSX")
204199

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

212207
dev = findCudaDevice()
213208

@@ -219,8 +214,7 @@ def main():
219214
cudart.cudaDeviceGetAttribute(cuda.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_MEMORY_POOLS_SUPPORTED, dev)
220215
)
221216
if not isMemPoolSupported:
222-
print("Waiving execution as device does not support Memory Pools")
223-
return
217+
pytest.skip("Waiving execution as device does not support Memory Pools")
224218

225219
global _vectorAddGPU
226220
kernelHelper = common.KernelHelper(streamOrderedAllocation, dev)
@@ -242,7 +236,7 @@ def main():
242236
ret2 = streamOrderedAllocationPostSync(dev, nelem, a, b, c)
243237

244238
if not ret1 or not ret2:
245-
sys.exit(-1)
239+
sys.exit(1)
246240

247241

248242
if __name__ == "__main__":

0 commit comments

Comments
 (0)