matlab - CPU 和 GPU 中的 SVD 速度

标签 matlab matrix cuda svd arrayfire

我正在 Matlab R2014a 中测试 svd,似乎没有 CPU vs GPU 加速.我使用的是 GTX 460 卡和 Core 2 duo E8500

这是我的代码:

%test SVD
n=10000;
%host
Mh= rand(n,1000);
tic
%[Uh,Sh,Vh]= svd(Mh);
svd(Mh);
toc
%device
Md = gpuArray.rand(n,1000);
tic
%[Ud,Sd,Vd]= svd(Md);
svd(Md);
toc

此外,运行时间因运行而异,但 CPUGPU 版本大致相同。为什么没有加速?

下面是一些测试

for i=1:10
    clear;
    m= 10000;
    n= 100;
    %host
    Mh= rand(m,n);
    tic
    [Uh,Sh,Vh]= svd(Mh);
    toc
    %device
    Md = gpuArray.rand(m,n);
    tic
    [Ud,Sd,Vd]= svd(Md);
    toc
end

>> test_gpu_svd
Elapsed time is 43.124130 seconds.
Elapsed time is 43.842277 seconds.
Elapsed time is 42.993283 seconds.
Elapsed time is 44.293410 seconds.
Elapsed time is 42.924541 seconds.
Elapsed time is 43.730343 seconds.
Elapsed time is 43.125938 seconds.
Elapsed time is 43.645095 seconds.
Elapsed time is 43.492129 seconds.
Elapsed time is 43.459277 seconds.
Elapsed time is 43.327012 seconds.
Elapsed time is 44.040959 seconds.
Elapsed time is 43.242291 seconds.
Elapsed time is 43.390881 seconds.
Elapsed time is 43.275379 seconds.
Elapsed time is 43.408705 seconds.
Elapsed time is 43.320387 seconds.
Elapsed time is 44.232156 seconds.
Elapsed time is 42.984002 seconds.
Elapsed time is 43.702430 seconds.


for i=1:10
    clear;
    m= 10000;
    n= 100;
    %host
    Mh= rand(m,n,'single');
    tic
    [Uh,Sh,Vh]= svd(Mh);
    toc
    %device
    Md = gpuArray.rand(m,n,'single');
    tic
    [Ud,Sd,Vd]= svd(Md);
    toc
end

>> test_gpu_svd
Elapsed time is 21.140301 seconds.
Elapsed time is 21.334361 seconds.
Elapsed time is 21.275991 seconds.
Elapsed time is 21.582602 seconds.
Elapsed time is 21.093408 seconds.
Elapsed time is 21.305413 seconds.
Elapsed time is 21.482931 seconds.
Elapsed time is 21.327842 seconds.
Elapsed time is 21.120969 seconds.
Elapsed time is 21.701752 seconds.
Elapsed time is 21.117268 seconds.
Elapsed time is 21.384318 seconds.
Elapsed time is 21.359225 seconds.
Elapsed time is 21.911570 seconds.
Elapsed time is 21.086259 seconds.
Elapsed time is 21.263040 seconds.
Elapsed time is 21.472175 seconds.
Elapsed time is 21.561370 seconds.
Elapsed time is 21.330314 seconds.
Elapsed time is 21.546260 seconds.

最佳答案

一般来说,SVD 是一个难以并行化的例程。您可以检查 here 使用高端 Tesla 卡,加速不是很可观。

你有一张 GTX460 卡 - Fermi architecture 。该卡针对游戏(单精度计算)而非 HPC( double 计算)进行了优化。 单精度/ double 吞吐量比为 12。因此该卡具有 873 GFLOPS SP/72 GFLOPS DP。检查 here .

所以如果 Md 数组使用 double 元素,那么它的计算会相当慢。此外,在调用 CPU 例程时,很有可能会利用所有 CPU 内核,从而减少在 GPU 上运行例程的可能 yield 。此外,在 GPU 运行中,您需要花时间将缓冲区传输到设备

根据 Divakar 的建议,您可以使用 Md = single(Md)将您的数组转换为单精度并再次运行基准测试。您可以尝试使用更大的数据集大小来查看是否有变化。我不希望在您的 GPU 上通过此例程获得太多 yield 。

更新 1:

你贴出结果后,我看到DP/SP时间比是2。在CPU这边这是正常的,因为你可以少装2倍double SSE 寄存器中的值。然而,在 GPU 端只有 2 的比率意味着 gpu 代码没有充分利用 SM 内核——因为理论比率是 12。换句话说,我本希望 SP 性能更好优化代码,与 DP 相比。好像不是这样的。

关于matlab - CPU 和 GPU 中的 SVD 速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26797226/

相关文章:

python - 通过 python distutils 编译带有可重定位设备代码的 cuda 代码(用于 python c 扩展)

cuda - AMD 相当于 CUDA 驱动程序 API?

matlab - Matlab中计算图像的边缘点

matlab - 在matlab中绘制具有渐变灰度颜色的圆

c++ - 在使用矩阵进行计算时删除 C++ 二维 vector 行

matlab - MATLAB 中的邻域分析

c - 如何使用 Embedded Coder 在 MATLAB 中内联 Level-2 .m S-Functions

matlab - 支持向量机参数 matlab

opencv - 在 OpenCV 中有效判断一幅图像是否完全由另一幅图像的像素值组成

cuda - nvprof 输出 : "No kernels were profiled" mean, 是什么以及如何修复它