|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cuda_runtime.h> |
| 4 | + |
| 5 | +#include <cstddef> |
| 6 | +#include <cstdint> |
| 7 | +#include <cstdio> |
| 8 | +#include <cstdlib> |
| 9 | +#include <limits> |
| 10 | + |
| 11 | +namespace quadtrix { |
| 12 | +namespace cuda { |
| 13 | + |
| 14 | +enum class DType : std::uint8_t { |
| 15 | + F32, |
| 16 | + F16, |
| 17 | + BF16, |
| 18 | + I32, |
| 19 | + U8, |
| 20 | +}; |
| 21 | + |
| 22 | +enum class DeviceKind : std::uint8_t { |
| 23 | + CPU, |
| 24 | + CUDA, |
| 25 | +}; |
| 26 | + |
| 27 | +struct Status { |
| 28 | + bool ok; |
| 29 | + cudaError_t cuda_error; |
| 30 | + const char* message; |
| 31 | + |
| 32 | + static Status success() { |
| 33 | + return {true, cudaSuccess, "ok"}; |
| 34 | + } |
| 35 | + |
| 36 | + static Status failure(cudaError_t error, const char* message) { |
| 37 | + return {false, error, message}; |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +inline const char* dtype_name(DType dtype) { |
| 42 | + switch (dtype) { |
| 43 | + case DType::F32: |
| 44 | + return "f32"; |
| 45 | + case DType::F16: |
| 46 | + return "f16"; |
| 47 | + case DType::BF16: |
| 48 | + return "bf16"; |
| 49 | + case DType::I32: |
| 50 | + return "i32"; |
| 51 | + case DType::U8: |
| 52 | + return "u8"; |
| 53 | + } |
| 54 | + return "unknown"; |
| 55 | +} |
| 56 | + |
| 57 | +inline std::size_t dtype_size(DType dtype) { |
| 58 | + switch (dtype) { |
| 59 | + case DType::F32: |
| 60 | + return 4; |
| 61 | + case DType::F16: |
| 62 | + return 2; |
| 63 | + case DType::BF16: |
| 64 | + return 2; |
| 65 | + case DType::I32: |
| 66 | + return 4; |
| 67 | + case DType::U8: |
| 68 | + return 1; |
| 69 | + } |
| 70 | + |
| 71 | + std::fprintf(stderr, "Unknown CUDA dtype value %u\n", static_cast<unsigned int>(dtype)); |
| 72 | + std::abort(); |
| 73 | +} |
| 74 | + |
| 75 | +inline bool checked_mul(std::size_t lhs, std::size_t rhs, std::size_t* out) { |
| 76 | + if (lhs != 0 && rhs > std::numeric_limits<std::size_t>::max() / lhs) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + *out = lhs * rhs; |
| 80 | + return true; |
| 81 | +} |
| 82 | + |
| 83 | +inline Status check_cuda(cudaError_t error, const char* expression, const char* file, int line) { |
| 84 | + if (error == cudaSuccess) { |
| 85 | + return Status::success(); |
| 86 | + } |
| 87 | + |
| 88 | + std::fprintf( |
| 89 | + stderr, |
| 90 | + "CUDA error at %s:%d: %s failed with %s\n", |
| 91 | + file, |
| 92 | + line, |
| 93 | + expression, |
| 94 | + cudaGetErrorString(error)); |
| 95 | + return Status::failure(error, expression); |
| 96 | +} |
| 97 | + |
| 98 | +inline void abort_on_cuda(cudaError_t error, const char* expression, const char* file, int line) { |
| 99 | + if (error == cudaSuccess) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + std::fprintf( |
| 104 | + stderr, |
| 105 | + "Fatal CUDA error at %s:%d: %s failed with %s\n", |
| 106 | + file, |
| 107 | + line, |
| 108 | + expression, |
| 109 | + cudaGetErrorString(error)); |
| 110 | + std::abort(); |
| 111 | +} |
| 112 | + |
| 113 | +} // namespace cuda |
| 114 | +} // namespace quadtrix |
| 115 | + |
| 116 | +#define QUADTRIX_CUDA_CHECK(expr) \ |
| 117 | + ::quadtrix::cuda::check_cuda((expr), #expr, __FILE__, __LINE__) |
| 118 | + |
| 119 | +#define QUADTRIX_CUDA_ABORT(expr) \ |
| 120 | + ::quadtrix::cuda::abort_on_cuda((expr), #expr, __FILE__, __LINE__) |
0 commit comments