Nektar++
Loading...
Searching...
No Matches
MemoryAlloc.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: MemoryAlloc.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#pragma once
36
37#include <algorithm>
38#include <cstring>
39
41
42namespace Nektar
43{
44
45#if defined(NEKTAR_ENABLE_CUDA)
46extern bool isSetDeviceMemoryPool;
47#elif defined(NEKTAR_ENABLE_HIP)
48extern bool isSetDeviceMemoryPool;
49#endif
50
51// Memory copy type
59
60// Memory allocation type
68
69static inline void SetDeviceMemoryPool(void)
70{
71#if defined(NEKTAR_ENABLE_CUDA)
72 if (!isSetDeviceMemoryPool)
73 {
74 auto device = nekGetDevice();
75 cudaMemPool_t mempool;
76 CHECK_HIPCUDA_ERROR(cudaDeviceGetDefaultMemPool(&mempool, device));
77 uint64_t threshold = UINT64_MAX;
78 CHECK_HIPCUDA_ERROR(cudaMemPoolSetAttribute(
79 mempool, cudaMemPoolAttrReleaseThreshold, &threshold));
80 }
81#elif defined(NEKTAR_ENABLE_HIP)
82 if (!isSetDeviceMemoryPool)
83 {
84 auto device = nekGetDevice();
85 hipMemPool_t mempool;
86 CHECK_HIPCUDA_ERROR(hipDeviceGetDefaultMemPool(&mempool, device));
87 uint64_t threshold = UINT64_MAX;
88 CHECK_HIPCUDA_ERROR(hipMemPoolSetAttribute(
89 mempool, hipMemPoolAttrReleaseThreshold, &threshold));
90 }
91#endif
92}
93
94template <typename TData>
95void deviceFillKernelLauncher(TData *dst, const TData val, const size_t size,
96 const unsigned int streamID);
97
98template <typename TData>
99void deviceZeroPaddingKernelLauncher(TData *dst, const unsigned int ncomp,
100 const size_t compSize, const size_t nelmt,
101 const unsigned int ndata,
102 const unsigned int width,
103 const unsigned int streamID);
104
105template <typename TData>
106inline void hostMalloc(TData **src, const size_t size,
107 [[maybe_unused]] const size_t alignment)
108{
109 if (size > 0)
110 {
111 *src = static_cast<TData *>(
112 ::operator new[](size, std::align_val_t(alignment)));
113 }
114 else
115 {
116 *src = nullptr;
117 }
118}
119
120template <typename TData>
121inline void hostMallocPinned(TData **src, const size_t size)
122{
123 if (size > 0)
124 {
125#if defined(NEKTAR_ENABLE_CUDA)
126 CHECK_HIPCUDA_ERROR(cudaMallocHost((void **)src, size));
127#elif defined(NEKTAR_ENABLE_HIP)
128 CHECK_HIPCUDA_ERROR(hipHostMalloc((void **)src, size));
129#elif defined(NEKTAR_ENABLE_SYCL)
130 sycl::queue &Q = SYCLQueue::GetInstance(0);
131 *src = (TData *)sycl::malloc_host(size, Q);
132#if defined(SYCL_ENABLE_CPU)
133 Q.wait();
134#endif
135#else
136 *src = (TData *)malloc(size);
137#endif
138 }
139 else
140 {
141 *src = nullptr;
142 }
143}
144
145template <typename TData>
146inline void deviceMalloc(
147 TData **src, const size_t size,
148 [[maybe_unused]] const unsigned int streamID,
149 [[maybe_unused]] const MemAllocType memAllocType = eHostPageable)
150{
151 if (size > 0)
152 {
153#if defined(NEKTAR_ENABLE_CUDA)
154 // Note: Do not use DeviceMemoryPool for MPI communication buffer.
155 if (memAllocType == eDeviceMemoryPool ||
156 memAllocType == eHostPinnedDeviceMemoryPool)
157 {
158 auto stream = CUDAStream::GetInstance(streamID);
160 GetDeviceProperties::CheckGlobalMemoryUsage(size);
161 CHECK_HIPCUDA_ERROR(cudaMallocAsync((void **)src, size, stream));
162 GetDeviceProperties::TotalGlobalMemory() -= size;
163 }
164 else
165 {
166 GetDeviceProperties::CheckGlobalMemoryUsage(size);
167 CHECK_HIPCUDA_ERROR(cudaMalloc((void **)src, size));
168 GetDeviceProperties::TotalGlobalMemory() -= size;
169 }
170#elif defined(NEKTAR_ENABLE_HIP)
171 // Note: Do not use DeviceMemoryPool for MPI communication buffer.
172 if (memAllocType == eDeviceMemoryPool ||
173 memAllocType == eHostPinnedDeviceMemoryPool)
174 {
175 auto stream = HIPStream::GetInstance(streamID);
177 GetDeviceProperties::CheckGlobalMemoryUsage(size);
178 CHECK_HIPCUDA_ERROR(hipMallocAsync((void **)src, size, stream));
179 GetDeviceProperties::TotalGlobalMemory() -= size;
180 }
181 else
182 {
183 GetDeviceProperties::CheckGlobalMemoryUsage(size);
184 CHECK_HIPCUDA_ERROR(hipMalloc((void **)src, size));
185 GetDeviceProperties::TotalGlobalMemory() -= size;
186 }
187#elif defined(NEKTAR_ENABLE_SYCL)
188 GetDeviceProperties::CheckGlobalMemoryUsage(size);
189 sycl::queue &Q = SYCLQueue::GetInstance(streamID);
190 *src = (TData *)sycl::malloc_device(size, Q);
191 GetDeviceProperties::TotalGlobalMemory() -= size;
192#if defined(SYCL_ENABLE_CPU)
193 Q.wait();
194#endif
195#else
196 *src = (TData *)malloc(size);
197#endif
198 }
199 else
200 {
201 *src = nullptr;
202 }
203}
204
205template <typename TData>
206inline void hostFree(TData *src, [[maybe_unused]] const size_t alignment)
207{
208 if (src == nullptr)
209 {
210 return;
211 }
212
213 operator delete[](src, std::align_val_t(alignment));
214}
215
216template <typename TData> inline void hostFreePinned(TData *src)
217{
218 if (src == nullptr)
219 {
220 return;
221 }
222
223#if defined(NEKTAR_ENABLE_CUDA)
224 CHECK_HIPCUDA_ERROR(cudaFreeHost(src));
225#elif defined(NEKTAR_ENABLE_HIP)
226 CHECK_HIPCUDA_ERROR(hipFreeHost(src));
227#elif defined(NEKTAR_ENABLE_SYCL)
228 sycl::queue &Q = SYCLQueue::GetInstance(0);
229 Q.wait();
230 sycl::free(src, Q);
231#else
232 free(src);
233#endif
234}
235
236template <typename TData>
237inline void deviceFree(
238 TData *src, [[maybe_unused]] const size_t size,
239 [[maybe_unused]] const unsigned int streamID,
240 [[maybe_unused]] const MemAllocType memAllocType = eHostPageable)
241{
242 if (src == nullptr)
243 {
244 return;
245 }
246
247#if defined(NEKTAR_ENABLE_CUDA)
248 if (memAllocType == eDeviceMemoryPool ||
249 memAllocType == eHostPinnedDeviceMemoryPool)
250 {
251 auto stream = CUDAStream::GetInstance(streamID);
252 CHECK_HIPCUDA_ERROR(cudaFreeAsync(src, stream));
253 GetDeviceProperties::TotalGlobalMemory() += size;
254 }
255 else
256 {
257 CHECK_HIPCUDA_ERROR(cudaFree(src));
258 GetDeviceProperties::TotalGlobalMemory() += size;
259 }
260#elif defined(NEKTAR_ENABLE_HIP)
261 if (memAllocType == eDeviceMemoryPool ||
262 memAllocType == eHostPinnedDeviceMemoryPool)
263 {
264 auto stream = HIPStream::GetInstance(streamID);
265 CHECK_HIPCUDA_ERROR(hipFreeAsync(src, stream));
266 GetDeviceProperties::TotalGlobalMemory() += size;
267 }
268 else
269 {
270 CHECK_HIPCUDA_ERROR(hipFree(src));
271 GetDeviceProperties::TotalGlobalMemory() += size;
272 }
273#elif defined(NEKTAR_ENABLE_SYCL)
274 sycl::queue &Q = SYCLQueue::GetInstance(streamID);
275 if (streamID != 0)
276 {
277 Q.wait();
278 }
279 else
280 {
281 // Imitate CUDA/HIP default stream blocking behaviour by synchronizing
282 // all SYCL queues.
283 auto &Queues = SYCLQueue::GetAllInstances();
284 for (auto &item : Queues)
285 {
286 item.second->wait();
287 }
288 }
289 sycl::free(src, Q);
290 GetDeviceProperties::TotalGlobalMemory() += size;
291#else
292 free(src);
293#endif
294}
295
296template <typename TData>
297inline void deviceMemset(TData *dst, const int val, const size_t size,
298 [[maybe_unused]] const unsigned int streamID)
299{
300 if (size == 0)
301 {
302 return;
303 }
304
305#if defined(NEKTAR_ENABLE_CUDA)
306 auto stream = CUDAStream::GetInstance(streamID);
307 CHECK_HIPCUDA_ERROR(cudaMemsetAsync((void *)dst, val, size, stream));
308#elif defined(NEKTAR_ENABLE_HIP)
309 auto stream = HIPStream::GetInstance(streamID);
310 CHECK_HIPCUDA_ERROR(hipMemsetAsync((void *)dst, val, size, stream));
311#elif defined(NEKTAR_ENABLE_SYCL)
312 sycl::queue &Q = SYCLQueue::GetInstance(streamID);
313 sycl::event e = Q.submit([&](sycl::handler &cgh) {
314 setSYCLDefaultExecutionDependency(streamID, cgh);
315 cgh.memset((void *)dst, val, size);
316 });
317 SYCLQueue::SetEvent(streamID, e);
318#else
319 memset((void *)dst, val, size);
320#endif
321}
322
323template <typename TData>
324inline void deviceFill(TData *dst, const TData val, const size_t size,
325 [[maybe_unused]] const unsigned int streamID)
326{
327 if (size == 0)
328 {
329 return;
330 }
331
332#if defined(NEKTAR_ENABLE_CUDA)
333 deviceFillKernelLauncher(dst, val, size, streamID);
334#elif defined(NEKTAR_ENABLE_HIP)
335 deviceFillKernelLauncher(dst, val, size, streamID);
336#elif defined(NEKTAR_ENABLE_SYCL)
337 sycl::queue &Q = SYCLQueue::GetInstance(streamID);
338 sycl::event e = Q.submit([&](sycl::handler &cgh) {
339 setSYCLDefaultExecutionDependency(streamID, cgh);
340 cgh.fill(dst, val, size);
341 });
342 SYCLQueue::SetEvent(streamID, e);
343#else
344 std::fill(dst, dst + size, val);
345#endif
346}
347
348// Zero, in place, the padding of the ncomp components of compSize values at
349// dst, stored as element groups of width lanes by ndata points, of which the
350// first nelmt elements are not padding. Only the element groups from the first
351// padded element on are visited.
352template <typename TData>
353inline void hostZeroPadding(TData *dst, const unsigned int ncomp,
354 const size_t compSize, const size_t nelmt,
355 const unsigned int ndata, const unsigned int width)
356{
357 const size_t group0 = nelmt / width;
358 const unsigned int lane0 = nelmt % width;
359 for (unsigned int n = 0; n < ncomp; ++n, dst += compSize)
360 {
361 // The padded lanes of the element group holding the first padded
362 // element, point by point, then the element groups after it at once.
363 TData *ptr = dst + group0 * ndata * width;
364 for (unsigned int pt = 0; lane0 != 0 && pt < ndata; ++pt, ptr += width)
365 {
366 std::fill(ptr + lane0, ptr + width, TData(0));
367 }
368 std::fill(ptr, dst + compSize, TData(0));
369 }
370}
371
372template <typename TData>
373inline void deviceZeroPadding(TData *dst, const unsigned int ncomp,
374 const size_t compSize, const size_t nelmt,
375 const unsigned int ndata,
376 const unsigned int width,
377 [[maybe_unused]] const unsigned int streamID)
378{
379 const size_t prefix = (nelmt / width) * width * ndata;
380 if (prefix == compSize)
381 {
382 return;
383 }
384
385#if defined(NEKTAR_ENABLE_CUDA) || defined(NEKTAR_ENABLE_HIP)
386 deviceZeroPaddingKernelLauncher(dst, ncomp, compSize, nelmt, ndata, width,
387 streamID);
388#elif defined(NEKTAR_ENABLE_SYCL)
389 const size_t tail = compSize - prefix;
390 sycl::queue &Q = SYCLQueue::GetInstance(streamID);
391 sycl::event e = Q.submit([=](sycl::handler &cgh) {
392 setSYCLDefaultExecutionDependency(streamID, cgh);
393 cgh.parallel_for(sycl::range<1>(ncomp * tail), [=](sycl::id<1> indx) {
394 const size_t idx = indx[0];
395 const size_t i = prefix + idx % tail;
396 if ((i / (ndata * width)) * width + i % width >= nelmt)
397 {
398 dst[(idx / tail) * compSize + i] = TData(0);
399 }
400 });
401 });
402 SYCLQueue::SetEvent(streamID, e);
403#else
404 hostZeroPadding(dst, ncomp, compSize, nelmt, ndata, width);
405#endif
406}
407
408template <MemCopyType MemCopy, typename TData>
409inline void deviceMemcpy(TData *dst, const TData *src, const size_t size,
410 [[maybe_unused]] const unsigned int streamID)
411{
412 if (size == 0)
413 {
414 return;
415 }
416
417 if constexpr (MemCopy == eHostToHost)
418 {
419 memcpy(dst, src, size);
420 }
421 else if constexpr (MemCopy == eDeviceToHost)
422 {
423#if defined(NEKTAR_ENABLE_CUDA)
424 auto stream = CUDAStream::GetInstance(streamID);
426 cudaMemcpyAsync(dst, src, size, cudaMemcpyDeviceToHost, stream));
427 CHECK_HIPCUDA_ERROR(cudaStreamSynchronize(stream));
428#elif defined(NEKTAR_ENABLE_HIP)
429 auto stream = HIPStream::GetInstance(streamID);
431 hipMemcpyAsync(dst, src, size, hipMemcpyDeviceToHost, stream));
432 CHECK_HIPCUDA_ERROR(hipStreamSynchronize(stream));
433#elif defined(NEKTAR_ENABLE_SYCL)
434 sycl::queue &Q = SYCLQueue::GetInstance(streamID);
435 sycl::event e = Q.submit([&](sycl::handler &cgh) {
436 setSYCLDefaultExecutionDependency(streamID, cgh);
437 cgh.memcpy(dst, src, size);
438 });
439 SYCLQueue::SetEvent(streamID, e);
440 Q.wait();
441#else
442 memcpy(dst, src, size);
443#endif
444 }
445 else if constexpr (MemCopy == eHostToDevice)
446 {
447#if defined(NEKTAR_ENABLE_CUDA)
448 auto stream = CUDAStream::GetInstance(streamID);
450 cudaMemcpyAsync(dst, src, size, cudaMemcpyHostToDevice, stream));
451#elif defined(NEKTAR_ENABLE_HIP)
452 auto stream = HIPStream::GetInstance(streamID);
454 hipMemcpyAsync(dst, src, size, hipMemcpyHostToDevice, stream));
455#elif defined(NEKTAR_ENABLE_SYCL)
456 sycl::queue &Q = SYCLQueue::GetInstance(streamID);
457 sycl::event e = Q.submit([&](sycl::handler &cgh) {
458 setSYCLDefaultExecutionDependency(streamID, cgh);
459 cgh.memcpy(dst, src, size);
460 });
461 SYCLQueue::SetEvent(streamID, e);
462#else
463 memcpy(dst, src, size);
464#endif
465 }
466 else if constexpr (MemCopy == eDeviceToDevice)
467 {
468#if defined(NEKTAR_ENABLE_CUDA)
469 auto stream = CUDAStream::GetInstance(streamID);
471 cudaMemcpyAsync(dst, src, size, cudaMemcpyDeviceToDevice, stream));
472#elif defined(NEKTAR_ENABLE_HIP)
473 auto stream = HIPStream::GetInstance(streamID);
475 hipMemcpyAsync(dst, src, size, hipMemcpyDeviceToDevice, stream));
476#elif defined(NEKTAR_ENABLE_SYCL)
477 sycl::queue &Q = SYCLQueue::GetInstance(streamID);
478 sycl::event e = Q.submit([&](sycl::handler &cgh) {
479 setSYCLDefaultExecutionDependency(streamID, cgh);
480 cgh.memcpy(dst, src, size);
481 });
482 SYCLQueue::SetEvent(streamID, e);
483#else
484 memcpy(dst, src, size);
485#endif
486 }
487}
488
489} // namespace Nektar
#define CHECK_HIPCUDA_ERROR(err)
static cudaStream_t & GetInstance(unsigned int id)
static hipStream_t & GetInstance(unsigned int id)
Definition HIPStream.hpp:85
static std::unordered_map< unsigned int, sycl::queue * > & GetAllInstances(void)
static sycl::queue & GetInstance(unsigned int id)
Definition SYCLQueue.hpp:55
static void SetEvent(unsigned int id, sycl::event &e)
void deviceFree(TData *src, const size_t size, const unsigned int streamID, const MemAllocType memAllocType=eHostPageable)
void hostFreePinned(TData *src)
void hostFree(TData *src, const size_t alignment)
static unsigned int nekGetDevice()
Definition Backends.hpp:380
void deviceZeroPadding(TData *dst, const unsigned int ncomp, const size_t compSize, const size_t nelmt, const unsigned int ndata, const unsigned int width, const unsigned int streamID)
void deviceMemset(TData *dst, const int val, const size_t size, const unsigned int streamID)
@ eDeviceToDevice
@ eDeviceToHost
@ eHostToDevice
void hostZeroPadding(TData *dst, const unsigned int ncomp, const size_t compSize, const size_t nelmt, const unsigned int ndata, const unsigned int width)
void deviceFillKernelLauncher(TData *dst, const TData val, const size_t size, const unsigned int streamID)
void deviceMalloc(TData **src, const size_t size, const unsigned int streamID, const MemAllocType memAllocType=eHostPageable)
static void SetDeviceMemoryPool(void)
@ eHostPinnedDeviceMemoryPool
@ eHostPageable
@ eDeviceMemoryPool
void deviceFill(TData *dst, const TData val, const size_t size, const unsigned int streamID)
void deviceMemcpy(TData *dst, const TData *src, const size_t size, const unsigned int streamID)
std::uint64_t uint64_t
void hostMallocPinned(TData **src, const size_t size)
void deviceZeroPaddingKernelLauncher(TData *dst, const unsigned int ncomp, const size_t compSize, const size_t nelmt, const unsigned int ndata, const unsigned int width, const unsigned int streamID)
void hostMalloc(TData **src, const size_t size, const size_t alignment)