有人能帮忙吗?我是一名经验丰富的Matlab用户,但在加速以下代码方面遇到了麻烦。
使用12个核心,我能够实现通过所有三个循环的最快时间是~200秒。实际函数将被调用~720次,按此速度执行需要超过40小时。根据Matlab分析器,大部分CPU时间用于指数函数调用。我已经成功地使用gpuArray加速了这个过程,然后在Quadro 4000图形卡上运行exp调用,但这将阻止使用parfor循环,因为工作站只有一个图形卡,这会抵消任何收益。有人能帮忙吗?或者这段代码是否接近使用Matlab可以实现的最佳状态?我已经使用openMP编写了非常简单的C++实现,但几乎没有提高。
非常感谢您的帮助。
使用12个核心,我能够实现通过所有三个循环的最快时间是~200秒。实际函数将被调用~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
0.5*myarray
会破坏它。复数指数是数学密集型的,很容易比内存访问时间更长。此外,减少内存占用可以帮助提高带宽。最后,许多GPU在双精度和单精度之间的性能差异达到20倍。 - Mikhail