Skip to content

Commit 297c7c0

Browse files
committed
fix(examples): standardize explicit resource cleanup paths
Ensure cuda.core and cuda.bindings examples deterministically release streams, buffers, and loaded modules so examples remain safe when copied into long-running programs. Made-with: Cursor
1 parent 0aa9632 commit 297c7c0

21 files changed

+607
-519
lines changed

cuda_bindings/examples/0_Introduction/clock_nvrtc_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def main():
102102
checkCudaErrors(cuda.cuMemFree(dinput))
103103
checkCudaErrors(cuda.cuMemFree(doutput))
104104
checkCudaErrors(cuda.cuMemFree(dtimer))
105+
kernelHelper.close()
105106

106107
avgElapsedClocks = 0.0
107108

cuda_bindings/examples/0_Introduction/simpleCubemapTexture_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ def main():
205205
checkCudaErrors(cudart.cudaMemcpy(h_odata, d_data, size, cudart.cudaMemcpyKind.cudaMemcpyDeviceToHost))
206206

207207
checkCudaErrors(cudart.cudaDestroyTextureObject(tex))
208+
kernelHelper.close()
208209
checkCudaErrors(cudart.cudaFree(d_data))
209210
checkCudaErrors(cudart.cudaFreeArray(cu_3darray))
210211

cuda_bindings/examples/0_Introduction/simpleP2P_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ def main():
227227

228228
# Cleanup and shutdown
229229
print("Shutting down...")
230+
for helper in kernelHelper:
231+
if helper is not None:
232+
helper.close()
230233
checkCudaErrors(cudart.cudaEventDestroy(start_event))
231234
checkCudaErrors(cudart.cudaEventDestroy(stop_event))
232235
checkCudaErrors(cudart.cudaSetDevice(gpuid[0]))

cuda_bindings/examples/0_Introduction/simpleZeroCopy_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ def main():
163163
errorNorm = math.sqrt(errorNorm)
164164
refNorm = math.sqrt(refNorm)
165165

166+
kernelHelper.close()
167+
166168
# Memory clean up
167169

168170
print("Releasing CPU memory...")

cuda_bindings/examples/0_Introduction/systemWideAtomics_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def main():
234234

235235
# Compute & verify reference solution
236236
testResult = verify(atom_arr_h, numThreads * numBlocks)
237+
kernelHelper.close()
237238

238239
if device_prop.pageableMemoryAccess:
239240
pass

cuda_bindings/examples/0_Introduction/vectorAddDrv_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def main():
104104
checkCudaErrors(cuda.cuMemFree(d_A))
105105
checkCudaErrors(cuda.cuMemFree(d_B))
106106
checkCudaErrors(cuda.cuMemFree(d_C))
107+
kernelHelper.close()
107108

108109
checkCudaErrors(cuda.cuCtxDestroy(cuContext))
109110
print("{}".format("Result = PASS" if i + 1 == N else "Result = FAIL"))

cuda_bindings/examples/0_Introduction/vectorAddMMAP_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ def main():
295295
checkCudaErrors(simpleFreeMultiDeviceMmap(d_A, allocationSize))
296296
checkCudaErrors(simpleFreeMultiDeviceMmap(d_B, allocationSize))
297297
checkCudaErrors(simpleFreeMultiDeviceMmap(d_C, allocationSize))
298+
kernelHelper.close()
298299

299300
checkCudaErrors(cuda.cuCtxDestroy(cuContext))
300301

cuda_bindings/examples/2_Concepts_and_Techniques/streamOrderedAllocation_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def main():
239239

240240
ret1 = basicStreamOrderedAllocation(dev, nelem, a, b, c)
241241
ret2 = streamOrderedAllocationPostSync(dev, nelem, a, b, c)
242+
kernelHelper.close()
242243

243244
if not ret1 or not ret2:
244245
sys.exit(-1)

cuda_bindings/examples/3_CUDA_Features/globalToShmemAsyncCopy_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,7 @@ def main():
12111211
_MatrixMulNaiveLargeChunk = kernelHelper.getFunction(b"MatrixMulNaiveLargeChunk")
12121212

12131213
matrix_result = MatrixMultiply(dimsA, dimsB, selected_kernel)
1214+
kernelHelper.close()
12141215

12151216
if matrix_result != 0:
12161217
sys.exit(-1)

cuda_bindings/examples/3_CUDA_Features/simpleCudaGraphs_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ def cudaGraphsUsingStreamCapture(inputVec_h, inputVec_d, outputVec_d, result_d,
356356
checkCudaErrors(cudart.cudaGraphExecDestroy(clonedGraphExec))
357357
checkCudaErrors(cudart.cudaGraphDestroy(graph))
358358
checkCudaErrors(cudart.cudaGraphDestroy(clonedGraph))
359+
checkCudaErrors(cudart.cudaEventDestroy(memsetEvent2))
360+
checkCudaErrors(cudart.cudaEventDestroy(memsetEvent1))
361+
checkCudaErrors(cudart.cudaEventDestroy(forkStreamEvent))
362+
checkCudaErrors(cudart.cudaStreamDestroy(stream3))
359363
checkCudaErrors(cudart.cudaStreamDestroy(stream1))
360364
checkCudaErrors(cudart.cudaStreamDestroy(stream2))
361365
checkCudaErrors(cudart.cudaStreamDestroy(streamForGraph))
@@ -387,6 +391,7 @@ def main():
387391

388392
cudaGraphsManual(inputVec_h, inputVec_d, outputVec_d, result_d, size, maxBlocks)
389393
cudaGraphsUsingStreamCapture(inputVec_h, inputVec_d, outputVec_d, result_d, size, maxBlocks)
394+
kernelHelper.close()
390395

391396
checkCudaErrors(cudart.cudaFree(inputVec_d))
392397
checkCudaErrors(cudart.cudaFree(outputVec_d))

0 commit comments

Comments
 (0)