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.
6#include <kernel/util/memory_aux.hpp>
7#include <kernel/util/cuda_helper.cuh>
8#include <kernel/util/cuda_util.hpp>
14 void* alloc_main(std::size_t bytes)
17 if(cudaSuccess != ::cudaMallocHost(&ptr, bytes))
22 void free_main(void* ptr)
26 cudaCheck(::cudaFreeHost(ptr), "Memory::free_main: cudaFreeHost");
29 void* alloc_cuda(std::size_t bytes)
34 cudaCheck(::cudaMalloc(&ptr, bytes), "Memory::alloc_cuda: cudaMalloc");
38 void* alloc_cuda_async(std::size_t bytes, [[maybe_unused]] const Util::StreamWrapper& stream)
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);
46 st = ::cudaMalloc(&ptr, bytes);
48 cudaCheck(st, "Memory::alloc_cuda_async");
52 void free_cuda(void* ptr)
56 cudaCheck(::cudaFree(ptr), "Memory::free_cuda: cudaFree");
59 void free_cuda_async(void* ptr, [[maybe_unused]] const Util::StreamWrapper& stream)
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);
70 cudaCheck(st, "Memory::free_cuda_async");
73 void memset_cuda(void* dst, int value, std::size_t bytes, const Util::StreamWrapper& stream)
75 if(bytes <= std::size_t(0))
78 XASSERTM(dst != nullptr, "cuda device memset destination is nullptr");
80 if constexpr(async_operations)
82 const cudaStream_t s_stream = reinterpret_cast<cudaStream_t>(stream.stream);
83 cudaCheck(cudaMemsetAsync(dst, value, bytes, s_stream), "Memory::memset_cuda async");
87 cudaCheck(cudaMemset(dst, value, bytes), "Memory::memset_cuda");
91 void memcopy_cuda(void* dst, const void* src, std::size_t bytes, const Util::StreamWrapper& stream)
93 if(bytes <= std::size_t(0))
96 XASSERTM(dst != nullptr, "cuda device-to-device memcpy destination is nullptr");
97 XASSERTM(src != nullptr, "cuda device-to-device memcpy source is nullptr");
99 if constexpr(async_operations)
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");
106 cudaCheck(cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToDevice), "Memory::memcopy_cuda");
110 void memcopy_cuda_to_main(void* dst, const void* src, std::size_t bytes)
112 if(bytes <= std::size_t(0))
115 XASSERTM(dst != nullptr, "cuda device-to-host memcpy destination is nullptr");
116 XASSERTM(src != nullptr, "cuda device-to-host memcpy source is nullptr");
118 cudaCheck(cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToHost), "Memory::memcopy_cuda_to_main");
121 void memcopy_cuda_to_main_async(void* dst, const void* src, std::size_t bytes, const Util::StreamWrapper& stream)
123 if(bytes <= std::size_t(0))
126 XASSERTM(dst != nullptr, "cuda device-to-host memcpy destination is nullptr");
127 XASSERTM(src != nullptr, "cuda device-to-host memcpy source is nullptr");
129 if constexpr(async_operations)
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");
135 cudaCheck(cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToHost), "Memory::memcopy_cuda_to_main_async");
138 void memcopy_main_to_cuda(void* dst, const void* src, std::size_t bytes, const Util::StreamWrapper& stream)
140 if(bytes <= std::size_t(0))
143 XASSERTM(dst != nullptr, "cuda host-to-device memcpy destination is nullptr");
144 XASSERTM(src != nullptr, "cuda host-to-device memcpy source is nullptr");
146 // should be okay to do on main stream, since we can not delete source memory without sync call
147 if constexpr(async_operations)
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");
153 cudaCheck(cudaMemcpy(dst, src, bytes, cudaMemcpyHostToDevice), "Memory::memcopy_main_to_cuda");
156 template <typename Tdst_, typename Tsrc_>
157 __global__ void convert_cuda_impl(Tdst_* dst, const Tsrc_* src, const Index count)
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]);
164 template<typename Tdst_, typename Tsrc_>
165 void convert_cuda(Tdst_* dst, const Tsrc_* src, const Index count)
167 if(count <= Index(0))
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);
175 cudaError_t last_error(cudaGetLastError());
176 if (cudaSuccess != last_error)
178 std::string strerr("Memory::convert_cuda failed!\n");
179 strerr.append(cudaGetErrorString(last_error));
180 XABORTM(strerr.c_str());
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);
192 // Python code for generating the mumbo jumbo below:
198 // "signed long long",
201 // "unsigned long long"
207 // print("template void convert_cuda<" + t + ", " + s + ">(" + t + "*, const " + s + "*, const Index);")
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);
266 template <typename T_>
267 __global__ void format_cuda_impl(T_* dst, T_ value, const Index count)
269 // grid strided for loop
270 for(Index i = threadIdx.x + blockDim.x * blockIdx.x; i < count; i += blockDim.x * gridDim.x)
274 template<typename T_>
275 void format_cuda(T_* dst, const T_& value, const Index count)
277 if(count <= Index(0))
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);
285 cudaDeviceSynchronize();
286 cudaError_t last_error(cudaGetLastError());
287 if (cudaSuccess != last_error)
289 std::string strerr("Memory::format_cuda failed!\n");
290 strerr.append(cudaGetErrorString(last_error));
291 XABORTM(strerr.c_str());
296#ifdef FEAT_HAVE_HALFMATH
297 template void format_cuda<Half>(Half *, const Half&, const Index);
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