init
This commit is contained in:
parent
9f1e9881d6
commit
87f1181a1a
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.idea/*
|
||||
cmake-*
|
||||
build/
|
15
CMakeLists.txt
Normal file
15
CMakeLists.txt
Normal file
@ -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 $<$<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
|
||||
)
|
13
safe_cuda/stream_related.cpp
Normal file
13
safe_cuda/stream_related.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include "stream_related.h"
|
||||
|
||||
namespace safe_cuda {
|
||||
returnType<std::unique_ptr<CUstream_st, streamDestroyType>> 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 };
|
||||
}
|
||||
|
||||
}
|
21
safe_cuda/stream_related.h
Normal file
21
safe_cuda/stream_related.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef RAIISAFECUDA_STREAM_RELATED_H
|
||||
#define RAIISAFECUDA_STREAM_RELATED_H
|
||||
|
||||
#include <memory>
|
||||
#include <cuda_runtime_api.h>
|
||||
#include <variant>
|
||||
|
||||
namespace safe_cuda {
|
||||
template<typename T>
|
||||
using returnType = std::variant<T, cudaError_t>;
|
||||
|
||||
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<std::unique_ptr<CUstream_st, streamDestroyType>> create_stream() noexcept;
|
||||
}
|
||||
|
||||
#endif //RAIISAFECUDA_STREAM_RELATED_H
|
Loading…
Reference in New Issue
Block a user