Search code examples
matlabopenmpvectorizationmulticore

How does vectorization in MATLAB work?


I wonder how MATLAB works with vectorized operations?

Data1 = fread(fin1, 10e6, 'uint8');
Data2 = fread(fin2, 10e6, 'uint8');
DiffA = diff(Data1);
DiffB = diff(Data2);

Does MATLAB use a kind of SIMD parallelism to execute this code? Does it use a vector processor (like a GPU) or it just using system cores (in a multicore system)?


Solution

  • MATLAB uses JIT to speed-up computations. I've found no explicit info on JIT, only general suggestions. In my own use cases I prefer thinking of it as Java JIT, since optimization techniques match closely. MATLAB had done 4 major steps in improving performance:

    • In R13 (2002) JIT was introduced
    • In R2006b in-place computation without temp storage was introduced, see details on page 7 of the document
    • In R2007a multithreading (in engine, not in Parallel toolbox) was introduced, in R2008 in was widely adopted
    • In R2016 was introduced new engine with JIT widely adopted

    For little more details pleasee see this blog post, the comments can be really useful. The main thing I got to know is that in newer releases "clean all" removes precompiled in the session code. Yair Altman tinkered JIT and wrote a perfect book on MATLAB performance. Some details on guts and usage of MATLAB JIT are available here. For introduction on performance I'd recommend to start with official manual