ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
cuda_mem_wrapper.h
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_HAMILT_MODULE_GINT_KERNEL_CUDA_MEM_WRAPPER_H
2#define ABACUS_SOURCE_HAMILT_MODULE_GINT_KERNEL_CUDA_MEM_WRAPPER_H
3
6#include <cuda_runtime.h>
7
8template <typename T>
10{
11 public:
12 CudaMemWrapper() = default;
13 CudaMemWrapper(const CudaMemWrapper &other) = delete;
14 CudaMemWrapper &operator=(const CudaMemWrapper &other) = delete;
16 {
17 this->device_ptr_ = other.device_ptr_;
18 this->host_ptr_ = other.host_ptr_;
19 this->size_ = other.size_;
20 this->malloc_host_ = other.malloc_host_;
21 this->stream_ = other.stream_;
22
23 other.device_ptr_ = nullptr;
24 other.host_ptr_ = nullptr;
25 other.size_ = 0;
26 other.malloc_host_ = false;
27 other.stream_ = 0;
28 }
29
31 {
32 if (this != &other)
33 {
34 this->device_ptr_ = other.device_ptr_;
35 this->host_ptr_ = other.host_ptr_;
36 this->size_ = other.size_;
37 this->malloc_host_ = other.malloc_host_;
38 this->stream_ = other.stream_;
39
40 other.device_ptr_ = nullptr;
41 other.host_ptr_ = nullptr;
42 other.size_ = 0;
43 other.malloc_host_ = false;
44 other.stream_ = 0;
45 }
46 return *this;
47 }
48
49 CudaMemWrapper(size_t size, cudaStream_t stream = 0, bool malloc_host = true)
50 {
51 size_ = size;
52 malloc_host_ = malloc_host;
53 stream_ = stream;
54
55 if (malloc_host)
56 {
57 CHECK_CUDA(cudaMallocHost((void **)&host_ptr_, size_ * sizeof(T)));
58 memset(host_ptr_, 0, size_ * sizeof(T));
59 }
60 else
61 {
62 host_ptr_ = nullptr;
63 }
64
65 CHECK_CUDA(cudaMalloc((void **)&device_ptr_, size_ * sizeof(T)));
66 CHECK_CUDA(cudaMemset(device_ptr_, 0, size_ * sizeof(T)));
67 }
68
70
71 void copy_host_to_device_sync(size_t size)
72 {
73 if (host_ptr_ == nullptr)
74 {
75 ModuleBase::WARNING_QUIT("cuda_mem_wrapper", "Host pointer is null, cannot copy to device.");
76 }
77 CHECK_CUDA(cudaMemcpy(device_ptr_, host_ptr_, size * sizeof(T), cudaMemcpyHostToDevice));
78 }
79
81
82 void copy_host_to_device_async(size_t size)
83 {
84 if (host_ptr_ == nullptr)
85 {
86 ModuleBase::WARNING_QUIT("cuda_mem_wrapper", "Host pointer is null, cannot copy to device.");
87 }
88 CHECK_CUDA(cudaMemcpyAsync(device_ptr_, host_ptr_, size * sizeof(T), cudaMemcpyHostToDevice, stream_));
89 }
90
92
93 void copy_device_to_host_sync(size_t size)
94 {
95 if (host_ptr_ == nullptr)
96 {
97 ModuleBase::WARNING_QUIT("cuda_mem_wrapper", "Host pointer is null, cannot copy to host.");
98 }
99 CHECK_CUDA(cudaMemcpy(host_ptr_, device_ptr_, size * sizeof(T), cudaMemcpyDeviceToHost));
100 }
101
103
105 {
106 if (host_ptr_ == nullptr)
107 {
108 ModuleBase::WARNING_QUIT("cuda_mem_wrapper", "Host pointer is null, cannot copy to host.");
109 }
110 CHECK_CUDA(cudaMemcpyAsync(host_ptr_, device_ptr_, size * sizeof(T), cudaMemcpyDeviceToHost, stream_));
111 }
112
114
115 void memset_device_sync(const size_t size, const int value = 0)
116 {
117 CHECK_CUDA(cudaMemset(device_ptr_, value, size * sizeof(T)));
118 }
119
120 void memset_device_sync(const int value = 0) { memset_device_sync(size_, value); }
121
122 void memset_device_async(const size_t size, const int value = 0)
123 {
124 CHECK_CUDA(cudaMemsetAsync(device_ptr_, value, size * sizeof(T), stream_));
125 }
126
127 void memset_device_async(const int value = 0) { memset_device_async(size_, value); }
128
129 void memset_host(const size_t size, const int value = 0)
130 {
131 if (host_ptr_ == nullptr)
132 {
133 ModuleBase::WARNING_QUIT("cuda_mem_wrapper", "Host pointer is null, cannot memset host.");
134 }
135 CHECK_CUDA(cudaMemset(host_ptr_, value, size * sizeof(T)));
136 }
137
138 void memset_host(const int value = 0) { memset_host(size_, value); }
139
140 void free()
141 {
142 CHECK_CUDA(cudaFree(device_ptr_));
143 CHECK_CUDA(cudaFreeHost(host_ptr_));
144 }
145
147 T *get_host_ptr() { return host_ptr_; }
148 const T *get_device_ptr() const { return device_ptr_; }
149 const T *get_host_ptr() const { return host_ptr_; }
150 size_t get_size() const { return size_; }
151
152 private:
153 T *device_ptr_ = nullptr;
154 T *host_ptr_ = nullptr;
155 size_t size_ = 0;
156 bool malloc_host_ = false;
157 cudaStream_t stream_ = 0;
158};
159
160#endif // ABACUS_SOURCE_HAMILT_MODULE_GINT_KERNEL_CUDA_MEM_WRAPPER_H
T * get_host_ptr()
Definition cuda_mem_wrapper.h:147
void free()
Definition cuda_mem_wrapper.h:140
T * get_device_ptr()
Definition cuda_mem_wrapper.h:146
CudaMemWrapper & operator=(CudaMemWrapper &&other) noexcept
Definition cuda_mem_wrapper.h:30
void memset_device_sync(const int value=0)
Definition cuda_mem_wrapper.h:120
CudaMemWrapper(const CudaMemWrapper &other)=delete
cudaStream_t stream_
Definition cuda_mem_wrapper.h:157
void copy_device_to_host_sync(size_t size)
Definition cuda_mem_wrapper.h:93
T * host_ptr_
Definition cuda_mem_wrapper.h:154
void memset_device_sync(const size_t size, const int value=0)
Definition cuda_mem_wrapper.h:115
void copy_host_to_device_async(size_t size)
Definition cuda_mem_wrapper.h:82
size_t get_size() const
Definition cuda_mem_wrapper.h:150
CudaMemWrapper()=default
const T * get_host_ptr() const
Definition cuda_mem_wrapper.h:149
~CudaMemWrapper()
Definition cuda_mem_wrapper.h:69
void copy_device_to_host_async()
Definition cuda_mem_wrapper.h:113
void memset_device_async(const size_t size, const int value=0)
Definition cuda_mem_wrapper.h:122
void copy_device_to_host_sync()
Definition cuda_mem_wrapper.h:102
CudaMemWrapper(size_t size, cudaStream_t stream=0, bool malloc_host=true)
Definition cuda_mem_wrapper.h:49
void copy_host_to_device_async()
Definition cuda_mem_wrapper.h:91
bool malloc_host_
Definition cuda_mem_wrapper.h:156
size_t size_
Definition cuda_mem_wrapper.h:155
void memset_host(const int value=0)
Definition cuda_mem_wrapper.h:138
T * device_ptr_
Definition cuda_mem_wrapper.h:153
void copy_host_to_device_sync(size_t size)
Definition cuda_mem_wrapper.h:71
CudaMemWrapper & operator=(const CudaMemWrapper &other)=delete
void copy_host_to_device_sync()
Definition cuda_mem_wrapper.h:80
void memset_host(const size_t size, const int value=0)
Definition cuda_mem_wrapper.h:129
void copy_device_to_host_async(size_t size)
Definition cuda_mem_wrapper.h:104
CudaMemWrapper(CudaMemWrapper &&other) noexcept
Definition cuda_mem_wrapper.h:15
const T * get_device_ptr() const
Definition cuda_mem_wrapper.h:148
void memset_device_async(const int value=0)
Definition cuda_mem_wrapper.h:127
#define T
Definition exp.cpp:237
void WARNING_QUIT(const std::string &, const std::string &)
Combine the functions of WARNING and QUIT.
Definition test_delley.cpp:15