CPPuddle
Loading...
Searching...
No Matches
cuda_underlying_allocators.hpp
Go to the documentation of this file.
1// LANL Copyright Notice:
2//
3//© 2026. Triad National Security, LLC. All rights reserved.
4//
5// This program was produced under U.S. Government contract 89233218CNA000001
6// for Los Alamos National Laboratory (LANL), which is operated by Triad
7// National Security, LLC for the U.S. Department of Energy/National Nuclear
8// Security Administration. All rights in the program are reserved by Triad
9// National Security, LLC, and the U.S. Department of Energy/National Nuclear
10// Security Administration. The Government is granted for itself and others
11// acting on its behalf a nonexclusive, paid-up, irrevocable worldwide license
12// in this material to reproduce, prepare. derivative works, distribute copies
13// to the public, perform publicly and display publicly, and to permit others
14// to do so.
15//
16//
17// Distributed under the Boost Software License, Version 1.0. (See accompanying
18// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
19
20#ifndef CUDA_UNDERLYING_ALLOCATORS_HPP
21#define CUDA_UNDERLYING_ALLOCATORS_HPP
22
23#include <cuda_runtime.h>
24#include <stdexcept>
25#include <string>
26#include <type_traits>
27
28namespace cppuddle {
29namespace memory_recycling {
30namespace detail {
32template <class T> struct cuda_pinned_allocator {
33 using value_type = T;
34 cuda_pinned_allocator() noexcept = default;
35 template <class U>
36 explicit cuda_pinned_allocator(cuda_pinned_allocator<U> const &) noexcept {}
37 T *allocate(std::size_t n) {
38 T *data;
39 cudaError_t error =
40 cudaMallocHost(reinterpret_cast<void **>(&data), n * sizeof(T));
41 if (error != cudaSuccess) {
42 std::string msg =
43 std::string(
44 "cuda_pinned_allocator failed due to cudaMallocHost failure : ") +
45 std::string(cudaGetErrorString(error));
46 throw std::runtime_error(msg);
47 }
48 return data;
49 }
50 void deallocate(T *p, std::size_t n) {
51 cudaError_t error = cudaFreeHost(p);
52 if (error != cudaSuccess) {
53 std::string msg =
54 std::string(
55 "cuda_pinned_allocator failed due to cudaFreeHost failure : ") +
56 std::string(cudaGetErrorString(error));
57 throw std::runtime_error(msg);
58 }
59 }
60};
61
62template <class T, class U>
63constexpr bool operator==(cuda_pinned_allocator<T> const &,
64 cuda_pinned_allocator<U> const &) noexcept {
65 return true;
66}
67template <class T, class U>
68constexpr bool operator!=(cuda_pinned_allocator<T> const &,
69 cuda_pinned_allocator<U> const &) noexcept {
70 return false;
71}
72
74template <class T> struct cuda_device_allocator {
75 using value_type = T;
76 cuda_device_allocator() noexcept = default;
77 template <class U>
78 explicit cuda_device_allocator(cuda_device_allocator<U> const &) noexcept {}
79 T *allocate(std::size_t n) {
80 T *data;
81 cudaError_t error = cudaMalloc(&data, n * sizeof(T));
82 if (error != cudaSuccess) {
83 std::string msg =
84 std::string(
85 "cuda_device_allocator failed due to cudaMalloc failure : ") +
86 std::string(cudaGetErrorString(error));
87 throw std::runtime_error(msg);
88 }
89 return data;
90 }
91 void deallocate(T *p, std::size_t n) {
92 cudaError_t error = cudaFree(p);
93 if (error != cudaSuccess) {
94 std::string msg =
95 std::string(
96 "cuda_device_allocator failed due to cudaFree failure : ") +
97 std::string(cudaGetErrorString(error));
98 throw std::runtime_error(msg);
99 }
100 }
101};
102template <class T, class U>
104 cuda_device_allocator<U> const &) noexcept {
105 return true;
106}
107template <class T, class U>
109 cuda_device_allocator<U> const &) noexcept {
110 return false;
111}
112
113template <class T> struct cuda_managed_memory_allocator {
114 using value_type = T;
115 cuda_managed_memory_allocator() noexcept = default;
116 template <class U>
118 T *allocate(std::size_t n) {
119 T *data;
120 cudaError_t error = cudaMallocManaged(&data, n * sizeof(T));
121 if (error != cudaSuccess) {
122 std::string msg =
123 std::string(
124 "cuda_managed_memory_allocator failed due to cudaMallocManaged failure : ") +
125 std::string(cudaGetErrorString(error));
126 throw std::runtime_error(msg);
127 }
128 return data;
129 }
130 void deallocate(T *p, std::size_t n) {
131 cudaError_t error = cudaFree(p);
132 if (error != cudaSuccess) {
133 std::string msg =
134 std::string(
135 "cuda_managed_memory_allocator failed due to cudaFree failure : ") +
136 std::string(cudaGetErrorString(error));
137 throw std::runtime_error(msg);
138 }
139 }
140};
141template <class T, class U>
143 cuda_managed_memory_allocator<U> const &) noexcept {
144 return true;
145}
146template <class T, class U>
148 cuda_managed_memory_allocator<U> const &) noexcept {
149 return false;
150}
151} // end namespace detail
152} // namespace memory_recycling
153} // end namespace cppuddle
154
155#endif
Definition buffer_management.hpp:79
constexpr bool operator!=(recycle_allocator< T, Host_Allocator > const &, recycle_allocator< U, Host_Allocator > const &) noexcept
Definition buffer_management.hpp:855
constexpr bool operator==(recycle_allocator< T, Host_Allocator > const &, recycle_allocator< U, Host_Allocator > const &) noexcept
Definition buffer_management.hpp:846
CPPuddle namespace containing the memory recycling functionality.
Definition aligned_recycling_allocators.hpp:17
Definition config.hpp:31
Underlying allocator for CUDA device memory.
Definition cuda_underlying_allocators.hpp:74
void deallocate(T *p, std::size_t n)
Definition cuda_underlying_allocators.hpp:91
T value_type
Definition cuda_underlying_allocators.hpp:75
T * allocate(std::size_t n)
Definition cuda_underlying_allocators.hpp:79
Underlying allocator for CUDA managed memory.
Definition cuda_underlying_allocators.hpp:113
T value_type
Definition cuda_underlying_allocators.hpp:114
void deallocate(T *p, std::size_t n)
Definition cuda_underlying_allocators.hpp:130
T * allocate(std::size_t n)
Definition cuda_underlying_allocators.hpp:118
Underlying host allocator for CUDA pinned memory.
Definition cuda_underlying_allocators.hpp:32
T value_type
Definition cuda_underlying_allocators.hpp:33
T * allocate(std::size_t n)
Definition cuda_underlying_allocators.hpp:37
void deallocate(T *p, std::size_t n)
Definition cuda_underlying_allocators.hpp:50