Sie sind auf Seite 1von 40

Why is Matlab so slow?

IAP 2009 Scott Gorlin gorlins@mit.ed

htt!"##stellar.mit.ed #S#!ro$ect#ad%anced&matlab#

'%eryone Gets (r strated


)

Why does it ta*e +ore%er to do ,? -ther a!!lications analy.e my data +aster/

'%eryone Gets (r strated


)

Why only 001?2?2

3he 4 rse o+ 4om!ilation


)

4om! ters don5t s!ea* 'nglish 'nglish commands 67m&+iles58 m st either be interpreted or compiled

3he 4 rse o+ 4om!ilation


)

4om!iled
) (ast lang ages 6i.e. 4998 are compiled to machine code: which t rns them into e,ec tables ) Slow creation time: +ast e,ec tion time

3he 4 rse o+ 4om!ilation


)

4om!iled
) Static memory ; declare all %ariables: ty!es
) Cant change memory ty!es: add#remo%e %ariables ) Se!arate + nctions +or int: do ble: etc2

3he 4 rse o+ 4om!ilation


)

Inter!reted
) '%ery command immediately translated: e,ec ted at r n&time ) <o !&+ront time: much slower e,ec tion time
) <=" > 000 %s ?0 @s !er command

) Allows dynamic memory addressing

3he 4 rse o+ 4om!ilations


)

Inter!reted
) ',am!les o+ dynamic bene+it"
) ) ) ) Aon5t need to declare#init %ariables 4an interacti%ely r n code 6console: g i5s8 Immediately !date code 6anonymo s + nctions8 Wor* with dynamic data ty!es 6str ct res: cells8

) Arawbac*
) 3a*es longer to inter!ret commands than to e,ec te them2

-!timi.ation
)

Bectori.e/
) Get rid o+ for loo!s
) fib = [1 1 2 3 5 8 13 21 34 ...]; ) s = sum(fib);

instead o+
) for i = 1:length(fib) s = s+fib(i); end

-!timi.ation
)

Bectori.e...
) 4an get o%erly com!le,2 'g:

Inter!reters
)

CI3
) C st&In&3ime com!ilation ) Matlab immediately com!iles !arts o+ + nctions when they are called: be+ore r nning ) Small !&+ront cost 6ms8 +irst time a + nction is called ) M ch +aster e,ec tion +or some commands
) 's!. loo!s

-!timi.ation
)

What no one *nows"


) CI3 ma*es %ectori.ing obsolete2
) 6sometimes8

-!timi.ation
)

(or ?0&?000, s!eed !: write CI3&com!liant code2 -n Stellar" accelDmatlab.!d+ describes details o+ this Shaded" Accelerated data ty!es 6as o+ %E.08

-!timi.ation
)

4ell %s do ble arrays


) E0 +old acceleration2 ) W#o CI3: is >F

-!timi.ation
)

Gere: the for 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 %ectori.ed code2 W#o CI3: ?st ratio is ?

-!timi.ation
)

CI3 3a*ehome"
) Inter!reting is !ower+ l b t SH-W ) Write code which can be 7com!iled5
) Gi%e ! some +le,ibility +or a lot o+ s!eed2

) 3o test code com!liance"


) >> feature accel off

) Ie,ec te + nction againJ: note time di++erence

-!timi.ation
)

CI3 4heat Sheet


) All code in a loo! m st be com!liant +or loo! to be com!iled ) S !!orted data ty!es
) Ie int#do ble#char arrays: not cells#str cts

) ) ) )

= 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

-!timi.ation
)

CI3 <o&nos 6%ersion#!lat+orm de!endent8


) ) ) ) K9 dimensional arrays ( nction calls Inter!reted commands#dynamic allocation P blishing: e%al ating w# L&clic*: console commands ) In some %ersions: sing i: j: e witho t de+ining them +irst ) 4alling + nction w# di++erent data ty!es 6+orces recom!ile8

-!timi.ation
)

CI3 6Mn8(ort nate 3r th"


) It is VERY POORLY doc mented
) See original s!ec 6online8: then read ?&liners in the release notes +or e%ery %ersion o+ Matlab since E.0

) = t: CI3 im!ro%es in e%ery %ersion


) Ie console accelerated in N.E

) AoesnOt wor* as well on -SP: Hin ,: b t getting better ) Bectori.e i+ !ossible: when ns re

-!timi.ation
)

Hocal %ariables co!ied when !assed to + nctions <ot entirely tr e/ 6see lin*s nder Materials8
) 4o!y&-n&Write ) Pass&In&Place

-!timi.ation
)

4o!y&-n&Write
) Bariables not really co!ied ntil they are changed ) Passing or renaming a %ariable does not increase memory i+ the %ariable is nchanged2

-!timi.ation
)

In&Place -!erations
) ( nction m st ret rn the same %ariable itOs !assed: both in de+inition and when called

'%eryone Gets (r strated


)

Why only 001?2?2

M ltithreading
)

A com! ter can only do ? thing at once Start Windows: e%erything +ree.es/

M ltithreading
)

