-
Notifications
You must be signed in to change notification settings - Fork 831
[SYCL] Support sycl_ext_oneapi_platform_device_index #20758
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
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e1d8231
[SYCL] Support sycl_ext_oneapi_platform_device_index
KornevNikita 0354e53
Merge remote-tracking branch 'origin/sycl' into platform_device_index
KornevNikita 79248bc
fix abi
KornevNikita 2c79158
fix-win-precommit
KornevNikita 9b73244
fix precommit
KornevNikita 258fc0a
fix precommit 2
KornevNikita a467901
apply-suggestions
KornevNikita 7e68af9
fix-ctor
KornevNikita d3d4fa9
format
KornevNikita 32e9033
fix-test
KornevNikita 711670d
apply suggestions
KornevNikita c573f94
fix-exception
KornevNikita c9b7b92
Merge remote-tracking branch 'origin/sycl' into platform_device_index
KornevNikita 07a747c
add-missing-const
KornevNikita f91d874
apply suggestions
KornevNikita 8ec2fb3
Try another solution
KornevNikita 6832f27
Merge remote-tracking branch 'origin/sycl' into platform_device_index
KornevNikita 312a2b4
format
KornevNikita fc82542
Merge remote-tracking branch 'origin/sycl' into platform_device_index
KornevNikita File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
sycl/test-e2e/Basic/sycl_ext_oneapi_platform_device_index.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // RUN: %{build} %level_zero_options %opencl_lib -o %t.out | ||
| // RUN: %{run} %t.out | ||
|
|
||
| #include "../helpers.hpp" | ||
|
|
||
| #include <level_zero/ze_api.h> | ||
| #include <sycl/detail/core.hpp> | ||
| #include <sycl/ext/oneapi/backend/level_zero.hpp> | ||
| #include <sycl/platform.hpp> | ||
|
|
||
| #include <algorithm> | ||
| #include <cassert> | ||
| #include <cstdlib> | ||
| #include <iostream> | ||
| #include <iterator> | ||
| #include <vector> | ||
|
|
||
| int main() { | ||
| sycl::device orig_dev; | ||
| auto plt = orig_dev.get_platform(); | ||
| auto devices = plt.get_devices(); | ||
| auto it = std::find(devices.begin(), devices.end(), orig_dev); | ||
| auto orig_dev_index_within_plt = std::distance(devices.begin(), it); | ||
|
|
||
| // ext_oneapi_index_within_platform | ||
| size_t ext_oneapi_index_within_platform = | ||
| orig_dev.ext_oneapi_index_within_platform(); | ||
|
|
||
| // sycl_ext_oneapi_platform_device_index guarantees: | ||
| // The device index returned from device::ext_oneapi_index_within_platform is | ||
| // compatible with the index of the underlying backend device when the | ||
| // ONEAPI_DEVICE_SELECTOR environment variable is not set. | ||
| // | ||
| // When the platform’s backend is backend::ext_oneapi_level_zero, the index | ||
| // returned from device::ext_oneapi_index_within_platform matches the index of | ||
| // the device’s underlying ze_device_handle_t within the list of handles | ||
| // returned from zeDeviceGet. | ||
|
|
||
| // When the platform’s backend is backend::opencl, the index returned from | ||
| // device::ext_oneapi_index_within_platform matches the index of the device’s | ||
| // underlying cl_device_id within the list of IDs returned from | ||
| // clGetDeviceIDs. | ||
|
|
||
| // Check if the index matches the index of the device’s underlying handle | ||
| // within the list of handles returned from zeDeviceGet/clGetDeviceIDs. | ||
| std::string selector = env::getVal("ONEAPI_DEVICE_SELECTOR"); | ||
| if (!selector.empty()) { | ||
| assert(orig_dev_index_within_plt == ext_oneapi_index_within_platform && | ||
| "The index returned from device::ext_oneapi_index_within_platform " | ||
| "doesn't match the index that the device has in the std::vector " | ||
| "that is returned when calling platform::get_devices() on the " | ||
| "platform that contains this device"); | ||
| } else { | ||
| if (orig_dev.get_backend() == sycl::backend::ext_oneapi_level_zero) { | ||
| auto l0_plt = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(plt); | ||
| auto l0_dev = | ||
| sycl::get_native<sycl::backend::ext_oneapi_level_zero>(orig_dev); | ||
|
|
||
| uint32_t num_devices = 0; | ||
| zeDeviceGet(l0_plt, &num_devices, nullptr); | ||
|
|
||
| std::vector<ze_device_handle_t> l0_devices(num_devices); | ||
| zeDeviceGet(l0_plt, &num_devices, l0_devices.data()); | ||
|
|
||
| auto it = std::find(l0_devices.begin(), l0_devices.end(), l0_dev); | ||
| assert(ext_oneapi_index_within_platform == | ||
| (std::distance(l0_devices.begin(), it)) && | ||
| "The index returned from device::ext_oneapi_index_within_platform " | ||
| "doesn't match the index of the device’s underlying cl_device_id " | ||
| "within the list of IDs returned from clGetDeviceIDs"); | ||
|
|
||
| } else if (orig_dev.get_backend() == sycl::backend::opencl) { | ||
| auto cl_plt = sycl::get_native<sycl::backend::opencl>(plt); | ||
| auto cl_dev = sycl::get_native<sycl::backend::opencl>(orig_dev); | ||
|
|
||
| cl_uint num_devices = 0; | ||
| clGetDeviceIDs(cl_plt, CL_DEVICE_TYPE_ALL, 0, nullptr, &num_devices); | ||
|
|
||
| std::vector<cl_device_id> cl_devices(num_devices); | ||
| clGetDeviceIDs(cl_plt, CL_DEVICE_TYPE_ALL, num_devices, cl_devices.data(), | ||
| nullptr); | ||
|
|
||
| auto it = std::find(cl_devices.begin(), cl_devices.end(), cl_dev); | ||
| assert(orig_dev_index_within_plt == | ||
| (std::distance(cl_devices.begin(), it)) && | ||
| "The index returned from device::ext_oneapi_index_within_platform " | ||
| "doesn't match the index of the device’s underlying cl_device_id " | ||
| "within the list of IDs returned from clGetDeviceIDs"); | ||
| } | ||
| } | ||
| // Test non-root device exception (if partition is supported) | ||
| auto partition_properties = | ||
| orig_dev.get_info<sycl::info::device::partition_properties>(); | ||
| if (std::find(partition_properties.begin(), partition_properties.end(), | ||
| sycl::info::partition_property::partition_equally) != | ||
| partition_properties.end()) { | ||
| std::vector<sycl::device> sub_devices = orig_dev.create_sub_devices< | ||
| sycl::info::partition_property::partition_equally>(2); | ||
| try { | ||
| sub_devices[0].ext_oneapi_index_within_platform(); | ||
| assert(false && "Missing an exception"); | ||
| } catch (sycl::exception &e) { | ||
| std::cout << e.what() << std::endl; | ||
| } | ||
| } | ||
|
|
||
| // ext_oneapi_device_at_index | ||
| auto ext_oneapi_device_at_index = plt.ext_oneapi_device_at_index( | ||
| static_cast<size_t>(orig_dev_index_within_plt)); | ||
| assert(orig_dev == ext_oneapi_device_at_index && | ||
| "A copy of the device object which has that index doesn't match the " | ||
| "original device"); | ||
| // Test out-of-range exception | ||
| try { | ||
| plt.ext_oneapi_device_at_index(devices.size()); | ||
| assert(false && "Missing an exception"); | ||
| } catch (sycl::exception &e) { | ||
| std::cout << e.what() << std::endl; | ||
| } | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| add_sycl_unittest(DeviceIndexExtensionTests OBJECT | ||
| DeviceIndex.cpp | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| //==- DeviceIndex.cpp -- sycl_ext_oneapi_platform_device_index unit tests --==// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <sycl/sycl.hpp> | ||
|
|
||
| #include <helpers/UrMock.hpp> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| namespace { | ||
| const auto DEVICE1 = reinterpret_cast<ur_device_handle_t>(1u); | ||
| const auto DEVICE2 = reinterpret_cast<ur_device_handle_t>(2u); | ||
| const auto DEVICE3 = reinterpret_cast<ur_device_handle_t>(3u); | ||
|
|
||
| ur_result_t redefine_urDeviceGet(void *pParams) { | ||
| auto params = *static_cast<ur_device_get_params_t *>(pParams); | ||
| if (*params.ppNumDevices) | ||
| **params.ppNumDevices = 3; | ||
| if (*params.pphDevices && *params.pNumEntries > 0) { | ||
| (*params.pphDevices)[0] = DEVICE1; | ||
| (*params.pphDevices)[1] = DEVICE2; | ||
| (*params.pphDevices)[2] = DEVICE3; | ||
| } | ||
| return UR_RESULT_SUCCESS; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(sycl_ext_oneapi_platform_device_index, CheckDeviceIndexes) { | ||
| sycl::unittest::UrMock<> Mock; | ||
| mock::getCallbacks().set_replace_callback("urDeviceGet", | ||
| &redefine_urDeviceGet); | ||
|
|
||
| sycl::platform plt = sycl::platform(); | ||
| auto devs = plt.get_devices(); | ||
|
|
||
| ASSERT_EQ(devs.size(), 3ull); | ||
|
|
||
| for (size_t i = 0; i < devs.size(); i++) | ||
| ASSERT_EQ(devs[i].ext_oneapi_index_within_platform(), i); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.