Actual source code: memoryManager.hpp
1: //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
2: // Copyright (c) 2016-21, Lawrence Livermore National Security, LLC
3: // and RAJA project contributors. See the RAJA/COPYRIGHT file for details.
4: //
5: // SPDX-License-Identifier: (BSD-3-Clause)
6: //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
8: #ifndef EXAMPLES_MEMORYMANAGER_HPP
9: #define EXAMPLES_MEMORYMANAGER_HPP
11: #include "RAJA/RAJA.hpp"
13: #if defined(RAJA_ENABLE_CUDA)
14: #include "RAJA/policy/cuda/raja_cudaerrchk.hpp"
15: #endif
17: #if defined(RAJA_ENABLE_HIP)
18: #include "RAJA/policy/hip/raja_hiperrchk.hpp"
19: #endif
21: /*
22: As RAJA does not manage memory we include a general purpose memory
23: manager which may be used to perform c++ style allocation/deallocation
24: or allocate/deallocate CUDA unified memory. The type of memory allocated
25: is dependent on how RAJA was configured.
26: */
27: namespace memoryManager
28: {
30: #if defined(RAJA_ENABLE_SYCL)
31: static camp::resources::Resource* sycl_res;
32: #endif
34: template <typename T>
35: T *allocate(RAJA::Index_type size)
36: {
37: T *ptr;
38: #if defined(RAJA_ENABLE_CUDA)
39: cudaErrchk(
40: cudaMallocManaged((void **)&ptr, sizeof(T) * size, cudaMemAttachGlobal));
41: #elif defined(RAJA_ENABLE_HIP)
42: hipErrchk(hipMalloc((void **)&ptr, sizeof(T) * size));
43: #elif defined(RAJA_ENABLE_SYCL)
44: ptr = sycl_res->allocate<T>(size);
45: #else
46: ptr = new T[size];
47: #endif
48: return ptr;
49: }
51: template <typename T>
52: void deallocate(T *&ptr)
53: {
54: if (ptr) {
55: #if defined(RAJA_ENABLE_CUDA)
56: cudaErrchk(cudaFree(ptr));
57: #elif defined(RAJA_ENABLE_HIP)
58: hipErrchk(hipFree(ptr));
59: #elif defined(RAJA_ENABLE_SYCL)
60: sycl_res->deallocate(ptr);
61: #else
62: delete[] ptr;
63: #endif
64: ptr = nullptr;
65: }
66: }
68: #if defined(RAJA_ENABLE_CUDA) || defined(RAJA_ENABLE_HIP) || defined(RAJA_ENABLE_SYCL)
69: template <typename T>
70: T *allocate_gpu(RAJA::Index_type size)
71: {
72: T *ptr;
73: #if defined(RAJA_ENABLE_CUDA)
74: cudaErrchk(cudaMalloc((void **)&ptr, sizeof(T) * size));
75: #elif defined(RAJA_ENABLE_HIP)
76: hipErrchk(hipMalloc((void **)&ptr, sizeof(T) * size));
77: #elif defined(RAJA_ENABLE_SYCL)
78: auto qu = sycl_res->get<camp::resources::Sycl>().get_queue();
79: ptr = cl::sycl::malloc_device<T>(size, *qu);
80: #endif
81: return ptr;
82: }
84: template <typename T>
85: void deallocate_gpu(T *&ptr)
86: {
87: if (ptr) {
88: #if defined(RAJA_ENABLE_CUDA)
89: cudaErrchk(cudaFree(ptr));
90: #elif defined(RAJA_ENABLE_HIP)
91: hipErrchk(hipFree(ptr));
92: #elif defined(RAJA_ENABLE_SYCL)
93: sycl_res->deallocate(ptr);
94: #endif
95: ptr = nullptr;
96: }
97: }
98: #endif
100: }; // namespace memoryManager
101: #endif