matlab for-loop的矢量化

时间:2015-10-19 11:29:11

标签: matlab matrix vectorization determinants

我正在寻找以下matlab函数的正确向量化,以通过多线程消除for循环和增益速度。

size(A) = N - 按 - N,其中30 <= N <= 60

1e4 <= numIter <= 1e6

function val=permApproxStochSquare(A,numIter)
%// A       ... input square non-negative matrix
%// numIter ... number of interations

N=size(A,1);

alpha=zeros(numIter,1);
for curIter=1:numIter
    U=randn(N,N);
    B=U.*sqrt(A);
    alpha(curIter)=det(B)^2;
end

val=mean(alpha);
end

1 个答案:

答案 0 :(得分:3)

总结评论中对两个版本代码的讨论,这些版本稍微改善了性能:

使用评论中的多个想法,代码需要大约1/3的时间:

N=size(A,1);
%precompute sqrt(A)
sA=sqrt(A);
alpha=zeros(numIter,1);
parfor curIter=1:numIter
    %vectorizing rand did not improve the performance because it increased communitcation when combined with parfor
    U=randn(N,N);
    B=U.*sA;
    alpha(curIter)=det(B);
end
%moved calculation out of the loop to vectorize
val=mean(alpha.^2);

另一种方法,尽可能使用for循环进行矢量化只会对性能进行小改进:

N=size(A,1);
%precompute sqrt(A)
sA=sqrt(A);
alpha=zeros(numIter,1);
%using a for, a vectorized rand outside the loop is faster.
U=randn(N,N,numIter);
B=bsxfun(@times,U,sA);
for curIter=1:numIter
    alpha(curIter)=det(B(:,:,curIter));
end
val=mean(alpha.^2);