DPC++ Runtime
Runtime libraries for oneAPI DPC++
backend_traits_opencl.hpp
Go to the documentation of this file.
1 //===---- backend_traits_opencl.hpp - Backend traits for OpenCL --*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the specializations of the sycl::detail::interop,
10 // sycl::detail::BackendInput, sycl::detail::BackendReturn and
11 // sycl::detail::InteropFeatureSupportMap class templates for the OpenCL
12 // backend.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #pragma once
17 
18 #include <sycl/backend_types.hpp> // for backend
19 #include <sycl/context.hpp> // for context
20 #include <sycl/detail/backend_traits.hpp> // for BackendInput, BackendReturn
21 #include <sycl/detail/cl.h> // for _cl_event, cl_event, cl_de...
22 #include <sycl/detail/pi.hpp> // for assertion, PiDevice, PiPro...
23 #include <sycl/device.hpp> // for device
24 #include <sycl/event.hpp> // for event
25 #include <sycl/handler.hpp> // for buffer
26 #include <sycl/kernel.hpp> // for kernel
27 #include <sycl/kernel_bundle.hpp> // for kernel_bundle
28 #include <sycl/kernel_bundle_enums.hpp> // for bundle_state
29 #include <sycl/platform.hpp> // for platform
30 #include <sycl/queue.hpp> // for queue
31 
32 #include <vector> // for vector
33 
34 namespace sycl {
35 inline namespace _V1 {
36 namespace detail {
37 
38 // TODO the interops for context, device, event, platform and program
39 // may be removed after removing the deprecated 'get_native()' methods
40 // from the corresponding classes.
41 template <> struct interop<backend::opencl, context> {
42  using type = cl_context;
43 };
44 
45 template <> struct interop<backend::opencl, device> {
46  using type = cl_device_id;
47 };
48 
49 template <> struct interop<backend::opencl, queue> {
50  using type = cl_command_queue;
51 };
52 
53 template <> struct interop<backend::opencl, platform> {
54  using type = cl_platform_id;
55 };
56 
57 template <typename DataT, int Dimensions, typename AllocatorT>
58 struct BackendInput<backend::opencl, buffer<DataT, Dimensions, AllocatorT>> {
59  using type = cl_mem;
60 };
61 
62 template <typename DataT, int Dimensions, typename AllocatorT>
63 struct BackendReturn<backend::opencl, buffer<DataT, Dimensions, AllocatorT>> {
64  using type = std::vector<cl_mem>;
65 };
66 
67 template <> struct BackendInput<backend::opencl, context> {
68  using type = cl_context;
69 };
70 
71 template <> struct BackendReturn<backend::opencl, context> {
72  using type = cl_context;
73 };
74 
75 template <> struct BackendInput<backend::opencl, device> {
76  using type = cl_device_id;
77 };
78 
79 template <> struct BackendReturn<backend::opencl, device> {
80  using type = cl_device_id;
81 };
82 
83 template <> struct interop<backend::opencl, event> {
84  using type = std::vector<cl_event>;
85  using value_type = cl_event;
86 };
87 template <> struct BackendInput<backend::opencl, event> {
88  using type = std::vector<cl_event>;
89  using value_type = cl_event;
90 };
91 template <> struct BackendReturn<backend::opencl, event> {
92  using type = std::vector<cl_event>;
93  using value_type = cl_event;
94 };
95 
96 template <> struct BackendInput<backend::opencl, queue> {
97  using type = cl_command_queue;
98 };
99 
100 template <> struct BackendReturn<backend::opencl, queue> {
101  using type = cl_command_queue;
102 };
103 
104 template <> struct BackendInput<backend::opencl, platform> {
105  using type = cl_platform_id;
106 };
107 
108 template <> struct BackendReturn<backend::opencl, platform> {
109  using type = cl_platform_id;
110 };
111 
112 template <bundle_state State>
114  using type = cl_program;
115 };
116 
117 template <bundle_state State>
119  using type = std::vector<cl_program>;
120 };
121 
122 template <> struct BackendInput<backend::opencl, kernel> {
123  using type = cl_kernel;
124 };
125 
126 template <> struct BackendReturn<backend::opencl, kernel> {
127  using type = cl_kernel;
128 };
129 
131  static constexpr bool MakePlatform = true;
132  static constexpr bool MakeDevice = true;
133  static constexpr bool MakeContext = true;
134  static constexpr bool MakeQueue = true;
135  static constexpr bool MakeEvent = true;
136  static constexpr bool MakeBuffer = true;
137  static constexpr bool MakeKernel = true;
138  static constexpr bool MakeKernelBundle = true;
139  static constexpr bool MakeImage = false;
140 };
141 
142 namespace pi {
143 // Cast for std::vector<cl_event>, according to the spec, make_event
144 // should create one(?) event from a vector of cl_event
145 template <class To> inline To cast(std::vector<cl_event> value) {
147  value.size() == 1,
148  "Temporary workaround requires that the "
149  "size of the input vector for make_event be equal to one.");
150  return cast<To>(value[0]);
151 }
152 
153 // These conversions should use PI interop API.
154 template <>
155 inline PiProgram
156  cast(cl_program) = delete; // Use piextCreateProgramWithNativeHandle
157 
158 template <>
159 inline PiDevice
160  cast(cl_device_id) = delete; // Use piextCreateDeviceWithNativeHandle
161 } // namespace pi
162 } // namespace detail
163 } // namespace _V1
164 } // namespace sycl
Defines a shared array that can be used by kernels in queues.
Definition: buffer.hpp:168
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:50
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
An event object can be used to synchronize memory transfers, enqueues of kernels and signaling barrie...
Definition: event.hpp:44
The kernel_bundle class represents collection of device images in a particular state.
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:77
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:99
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:111
void assertion(bool Condition, const char *Message=nullptr)
Definition: pi.cpp:531
PiProgram cast(cl_program)=delete
::pi_program PiProgram
Definition: pi.hpp:137
Definition: access.hpp:18
C++ wrapper of extern "C" PI interfaces.