在GPU上加速图像处理算法,并行处理Matlab

Accelarating image processing algorithm on GPU, parallel processing Matlab

我想加速我的算法,因为我需要 运行 它在数百张图像上,所以我尝试使用非矢量化 GPU 代码,运行在 GPU 上使用相同的代码,我有 nvidia我的电脑上有 2 GB 的 Geforce GT 650M,但是它比 CPU 版本慢很多。搜索后我确信使用批处理(pagefun,bsxfun)传递给矢量化 GPU 代码,我尝试了很多没有解决方案来解决这个问题。有人可以帮我解决这段代码吗:

Q=100;
       for i=3:n-2
        for j=3:m-2 
         A(i,j)=0;
            for c=1:Q
                        if B(i,j,c)~=0
                        A(i,j)=A(i,j)+(-(B(i,j,c)).*log(B(i,j,c)));
                        end
            end
        end
       end

另一个问题为什么 Matlab 只使用了我 CPU 的 20%?我如何利用我的 CPU 来加速我的处理

Matlab 是单线程应用程序吗?

提前致谢

您需要购买 parallel computing toolbox。 (使用 parfor)。

这是 matlab 的一个众所周知的限制,其中一些底层函数不能跨多个内核(不是线程)并行化。一个快速的大概是查看 matlab 使用了多少 CPU 并将其乘以您的 PC 中的 CORE 数量(这应该使您达到 100% 左右)。

如果您想利用计算机的 GPU,并行计算工具箱是唯一的途径。

来自mathworks

This really depends on what you are doing. For some code MATLAB can only utilize a single core of a single processor, for other code, MATLAB will automatically utilize all available cores (and maybe processors). It really depends on the underlying functions. Some things cannot be easily parallelized. Sometimes you can help MATLAB with things like parfor loops. Other times you might need something like MPI. Still other times there really is nothing you can do.

矢量化版本是这样的:

BB = B(3:(n-2),3:(m-2),:);
cutoff = 10^(-6);
logBB = log(BB);
logBB(BB<cutoff) = 0; % remove divergent terms
A = -sum(BB.*logBB,3);

即使在 CPU 上,这应该 运行 快得多。如果你有 GPU,你需要做的就是输入数组

BB = gpuArray(BB);

存储在GPU上,然后收集结果

A = gather(A);

回到CPU