优化值N分裂阵列开矢量化阵列,使得它运行的最快(Optimizing the value N to

2019-10-22 17:07发布

我试图优化值N来分割阵列了这么运行最快的不同机器上的矢量化的阵列。 下面我有一些测试代码

#example use random values
clear all,
t=rand(1,556790);
inner_freq=rand(8193,6);

N=100; # use N chunks
nn = int32(linspace(1, length(t)+1, N+1))
aa_sig_combined=zeros(size(t));
total_time_so_far=0;
for ii=1:N
    tic;
    ind = nn(ii):nn(ii+1)-1;
    aa_sig_combined(ind) = sum(diag(inner_freq(1:end-1,2)) * cos(2 .* pi .* inner_freq(1:end-1,1) * t(ind)) .+ repmat(inner_freq(1:end-1,3),[1 length(ind)]));
    toc
    total_time_so_far=total_time_so_far+sum(toc)
end
fprintf('- Complete  test in %4.4fsec or %4.4fmins\n',total_time_so_far,total_time_so_far/60);

这需要162.7963sec或2.7133mins完成当对16gig i7的机器运行Ubuntu N = 100

有没有办法找出N值应该得到这个不同的机器上跑的最快?

PS:我的16gig i7处理器的Ubuntu 14.04运行倍频3.8.1,但它也将甚至1G的树莓派2运行。

Answer 1:

这是Matlab的测试脚本,我用时间的每个参数。 返回用作它看起来像迭代的其余部分类似于第一迭代后打破它。

%example use random values
clear all;
t=rand(1,556790);
inner_freq=rand(8193,6);

N=100; % use N chunks
nn = int32( linspace(1, length(t)+1, N+1) );
aa_sig_combined=zeros(size(t));

D = diag(inner_freq(1:end-1,2));
for ii=1:N
    ind = nn(ii):nn(ii+1)-1;
    tic;
    cosPara = 2 * pi * A * t(ind);
    toc;
    cosResult = cos( cosPara );
    sumParaA = D * cosResult;
    toc;
    sumParaB = repmat(inner_freq(1:end-1,3),[1 length(ind)]);
    toc;
    aa_sig_combined(ind) = sum( sumParaA + sumParaB );
    toc;
    return;
end

输出被表示为如下。 请注意,我有一个缓慢的电脑。

Elapsed time is 0.156621 seconds.
Elapsed time is 17.384735 seconds.
Elapsed time is 17.922553 seconds.
Elapsed time is 18.452994 seconds.

正如你所看到的,COS操作是什么要花这么长。 您正在运行cos在8192x5568矩阵(45613056元),这是有道理的,它需要这么长时间。

如果你想提高性能,使用parfor因为它似乎每个迭代是独立的。 假设你有100个内核,运行100次迭代,你的脚本将17秒+做parfor开销。

cos的计算,你可能想看看,如果其他方法存在计算值的COS更快,更并行比股票方法。

另一个小优化是此行。 这将确保在diag功能不被循环内称为对角矩阵是恒定的。 你不想每次都可以产生8192x8192的对角矩阵! 我只是将其存储在循环外,它给出了一个有点性能提升的为好。

D = diag(inner_freq(1:end-1,2));

请注意,我没有使用Matlab的轮廓,因为它没有为我工作,但你应该使用在未来的多种官能化的代码。



文章来源: Optimizing the value N to split arrays up for vectorizing an array so it runs the quickest