Speeding up code: vectorization and others
    2 ビュー (過去 30 日間)
  
       古いコメントを表示
    
Hi,
Is there a way to speed this up even more? I did what I could already: everything is precomputed, preallocated etc. and compiled to .mex.
First, just to give you an idea about sizes of continers:
bessels = ones(1201, 1201, 101);    % 1.09 GB
negMcontainer = ones(1201, 1201, 100);
posMcontainer = negMcontainer;
maxN = 100;
levels = maxN + 1;
xElements = 1201;
Aj1 = complex(ones(101, 101);
Aj2 = Aj1;
Code:
parfor i = 1 : xElements
    for j = 1 : xElements
        umn = complex(zeros(levels, levels)); % cleaning
        for n = 0:maxN
            mm = 1;
            for m = -n:2:n
                nn = n + 1; % for indexing
                if m < 0
                    umn(nn, mm) = bessels(i, j, nn) * negMcontainer(i, j, abs(m));
                end
                if m > 0
                    umn(nn, mm) = bessels(i, j, nn) * posMcontainer(i, j, m);
                end
                if m == 0
                    umn(nn, mm) = bessels(i, j, nn);
                end
                mm = mm + 1; % for indexing
            end % m
        end % n
        beta1 = sum(sum(Aj1.*umn));
        betaSumSq1(i, j) = abs(beta1).^2;
        beta2 = sum(sum(Aj2.*umn));
        betaSumSq2(i, j) = abs(beta2).^2;
  end % j
end % i
Best regards, Alex
採用された回答
  per isakson
      
      
 2016 年 8 月 24 日
        
      編集済み: per isakson
      
      
 2016 年 8 月 24 日
  
      Your code choked my computer. R2016a, no parallel toolbox, no mex. However, regarding the innermost loop
for m = -n:2:n
end
- Move nn = n + 1; outside the loop. Helps a little bit (tic,toc).
 - Replace the three if-statements by a if-elseif-elseif-else-end. Again small effect. However, 30% faster when profiling.
 - Split the loop into two loops one with m<0 and one with m>0 and remove the if-statements. Note: odd and even n. Again small effect. However, when profiling the elapse time was cut in half.
 
Conclusion: The "JIT/Accelerator" don't need my help in these cases. However, the "JIT/Accelerator" is less effective together with the profiler.
5 件のコメント
  per isakson
      
      
 2016 年 8 月 27 日
				
      編集済み: per isakson
      
      
 2016 年 8 月 28 日
  
			I don't know what to say. I would have guessed that your vectorization would be faster than the loop. However, loops are much faster in current releases of Matlab than they were when we learned that loops to should always be avoided.
Lesson learned
- It's difficult to be smarter than the "JIT/Acceletator"
 - The function, profile, cannot always be trusted
 
その他の回答 (0 件)
参考
カテゴリ
				Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
			
	製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
