Skip to content

Eliminated redundancy in plugin initialization. #1868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sycl/include/CL/sycl/detail/pi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ template <class To, class From> To cast(From value);
extern std::shared_ptr<plugin> GlobalPlugin;

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

// Utility Functions to get Function Name for a PI Api.
template <PiApiKind PiApiOffset> struct PiFuncInfo {};
Expand Down
27 changes: 21 additions & 6 deletions sycl/source/detail/pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ constexpr const char *GVerStr = "sycl 1.0";

namespace pi {

static void initializePlugins(vector_class<plugin> *Plugins);

bool XPTIInitDone = false;

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

// Initializes all available Plugins.
vector_class<plugin> initialize() {
vector_class<plugin> Plugins;
const vector_class<plugin> &initialize() {
static std::once_flag PluginsInitDone;
static vector_class<plugin> *Plugins = nullptr;

std::call_once(PluginsInitDone, []() {
// The memory for "Plugins" is intentionally leaked because the application
// may call into the SYCL runtime from a global destructor, and such a call
// could eventually call down to initialize(). Therefore, there is no safe
// time when "Plugins" could be deleted.
Plugins = new vector_class<plugin>;
initializePlugins(Plugins);
});

return *Plugins;
}

static void initializePlugins(vector_class<plugin> *Plugins) {
vector_class<std::pair<std::string, backend>> PluginNames;
findPlugins(PluginNames);

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

#ifdef XPTI_ENABLE_INSTRUMENTATION
if (!(xptiTraceEnabled() && !XPTIInitDone))
return Plugins;
return;
// Not sure this is the best place to initialize the framework; SYCL runtime
// team needs to advise on the right place, until then we piggy-back on the
// initialization of the PI layer.
Expand Down Expand Up @@ -353,8 +370,6 @@ vector_class<plugin> initialize() {
xptiMakeEvent("PI Layer", &PIPayload, xpti::trace_algorithm_event,
xpti_at::active, &PiInstanceNo);
#endif

return Plugins;
}

// Report error and no return (keeps compiler from printing warnings).
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/platform_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static bool IsBannedPlatform(platform Platform) {

vector_class<platform> platform_impl::get_platforms() {
vector_class<platform> Platforms;
vector_class<plugin> Plugins = RT::initialize();
const vector_class<plugin> &Plugins = RT::initialize();

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