|
14 | 14 | #include <CL/sycl/detail/os_util.hpp>
|
15 | 15 | #include <CL/sycl/detail/pi.h>
|
16 | 16 |
|
| 17 | +#include <cassert> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +// Function to load the shared library |
| 21 | +// Implementation is OS dependent. |
| 22 | +void *loadOsLibrary(const std::string &Library); |
| 23 | + |
| 24 | +// Function to get Address of a symbol defined in the shared |
| 25 | +// library, implementation is OS dependent. |
| 26 | +void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName); |
| 27 | + |
17 | 28 | namespace cl {
|
18 | 29 | namespace sycl {
|
19 | 30 | namespace detail {
|
20 | 31 | namespace pi {
|
21 |
| - // For selection of SYCL RT back-end, now manually through the "SYCL_BE" |
22 |
| - // environment variable. |
23 |
| - // |
24 |
| - enum Backend { |
25 |
| - SYCL_BE_PI_OPENCL, |
26 |
| - SYCL_BE_PI_OTHER |
27 |
| - }; |
28 |
| - |
29 |
| - // Check for manually selected BE at run-time. |
30 |
| - bool useBackend(Backend Backend); |
31 |
| - |
32 |
| - using PiResult = ::pi_result; |
33 |
| - using PiPlatform = ::pi_platform; |
34 |
| - using PiDevice = ::pi_device; |
35 |
| - using PiDeviceType = ::pi_device_type; |
36 |
| - using PiDeviceInfo = ::pi_device_info; |
37 |
| - using PiDeviceBinaryType = ::pi_device_binary_type; |
38 |
| - using PiContext = ::pi_context; |
39 |
| - using PiProgram = ::pi_program; |
40 |
| - using PiKernel = ::pi_kernel; |
41 |
| - using PiQueue = ::pi_queue; |
42 |
| - using PiQueueProperties = ::pi_queue_properties; |
43 |
| - using PiMem = ::pi_mem; |
44 |
| - using PiMemFlags = ::pi_mem_flags; |
45 |
| - using PiEvent = ::pi_event; |
46 |
| - using PiSampler = ::pi_sampler; |
47 |
| - using PiSamplerInfo = ::pi_sampler_info; |
48 |
| - using PiSamplerProperties = ::pi_sampler_properties; |
49 |
| - using PiSamplerAddressingMode = ::pi_sampler_addressing_mode; |
50 |
| - using PiSamplerFilterMode = ::pi_sampler_filter_mode; |
51 |
| - using PiMemImageFormat = ::pi_image_format; |
52 |
| - using PiMemImageDesc = ::pi_image_desc; |
53 |
| - using PiMemImageInfo = ::pi_image_info; |
54 |
| - using PiMemObjectType = ::pi_mem_type; |
55 |
| - using PiMemImageChannelOrder = ::pi_image_channel_order; |
56 |
| - using PiMemImageChannelType = ::pi_image_channel_type; |
57 |
| - |
58 |
| - // Get a string representing a _pi_platform_info enum |
59 |
| - std::string platformInfoToString(pi_platform_info info); |
60 |
| - |
61 |
| - // Report error and no return (keeps compiler happy about no return statements). |
62 |
| - [[noreturn]] void die(const char *Message); |
63 |
| - void assertion(bool Condition, const char *Message = nullptr); |
64 |
| - |
65 |
| - // Want all the needed casts be explicit, do not define conversion operators. |
66 |
| - template<class To, class From> |
67 |
| - To cast(From value); |
68 |
| - |
69 |
| - // Forward declarations of the PI dispatch entries. |
| 32 | +// For selection of SYCL RT back-end, now manually through the "SYCL_BE" |
| 33 | +// environment variable. |
| 34 | +// |
| 35 | +enum Backend { SYCL_BE_PI_OPENCL, SYCL_BE_PI_OTHER }; |
| 36 | + |
| 37 | +#ifdef SYCL_RT_OS_WINDOWS |
| 38 | +#define PLUGIN_NAME "pi_opencl.dll" |
| 39 | +#else |
| 40 | +#define PLUGIN_NAME "libpi_opencl.so" |
| 41 | +#endif |
| 42 | + |
| 43 | +// Check for manually selected BE at run-time. |
| 44 | +bool useBackend(Backend Backend); |
| 45 | + |
| 46 | +using PiResult = ::pi_result; |
| 47 | +using PiPlatform = ::pi_platform; |
| 48 | +using PiDevice = ::pi_device; |
| 49 | +using PiDeviceType = ::pi_device_type; |
| 50 | +using PiDeviceInfo = ::pi_device_info; |
| 51 | +using PiDeviceBinaryType = ::pi_device_binary_type; |
| 52 | +using PiContext = ::pi_context; |
| 53 | +using PiProgram = ::pi_program; |
| 54 | +using PiKernel = ::pi_kernel; |
| 55 | +using PiQueue = ::pi_queue; |
| 56 | +using PiQueueProperties = ::pi_queue_properties; |
| 57 | +using PiMem = ::pi_mem; |
| 58 | +using PiMemFlags = ::pi_mem_flags; |
| 59 | +using PiEvent = ::pi_event; |
| 60 | +using PiSampler = ::pi_sampler; |
| 61 | +using PiSamplerInfo = ::pi_sampler_info; |
| 62 | +using PiSamplerProperties = ::pi_sampler_properties; |
| 63 | +using PiSamplerAddressingMode = ::pi_sampler_addressing_mode; |
| 64 | +using PiSamplerFilterMode = ::pi_sampler_filter_mode; |
| 65 | +using PiMemImageFormat = ::pi_image_format; |
| 66 | +using PiMemImageDesc = ::pi_image_desc; |
| 67 | +using PiMemImageInfo = ::pi_image_info; |
| 68 | +using PiMemObjectType = ::pi_mem_type; |
| 69 | +using PiMemImageChannelOrder = ::pi_image_channel_order; |
| 70 | +using PiMemImageChannelType = ::pi_image_channel_type; |
| 71 | + |
| 72 | +// Get a string representing a _pi_platform_info enum |
| 73 | +std::string platformInfoToString(pi_platform_info info); |
| 74 | + |
| 75 | +// Report error and no return (keeps compiler happy about no return statements). |
| 76 | +[[noreturn]] void die(const char *Message); |
| 77 | +void assertion(bool Condition, const char *Message = nullptr); |
| 78 | + |
| 79 | +// Want all the needed casts be explicit, do not define conversion operators. |
| 80 | +template <class To, class From> To cast(From value); |
| 81 | + |
| 82 | +// Forward declarations of the PI dispatch entries. |
70 | 83 | #define _PI_API(api) __SYCL_EXPORTED extern decltype(::api) *(api);
|
71 | 84 | #include <CL/sycl/detail/pi.def>
|
72 | 85 |
|
73 |
| - // Performs PI one-time initialization. |
74 |
| - void initialize(); |
75 |
| - |
76 |
| - // The PiCall helper structure facilitates performing a call to PI. |
77 |
| - // It holds utilities to do the tracing and to check the returned result. |
78 |
| - // TODO: implement a more mature and controllable tracing of PI calls. |
79 |
| - class PiCall { |
80 |
| - PiResult m_Result; |
81 |
| - static bool m_TraceEnabled; |
82 |
| - |
83 |
| - public: |
84 |
| - explicit PiCall(const char *Trace = nullptr); |
85 |
| - ~PiCall(); |
86 |
| - PiResult get(PiResult Result); |
87 |
| - template<typename Exception> |
88 |
| - void check(PiResult Result); |
89 |
| - }; |
90 |
| - |
91 |
| - // The run-time tracing of PI calls. |
92 |
| - // TODO: replace PiCall completely with this one (PiTrace) |
93 |
| - // |
94 |
| - template <typename T> inline |
95 |
| - void print(T val) { |
96 |
| - std::cout << "<unknown> : " << val; |
97 |
| - } |
| 86 | +// Performs PI one-time initialization. |
| 87 | +void initialize(); |
| 88 | + |
| 89 | +// The PiCall helper structure facilitates performing a call to PI. |
| 90 | +// It holds utilities to do the tracing and to check the returned result. |
| 91 | +// TODO: implement a more mature and controllable tracing of PI calls. |
| 92 | +class PiCall { |
| 93 | + PiResult m_Result; |
| 94 | + static bool m_TraceEnabled; |
| 95 | + |
| 96 | +public: |
| 97 | + explicit PiCall(const char *Trace = nullptr); |
| 98 | + ~PiCall(); |
| 99 | + PiResult get(PiResult Result); |
| 100 | + template <typename Exception> void check(PiResult Result); |
| 101 | +}; |
| 102 | + |
| 103 | +// The run-time tracing of PI calls. |
| 104 | +// TODO: replace PiCall completely with this one (PiTrace) |
| 105 | +// |
| 106 | +template <typename T> inline void print(T val) { |
| 107 | + std::cout << "<unknown> : " << val; |
| 108 | +} |
98 | 109 |
|
99 |
| - template<> inline void print<> (PiPlatform val) { std::cout << "pi_platform : " << val; } |
100 |
| - template<> inline void print<> (PiResult val) { |
101 |
| - std::cout << "pi_result : "; |
102 |
| - if (val == PI_SUCCESS) |
103 |
| - std::cout << "PI_SUCCESS"; |
104 |
| - else |
105 |
| - std::cout << val; |
106 |
| - } |
107 |
| - |
108 |
| - inline void printArgs(void) {} |
109 |
| - template <typename Arg0, typename... Args> |
110 |
| - void printArgs(Arg0 arg0, Args... args) { |
111 |
| - std::cout << std::endl << " "; |
112 |
| - print(arg0); |
113 |
| - printArgs(std::forward<Args>(args)...); |
| 110 | +template <> inline void print<>(PiPlatform val) { |
| 111 | + std::cout << "pi_platform : " << val; |
| 112 | +} |
| 113 | +template <> inline void print<>(PiResult val) { |
| 114 | + std::cout << "pi_result : "; |
| 115 | + if (val == PI_SUCCESS) |
| 116 | + std::cout << "PI_SUCCESS"; |
| 117 | + else |
| 118 | + std::cout << val; |
| 119 | +} |
| 120 | + |
| 121 | +inline void printArgs(void) {} |
| 122 | +template <typename Arg0, typename... Args> |
| 123 | +void printArgs(Arg0 arg0, Args... args) { |
| 124 | + std::cout << std::endl << " "; |
| 125 | + print(arg0); |
| 126 | + printArgs(std::forward<Args>(args)...); |
| 127 | +} |
| 128 | + |
| 129 | +template <typename FnType> class Trace { |
| 130 | +private: |
| 131 | + FnType m_FnPtr; |
| 132 | + static bool m_TraceEnabled; |
| 133 | + |
| 134 | +public: |
| 135 | + Trace(FnType FnPtr, const std::string &FnName) : m_FnPtr(FnPtr) { |
| 136 | + if (m_TraceEnabled) |
| 137 | + std::cout << "---> " << FnName << "("; |
114 | 138 | }
|
115 |
| - |
116 |
| - template <typename FnType> |
117 |
| - class Trace { |
118 |
| - private: |
119 |
| - FnType m_FnPtr; |
120 |
| - static bool m_TraceEnabled; |
121 |
| - public: |
122 |
| - Trace(FnType FnPtr, const std::string &FnName) : m_FnPtr(FnPtr) { |
123 |
| - if (m_TraceEnabled) |
124 |
| - std::cout << "---> " << FnName << "("; |
125 |
| - } |
126 |
| - |
127 |
| - template <typename... Args> |
128 |
| - typename std::result_of<FnType(Args...)>::type |
129 |
| - operator() (Args... args) { |
130 |
| - if (m_TraceEnabled) |
131 |
| - printArgs(args...); |
132 |
| - |
133 |
| - initialize(); |
134 |
| - auto r = m_FnPtr(args...); |
135 |
| - |
136 |
| - if (m_TraceEnabled) { |
137 |
| - std::cout << ") ---> "; |
138 |
| - std::cout << (print(r),"") << "\n"; |
139 |
| - } |
140 |
| - return r; |
| 139 | + |
| 140 | + template <typename... Args> |
| 141 | + typename std::result_of<FnType(Args...)>::type operator()(Args... args) { |
| 142 | + if (m_TraceEnabled) |
| 143 | + printArgs(args...); |
| 144 | + |
| 145 | + initialize(); |
| 146 | + auto r = m_FnPtr(args...); |
| 147 | + |
| 148 | + if (m_TraceEnabled) { |
| 149 | + std::cout << ") ---> "; |
| 150 | + std::cout << (print(r), "") << "\n"; |
141 | 151 | }
|
142 |
| - }; |
| 152 | + return r; |
| 153 | + } |
| 154 | +}; |
143 | 155 |
|
144 |
| - template <typename FnType> |
145 |
| - bool Trace<FnType>::m_TraceEnabled = (std::getenv("SYCL_PI_TRACE") != nullptr); |
| 156 | +template <typename FnType> |
| 157 | +bool Trace<FnType>::m_TraceEnabled = (std::getenv("SYCL_PI_TRACE") != nullptr); |
146 | 158 |
|
147 | 159 | } // namespace pi
|
148 | 160 |
|
149 | 161 | namespace RT = cl::sycl::detail::pi;
|
150 | 162 |
|
151 |
| -#define PI_ASSERT(cond, msg) \ |
152 |
| - RT::assertion((cond), "assert: " msg); |
| 163 | +#define PI_ASSERT(cond, msg) RT::assertion((cond), "assert: " msg); |
153 | 164 |
|
154 | 165 | #define PI_TRACE(func) RT::Trace<decltype(func)>(func, #func)
|
155 | 166 |
|
156 | 167 | // This does the call, the trace and the check for no errors.
|
157 |
| -#define PI_CALL(pi) \ |
158 |
| - RT::initialize(), \ |
159 |
| - RT::PiCall(#pi).check<cl::sycl::runtime_error>( \ |
160 |
| - RT::cast<detail::RT::PiResult>(pi)) |
| 168 | +#define PI_CALL(pi) \ |
| 169 | + RT::initialize(), RT::PiCall(#pi).check<cl::sycl::runtime_error>( \ |
| 170 | + RT::cast<detail::RT::PiResult>(pi)) |
161 | 171 |
|
162 | 172 | // This does the trace, the call, and returns the result
|
163 |
| -#define PI_CALL_RESULT(pi) \ |
164 |
| - RT::PiCall(#pi).get(detail::RT::cast<detail::RT::PiResult>(pi)) |
| 173 | +#define PI_CALL_RESULT(pi) \ |
| 174 | + RT::PiCall(#pi).get(detail::RT::cast<detail::RT::PiResult>(pi)) |
165 | 175 |
|
166 | 176 | // This does the check for no errors and possibly throws
|
167 |
| -#define PI_CHECK(pi) \ |
168 |
| - RT::PiCall().check<cl::sycl::runtime_error>( \ |
169 |
| - RT::cast<detail::RT::PiResult>(pi)) |
| 177 | +#define PI_CHECK(pi) \ |
| 178 | + RT::PiCall().check<cl::sycl::runtime_error>( \ |
| 179 | + RT::cast<detail::RT::PiResult>(pi)) |
170 | 180 |
|
171 | 181 | // This does the check for no errors and possibly throws x
|
172 |
| -#define PI_CHECK_THROW(pi, x) \ |
173 |
| - RT::PiCall().check<x>( \ |
174 |
| - RT::cast<detail::RT::PiResult>(pi)) |
| 182 | +#define PI_CHECK_THROW(pi, x) \ |
| 183 | + RT::PiCall().check<x>(RT::cast<detail::RT::PiResult>(pi)) |
175 | 184 |
|
176 | 185 | // Want all the needed casts be explicit, do not define conversion operators.
|
177 |
| -template<class To, class From> |
178 |
| -To pi::cast(From value) { |
| 186 | +template <class To, class From> To pi::cast(From value) { |
179 | 187 | // TODO: see if more sanity checks are possible.
|
180 | 188 | PI_ASSERT(sizeof(From) == sizeof(To), "cast failed size check");
|
181 | 189 | return (To)(value);
|
|
0 commit comments