| | | 1 | | // Copyright 2025 Digital Holography Foundation |
| | | 2 | | // |
| | | 3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
| | | 4 | | // you may not use this file except in compliance with the License. |
| | | 5 | | // You may obtain a copy of the License at |
| | | 6 | | // |
| | | 7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
| | | 8 | | // |
| | | 9 | | // Unless required by applicable law or agreed to in writing, software |
| | | 10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
| | | 11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | | 12 | | // See the License for the specific language governing permissions and |
| | | 13 | | // limitations under the License. |
| | | 14 | | |
| | | 15 | | #pragma once |
| | | 16 | | |
| | | 17 | | #include <cuda_runtime.h> |
| | | 18 | | #include <memory> |
| | | 19 | | #include <spdlog/spdlog.h> |
| | | 20 | | #include <stdexcept> |
| | | 21 | | |
| | | 22 | | namespace curaii::detail { |
| | | 23 | | |
| | | 24 | | void log_cuda_failure(spdlog::level::level_enum lvl, cudaError_t code, const char *expr, |
| | | 25 | | const char *file, int line); |
| | | 26 | | |
| | | 27 | | } // namespace curaii::detail |
| | | 28 | | |
| | | 29 | | #define CUDA_CHECK(expr) \ |
| | | 30 | | do { \ |
| | | 31 | | cudaError_t err__ = (expr); \ |
| | | 32 | | if (err__ != cudaSuccess) { \ |
| | | 33 | | ::curaii::detail::log_cuda_failure(spdlog::level::warn, err__, #expr, __FILE__, __LINE__); \ |
| | | 34 | | throw ::curaii::CudaError(err__, #expr, __FILE__, __LINE__); \ |
| | | 35 | | } \ |
| | | 36 | | } while (false) |
| | | 37 | | |
| | | 38 | | #define CUDA_CHECK_NT(expr) \ |
| | | 39 | | do { \ |
| | | 40 | | cudaError_t err__ = (expr); \ |
| | | 41 | | if (err__ != cudaSuccess) { \ |
| | | 42 | | ::curaii::detail::log_cuda_failure(spdlog::level::critical, err__, #expr, __FILE__, \ |
| | | 43 | | __LINE__); \ |
| | | 44 | | std::abort(); \ |
| | | 45 | | } \ |
| | | 46 | | } while (false) |
| | | 47 | | |
| | | 48 | | namespace curaii { |
| | | 49 | | |
| | | 50 | | class CudaError : public std::runtime_error { |
| | | 51 | | public: |
| | | 52 | | explicit CudaError(cudaError_t code, const char *what, const char *file, int line); |
| | | 53 | | |
| | | 54 | | [[nodiscard]] cudaError_t code() const noexcept; |
| | | 55 | | |
| | | 56 | | private: |
| | | 57 | | static std::string make_message(cudaError_t code, const char *what, const char *file, int line); |
| | | 58 | | |
| | | 59 | | cudaError_t code_; |
| | | 60 | | }; |
| | | 61 | | |
| | | 62 | | struct HostDeleter { |
| | | 63 | | void operator()(void *ptr) const noexcept; |
| | | 64 | | }; |
| | | 65 | | |
| | | 66 | | struct DeviceDeleter { |
| | | 67 | | void operator()(void *ptr) const noexcept; |
| | | 68 | | }; |
| | | 69 | | |
| | | 70 | | template <typename T> using unique_host_ptr = std::unique_ptr<T, HostDeleter>; |
| | | 71 | | |
| | | 72 | | template <typename T> [[nodiscard]] unique_host_ptr<T> make_unique_host_ptr(size_t count); |
| | | 73 | | |
| | | 74 | | template <typename T> using unique_device_ptr = std::unique_ptr<T, DeviceDeleter>; |
| | | 75 | | |
| | | 76 | | template <typename T> |
| | | 77 | | [[nodiscard]] unique_device_ptr<T> make_unique_device_ptr(size_t count, cudaStream_t stream = 0); |
| | | 78 | | |
| | | 79 | | class CudaStream { |
| | | 80 | | public: |
| | | 81 | | explicit CudaStream(unsigned flags = cudaStreamDefault, int priority = 0); |
| | | 82 | | ~CudaStream() noexcept; |
| | | 83 | | |
| | | 84 | | CudaStream(const CudaStream &) = delete; |
| | | 85 | | CudaStream &operator=(const CudaStream &) = delete; |
| | | 86 | | |
| | | 87 | | CudaStream(CudaStream &&other) noexcept; |
| | | 88 | | CudaStream &operator=(CudaStream &&other) noexcept; |
| | | 89 | | |
| | | 90 | | [[nodiscard]] cudaStream_t get() const noexcept; |
| | | 91 | | [[nodiscard]] cudaStream_t release() noexcept; |
| | | 92 | | void reset(cudaStream_t s = nullptr) noexcept; |
| | | 93 | | explicit operator bool() const noexcept; |
| | | 94 | | |
| | | 95 | | private: |
| | 1 | 96 | | cudaStream_t stream_{nullptr}; |
| | | 97 | | }; |
| | | 98 | | |
| | | 99 | | } // namespace curaii |
| | | 100 | | |
| | | 101 | | #define CURAII_CUDA_HXX_INCLUDED |
| | | 102 | | #include "curaii/cuda.hxx" |
| | | 103 | | #undef CURAII_CUDA_HXX_INCLUDED |