CPPuddle
cuda_recycling_device_buffer.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_RECYCLING_BUFFER_HPP
7 #define CUDA_RECYCLING_BUFFER_HPP
8 
9 // import recycle_allocator_cuda_device
11 
15 
16 namespace cppuddle {
17 namespace memory_recycling {
18 
19 
22 template <typename T, std::enable_if_t<std::is_trivial<T>::value, int> = 0>
23 struct cuda_device_buffer {
24  recycle_allocator_cuda_device<T> allocator;
25  T *device_side_buffer;
26  size_t number_of_elements;
27 
28  cuda_device_buffer(const size_t number_of_elements, const size_t device_id = 0)
29  : allocator{device_id}, number_of_elements(number_of_elements) {
30  assert(device_id < max_number_gpus);
31  device_side_buffer =
32  allocator.allocate(number_of_elements);
33  }
34  ~cuda_device_buffer() {
35  allocator.deallocate(device_side_buffer, number_of_elements);
36  }
37  // not yet implemented
38  cuda_device_buffer(cuda_device_buffer const &other) = delete;
39  cuda_device_buffer operator=(cuda_device_buffer const &other) = delete;
40  cuda_device_buffer(cuda_device_buffer const &&other) = delete;
41  cuda_device_buffer operator=(cuda_device_buffer const &&other) = delete;
42 
43 };
44 
47 template <typename T, typename Host_Allocator, std::enable_if_t<std::is_trivial<T>::value, int> = 0>
48 struct cuda_aggregated_device_buffer {
49  T *device_side_buffer;
50  size_t number_of_elements;
51  cuda_aggregated_device_buffer(size_t number_of_elements, Host_Allocator &alloc)
52  : number_of_elements(number_of_elements), alloc(alloc) {
53  device_side_buffer =
54  alloc.allocate(number_of_elements);
55  }
56  ~cuda_aggregated_device_buffer() {
57  alloc.deallocate(device_side_buffer, number_of_elements);
58  }
59  // not yet implemented
60  cuda_aggregated_device_buffer(cuda_aggregated_device_buffer const &other) = delete;
61  cuda_aggregated_device_buffer operator=(cuda_aggregated_device_buffer const &other) = delete;
62  cuda_aggregated_device_buffer(cuda_aggregated_device_buffer const &&other) = delete;
63  cuda_aggregated_device_buffer operator=(cuda_aggregated_device_buffer const &&other) = delete;
64 
65 private:
66  Host_Allocator &alloc; // will stay valid for the entire aggregation region and hence
67  // for the entire lifetime of this buffer
68 };
69 
70 } // namespace memory_recycling
71 } // end namespace cppuddle
72 #endif