faster octave and matlab code
play

Faster Octave and Matlab Code Christian Himpe ( - PowerPoint PPT Presentation

Faster Octave and Matlab Code Christian Himpe ( christian.himpe@wwu.de ) WWU Mnster Institute for Computational and Applied Mathematics 23.10.2013 Overview 1 Octave 2 Acceleration 3 Profiling 4 Miscellaneous 5 MEX Code GNU Octave What OCTAVE


  1. Faster Octave and Matlab Code Christian Himpe ( christian.himpe@wwu.de ) WWU Münster Institute for Computational and Applied Mathematics 23.10.2013

  2. Overview 1 Octave 2 Acceleration 3 Profiling 4 Miscellaneous 5 MEX Code

  3. GNU Octave What OCTAVE is: an open-source alternative (clone) to Matlab → http://octave.org reproducing some of the Matlab toolboxes (control, image, optim, multicore) → http://octave.sourceforge.net providing more (consistent) operators (++,+=,!=,*=,**,...) → http://octave.org/doc/interpreter What OCTAVE is not: compatible with all commands (but most) as fast as Matlab (but close) by default providing a GUI (but there are if you insist) Check if you are in OCTAVE: exist(’OCTAVE_VERSION’)

  4. Acceleration: Preallocate Faster Allocation bsxfun Copy-On-Write Java Linear Algebra

  5. Acceleration: tic; Preallocate for I=1:2000, Faster Allocation for J=1:2000, bsxfun x(I,J) = I + J; Copy-On-Write end; end; Java toc Linear Algebra

  6. Acceleration: tic; x = zeros(2000); Preallocate for I=1:2000, Faster Allocation for J=1:2000, x(I,J) = I + J; bsxfun end; Copy-On-Write end; Java toc Linear Algebra 28s vs 12s

  7. Acceleration: Preallocate Faster Allocation tic; bsxfun A = zeros(10000); Copy-On-Write toc Java Linear Algebra

  8. Acceleration: Preallocate tic; Faster Allocation B(10000,10000) = 0; bsxfun toc Copy-On-Write Java 0.2s vs 0.00003s Linear Algebra

  9. Acceleration: Preallocate tic; Faster Allocation A = rand(2000); bsxfun for I=1:2000, A(I,:)=A(I,:)-mean(A); Copy-On-Write end; Java toc Linear Algebra

  10. Acceleration: Preallocate tic; Faster Allocation A = rand(2000); bsxfun bsxfun(@minus,A,mean(A)); toc Copy-On-Write Java 7.3s vs 0.1s Linear Algebra

  11. Acceleration: function y = f(m) Preallocate m(1,1) = 1; Faster Allocation y = sum(sum(m)); bsxfun end Copy-On-Write tic; Java f(rand(8000)) Linear Algebra toc

  12. Acceleration: function y = f(m) y = sum(sum(m)) Preallocate y = y - m(1,1) + 1.0; Faster Allocation end bsxfun tic; Copy-On-Write f(rand(8000)) Java toc Linear Algebra 0.25s vs 0.05s

  13. Acceleration: Preallocate tic; Faster Allocation h = waitbar(0,’Wait!’); bsxfun for I=1:2000, waitbar(I/2000,h); Copy-On-Write end; Java toc Linear Algebra

  14. Acceleration: tic; Preallocate fprintf(’Wait!’); for I=1:2000, Faster Allocation fprintf(’|’); bsxfun end; Copy-On-Write fprintf(’\n’); Java toc Linear Algebra 1.5s vs 0.03s

  15. Acceleration: Preallocate tic; Faster Allocation A = rand(2000); bsxfun B = rand(2000); Copy-On-Write trace(A*B), Java toc Linear Algebra

  16. Acceleration: Preallocate tic; A = rand(2000); Faster Allocation B = rand(2000); bsxfun sum(sum(A.*B’)), Copy-On-Write toc Java 2s vs 0.2s Linear Algebra

  17. Profiling: Reproducible Randomness Timing Better Timing Static Code Analysis Code Complexity Runtime Profiling Memory Profiling

  18. Profiling: Reproducible Randomness Timing Better Timing rand(’seed’,x); Static Code Analysis Code Complexity Runtime Profiling Memory Profiling

  19. Profiling: Reproducible Randomness Timing Better Timing randn(’seed’,x); Static Code Analysis Code Complexity Runtime Profiling Memory Profiling

  20. Profiling: Reproducible Randomness Timing Better Timing tic; somecode(); Static Code Analysis toc Code Complexity Runtime Profiling Memory Profiling

  21. Profiling: Reproducible Randomness Timing T0 = cputime; Better Timing somecode(); Static Code Analysis cputime - T0 Code Complexity Runtime Profiling Memory Profiling

  22. Profiling: Reproducible Randomness Timing Better Timing mlint(’myfunc.m’); Static Code Analysis Code Complexity Runtime Profiling Memory Profiling

  23. Profiling: Reproducible Randomness Timing Better Timing mlint(’myfunc.m’,’-cyc’) Static Code Analysis Code Complexity Runtime Profiling Memory Profiling

  24. Profiling: Reproducible Randomness Timing profile on; Better Timing somecode(); Static Code Analysis profile off; Code Complexity profreport; Runtime Profiling Memory Profiling

  25. Profiling: Reproducible Randomness Timing profile -memory on; Better Timing somecode(); Static Code Analysis profile off; Code Complexity profreport; Runtime Profiling Memory Profiling

  26. Miscellaneous: Try to vectorize each for -loop! Use current versions of OCTAVE and MATLAB! OCTAVE and MATLAB use column-major matrices! Avoid implicit type-casts! Prefer shiftdim and permute over squeeze ! arrayfun can be slower than a for -loop! Do not use the Jet -colormap! Adopt a consistent coding style! i.e.: note.sonots.com/Matlab/MatlabCodingStyle.html

  27. MEX Code: MEX (Matlab EXecutable) allows to compile MATLAB scripts or enable calling C/C++ functions from MATLAB. Question you should ask yourself before writing MEX: Do I know how to use a compiler? (optimization, alignment, machine-type flags etc.) Do I know the current C/C++ standards? (move semantics, containers, constexpr) Do I know what slows down low-level code? (aliasing, branching, [wrong] caching) Do I know Static Code Analysis and Profiling for C++? (cppcheck, valgrind etc.)

  28. tl;dl Remember: 1 Correctness 2 Performance 3 Documentation 4 Compatibility 5 Readability Links: http://blogs.mathworks.com/loren http://matlabtips.com http://wiki.octave.org http://matlab.wikia.com/wiki/FAQ http://gist.github.com/gramian/6027733 Thanks!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend