使用示例
()
| 216 | |
| 217 | # 使用示例函数 |
| 218 | def monitor_gpu_memory_example(): |
| 219 | """使用示例""" |
| 220 | print("GPU显存监控示例") |
| 221 | print("=" * 50) |
| 222 | |
| 223 | try: |
| 224 | # 创建监控器 |
| 225 | monitor = GPUMemoryMonitor( |
| 226 | gpu_index=0, # 监控第一个GPU |
| 227 | interval=0.5, # 每0.5秒检查一次 |
| 228 | unit='GB' # 使用GB作为单位 |
| 229 | ) |
| 230 | |
| 231 | # 启动监控 |
| 232 | if monitor.start(): |
| 233 | # 这里可以运行你的GPU任务 |
| 234 | print("\n现在开始运行你的GPU任务...") |
| 235 | print("监控正在后台进行") |
| 236 | |
| 237 | # 模拟一些工作(在实际使用中,这里应该是你的GPU任务) |
| 238 | print("按回车键停止监控并获取峰值显存用量...") |
| 239 | input() # 等待用户按回车 |
| 240 | |
| 241 | # 停止监控并获取结果 |
| 242 | peak_memory_gb = monitor.stop() |
| 243 | |
| 244 | print(f"\n监控完成!峰值显存用量: {peak_memory_gb:.3f} GB") |
| 245 | |
| 246 | except Exception as e: |
| 247 | print(f"错误: {e}") |
| 248 | |
| 249 | |
| 250 | # 多GPU监控示例 |
nothing calls this directly
no test coverage detected