CPPuddle
Loading...
Searching...
No Matches
cuda_underlying_allocators.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2024 Gregor Daiß
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef CUDA_UNDERLYING_ALLOCATORS_HPP
7#define CUDA_UNDERLYING_ALLOCATORS_HPP
8
9#include <cuda_runtime.h>
10#include <stdexcept>
11#include <string>
12
13namespace cppuddle {
14namespace memory_recycling {
15namespace detail {
17template <class T> struct cuda_pinned_allocator {
18 using value_type = T;
19 cuda_pinned_allocator() noexcept = default;
20 template <class U>
21 explicit cuda_pinned_allocator(cuda_pinned_allocator<U> const &) noexcept {}
22 T *allocate(std::size_t n) {
23 T *data;
24 cudaError_t error =
25 cudaMallocHost(reinterpret_cast<void **>(&data), n * sizeof(T));
26 if (error != cudaSuccess) {
27 std::string msg =
28 std::string(
29 "cuda_pinned_allocator failed due to cudaMallocHost failure : ") +
30 std::string(cudaGetErrorString(error));
31 throw std::runtime_error(msg);
32 }
33 return data;
34 }
35 void deallocate(T *p, std::size_t n) {
36 cudaError_t error = cudaFreeHost(p);
37 if (error != cudaSuccess) {
38 std::string msg =
39 std::string(
40 "cuda_pinned_allocator failed due to cudaFreeHost failure : ") +
41 std::string(cudaGetErrorString(error));
42 throw std::runtime_error(msg);
43 }
44 }
45};
46
47template <class T, class U>
48constexpr bool operator==(cuda_pinned_allocator<T> const &,
49 cuda_pinned_allocator<U> const &) noexcept {
50 return true;
51}
52template <class T, class U>
53constexpr bool operator!=(cuda_pinned_allocator<T> const &,
54 cuda_pinned_allocator<U> const &) noexcept {
55 return false;
56}
57
59template <class T> struct cuda_device_allocator {
60 using value_type = T;
61 cuda_device_allocator() noexcept = default;
62 template <class U>
63 explicit cuda_device_allocator(cuda_device_allocator<U> const &) noexcept {}
64 T *allocate(std::size_t n) {
65 T *data;
66 cudaError_t error = cudaMalloc(&data, n * sizeof(T));
67 if (error != cudaSuccess) {
68 std::string msg =
69 std::string(
70 "cuda_device_allocator failed due to cudaMalloc failure : ") +
71 std::string(cudaGetErrorString(error));
72 throw std::runtime_error(msg);
73 }
74 return data;
75 }
76 void deallocate(T *p, std::size_t n) {
77 cudaError_t error = cudaFree(p);
78 if (error != cudaSuccess) {
79 std::string msg =
80 std::string(
81 "cuda_device_allocator failed due to cudaFree failure : ") +
82 std::string(cudaGetErrorString(error));
83 throw std::runtime_error(msg);
84 }
85 }
86};
87template <class T, class U>
88constexpr bool operator==(cuda_device_allocator<T> const &,
89 cuda_device_allocator<U> const &) noexcept {
90 return true;
91}
92template <class T, class U>
93constexpr bool operator!=(cuda_device_allocator<T> const &,
94 cuda_device_allocator<U> const &) noexcept {
95 return false;
96}
97} // end namespace detail
98} // namespace memory_recycling
99} // end namespace cppuddle
100
101#endif
constexpr bool operator!=(recycle_allocator< T, Host_Allocator > const &, recycle_allocator< U, Host_Allocator > const &) noexcept
Definition buffer_management.hpp:830
constexpr bool operator==(recycle_allocator< T, Host_Allocator > const &, recycle_allocator< U, Host_Allocator > const &) noexcept
Definition buffer_management.hpp:821
Definition config.hpp:31
Underlying allocator for CUDA device memory.
Definition cuda_underlying_allocators.hpp:59
void deallocate(T *p, std::size_t n)
Definition cuda_underlying_allocators.hpp:76
T value_type
Definition cuda_underlying_allocators.hpp:60
T * allocate(std::size_t n)
Definition cuda_underlying_allocators.hpp:64
Underlying host allocator for CUDA pinned memory.
Definition cuda_underlying_allocators.hpp:17
T value_type
Definition cuda_underlying_allocators.hpp:18
T * allocate(std::size_t n)
Definition cuda_underlying_allocators.hpp:22
void deallocate(T *p, std::size_t n)
Definition cuda_underlying_allocators.hpp:35