30#include <cuda_runtime.h>
35 [[nodiscard]]
size_t total_size()
const {
return unit *
size; }
42 explicit cuda_ptr(
const size_t in_unit = 0,
const int in_size = 0)
45 if(total_size() > 0) cudaMalloc(&ptr, total_size());
65 template<
typename T =
int> [[nodiscard]] T*
get(
const unsigned long long offset = 0)
const {
return static_cast<T*
>(ptr) + offset; }
67 auto copy_from(
const void* src,
const cudaStream_t s)
const { cudaMemcpyAsync(ptr, src, total_size(), cudaMemcpyHostToDevice, s); }
69 auto copy_to(
void* dest,
const cudaStream_t s)
const {
70 cudaMemcpyAsync(dest, ptr, total_size(), cudaMemcpyDeviceToHost, s);
71 cudaStreamSynchronize(s);
Definition cuda_ptr.hpp:32
cuda_ptr(const cuda_ptr &other)
Definition cuda_ptr.hpp:47
cuda_ptr(const size_t in_unit=0, const int in_size=0)
Definition cuda_ptr.hpp:42
cuda_ptr(cuda_ptr &&)=delete
size_t unit
Definition cuda_ptr.hpp:38
cuda_ptr & operator=(const cuda_ptr &)=delete
auto copy_from(const void *src, const cudaStream_t s) const
Definition cuda_ptr.hpp:67
T * get(const unsigned long long offset=0) const
Definition cuda_ptr.hpp:65
~cuda_ptr()
Definition cuda_ptr.hpp:63
int size
Definition cuda_ptr.hpp:40
cuda_ptr & operator=(cuda_ptr &&other) noexcept
Definition cuda_ptr.hpp:51
auto copy_to(void *dest, const cudaStream_t s) const
Definition cuda_ptr.hpp:69