从 GPU 的角度理解 Theano 示例 cores/threads

Understanding Theano Example in terms of GPU cores/threads

我刚刚开始使用 Theano 和深度学习。我正在试验 Theano 教程 (http://deeplearning.net/software/theano/tutorial/using_gpu.html#returning-a-handle-to-device-allocated-data) 中的一个示例。示例代码如下所示:

from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in xrange(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

我试图理解定义 'vlen'、

的表达式
vlen = 10 * 30 * 768  # 10 x #cores x # threads per core

我在文中找不到任何地方提到此示例中指定的 GPU 核心数以及为什么选择 30。我也找不到为什么使用 768 个线程的值。我的 GPU (GeForce 840M) 有 384 个核心。我是否可以假设如果我用 384 代替值 30,我将使用所有 384 个内核? 768个线程的值也应该保持不变吗?

我相信逻辑如下。查看 the referenced page,我们看到提到了 GTX 275 GPU。因此,该教程使用的 GPU 可能是 cc1.x 代的非常旧的 CUDA GPU(CUDA 7.0 和 7.5 不再支持)。在评论中,开发人员似乎在使用 "core" 这个词来指代 GPU SM(多处理器)。

该系列中有许多 GPU 有 30 个 SM(cc1.x SM 与 cc 2+ SM 完全不同),包括 GTX 275(240 个 CUDA 核心 = cc1.x 代 30SM * 8 核/SM)。所以30这个数字是根据当时正在使用的GPU中的SM数量得出的。

此外,如果您查看 old documentation 支持此类 GPU 的 CUDA 版本,您会发现 cc1.0 和 cc1.1 GPU 支持每个多处理器 (SM) 最多 768 个线程。所以我相信这就是 768 号码的来源。

最后,一个好的 CUDA 代码将超额订阅 GPU(线程总数超过 GPU 可以瞬时处理的线程数)。所以我相信10的因素只是为了确保"oversubscription"。

特定数字并没有什么魔力——它只是数组的长度 (vlen)。这个数组的长度,经过theano框架后,最终决定了CUDA内核启动的线程数。此代码并不是真正的基准或其他性能指标。声明的目的只是为了证明正在使用 GPU。

所以我不会过多地解读这个数字。这是开发人员的随意选择,遵循与手头 GPU 相关的一定数量的逻辑。