By default, only consider AMD GPU's in RSMI device list
With newly added initialization parameters that can be passed to rsmi_init(), you can tell RSMI to consider other devices. Also: -fixed incorrect header file name that would break in C builds -modified rsmi_init() and rsmi_shut_down() to reinitialize and clear static structures
This commit is contained in:
+16
-9
@@ -94,7 +94,7 @@ static rsmi_status_t handleException() {
|
||||
#define TRY try {
|
||||
#define CATCH } catch (...) {return handleException();}
|
||||
#define GET_DEV_FROM_INDX \
|
||||
amd::smi::RocmSMI smi = amd::smi::RocmSMI::getInstance(); \
|
||||
amd::smi::RocmSMI& smi = amd::smi::RocmSMI::getInstance(); \
|
||||
if (dv_ind >= smi.monitor_devices().size()) { \
|
||||
return RSMI_STATUS_INVALID_ARGS; \
|
||||
} \
|
||||
@@ -106,7 +106,8 @@ static rsmi_status_t handleException() {
|
||||
amd::smi::ScopedPthread _lock(_pw);
|
||||
|
||||
static pthread_mutex_t *get_mutex(uint32_t dv_ind) {
|
||||
amd::smi::RocmSMI smi = amd::smi::RocmSMI::getInstance();
|
||||
amd::smi::RocmSMI& smi = amd::smi::RocmSMI::getInstance();
|
||||
|
||||
if (dv_ind >= smi.monitor_devices().size()) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -382,7 +383,7 @@ static rsmi_status_t set_dev_mon_value(amd::smi::MonitorTypes type,
|
||||
|
||||
static rsmi_status_t get_power_mon_value(amd::smi::PowerMonTypes type,
|
||||
uint32_t dv_ind, uint64_t *val) {
|
||||
amd::smi::RocmSMI smi = amd::smi::RocmSMI::getInstance();
|
||||
amd::smi::RocmSMI& smi = amd::smi::RocmSMI::getInstance();
|
||||
|
||||
if (dv_ind >= smi.monitor_devices().size() || val == nullptr) {
|
||||
return RSMI_STATUS_INVALID_ARGS;
|
||||
@@ -416,11 +417,12 @@ static bool is_power_of_2(uint64_t n) {
|
||||
}
|
||||
|
||||
rsmi_status_t
|
||||
rsmi_init(uint64_t init_flags) {
|
||||
rsmi_init(uint64_t flags) {
|
||||
TRY
|
||||
(void)init_flags; // unused for now; for future use
|
||||
|
||||
amd::smi::RocmSMI smi = amd::smi::RocmSMI::getInstance();
|
||||
amd::smi::RocmSMI& smi = amd::smi::RocmSMI::getInstance();
|
||||
smi.Initialize(flags);
|
||||
|
||||
return RSMI_STATUS_SUCCESS;
|
||||
CATCH
|
||||
}
|
||||
@@ -430,6 +432,11 @@ rsmi_init(uint64_t init_flags) {
|
||||
rsmi_status_t
|
||||
rsmi_shut_down(void) {
|
||||
TRY
|
||||
|
||||
amd::smi::RocmSMI& smi = amd::smi::RocmSMI::getInstance();
|
||||
|
||||
smi.Cleanup();
|
||||
|
||||
return RSMI_STATUS_SUCCESS;
|
||||
CATCH
|
||||
}
|
||||
@@ -441,7 +448,7 @@ rsmi_num_monitor_devices(uint32_t *num_devices) {
|
||||
return RSMI_STATUS_INVALID_ARGS;
|
||||
}
|
||||
|
||||
amd::smi::RocmSMI smi = amd::smi::RocmSMI::getInstance();
|
||||
amd::smi::RocmSMI& smi = amd::smi::RocmSMI::getInstance();
|
||||
|
||||
*num_devices = smi.monitor_devices().size();
|
||||
return RSMI_STATUS_SUCCESS;
|
||||
@@ -1086,7 +1093,7 @@ rsmi_dev_gpu_clk_freq_set(uint32_t dv_ind,
|
||||
|
||||
assert(freqs.num_supported <= RSMI_MAX_NUM_FREQUENCIES);
|
||||
|
||||
amd::smi::RocmSMI smi = amd::smi::RocmSMI::getInstance();
|
||||
amd::smi::RocmSMI& smi = amd::smi::RocmSMI::getInstance();
|
||||
|
||||
// Above call to rsmi_dev_get_gpu_clk_freq should have emitted an error if
|
||||
// assert below is not true
|
||||
@@ -1366,7 +1373,7 @@ rsmi_dev_pci_bandwidth_set(uint32_t dv_ind, uint64_t bw_bitmask) {
|
||||
|
||||
assert(bws.transfer_rate.num_supported <= RSMI_MAX_NUM_FREQUENCIES);
|
||||
|
||||
amd::smi::RocmSMI smi = amd::smi::RocmSMI::getInstance();
|
||||
amd::smi::RocmSMI& smi = amd::smi::RocmSMI::getInstance();
|
||||
|
||||
// Above call to rsmi_dev_pci_bandwidth_get() should have emitted an error
|
||||
// if assert below is not true
|
||||
|
||||
@@ -234,10 +234,13 @@ static uint32_t GetMonitorDevices(const std::shared_ptr<amd::smi::Device> &d,
|
||||
|
||||
std::vector<std::shared_ptr<amd::smi::Device>> RocmSMI::s_monitor_devices;
|
||||
|
||||
RocmSMI::RocmSMI(void) {
|
||||
void
|
||||
RocmSMI::Initialize(uint64_t flags) {
|
||||
auto i = 0;
|
||||
uint32_t ret;
|
||||
|
||||
init_options_ = flags;
|
||||
|
||||
GetEnvVariables();
|
||||
|
||||
while (std::string(kAMDMonitorTypes[i]) != "") {
|
||||
@@ -260,15 +263,23 @@ RocmSMI::RocmSMI(void) {
|
||||
}
|
||||
}
|
||||
|
||||
RocmSMI::~RocmSMI() {
|
||||
void
|
||||
RocmSMI::Cleanup() {
|
||||
s_monitor_devices.clear();
|
||||
devices_.clear();
|
||||
monitors_.clear();
|
||||
}
|
||||
|
||||
RocmSMI& RocmSMI::getInstance(void) {
|
||||
RocmSMI::RocmSMI(uint64_t flags) : init_options_(flags) {
|
||||
}
|
||||
|
||||
RocmSMI::~RocmSMI() {
|
||||
}
|
||||
|
||||
RocmSMI& RocmSMI::getInstance(uint64_t flags) {
|
||||
// Assume c++11 or greater. static objects will be created by only 1 thread
|
||||
// and creation will be thread-safe.
|
||||
static RocmSMI singleton;
|
||||
static RocmSMI singleton(flags);
|
||||
return singleton;
|
||||
}
|
||||
|
||||
@@ -324,6 +335,33 @@ RocmSMI::AddToDeviceList(std::string dev_name) {
|
||||
return;
|
||||
}
|
||||
|
||||
static const uint32_t kAmdGpuId=0x1002;
|
||||
|
||||
static bool isAMDGPU(std::string dev_path) {
|
||||
|
||||
std::string vend_path = dev_path + "/device/vendor";
|
||||
if (!FileExists(vend_path.c_str())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ifstream fs;
|
||||
fs.open(vend_path);
|
||||
|
||||
if (!fs.is_open()) {
|
||||
return errno;
|
||||
}
|
||||
|
||||
uint32_t vendor_id;
|
||||
|
||||
fs >> std::hex >> vendor_id;
|
||||
|
||||
fs.close();
|
||||
|
||||
if (vendor_id == kAmdGpuId) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t RocmSMI::DiscoverDevices(void) {
|
||||
auto ret = 0;
|
||||
@@ -346,7 +384,14 @@ uint32_t RocmSMI::DiscoverDevices(void) {
|
||||
while (dentry != nullptr) {
|
||||
if (memcmp(dentry->d_name, kDeviceNamePrefix, strlen(kDeviceNamePrefix))
|
||||
== 0) {
|
||||
AddToDeviceList(dentry->d_name);
|
||||
std::string vend_str_path = kPathDRMRoot;
|
||||
vend_str_path += "/";
|
||||
vend_str_path += dentry->d_name;
|
||||
|
||||
if (isAMDGPU(vend_str_path) ||
|
||||
(init_options_ & RSMI_INIT_FLAG_ALL_GPUS)) {
|
||||
AddToDeviceList(dentry->d_name);
|
||||
}
|
||||
}
|
||||
dentry = readdir(drm_dir);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user