performance - matlab矩阵运算速度

标签 performance matlab matrix matrix-multiplication

我被要求让一些 MATLAB 代码运行得更快,并且遇到了一些对我来说似乎很奇怪的事情。

在其中一个函数中有一个循环,我们将一个 3x1 向量(我们称它为 x)与一个 3x3 矩阵(我们称它为 A)相乘 - 以及x 的转置,产生一个标量。代码有整套的逐个元素的乘法和加法,相当繁琐:

val = x(1)*A(1,1)*x(1) + x(1)*A(1,2)*x(2) + x(1)*A(1,3)*x(3) + ...
      x(2)*A(2,1)*x(1) + x(2)*A(2,2)*x(2) + x(2)*A(2,3)*x(3) + ... 
      x(3)*A(3,1)*x(1) + x(3)*A(3,2)*x(2) + x(3)*A(3,3)*x(3);

我想我只需将其全部替换为:

val = x*A*x';

令我惊讶的是,它的运行速度明显变慢(慢了 4-5 倍)。难道只是向量和矩阵太小以至于 MATLAB 的优化不适用?

最佳答案

编辑:我改进了测试以提供更准确的时间。我还优化了展开版本,它现在比我最初拥有的版本好得多,但随着大小的增加,矩阵乘法仍然更快。

EDIT2:为确保 JIT 编译器正在处理展开的函数,我修改了代码以将生成的函数编写为 M 文件。此外,现在可以将比较视为公平的,因为通过将 TIMEIT 传递给函数句柄来评估两种方法:timeit(@myfunc)


对于合理的大小,我不相信您的方法比矩阵乘法更快。因此,让我们比较一下这两种方法。

我正在使用 Symbolic Math Toolbox 来帮助我获得 x'*A*x 方程的“展开”形式(尝试手动乘以 20x20 矩阵和 20x1 向量!):

