Skip to content

Commit 0ddb9fe

Browse files
authored
[SYCL] Add support for get_backend() API (#1879)
This API returns the backend associated with a platform. A LIT test has also been added. Signed-off-by: Gail Lyons <[email protected]>
1 parent bbe8457 commit 0ddb9fe

File tree

6 files changed

+73
-0
lines changed

6 files changed

+73
-0
lines changed

sycl/include/CL/sycl/backend_types.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,34 @@
1010

1111
#include <CL/sycl/detail/defines.hpp>
1212

13+
#include <fstream>
14+
#include <iostream>
15+
#include <istream>
16+
#include <string>
17+
1318
__SYCL_INLINE_NAMESPACE(cl) {
1419
namespace sycl {
1520

1621
enum class backend : char { host, opencl, level0, cuda };
1722

1823
template <backend name, typename SYCLObjectT> struct interop;
1924

25+
inline std::ostream &operator<<(std::ostream &Out, backend be) {
26+
switch (be) {
27+
case backend::host:
28+
Out << std::string("host");
29+
break;
30+
case backend::opencl:
31+
Out << std::string("opencl");
32+
break;
33+
case backend::level0:
34+
Out << std::string("level-zero");
35+
break;
36+
case backend::cuda:
37+
Out << std::string("cuda");
38+
}
39+
return Out;
40+
}
41+
2042
} // namespace sycl
2143
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/include/CL/sycl/platform.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ class __SYCL_EXPORT platform {
102102
/// \return a vector of all available SYCL platforms.
103103
static vector_class<platform> get_platforms();
104104

105+
/// Returns the backend associated with this platform.
106+
///
107+
/// \return the backend associated with this platform
108+
backend get_backend() const noexcept;
109+
105110
/// Gets the native handle of the SYCL platform.
106111
///
107112
/// \return a native handle, the type of which defined by the backend.

sycl/source/detail/platform_impl.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class platform_impl {
106106
/// \return a vector of all available SYCL platforms.
107107
static vector_class<platform> get_platforms();
108108

109+
// \return the Backend associated with this platform.
110+
backend get_backend() const noexcept { return getPlugin().getBackend(); }
111+
109112
// \return the Plugin associated with this platform.
110113
const plugin &getPlugin() const {
111114
assert(!MHostPlatform && "Plugin is not available for Host.");

sycl/source/platform.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ vector_class<platform> platform::get_platforms() {
4343
return detail::platform_impl::get_platforms();
4444
}
4545

46+
backend platform::get_backend() const noexcept { return impl->get_backend(); }
47+
4648
template <info::platform param>
4749
typename info::param_traits<info::platform, param>::return_type
4850
platform::get_info() const {

sycl/test/abi/sycl_symbols_linux.dump

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,6 +3523,7 @@ _ZNK2cl4sycl7sampler19get_addressing_modeEv
35233523
_ZNK2cl4sycl7sampler33get_coordinate_normalization_modeEv
35243524
_ZNK2cl4sycl7samplereqERKS1_
35253525
_ZNK2cl4sycl7samplerneERKS1_
3526+
_ZNK2cl4sycl8platform11get_backendEv
35263527
_ZNK2cl4sycl8platform11get_devicesENS0_4info11device_typeE
35273528
_ZNK2cl4sycl8platform13has_extensionERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
35283529
_ZNK2cl4sycl8platform3getEv

sycl/test/basic_tests/get_backend.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: %t.out
3+
//
4+
//==----------------- get_backend.cpp ------------------------==//
5+
// This is a test of get_backend().
6+
// Also prints handy info about the system.
7+
// Do not set SYCL_BE. We do not want the preferred backend.
8+
//==----------------------------------------------------------==//
9+
10+
#include <CL/sycl.hpp>
11+
#include <CL/sycl/backend_types.hpp>
12+
#include <iostream>
13+
14+
using namespace cl::sycl;
15+
16+
bool check(backend be) {
17+
switch (be) {
18+
case backend::opencl:
19+
case backend::level0:
20+
case backend::cuda:
21+
case backend::host:
22+
return true;
23+
default:
24+
return false;
25+
}
26+
return false;
27+
}
28+
29+
int main() {
30+
for (const auto &plt : platform::get_platforms()) {
31+
if (!plt.is_host()) {
32+
if (check(plt.get_backend()) == false) {
33+
std::cout << "Failed" << std::endl;
34+
return 1;
35+
}
36+
}
37+
}
38+
std::cout << "Passed" << std::endl;
39+
return 0;
40+
}

0 commit comments

Comments
 (0)