Skip to content

[SYCL] Add sycl-ls utility for listing devices discovered/selected by SYCL RT #1575

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 6 commits into from
May 1, 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: 2 additions & 0 deletions sycl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ add_custom_target( sycl-toolchain
llvm-link
llvm-objcopy
sycl-post-link
sycl-ls
COMMENT "Building SYCL compiler toolchain..."
)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions sycl/include/CL/sycl/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ template <typename Type> struct get_kernel_name_t<detail::auto_name, Type> {

__SYCL_EXPORT device getDeviceFromHandler(handler &);

device getDeviceFromHandler(handler &);

} // namespace detail

namespace intel {
Expand Down
15 changes: 15 additions & 0 deletions sycl/test/plugins/sycl-ls-gpu-cuda.cpp
Original file line number Diff line number Diff line change
@@ -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
//
//===----------------------------------------------------------------------===//
18 changes: 18 additions & 0 deletions sycl/test/plugins/sycl-ls-gpu-opencl.cpp
Original file line number Diff line number Diff line change
@@ -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
//
//===----------------------------------------------------------------------===//
24 changes: 24 additions & 0 deletions sycl/test/plugins/sycl-ls-gpu-sycl-be.cpp
Original file line number Diff line number Diff line change
@@ -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
//
//===----------------------------------------------------------------------===//
11 changes: 11 additions & 0 deletions sycl/test/plugins/sycl-ls.cpp
Original file line number Diff line number Diff line change
@@ -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
//
//===----------------------------------------------------------------------===//
3 changes: 2 additions & 1 deletion sycl/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions sycl/tools/sycl-ls/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
157 changes: 157 additions & 0 deletions sycl/tools/sycl-ls/sycl-ls.cpp
Original file line number Diff line number Diff line change
@@ -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 <CL/sycl.hpp>

#include <cstdlib>
#include <iostream>
#include <stdlib.h>

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<info::device::device_type>() == MType ? 1 : -1;
}
};

static void printDeviceInfo(const device &Device, const std::string &Prepend) {
auto DeviceType = Device.get_info<info::device::device_type>();
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<info::device::version>();
auto DeviceName = Device.get_info<info::device::name>();
auto DeviceVendor = Device.get_info<info::device::vendor>();
auto DeviceDriverVersion = Device.get_info<info::device::driver_version>();

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<info::platform::version>();
auto PlatformName = Platform.get_info<info::platform::name>();
auto PlatformVendor = Platform.get_info<info::platform::vendor>();
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;
}