RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
MemUtils_SYCL.hpp
Go to the documentation of this file.
1 
12 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
13 // Copyright (c) Lawrence Livermore National Security, LLC and other
14 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
15 // files for dates and other details. No copyright assignment is required
16 // to contribute to RAJA.
17 //
18 // SPDX-License-Identifier: (BSD-3-Clause)
19 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
20 
21 #ifndef RAJA_MemUtils_SYCL_HPP
22 #define RAJA_MemUtils_SYCL_HPP
23 
24 #include "RAJA/config.hpp"
25 
26 #if defined(RAJA_ENABLE_SYCL)
27 
29 
30 #include <cassert>
31 #include <cstddef>
32 #include <cstdio>
33 #include <mutex>
34 #include <type_traits>
35 #include <unordered_map>
36 
37 
39 #include "RAJA/util/types.hpp"
40 
42 
43 namespace RAJA
44 {
45 
46 namespace sycl
47 {
48 
49 namespace detail
50 {
51 
53 struct syclInfo
54 {
55  sycl_dim_t gridDim {0};
56  sycl_dim_t blockDim {0};
57  ::sycl::queue qu = ::sycl::queue();
58  bool setup_reducers = false;
59 };
60 
61 extern syclInfo g_status;
62 
63 thread_local extern syclInfo tl_status;
64 
65 extern std::unordered_map<::sycl::queue, bool> g_queue_info_map;
66 
67 } // namespace detail
68 
70 struct PinnedAllocator
71 {
72 
73  // returns a valid pointer on success, nullptr on failure
74  void* malloc(size_t nbytes)
75  {
76  void* ptr;
77  ::sycl::queue* q = ::camp::resources::Sycl::get_default().get_queue();
78  ptr = ::sycl::malloc_host(nbytes, *q);
79  return ptr;
80  }
81 
82  // returns true on success
83  // Will throw if ptr is not in q's context
84  bool free(void* ptr)
85  {
86  ::sycl::queue* q = ::camp::resources::Sycl::get_default().get_queue();
87  ::sycl::free(ptr, *q);
88  return true;
89  }
90 };
91 
93 struct DeviceAllocator
94 {
95 
96  // returns a valid pointer on success, nullptr on failure
97  void* malloc(size_t nbytes)
98  {
99  void* ptr;
100  ::sycl::queue* q = ::camp::resources::Sycl::get_default().get_queue();
101  ptr = ::sycl::malloc_device(nbytes, *q);
102  return ptr;
103  }
104 
105  // returns true on success
106  // Will throw if ptr is not in q's context
107  bool free(void* ptr)
108  {
109  ::sycl::queue* q = ::camp::resources::Sycl::get_default().get_queue();
110  ::sycl::free(ptr, *q);
111  return true;
112  }
113 };
114 
116 // Note: Memory must be zero when returned to mempool
117 struct DeviceZeroedAllocator
118 {
119 
120  // returns a valid pointer on success, nullptr on failure
121  void* malloc(size_t nbytes)
122  {
123  void* ptr;
124  ::sycl::queue* q = ::camp::resources::Sycl::get_default().get_queue();
125  ptr = ::sycl::malloc_device(nbytes, *q);
126  q->memset(ptr, 0, nbytes);
127  return ptr;
128  }
129 
130  // Returns true on success
131  // Will throw if ptr is not in q's context
132  bool free(void* ptr)
133  {
134  ::sycl::queue* q = ::camp::resources::Sycl::get_default().get_queue();
135  ::sycl::free(ptr, *q);
136  return true;
137  }
138 };
139 
140 using device_mempool_type = basic_mempool::MemPool<DeviceAllocator>;
141 using device_zeroed_mempool_type =
142  basic_mempool::MemPool<DeviceZeroedAllocator>;
143 using pinned_mempool_type = basic_mempool::MemPool<PinnedAllocator>;
144 
145 } // namespace sycl
146 
147 } // namespace RAJA
148 
149 #endif // closing endif for RAJA_ENABLE_SYCL
150 
151 #endif // closing endif for header file include guard
RAJA header file containing an implementation of a memory pool.
Definition: AlignedRangeIndexSetBuilders.cpp:35
Header file containing RAJA SYCL policy definitions.
RAJA header file for handling different SYCL header include paths.
Header file for RAJA type definitions.