作为一般规则,如果数据可以保留在GPU内存中,通常最好避免重复地将数据从RAM复制到GPU内存。因此,一些代码将数据集作为共享变量(example)存储在GPU内存中。
在Theano中,有没有办法量化执行脚本时有多少数据被复制到GPU中?我的目标是估计由于将数据从RAM复制到GPU内存,不将数据集声明为共享变量会在多大程度上减慢程序速度。(在我目前正在分析的脚本中将数据集声明为共享变量需要进行大量的代码更改,因此我更喜欢在开始编写代码之前获得估计值)。
发布于 2015-12-26 06:35:35
据我所知,您无法计算函数或脚本所需的内存量。假设您知道要在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库轻松获得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 GBshttps://stackoverflow.com/questions/34421017
复制相似问题