CNTK中GPU信息的获取
device接口
CNTK提供了device接口,可以访问gpu的几个基本参数。
获取所有的设备
首先可以通过cntk.device.all_devices方法来获取当前的设备
>>> C.device.all_devices()
(GPU[0] GeForce GTX 960M, CPU)
获取GPU
知道了系统里有多少设备了之后,就可以通过设备号来通过device.gpu来访问GPU设备了。
例:
>>> C.device.gpu(0)
GPU[0] GeForce GTX 960M
GPU属性
通过device.gpu(id)获取了gpu的引用之后,我们就可以通过device.get_gpu_properties函数来获取属性:
>>> prop = C.device.get_gpu_properties(C.device.gpu(0))
>>> prop
<cntk.cntk_py.GPUProperties; proxy of <Swig Object of type 'CNTK::GPUProperties *' at 0x000001A1195C3420> >
属性有:
- device_id: 设备号
- name: 名字
- version_major: 主版本号
- version_minor: 副版本号
- cuda_cores: CUDA核
- total_memory: 显存大小
例:
>>> prop.name
'GeForce GTX 960M'
>>> prop.version_major
5
>>> prop.version_minor
0
>>> prop.cuda_cores
960
>>> prop.total_memory
2048
>>> prop.device_id
0
如何监控GPU内存的分配与释放
如果想要监控内存使用情况的话,上面的简单的API是不够用的,我们使用trace功能吧:
C.cntk_py.set_gpumemory_allocation_trace_level(1)
例,运行时打印出来的效果是这样的:
Allocating Matrix<float> (Rows = 1, Cols = 5416) buffer on DeviceId = 0; GPU Memory Free = 29 MB of 2048 MB
Allocated DeviceData = 000000050323AA00
Allocating Matrix<float> (Rows = 1, Cols = 8124) buffer on DeviceId = 0; GPU Memory Free = 29 MB of 2048 MB
Allocated DeviceData = 0000000504E17A00
Allocating Matrix<float> (Rows = 1, Cols = 5416) buffer on DeviceId = 0; GPU Memory Free = 29 MB of 2048 MB
Allocated DeviceData = 0000000502A38E00
Freed buffer<float> DeviceData = 0000000502A38E00 on DeviceId = 0; GPU Memory Free = 29 MB of 2048 MB
Freed buffer<float> DeviceData = 0000000504E17A00 on DeviceId = 0; GPU Memory Free = 29 MB of 2048 MB
Freed buffer<float> DeviceData = 000000050323AA00 on DeviceId = 0; GPU Memory Free = 29 MB of 2048 MB
Freed buffer<float> DeviceData = 0000000567440000 on DeviceId = 0; GPU Memory Free = 98 MB of 2048 MB
Allocating Matrix<float> (Rows = 650, Cols = 8124) buffer on DeviceId = 0; GPU Memory Free = 98 MB of 2048 MB
Allocated DeviceData = 0000000541BC0000
Freed buffer<char> DeviceData = 0000000502B3E600 on DeviceId = 0; GPU Memory Free = 78 MB of 2048 MB
Allocating Matrix<float> (Rows = 650, Cols = 5416) buffer on DeviceId = 0; GPU Memory Free = 78 MB of 2048 MB
Allocated DeviceData = 0000000543000000
Allocating Matrix<float> (Rows = 1, Cols = 5416) buffer on DeviceId = 0; GPU Memory Free = 65 MB of 2048 MB
Allocated DeviceData = 000000050323AA00
Freed buffer<float> DeviceData = 000000050323AA00 on DeviceId = 0; GPU Memory Free = 65 MB of 2048 MB
Freed buffer<float> DeviceData = 0000000543000000 on DeviceId = 0; GPU Memory Free = 78 MB of 2048 MB
Allocating Matrix<char> (Rows = 1, Cols = 5416) buffer on DeviceId = 0; GPU Memory Free = 78 MB of 2048 MB
Allocated DeviceData = 0000000502B3E600
Freed buffer<float> DeviceData = 0000000541BC0000 on DeviceId = 0; GPU Memory Free = 98 MB of 2048 MB
Allocating Matrix<float> (Rows = 650, Cols = 8066) buffer on DeviceId = 0; GPU Memory Free = 98 MB of 2048 MB
Allocated DeviceData = 0000000541BC0000
Allocating Matrix<float> (Rows = 1, Cols = 8066) buffer on DeviceId = 0; GPU Memory Free = 78 MB of 2048 MB
Allocated DeviceData = 0000000504E17A00
Freed buffer<float> DeviceData = 0000000504E17A00 on DeviceId = 0; GPU Memory Free = 78 MB of 2048 MB
Freed buffer<float> DeviceData = 0000000541BC0000 on DeviceId = 0; GPU Memory Free = 98 MB of 2048 MB
Allocating Matrix<float> (Rows = 3377, Cols = 1) buffer on DeviceId = 0; GPU Memory Free = 98 MB of 2048 MB
Allocated DeviceData = 00000005050DCA00
Freed buffer<float> DeviceData = 00000005050DCA00 on DeviceId = 0; GPU Memory Free = 98 MB of 2048 MB
Allocating Matrix<float> (Rows = 3377, Cols = 8066) buffer on DeviceId = 0; GPU Memory Free = 98 MB of 2048 MB