diff --git a/AGENTS.md b/AGENTS.md index 7250b65..5cb77d0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -4,7 +4,7 @@ **pymtml** is Python bindings for the Moore Threads Management Library (MTML) - a C-based API for monitoring and managing Moore Threads GPU devices. It provides: -1. **Native MTML bindings** - Direct Python wrappers for libmtml.so C library functions +1. **Native MTML bindings** - Direct Python wrappers for MTML C library functions (Linux: libmtml.so, Windows: mtml.dll) 2. **NVML compatibility layer** - Drop-in replacement for NVIDIA's pynvml library Moore Threads GPUs use **MUSA** (Meta-computing Unified System Architecture) as their compute platform, analogous to NVIDIA's CUDA. @@ -93,13 +93,13 @@ def mtmlDeviceGetSomething(device): ## Testing Instructions - **Always run tests before committing**: `python test_pymtml.py && python test_pynvml.py` -- **Tests require Moore Threads GPU hardware** with driver and libmtml.so installed +- **Tests require Moore Threads GPU hardware** with driver and MTML library installed (Linux: libmtml.so, Windows: mtml.dll) - **Test init/shutdown cycles**: The library supports multiple init/shutdown cycles - **Check for segfaults**: Library shutdown must not cause crashes ## Security Considerations -- This library loads `libmtml.so` dynamically via ctypes +- This library loads MTML library dynamically via ctypes (Linux: libmtml.so, Windows: mtml.dll) - No network operations or external data fetching - GPU operations require appropriate system permissions - Handle device handles carefully - don't use after shutdown diff --git a/MANUAL.md b/MANUAL.md index 2e50c44..a53623d 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -23,14 +23,14 @@ Python Bindings for Moore Threads Management Library (MTML) ## 1. 项目简介 -**pymtml** 是摩尔线程 GPU 管理库 (MTML) 的 Python 绑定,通过 ctypes 动态加载 `libmtml.so` 共享库,提供对摩尔线程 GPU 设备的监控与管理能力。 +**pymtml** 是摩尔线程 GPU 管理库 (MTML) 的 Python 绑定,通过 ctypes 动态加载 MTML 共享库(Linux: `libmtml.so`, Windows: `mtml.dll`),提供对摩尔线程 GPU 设备的监控与管理能力。 ### 核心特性 | 特性 | 说明 | | --- | --- | -| **原生 MTML API** | 直接封装 libmtml.so C 库函数 | +| **原生 MTML API** | 直接封装 MTML C 库函数(Linux: libmtml.so, Windows: mtml.dll) | | **NVML 兼容层** | 提供 pynvml 的替代接口,支持一行替换 | | **上下文管理器** | 提供 `with` 语句管理 GPU/Memory/VPU 子组件生命周期 | | **多 GPU 支持** | 支持多卡拓扑查询、P2P 状态检测、MtLink 互连检测 | @@ -66,14 +66,43 @@ mthreads-ml-py/ 验证驱动是否安装: +### Linux + ```bash -# 检查 libmtml.so 是否可用 +# 检查 MTML 库是否可用 ldconfig -p | grep libmtml # 检查 GPU 设备 ls /dev/dri/render* ``` +### Windows + +#### 方式一:检查mtml.dll是否在PATH中 + +```cmd +where mtml.dll +``` +如果MTML已正确安装并加入系统PATH,将会看到类似输出: + +``` +C:\Program Files\MooreThreads\MTML\bin\mtml.dll +``` + +#### 方式二:使用Python验证(推荐) + +```python +import ctypes + +try: + ctypes.WinDLL("mtml.dll") + print("MTML runtime is available.") +except OSError as e: + print("MTML runtime not found:", e) +``` +如果 DLL 可以被成功加载,则说明 MTML 运行时已正确安装。 + + --- ## 3. 安装方式 @@ -193,7 +222,7 @@ pynvml.nvmlShutdown() | 函数 | 说明 | | --- | --- | -| `mtmlLibraryInit()` | 初始化 MTML 库,加载 `libmtml.so` | +| `mtmlLibraryInit()` | 初始化 MTML 库,加载 MTML 共享库(Linux: libmtml.so, Windows: mtml.dll) | | `mtmlLibraryShutDown()` | 关闭库接口(库本身保持加载) | | `mtmlLibraryGetVersion()` | 获取 MTML 库版本号 | | `mtmlLibraryCountDevice()` | 获取 GPU 设备数量 | @@ -999,7 +1028,7 @@ pymtml.MTMLError_DriverNotLoaded: Driver Not Loaded **原因**:摩尔线程 GPU 驱动未加载或不在当前环境中可用(例如在沙箱、容器中运行时)。 -**解决**:确认 GPU 驱动已安装,`libmtml.so` 在 `LD_LIBRARY_PATH` 中。 +**解决**:确认 GPU 驱动已安装,MTML 库在系统路径中(Linux: `libmtml.so` 在 `LD_LIBRARY_PATH` 中,Windows: `mtml.dll` 在 `PATH` 中或 `mtml/` 目录下)。 ### Q: `nvmlDeviceGetCudaComputeCapability()` 返回 `(0, 0)` diff --git a/README.md b/README.md index 71178d7..fa78a40 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,8 @@ Moore Threads GPUs use MUSA (Meta-computing Unified System Architecture) as thei - Python 3.7+ - Moore Threads GPU driver with MTML library installed -- The `libmtml.so` shared library must be in the library path +- **Linux**: The `libmtml.so` shared library must be in the library path +- **Windows**: The `mtml.dll` library must be in the system PATH or in the `mtml/` directory relative to the script ## Installation diff --git a/pymtml.py b/pymtml.py index 949ddd6..0239bc7 100644 --- a/pymtml.py +++ b/pymtml.py @@ -3,6 +3,7 @@ ## from __future__ import annotations +import os import string import sys import threading @@ -632,6 +633,50 @@ def wrapper(*args, **kwargs): return wrapper return func +def _LoadWindowsLibrary(): + """ + Load the MTML library on Windows platform + """ + lib_name = "mtml.dll" + lib_loader = WinDLL + + # Try loading the library with different names + try: + return lib_loader(lib_name) + except OSError as ose: + pass + + # If all attempts failed, try search PATH + for p in os.environ.get("PATH", "").split(os.pathsep): + if not p: + continue + candidate = os.path.join(p, lib_name) + + if os.path.isfile(candidate): + os.add_dll_directory(p) + return WinDLL(candidate) + + raise OSError(f"Failed to load MTML library on Windows. Tried: {lib_name}") + +def _LoadLinuxLibrary(): + """ + Load the MTML library on Linux/Unix platform + """ + lib_name = "libmtml.so" + lib_loader = CDLL + + # Try loading the library with different names + last_error = None + try: + return lib_loader(lib_name) + except OSError as ose: + last_error = ose + + # If failed, raise detailed error + error_msg = f"Failed to load MTML library on Linux. Tried: {lib_name}" + if last_error: + error_msg += f"\nLast error: {last_error}" + raise OSError(error_msg) ## C function wrappers ## def _LoadMtmlLibrary(): @@ -647,13 +692,18 @@ def _LoadMtmlLibrary(): try: # ensure the library still isn't loaded if mtmlLib == None: - try: - # assume linux - mtmlLib = CDLL("libmtml.so") - except OSError as ose: - _mtmlCheckReturn(MTML_ERROR_FUNCTION_NOT_FOUND) + # Platform-specific library loading + platform = sys.platform + + if platform.startswith("win32") or platform.startswith("cygwin"): + # Windows platform + mtmlLib = _LoadWindowsLibrary() + else: + # Linux/Unix platform + mtmlLib = _LoadLinuxLibrary() + if mtmlLib == None: - _mtmlCheckReturn(MTML_ERROR_FUNCTION_NOT_FOUND) + raise OSError("Failed to load MTML library on platform: " + platform) finally: # lock is always freed libLoadLock.release() diff --git a/setup.py b/setup.py index 2b89143..5144237 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,15 @@ -from distutils.core import setup +try: + from setuptools import setup + from setuptools.dist import DistributionMetadata +except ImportError: + from distutils.core import setup + from distutils.dist import DistributionMetadata + from sys import version from sys import exit # earlier versions don't support all classifiers if version < '2.2.3': - from distutils.dist import DistributionMetadata DistributionMetadata.classifiers = None DistributionMetadata.download_url = None @@ -42,7 +47,7 @@ setup(name=_package_name, - version='2.2.10', + version='2.2.11', description='Python Bindings for the Moore Threads GPU Management Library', long_description=long_description, long_description_content_type='text/markdown',