|
15 | 15 | #include <CL/sycl/context.hpp> |
16 | 16 | #include <CL/sycl/detail/common.hpp> |
17 | 17 | #include <CL/sycl/detail/pi.hpp> |
| 18 | +#include <detail/config.hpp> |
18 | 19 | #include <detail/plugin.hpp> |
19 | 20 |
|
20 | 21 | #include <bitset> |
21 | 22 | #include <cstdarg> |
22 | 23 | #include <cstring> |
23 | 24 | #include <iostream> |
24 | 25 | #include <map> |
| 26 | +#include <sstream> |
25 | 27 | #include <stddef.h> |
26 | 28 | #include <string> |
27 | | -#include <sstream> |
28 | 29 |
|
29 | 30 | #ifdef XPTI_ENABLE_INSTRUMENTATION |
30 | 31 | // Include the headers necessary for emitting |
@@ -141,39 +142,21 @@ std::string memFlagsToString(pi_mem_flags Flags) { |
141 | 142 | return Sstream.str(); |
142 | 143 | } |
143 | 144 |
|
144 | | -// Check for manually selected BE at run-time. |
145 | | -static Backend getBackend() { |
146 | | - static const char *GetEnv = std::getenv("SYCL_BE"); |
147 | | - // Current default backend as SYCL_BE_PI_OPENCL |
148 | | - // Valid values of GetEnv are "PI_OPENCL", "PI_CUDA" and "PI_OTHER" |
149 | | - std::string StringGetEnv = (GetEnv ? GetEnv : "PI_OPENCL"); |
150 | | - static const Backend Use = |
151 | | - std::map<std::string, Backend>{ |
152 | | - { "PI_OPENCL", SYCL_BE_PI_OPENCL }, |
153 | | - { "PI_CUDA", SYCL_BE_PI_CUDA }, |
154 | | - { "PI_OTHER", SYCL_BE_PI_OTHER } |
155 | | - }[ GetEnv ? StringGetEnv : "PI_OPENCL"]; |
156 | | - return Use; |
157 | | -} |
158 | | - |
159 | | -// Check for manually selected BE at run-time. |
160 | | -bool useBackend(Backend TheBackend) { |
161 | | - return TheBackend == getBackend(); |
162 | | -} |
163 | | - |
164 | 145 | // GlobalPlugin is a global Plugin used with Interoperability constructors that |
165 | 146 | // use OpenCL objects to construct SYCL class objects. |
166 | 147 | std::shared_ptr<plugin> GlobalPlugin; |
167 | 148 |
|
168 | 149 | // Find the plugin at the appropriate location and return the location. |
169 | | -// TODO: Change the function appropriately when there are multiple plugins. |
170 | | -bool findPlugins(vector_class<std::string> &PluginNames) { |
| 150 | +bool findPlugins(vector_class<std::pair<std::string, backend>> &PluginNames) { |
171 | 151 | // TODO: Based on final design discussions, change the location where the |
172 | 152 | // plugin must be searched; how to identify the plugins etc. Currently the |
173 | 153 | // search is done for libpi_opencl.so/pi_opencl.dll file in LD_LIBRARY_PATH |
174 | 154 | // env only. |
175 | | - PluginNames.push_back(OPENCL_PLUGIN_NAME); |
176 | | - PluginNames.push_back(CUDA_PLUGIN_NAME); |
| 155 | + // |
| 156 | + PluginNames.push_back(std::make_pair<std::string, backend>(OPENCL_PLUGIN_NAME, |
| 157 | + backend::opencl)); |
| 158 | + PluginNames.push_back( |
| 159 | + std::make_pair<std::string, backend>(CUDA_PLUGIN_NAME, backend::cuda)); |
177 | 160 | return true; |
178 | 161 | } |
179 | 162 |
|
@@ -207,52 +190,59 @@ bool bindPlugin(void *Library, PiPlugin *PluginInformation) { |
207 | 190 | return true; |
208 | 191 | } |
209 | 192 |
|
210 | | -// Load the plugin based on SYCL_BE. |
211 | | -// TODO: Currently only accepting OpenCL and CUDA plugins. Edit it to identify |
212 | | -// and load other kinds of plugins, do the required changes in the |
213 | | -// findPlugins, loadPlugin and bindPlugin functions. |
| 193 | +bool trace(TraceLevel Level) { |
| 194 | + auto TraceLevelMask = SYCLConfig<SYCL_PI_TRACE>::get(); |
| 195 | + return (TraceLevelMask & Level) == Level; |
| 196 | +} |
| 197 | + |
| 198 | +// Initializes all available Plugins. |
214 | 199 | vector_class<plugin> initialize() { |
215 | 200 | vector_class<plugin> Plugins; |
216 | | - |
217 | | - if (!useBackend(SYCL_BE_PI_OPENCL) && !useBackend(SYCL_BE_PI_CUDA)) { |
218 | | - die("Unknown SYCL_BE"); |
219 | | - } |
220 | | - |
221 | | - bool EnableTrace = (std::getenv("SYCL_PI_TRACE") != nullptr); |
222 | | - |
223 | | - vector_class<std::string> PluginNames; |
| 201 | + vector_class<std::pair<std::string, backend>> PluginNames; |
224 | 202 | findPlugins(PluginNames); |
225 | 203 |
|
226 | | - if (PluginNames.empty() && EnableTrace) |
227 | | - std::cerr << "No Plugins Found." << std::endl; |
| 204 | + if (PluginNames.empty() && trace(PI_TRACE_ALL)) |
| 205 | + std::cerr << "SYCL_PI_TRACE[all]: " |
| 206 | + << "No Plugins Found." << std::endl; |
228 | 207 |
|
229 | | - PiPlugin PluginInformation; // TODO: include. |
| 208 | + PiPlugin PluginInformation; |
230 | 209 | for (unsigned int I = 0; I < PluginNames.size(); I++) { |
231 | | - void *Library = loadPlugin(PluginNames[I]); |
| 210 | + void *Library = loadPlugin(PluginNames[I].first); |
232 | 211 |
|
233 | 212 | if (!Library) { |
234 | | - if (EnableTrace) { |
235 | | - std::cerr << "Check if plugin is present. Failed to load plugin: " |
236 | | - << PluginNames[I] << std::endl; |
| 213 | + if (trace(PI_TRACE_ALL)) { |
| 214 | + std::cerr << "SYCL_PI_TRACE[all]: " |
| 215 | + << "Check if plugin is present. " |
| 216 | + << "Failed to load plugin: " << PluginNames[I].first |
| 217 | + << std::endl; |
237 | 218 | } |
238 | 219 | continue; |
239 | 220 | } |
240 | 221 |
|
241 | | - if (!bindPlugin(Library, &PluginInformation) && EnableTrace) { |
242 | | - std::cerr << "Failed to bind PI APIs to the plugin: " << PluginNames[I] |
243 | | - << std::endl; |
| 222 | + if (!bindPlugin(Library, &PluginInformation)) { |
| 223 | + if (trace(PI_TRACE_ALL)) { |
| 224 | + std::cerr << "SYCL_PI_TRACE[all]: " |
| 225 | + << "Failed to bind PI APIs to the plugin: " |
| 226 | + << PluginNames[I].first << std::endl; |
| 227 | + } |
| 228 | + continue; |
244 | 229 | } |
245 | | - if (useBackend(SYCL_BE_PI_OPENCL) && |
246 | | - PluginNames[I].find("opencl") != std::string::npos) { |
| 230 | + backend *BE = SYCLConfig<SYCL_BE>::get(); |
| 231 | + if (!BE || (*BE == backend::opencl && |
| 232 | + PluginNames[I].first.find("opencl") != std::string::npos)) { |
247 | 233 | // Use the OpenCL plugin as the GlobalPlugin |
248 | | - GlobalPlugin = std::make_shared<plugin>(PluginInformation); |
249 | | - } |
250 | | - if (useBackend(SYCL_BE_PI_CUDA) && |
251 | | - PluginNames[I].find("cuda") != std::string::npos) { |
| 234 | + GlobalPlugin = |
| 235 | + std::make_shared<plugin>(PluginInformation, backend::opencl); |
| 236 | + } else if (*BE == backend::cuda && |
| 237 | + PluginNames[I].first.find("cuda") != std::string::npos) { |
252 | 238 | // Use the CUDA plugin as the GlobalPlugin |
253 | | - GlobalPlugin = std::make_shared<plugin>(PluginInformation); |
| 239 | + GlobalPlugin = std::make_shared<plugin>(PluginInformation, backend::cuda); |
254 | 240 | } |
255 | | - Plugins.push_back(plugin(PluginInformation)); |
| 241 | + Plugins.emplace_back(plugin(PluginInformation, PluginNames[I].second)); |
| 242 | + if (trace(TraceLevel::PI_TRACE_BASIC)) |
| 243 | + std::cerr << "SYCL_PI_TRACE[basic]: " |
| 244 | + << "Plugin found and successfully loaded: " |
| 245 | + << PluginNames[I].first << std::endl; |
256 | 246 | } |
257 | 247 |
|
258 | 248 | #ifdef XPTI_ENABLE_INSTRUMENTATION |
|
0 commit comments