2
2
import time
3
3
import warnings
4
4
5
- FROM_mWATTS_TO_kWATTH = 1000 * 1000 * 3600
5
+ FROM_mWATTS_TO_kWATTH = 1000 * 1000 * 3600
6
+
6
7
7
8
class NoGPUWarning (Warning ):
8
9
pass
9
10
10
- class GPU ():
11
+
12
+ class GPU :
11
13
"""
12
- This class is interface for tracking gpu consumption.
13
- All methods are done here on the assumption that all gpu devices are of equal model.
14
- The GPU class is not intended for separate usage, outside the Tracker class
14
+ This class is interface for tracking gpu consumption.
15
+ All methods are done here on the assumption that all gpu devices are of equal model.
16
+ The GPU class is not intended for separate usage, outside the Tracker class
15
17
16
18
"""
19
+
17
20
def __init__ (self , ignore_warnings = False ):
18
21
"""
19
- This class method initializes GPU object.
20
- Creates fields of class object. All the fields are private variables
22
+ This class method initializes GPU object.
23
+ Creates fields of class object. All the fields are private variables
21
24
22
- Parameters
23
- ----------
24
- ignore_warnings: bool
25
- If true, then user will be notified of all the warnings. If False, there won't be any warnings.
26
- The default is False.
25
+ Parameters
26
+ ----------
27
+ ignore_warnings: bool
28
+ If true, then user will be notified of all the warnings. If False, there won't be any warnings.
29
+ The default is False.
27
30
28
- Returns
29
- -------
30
- GPU: GPU
31
- Object of class GPU
31
+ Returns
32
+ -------
33
+ GPU: GPU
34
+ Object of class GPU
32
35
33
36
"""
34
37
self ._consumption = 0
35
38
self ._ignore_warnings = ignore_warnings
36
39
self .is_gpu_available = is_gpu_available ()
37
40
38
41
if not self .is_gpu_available and not self ._ignore_warnings :
39
- warnings .warn (message = "\n \n There is no any available GPU devices or your GPU is not supported by Nvidia library!\n The tracker will consider CPU usage only\n " ,
40
- category = NoGPUWarning )
42
+ warnings .warn (
43
+ message = """There is no any available GPU devices or your GPU is not supported by Nvidia library!\n The tracker will consider CPU usage only""" ,
44
+ category = NoGPUWarning ,
45
+ )
41
46
if self .is_gpu_available :
42
47
self ._start = time .time ()
43
-
48
+
44
49
def calculate_consumption (self ):
45
50
"""
46
- This class method calculates GPU power consumption.
47
-
48
- Parameters
49
- ----------
50
- No parameters
51
-
52
- Returns
53
- -------
54
- consumption: float
55
- CPU power consumption
51
+ This class method calculates GPU power consumption.
52
+
53
+ Parameters
54
+ ----------
55
+ No parameters
56
+
57
+ Returns
58
+ -------
59
+ consumption: float
60
+ CPU power consumption
56
61
"""
57
62
if not self .is_gpu_available :
58
63
return 0
@@ -65,37 +70,37 @@ def calculate_consumption(self):
65
70
consumption = 0
66
71
self ._consumption += consumption
67
72
return consumption
68
-
73
+
69
74
def get_consumption (self ):
70
75
"""
71
- This class method returns GPU power consupmtion amount.
76
+ This class method returns GPU power consupmtion amount.
72
77
73
- Parameters
74
- ----------
75
- No parameters
78
+ Parameters
79
+ ----------
80
+ No parameters
76
81
77
- Returns
78
- -------
79
- self._consumption: float
80
- CPU power consumption
82
+ Returns
83
+ -------
84
+ self._consumption: float
85
+ CPU power consumption
81
86
82
87
"""
83
88
if not self .is_gpu_available :
84
89
return 0
85
90
return self ._consumption
86
-
91
+
87
92
def gpu_memory (self ):
88
93
"""
89
- This class method returns GPU Memory used. Pynvml library is used.
94
+ This class method returns GPU Memory used. Pynvml library is used.
90
95
91
- Parameters
92
- ----------
93
- No parameters
96
+ Parameters
97
+ ----------
98
+ No parameters
94
99
95
- Returns
96
- -------
97
- gpus_memory: list
98
- list of GPU Memory used per every GPU
100
+ Returns
101
+ -------
102
+ gpus_memory: list
103
+ list of GPU Memory used per every GPU
99
104
100
105
"""
101
106
if not self .is_gpu_available :
@@ -111,16 +116,16 @@ def gpu_memory(self):
111
116
112
117
def gpu_temperature (self ):
113
118
"""
114
- This class method returns GPU temperature. Pynvml library is used.
119
+ This class method returns GPU temperature. Pynvml library is used.
115
120
116
- Parameters
117
- ----------
118
- No parameters
121
+ Parameters
122
+ ----------
123
+ No parameters
119
124
120
- Returns
121
- -------
122
- gpus_temps: list
123
- list of GPU temperature per every GPU
125
+ Returns
126
+ -------
127
+ gpus_temps: list
128
+ list of GPU temperature per every GPU
124
129
125
130
"""
126
131
if not self .is_gpu_available :
@@ -136,16 +141,16 @@ def gpu_temperature(self):
136
141
137
142
def gpu_power (self ):
138
143
"""
139
- This class method returns GPU power consumption. Pynvml library is used.
144
+ This class method returns GPU power consumption. Pynvml library is used.
140
145
141
- Parameters
142
- ----------
143
- No parameters
146
+ Parameters
147
+ ----------
148
+ No parameters
144
149
145
- Returns
146
- -------
147
- gpus_powers: list
148
- list of GPU power consumption per every GPU
150
+ Returns
151
+ -------
152
+ gpus_powers: list
153
+ list of GPU power consumption per every GPU
149
154
150
155
"""
151
156
if not self .is_gpu_available :
@@ -161,16 +166,16 @@ def gpu_power(self):
161
166
162
167
def gpu_power_limit (self ):
163
168
"""
164
- This class method returns GPU power limits. Pynvml library is used.
169
+ This class method returns GPU power limits. Pynvml library is used.
165
170
166
- Parameters
167
- ----------
168
- No parameters
171
+ Parameters
172
+ ----------
173
+ No parameters
169
174
170
- Returns
171
- -------
172
- gpus_limits: list
173
- list of GPU power limits per every GPU
175
+ Returns
176
+ -------
177
+ gpus_limits: list
178
+ list of GPU power limits per every GPU
174
179
175
180
"""
176
181
if not self .is_gpu_available :
@@ -183,21 +188,23 @@ def gpu_power_limit(self):
183
188
gpus_limits .append (pynvml .nvmlDeviceGetEnforcedPowerLimit (handle ))
184
189
pynvml .nvmlShutdown ()
185
190
return gpus_limits
186
-
187
- def name (self ,):
191
+
192
+ def name (
193
+ self ,
194
+ ):
188
195
"""
189
- This class method returns GPU name if there are any GPU visible
190
- or it returns empty string. All the GPU devices are intended to be of the same model
191
- Pynvml library is used.
196
+ This class method returns GPU name if there are any GPU visible
197
+ or it returns empty string. All the GPU devices are intended to be of the same model
198
+ Pynvml library is used.
192
199
193
- Parameters
194
- ----------
195
- No parameters
200
+ Parameters
201
+ ----------
202
+ No parameters
196
203
197
- Returns
198
- -------
199
- gpus_name: string
200
- string with GPU name.
204
+ Returns
205
+ -------
206
+ gpus_name: string
207
+ string with GPU name.
201
208
202
209
"""
203
210
try :
@@ -212,20 +219,20 @@ def name(self,):
212
219
return gpus_name [0 ].encode ().decode ("UTF-8" )
213
220
except :
214
221
return ""
215
-
222
+
216
223
def gpu_num (self ):
217
224
"""
218
- This class method returns number of visible GPU devices.
219
- Pynvml library is used.
225
+ This class method returns number of visible GPU devices.
226
+ Pynvml library is used.
220
227
221
- Parameters
222
- ----------
223
- No parameters
228
+ Parameters
229
+ ----------
230
+ No parameters
224
231
225
- Returns
226
- -------
227
- deviceCount: int
228
- Number of visible GPU devices.
232
+ Returns
233
+ -------
234
+ deviceCount: int
235
+ Number of visible GPU devices.
229
236
230
237
"""
231
238
try :
@@ -236,24 +243,24 @@ def gpu_num(self):
236
243
pynvml .nvmlDeviceGetPowerUsage (handle )
237
244
pynvml .nvmlShutdown ()
238
245
return deviceCount
239
- except :
246
+ except :
240
247
return 0
241
248
242
249
243
250
def is_gpu_available ():
244
251
"""
245
- This function checks if there are any available GPU devices
246
- All the GPU devices are intended to be of the same model
247
-
248
- Parameters
249
- ----------
250
- No parameters
251
-
252
- Returns
253
- -------
254
- gpu_availability: bool
255
- If there are any visible GPU devices,
256
- then gpu_availability = True, else gpu_availability = False
252
+ This function checks if there are any available GPU devices
253
+ All the GPU devices are intended to be of the same model
254
+
255
+ Parameters
256
+ ----------
257
+ No parameters
258
+
259
+ Returns
260
+ -------
261
+ gpu_availability: bool
262
+ If there are any visible GPU devices,
263
+ then gpu_availability = True, else gpu_availability = False
257
264
258
265
"""
259
266
try :
@@ -268,18 +275,19 @@ def is_gpu_available():
268
275
except pynvml .NVMLError :
269
276
return False
270
277
278
+
271
279
def all_available_gpu ():
272
280
"""
273
- This function prints all seeable GPU devices
274
- All the GPU devices are intended to be of the same model
275
-
276
- Parameters
277
- ----------
278
- No parameters
279
-
280
- Returns
281
- -------
282
- No returns
281
+ This function prints all seeable GPU devices
282
+ All the GPU devices are intended to be of the same model
283
+
284
+ Parameters
285
+ ----------
286
+ No parameters
287
+
288
+ Returns
289
+ -------
290
+ No returns
283
291
284
292
"""
285
293
try :
@@ -295,4 +303,4 @@ def all_available_gpu():
295
303
print (string )
296
304
pynvml .nvmlShutdown ()
297
305
except :
298
- print ("There is no any available gpu device(s)" )
306
+ print ("There is no any available gpu device(s)" )
0 commit comments