graph
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cuda_zero_copy.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdlib>
4 #include <new>
5 
6 #include <cuda_runtime.h>
7 
8 template <class T>
10 {
11  typedef T value_type;
12  CUDAZeroCopyAllocator() = default;
13  template <class U>
14  constexpr CUDAZeroCopyAllocator(const CUDAZeroCopyAllocator<U> &) noexcept {}
15  T *allocate(std::size_t n)
16  {
17  if (n > std::size_t(-1) / sizeof(T))
18  throw std::bad_alloc();
19  T *p;
20  cudaError_t err = cudaHostAlloc(&p, n * sizeof(T), cudaHostAllocMapped | cudaHostAllocPortable);
21  if (err != cudaSuccess)
22  {
23  throw std::bad_alloc();
24  }
25  return p;
26  }
27  void deallocate(T *p, std::size_t) noexcept { cudaFreeHost(p); }
28 };
29 template <class T, class U>
30 bool operator==(const CUDAZeroCopyAllocator<T> &, const CUDAZeroCopyAllocator<U> &) { return true; }
31 template <class T, class U>
32 bool operator!=(const CUDAZeroCopyAllocator<T> &, const CUDAZeroCopyAllocator<U> &) { return false; }
bool operator!=(const CUDAZeroCopyAllocator< T > &, const CUDAZeroCopyAllocator< U > &)
Definition: cuda_zero_copy.hpp:32
T * allocate(std::size_t n)
Definition: cuda_zero_copy.hpp:15
T value_type
Definition: cuda_zero_copy.hpp:11
bool operator==(const CUDAZeroCopyAllocator< T > &, const CUDAZeroCopyAllocator< U > &)
Definition: cuda_zero_copy.hpp:30
CUDAZeroCopyAllocator()=default
Definition: cuda_zero_copy.hpp:9
void deallocate(T *p, std::size_t) noexcept
Definition: cuda_zero_copy.hpp:27
constexpr CUDAZeroCopyAllocator(const CUDAZeroCopyAllocator< U > &) noexcept
Definition: cuda_zero_copy.hpp:14