-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhardware.py
51 lines (36 loc) · 1.15 KB
/
hardware.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
import numpy as np
import torch
from util import log
def get_hardware_device(gpu_preferred: bool = True):
''' Pick GPU if available, else run on CPU.
Returns the corresponding device.
'''
if gpu_preferred:
init()
print_gpu_status()
if torch.cuda.is_available():
print('Running on GPU.')
return torch.device('cuda')
else:
print(' =================')
print('Wanted to run on GPU but it is not available!!')
print(' =================')
log.write('Running on CPU.')
return torch.device('cpu')
def print_gpu_status(silent: bool = False) -> [str]:
out = []
out.append('Torch: is Cuda available: ' + str(torch.cuda.is_available()))
out.append('Torch: Visible Devices: ' + str(torch.cuda.device_count()))
out.append('Torch: Compiled Torch Architecture: ' + str(torch.cuda.get_arch_list()))
if not silent:
for line in out:
print(line)
return out
def init():
torch.cuda.init()
def main():
print('Running GPU Hardware diagnostics...')
init()
print_gpu_status()
if __name__ == '__main__':
main()