|
| 1 | +//===-- dpctl_sycl_type_casters.h - Defines casters between --------*-C++-*- =// |
| 2 | +// the opaque and the underlying types. |
| 3 | +// |
| 4 | +// Data Parallel Control (dpctl) |
| 5 | +// |
| 6 | +// Copyright 2020-2022 Intel Corporation |
| 7 | +// |
| 8 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +// you may not use this file except in compliance with the License. |
| 10 | +// You may obtain a copy of the License at |
| 11 | +// |
| 12 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +// |
| 14 | +// Unless required by applicable law or agreed to in writing, software |
| 15 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +// See the License for the specific language governing permissions and |
| 18 | +// limitations under the License. |
| 19 | +// |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | +/// |
| 22 | +/// \file |
| 23 | +/// This file defines casters between opaque types and underlying SYCL types. |
| 24 | +/// |
| 25 | +//===----------------------------------------------------------------------===// |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +#ifdef __cplusplus |
| 30 | + |
| 31 | +#include "dpctl_sycl_types.h" |
| 32 | +#include <CL/sycl.hpp> |
| 33 | +#include <vector> |
| 34 | + |
| 35 | +#if __SYCL_COMPILER_VERSION >= 20221020 |
| 36 | + |
| 37 | +class dpctl_device_selector |
| 38 | +{ |
| 39 | +public: |
| 40 | + virtual ~dpctl_device_selector() = default; |
| 41 | + |
| 42 | + virtual int operator()(const sycl::device &device) const = 0; |
| 43 | +}; |
| 44 | + |
| 45 | +class dpctl_accelerator_selector : public dpctl_device_selector |
| 46 | +{ |
| 47 | +public: |
| 48 | + dpctl_accelerator_selector() = default; |
| 49 | + int operator()(const sycl::device &d) const |
| 50 | + { |
| 51 | + return sycl::accelerator_selector_v(d); |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +class dpctl_default_selector : public dpctl_device_selector |
| 56 | +{ |
| 57 | +public: |
| 58 | + dpctl_default_selector() = default; |
| 59 | + int operator()(const sycl::device &d) const |
| 60 | + { |
| 61 | + return sycl::default_selector_v(d); |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +class dpctl_gpu_selector : public dpctl_device_selector |
| 66 | +{ |
| 67 | +public: |
| 68 | + dpctl_gpu_selector() = default; |
| 69 | + int operator()(const sycl::device &d) const |
| 70 | + { |
| 71 | + return sycl::gpu_selector_v(d); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +class dpctl_cpu_selector : public dpctl_device_selector |
| 76 | +{ |
| 77 | +public: |
| 78 | + dpctl_cpu_selector() = default; |
| 79 | + int operator()(const sycl::device &d) const |
| 80 | + { |
| 81 | + return sycl::cpu_selector_v(d); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +class dpctl_filter_selector : public dpctl_device_selector |
| 86 | +{ |
| 87 | +public: |
| 88 | + dpctl_filter_selector(const std::string &fs) : _impl(fs) {} |
| 89 | + |
| 90 | + int operator()(const sycl::device &d) const |
| 91 | + { |
| 92 | + return _impl(d); |
| 93 | + } |
| 94 | + |
| 95 | +private: |
| 96 | + sycl::ext::oneapi::filter_selector _impl; |
| 97 | +}; |
| 98 | + |
| 99 | +class dpctl_host_selector : public dpctl_device_selector |
| 100 | +{ |
| 101 | +public: |
| 102 | + dpctl_host_selector() = default; |
| 103 | + int operator()(const sycl::device &) const |
| 104 | + { |
| 105 | + return REJECTED_SCORE; |
| 106 | + } |
| 107 | + |
| 108 | +private: |
| 109 | + constexpr static int REJECTED_SCORE = -1; |
| 110 | +}; |
| 111 | + |
| 112 | +#else |
| 113 | + |
| 114 | +class dpctl_device_selector : public sycl::device_selector |
| 115 | +{ |
| 116 | +public: |
| 117 | + virtual ~dpctl_device_selector() = default; |
| 118 | + |
| 119 | + virtual int operator()(const sycl::device &device) const = 0; |
| 120 | +}; |
| 121 | + |
| 122 | +class dpctl_accelerator_selector : public dpctl_device_selector |
| 123 | +{ |
| 124 | +public: |
| 125 | + dpctl_accelerator_selector() : _impl(){}; |
| 126 | + int operator()(const sycl::device &d) const |
| 127 | + { |
| 128 | + return _impl(d); |
| 129 | + } |
| 130 | + |
| 131 | +private: |
| 132 | + sycl::accelerator_selector _impl; |
| 133 | +}; |
| 134 | + |
| 135 | +class dpctl_default_selector : public dpctl_device_selector |
| 136 | +{ |
| 137 | +public: |
| 138 | + dpctl_default_selector() : _impl(){}; |
| 139 | + int operator()(const sycl::device &d) const |
| 140 | + { |
| 141 | + return _impl(d); |
| 142 | + } |
| 143 | + |
| 144 | +private: |
| 145 | + sycl::default_selector _impl; |
| 146 | +}; |
| 147 | + |
| 148 | +class dpctl_gpu_selector : public dpctl_device_selector |
| 149 | +{ |
| 150 | +public: |
| 151 | + dpctl_gpu_selector() : _impl(){}; |
| 152 | + int operator()(const sycl::device &d) const |
| 153 | + { |
| 154 | + return _impl(d); |
| 155 | + } |
| 156 | + |
| 157 | +private: |
| 158 | + sycl::gpu_selector _impl; |
| 159 | +}; |
| 160 | + |
| 161 | +class dpctl_cpu_selector : public dpctl_device_selector |
| 162 | +{ |
| 163 | +public: |
| 164 | + dpctl_cpu_selector() : _impl(){}; |
| 165 | + int operator()(const sycl::device &d) const |
| 166 | + { |
| 167 | + return _impl(d); |
| 168 | + } |
| 169 | + |
| 170 | +private: |
| 171 | + sycl::cpu_selector _impl; |
| 172 | +}; |
| 173 | + |
| 174 | +class dpctl_filter_selector : public dpctl_device_selector |
| 175 | +{ |
| 176 | +public: |
| 177 | + dpctl_filter_selector(const std::string &fs) : _impl(fs) {} |
| 178 | + |
| 179 | + int operator()(const sycl::device &d) const |
| 180 | + { |
| 181 | + return _impl(d); |
| 182 | + } |
| 183 | + |
| 184 | +private: |
| 185 | + sycl::ext::oneapi::filter_selector _impl; |
| 186 | +}; |
| 187 | + |
| 188 | +class dpctl_host_selector : public dpctl_device_selector |
| 189 | +{ |
| 190 | +public: |
| 191 | + dpctl_host_selector() : _impl(){}; |
| 192 | + int operator()(const sycl::device &d) const |
| 193 | + { |
| 194 | + return _impl(d); |
| 195 | + } |
| 196 | + |
| 197 | +private: |
| 198 | + sycl::host_selector _impl; |
| 199 | +}; |
| 200 | + |
| 201 | +#endif |
| 202 | + |
| 203 | +/*! |
| 204 | + @brief Creates two convenience functions to reinterpret_cast an opaque |
| 205 | + pointer to a pointer to a Sycl type and vice-versa. |
| 206 | +*/ |
| 207 | +#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \ |
| 208 | + __attribute__((unused)) inline ty *unwrap(ref P) \ |
| 209 | + { \ |
| 210 | + return reinterpret_cast<ty *>(P); \ |
| 211 | + } \ |
| 212 | + \ |
| 213 | + __attribute__((unused)) inline ref wrap(const ty *P) \ |
| 214 | + { \ |
| 215 | + return reinterpret_cast<ref>(const_cast<ty *>(P)); \ |
| 216 | + } \ |
| 217 | + template <typename T, \ |
| 218 | + std::enable_if_t<std::is_same<T, ty>::value, bool> = true> \ |
| 219 | + ref wrap(const ty *P) \ |
| 220 | + { \ |
| 221 | + return reinterpret_cast<ref>(const_cast<ty *>(P)); \ |
| 222 | + } |
| 223 | + |
| 224 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(dpctl_device_selector, |
| 225 | + DPCTLSyclDeviceSelectorRef) |
| 226 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::device, DPCTLSyclDeviceRef) |
| 227 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::context, DPCTLSyclContextRef) |
| 228 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::queue, DPCTLSyclQueueRef) |
| 229 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(void, DPCTLSyclUSMRef) |
| 230 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::platform, DPCTLSyclPlatformRef) |
| 231 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::event, DPCTLSyclEventRef) |
| 232 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::kernel, DPCTLSyclKernelRef) |
| 233 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS( |
| 234 | + sycl::kernel_bundle<sycl::bundle_state::executable>, |
| 235 | + DPCTLSyclKernelBundleRef) |
| 236 | + |
| 237 | +#include "dpctl_sycl_device_manager.h" |
| 238 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::vector<DPCTLSyclDeviceRef>, |
| 239 | + DPCTLDeviceVectorRef) |
| 240 | + |
| 241 | +#include "dpctl_sycl_platform_manager.h" |
| 242 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::vector<DPCTLSyclPlatformRef>, |
| 243 | + DPCTLPlatformVectorRef) |
| 244 | + |
| 245 | +#include "dpctl_sycl_event_interface.h" |
| 246 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::vector<DPCTLSyclEventRef>, |
| 247 | + DPCTLEventVectorRef) |
| 248 | + |
| 249 | +#endif |
0 commit comments