From ccaa81fa528cd1abeb70cfa30e6c1f4574c9798a Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Tue, 10 Sep 2024 15:36:01 -0400 Subject: [PATCH 1/4] Intentionally leak static CUDA resources --- cpp/include/kvikio/posix_io.hpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/cpp/include/kvikio/posix_io.hpp b/cpp/include/kvikio/posix_io.hpp index 9e88a3e265..e596760314 100644 --- a/cpp/include/kvikio/posix_io.hpp +++ b/cpp/include/kvikio/posix_io.hpp @@ -42,16 +42,7 @@ class StreamsByThread { public: StreamsByThread() = default; - ~StreamsByThread() noexcept - { - for (auto& [_, stream] : _streams) { - try { - CUDA_DRIVER_TRY(cudaAPI::instance().StreamDestroy(stream)); - } catch (const CUfileException& e) { - std::cerr << e.what() << std::endl; - } - } - } + ~StreamsByThread() = default; static CUstream get(CUcontext ctx, std::thread::id thd_id) { From f919ce436cbfd85a16c4eded76420947f98dee08 Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Tue, 10 Sep 2024 16:32:26 -0400 Subject: [PATCH 2/4] Reformat --- cpp/include/kvikio/posix_io.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/include/kvikio/posix_io.hpp b/cpp/include/kvikio/posix_io.hpp index e596760314..bb06d44fa1 100644 --- a/cpp/include/kvikio/posix_io.hpp +++ b/cpp/include/kvikio/posix_io.hpp @@ -41,7 +41,7 @@ class StreamsByThread { std::map, CUstream> _streams; public: - StreamsByThread() = default; + StreamsByThread() = default; ~StreamsByThread() = default; static CUstream get(CUcontext ctx, std::thread::id thd_id) From df06a5d12f7b13aab7fa5b6a7fe7b95fcaae01b0 Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Wed, 11 Sep 2024 14:30:50 -0400 Subject: [PATCH 3/4] Add references in the comment --- cpp/include/kvikio/posix_io.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cpp/include/kvikio/posix_io.hpp b/cpp/include/kvikio/posix_io.hpp index bb06d44fa1..26e103212a 100644 --- a/cpp/include/kvikio/posix_io.hpp +++ b/cpp/include/kvikio/posix_io.hpp @@ -41,7 +41,12 @@ class StreamsByThread { std::map, CUstream> _streams; public: - StreamsByThread() = default; + StreamsByThread() = default; + + // Here we intentionally do not destroy in the destructor the CUDA resources + // (e.g. CUstream) with static storage duration, but instead let them leak + // on program termination. This is to prevent undefined behavior in CUDA. See + // ~StreamsByThread() = default; static CUstream get(CUcontext ctx, std::thread::id thd_id) From 49c1bd03fce57d268923646cd89d2ace67b6b366 Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Wed, 11 Sep 2024 14:36:14 -0400 Subject: [PATCH 4/4] Add more clarifying comments --- cpp/include/kvikio/posix_io.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp/include/kvikio/posix_io.hpp b/cpp/include/kvikio/posix_io.hpp index 26e103212a..9a28e06eec 100644 --- a/cpp/include/kvikio/posix_io.hpp +++ b/cpp/include/kvikio/posix_io.hpp @@ -47,6 +47,8 @@ class StreamsByThread { // (e.g. CUstream) with static storage duration, but instead let them leak // on program termination. This is to prevent undefined behavior in CUDA. See // + // This also prevents crash (segmentation fault) if clients call + // cuDevicePrimaryCtxReset() or cudaDeviceReset() before program termination. ~StreamsByThread() = default; static CUstream get(CUcontext ctx, std::thread::id thd_id)