1
0
Fork 0

Compare commits

...

4 Commits

Author SHA1 Message Date
Pcornat 4b474e21ed
Specify major version for Catch2 2024-03-25 18:21:04 +01:00
Pcornat 24a83dfa08
refactor repo 2024-03-25 18:21:04 +01:00
Pcornat 22ebb857eb
init — unit tests 2024-03-25 18:21:04 +01:00
Pcornat 87f1181a1a
init 2024-03-25 18:21:04 +01:00
8 changed files with 115 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea/*
cmake-*
build/

29
CMakeLists.txt Normal file
View File

@ -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 $<$<CONFIG:Debug>:_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 ()

View File

@ -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

View File

@ -0,0 +1,18 @@
#ifndef RAIISAFECUDA_STREAM_RELATED_HPP
#define RAIISAFECUDA_STREAM_RELATED_HPP
#include <memory>
#include <cuda_runtime_api.h>
#include <variant>
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<std::unique_ptr<CUstream_st, streamDestroyType>, cudaError_t> create_stream() noexcept;
}
#endif //RAIISAFECUDA_STREAM_RELATED_HPP

5
src/malloc_unmanagef.cpp Normal file
View File

@ -0,0 +1,5 @@
//
// Created by postaron on 25/03/24.
//
#include "malloc_unmanagef.hpp"

13
src/stream_related.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "stream_related.hpp"
namespace safe_cuda {
std::variant<std::unique_ptr<CUstream_st, streamDestroyType>, cudaError_t> create_stream() noexcept {
cudaStream_t stream = nullptr;
const cudaError_t error = cudaStreamCreate(&stream);
if (error != cudaSuccess) {
return error;
}
return std::unique_ptr<CUstream_st, streamDestroyType>{ stream, cudaStreamDestroy };
}
}

26
tests/CMakeLists.txt Normal file
View File

@ -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})

9
tests/tests_stream.cpp Normal file
View File

@ -0,0 +1,9 @@
//
// Created by postaron on 21/03/24.
//
#include <catch2/catch_all.hpp>
TEST_CASE("hello", "world") {
}