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
T * allocate(std::size_t n) const __attribute__((warn_unused_result)) __attribute__((malloc)) __attribute__((returns_nonnull))
Definition: HostAllocator.h:33
void deallocate(T *p, std::size_t n) const
Definition: HostAllocator.h:46
__attribute__((always_inline)) void countFromVector(Histo *__restrict__ h
#define CMS_THREAD_SAFE
Namespace of DDCMS conversion namespace.
const char * what() const noexcept override
Definition: HostAllocator.h:17
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t Func __host__ __device__ V int n
bad_alloc(cudaError_t error) noexcept
Definition: HostAllocator.h:15
long double T