41 const std::lock_guard<std::mutex> Guard(
42 GlobalHandler::instance().getPlatformMapMutex());
44 std::vector<PlatformImplPtr> &PlatformCache =
45 GlobalHandler::instance().getPlatformCache();
48 for (
const auto &PlatImpl : PlatformCache) {
54 Result = std::make_shared<platform_impl>(
PiPlatform, Plugin);
55 PlatformCache.emplace_back(Result);
66 sizeof(Plt), &Plt,
nullptr);
67 return getOrMakePlatformImpl(Plt, Plugin);
82 auto IsMatchingOpenCL = [](
platform Platform,
const std::string_view name) {
86 const bool HasNameMatch = Platform.
get_info<info::platform::name>().find(
87 name) != std::string::npos;
90 const bool IsMatchingOCL = (HasNameMatch && Backend == backend::opencl);
93 std::cout <<
"SYCL_PI_TRACE[all]: " << name
94 <<
" OpenCL platform found but is not compatible." << std::endl;
98 return IsMatchingOpenCL(Platform,
"NVIDIA CUDA") ||
99 IsMatchingOpenCL(Platform,
"AMD Accelerated Parallel Processing");
104 std::vector<platform> platform_impl::get_platforms() {
105 std::vector<platform> Platforms;
107 for (
plugin &Plugin : Plugins) {
112 0,
nullptr, &NumPlatforms) != PI_SUCCESS)
116 std::vector<RT::PiPlatform> PiPlatforms(NumPlatforms);
118 NumPlatforms, PiPlatforms.data(),
nullptr) != PI_SUCCESS)
122 platform Platform = detail::createSyclObjFromImpl<platform>(
130 std::lock_guard<std::mutex> Guard(*Plugin.getPluginMutex());
138 if (!Platform.
get_devices(info::device_type::all).empty()) {
139 Platforms.push_back(Platform);
153 GlobalHandler::registerDefaultContextReleaseHandler();
168 template <
typename ListT,
typename FilterT>
173 constexpr
bool is_ods_target = std::is_same_v<FilterT, ods_target>;
178 if constexpr (is_ods_target) {
186 std::sort(FilterList->get().begin(), FilterList->get().end(),
188 return filter1.IsNegativeTarget && !filter2.IsNegativeTarget;
196 std::map<int, bool> Blacklist;
199 std::vector<int> original_indices;
203 std::find_if(Plugins.begin(), Plugins.end(), [Platform](
plugin &Plugin) {
204 return Plugin.containsPiPlatform(Platform);
206 if (It == Plugins.end()) {
207 return original_indices;
220 &PiDevType,
nullptr);
225 for (
const FilterT &Filter : FilterList->get()) {
226 backend FilterBackend = Filter.Backend.value_or(backend::all);
228 if (FilterBackend == Backend || FilterBackend == backend::all) {
230 Filter.DeviceType.value_or(info::device_type::all);
232 if (FilterDevType == info::device_type::all) {
234 if (!Filter.DeviceNum || DeviceNum == Filter.DeviceNum.value()) {
235 if constexpr (is_ods_target) {
236 if (!Blacklist[DeviceNum]) {
237 if (!Filter.IsNegativeTarget) {
238 PiDevices[InsertIDx++] = Device;
239 original_indices.push_back(DeviceNum);
243 Blacklist[DeviceNum] =
true;
247 PiDevices[InsertIDx++] = Device;
248 original_indices.push_back(DeviceNum);
253 }
else if (FilterDevType == DeviceType) {
254 if (!Filter.DeviceNum || DeviceNum == Filter.DeviceNum.value()) {
255 if constexpr (is_ods_target) {
256 if (!Blacklist[DeviceNum]) {
257 if (!Filter.IsNegativeTarget) {
258 PiDevices[InsertIDx++] = Device;
259 original_indices.push_back(DeviceNum);
263 Blacklist[DeviceNum] =
true;
267 PiDevices[InsertIDx++] = Device;
268 original_indices.push_back(DeviceNum);
277 PiDevices.resize(InsertIDx);
282 return original_indices;
285 std::shared_ptr<device_impl>
287 const std::lock_guard<std::mutex> Guard(MDeviceMapMutex);
288 return getDeviceImplHelper(
PiDevice);
291 std::shared_ptr<device_impl> platform_impl::getOrMakeDeviceImpl(
293 const std::lock_guard<std::mutex> Guard(MDeviceMapMutex);
295 std::shared_ptr<device_impl> Result = getDeviceImplHelper(
PiDevice);
300 Result = std::make_shared<device_impl>(
PiDevice, PlatformImpl);
301 MDeviceCache.emplace_back(Result);
309 if (partitionProp != info::partition_property::partition_by_affinity_domain) {
312 auto supported = dev.
get_info<info::device::partition_affinity_domains>();
313 auto It = std::find(std::begin(supported), std::end(supported), domain);
314 return It != std::end(supported);
319 auto supported = dev.
get_info<info::device::partition_properties>();
321 std::find(std::begin(supported), std::end(supported), partitionProp);
322 return It != std::end(supported);
326 backend PlatformBackend, std::vector<device> &DeviceList,
327 ods_target_list *OdsTargetList,
const std::vector<int> &original_indices,
330 info::partition_property::partition_by_affinity_domain;
332 info::partition_affinity_domain::next_partitionable;
334 std::vector<device> FinalResult;
339 for (
unsigned i = 0; i < DeviceList.size(); i++) {
342 device &dev = DeviceList[i];
343 bool deviceAdded =
false;
345 backend TargetBackend =
target.Backend.value_or(backend::all);
346 if (PlatformBackend == TargetBackend || TargetBackend == backend::all) {
347 bool deviceMatch =
target.HasDeviceWildCard;
349 deviceMatch = ((
target.DeviceType == info::device_type::all) ||
353 }
else if (
target.DeviceNum) {
354 deviceMatch = (
target.DeviceNum.value() == original_indices[i]);
361 bool supportsSubPartitioning =
364 bool wantSubSubDevice =
365 target.SubSubDeviceNum ||
target.HasSubSubDeviceWildCard;
368 if (!wantSubDevice) {
370 FinalResult.push_back(dev);
374 if (!supportsSubPartitioning) {
377 (
target.DeviceType.value() != info::device_type::all))) {
386 if (wantSubSubDevice) {
388 auto subDevicesToPartition =
390 if (
target.SubDeviceNum) {
391 if (subDevicesToPartition.size() >
392 target.SubDeviceNum.value()) {
393 subDevicesToPartition[0] =
394 subDevicesToPartition[
target.SubDeviceNum.value()];
395 subDevicesToPartition.resize(1);
402 for (
device subDev : subDevicesToPartition) {
403 bool supportsSubSubPartitioning =
407 if (!supportsSubSubPartitioning) {
408 if (
target.SubDeviceNum) {
418 subDev.create_sub_devices<partitionProperty>(
420 if (
target.HasSubSubDeviceWildCard) {
421 FinalResult.insert(FinalResult.end(), subSubDevices.begin(),
422 subSubDevices.end());
424 if (subSubDevices.size() >
target.SubSubDeviceNum.value()) {
425 FinalResult.push_back(
426 subSubDevices[
target.SubSubDeviceNum.value()]);
429 <<
"sub-sub-device index out of bounds: " <<
target
434 }
else if (wantSubDevice) {
436 info::partition_property::partition_by_affinity_domain>(
438 if (
target.HasSubDeviceWildCard) {
439 FinalResult.insert(FinalResult.end(), subDevices.begin(),
442 if (subDevices.size() >
target.SubDeviceNum.value()) {
443 FinalResult.push_back(
444 subDevices[
target.SubDeviceNum.value()]);
461 std::vector<device> Res;
467 if (is_host() && (DeviceType == info::device_type::host ||
468 DeviceType == info::device_type::all)) {
470 createSyclObjFromImpl<device>(device_impl::getHostDeviceImpl()));
475 if (is_host() || DeviceType == info::device_type::host)
481 MPlatform, pi::cast<RT::PiDeviceType>(DeviceType),
483 pi::cast<RT::PiDevice *>(
nullptr), &NumDevices);
486 if (NumDevices == 0) {
493 auto It = std::find_if(Plugins.begin(), Plugins.end(),
494 [&Platform = MPlatform](
plugin &Plugin) {
495 return Plugin.containsPiPlatform(Platform);
497 if (It != Plugins.end()) {
498 std::lock_guard<std::mutex> Guard(*(It->getPluginMutex()));
499 (*It).adjustLastDeviceId(MPlatform);
504 std::vector<RT::PiDevice> PiDevices(NumDevices);
508 pi::cast<RT::PiDeviceType>(DeviceType),
509 NumDevices, PiDevices.data(),
nullptr);
513 std::vector<RT::PiDevice> PiDevicesToCleanUp = PiDevices;
522 std::vector<int> PlatformDeviceIndices;
526 "ONEAPI_DEVICE_SELECTOR cannot be used in "
527 "conjunction with SYCL_DEVICE_FILTER");
529 PlatformDeviceIndices = filterDeviceFilter<ods_target_list, ods_target>(
530 PiDevices, MPlatform, OdsTargetList);
531 }
else if (FilterList) {
532 PlatformDeviceIndices =
533 filterDeviceFilter<device_filter_list, device_filter>(
534 PiDevices, MPlatform, FilterList);
539 PlatformImplPtr PlatformImpl = getOrMakePlatformImpl(MPlatform, Plugin);
541 PiDevices.begin(), PiDevices.end(), std::back_inserter(Res),
543 return detail::createSyclObjFromImpl<device>(
544 PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl));
555 if (!OdsTargetList || Res.size() == 0)
561 PlatformDeviceIndices, PlatformImpl);
571 return (AllExtensionNames.find(ExtensionName) != std::string::npos);
581 template <
typename Param>
582 typename Param::return_type platform_impl::get_info()
const {
584 return get_platform_info_host<Param>();
586 return get_platform_info<Param>(this->getHandleRef(),
getPlugin());
590 bool platform_impl::has(aspect Aspect)
const {
591 for (
const auto &dev : get_devices()) {
592 if (dev.has(Aspect) ==
false) {
599 std::shared_ptr<device_impl>
601 for (
const std::weak_ptr<device_impl> &DeviceWP : MDeviceCache) {
602 if (std::shared_ptr<device_impl> Device = DeviceWP.lock()) {
603 if (Device->getHandleRef() ==
PiDevice)
610 #define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, PiCode) \
611 template ReturnT platform_impl::get_info<info::platform::Desc>() const;
613 #include <sycl/info/platform_traits.def>
614 #undef __SYCL_PARAM_TRAITS_SPEC