Skip to content

Commit 65dc2df

Browse files
authored
[sycl-rel] Cherry-pick patches for sycl_ext_intel_device_info (intel#18886)
Cherry-pick commits that reached the internal branch between intel/llvm cutoff and release branch pulldown. Patches included: --- [SYCL][Doc] Add new device descriptors to sycl_ext_intel_device_info extension (intel#17386) Patch-by: Artur Gainullin <[email protected]> --- [SYCL][CUDA] Add implementation of new device descriptors (intel#17590) Tests were not added because there are existing conformance tests which cover this functionality. Patch-by: Artur Gainullin <[email protected]> --- [UR][CUDA] Use different throttle reasons API based on CUDA version (intel#17719) Our pre-commit CI uses CUDA 12.6 but nightly uses CUDA 12.1, it turns out nvml which is part of CUDA 12.6 has nvmlDeviceGetCurrentClocksEventReasons API, but nvml which is part of CUDA 12.1 doesn't have it, but only supports older deprecated nvmlDeviceGetCurrentClocksThrottleReasons. NVML doesn't provide a version macro to check the support for that API, so use new API nvmlDeviceGetCurrentClocksEventReasons based on cuda version. Patch-by: Artur Gainullin <[email protected]>
1 parent 38818a4 commit 65dc2df

File tree

8 files changed

+287
-6
lines changed

8 files changed

+287
-6
lines changed

sycl/doc/extensions/supported/sycl_ext_intel_device_info.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The Feature Test Macro SYCL\_EXT\_INTEL\_DEVICE\_INFO will be defined as one of
1919
| 4 | Free device memory query is supported |
2020
| 5 | Device ID is supported |
2121
| 6 | Memory clock rate and bus width queries are supported |
22+
| 7 | Throttle reasons, fan speed and power limits queries are supported |
2223

2324

2425

@@ -489,6 +490,144 @@ Then the memory bus width can be obtained using the standard get\_info() interfa
489490
auto MemoryBusWidth = dev.get_info<ext::intel::info::device::memory_bus_width>();
490491
}
491492

493+
# Throttle reason #
494+
495+
A new device descriptor is added which provides the current clock throttle reasons.
496+
A new enum is added with the list of possible throttle reasons.
497+
498+
## Version ##
499+
500+
The extension supports this query in version 7 and later.
501+
502+
## Throttle reasons ##
503+
504+
| Reason | Description |
505+
| ------------------ | ----------- |
506+
| `power_cap` | The clock frequency is throttled due to hitting the power limit. |
507+
| `current_limit` | The clock frequency is throttled due to hitting the current limit. |
508+
| `thermal_limit` | The clock frequency is throttled due to hitting the thermal limit. |
509+
| `psu_alert` | The clock frequency is throttled due to power supply assertion. |
510+
| `sw_range` | The clock frequency is throttled due to software supplied frequency range. |
511+
| `hw_range` | The clock frequency is throttled because there is a sub block that has a lower frequency when it receives clocks. |
512+
| `other` | The clock frequency is throttled due to other reason. |
513+
514+
515+
```
516+
namespace sycl::ext::intel {
517+
518+
enum class throttle_reason {
519+
power_cap,
520+
current_limit,
521+
thermal_limit,
522+
psu_alert,
523+
sw_range,
524+
hw_range,
525+
other
526+
}
527+
528+
}
529+
```
530+
531+
## Device Information Descriptors ##
532+
533+
| Device Descriptors | Return Type | Description |
534+
| ------------------ | ----------- | ----------- |
535+
| `ext::intel::info::device::current_clock_throttle_reasons` | `std::vector<ext::intel::throttle_reason>` | Returns the set of throttle reasons describing why the frequency is being limited by the hardware. Returns empty set if frequency is not throttled. |
536+
537+
538+
## Aspects ##
539+
540+
A new aspect, `ext_intel_current_clock_throttle_reasons`, is added.
541+
542+
543+
## Error Condition ##
544+
545+
Throws a synchronous `exception` with the `errc::feature_not_supported` error code if the device does not have `aspect::ext_intel_current_clock_throttle_reasons`.
546+
547+
## Example Usage ##
548+
549+
Then the current clock throttle reasons can be obtained using the standard `get_info()` interface.
550+
551+
```
552+
if (dev.has(aspect::ext_intel_current_clock_throttle_reasons)) {
553+
std::vector<ext::inte::info::throttle_reason> Reasons = dev.get_info<ext::intel::info::device::current_clock_throttle_reasons<>();
554+
}
555+
```
556+
557+
558+
# Fan speed #
559+
560+
A new device descriptor is added which provides the fan speed for the device.
561+
562+
## Version ##
563+
564+
The extension supports this query in version 7 and later.
565+
566+
## Device Information Descriptors ##
567+
568+
| Device Descriptors | Return Type | Description |
569+
| ------------------ | ----------- | ----------- |
570+
| `ext::intel::info::device::fan_speed` | `int32_t` | Returns the current speed of device's fan (as a percentage of the maximum speed of the fan). If fan speed can't be measured then returns -1. If there are multiple fans, then returns maximum value. |
571+
572+
573+
## Aspects ##
574+
575+
A new aspect, `ext_intel_fan_speed`, is added.
576+
577+
578+
## Error Condition ##
579+
580+
Throws a synchronous `exception` with the `errc::feature_not_supported` error code if the device does not have `aspect::ext_intel_fan_speed`.
581+
582+
## Example Usage ##
583+
584+
Then the fan speed can be obtained using the standard `get_info()` interface.
585+
586+
```
587+
if (dev.has(aspect::ext_intel_fan_speed)) {
588+
auto FanSpeed = dev.get_info<ext::intel::info::device::fan_speed>();
589+
}
590+
```
591+
592+
# Power limits #
593+
594+
New device descriptors are added which provide the maximum and minimum power limits for the device.
595+
596+
## Version ##
597+
598+
The extension supports this query in version 7 and later.
599+
600+
## Device Information Descriptors ##
601+
602+
| Device Descriptors | Return Type | Description |
603+
| ------------------ | ----------- | ----------- |
604+
|`ext::intel::info::device::min_power_limit` |`int32_t` | Returns the minimum power limit of the device in milliwatts. Returns -1 if the limit is not known. |
605+
|`ext::intel::info::device::max_power_limit` |`int32_t` | Returns the maximum power limit of the device in milliwatts. Returns -1 if the limit is not known. |
606+
607+
608+
## Aspects ##
609+
610+
A new aspect, `ext_intel_power_limits`, is added.
611+
612+
613+
## Error Condition ##
614+
615+
Throws a synchronous `exception` with the `errc::feature_not_supported` error code if the device does not have `aspect::ext_intel_power_limits`.
616+
617+
## Example Usage ##
618+
619+
Then the power limits can be obtained using the standard `get_info()` interface.
620+
621+
```
622+
if (dev.has(aspect::ext_intel_power_limits)) {
623+
auto Min = dev.get_info<ext::intel::info::device::min_power_limit>();
624+
auto Max = dev.get_info<ext::intel::info::device::max_power_limit>();
625+
}
626+
```
627+
628+
629+
630+
492631
# Deprecated queries #
493632

494633
The table below lists deprecated, that would soon be removed and their replacements:

unified-runtime/source/adapters/cuda/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ target_link_libraries(${TARGET_NAME} PRIVATE
119119
${PROJECT_NAME}::umf
120120
Threads::Threads
121121
cudadrv
122+
CUDA::nvml
122123
)
123124

124125
target_include_directories(${TARGET_NAME} PRIVATE

unified-runtime/source/adapters/cuda/common.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "logger/ur_logger.hpp"
1313

1414
#include <cuda.h>
15+
#include <nvml.h>
1516

1617
#include <sstream>
1718

@@ -36,6 +37,23 @@ ur_result_t mapErrorUR(CUresult Result) {
3637
}
3738
}
3839

40+
ur_result_t mapErrorUR(nvmlReturn_t Result) {
41+
switch (Result) {
42+
case NVML_SUCCESS:
43+
return UR_RESULT_SUCCESS;
44+
case NVML_ERROR_NOT_SUPPORTED:
45+
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
46+
case NVML_ERROR_GPU_IS_LOST:
47+
return UR_RESULT_ERROR_DEVICE_LOST;
48+
case NVML_ERROR_MEMORY:
49+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
50+
case NVML_ERROR_INSUFFICIENT_RESOURCES:
51+
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
52+
default:
53+
return UR_RESULT_ERROR_UNKNOWN;
54+
}
55+
}
56+
3957
void checkErrorUR(CUresult Result, const char *Function, int Line,
4058
const char *File) {
4159
if (Result == CUDA_SUCCESS || Result == CUDA_ERROR_DEINITIALIZED) {
@@ -63,6 +81,30 @@ void checkErrorUR(CUresult Result, const char *Function, int Line,
6381
throw mapErrorUR(Result);
6482
}
6583

84+
void checkErrorUR(nvmlReturn_t Result, const char *Function, int Line,
85+
const char *File) {
86+
if (Result == NVML_SUCCESS) {
87+
return;
88+
}
89+
90+
const char *ErrorString = nullptr;
91+
ErrorString = nvmlErrorString(Result);
92+
std::stringstream SS;
93+
SS << "\nUR NVML ERROR:"
94+
<< "\n\tValue: " << Result
95+
<< "\n\tDescription: " << ErrorString
96+
<< "\n\tFunction: " << Function << "\n\tSource Location: " << File
97+
<< ":" << Line << "\n";
98+
logger::error("{}", SS.str());
99+
100+
if (std::getenv("PI_CUDA_ABORT") != nullptr ||
101+
std::getenv("UR_CUDA_ABORT") != nullptr) {
102+
std::abort();
103+
}
104+
105+
throw mapErrorUR(Result);
106+
}
107+
66108
void checkErrorUR(ur_result_t Result, const char *Function, int Line,
67109
const char *File) {
68110
if (Result == UR_RESULT_SUCCESS) {

unified-runtime/source/adapters/cuda/common.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#pragma once
1111

1212
#include <cuda.h>
13+
#include <nvml.h>
1314
#include <ur/ur.hpp>
1415

1516
#include <umf/base.h>
@@ -35,6 +36,9 @@ ur_result_t mapErrorUR(CUresult Result);
3536
void checkErrorUR(CUresult Result, const char *Function, int Line,
3637
const char *File);
3738

39+
void checkErrorUR(nvmlReturn_t Result, const char *Function, int Line,
40+
const char *File);
41+
3842
void checkErrorUR(ur_result_t Result, const char *Function, int Line,
3943
const char *File);
4044

unified-runtime/source/adapters/cuda/device.cpp

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "logger/ur_logger.hpp"
1919
#include "platform.hpp"
2020
#include "ur_util.hpp"
21+
#include <nvml.h>
2122

2223
int getAttribute(ur_device_handle_t device, CUdevice_attribute attribute) {
2324
int value;
@@ -1085,11 +1086,69 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
10851086
case UR_DEVICE_INFO_GPU_EU_COUNT_PER_SUBSLICE:
10861087
case UR_DEVICE_INFO_GPU_HW_THREADS_PER_EU:
10871088
case UR_DEVICE_INFO_IP_VERSION:
1088-
case UR_DEVICE_INFO_CURRENT_CLOCK_THROTTLE_REASONS:
1089-
case UR_DEVICE_INFO_FAN_SPEED:
1090-
case UR_DEVICE_INFO_MIN_POWER_LIMIT:
1091-
case UR_DEVICE_INFO_MAX_POWER_LIMIT:
10921089
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
1090+
case UR_DEVICE_INFO_CURRENT_CLOCK_THROTTLE_REASONS: {
1091+
unsigned long long ClocksEventReasons;
1092+
#if (CUDA_VERSION >= 12060)
1093+
UR_CHECK_ERROR(nvmlDeviceGetCurrentClocksEventReasons(hDevice->getNVML(),
1094+
&ClocksEventReasons));
1095+
#else
1096+
UR_CHECK_ERROR(nvmlDeviceGetCurrentClocksThrottleReasons(
1097+
hDevice->getNVML(), &ClocksEventReasons));
1098+
#endif
1099+
ur_device_throttle_reasons_flags_t ThrottleReasons = 0;
1100+
constexpr unsigned long long NVMLThrottleFlags[] = {
1101+
nvmlClocksThrottleReasonSwPowerCap,
1102+
nvmlClocksThrottleReasonHwThermalSlowdown ||
1103+
nvmlClocksThrottleReasonSwThermalSlowdown,
1104+
nvmlClocksThrottleReasonHwPowerBrakeSlowdown,
1105+
nvmlClocksThrottleReasonApplicationsClocksSetting};
1106+
1107+
constexpr ur_device_throttle_reasons_flags_t UrThrottleFlags[] = {
1108+
UR_DEVICE_THROTTLE_REASONS_FLAG_POWER_CAP,
1109+
UR_DEVICE_THROTTLE_REASONS_FLAG_THERMAL_LIMIT,
1110+
UR_DEVICE_THROTTLE_REASONS_FLAG_PSU_ALERT,
1111+
UR_DEVICE_THROTTLE_REASONS_FLAG_SW_RANGE};
1112+
1113+
for (size_t i = 0;
1114+
i < sizeof(NVMLThrottleFlags) / sizeof(NVMLThrottleFlags[0]); ++i) {
1115+
if (ClocksEventReasons & NVMLThrottleFlags[i]) {
1116+
ThrottleReasons |= UrThrottleFlags[i];
1117+
ClocksEventReasons &= ~NVMLThrottleFlags[i];
1118+
}
1119+
}
1120+
if (ClocksEventReasons) {
1121+
ThrottleReasons |= UR_DEVICE_THROTTLE_REASONS_FLAG_OTHER;
1122+
}
1123+
return ReturnValue(ThrottleReasons);
1124+
}
1125+
case UR_DEVICE_INFO_MIN_POWER_LIMIT:
1126+
case UR_DEVICE_INFO_MAX_POWER_LIMIT: {
1127+
unsigned int minLimit, maxLimit;
1128+
auto NVMLHandle = hDevice->getNVML();
1129+
auto NVMLError = nvmlDeviceGetPowerManagementLimitConstraints(
1130+
NVMLHandle, &minLimit, &maxLimit);
1131+
if (NVMLError == NVML_ERROR_NOT_SUPPORTED) {
1132+
if (propName == UR_DEVICE_INFO_MAX_POWER_LIMIT) {
1133+
UR_CHECK_ERROR(
1134+
nvmlDeviceGetPowerManagementLimit(NVMLHandle, &maxLimit));
1135+
return ReturnValue(static_cast<int32_t>(maxLimit));
1136+
} else if (propName == UR_DEVICE_INFO_MIN_POWER_LIMIT) {
1137+
return ReturnValue(static_cast<int32_t>(-1));
1138+
}
1139+
}
1140+
if (propName == UR_DEVICE_INFO_MAX_POWER_LIMIT) {
1141+
return ReturnValue(static_cast<int32_t>(maxLimit));
1142+
} else if (propName == UR_DEVICE_INFO_MIN_POWER_LIMIT) {
1143+
return ReturnValue(static_cast<int32_t>(minLimit));
1144+
}
1145+
break;
1146+
}
1147+
case UR_DEVICE_INFO_FAN_SPEED: {
1148+
unsigned int Speed;
1149+
UR_CHECK_ERROR(nvmlDeviceGetFanSpeed(hDevice->getNVML(), &Speed));
1150+
return ReturnValue(static_cast<int32_t>(Speed));
1151+
}
10931152
case UR_DEVICE_INFO_2D_BLOCK_ARRAY_CAPABILITIES_EXP:
10941153
return ReturnValue(
10951154
static_cast<ur_exp_device_2d_block_array_capability_flags_t>(0));

unified-runtime/source/adapters/cuda/device.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ struct ur_device_handle_t_ {
3636
int MaxChosenLocalMem{0};
3737
bool MaxLocalMemSizeChosen{false};
3838
uint32_t NumComputeUnits{0};
39+
std::once_flag NVMLInitFlag;
40+
std::optional<nvmlDevice_t> NVMLDevice;
3941

4042
public:
4143
ur_device_handle_t_(native_type cuDevice, CUcontext cuContext, CUevent evBase,
4244
ur_platform_handle_t platform, uint32_t DevIndex)
4345
: CuDevice(cuDevice), CuContext(cuContext), EvBase(evBase), RefCount{1},
4446
Platform(platform), DeviceIndex{DevIndex} {
45-
4647
UR_CHECK_ERROR(cuDeviceGetAttribute(
4748
&MaxRegsPerBlock, CU_DEVICE_ATTRIBUTE_MAX_REGISTERS_PER_BLOCK,
4849
cuDevice));
@@ -102,11 +103,28 @@ struct ur_device_handle_t_ {
102103
if (MemoryProviderShared) {
103104
umfMemoryProviderDestroy(MemoryProviderShared);
104105
}
106+
if (NVMLDevice.has_value()) {
107+
UR_CHECK_ERROR(nvmlShutdown());
108+
}
105109
cuDevicePrimaryCtxRelease(CuDevice);
106110
}
107111

108112
native_type get() const noexcept { return CuDevice; };
109113

114+
nvmlDevice_t getNVML() {
115+
// Initialization happens lazily once per device object. Call to nvmlInit by
116+
// different objects will just increase the reference count. Each object's
117+
// destructor calls shutdown method, so once there will be no NVML users
118+
// left, resources will be released.
119+
std::call_once(NVMLInitFlag, [this]() {
120+
UR_CHECK_ERROR(nvmlInit());
121+
nvmlDevice_t Handle;
122+
UR_CHECK_ERROR(nvmlDeviceGetHandleByIndex(DeviceIndex, &Handle));
123+
NVMLDevice = Handle;
124+
});
125+
return NVMLDevice.value();
126+
};
127+
110128
CUcontext getNativeContext() const noexcept { return CuContext; };
111129

112130
uint32_t getReferenceCount() const noexcept { return RefCount; }

unified-runtime/test/adapters/cuda/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ target_include_directories(test-adapter-cuda PRIVATE
2929
${PROJECT_SOURCE_DIR}/source/adapters/cuda
3030
)
3131

32-
target_link_libraries(test-adapter-cuda PRIVATE cudadrv ${PROJECT_NAME}::umf)
32+
find_package(CUDAToolkit 10.1 REQUIRED)
33+
34+
target_link_libraries(test-adapter-cuda PRIVATE cudadrv CUDA::nvml ${PROJECT_NAME}::umf)

0 commit comments

Comments
 (0)