Skip to content

Commit e389522

Browse files
committed
better badalloc
1 parent 2c31785 commit e389522

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

slides/moderncuda/better_cuda.cuh

+13-15
Original file line numberDiff line numberDiff line change
@@ -410,43 +410,41 @@ struct CudaAllocator : private Arena {
410410

411411
T *allocate(size_t size) {
412412
void *ptr = nullptr;
413-
if (size >= std::numeric_limits<size_t>::max() / sizeof(T))
414-
[[unlikely]] {
413+
if (sizeof(T) <= 1 || size > std::numeric_limits<size_t>::max() /
414+
sizeof(T)) [[unlikely]] {
415415
throw std::bad_array_new_length();
416416
}
417417
cudaError_t res = Arena::doMalloc(&ptr, size * sizeof(T));
418-
if (res != cudaSuccess) [[unlikely]] {
418+
if (res == cudaErrorMemoryAllocation) [[unlikely]] {
419419
throw std::bad_alloc();
420420
}
421+
CHECK_CUDA(("Arena::doMalloc", res));
421422
return static_cast<T *>(ptr);
422423
}
423424

424-
void deallocate(T *ptr, size_t size = 0) noexcept {
425-
Arena::doFree(ptr);
425+
void deallocate(T *ptr, size_t size = 0) {
426+
CHECK_CUDA(Arena::doFree(ptr));
426427
}
427428

428429
template <class... Args>
429430
static constexpr std::enable_if_t<sizeof...(Args)>
430-
construct(T *p, Args &&...args) noexcept(
431-
noexcept(::new(p) T(std::forward<Args>(args)...))) {
432-
::new ((void *)p) T(std::forward<Args>(args)...);
431+
construct(T *p, Args &&...args) noexcept(noexcept(
432+
::new(static_cast<void *>(p)) T(std::forward<Args>(args)...))) {
433+
::new (static_cast<void *>(p)) T(std::forward<Args>(args)...);
433434
}
434435

435-
static constexpr void construct(T *p) noexcept(noexcept(::new(p) T)) {
436-
::new ((void *)p) T;
436+
static constexpr void
437+
construct(T *p) noexcept(noexcept(::new(static_cast<void *>(p)) T)) {
438+
::new (static_cast<void *>(p)) T;
437439
}
438440

439441
static constexpr void destroy(T *p) noexcept(noexcept(p->~T())) {
440442
p->~T();
441443
}
442444

443-
template <class U> // cihou shabi wendous
445+
template <class U>
444446
constexpr CudaAllocator(CudaAllocator<U> const &other) noexcept {}
445447

446-
constexpr bool operator==(CudaAllocator<T> const &other) const noexcept {
447-
return true;
448-
}
449-
450448
template <class U>
451449
constexpr bool operator==(CudaAllocator<U> const &other) const noexcept {
452450
return true;

slides/moderncuda/cloth.cu renamed to slides/moderncuda/example.cu

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ using namespace cupp;
55
/*__host__*/ void host_func() {
66
}
77

8-
/*__host__ __device__*/ constexpr void constexpr_func() {
9-
}
10-
118
__device__ void device_func() {
129
}
1310

1411
__host__ __device__ void host_device_func() {
1512
}
1613

14+
constexpr void constexpr_func() {
15+
}
16+
1717
__global__ void kernel() {
1818
device_func();
1919
host_device_func();

0 commit comments

Comments
 (0)