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

faster octave and matlab code
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Faster Octave and Matlab Code

Christian Himpe (christian.himpe@wwu.de)

WWU Münster Institute for Computational and Applied Mathematics

23.10.2013

slide-2
SLIDE 2

Overview

1 Octave 2 Acceleration 3 Profiling 4 Miscellaneous 5 MEX Code

slide-3
SLIDE 3

GNU Octave

What OCTAVE is: an open-source alternative (clone) to Matlab → http://octave.org reproducing some of the Matlab toolboxes (control, image,

  • ptim, 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’)

slide-4
SLIDE 4

Acceleration:

Preallocate Faster Allocation bsxfun Copy-On-Write Java Linear Algebra

slide-5
SLIDE 5

Acceleration:

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

slide-6
SLIDE 6

Acceleration:

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

slide-7
SLIDE 7

Acceleration:

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

slide-8
SLIDE 8

Acceleration:

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

slide-9
SLIDE 9

Acceleration:

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

slide-10
SLIDE 10

Acceleration:

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

slide-11
SLIDE 11

Acceleration:

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

slide-12
SLIDE 12

Acceleration:

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

slide-13
SLIDE 13

Acceleration:

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

slide-14
SLIDE 14

Acceleration:

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

slide-15
SLIDE 15

Acceleration:

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

slide-16
SLIDE 16

Acceleration:

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

slide-17
SLIDE 17

Profiling:

Reproducible Randomness Timing Better Timing Static Code Analysis Code Complexity Runtime Profiling Memory Profiling

slide-18
SLIDE 18

Profiling:

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

slide-19
SLIDE 19

Profiling:

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

slide-20
SLIDE 20

Profiling:

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

slide-21
SLIDE 21

Profiling:

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

slide-22
SLIDE 22

Profiling:

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

slide-23
SLIDE 23

Profiling:

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

slide-24
SLIDE 24

Profiling:

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

slide-25
SLIDE 25

Profiling:

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

slide-26
SLIDE 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

slide-27
SLIDE 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.)

slide-28
SLIDE 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!