FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
memory_aux_cuda.cu
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#include <kernel/util/memory_aux.hpp>
7#include <kernel/util/cuda_helper.cuh>
8#include <kernel/util/cuda_util.hpp>
9
10namespace FEAT
11{
12 namespace Memory
13 {
14 void* alloc_main(std::size_t bytes)
15 {
16 void* ptr = nullptr;
17 if(cudaSuccess != ::cudaMallocHost(&ptr, bytes))
18 return nullptr;
19 return ptr;
20 }
21
22 void free_main(void* ptr)
23 {
24 if(ptr == nullptr)
25 return;
26 cudaCheck(::cudaFreeHost(ptr), "Memory::free_main: cudaFreeHost");
27 }
28
29 void* alloc_cuda(std::size_t bytes)
30 {
31 void* ptr = nullptr;
32 if(bytes == 0u)
33 return ptr;
34 cudaCheck(::cudaMalloc(&ptr, bytes), "Memory::alloc_cuda: cudaMalloc");
35 return ptr;
36 }
37
38 void* alloc_cuda_async(std::size_t bytes, [[maybe_unused]] const Util::StreamWrapper& stream)
39 {
40 void* ptr = nullptr;
41 cudaError_t st;
42 #ifdef CUDA_ASYNC_SUPPORTED
43 const cudaStream_t s_stream = reinterpret_cast<cudaStream_t>(stream.stream);
44 st = Util::cuda_get_stream_ordered_alloc_supported() ? ::cudaMallocAsync(&ptr, bytes, s_stream) : ::cudaMalloc(&ptr, bytes);
45 #else
46 st = ::cudaMalloc(&ptr, bytes);
47 #endif
48 cudaCheck(st, "Memory::alloc_cuda_async");
49 return ptr;
50 }
51
52 void free_cuda(void* ptr)
53 {
54 if(ptr == nullptr)
55 return;
56 cudaCheck(::cudaFree(ptr), "Memory::free_cuda: cudaFree");
57 }
58
59 void free_cuda_async(void* ptr, [[maybe_unused]] const Util::StreamWrapper& stream)
60 {
61 if(ptr == nullptr)
62 return;
63 cudaError_t st;
64 #ifdef CUDA_ASYNC_SUPPORTED
65 const cudaStream_t s_stream = reinterpret_cast<cudaStream_t>(stream.stream);
66 st = Util::cuda_get_stream_ordered_alloc_supported() ? cudaFreeAsync(ptr, s_stream) : cudaFree(ptr);
67 #else
68 st = ::cudaFree(ptr);
69 #endif
70 cudaCheck(st, "Memory::free_cuda_async");
71 }
72
73 void memset_cuda(void* dst, int value, std::size_t bytes, const Util::StreamWrapper& stream)
74 {
75 if(bytes <= std::size_t(0))
76 return;
77
78 XASSERTM(dst != nullptr, "cuda device memset destination is nullptr");
79
80 if constexpr(async_operations)
81 {
82 const cudaStream_t s_stream = reinterpret_cast<cudaStream_t>(stream.stream);
83 cudaCheck(cudaMemsetAsync(dst, value, bytes, s_stream), "Memory::memset_cuda async");
84 }
85 else
86 {
87 cudaCheck(cudaMemset(dst, value, bytes), "Memory::memset_cuda");
88 }
89 }
90
91 void memcopy_cuda(void* dst, const void* src, std::size_t bytes, const Util::StreamWrapper& stream)
92 {
93 if(bytes <= std::size_t(0))
94 return;
95
96 XASSERTM(dst != nullptr, "cuda device-to-device memcpy destination is nullptr");
97 XASSERTM(src != nullptr, "cuda device-to-device memcpy source is nullptr");
98
99 if constexpr(async_operations)
100 {
101 const cudaStream_t s_stream = reinterpret_cast<cudaStream_t>(stream.stream);
102 cudaCheck(cudaMemcpyAsync(dst, src, bytes, cudaMemcpyDeviceToDevice, s_stream), "Memory::memcopy_cuda async");
103 }
104 else
105 {
106 cudaCheck(cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToDevice), "Memory::memcopy_cuda");
107 }
108 }
109
110 void memcopy_cuda_to_main(void* dst, const void* src, std::size_t bytes)
111 {
112 if(bytes <= std::size_t(0))
113 return;
114
115 XASSERTM(dst != nullptr, "cuda device-to-host memcpy destination is nullptr");
116 XASSERTM(src != nullptr, "cuda device-to-host memcpy source is nullptr");
117
118 cudaCheck(cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToHost), "Memory::memcopy_cuda_to_main");
119 }
120
121 void memcopy_cuda_to_main_async(void* dst, const void* src, std::size_t bytes, const Util::StreamWrapper& stream)
122 {
123 if(bytes <= std::size_t(0))
124 return;
125
126 XASSERTM(dst != nullptr, "cuda device-to-host memcpy destination is nullptr");
127 XASSERTM(src != nullptr, "cuda device-to-host memcpy source is nullptr");
128
129 if constexpr(async_operations)
130 {
131 const cudaStream_t s_stream = reinterpret_cast<cudaStream_t>(stream.stream);
132 cudaCheck(cudaMemcpyAsync(dst, src, bytes, cudaMemcpyDeviceToHost, s_stream), "Memory::memcopy_cuda_to_main_async");
133 }
134 else
135 cudaCheck(cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToHost), "Memory::memcopy_cuda_to_main_async");
136 }
137
138 void memcopy_main_to_cuda(void* dst, const void* src, std::size_t bytes, const Util::StreamWrapper& stream)
139 {
140 if(bytes <= std::size_t(0))
141 return;
142
143 XASSERTM(dst != nullptr, "cuda host-to-device memcpy destination is nullptr");
144 XASSERTM(src != nullptr, "cuda host-to-device memcpy source is nullptr");
145
146 // should be okay to do on main stream, since we can not delete source memory without sync call
147 if constexpr(async_operations)
148 {
149 const cudaStream_t s_stream = reinterpret_cast<cudaStream_t>(stream.stream);
150 cudaCheck(cudaMemcpyAsync(dst, src, bytes, cudaMemcpyHostToDevice, s_stream), "Memory::memcopy_main_to_cuda async");
151 }
152 else
153 cudaCheck(cudaMemcpy(dst, src, bytes, cudaMemcpyHostToDevice), "Memory::memcopy_main_to_cuda");
154 }
155
156 template <typename Tdst_, typename Tsrc_>
157 __global__ void convert_cuda_impl(Tdst_* dst, const Tsrc_* src, const Index count)
158 {
159 // grid strided for loop
160 for(Index i = threadIdx.x + blockDim.x * blockIdx.x; i < count; i += blockDim.x * gridDim.x)
161 dst[i] = Tdst_(src[i]);
162 }
163
164 template<typename Tdst_, typename Tsrc_>
165 void convert_cuda(Tdst_* dst, const Tsrc_* src, const Index count)
166 {
167 if(count <= Index(0))
168 return;
169
170 dim3 block(static_cast<unsigned int>(Util::cuda_blocksize_misc));
171 dim3 grid((static_cast<unsigned int>(count) + block.x - 1u) / block.x); // round up to next integer
172 convert_cuda_impl<<<grid, block>>>(dst, src, count);
173
174#ifdef DEBUG
175 cudaError_t last_error(cudaGetLastError());
176 if (cudaSuccess != last_error)
177 {
178 std::string strerr("Memory::convert_cuda failed!\n");
179 strerr.append(cudaGetErrorString(last_error));
180 XABORTM(strerr.c_str());
181 }
182#endif
183 }
184
185#ifdef FEAT_HAVE_HALFMATH
186 template void convert_cuda<Half, float>(Half *, const float *, const Index);
187 template void convert_cuda<Half, double>(Half *, const double *, const Index);
188 template void convert_cuda<float, Half>(float *, const Half *, const Index);
189 template void convert_cuda<double, Half>(double *, const Half *, const Index);
190#endif
191
192 // Python code for generating the mumbo jumbo below:
193 // tt = [
194 // "float",
195 // "double",
196 // "signed int",
197 // "signed long",
198 // "signed long long",
199 // "unsigned int",
200 // "unsigned long",
201 // "unsigned long long"
202 // ]
203 //
204 // for t in tt:
205 // for s in tt:
206 // if t != s:
207 // print("template void convert_cuda<" + t + ", " + s + ">(" + t + "*, const " + s + "*, const Index);")
208
209 template void convert_cuda<float, double>(float*, const double*, const Index);
210 template void convert_cuda<float, signed int>(float*, const signed int*, const Index);
211 template void convert_cuda<float, signed long>(float*, const signed long*, const Index);
212 template void convert_cuda<float, signed long long>(float*, const signed long long*, const Index);
213 template void convert_cuda<float, unsigned int>(float*, const unsigned int*, const Index);
214 template void convert_cuda<float, unsigned long>(float*, const unsigned long*, const Index);
215 template void convert_cuda<float, unsigned long long>(float*, const unsigned long long*, const Index);
216 template void convert_cuda<double, float>(double*, const float*, const Index);
217 template void convert_cuda<double, signed int>(double*, const signed int*, const Index);
218 template void convert_cuda<double, signed long>(double*, const signed long*, const Index);
219 template void convert_cuda<double, signed long long>(double*, const signed long long*, const Index);
220 template void convert_cuda<double, unsigned int>(double*, const unsigned int*, const Index);
221 template void convert_cuda<double, unsigned long>(double*, const unsigned long*, const Index);
222 template void convert_cuda<double, unsigned long long>(double*, const unsigned long long*, const Index);
223 template void convert_cuda<signed int, float>(signed int*, const float*, const Index);
224 template void convert_cuda<signed int, double>(signed int*, const double*, const Index);
225 template void convert_cuda<signed int, signed long>(signed int*, const signed long*, const Index);
226 template void convert_cuda<signed int, signed long long>(signed int*, const signed long long*, const Index);
227 template void convert_cuda<signed int, unsigned int>(signed int*, const unsigned int*, const Index);
228 template void convert_cuda<signed int, unsigned long>(signed int*, const unsigned long*, const Index);
229 template void convert_cuda<signed int, unsigned long long>(signed int*, const unsigned long long*, const Index);
230 template void convert_cuda<signed long, float>(signed long*, const float*, const Index);
231 template void convert_cuda<signed long, double>(signed long*, const double*, const Index);
232 template void convert_cuda<signed long, signed int>(signed long*, const signed int*, const Index);
233 template void convert_cuda<signed long, signed long long>(signed long*, const signed long long*, const Index);
234 template void convert_cuda<signed long, unsigned int>(signed long*, const unsigned int*, const Index);
235 template void convert_cuda<signed long, unsigned long>(signed long*, const unsigned long*, const Index);
236 template void convert_cuda<signed long, unsigned long long>(signed long*, const unsigned long long*, const Index);
237 template void convert_cuda<signed long long, float>(signed long long*, const float*, const Index);
238 template void convert_cuda<signed long long, double>(signed long long*, const double*, const Index);
239 template void convert_cuda<signed long long, signed int>(signed long long*, const signed int*, const Index);
240 template void convert_cuda<signed long long, signed long>(signed long long*, const signed long*, const Index);
241 template void convert_cuda<signed long long, unsigned int>(signed long long*, const unsigned int*, const Index);
242 template void convert_cuda<signed long long, unsigned long>(signed long long*, const unsigned long*, const Index);
243 template void convert_cuda<signed long long, unsigned long long>(signed long long*, const unsigned long long*, const Index);
244 template void convert_cuda<unsigned int, float>(unsigned int*, const float*, const Index);
245 template void convert_cuda<unsigned int, double>(unsigned int*, const double*, const Index);
246 template void convert_cuda<unsigned int, signed int>(unsigned int*, const signed int*, const Index);
247 template void convert_cuda<unsigned int, signed long>(unsigned int*, const signed long*, const Index);
248 template void convert_cuda<unsigned int, signed long long>(unsigned int*, const signed long long*, const Index);
249 template void convert_cuda<unsigned int, unsigned long>(unsigned int*, const unsigned long*, const Index);
250 template void convert_cuda<unsigned int, unsigned long long>(unsigned int*, const unsigned long long*, const Index);
251 template void convert_cuda<unsigned long, float>(unsigned long*, const float*, const Index);
252 template void convert_cuda<unsigned long, double>(unsigned long*, const double*, const Index);
253 template void convert_cuda<unsigned long, signed int>(unsigned long*, const signed int*, const Index);
254 template void convert_cuda<unsigned long, signed long>(unsigned long*, const signed long*, const Index);
255 template void convert_cuda<unsigned long, signed long long>(unsigned long*, const signed long long*, const Index);
256 template void convert_cuda<unsigned long, unsigned int>(unsigned long*, const unsigned int*, const Index);
257 template void convert_cuda<unsigned long, unsigned long long>(unsigned long*, const unsigned long long*, const Index);
258 template void convert_cuda<unsigned long long, float>(unsigned long long*, const float*, const Index);
259 template void convert_cuda<unsigned long long, double>(unsigned long long*, const double*, const Index);
260 template void convert_cuda<unsigned long long, signed int>(unsigned long long*, const signed int*, const Index);
261 template void convert_cuda<unsigned long long, signed long>(unsigned long long*, const signed long*, const Index);
262 template void convert_cuda<unsigned long long, signed long long>(unsigned long long*, const signed long long*, const Index);
263 template void convert_cuda<unsigned long long, unsigned int>(unsigned long long*, const unsigned int*, const Index);
264 template void convert_cuda<unsigned long long, unsigned long>(unsigned long long*, const unsigned long*, const Index);
265
266 template <typename T_>
267 __global__ void format_cuda_impl(T_* dst, T_ value, const Index count)
268 {
269 // grid strided for loop
270 for(Index i = threadIdx.x + blockDim.x * blockIdx.x; i < count; i += blockDim.x * gridDim.x)
271 dst[i] = value;
272 }
273
274 template<typename T_>
275 void format_cuda(T_* dst, const T_& value, const Index count)
276 {
277 if(count <= Index(0))
278 return;
279
280 dim3 block(static_cast<unsigned int>(Util::cuda_blocksize_misc));
281 dim3 grid((static_cast<unsigned int>(count) + block.x - 1u) / block.x); // round up to next integer
282 format_cuda_impl<<<grid, block>>>(dst, value, count);
283
284#ifdef DEBUG
285 cudaDeviceSynchronize();
286 cudaError_t last_error(cudaGetLastError());
287 if (cudaSuccess != last_error)
288 {
289 std::string strerr("Memory::format_cuda failed!\n");
290 strerr.append(cudaGetErrorString(last_error));
291 XABORTM(strerr.c_str());
292 }
293#endif
294 }
295
296#ifdef FEAT_HAVE_HALFMATH
297 template void format_cuda<Half>(Half *, const Half&, const Index);
298#endif
299 template void format_cuda<float>(float *, const float&, const Index);
300 template void format_cuda<double>(double *, const double&, const Index);
301 template void format_cuda<signed int>(signed int *, const signed int&, const Index);
302 template void format_cuda<signed long>(signed long *, const signed long&, const Index);
303 template void format_cuda<signed long long>(signed long long*, const signed long long&, const Index);
304 template void format_cuda<unsigned int>(unsigned int *, const unsigned int&, const Index);
305 template void format_cuda<unsigned long>(unsigned long *, const unsigned long&, const Index);
306 template void format_cuda<unsigned long long>(unsigned long long*, const unsigned long long&, const Index);
307 } // namespace Memory
308} // namespace FEAT