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(cudaMallocManaged((void **)&ptr, sizeof(T) * size, cudaMemAttachGlobal));
40: #elif defined(RAJA_ENABLE_HIP)
41: hipErrchk(hipMalloc((void **)&ptr, sizeof(T) * size));
42: #elif defined(RAJA_ENABLE_SYCL)
43: ptr = sycl_res->allocate<T>(size);
44: #else
45: ptr = new T[size];
46: #endif
47: return ptr;
48: }
50: template <typename T>
51: void deallocate(T *&ptr)
52: {
53: if (ptr) {
54: #if defined(RAJA_ENABLE_CUDA)
55: cudaErrchk(cudaFree(ptr));
56: #elif defined(RAJA_ENABLE_HIP)
57: hipErrchk(hipFree(ptr));
58: #elif defined(RAJA_ENABLE_SYCL)
59: sycl_res->deallocate(ptr);
60: #else
61: delete[] ptr;
62: #endif
63: ptr = nullptr;
64: }
65: }
67: #if defined(RAJA_ENABLE_CUDA) || defined(RAJA_ENABLE_HIP) || defined(RAJA_ENABLE_SYCL)
68: template <typename T>
69: T *allocate_gpu(RAJA::Index_type size)
70: {
71: T *ptr;
72: #if defined(RAJA_ENABLE_CUDA)
73: cudaErrchk(cudaMalloc((void **)&ptr, sizeof(T) * size));
74: #elif defined(RAJA_ENABLE_HIP)
75: hipErrchk(hipMalloc((void **)&ptr, sizeof(T) * size));
76: #elif defined(RAJA_ENABLE_SYCL)
77: auto qu = sycl_res->get<camp::resources::Sycl>().get_queue();
78: ptr = cl::sycl::malloc_device<T>(size, *qu);
79: #endif
80: return ptr;
81: }
83: template <typename T>
84: void deallocate_gpu(T *&ptr)
85: {
86: if (ptr) {
87: #if defined(RAJA_ENABLE_CUDA)
88: cudaErrchk(cudaFree(ptr));
89: #elif defined(RAJA_ENABLE_HIP)
90: hipErrchk(hipFree(ptr));
91: #elif defined(RAJA_ENABLE_SYCL)
92: sycl_res->deallocate(ptr);
93: #endif
94: ptr = nullptr;
95: }
96: }
97: #endif
99: }; // namespace memoryManager
100: #endif