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 
9 
10 namespace cms {
11  namespace cuda {
12 
13  class bad_alloc : public std::bad_alloc {
14  public:
15  bad_alloc(cudaError_t error) noexcept : error_(error) {}
16 
17  const char* what() const noexcept override { return cudaGetErrorString(error_); }
18 
19  private:
20  cudaError_t error_;
21  };
22 
23  template <typename T, unsigned int FLAGS = cudaHostAllocDefault>
24  class HostAllocator {
25  public:
26  using value_type = T;
27 
28  template <typename U>
29  struct rebind {
31  };
32 
33  CMS_THREAD_SAFE T* allocate(std::size_t n) const __attribute__((warn_unused_result)) __attribute__((malloc))
34  __attribute__((returns_nonnull)) {
35  void* ptr = nullptr;
36  cudaError_t status = cudaMallocHost(&ptr, n * sizeof(T), FLAGS);
37  if (status != cudaSuccess) {
38  throw bad_alloc(status);
39  }
40  if (ptr == nullptr) {
41  throw std::bad_alloc();
42  }
43  return static_cast<T*>(ptr);
44  }
45 
46  void deallocate(T* p, std::size_t n) const {
47  cudaError_t status = cudaFreeHost(p);
48  if (status != cudaSuccess) {
49  throw bad_alloc(status);
50  }
51  }
52  };
53 
54  } // namespace cuda
55 } // namespace cms
56 
57 #endif // HeterogeneousCore_CUDAUtilities_HostAllocator_h
cms::cuda::bad_alloc
Definition: HostAllocator.h:13
mps_update.status
status
Definition: mps_update.py:69
cms::cuda::bad_alloc::bad_alloc
bad_alloc(cudaError_t error) noexcept
Definition: HostAllocator.h:15
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:33
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:46
cms::cuda::HostAllocator
Definition: HostAllocator.h:24
CMS_THREAD_SAFE
#define CMS_THREAD_SAFE
Definition: thread_safety_macros.h:4
cms::cuda::bad_alloc::what
const char * what() const noexcept override
Definition: HostAllocator.h:17
cms::cuda::HostAllocator::value_type
T value_type
Definition: HostAllocator.h:26
cms::cuda::n
cudaStream_t T uint32_t const T *__restrict__ const uint32_t *__restrict__ uint32_t int cudaStream_t Func __host__ __device__ V int n
Definition: HistoContainer.h:124
thread_safety_macros.h
cms::cuda::HostAllocator::rebind
Definition: HostAllocator.h:29
T
long double T
Definition: Basic3DVectorLD.h:48
ecalDigis_cff.cuda
cuda
Definition: ecalDigis_cff.py:35
cms::cuda::__attribute__
__attribute__((always_inline)) void countFromVector(Histo *__restrict__ h
cms::cuda::bad_alloc::error_
cudaError_t error_
Definition: HostAllocator.h:20
cms
Namespace of DDCMS conversion namespace.
Definition: ProducerAnalyzer.cc:21