M ltithreading ; ill sion o+ 7conc rrent5 !rocesses -S ra!idly switches between 7threads5 and allows each to wor* +or a brie+ !eriod o+ time

M ltithreading
)

Side note"

) Windows is a !oor sched ler: meaning threads do not always begin when they sho ld ) 3his leads to timing $itters: or random delays in Matlab ! to se%eral h ndred 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 thro gh a Ca%a#4 + nction: or download Psychtoolbo, +or an im!lementation ) -n a single core: will (L''R' windows ntil !rocess com!letes2

M ltithreading
)

Parallel Programming
) 2 or more threads to sim ltaneo sly do something ) -n a single&core com! ter gi%es sim ltaneo s e,ec tion
) I.e.: one thread handles GMI: other does bac*gro nd calc lations

) -n a d al&core com! ter: ideally: do bles !er+ormance

M ltithreading
)

Matlab is Single&3hreaded
) Means yo can only do ? thing at once ) -n newer: d al&core com! ters this is not o!timal

M ltithreading
)

Matlab is Single&3hreaded
) <ew %ersions ha%e 7m lti&threading5 +eat res ) 3his is a misnomer ; only a m ltithreaded =HAS
) (aster matri, o!erations 6sometimes8: b t thatOs it

M ltithreading
)

Matlab is Single&3hreaded
) =ene+its
) Aon5t worry abo t Thread Safety ) -n a A al 4ore: setting Lealtime Priority lets Matlab dominate one core: and the rest o+ the com! ter r ns on the other2

) We can 7hac*5 m ltithreading: some o+ the time

M ltithreading
)

Sim!lest hac* ; timer +

nction

) 3his act ally creates a Ca%a ob$ect: and e,ec tes a callbac* + nction with %ariable delay: inter%al: etc ) Gowe%er: not a tr e m ltithread ; will not e,ec te while another !rocess is dominating2 6at least thro gh %N.K8
) 3here+ore mostly se+ l in GMI#console a!!lications: etc ) <=" Brief testing in %N.E indicates it may be asynchrono s now2 ) <ot recommended to b ild tr e !arallel a!!lications with this method...

M ltithreading
)

3r e !arallel !rogramming
) M st either start a 2nd Matlab session:
) 3wo Matlab sessions can r n in !arallel %ia 4-M: Ca%a: or shared memory s!aces ) Act ally 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 b t !ower+ l ) -r: se the distrib ted com! ting toolbo, ) SS: not !ro%ided with +ree MI3 st dent b ndle

A tomation Ser%er
) ) ) ) )

Sim!lest !arallel Matlab e,am!le See ',ternal Inter+aces#4-M A tomation Ser%er +or doc mentation Starts 2nd Matlab session %ia 4-M#Acti%eP 6Windows only: sadly8 4-M !rotocol may be too slow +or yo

A tomation Ser%er
)

4reate ser%er with"


h = !t"ser#er($m tl b. %%li! ti&n$); fiel's(h)( meth&'s(h) show ways to control new Matlab ser%er. Im!ortant are"
)"e!ute(h( *!&mm n'+) ,e# l(h( *f!n- me+( num&ut( rg1( rg2( .) /ut,ull0 tri"( 1et,ull0 tri": etc

) 4an e%en hide window: b t ma*e s re to later close it !rogramatically2


h.2isible = 3; ... h.4uit();

A tomation Ser%er
)

=ene+its
) <ati%e Matlab inter+ace: easy to se: get#send data directly thro gh Matlab

A tomation Ser%er
)

=M3"
) =y de+a lt: still single threaded22 ) 4alling ',ec te and (e%al starts + nction in ser%er wor*s!ace: b t client Matlab pauses and waits +or com!letion2 ) Ma*es sense +or (e%al6waits +or a ret rn %al e8 b t not +or ',ec te ) M st +ind a way to ret rn control to client while ser%er com! tes2

A tomation Ser%er
)

Lemember timer()?
) ',ec te or call a + nction which starts a timer in the new wor*s!ace2 ) 3his will allow client to ret rn: and ser%er will !rocess timer thread immediately a+ter ) Main !roblem" still a SH-W !rotocol
) Aata trans+er not too +ast ) Min >Fms sim!ly to in%o*e + nction in ser%er and ret rn ) May or may not be +ast eno gh +or yo r a!!lication

Parallel Matlab
)

=etter: more com!licated

) M ltithread in Ca%a#4&me, code ) -!en 2nd Matlab: b t comm nicate %ia a di++erent !rotocol
) Soc*ets ) Ca%a LMI between the CBM5s
) )

) Shared memory thro gh 4&me,


)

Act ally T ite sim!le ; code !osted online in MatlabLMI..i! Also enables the second Matlab session to r n anywhere on your networ : +or a tr ly distrib ted !rogram ) May r n with Matlab 4om!onent L ntime 6 ntested8 I ha%e no idea how to do this2

) Aistrib ted com! ting toolbo,

) MPI

) M st ! 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 ; b t not designed +or conc rrent !rogramming

-!timi.ation
)

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

(riday
)

-b$ect -riented Programming

Das könnte Ihnen auch gefallen