function f = buildUnrolledFunction(N)
    % avoid regenerating files, CCODE below can be really slow!
    fname = sprintf('f%d',N);
    if exist([fname '.m'], 'file')
        f = str2func(fname);
        return
    end

    % construct symbolic vector/matrix of the specified size
    x = sym('x', [N 1]);
    A = sym('A', [N N]);

    % work out the expanded form of the matrix-multiplication
    % and convert it to a string
    s = ccode(expand(x.'*A*x));    % instead of char(.) to avoid x^2

    % a bit of RegExp to fix the notation of the variable names
    % also convert indexing into linear indices: A(3,3) into A(9)
    s = regexprep(regexprep(s, '^.*=\s+', ''), ';$', '');
    s = regexprep(regexprep(s, 'x(\d+)', 'x($1)'), 'A(\d+)_(\d+)', ...
        'A(${ int2str(sub2ind([N N],str2num($1),str2num($2))) })');

    % build an M-function from the string, and write it to file
    fid = fopen([fname '.m'], 'wt');
    fprintf(fid, 'function v = %s(A,x)\nv = %s;\nend\n', fname, s);
    fclose(fid);

    % rehash path and return a function handle
    rehash
    clear(fname)
    f = str2func(fname);
end

我试图通过避免求幂来优化生成的函数(我们更喜欢 x*x 而不是 x^2 )。我还将下标转换为线性索引( A(9) 而不是 A(3,3) )。因此,对于 n=3,我们得到与您相同的等式:

>> s
s =
A(1)*(x(1)*x(1)) + A(5)*(x(2)*x(2)) + A(9)*(x(3)*x(3)) + 
A(4)*x(1)*x(2) + A(7)*x(1)*x(3) + A(2)*x(1)*x(2) + 
A(8)*x(2)*x(3) + A(3)*x(1)*x(3) + A(6)*x(2)*x(3)

鉴于上述构造 M 函数的方法,我们现在评估它的各种大小并将其与 matrix-multiplication 形式进行比较(我将它放在一个单独的函数中以考虑函数调用开销)。我正在使用 TIMEIT 函数而不是 tic/toc 来获得更准确的计时。此外,为了进行公平比较,每个方法都作为一个 M 文件函数实现,该函数将所有需要的变量作为输入参数传递。

function results = testMatrixMultVsUnrolled()
    % vector/matrix size
    N_vec = 2:50;
    results = zeros(numel(N_vec),3);
    for ii = 1:numel(N_vec);
        % some random data
        N = N_vec(ii);
        x = rand(N,1); A = rand(N,N);

        % matrix multiplication
        f = @matMult;
        results(ii,1) = timeit(@() feval(f, A,x));

        % unrolled equation
        f = buildUnrolledFunction(N);
        results(ii,2) = timeit(@() feval(f, A,x));

        % check result
        results(ii,3) = norm(matMult(A,x) - f(A,x));
    end

    % display results
    fprintf('N = %2d: mtimes = %.6f ms, unroll = %.6f ms [error = %g]\n', ...
        [N_vec(:) results(:,1:2)*1e3 results(:,3)]')
    plot(N_vec, results(:,1:2)*1e3, 'LineWidth',2)
    xlabel('size (N)'), ylabel('timing [msec]'), grid on
    legend({'mtimes','unrolled'})
    title('Matrix multiplication: $$x^\mathsf{T}Ax$$', ...
        'Interpreter','latex', 'FontSize',14)
end

function v = matMult(A,x)
    v = x.' * A * x;
end

结果:

timing timing_closeup

N =  2: mtimes = 0.008816 ms, unroll = 0.006793 ms [error = 0]
N =  3: mtimes = 0.008957 ms, unroll = 0.007554 ms [error = 0]
N =  4: mtimes = 0.009025 ms, unroll = 0.008261 ms [error = 4.44089e-16]
N =  5: mtimes = 0.009075 ms, unroll = 0.008658 ms [error = 0]
N =  6: mtimes = 0.009003 ms, unroll = 0.008689 ms [error = 8.88178e-16]
N =  7: mtimes = 0.009234 ms, unroll = 0.009087 ms [error = 1.77636e-15]
N =  8: mtimes = 0.008575 ms, unroll = 0.009744 ms [error = 8.88178e-16]
N =  9: mtimes = 0.008601 ms, unroll = 0.011948 ms [error = 0]
N = 10: mtimes = 0.009077 ms, unroll = 0.014052 ms [error = 0]
N = 11: mtimes = 0.009339 ms, unroll = 0.015358 ms [error = 3.55271e-15]
N = 12: mtimes = 0.009271 ms, unroll = 0.018494 ms [error = 3.55271e-15]
N = 13: mtimes = 0.009166 ms, unroll = 0.020238 ms [error = 0]
N = 14: mtimes = 0.009204 ms, unroll = 0.023326 ms [error = 7.10543e-15]
N = 15: mtimes = 0.009396 ms, unroll = 0.024767 ms [error = 3.55271e-15]
N = 16: mtimes = 0.009193 ms, unroll = 0.027294 ms [error = 2.4869e-14]
N = 17: mtimes = 0.009182 ms, unroll = 0.029698 ms [error = 2.13163e-14]
N = 18: mtimes = 0.009330 ms, unroll = 0.033295 ms [error = 7.10543e-15]
N = 19: mtimes = 0.009411 ms, unroll = 0.152308 ms [error = 7.10543e-15]
N = 20: mtimes = 0.009366 ms, unroll = 0.167336 ms [error = 7.10543e-15]
N = 21: mtimes = 0.009335 ms, unroll = 0.183371 ms [error = 0]
N = 22: mtimes = 0.009349 ms, unroll = 0.200859 ms [error = 7.10543e-14]
N = 23: mtimes = 0.009411 ms, unroll = 0.218477 ms [error = 8.52651e-14]
N = 24: mtimes = 0.009307 ms, unroll = 0.235668 ms [error = 4.26326e-14]
N = 25: mtimes = 0.009425 ms, unroll = 0.256491 ms [error = 1.13687e-13]
N = 26: mtimes = 0.009392 ms, unroll = 0.274879 ms [error = 7.10543e-15]
N = 27: mtimes = 0.009515 ms, unroll = 0.296795 ms [error = 2.84217e-14]
N = 28: mtimes = 0.009567 ms, unroll = 0.319032 ms [error = 5.68434e-14]
N = 29: mtimes = 0.009548 ms, unroll = 0.339517 ms [error = 3.12639e-13]
N = 30: mtimes = 0.009617 ms, unroll = 0.361897 ms [error = 1.7053e-13]
N = 31: mtimes = 0.009672 ms, unroll = 0.387270 ms [error = 0]
N = 32: mtimes = 0.009629 ms, unroll = 0.410932 ms [error = 1.42109e-13]
N = 33: mtimes = 0.009605 ms, unroll = 0.434452 ms [error = 1.42109e-13]
N = 34: mtimes = 0.009534 ms, unroll = 0.462961 ms [error = 0]
N = 35: mtimes = 0.009696 ms, unroll = 0.489474 ms [error = 5.68434e-14]
N = 36: mtimes = 0.009691 ms, unroll = 0.512198 ms [error = 8.52651e-14]
N = 37: mtimes = 0.009671 ms, unroll = 0.544485 ms [error = 5.68434e-14]
N = 38: mtimes = 0.009710 ms, unroll = 0.573564 ms [error = 8.52651e-14]
N = 39: mtimes = 0.009946 ms, unroll = 0.604567 ms [error = 3.41061e-13]
N = 40: mtimes = 0.009735 ms, unroll = 0.636640 ms [error = 3.12639e-13]
N = 41: mtimes = 0.009858 ms, unroll = 0.665719 ms [error = 5.40012e-13]
N = 42: mtimes = 0.009876 ms, unroll = 0.697364 ms [error = 0]
N = 43: mtimes = 0.009956 ms, unroll = 0.730506 ms [error = 2.55795e-13]
N = 44: mtimes = 0.009897 ms, unroll = 0.765358 ms [error = 4.26326e-13]
N = 45: mtimes = 0.009991 ms, unroll = 0.800424 ms [error = 0]
N = 46: mtimes = 0.009956 ms, unroll = 0.829717 ms [error = 2.27374e-13]
N = 47: mtimes = 0.010210 ms, unroll = 0.865424 ms [error = 2.84217e-13]
N = 48: mtimes = 0.010022 ms, unroll = 0.907974 ms [error = 3.97904e-13]
N = 49: mtimes = 0.010098 ms, unroll = 0.944536 ms [error = 5.68434e-13]
N = 50: mtimes = 0.010153 ms, unroll = 0.984486 ms [error = 4.54747e-13]

在小尺寸情况下,这两种方法的表现有些相似。虽然对于 N<7 扩展版本胜过 mtimes ,但差别不大。一旦我们越过微小尺寸,矩阵乘法就会快几个数量级。

这并不奇怪;只有 N=20formula 长得吓人,涉及添加 400 个术语。由于解释了 MATLAB 语言,我怀疑这是否非常有效。


现在我同意调用外部函数与直接内嵌代码相比会产生开销,但这种方法的实用性如何。即使对于像 N=20 这样的小尺寸,生成的行也超过 7000 个字符!我还注意到 MATLAB 编辑器由于排长队而变得迟缓 :)

此外,在 N>10 之后优势很快消失。我比较了嵌入式代码/显式编写与矩阵乘法,类似于@DennisJaheruddin 的建议。 results:

N=3:
  Elapsed time is 0.062295 seconds.    % unroll
  Elapsed time is 1.117962 seconds.    % mtimes

N=12:
  Elapsed time is 1.024837 seconds.    % unroll
  Elapsed time is 1.126147 seconds.    % mtimes

N=19:
  Elapsed time is 140.915138 seconds.  % unroll
  Elapsed time is 1.305382 seconds.    % mtimes

...对于展开的版本,情况只会变得更糟。正如我之前所说,MATLAB 是解释型的,因此解析代码的成本开始显示在如此大的文件中。

在我看来,在进行了一百万次迭代之后,我们最多只获得了 1 秒的时间,我认为这并不能证明所有的麻烦和黑客攻击都是合理的,而不是使用更具可读性和简洁性的 v=x'*A*x 。所以 perhaps 代码中还有其他地方可以改进,而不是专注于已经优化的操作,例如矩阵乘法。

MATLAB 中的 Matrix multiplication is seriously fast(这是 MATLAB 最擅长的!)。一旦您获得足够大的数据(如 multithreading 启动),它就会真正发光:

>> N=5000; x=rand(N,1); A=rand(N,N);
>> tic, for i=1e4, v=x.'*A*x; end, toc
Elapsed time is 0.021959 seconds.

关于performance - matlab矩阵运算速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18524039/

相关文章:

python - 没有箭头 3d 箭袋图 python

matlab - 查找 .mat 文件之间的所有差异

matlab - 模式识别中绘图的相关问题(第1部分)

python - 提高在二维 numpy 数组中查找最小元素的速度,该数组有许多条目设置为 np.inf

ios - session 重启后 AVcapture session 启动缓慢

c++ - 使用索引安全方式迭代 STL 容器以避免使用锁?

javascript - javascript 中循环和 CPS 循环的完全奇怪的性能

SQL不等于或为null

javascript - 使用JS组织毡尖笔: optimizing the arrangement of items in a 2D grid by similarity of adjacent items, [更新]

python - Theano:如何获取元素为矩阵的 "matrix outer product"