Beruflich Dokumente
Kultur Dokumente
http://stellar.mit.edu/S/project/advanced-matlab/
Computers dont speak English English commands (m-files) must either be interpreted or compiled
Compiled
Fast languages (i.e. C++) are compiled to machine code, which turns them into executables Slow creation time, fast execution time
Compiled
Static memory declare all variables, types
Cant change memory types, add/remove variables Separate functions for int, double, etc!
Interpreted
Every command immediately translated, executed at run-time No up-front time, much slower execution time
NB: ~ 500 vs 10 s per command
Interpreted
Examples of dynamic benefit:
Dont need to declare/init variables Can interactively run code (console, guis) Immediately update code (anonymous functions) Work with dynamic data types (structures, cells)
Drawback
Takes longer to interpret commands than to execute them!
Optimization
Vectorize
Get rid of for loops
fib = [1 1 2 3 5 8 13 21 34 ...]; s = sum(fib);
instead of
for i = 1:length(fib) s = s+fib(i); end
Optimization
Vectorize...
Can get overly complex! Eg,
Interpreters
JIT
Just-In-Time compilation Matlab immediately compiles parts of functions when they are called, before running Small up-front cost (ms) first time a function is called Much faster execution for some commands
Esp. loops
Optimization
Optimization
For 10-1000x speedup, write JIT-compliant code! On Stellar: accel_matlab.pdf describes details of this Shaded: Accelerated data types (as of v6.5)
Optimization
Optimization
Here, the for loop is made JIT compliant by using scalar indexing instead of a vector In fact, here the JIT code is faster than the vectorized code! W/o JIT, 1st ratio is 1
Optimization
JIT Takehome:
Interpreting is powerful but SLOW Write code which can be compiled
Give up some flexibility for a lot of speed!
Optimization
Built-in (non m-code) Matlab commands For loops: scalar indexes if/switch/while: scalar expressions 3 or fewer dimensions per array
Optimization
Optimization
Doesn't work as well on OSX, Linux, but getting better Vectorize if possible, when unsure
Optimization
Local variables copied when passed to functions Not entirely true (see links under Materials)
Copy-On-Write Pass-In-Place
Optimization
Copy-On-Write
Variables not really copied until they are changed Passing or renaming a variable does not increase memory if the variable is unchanged!
Optimization
In-Place Operations
Function must return the same variable it's passed, both in definition and when called
Multithreading
Multithreading
Multithreading illusion of concurrent processes OS rapidly switches between threads and allows each to work for a brief period of time
Multithreading
Side note:
Windows is a poor scheduler, meaning threads do not always begin when they should This leads to timing jitters, or random delays in Matlab up to several hundred ms Can be helped by realtime priority: Matlab thread takes precedence over other threads
Task Manager -> Processes -> MATLAB.exe R-click and set priority to Realtime Can be done programatically through a Java/C function, or download Psychtoolbox for an implementation On a single core, will FREEZE windows until process completes!
Multithreading
Parallel Programming
2 or more threads to simultaneously do something On a single-core computer gives simultaneous execution
I.e., one thread handles GUI, other does background calculations
Multithreading
Matlab is Single-Threaded
Means you can only do 1 thing at once On newer, dual-core computers this is not optimal
Multithreading
Matlab is Single-Threaded
New versions have multi-threading features This is a misnomer only a multithreaded BLAS
Faster matrix operations (sometimes), but that's it
Multithreading
Matlab is Single-Threaded
Benefits
Dont worry about Thread Safety On a Dual Core, setting Realtime Priority lets Matlab dominate one core, and the rest of the computer runs on the other!
Multithreading
Multithreading
Write a C/Java applet for native threading, Complicated but powerful Or, use the distributed computing toolbox $$, not provided with free MIT student bundle
Automation Server
Simplest parallel Matlab example See External Interfaces/COM Automation Server for documentation Starts 2nd Matlab session via COM/ActiveX (Windows only, sadly) COM protocol may be too slow for you
Automation Server
Can even hide window, but make sure to later close it programatically!
h.Visible = 0; ... h.Quit();
Automation Server
Benefits
Native Matlab interface, easy to use, get/send data directly through Matlab
Automation Server
BUT:
By default, still single threaded!! Calling Execute and Feval starts function in server workspace, but client Matlab pauses and waits for completion! Makes sense for Feval(waits for a return value) but not for Execute Must find a way to return control to client while server computes!
Automation Server
Remember timer()?
Execute or call a function which starts a timer in the new workspace! This will allow client to return, and server will process timer thread immediately after Main problem: still a SLOW protocol
Data transfer not too fast Min ~3ms simply to invoke function in server and return May or may not be fast enough for your application
Parallel Matlab
Multithread in Java/C-mex code Open 2nd Matlab, but communicate via a different protocol
Sockets Java RMI between the JVMs
Actually quite simple code posted online in MatlabRMI.zip Also enables the second Matlab session to run anywhere on your network, for a truly distributed program May run with Matlab Component Runtime (untested) I have no idea how to do this!
MPI
Must purchase, I have no experience with so cannot recommend either way Seems good for parallel array operations, batching identical tasks across worker pool but not designed for concurrent programming
Optimization
Friday