diff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txt index 61049b9e0a3f6..454a8f62e635e 100644 --- a/sycl/CMakeLists.txt +++ b/sycl/CMakeLists.txt @@ -232,6 +232,7 @@ add_custom_target( sycl-toolchain llvm-link llvm-objcopy sycl-post-link + sycl-ls COMMENT "Building SYCL compiler toolchain..." ) @@ -281,6 +282,7 @@ set( SYCL_TOOLCHAIN_DEPLOY_COMPONENTS llvm-link llvm-objcopy sycl-post-link + sycl-ls clang-resource-headers opencl-headers sycl-headers diff --git a/sycl/include/CL/sycl/handler.hpp b/sycl/include/CL/sycl/handler.hpp index 49767539bc683..a66bfa97d1802 100644 --- a/sycl/include/CL/sycl/handler.hpp +++ b/sycl/include/CL/sycl/handler.hpp @@ -107,8 +107,6 @@ template struct get_kernel_name_t { __SYCL_EXPORT device getDeviceFromHandler(handler &); -device getDeviceFromHandler(handler &); - } // namespace detail namespace intel { diff --git a/sycl/test/plugins/sycl-ls-gpu-cuda.cpp b/sycl/test/plugins/sycl-ls-gpu-cuda.cpp new file mode 100755 index 0000000000000..8a2a9aca5b2c0 --- /dev/null +++ b/sycl/test/plugins/sycl-ls-gpu-cuda.cpp @@ -0,0 +1,15 @@ +// REQUIRES: gpu, cuda + +// RUN: env SYCL_BE=PI_CUDA sycl-ls --verbose >%t.cuda.out +// RUN: FileCheck %s --check-prefixes=CHECK-BUILTIN-GPU-CUDA,CHECK-CUSTOM-GPU-CUDA --input-file %t.cuda.out + +// CHECK-BUILTIN-GPU-CUDA: gpu_selector(){{.*}}GPU :{{.*}}CUDA +// CHECK-CUSTOM-GPU-CUDA: custom_selector(gpu){{.*}}GPU :{{.*}}CUDA + +//==---- sycl-ls-gpu-cuda.cpp - SYCL test for discovered/selected devices --==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// diff --git a/sycl/test/plugins/sycl-ls-gpu-opencl.cpp b/sycl/test/plugins/sycl-ls-gpu-opencl.cpp new file mode 100755 index 0000000000000..58a04c6ab4c3a --- /dev/null +++ b/sycl/test/plugins/sycl-ls-gpu-opencl.cpp @@ -0,0 +1,18 @@ +// REQUIRES: gpu, opencl + +// RUN: sycl-ls --verbose >%t.default.out +// RUN: FileCheck %s --check-prefixes=CHECK-GPU-BUILTIN,CHECK-GPU-CUSTOM --input-file %t.default.out + +// RUN: env SYCL_BE=PI_OPENCL sycl-ls --verbose >%t.opencl.out +// RUN: FileCheck %s --check-prefixes=CHECK-GPU-BUILTIN,CHECK-GPU-CUSTOM --input-file %t.opencl.out + +// CHECK-GPU-BUILTIN: gpu_selector(){{.*}}GPU : OpenCL +// CHECK-GPU-CUSTOM: custom_selector(gpu){{.*}}GPU : OpenCL + +//==-- sycl-ls-gpu-opencl.cpp - SYCL test for discovered/selected devices -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// diff --git a/sycl/test/plugins/sycl-ls-gpu-sycl-be.cpp b/sycl/test/plugins/sycl-ls-gpu-sycl-be.cpp new file mode 100755 index 0000000000000..02d217941de24 --- /dev/null +++ b/sycl/test/plugins/sycl-ls-gpu-sycl-be.cpp @@ -0,0 +1,24 @@ +// REQUIRES: gpu, cuda, opencl + +// RUN: sycl-ls --verbose >%t.default.out +// RUN: FileCheck %s --check-prefixes=CHECK-BUILTIN-GPU-OPENCL,CHECK-CUSTOM-GPU-OPENCL --input-file %t.default.out + +// RUN: env SYCL_BE=PI_OPENCL sycl-ls --verbose >%t.opencl.out +// RUN: FileCheck %s --check-prefixes=CHECK-BUILTIN-GPU-OPENCL,CHECK-CUSTOM-GPU-OPENCL --input-file %t.opencl.out + +// CHECK-BUILTIN-GPU-OPENCL: gpu_selector(){{.*}}GPU : OpenCL +// CHECK-CUSTOM-GPU-OPENCL: custom_selector(gpu){{.*}}GPU : OpenCL + +// RUN: env SYCL_BE=PI_CUDA sycl-ls --verbose >%t.cuda.out +// RUN: FileCheck %s --check-prefixes=CHECK-BUILTIN-GPU-CUDA,CHECK-CUSTOM-GPU-CUDA --input-file %t.cuda.out + +// CHECK-BUILTIN-GPU-CUDA: gpu_selector(){{.*}}GPU : CUDA +// CHECK-CUSTOM-GPU-CUDA: custom_selector(gpu){{.*}}GPU : CUDA + +//==---- sycl-ls-gpu-sycl-be.cpp - SYCL test for discovered/selected devices --==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// diff --git a/sycl/test/plugins/sycl-ls.cpp b/sycl/test/plugins/sycl-ls.cpp new file mode 100755 index 0000000000000..c9baaf508da0a --- /dev/null +++ b/sycl/test/plugins/sycl-ls.cpp @@ -0,0 +1,11 @@ +// RUN: sycl-ls --verbose | grep "Device \[" | wc -l >%t.verbose.out +// RUN: sycl-ls | wc -l >%t.concise.out +// RUN: diff %t.verbose.out %t.concise.out + +//==---- sycl-ls.cpp - SYCL test for consistency of sycl-ls output ---------==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// diff --git a/sycl/tools/CMakeLists.txt b/sycl/tools/CMakeLists.txt index c90f55acd508b..6fb4b21c3842d 100644 --- a/sycl/tools/CMakeLists.txt +++ b/sycl/tools/CMakeLists.txt @@ -2,8 +2,9 @@ set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -# TODO: move each tool in its own sub-directory +add_subdirectory(sycl-ls) +# TODO: move each tool in its own sub-directory add_executable(get_device_count_by_type get_device_count_by_type.cpp) add_dependencies(get_device_count_by_type ocl-headers ocl-icd) diff --git a/sycl/tools/sycl-ls/CMakeLists.txt b/sycl/tools/sycl-ls/CMakeLists.txt new file mode 100644 index 0000000000000..79a022841efb0 --- /dev/null +++ b/sycl/tools/sycl-ls/CMakeLists.txt @@ -0,0 +1,8 @@ +add_executable(sycl-ls sycl-ls.cpp) +add_dependencies(sycl-ls sycl) +target_include_directories(sycl-ls PRIVATE "${sycl_inc_dir}") +target_link_libraries(sycl-ls + PRIVATE + sycl + OpenCL::Headers +) diff --git a/sycl/tools/sycl-ls/sycl-ls.cpp b/sycl/tools/sycl-ls/sycl-ls.cpp new file mode 100755 index 0000000000000..f3909723f422f --- /dev/null +++ b/sycl/tools/sycl-ls/sycl-ls.cpp @@ -0,0 +1,157 @@ +//==----------- sycl-ls.cpp ------------------------------------------------==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// The "sycl-ls" utility lists all platforms/devices discovered by SYCL similar +// to how clinfo prints this for OpenCL devices. +// +// There are two types of output: +// concise (default) and +// verbose (enabled with --verbose). +// +// In verbose mode it also prints, which devices would be chosen by various SYCL +// device selectors. +// +#include + +#include +#include +#include + +using namespace cl::sycl; + +// Controls verbose output vs. concise. +bool verbose; + +// Trivial custom selector that selects a device of the given type. +class custom_selector : public device_selector { + info::device_type MType; + +public: + custom_selector(info::device_type Type) : MType(Type) {} + int operator()(const device &Dev) const override { + return Dev.get_info() == MType ? 1 : -1; + } +}; + +static void printDeviceInfo(const device &Device, const std::string &Prepend) { + auto DeviceType = Device.get_info(); + std::string DeviceTypeName; + switch (DeviceType) { + case info::device_type::cpu: + DeviceTypeName = "CPU "; + break; + case info::device_type::gpu: + DeviceTypeName = "GPU "; + break; + case info::device_type::host: + DeviceTypeName = "HOST"; + break; + case info::device_type::accelerator: + DeviceTypeName = "ACC "; + break; + default: + DeviceTypeName = "UNKNOWN"; + break; + } + + auto DeviceVersion = Device.get_info(); + auto DeviceName = Device.get_info(); + auto DeviceVendor = Device.get_info(); + auto DeviceDriverVersion = Device.get_info(); + + if (verbose) { + std::cout << Prepend << "Type : " << DeviceTypeName << std::endl; + std::cout << Prepend << "Version : " << DeviceVersion << std::endl; + std::cout << Prepend << "Name : " << DeviceName << std::endl; + std::cout << Prepend << "Vendor : " << DeviceVendor << std::endl; + std::cout << Prepend << "Driver : " << DeviceDriverVersion << std::endl; + } else { + std::cout << Prepend << DeviceTypeName << ": " << DeviceVersion << "[ " + << DeviceDriverVersion << " ]" << std::endl; + } +} + +static void printSelectorChoice(const device_selector &Selector, + const std::string &Prepend) { + try { + const auto &Dev = device(Selector); + printDeviceInfo(Dev, Prepend); + + } catch (const cl::sycl::runtime_error &Exception) { + // Truncate long string so it can fit in one-line + std::string What = Exception.what(); + if (What.length() > 50) + What = What.substr(0, 50) + "..."; + std::cout << Prepend << What << std::endl; + } +} + +int main(int argc, char **argv) { + + // See if verbose output is requested + if (argc == 1) + verbose = false; + else if (argc == 2 && std::string(argv[1]) == "--verbose") + verbose = true; + else { + std::cout << "Usage: sycl-ls [--verbose]" << std::endl; + return EXIT_FAILURE; + } + + auto Platforms = platform::get_platforms(); + if (verbose) + std::cout << "Platforms: " << Platforms.size() << std::endl; + + uint32_t PlatformNum = 0; + for (const auto &Platform : Platforms) { + ++PlatformNum; + if (verbose) { + auto PlatformVersion = Platform.get_info(); + auto PlatformName = Platform.get_info(); + auto PlatformVendor = Platform.get_info(); + std::cout << "Platform [#" << PlatformNum << "]:" << std::endl; + std::cout << " Version : " << PlatformVersion << std::endl; + std::cout << " Name : " << PlatformName << std::endl; + std::cout << " Vendor : " << PlatformVendor << std::endl; + } + auto Devices = Platform.get_devices(); + if (verbose) + std::cout << " Devices : " << Devices.size() << std::endl; + uint32_t DeviceNum = 0; + for (const auto &Device : Devices) { + ++DeviceNum; + if (verbose) + std::cout << " Device [#" << DeviceNum << "]:" << std::endl; + printDeviceInfo(Device, verbose ? " " : ""); + } + } + + if (!verbose) { + return EXIT_SUCCESS; + } + + // Print the selectors choice in one-line always + verbose = false; + + // Print built-in device selectors choice + printSelectorChoice(default_selector(), "default_selector() : "); + printSelectorChoice(host_selector(), "host_selector() : "); + printSelectorChoice(accelerator_selector(), "accelerator_selector() : "); + printSelectorChoice(cpu_selector(), "cpu_selector() : "); + printSelectorChoice(gpu_selector(), "gpu_selector() : "); + + // Print trivial custom selectors choice + printSelectorChoice(custom_selector(info::device_type::gpu), + "custom_selector(gpu) : "); + printSelectorChoice(custom_selector(info::device_type::cpu), + "custom_selector(cpu) : "); + printSelectorChoice(custom_selector(info::device_type::accelerator), + "custom_selector(acc) : "); + + return EXIT_SUCCESS; +}