Skip to content

[Offload] Allow "tagging" device info entries with offload keys #147317

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions offload/plugins-nextgen/amdgpu/src/rtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2562,7 +2562,8 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
Status2 = hsa_system_get_info(HSA_SYSTEM_INFO_VERSION_MINOR, &Minor);
if (Status == HSA_STATUS_SUCCESS && Status2 == HSA_STATUS_SUCCESS)
Info.add("HSA Runtime Version",
std::to_string(Major) + "." + std::to_string(Minor));
std::to_string(Major) + "." + std::to_string(Minor), "",
DeviceInfo::DRIVER_VERSION);

Info.add("HSA OpenMP Device Number", DeviceId);

Expand All @@ -2572,11 +2573,11 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {

Status = getDeviceAttrRaw(HSA_AGENT_INFO_NAME, TmpChar);
if (Status == HSA_STATUS_SUCCESS)
Info.add("Device Name", TmpChar);
Info.add("Device Name", TmpChar, "", DeviceInfo::NAME);

Status = getDeviceAttrRaw(HSA_AGENT_INFO_VENDOR_NAME, TmpChar);
if (Status == HSA_STATUS_SUCCESS)
Info.add("Vendor Name", TmpChar);
Info.add("Vendor Name", TmpChar, "", DeviceInfo::VENDOR);

hsa_device_type_t DevType;
Status = getDeviceAttrRaw(HSA_AGENT_INFO_DEVICE, DevType);
Expand Down Expand Up @@ -2652,7 +2653,9 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {

Status = getDeviceAttrRaw(HSA_AGENT_INFO_WORKGROUP_MAX_DIM, WorkgrpMaxDim);
if (Status == HSA_STATUS_SUCCESS) {
auto &MaxSize = *Info.add("Workgroup Max Size per Dimension");
auto &MaxSize =
*Info.add("Workgroup Max Size per Dimension", std::monostate{}, "",
DeviceInfo::MAX_WORK_GROUP_SIZE);
MaxSize.add("x", WorkgrpMaxDim[0]);
MaxSize.add("y", WorkgrpMaxDim[1]);
MaxSize.add("z", WorkgrpMaxDim[2]);
Expand Down
28 changes: 25 additions & 3 deletions offload/plugins-nextgen/common/include/PluginInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ struct AsyncInfoWrapperTy {
__tgt_async_info *AsyncInfoPtr;
};

enum class DeviceInfo {
#define OFFLOAD_DEVINFO(Name, _, Value) Name = Value,
#include "OffloadInfo.inc"
#undef OFFLOAD_DEVINFO
};

/// Tree node for device information
///
/// This information is either printed or used by liboffload to extract certain
Expand All @@ -133,17 +139,21 @@ struct InfoTreeNode {
// * The same key can appear multiple times
std::unique_ptr<llvm::SmallVector<InfoTreeNode, 8>> Children;

llvm::DenseMap<DeviceInfo, size_t> DeviceInfoMap;

InfoTreeNode() : InfoTreeNode("", std::monostate{}, "") {}
InfoTreeNode(std::string Key, VariantType Value, std::string Units)
: Key(Key), Value(Value), Units(Units) {}

/// Add a new info entry as a child of this node. The entry requires at least
/// a key string in \p Key. The value in \p Value is optional and can be any
/// type that is representable as a string. The units in \p Units is optional
/// and must be a string.
/// and must be a string. Providing a device info key allows liboffload to
/// use that value for an appropriate olGetDeviceInfo query
template <typename T = std::monostate>
InfoTreeNode *add(std::string Key, T Value = T(),
const std::string &Units = std::string()) {
const std::string &Units = std::string(),
std::optional<DeviceInfo> DeviceInfoKey = std::nullopt) {
assert(!Key.empty() && "Invalid info key");

if (!Children)
Expand All @@ -157,7 +167,12 @@ struct InfoTreeNode {
else
ValueVariant = std::string{Value};

return &Children->emplace_back(Key, ValueVariant, Units);
auto Ptr = &Children->emplace_back(Key, ValueVariant, Units);

if (DeviceInfoKey)
DeviceInfoMap[*DeviceInfoKey] = Children->size() - 1;

return Ptr;
}

std::optional<InfoTreeNode *> get(StringRef Key) {
Expand All @@ -171,6 +186,13 @@ struct InfoTreeNode {
return It;
}

std::optional<InfoTreeNode *> get(DeviceInfo Info) {
auto Result = DeviceInfoMap.find(Info);
if (Result != DeviceInfoMap.end())
return &(*Children)[Result->second];
return std::nullopt;
}

/// Print all info entries in the tree
void print() const {
// Fake an additional indent so that values are offset from the keys
Expand Down
10 changes: 6 additions & 4 deletions offload/plugins-nextgen/cuda/src/rtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,15 +935,16 @@ struct CUDADeviceTy : public GenericDeviceTy {
if (Res == CUDA_SUCCESS)
// For consistency with other drivers, store the version as a string
// rather than an integer
Info.add("CUDA Driver Version", std::to_string(TmpInt));
Info.add("CUDA Driver Version", std::to_string(TmpInt), "",
DeviceInfo::DRIVER_VERSION);

Info.add("CUDA OpenMP Device Number", DeviceId);

Res = cuDeviceGetName(TmpChar, 1000, Device);
if (Res == CUDA_SUCCESS)
Info.add("Device Name", TmpChar);
Info.add("Device Name", TmpChar, "", DeviceInfo::NAME);

Info.add("Vendor Name", "NVIDIA");
Info.add("Vendor Name", "NVIDIA", "", DeviceInfo::VENDOR);

Res = cuDeviceTotalMem(&TmpSt, Device);
if (Res == CUDA_SUCCESS)
Expand Down Expand Up @@ -978,7 +979,8 @@ struct CUDADeviceTy : public GenericDeviceTy {
if (Res == CUDA_SUCCESS)
Info.add("Maximum Threads per Block", TmpInt);

auto &MaxBlock = *Info.add("Maximum Block Dimensions", "");
auto &MaxBlock = *Info.add("Maximum Block Dimensions", std::monostate{}, "",
DeviceInfo::MAX_WORK_GROUP_SIZE);
Res = getDeviceAttrRaw(CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X, TmpInt);
if (Res == CUDA_SUCCESS)
MaxBlock.add("x", TmpInt);
Expand Down
Loading