Reputation: 532
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)?
Upvotes: 3
Views: 431
Reputation: 24127
OK, first of all think about C initially rather than MATLAB, because i) C is closer to the machine and ii) MATLAB is written in C (at least, the majority of the language execution engine, and almost all the numerical code is written in C - the desktop is mostly Java-based at the moment, but that's not relevant here).
So if you have a loop in C, like
for (i = 0; i < 1024; i++)
{
C[i] = A[i]*B[i];
}
this is asking the computer to one by one add the elements of A
and B
together into the array C
. However, some processors (vector processors, which most modern CPUs are) might be able to execute several of these additions at once, using a SIMD (single instruction, multiple data) instruction. So you might be able to rewrite the loop to say
for (i = 0; i < 1024; i+=4)
{
C[i:i+3] = A[i:i+3]*B[i:i+3];
}
in which case four additions would be done at once. That is a simple example of vectorized C code, by partially unrolling the loop.
It's important to note that you don't necessarily have to do this explicitly in your C code, as your C compiler is very clever. It will notice parts of your code that can be vectorized, and will rewrite those parts for you before it compiles them. The compiler is pretty good at this, but it can't pick up everything, and if you understand some about how vectorization works in your CPU, then you can give the compiler hints by structuring your code in particular ways, and you can tell the compiler explicitly to do particular things.
Note also that this vectorization, although it's a simple form of parallelism, can be used even on a single-core CPU (as long as it's a vector processor, which most modern CPUs are). The parallelism occurs at the register level of the CPU, by operating on multiple bits of data at once with the same instruction. There are other forms of parallelism that occur across cores in a multicore CPU or on a GPU.
Coming back to MATLAB now - MATLAB implements several forms of parallelism, including multithreading, explicit parallelism across cores and on a cluster, and GPU parallelism (some of which require add-on products such as Parallel Computing Toolbox). But at its core, MATLAB implements a set of highly optimized, highly vectorized C routines for numerical processing and linear algebra.
Unlike C, MATLAB is not a compiled language - it is interpreted, with a JIT compiler. But it's still looking at your code and trying to find optimizations it can use in order to execute it fast. And if you write your code in particular ways, you can help MATLAB to choose how to optimally execute it.
For example, the code
a = rand(3,4);
b = rand(4,2);
c = zeros(size(a,1),size(b,2));
for i = 1:size(a,1)
for j = 1:size(b,2)
element = 0;
for k = 1:size(a,2)
element = element + a(i,k).*b(k,j);
end
c(i,j) = element;
end
end
does the same as
a = rand(3,4);
b = rand(4,2);
d = a*b;
But in the latter case, MATLAB knows that it can call out to one of its super-optimised libraries for matrix multiplication rather than multiplying and accumulating the elements one by one. This is a simple example of vectorized MATLAB code.
Upvotes: 1
Reputation: 549
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:
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
Upvotes: 3