diff --git a/projects/amdsmi/py-interface/amdsmi_wrapper.py b/projects/amdsmi/py-interface/amdsmi_wrapper.py index 47186d6f83..785ff11f81 100644 --- a/projects/amdsmi/py-interface/amdsmi_wrapper.py +++ b/projects/amdsmi/py-interface/amdsmi_wrapper.py @@ -165,19 +165,47 @@ def char_pointer_cast(string, encoding='utf-8'): _libraries = {} from pathlib import Path -libamd_smi_parent_dir = Path(__file__).resolve().parent / "libamd_smi.so" -libamd_smi_cwd = Path.cwd() / "libamd_smi.so" +# libamd_smi.so can be located in several different places. +# Look for it with below priority: +# 1. ROCM_HOME/ROCM_PATH environment variables +# - ROCM_HOME/lib +# - ROCM_PATH/lib (usually set to /opt/rocm/) +# 2. Decided by the linker +# - LD_LIBRARY_PATH env var +# - defined path in /etc/ld.so.conf.d/ +# 3. Relative to amdsmi_wrapper.py +# - parent directory +# - current directory +def find_smi_library(): + err = OSError("Could not load libamd_smi.so") + possible_locations = list() + # 1. + rocm_path = os.getenv("ROCM_HOME", os.getenv("ROCM_PATH")) + if rocm_path: + possible_locations.append(os.path.join(rocm_path, "lib/libamd_smi.so")) + # 2. + possible_locations.append("libamd_smi.so") + # 3. + libamd_smi_parent_dir = Path(__file__).resolve().parent / "libamd_smi.so" + libamd_smi_cwd = Path.cwd() / "libamd_smi.so" + possible_locations.append(libamd_smi_parent_dir) + possible_locations.append(libamd_smi_cwd) + + for location in possible_locations: + try: + lib = ctypes.CDLL(location) + return lib, location + except OSError as e: + err = e + continue + raise err try: - if libamd_smi_parent_dir.is_file(): - # try to fall back to parent directory - _libraries['libamd_smi.so'] = ctypes.CDLL(libamd_smi_parent_dir) - else: - # lastly - search in current working directory - _libraries['libamd_smi.so'] = ctypes.CDLL(libamd_smi_cwd) -except OSError as error: - print(error) - print("Unable to find amdsmi library try installing amd-smi-lib from your package manager") + _libraries['libamd_smi.so'], location = find_smi_library() + #print(f"found smi lib in [", location, "]") +except OSError as e: + print(e) + print("Unable to find libamd_smi.so library try installing amd-smi-lib from your package manager") @@ -815,6 +843,19 @@ amdsmi_card_form_factor_t = ctypes.c_uint32 # enum class struct_amdsmi_pcie_info_t(Structure): pass +class struct_pcie_static_(Structure): + pass + +struct_pcie_static_._pack_ = 1 # source:False +struct_pcie_static_._fields_ = [ + ('max_pcie_width', ctypes.c_uint16), + ('PADDING_0', ctypes.c_ubyte * 2), + ('max_pcie_speed', ctypes.c_uint32), + ('pcie_interface_version', ctypes.c_uint32), + ('slot_type', amdsmi_card_form_factor_t), + ('reserved', ctypes.c_uint64 * 10), +] + class struct_pcie_metric_(Structure): pass @@ -835,19 +876,6 @@ struct_pcie_metric_._fields_ = [ ('reserved', ctypes.c_uint64 * 12), ] -class struct_pcie_static_(Structure): - pass - -struct_pcie_static_._pack_ = 1 # source:False -struct_pcie_static_._fields_ = [ - ('max_pcie_width', ctypes.c_uint16), - ('PADDING_0', ctypes.c_ubyte * 2), - ('max_pcie_speed', ctypes.c_uint32), - ('pcie_interface_version', ctypes.c_uint32), - ('slot_type', amdsmi_card_form_factor_t), - ('reserved', ctypes.c_uint64 * 10), -] - struct_amdsmi_pcie_info_t._pack_ = 1 # source:False struct_amdsmi_pcie_info_t._fields_ = [ ('pcie_static', struct_pcie_static_), diff --git a/projects/amdsmi/tools/generator.py b/projects/amdsmi/tools/generator.py index aa0910ad2c..2882f49486 100644 --- a/projects/amdsmi/tools/generator.py +++ b/projects/amdsmi/tools/generator.py @@ -180,19 +180,47 @@ def main(): library_path = os.path.join(os.path.dirname(__file__), library) line_to_replace = "_libraries['{}'] = ctypes.CDLL('{}')".format(library_name, library_path) new_line = f"""from pathlib import Path -libamd_smi_parent_dir = Path(__file__).resolve().parent / "{library_name}" -libamd_smi_cwd = Path.cwd() / "{library_name}" +# {library_name} can be located in several different places. +# Look for it with below priority: +# 1. ROCM_HOME/ROCM_PATH environment variables +# - ROCM_HOME/lib +# - ROCM_PATH/lib (usually set to /opt/rocm/) +# 2. Decided by the linker +# - LD_LIBRARY_PATH env var +# - defined path in /etc/ld.so.conf.d/ +# 3. Relative to amdsmi_wrapper.py +# - parent directory +# - current directory +def find_smi_library(): + err = OSError("Could not load {library_name}") + possible_locations = list() + # 1. + rocm_path = os.getenv("ROCM_HOME", os.getenv("ROCM_PATH")) + if rocm_path: + possible_locations.append(os.path.join(rocm_path, "lib/{library_name}")) + # 2. + possible_locations.append("{library_name}") + # 3. + libamd_smi_parent_dir = Path(__file__).resolve().parent / "{library_name}" + libamd_smi_cwd = Path.cwd() / "{library_name}" + possible_locations.append(libamd_smi_parent_dir) + possible_locations.append(libamd_smi_cwd) + + for location in possible_locations: + try: + lib = ctypes.CDLL(location) + return lib, location + except OSError as e: + err = e + continue + raise err try: - if libamd_smi_parent_dir.is_file(): - # try to fall back to parent directory - _libraries['{library_name}'] = ctypes.CDLL(libamd_smi_parent_dir) - else: - # lastly - search in current working directory - _libraries['{library_name}'] = ctypes.CDLL(libamd_smi_cwd) -except OSError as error: - print(error) - print("Unable to find amdsmi library try installing amd-smi-lib from your package manager")""" + _libraries['{library_name}'], location = find_smi_library() + #print(f"found smi lib in [", location, "]") +except OSError as e: + print(e) + print("Unable to find {library_name} library try installing amd-smi-lib from your package manager")""" else: print("Unknown operating system. It is only supporing Linux and Windows.") return