From 87f1181a1a4362479ec1526896c6e54dc58201c9 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Thu, 21 Mar 2024 16:21:05 +0100 Subject: [PATCH] init --- .gitignore | 3 +++ CMakeLists.txt | 15 +++++++++++++++ safe_cuda/stream_related.cpp | 13 +++++++++++++ safe_cuda/stream_related.h | 21 +++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 safe_cuda/stream_related.cpp create mode 100644 safe_cuda/stream_related.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae82309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/* +cmake-* +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..5ad1f01 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.28) +project(raiiSafeCuda) + +find_package(CUDAToolkit REQUIRED) + +add_library(raiiSafeCuda SHARED safe_cuda/stream_related.cpp) +target_compile_definitions(raiiSafeCuda PRIVATE $<$:_GLIBCXX_DEBUG>) +target_compile_options(raiiSafeCuda PRIVATE -pipe -Wall -Wextra -pedantic) +target_link_libraries(raiiSafeCuda CUDA::cudart) + +set_target_properties(raiiSafeCuda PROPERTIES + CXX_STANDARD 20 + CXX_EXTENSIONS OFF + INTERPROCEDURAL_OPTIMIZATION ON +) diff --git a/safe_cuda/stream_related.cpp b/safe_cuda/stream_related.cpp new file mode 100644 index 0000000..666a41d --- /dev/null +++ b/safe_cuda/stream_related.cpp @@ -0,0 +1,13 @@ +#include "stream_related.h" + +namespace safe_cuda { + returnType> create_stream() noexcept { + cudaStream_t stream = nullptr; + const cudaError_t error = cudaStreamCreate(&stream); + if (error != cudaSuccess) { + return error; + } + return std::unique_ptr{ stream, cudaStreamDestroy }; + } + +} \ No newline at end of file diff --git a/safe_cuda/stream_related.h b/safe_cuda/stream_related.h new file mode 100644 index 0000000..64cb03c --- /dev/null +++ b/safe_cuda/stream_related.h @@ -0,0 +1,21 @@ +#ifndef RAIISAFECUDA_STREAM_RELATED_H +#define RAIISAFECUDA_STREAM_RELATED_H + +#include +#include +#include + +namespace safe_cuda { + template + using returnType = std::variant; + + using streamDestroyType = decltype(&cudaStreamDestroy); + + /** + * \brief It tries to create a stream, putting it in a smart pointer with its correct destructor. + * \return smart pointer if creation is OK, else the CUDA error + */ + returnType> create_stream() noexcept; +} + +#endif //RAIISAFECUDA_STREAM_RELATED_H