Skip to content

Commit 71da564

Browse files
committed
show cuda error string
1 parent 231acd3 commit 71da564

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

include/ttl/bits/fake_cuda_runtime.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <cstdio>
23
#include <cstring>
34
#include <map>
45
#include <stdexcept>
@@ -12,7 +13,7 @@ constexpr const cudaMemcpyKind cudaMemcpyHostToDevice = 1;
1213
constexpr const cudaMemcpyKind cudaMemcpyDeviceToHost = 2;
1314
constexpr const cudaMemcpyKind cudaMemcpyDeviceToDevice = 3;
1415

15-
class fake_device
16+
class fake_cuda_device
1617
{
1718
std::map<const void *, size_t> _allocs;
1819

@@ -33,7 +34,9 @@ class fake_device
3334
}
3435

3536
public:
36-
~fake_device() { check_leak(); }
37+
fake_cuda_device() { std::printf("using fake_cuda_device!\n"); }
38+
39+
~fake_cuda_device() { check_leak(); }
3740

3841
void *alloc(size_t size)
3942
{
@@ -68,7 +71,7 @@ class fake_device
6871
}
6972
};
7073

71-
fake_device fake_cuda;
74+
fake_cuda_device fake_cuda;
7275

7376
cudaError_t cudaMalloc(void **ptr, size_t count)
7477
{
@@ -88,3 +91,8 @@ cudaError_t cudaMemcpy(void *dst, const void *src, size_t size,
8891
fake_cuda.memcpy(dst, src, size, dir);
8992
return cudaSuccess;
9093
}
94+
95+
std::string cudaGetErrorString(const cudaError_t err)
96+
{
97+
return "fake_cudaError_t(" + std::to_string(static_cast<int>(err)) + ")";
98+
}

include/ttl/bits/std_cuda_allocator.hpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <cstddef>
33
#include <stdexcept>
4+
#include <string>
45

56
#include <ttl/bits/std_cuda_runtime.hpp>
67
#include <ttl/bits/std_device.hpp>
@@ -10,6 +11,23 @@ namespace ttl
1011
{
1112
namespace internal
1213
{
14+
class std_cuda_error_checker_t
15+
{
16+
const std::string func_name_;
17+
18+
public:
19+
std_cuda_error_checker_t(const char *func_name) : func_name_(func_name) {}
20+
21+
void operator<<(const cudaError_t err) const
22+
{
23+
if (err != cudaSuccess) {
24+
throw std::runtime_error(func_name_ + " failed with: " +
25+
std::to_string(static_cast<int>(err)) +
26+
": " + cudaGetErrorString(err));
27+
}
28+
}
29+
}; // namespace ttl
30+
1331
struct cuda_copier {
1432
static constexpr auto h2d = cudaMemcpyHostToDevice;
1533
static constexpr auto d2h = cudaMemcpyDeviceToHost;
@@ -18,10 +36,8 @@ struct cuda_copier {
1836
template <cudaMemcpyKind dir>
1937
static void copy(void *dst, const void *src, size_t size)
2038
{
21-
const cudaError_t err = cudaMemcpy(dst, src, size, dir);
22-
if (err != cudaSuccess) {
23-
throw std::runtime_error("cudaMemcpy failed");
24-
}
39+
static std_cuda_error_checker_t check("cudaMemcpy");
40+
check << cudaMemcpy(dst, src, size, dir);
2541
}
2642
};
2743

@@ -54,10 +70,8 @@ class basic_allocator<R, cuda_memory>
5470
void *deviceMem;
5571
// cudaMalloc<R>(&deviceMem, count);
5672
// https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html#group__CUDART__MEMORY
57-
const cudaError_t err = cudaMalloc(&deviceMem, count * sizeof(R));
58-
if (err != cudaSuccess) {
59-
throw std::runtime_error("cudaMalloc failed");
60-
}
73+
static std_cuda_error_checker_t check("cudaMalloc");
74+
check << cudaMalloc(&deviceMem, count * sizeof(R));
6175
return reinterpret_cast<R *>(deviceMem);
6276
}
6377
};
@@ -68,8 +82,8 @@ class basic_deallocator<R, cuda_memory>
6882
public:
6983
void operator()(R *data)
7084
{
71-
const cudaError_t err = cudaFree(data);
72-
if (err != cudaSuccess) { throw std::runtime_error("cudaFree failed"); }
85+
static std_cuda_error_checker_t check("cudaFree");
86+
check << cudaFree(data);
7387
}
7488
};
7589
} // namespace internal

0 commit comments

Comments
 (0)