From 87f1181a1a4362479ec1526896c6e54dc58201c9 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Thu, 21 Mar 2024 16:21:05 +0100 Subject: [PATCH 1/4] 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 From 22ebb857eb3d9f62d313f927545d46f8fdd8cc43 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Thu, 21 Mar 2024 18:02:47 +0100 Subject: [PATCH 2/4] =?UTF-8?q?init=20=E2=80=94=20unit=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 10 ++++++++-- tests/CMakeLists.txt | 26 ++++++++++++++++++++++++++ tests/tests_stream.cpp | 9 +++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/tests_stream.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ad1f01..c25b0a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ -cmake_minimum_required(VERSION 3.28) -project(raiiSafeCuda) +cmake_minimum_required(VERSION 3.28 FATAL_ERROR) +project(raiiSafeCuda CXX) + +option(ENABLE_TESTS "Enable tests' cmake target." ON) find_package(CUDAToolkit REQUIRED) @@ -13,3 +15,7 @@ set_target_properties(raiiSafeCuda PROPERTIES CXX_EXTENSIONS OFF INTERPROCEDURAL_OPTIMIZATION ON ) + +if (ENABLE_TESTS) + add_subdirectory(tests) +endif () diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..0f967ee --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,26 @@ +project(testsRaiiSafeCuda CXX) + +find_package(Catch2) +if (NOT Catch2_FOUND) + message(STATUS "Using FetchContent to download and use Catch2") + include(FetchContent) + fetchcontent_declare( + Catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2 + GIT_TAG v3.5.3 + EXCLUDE_FROM_ALL + OVERRIDE_FIND_PACKAGE + ) + fetchcontent_makeavailable(Catch2) + find_package(Catch2 3 REQUIRED) +endif () + +include(CTest) +enable_testing() +include(Catch) +include(CatchAddTests) + +add_executable(tests tests_stream.cpp) +target_link_libraries(tests Catch2::Catch2WithMain) + +catch_discover_tests(tests WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/tests/tests_stream.cpp b/tests/tests_stream.cpp new file mode 100644 index 0000000..2e71430 --- /dev/null +++ b/tests/tests_stream.cpp @@ -0,0 +1,9 @@ +// +// Created by postaron on 21/03/24. +// + +#include + +TEST_CASE("hello", "world") { + +} From 24a83dfa08164031908d77dc65f599ce95bf3ccb Mon Sep 17 00:00:00 2001 From: Pcornat Date: Mon, 25 Mar 2024 18:13:16 +0100 Subject: [PATCH 3/4] refactor repo --- CMakeLists.txt | 10 +++++++++- include/malloc_unmanagef.hpp | 12 ++++++++++++ .../stream_related.h => include/stream_related.hpp | 11 ++++------- src/malloc_unmanagef.cpp | 5 +++++ {safe_cuda => src}/stream_related.cpp | 4 ++-- 5 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 include/malloc_unmanagef.hpp rename safe_cuda/stream_related.h => include/stream_related.hpp (54%) create mode 100644 src/malloc_unmanagef.cpp rename {safe_cuda => src}/stream_related.cpp (68%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c25b0a1..a83ad04 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,15 @@ option(ENABLE_TESTS "Enable tests' cmake target." ON) find_package(CUDAToolkit REQUIRED) -add_library(raiiSafeCuda SHARED safe_cuda/stream_related.cpp) +file(GLOB_RECURSE SAFE_CUDA_HEADERS include/*.hpp) +file(GLOB_RECURSE SAFE_CUDA_SOURCES src/*.cpp) + + +add_library(raiiSafeCuda SHARED + ${SAFE_CUDA_HEADERS} + ${SAFE_CUDA_SOURCES} +) +target_precompile_headers(raiiSafeCuda PRIVATE ${SAFE_CUDA_HEADERS}) target_compile_definitions(raiiSafeCuda PRIVATE $<$:_GLIBCXX_DEBUG>) target_compile_options(raiiSafeCuda PRIVATE -pipe -Wall -Wextra -pedantic) target_link_libraries(raiiSafeCuda CUDA::cudart) diff --git a/include/malloc_unmanagef.hpp b/include/malloc_unmanagef.hpp new file mode 100644 index 0000000..3a8f745 --- /dev/null +++ b/include/malloc_unmanagef.hpp @@ -0,0 +1,12 @@ +// +// Created by postaron on 25/03/24. +// + +#ifndef RAIISAFECUDA_MALLOC_UNMANAGEF_HPP +#define RAIISAFECUDA_MALLOC_UNMANAGEF_HPP + +namespace safe_cuda { + +} + +#endif //RAIISAFECUDA_MALLOC_UNMANAGEF_HPP diff --git a/safe_cuda/stream_related.h b/include/stream_related.hpp similarity index 54% rename from safe_cuda/stream_related.h rename to include/stream_related.hpp index 64cb03c..17594b4 100644 --- a/safe_cuda/stream_related.h +++ b/include/stream_related.hpp @@ -1,21 +1,18 @@ -#ifndef RAIISAFECUDA_STREAM_RELATED_H -#define RAIISAFECUDA_STREAM_RELATED_H +#ifndef RAIISAFECUDA_STREAM_RELATED_HPP +#define RAIISAFECUDA_STREAM_RELATED_HPP #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; + std::variant, cudaError_t> create_stream() noexcept; } -#endif //RAIISAFECUDA_STREAM_RELATED_H +#endif //RAIISAFECUDA_STREAM_RELATED_HPP diff --git a/src/malloc_unmanagef.cpp b/src/malloc_unmanagef.cpp new file mode 100644 index 0000000..bd282b5 --- /dev/null +++ b/src/malloc_unmanagef.cpp @@ -0,0 +1,5 @@ +// +// Created by postaron on 25/03/24. +// + +#include "malloc_unmanagef.hpp" diff --git a/safe_cuda/stream_related.cpp b/src/stream_related.cpp similarity index 68% rename from safe_cuda/stream_related.cpp rename to src/stream_related.cpp index 666a41d..a863685 100644 --- a/safe_cuda/stream_related.cpp +++ b/src/stream_related.cpp @@ -1,7 +1,7 @@ -#include "stream_related.h" +#include "stream_related.hpp" namespace safe_cuda { - returnType> create_stream() noexcept { + std::variant, cudaError_t> create_stream() noexcept { cudaStream_t stream = nullptr; const cudaError_t error = cudaStreamCreate(&stream); if (error != cudaSuccess) { From 4b474e21edde39ece5c605c517ee8bf320437611 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Mon, 25 Mar 2024 18:14:17 +0100 Subject: [PATCH 4/4] Specify major version for Catch2 --- tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0f967ee..2bc2b61 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,6 @@ project(testsRaiiSafeCuda CXX) -find_package(Catch2) +find_package(Catch2 3) if (NOT Catch2_FOUND) message(STATUS "Using FetchContent to download and use Catch2") include(FetchContent)