Skip to content

Commit ab3af3c

Browse files
authored
[SYCL][COMPAT] Add a free function version of has_capability_or_fail (#15717)
Signed-off-by: Jiang, Zhiwei <[email protected]> --------- Signed-off-by: Jiang, Zhiwei <[email protected]>
1 parent e94cfda commit ab3af3c

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

sycl/doc/syclcompat/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,10 @@ static inline unsigned int get_device_id(const sycl::device &dev);
10481048
// Util function to get the number of available devices
10491049
static inline unsigned int device_count();
10501050
1051+
// Util function to check whether a device supports some kinds of sycl::aspect.
1052+
static inline void
1053+
has_capability_or_fail(const sycl::device &dev,
1054+
const std::initializer_list<sycl::aspect> &props);
10511055
} // syclcompat
10521056
```
10531057

sycl/include/syclcompat/device.hpp

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,51 @@ static int get_minor_version(const sycl::device &dev) {
334334
return minor;
335335
}
336336

337+
static inline void
338+
has_capability_or_fail(const sycl::device &dev,
339+
const std::initializer_list<sycl::aspect> &props) {
340+
for (const auto &it : props) {
341+
if (dev.has(it))
342+
continue;
343+
switch (it) {
344+
case sycl::aspect::fp64:
345+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
346+
"[SYCLcompat] 'double' is not supported in '" +
347+
dev.get_info<sycl::info::device::name>() +
348+
"' device");
349+
break;
350+
case sycl::aspect::fp16:
351+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
352+
"[SYCLcompat] 'half' is not supported in '" +
353+
dev.get_info<sycl::info::device::name>() +
354+
"' device");
355+
break;
356+
default:
357+
#define __SYCL_ASPECT(ASPECT, ID) \
358+
case sycl::aspect::ASPECT: \
359+
return #ASPECT;
360+
#define __SYCL_ASPECT_DEPRECATED(ASPECT, ID, MESSAGE) __SYCL_ASPECT(ASPECT, ID)
361+
#define __SYCL_ASPECT_DEPRECATED_ALIAS(ASPECT, ID, MESSAGE)
362+
auto getAspectNameStr = [](sycl::aspect AspectNum) -> std::string {
363+
switch (AspectNum) {
364+
#include <sycl/info/aspects.def>
365+
#include <sycl/info/aspects_deprecated.def>
366+
default:
367+
return "unknown aspect";
368+
}
369+
};
370+
#undef __SYCL_ASPECT_DEPRECATED_ALIAS
371+
#undef __SYCL_ASPECT_DEPRECATED
372+
#undef __SYCL_ASPECT
373+
throw sycl::exception(
374+
sycl::make_error_code(sycl::errc::runtime),
375+
"[SYCLcompat] '" + getAspectNameStr(it) + "' is not supported in '" +
376+
dev.get_info<sycl::info::device::name>() + "' device");
377+
}
378+
break;
379+
}
380+
}
381+
337382
/// device extension
338383
class device_ext : public sycl::device {
339384
public:
@@ -613,47 +658,7 @@ Use 64 bits as memory_bus_width default value."
613658
/// sycl::aspect.
614659
void has_capability_or_fail(
615660
const std::initializer_list<sycl::aspect> &props) const {
616-
for (const auto &it : props) {
617-
if (has(it))
618-
continue;
619-
switch (it) {
620-
case sycl::aspect::fp64:
621-
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
622-
"[SYCLcompat] 'double' is not supported in '" +
623-
get_info<sycl::info::device::name>() +
624-
"' device");
625-
break;
626-
case sycl::aspect::fp16:
627-
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
628-
"[SYCLcompat] 'half' is not supported in '" +
629-
get_info<sycl::info::device::name>() +
630-
"' device");
631-
break;
632-
default:
633-
#define __SYCL_ASPECT(ASPECT, ID) \
634-
case sycl::aspect::ASPECT: \
635-
return #ASPECT;
636-
#define __SYCL_ASPECT_DEPRECATED(ASPECT, ID, MESSAGE) __SYCL_ASPECT(ASPECT, ID)
637-
#define __SYCL_ASPECT_DEPRECATED_ALIAS(ASPECT, ID, MESSAGE)
638-
auto getAspectNameStr = [](sycl::aspect AspectNum) -> std::string {
639-
switch (AspectNum) {
640-
#include <sycl/info/aspects.def>
641-
#include <sycl/info/aspects_deprecated.def>
642-
default:
643-
return "unknown aspect";
644-
}
645-
};
646-
#undef __SYCL_ASPECT_DEPRECATED_ALIAS
647-
#undef __SYCL_ASPECT_DEPRECATED
648-
#undef __SYCL_ASPECT
649-
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
650-
"[SYCLcompat] '" + getAspectNameStr(it) +
651-
"' is not supported in '" +
652-
get_info<sycl::info::device::name>() +
653-
"' device");
654-
}
655-
break;
656-
}
661+
::syclcompat::has_capability_or_fail(*this, props);
657662
}
658663
659664
private:

0 commit comments

Comments
 (0)