필터 지우기
필터 지우기

Speeding up (vectorizing) barycentric interpolation

조회 수: 2 (최근 30일)
Adam Pigon
Adam Pigon 2020년 4월 4일
댓글: darova 2020년 4월 12일
Dear All, I have the following problem:
is it possible to speed up the barycentric interpolation given below? I presume that vectorization could be particularly effective but I do not know how to do it. As the code has to be run many times (and the size of matrices involved is rather large) any increase in efficiency would be helpful. Could GPU computing, after vectorization, be of any help here?
na = 100;
nh = 100;
nm = 100;
nap = na+2;
nhp = nh+2;
H = randn(nap,nhp) ;
M = randn(nap,nhp) ;
MP = randn(nap,nhp) ;
ap_trial = NaN(nh,nm) ;
hp_trial = NaN(nh,nm) ;
mp_trial = NaN(nh,nm) ;
[h_grid,m_grid] = ndgrid(1:nh,1:nm);
[ap_grid,hp_grid] = ndgrid(1:nap,1:nhp);
tic
for r0 = 0:1
for r1 = 0:(nap-2)
for r2 = 0:(nhp-2) % loops picking up triangles constructed by adjacent points in matrices H and M
h1 = H(1+r1+r0,1+r2+r0);
h2 = H(1+r1,2+r2);
h3 = H(2+r1,1+r2);
m1 = M(1+r1+r0,1+r2+r0);
m2 = M(1+r1,2+r2);
m3 = M(2+r1,1+r2);
% barycentric interpolation (weights)
w1 = ( (m2-m3)*( h_grid -h3) + (h3-h2)*( m_grid -m3) ) / ( (m2-m3)*(h1-h3) + (h3-h2)*(m1-m3) );
w2 = ( (m3-m1)*( h_grid -h3) + (h1-h3)*( m_grid -m3) ) / ( (m2-m3)*(h1-h3) + (h3-h2)*(m1-m3) );
w3 = 1 - w1 - w2;
% preventing extrapolation
w1(w1 < 0) = NaN;
w2(w2 < 0) = NaN;
w3(w3 < 0) = NaN;
% barycentric interpolation (results)
ap = w1 * ap_grid(1+r1+r0,1+r2+r0) + w2 * ap_grid(1+r1,2+r2) + w3 * ap_grid(2+r1,1+r2);
hp = w1 * hp_grid(1+r1+r0,1+r2+r0) + w2 * hp_grid(1+r1,2+r2) + w3 * hp_grid(2+r1,1+r2);
mp = w1 * MP(1+r1+r0,1+r2+r0) + w2 * MP(1+r1,2+r2) + w3 * MP(2+r1,1+r2);
ap_trial( isnan(ap) == 0 ) = ap( isnan(ap) == 0 );
hp_trial( isnan(hp) == 0 ) = hp( isnan(hp) == 0 );
mp_trial( isnan(mp) == 0 ) = mp( isnan(mp) == 0 );
end
end
end
toc
  댓글 수: 14
Adam Pigon
Adam Pigon 2020년 4월 12일
Thank you for your idea! I have tested this improvement and my conclusions are as follows:
Matlab is pretty slow in indexing matrices (see this thread for interesting discussion: https://www.mathworks.com/matlabcentral/answers/54522-why-is-indexing-vectors-matrices-in-matlab-very-inefficient ), which means that my code needed some streamlining to achieve better performance. Including additional indices, i.e. of these points that are within the triangle, that have to be computed and implemented is costly. Therefore, dimensions nh and nm have to be large enough to achieve a good trade off beteween an additional cost of indexing and an efficiency gain from operating with smaller matrices that exploit only a fraction of matrices of size (nh x nm). In a nuthshell, if nm and nh are large enough, the efficiency gain can be sizable. An increase in nm is actually costless as nm does not show up in any of the loop sizes and therefore it doesn't put any additional indexing burden.
Thanks!
darova
darova 2020년 4월 12일
Thanks for the link

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by