Repository navigation
[UR][HIP] Opt into the larger per-block shared-memory pool #23419
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
Open
zjin-lcf
wants to merge
5
commits into
intel:sycl
Choose a base branch
from
zjin-lcf:hip-local-mem-optin
base: sycl
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
502395c
[UR][HIP] Opt into the larger per-block shared-memory pool
zjin-lcf 19db928
[SYCL][E2E] Test HIP opt-in local memory launches
zjin-lcf 475f087
[UR][HIP] Address opt-in local memory review
zjin-lcf cc63e0a
[SYCL][E2E] Skip HIP local memory test on Windows
zjin-lcf f0f665b
[SYCL][E2E] Explain why the HIP local-memory test skips Windows
zjin-lcf 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| // REQUIRES: hip, hip_dev_kit | ||
| // UNSUPPORTED: windows | ||
| // UNSUPPORTED-INTENDED: %{build} targets spir64, so --rocm-path from | ||
| // %hip_options is an unused argument and fails under -Werror. | ||
|
|
||
| // RUN: %{build} -o %t.out %hip_options | ||
| // The opt-in launch is skipped when the opt-in ceiling equals the non-opt-in | ||
| // limit. Otherwise it uses one byte more than that limit. | ||
| // RUN: env SYCL_DEVICE_FILTER=hip %{run} %t.out | ||
| // A cap below the device ceiling lowers the query and rejects a larger launch. | ||
| // RUN: env SYCL_DEVICE_FILTER=hip UR_HIP_MAX_LOCAL_MEM_SIZE=32768 %{run} %t.out | ||
|
|
||
| //==------------------------ hip-local-mem-optin.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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // Dynamic local memory above the non-opt-in per-block limit has to opt in with | ||
| // hipFuncAttributeMaxDynamicSharedMemorySize. AMD devices report the same | ||
| // ceiling for both attributes (64KiB LDS on gfx942), so that launch is skipped | ||
| // there. The test still checks the reported ceiling, a launch of that size, | ||
| // and rejection of one byte more. UR_HIP_MAX_LOCAL_MEM_SIZE caps both. | ||
|
|
||
| #include <sycl/detail/core.hpp> | ||
| #include <sycl/ext/intel/info/device.hpp> | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdio> | ||
| #include <cstdlib> | ||
| #include <string> | ||
|
|
||
| #if !defined(__HIP_PLATFORM_AMD__) && !defined(__HIP_PLATFORM_NVIDIA__) | ||
| #define __HIP_PLATFORM_AMD__ | ||
| #endif | ||
| #include <hip/hip_runtime_api.h> | ||
|
|
||
| namespace { | ||
|
|
||
| int queryAttribute(int Device, hipDeviceAttribute_t Attr, bool *Ok) { | ||
| int Value = 0; | ||
| hipError_t Err = hipDeviceGetAttribute(&Value, Attr, Device); | ||
| if (Err != hipSuccess) { | ||
| (void)hipGetLastError(); | ||
| *Ok = false; | ||
| return 0; | ||
| } | ||
| *Ok = true; | ||
| return Value; | ||
| } | ||
|
|
||
| bool launchAndCheck(sycl::queue &Q, size_t Bytes) { | ||
| sycl::buffer<unsigned char, 1> Out{sycl::range<1>(2)}; | ||
| Q.submit([&](sycl::handler &CGH) { | ||
| sycl::accessor OutAcc{Out, CGH, sycl::write_only}; | ||
| sycl::local_accessor<unsigned char, 1> Local{Bytes, CGH}; | ||
| CGH.parallel_for(sycl::nd_range<1>{1, 1}, [=](sycl::nd_item<1>) { | ||
| Local[0] = 0xA5; | ||
| Local[Bytes - 1] = 0x5A; | ||
| OutAcc[0] = Local[0]; | ||
| OutAcc[1] = Local[Bytes - 1]; | ||
| }); | ||
| }).wait(); | ||
| sycl::host_accessor Host{Out, sycl::read_only}; | ||
| if (Host[0] != 0xA5 || Host[1] != 0x5A) { | ||
| std::printf("launch %zu read back %u %u\n", Bytes, Host[0], Host[1]); | ||
| return false; | ||
| } | ||
| std::printf("launch %zu OK\n", Bytes); | ||
| return true; | ||
| } | ||
|
|
||
| bool expectReject(sycl::queue &Q, size_t Bytes, const char *Expected) { | ||
| try { | ||
| launchAndCheck(Q, Bytes); | ||
| } catch (const std::exception &E) { | ||
| if (std::string(E.what()).find(Expected) == std::string::npos) { | ||
| std::printf("launch %zu rejected with unexpected message: %s\n", Bytes, | ||
| E.what()); | ||
| return false; | ||
| } | ||
| std::printf("launch %zu rejected as expected\n", Bytes); | ||
| return true; | ||
| } | ||
| std::printf("launch %zu was expected to fail\n", Bytes); | ||
| return false; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| int main() { | ||
| sycl::queue Q; | ||
| const std::string Pci = | ||
| Q.get_device().get_info<sycl::ext::intel::info::device::pci_address>(); | ||
| int DeviceCount = 0; | ||
| if (hipGetDeviceCount(&DeviceCount) != hipSuccess || DeviceCount <= 0) { | ||
| std::printf("failed to enumerate HIP devices\n"); | ||
| return 1; | ||
| } | ||
| int HipDevice = -1; | ||
| for (int Index = 0; Index < DeviceCount; ++Index) { | ||
| char BusId[32] = {}; | ||
| if (hipDeviceGetPCIBusId(BusId, sizeof(BusId), Index) == hipSuccess && | ||
| Pci == BusId) { | ||
| HipDevice = Index; | ||
| break; | ||
| } | ||
| } | ||
| if (HipDevice < 0) { | ||
| std::printf("no HIP device matches PCI address %s\n", Pci.c_str()); | ||
| return 1; | ||
| } | ||
|
|
||
| bool DefaultOk = false; | ||
| const int Default = queryAttribute( | ||
| HipDevice, hipDeviceAttributeMaxSharedMemoryPerBlock, &DefaultOk); | ||
| if (!DefaultOk || Default <= 0) { | ||
| std::printf("failed to query non-opt-in shared memory on HIP device %d\n", | ||
| HipDevice); | ||
| return 1; | ||
| } | ||
|
|
||
| bool OptinOk = false; | ||
| int Optin = queryAttribute( | ||
| HipDevice, hipDeviceAttributeSharedMemPerBlockOptin, &OptinOk); | ||
| // Match the adapter: a rejected or non-positive opt-in query keeps the | ||
| // non-opt-in size. | ||
| if (!OptinOk || Optin <= 0) | ||
| Optin = Default; | ||
| const int Ceiling = std::max(Default, Optin); | ||
|
|
||
| const auto Reported = static_cast<size_t>( | ||
| Q.get_device().get_info<sycl::info::device::local_mem_size>()); | ||
|
|
||
| const char *CapEnv = std::getenv("UR_HIP_MAX_LOCAL_MEM_SIZE"); | ||
| size_t Expected = static_cast<size_t>(Ceiling); | ||
| if (CapEnv) { | ||
| const int Cap = std::atoi(CapEnv); | ||
| if (Cap <= 0) { | ||
| std::printf("UR_HIP_MAX_LOCAL_MEM_SIZE is not a positive size\n"); | ||
| return 1; | ||
| } | ||
| Expected = static_cast<size_t>(std::min(Cap, Ceiling)); | ||
| } | ||
|
|
||
| std::printf("device %d default %d opt-in %d ceiling %d reported %zu\n", | ||
| HipDevice, Default, Optin, Ceiling, Reported); | ||
| if (Reported != Expected) { | ||
| std::printf("local_mem_size %zu, expected %zu\n", Reported, Expected); | ||
| return 1; | ||
| } | ||
|
|
||
| if (Reported == 0 || !launchAndCheck(Q, Reported)) | ||
| return 1; | ||
|
|
||
| const char *Reject = | ||
| (Reported + 1 > static_cast<size_t>(Ceiling)) | ||
| ? "Excessive allocation of local memory on the device" | ||
| : "UR_HIP_MAX_LOCAL_MEM_SIZE"; | ||
| if (!expectReject(Q, Reported + 1, Reject)) | ||
| return 1; | ||
|
|
||
| // One byte past the non-opt-in limit is the hipFuncSetAttribute path. It is | ||
| // unreachable when the ceiling equals that limit (gfx942 and the other AMD | ||
| // ISAs) or when the env cap keeps the launch inside the default pool. | ||
| const size_t OptInBytes = static_cast<size_t>(Default) + 1; | ||
| if (Ceiling > Default && OptInBytes <= Reported && OptInBytes != Reported) { | ||
| if (!launchAndCheck(Q, OptInBytes)) | ||
| return 1; | ||
| std::printf("opt-in launch %zu OK\n", OptInBytes); | ||
| } else if (Ceiling > Default && OptInBytes == Reported) { | ||
| std::printf("opt-in launch covered by the %zu-byte launch\n", Reported); | ||
| } else { | ||
| std::printf("skipped opt-in launch: ceiling %d default %d reported %zu\n", | ||
| Ceiling, Default, Reported); | ||
| } | ||
| 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
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
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.