-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_measure.py
56 lines (42 loc) · 1.39 KB
/
gpu_measure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import py3nvml.py3nvml as nvml
import time
from threading import Thread
from typing import Tuple
import gc
class MyThread(Thread):
def __init__(self, func, params):
super(MyThread, self).__init__()
self.func = func
self.params = params
self.result = None
def run(self):
self.result = self.func(*self.params)
def get_result(self):
return self.result
class GpuMemoryMeasure:
def __init__(self):
nvml.nvmlInit()
self.handle = nvml.nvmlDeviceGetHandleByIndex(0)
self.gpu_memory_limit = nvml.nvmlDeviceGetMemoryInfo(self.handle).total >> 20
self.gpu_mem_usage = []
def start_measure_gpu_mem(self):
def _get_mem_usage():
while True:
self.gpu_mem_usage.append(
nvml.nvmlDeviceGetMemoryInfo(self.handle).used >> 20
)
time.sleep(0.5)
if self.stop:
break
return self.gpu_mem_usage
self.stop = False
self.thread = MyThread(_get_mem_usage, params=())
self.thread.start()
def stop_measure_gpu_mem(self) -> Tuple[float, float]:
self.stop = True
self.thread.join()
max_memory_usage = max(self.gpu_mem_usage)
return max_memory_usage, self.gpu_memory_limit
def __del__(self):
nvml.nvmlShutdown()
gc.collect()