Why is Matlab so slow? IAP 2009 Scott Gorlin
[email protected]
htt!"##stellar.mit.ed#S#!ro$ect#ad%anced&matlab#
'%eryone Gets (rstrated )
Why does it ta*e +ore%er to do ,?
)
-ther a!!lications analye my data +aster/
'%eryone Gets (rstrated )
Why does it ta*e +ore%er to do ,?
)
-ther a!!lications analye my data +aster/
'%eryone Gets (rstrated )
Why only 01??
3he 4rse o+ 4om!ilation )
)
4om!ters don5t s!ea* 'nglish 'nglish commands 67m&+iles58 mst either be interpreted or compiled
3he 4rse o+ 4om!ilation )
4om!iled ) (ast langages 6i.e. 48 are compiled to machine code: which trns them into e,ectables ) Slow creation time: +ast e,ection time
3he 4rse o+ 4om!ilation )
4om!iled ) Static memory memory ; declare all %ariables: ty!es ) Can’t change memory ty!es: add#remo%e %ariables ) Se!arate +nctions +or int: doble: etc
3he 4rse o+ 4om!ilation )
Inter!reted ) '%ery command immediately translated: e,ected at rn&time )
00 %s 0 s !er command
) Allows dynamic memory addressing
3he 4rse o+ 4om!ilations )
Inter!reted ) ',am!les o+ dynamic bene+it" ) ) ) )
on5t need to declare#init %ariables 4an interacti%ely rn code 6console: gi5s8 Immediately !date code 6anonymos +nctions8 Wor* with dynamic data ty!es 6strctres: cells8
) rawbac* ) 3a*es longer to inter!ret commands than to e,ecte them
-!timiation )
Bectorie/ ) Get rid o+ for loo!s ) fib = [1 1 2 3 5 8 13 21 34 ...]; ) s = sum(fib);
instead o+ )
i for i
= 1:length(fib)
s = s+fib(i); end
-!timiation )
Bectorie... ) 4an get o%erly com!le, 'g:
Inter!reters )
CI3 ) Cst&In&3ime com!ilation ) Matlab immediately com!iles !arts o+ +nctions when they are called: be+ore rnning ) Small !&+ront cost 6ms8 +irst time a +nction is called ) Mch +aster e,ection +or some commands ) 's!. loo!s
-!timiation )
What no one *nows" ) CI3 ma*es %ectoriing obsolete ) 6sometimes8
-!timiation )
(or 0&000, s!eed!: write CI3&com!liant code
)
-n Stellar" accelDmatlab.!d+ describes details o+ this
)
Shaded" Accelerated data ty!es 6as o+ %E.8
-!timiation )
4ell %s doble arrays ) E0 +old acceleration ) W#o CI3: is >F
-!timiation )
ere: the for loo! loo! is made CI3 com!liant by sing scalar inde,ing instead o+ a %ector
)
In +act: here the CI3 code is faster than the %ectoried code
)
W#o CI3: st ratio ratio is
-!timiation )
CI3 3a*ehome" ) Inter!reting is !ower+l bt SH-W ) Write code which can be 7com!iled5 ) Gi%e ! some +le,ibility +or a lot o+ s!eed
) 3o test code com!liance" ) >>
feature accel off
) e,ecte +nction againJ: note time di++erence
-!timiation )
CI3 4heat Sheet ) All code in a loo! mst be com!liant +or loo! to be com!iled ) S!!orted data ty!es ) Ie int#doble#char arrays: not cells#strcts
) ) ) )
=ilt&in 6non m&code8 Matlab commands (or loo!s" scalar inde,es i+#switch#while" scalar e,!ressions F or +ewer dimensions !er array
-!timiation )
CI3
K dimensional arrays (nction calls Inter!reted commands#dynamic allocation Pblishing: e%alating w# L&clic*: console commands ) In some %ersions: sing i: j: e withot withot de+ining them +irst ) 4alling +nction w# di++erent data ty!es 6+orces
-!timiation )
CI3 6n8(ortnate 3rth" ) It is VERY POORLY docmented ) See original s!ec 6online8: then read &liners in the release notes +or e%ery %ersion o+ Matlab since E.
) =t: CI3 im!ro%es in e%ery %ersion ) Ie console accelerated in N.E
) oesnOt wor* as well on -S: Hin,: bt getting better ) Bectorie i+ !ossible: when nsre
-!timiation )
)
Hocal %ariables co!ied when !assed to +nctions
-!timiation )
4o!y&-n&Write ) Bariables not really co!ied co!ied ntil they are changed ) Passing or renaming a %ariable does not increase memory i+ the %ariable is nchanged
-!timiation )
In&Place -!erations ) (nction mst retrn the same %ariable itOs !assed: both in de+inition and when called
'%eryone Gets (rstrated )
Why only 01??
Mltithreading )
A com!ter can only do thing at once
)
Start Windows: e%erything +reees/
Mltithreading )
)
Mltithreading ; illsion o+ 7concrrent5 !rocesses -S ra!idly switches between 7threads5 and allows each to wor* +or a brie+ !eriod o+ time
Mltithreading )
Side note" ) Windows is a !oor schedler: meaning threads do not always begin when they shold ) 3his leads to timing $itters: or random delays in Matlab ! to se%eral hndred ms ) 4an be hel!ed by 7realtime5 !riority" Matlab thread ta*es !recedence o%er other threads ) 3as* Manager &Q Processes &Q MA3HA=.e,e L&clic* and set !riority to Lealtime ) 4an be done !rogramatically throgh a Ca%a#4 +nction: or download Psychtoolbo, +or an im!lementation ) -n a single core: will (L''R' windows ntil !rocess com!letes
Mltithreading )
Parallel Programming ) 2 or more threads to simltaneosly do something ) -n a single&core com!ter gi%es simltaneos e,ection ) I.e.: one thread handles GI: other does bac*grond calclations
) -n a dal&core com!ter: ideally: dobles !er+ormance
Mltithreading )
Matlab is Single&3hreaded ) Means yo can only do thing at once ) -n newer: dal&core com!ters this is not o!timal
Mltithreading )
Matlab is Single&3hreaded )
Mltithreading )
Matlab is Single&3hreaded ) =ene+its ) on5t worry abot Thread Safety ) -n a al 4ore: setting Lealtime Priority lets Matlab dominate one core: and the rest o+ the com!ter rns on the other
) We can 7hac*5 mltithreading: some o+ the time
Mltithreading )
+nction Sim!lest hac* ; timer +nction ) 3his actally creates a Ca%a ob$ect: and e,ectes a callbac* +nction with %ariable delay: inter%al: etc ) owe%er: not a tre mltithread ; will not e,ecte while another !rocess is dominating 6at least throgh %N.K8 ) 3here+ore mostly se+l in GI#console a!!lications: etc ) <=" Brief testing in %N.E indicates it may be asynchronos now )
Mltithreading )
3re !arallel !rogramming ) Mst either start a 2 nd Matlab Matlab session: ) 3wo Matlab sessions can rn in !arallel %ia 4-M: Ca%a: or shared memory s!aces ) Actally a client#ser%er inter+ace ; one session will dis!atch $obs to the other
) Write a 4#Ca%a a!!let +or nati%e threading: ) 4om!licated bt !ower+l ) -r: se the distribted com!ting toolbo, ) : not !ro%ided with +ree MI3 stdent bndle
Atomation Ser%er ) ) ) ) )
Sim!lest !arallel Matlab e,am!le See ',ternal Inter+aces#4-M Atomation Ser%er +or docmentation Starts 2nd Matlab Matlab session %ia 4-M#Acti%e 6Windows only: sadly8 4-M !rotocol may be too slow +or yo
Atomation Ser%er )
4reate ser%er with" • h = !t"ser#er($mtlb.%%li!ti&n$); • fiel's(h) meth&'s(h) show ways to control
new Matlab ser%er. Im!ortant are" • "e!ute(h *!&mmn') • ,e#l(h *f!n-me num&ut rg1 rg2 ) • /ut,ull0tri" et,ull0tri" :
etc
) 4an e%en hide window: bt ma*e sre to later close it !rogramatically • h.isible = ; ... h.uit();
Atomation Ser%er )
=ene+its )
Atomation Ser%er )
=3" ) =y de+alt: still single threaded ) 4alling ',ecte and (e%al starts +nction in ser%er wor*s!ace: bt client Matlab pauses and and waits +or com!letion ) Ma*es sense +or (e%al6waits +or a retrn %ale8 bt not +or ',ecte ) Mst +ind a way to retrn control to client while ser%er com!tes
Atomation Ser%er )
Lemember timer()? ) ',ecte or call a +nction which starts a timer in the new wor*s!ace ) 3his will allow client to retrn: and ser%er will !rocess timer thread immediately a+ter ) Main !roblem" still a SH-W !rotocol ) ) )
ata trans+er not too +ast Min >Fms sim!ly to in%o*e +nction in ser%er and retrn May or may not be +ast enogh +or yor a!!lication
Parallel Matlab )
=etter: more com!licated ) Mltithread in Ca%a#4&me, code ) -!en 2nd Matlab: Matlab: bt commnicate %ia a di++erent !rotocol ) Soc*ets ) Ca%a LMI between the CBM5s ) )
Actally Tite sim!le ; code !osted online online in MatlabLMI.i! Also enables the second Matlab session to rn anywhere on your networ : +or a trly distribted !rogram !rogram ) May rn with Matlab 4om!onent Lntime 6ntested8
) Shared memory throgh 4&me, )
I ha%e no idea how to do this
) MPI
) istribted com!ting toolbo, ) Mst !rchase: I ha%e no e,!erience with so cannot recommend either way ) Seems good +or !arallel array o!erations: batching identical tas*s across wor*er !ool ; bt not designed +or concrrent !rogramming
-!timiation )
3a*e home K ste!s +or +aster !rograms ) CI3 com!liant code ) Memory management ) 4o!y&-n&Write ) Pass&In&Place
) Write a !arallel !rogram ) Write a Ca%a#4 ob$ect