MATLAB速度优化

jac*_*ack 6 parallel-processing performance matlab

有人可以帮忙吗?我是一位相当有经验的Matlab用户,但我在加速下面的代码时遇到了麻烦.

我使用12个内核在所有三个循环中运行的最快时间是〜200s.实际功能将被调用~720次,并且以此速率执行需要40多个小时.根据Matlab分析器,大部分cpu时间都花在指数函数调用中.我已经设法使用gpuArray大幅提高了速度,然后在Quadro 4000显卡上运行exp调用,但这会阻止parfor循环的使用,因为工作站只有一个显卡,可以消除任何收益.任何人都可以提供帮助,或者这段代码是否接近使用Matlab可以实现的最佳值?我用openMP编写了一个非常粗略的c ++实现,但收效甚微.

提前谢谢了

function SPEEDtest_CPU

% Variable setup:
% - For testing I'll use random variables. These will actually be fed into 
%   the function for the real version of this code.
sy    = 320;
sx    = 100;
sz    = 32;
A     = complex(rand(sy,sx,sz),rand(sy,sx,sz));
B     = complex(rand(sy,sx,sz),rand(sy,sx,sz));
C     = rand(sy,sx);
D     = rand(sy*sx,1);
F     = zeros(sy,sx,sz);
x     = rand(sy*sx,1);  
y     = rand(sy*sx,1);
x_ind = (1:sx) - (sx / 2) - 1;
y_ind = (1:sy) - (sy / 2) - 1;


% MAIN LOOPS 
%  - In the real code this set of three loops will be called ~720 times!
%  - Using 12 cores, the fastest I have managed is ~200 seconds for one
%    call of this function.
tic
for z = 1 : sz
    A_slice = A(:,:,z);
    A_slice = A_slice(:);
    parfor cx = 1 : sx       
        for cy = 1 : sy       
            E = ( x .* x_ind(cx) ) + ( y .* y_ind(cy) ) + ( C(cy,cx) .* D );                                                          

            F(cy,cx,z) = (B(cy,cx,z) .* exp(-1i .* E))' * A_slice; 
        end       
    end   
end
toc

end
Run Code Online (Sandbox Code Playgroud)

Mik*_*ail 3

需要考虑的一些事情:

您考虑过使用单打吗?

您能否对 cx、cy 部分进行向量化,以便它们代表数组运算?

考虑更改浮点舍入或信号模式。

  • 单身会让你的记忆力下降。但是,基于此我认为这会阻止优化:http://www.ee.columbia.edu/~marios/matlab/accel_matlab.pdf,当然测试不会有什么坏处。 (2认同)