Python 矩阵提供 numpy.dot()

Python matrix provide with numpy.dot()

在 Python (numba lib) 中接触 CUDA 期间,我实现了矩阵提供方法:

所以我在两种类型的数据上测试了它:

对于 int32,我获得了预期的结果,其中我的 GPU 算法比使用 numpy 的 CPU 表现更好:

然而,在 float64 类型上,numpy.dot() 优于我所有的 GPU 方法:

所以,问题是: 为什么 numpy.dot() 使用 float64 数组那么快,numpy 使用 GPU 吗?

我知道 numpy 会自动使用编译库的多个 cpu 处理器。对于某些功能(我认为 dot() 是其中之一,尽管我现在找不到 ref)。我怀疑这就是正在发生的事情。我不知道有任何尝试获得 numpy gpu 后端 http://www.reddit.com/r/Python/comments/1mw9mb/is_there_a_gpu_backend_for_numpyscipy_money_is_no/

典型的 numpy 安装将动态链接到 BLAS library, which provides routines for matrix-matrix and matrix-vector multiplication. For example, when you use np.dot() on a pair of float64 arrays, numpy will call the BLAS dgemm routine in the background. Although these library functions run on the CPU rather than the GPU, they are often multithreaded, and are very finely tuned for performance. A good BLAS implementation, such as MKL or OpenBLAS,在性能方面可能很难被击败,即使在 GPU* 上也是如此。

但是,BLAS 只支持浮点类型。如果您在整数数组上调用 np.dot(),numpy 将退回到使用 a very simple internal C++ implementation,这是单线程的,并且比两个浮点数组上的 BLAS 点慢得多。

在不了解您如何执行这些基准测试的情况下,我敢打赌,对 numpy.dot 的简单调用也能轻松击败其他 3 种用于 float32、complex64 和 complex128 数组的方法,它们是其他 3 种类型由 BLAS 支持。


* 击败标准 BLAS 的一种可能方法是使用 cuBLAS, which is a BLAS implementation that will run on an NVIDIA GPU. The scikit-cuda 库似乎为其提供 Python 绑定,尽管我自己从未使用过它。