From 93b6cdbb7b12a507dac5d5f67b6f62404e1a092a Mon Sep 17 00:00:00 2001 From: Alexander Johnston Date: Fri, 14 Feb 2020 17:10:51 +0000 Subject: [PATCH 1/4] [SYCL] Have default_selector consider SYCL_BE Have the default_selector consider the env var SYCL_BE when rating device scores to make choosing a backend easier. Signed-off-by: Alexander Johnston --- sycl/source/device_selector.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/sycl/source/device_selector.cpp b/sycl/source/device_selector.cpp index 6f3e1c636bee..8188254a6e71 100644 --- a/sycl/source/device_selector.cpp +++ b/sycl/source/device_selector.cpp @@ -31,6 +31,24 @@ device device_selector::select_device() const { } int default_selector::operator()(const device &dev) const { + + // Take note of the SYCL_BE environment variable when doing default selection + const char *SYCL_BE = std::getenv("SYCL_BE"); + std::string backend = (SYCL_BE ? SYCL_BE : ""); + if (backend != "") { + const std::string DriverVersion = dev.get_info(); + // If we have a cuda device but aren't using PI_CUDA, don't use this device + if (DriverVersion.find("CUDA") == std::string::npos && backend == "PI_CUDA") { + return -1; + } + // We can't easily check for an OpenCL device as there is no common string + // to search for on all devices, so just guarantee we don't choose a cuda + // device when PI_OPENCL is used + if (DriverVersion.find("CUDA") != std::string::npos && backend == "PI_OPENCL") { + return -1; + } + } + if (dev.is_gpu()) return 500; From bfca37a3e8d5b3466e25bf88689d31d979e29900 Mon Sep 17 00:00:00 2001 From: Alexander Johnston Date: Mon, 17 Feb 2020 15:40:57 +0000 Subject: [PATCH 2/4] [SYCL] Select GlobalPlugin based on SYCL_BE Rather than choose the last found plugin as GlobalPlugin, select it depending on the SYCL_BE env var. Signed-off-by: Alexander Johnston --- sycl/source/detail/pi.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index f5ce57c79f58..94f2642aa758 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -166,9 +166,9 @@ bool bindPlugin(void *Library, PiPlugin *PluginInformation) { } // Load the plugin based on SYCL_BE. -// TODO: Currently only accepting OpenCL and CUDA plugins. Edit it to identify and load -// other kinds of plugins, do the required changes in the findPlugins, -// loadPlugin and bindPlugin functions. +// TODO: Currently only accepting OpenCL and CUDA plugins. Edit it to identify +// and load other kinds of plugins, do the required changes in the +// findPlugins, loadPlugin and bindPlugin functions. vector_class initialize() { vector_class Plugins; @@ -196,11 +196,18 @@ vector_class initialize() { std::cerr << "Failed to bind PI APIs to the plugin: " << PluginNames[I] << std::endl; } + if (useBackend(SYCL_BE_PI_OPENCL) && + PluginNames[I].find("opencl") != std::string::npos) { + // Use the OpenCL plugin as the GlobalPlugin + GlobalPlugin = std::make_shared(PluginInformation); + } + if (useBackend(SYCL_BE_PI_CUDA) && + PluginNames[I].find("cuda") != std::string::npos) { + // Use the CUDA plugin as the GlobalPlugin + GlobalPlugin = std::make_shared(PluginInformation); + } Plugins.push_back(plugin(PluginInformation)); } - // TODO: Correct the logic to store the appropriate plugin into GlobalPlugin - // variable. Currently it saves the last plugin found. - GlobalPlugin = std::make_shared(PluginInformation); return Plugins; } From 3357807322605d1456725f9fcbad2f407d8bde9c Mon Sep 17 00:00:00 2001 From: Alexander Johnston Date: Mon, 17 Feb 2020 15:42:34 +0000 Subject: [PATCH 3/4] [SYCL] Improve default device selection checks Better checks for CUDA and OpenCL devices to match with SYCL_BE in the default device selection, based on the platform version info. Signed-off-by: Alexander Johnston --- sycl/source/device_selector.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/sycl/source/device_selector.cpp b/sycl/source/device_selector.cpp index 8188254a6e71..fbdbdafb76bd 100644 --- a/sycl/source/device_selector.cpp +++ b/sycl/source/device_selector.cpp @@ -36,15 +36,16 @@ int default_selector::operator()(const device &dev) const { const char *SYCL_BE = std::getenv("SYCL_BE"); std::string backend = (SYCL_BE ? SYCL_BE : ""); if (backend != "") { - const std::string DriverVersion = dev.get_info(); - // If we have a cuda device but aren't using PI_CUDA, don't use this device - if (DriverVersion.find("CUDA") == std::string::npos && backend == "PI_CUDA") { + // Taking the version information from the platform gives us more useful + // information than the driver_version of the device. + const platform Platform = dev.get_info(); + const std::string PlatformVersion = Platform.get_info(); + // If using PI_CUDA, don't accept a non-CUDA device + if (PlatformVersion.find("CUDA") == std::string::npos && backend == "PI_CUDA") { return -1; } - // We can't easily check for an OpenCL device as there is no common string - // to search for on all devices, so just guarantee we don't choose a cuda - // device when PI_OPENCL is used - if (DriverVersion.find("CUDA") != std::string::npos && backend == "PI_OPENCL") { + // If using PI_OPENCL, don't accept a non-OpenCL device + if (PlatformVersion.find("OpenCL") == std::string::npos && backend == "PI_OPENCL") { return -1; } } From e88ed595c7785406480e213793fda0d079d386e9 Mon Sep 17 00:00:00 2001 From: Alexander Johnston Date: Tue, 18 Feb 2020 12:05:02 +0000 Subject: [PATCH 4/4] [SYCL] Formatting update for device_selector.cpp Signed-off-by: Alexander Johnston --- sycl/source/device_selector.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/sycl/source/device_selector.cpp b/sycl/source/device_selector.cpp index fbdbdafb76bd..89439b023759 100644 --- a/sycl/source/device_selector.cpp +++ b/sycl/source/device_selector.cpp @@ -34,18 +34,21 @@ int default_selector::operator()(const device &dev) const { // Take note of the SYCL_BE environment variable when doing default selection const char *SYCL_BE = std::getenv("SYCL_BE"); - std::string backend = (SYCL_BE ? SYCL_BE : ""); - if (backend != "") { + if (SYCL_BE) { + std::string backend = (SYCL_BE ? SYCL_BE : ""); // Taking the version information from the platform gives us more useful // information than the driver_version of the device. const platform Platform = dev.get_info(); - const std::string PlatformVersion = Platform.get_info(); + const std::string PlatformVersion = + Platform.get_info();; // If using PI_CUDA, don't accept a non-CUDA device - if (PlatformVersion.find("CUDA") == std::string::npos && backend == "PI_CUDA") { + if (PlatformVersion.find("CUDA") == std::string::npos && + backend == "PI_CUDA") { return -1; } // If using PI_OPENCL, don't accept a non-OpenCL device - if (PlatformVersion.find("OpenCL") == std::string::npos && backend == "PI_OPENCL") { + if (PlatformVersion.find("OpenCL") == std::string::npos && + backend == "PI_OPENCL") { return -1; } }