Speeding up this function involving 3D matrix multiplications.
조회 수: 4 (최근 30일)
이전 댓글 표시
I have this function that is called several times over in my project. Running the code analyer shows that this is the bottleneck. There are a lot of 3D matrix multplicaions in here. I was looping to evaluate it first, but then someone suggested vectorizing it. However, the only function I could find that helped me is pagemtimes(). Is there a way I can speed this up further? My aim is to deploy this on real time experiment, that is why speed is of paramount importance!
Function:
function [fun] = evalP(input, xd, d, pars, Wo, Ph)
% input - 67x1000 double
% xd - 2x1 double
% d - scalar
% pars - 7x1 double
% Wo - 2x4 double
% Ph - 4x19 double
dim = size(Ph, 2);
sz = size(input, 2);
x = zeros(2,1,sz); w1 = zeros(4,1,sz); w2 = w1; dq = zeros(dim, 1, sz); u = dq; q = dq; fun = zeros(67, sz);
x(:, 1, :) = input(1:2, :);
w1(:, 1, :) = input(3:6, :); w2(:, 1, :) = input(7:10, :);
dq(:, 1, :) = input(11:29, :); u(:, 1, :) = input(30:48, :); q(:, 1, :) = input(49:67, :);
wh = cat(2, w1, w2);
f1 = x + d * pagemtimes(Wo * Ph, u);
common_term = pagemtimes(Ph, pagemtimes(pagemtimes(dq, permute(dq, [2,1,3])), Ph'));
f2 = w1 + d * -pars(1) * permute( pagemtimes(permute(w1 - Wo(1, :)', [2,1,3]), common_term), [2,1,3]);
f3 = w2 + d * -pars(1) * permute( pagemtimes(permute(w2 - Wo(2, :)', [2,1,3]), common_term), [2,1,3]);
f4 = dq + d * (-pars(7)*dq + u) + sqrt(d) * pars(6) * randn(dim, 1, sz);
f5 = u + d * -pars(2) * ( pagemtimes( pagemtimes(pagemtimes(Ph', pagemtimes(wh, permute(wh, [2,1,3]))), Ph) + pars(3)*eye(dim), u) - pars(4) * pagemtimes(pagemtimes(Ph', wh), (xd - x)) ) + sqrt(d) * pars(5) * randn(dim, 1, sz);
f6 = q + d * u;
fun(:,:) = cat(1, f1, f2, f3, f4, f5, f6);
end
Edit: This MATLAB function is trying to compute a function value for 1000 data points (input). W is a 2x4x1000 double. input here is [x; w1; w2; dq; u; q] where W is factored as w1 and w2 to vectorize it. p_i are the pars(i)
댓글 수: 2
James Tursa
2024년 2월 12일
Can you add some explanatory text stating what the calculation is, maybe with a general equation etc.? I would note that all of your permute( ) and indexing and cat( ) stuff results in deep data copies (performance drag), so my first suggestion would be to rearrange your data up front to maybe avoid some or all of this, but you could help us by showing at a higher level what the calculations are doing.
채택된 답변
Matt J
2024년 2월 12일
편집: Matt J
2024년 2월 12일
Don't use permute(___,[2,1,3]) to transpose the pages in conjunction with pagemtimes. Use the transposition flags,
댓글 수: 4
Matt J
2024년 2월 12일
I have similar concerns about f5, but to know how best to handle it, we need to know what you plan to use it for
But one thing that's for sure is you will not build the matrix if you are merely going to be multiplying it with u. You will instead re-express the product as,
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!