CMS 3D CMS Logo

HostAllocator.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_CUDAUtilities_HostAllocator_h
2 #define HeterogeneousCore_CUDAUtilities_HostAllocator_h
3 
4 #include <memory>
5 #include <new>
6 #include <cuda_runtime.h>
7 
8 namespace cms {
9  namespace cuda {
10 
11  class bad_alloc : public std::bad_alloc {
12  public:
13  bad_alloc(cudaError_t error) noexcept : error_(error) {}
14 
15  const char* what() const noexcept override { return cudaGetErrorString(error_); }
16 
17  private:
18  cudaError_t error_;
19  };
20 
21  template <typename T, unsigned int FLAGS = cudaHostAllocDefault>
22  class HostAllocator {
23  public:
24  using value_type = T;
25 
26  template <typename U>
27  struct rebind {
29  };
30 
31  T* allocate(std::size_t n) const __attribute__((warn_unused_result)) __attribute__((malloc))
32  __attribute__((returns_nonnull)) {
33  void* ptr = nullptr;
34  cudaError_t status = cudaMallocHost(&ptr, n * sizeof(T), FLAGS);
35  if (status != cudaSuccess) {
36  throw bad_alloc(status);
37  }
38  if (ptr == nullptr) {
39  throw std::bad_alloc();
40  }
41  return static_cast<T*>(ptr);
42  }
43 
44  void deallocate(T* p, std::size_t n) const {
45  cudaError_t status = cudaFreeHost(p);
46  if (status != cudaSuccess) {
47  throw bad_alloc(status);
48  }
49  }
50  };
51 
52  } // namespace cuda
53 } // namespace cms
54 
55 #endif // HeterogeneousCore_CUDAUtilities_HostAllocator_h
cms::cuda::n
cudaStream_t T uint32_t const T *__restrict__ const uint32_t *__restrict__ uint32_t int cudaStream_t Func V int n
Definition: HistoContainer.h:124
cms::cuda::bad_alloc
Definition: HostAllocator.h:11
mps_update.status
status
Definition: mps_update.py:69
cms::cuda::bad_alloc::bad_alloc
bad_alloc(cudaError_t error) noexcept
Definition: HostAllocator.h:13
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
cms::cuda::HostAllocator::allocate
T * allocate(std::size_t n) const __attribute__((warn_unused_result)) __attribute__((malloc)) __attribute__((returns_nonnull))
Definition: HostAllocator.h:31
watchdog.const
const
Definition: watchdog.py:83
relativeConstraints.error
error
Definition: relativeConstraints.py:53
cms::cuda::HostAllocator::deallocate
void deallocate(T *p, std::size_t n) const
Definition: HostAllocator.h:44
cms::cuda::HostAllocator
Definition: HostAllocator.h:22
prod1Switch_cff.cuda
cuda
Definition: prod1Switch_cff.py:11
cms::cuda::bad_alloc::what
const char * what() const noexcept override
Definition: HostAllocator.h:15
cms::cuda::HostAllocator::value_type
T value_type
Definition: HostAllocator.h:24
cms::cuda::HostAllocator::rebind
Definition: HostAllocator.h:27
T
long double T
Definition: Basic3DVectorLD.h:48
cms::cuda::__attribute__
__attribute__((always_inline)) void countFromVector(Histo *__restrict__ h
cms::cuda::bad_alloc::error_
cudaError_t error_
Definition: HostAllocator.h:18
cms
Namespace of DDCMS conversion namespace.
Definition: ProducerAnalyzer.cc:21