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..a83ad04 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,29 @@ +cmake_minimum_required(VERSION 3.28 FATAL_ERROR) +project(raiiSafeCuda CXX) + +option(ENABLE_TESTS "Enable tests' cmake target." ON) + +find_package(CUDAToolkit REQUIRED) + +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) + +set_target_properties(raiiSafeCuda PROPERTIES + CXX_STANDARD 20 + CXX_EXTENSIONS OFF + INTERPROCEDURAL_OPTIMIZATION ON +) + +if (ENABLE_TESTS) + add_subdirectory(tests) +endif () 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/include/stream_related.hpp b/include/stream_related.hpp new file mode 100644 index 0000000..17594b4 --- /dev/null +++ b/include/stream_related.hpp @@ -0,0 +1,18 @@ +#ifndef RAIISAFECUDA_STREAM_RELATED_HPP +#define RAIISAFECUDA_STREAM_RELATED_HPP + +#include +#include +#include + +namespace safe_cuda { + 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 + */ + std::variant, cudaError_t> create_stream() noexcept; +} + +#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/src/stream_related.cpp b/src/stream_related.cpp new file mode 100644 index 0000000..a863685 --- /dev/null +++ b/src/stream_related.cpp @@ -0,0 +1,13 @@ +#include "stream_related.hpp" + +namespace safe_cuda { + std::variant, cudaError_t> 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/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..2bc2b61 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,26 @@ +project(testsRaiiSafeCuda CXX) + +find_package(Catch2 3) +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") { + +}