Skip to content

Commit 03dd60d

Browse files
[SYCL] Eliminate the redundancy in pi initialization. (#1868)
Passed the implementation of pi initialization into a "call_once" function, so that plugin initialization only needs to be executed once. After the first time, the already initialized plugin will be returned. Also, updated pi::initialize to return const reference to the global vector of plugins, rather than a temporary copy of it.
1 parent 03d934e commit 03dd60d

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

sycl/include/CL/sycl/detail/pi.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ template <class To, class From> To cast(From value);
137137
extern std::shared_ptr<plugin> GlobalPlugin;
138138

139139
// Performs PI one-time initialization.
140-
vector_class<plugin> initialize();
140+
const vector_class<plugin> &initialize();
141141

142142
// Utility Functions to get Function Name for a PI Api.
143143
template <PiApiKind PiApiOffset> struct PiFuncInfo {};

sycl/source/detail/pi.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ constexpr const char *GVerStr = "sycl 1.0";
5252

5353
namespace pi {
5454

55+
static void initializePlugins(vector_class<plugin> *Plugins);
56+
5557
bool XPTIInitDone = false;
5658

5759
// Implementation of the SYCL PI API call tracing methods that use XPTI
@@ -254,8 +256,23 @@ bool trace(TraceLevel Level) {
254256
}
255257

256258
// Initializes all available Plugins.
257-
vector_class<plugin> initialize() {
258-
vector_class<plugin> Plugins;
259+
const vector_class<plugin> &initialize() {
260+
static std::once_flag PluginsInitDone;
261+
static vector_class<plugin> *Plugins = nullptr;
262+
263+
std::call_once(PluginsInitDone, []() {
264+
// The memory for "Plugins" is intentionally leaked because the application
265+
// may call into the SYCL runtime from a global destructor, and such a call
266+
// could eventually call down to initialize(). Therefore, there is no safe
267+
// time when "Plugins" could be deleted.
268+
Plugins = new vector_class<plugin>;
269+
initializePlugins(Plugins);
270+
});
271+
272+
return *Plugins;
273+
}
274+
275+
static void initializePlugins(vector_class<plugin> *Plugins) {
259276
vector_class<std::pair<std::string, backend>> PluginNames;
260277
findPlugins(PluginNames);
261278

@@ -303,7 +320,7 @@ vector_class<plugin> initialize() {
303320
// Use the CUDA plugin as the GlobalPlugin
304321
GlobalPlugin = std::make_shared<plugin>(PluginInformation, backend::cuda);
305322
}
306-
Plugins.emplace_back(plugin(PluginInformation, PluginNames[I].second));
323+
Plugins->emplace_back(plugin(PluginInformation, PluginNames[I].second));
307324
if (trace(TraceLevel::PI_TRACE_BASIC))
308325
std::cerr << "SYCL_PI_TRACE[basic]: "
309326
<< "Plugin found and successfully loaded: "
@@ -312,7 +329,7 @@ vector_class<plugin> initialize() {
312329

313330
#ifdef XPTI_ENABLE_INSTRUMENTATION
314331
if (!(xptiTraceEnabled() && !XPTIInitDone))
315-
return Plugins;
332+
return;
316333
// Not sure this is the best place to initialize the framework; SYCL runtime
317334
// team needs to advise on the right place, until then we piggy-back on the
318335
// initialization of the PI layer.
@@ -353,8 +370,6 @@ vector_class<plugin> initialize() {
353370
xptiMakeEvent("PI Layer", &PIPayload, xpti::trace_algorithm_event,
354371
xpti_at::active, &PiInstanceNo);
355372
#endif
356-
357-
return Plugins;
358373
}
359374

360375
// Report error and no return (keeps compiler from printing warnings).

sycl/source/detail/platform_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static bool IsBannedPlatform(platform Platform) {
4949

5050
vector_class<platform> platform_impl::get_platforms() {
5151
vector_class<platform> Platforms;
52-
vector_class<plugin> Plugins = RT::initialize();
52+
const vector_class<plugin> &Plugins = RT::initialize();
5353

5454
info::device_type ForcedType = detail::get_forced_type();
5555
for (unsigned int i = 0; i < Plugins.size(); i++) {

0 commit comments

Comments
 (0)