Beruflich Dokumente
Kultur Dokumente
htt!"##stellar.mit.ed #S#!ro$ect#ad%anced&matlab#
4om! ters don5t s!ea* 'nglish 'nglish commands 67m&+iles58 m st either be interpreted or compiled
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
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
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
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
)
-!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
)
-!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
-!timi.ation
)
) ) ) )
= 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
)
-!timi.ation
)
) 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
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
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
M ltithreading
)
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
)
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
)
) 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
) )
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
) 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
)
(riday
)