如何量化在Theano脚本中将多少数据复制到GPU中?

时间:2015-12-22 17:07:45

标签: python memory-management gpu gpgpu theano

作为一般规则,如果数据可以保留在GPU内存中,通常最好避免重复将数据从RAM复制到GPU内存。因此,某些代码将GPU内存中的数据集存储为共享变量(example)。

在Theano中,有没有办法量化执行脚本时将多少数据复制到GPU中?我的目标是估计在没有将数据集声明为共享变量的情况下,由于将数据从RAM复制到GPU内存而导致程序运行速度变慢。 (在我目前正在分析的脚本中将数据集声明为共享变量需要进行一些代码更改,因此我希望在深入了解代码之前获得估算值。)

1 个答案:

答案 0 :(得分:1)

据我所知,您无法计算函数或脚本所需的内存量。假设您知道要存储在GPU上的元素数量,您可以轻松计算存储这些元素所需的内存量。

一个简单的例子:

import numpy as np
import theano.tensor as T
T.config.floatX = 'float32'
dataPoints = np.random.random((5000, 256 * 256)).astype(T.config.floatX)
#float32 data type requires 4 bytes
sizeinGBs = 5000 * 256 * 256 * 4 / 1024. / 1024 / 1024 + (some small over-head constant which we can ignore safely)

大约1.22 GB

<强>更新

我找到了你问题的答案。您可以使用CUDA libray轻松获取GPU上的可用内存量。下面的代码可以帮助您计算在执行函数之前和之后专用于任务的GPU内存量。

import theano.sandbox.cuda.basic_ops as sbcuda
import numpy as np
import theano.tensor as T
T.config.floatX = 'float32'
freeGPUMemInGBs = sbcuda.cuda_ndarray.cuda_ndarray.mem_info()[0]/1024./1024/1024
print "Your GPU has %s GBs of free memory" % str(freeGPUMemInGBs)
#An operation is to be executed below
testData = shared(np.random.random((5000, 256 * 256)).astype(T.config.floatX), borrow = True)
print "The tasks above used %s GBs of your GPU memory. The available memory is %s GBs" % (str(freeGPUMemInGBs - sbcuda.cuda_ndarray.cuda_ndarray.mem_info()[0]/1024./1024/1024), str(sbcuda.cuda_ndarray.cuda_ndarray.mem_info()[0]/1024./1024/1024))

然后输出(对于我的机器)是:

>>> Your GPU has 11.2557678223 GBs of free memory
>>> The tasks above used 1.22077941895 GBs of your GPU memory. The available memory is 10.0349884033 GBs
相关问题