Matlab JIT engine nested loops data type

4 views (last 30 days)
Alessandro D
Alessandro D on 30 Aug 2021
Edited: DGM on 2 Sep 2021
In the textbook "Matlab programming for engineers" by S.Chapman (4th edition) it is stated that (see pp. 165-166)
  1. The JIT compiler only accelerates loops containing double, logical, and char data types (plus integer data types that we haven’t met yet). If other data types such as cell arrays or structures appear in the loop, it will not be accelerated.
  2. If an array in the loop has more than two dimensions, the loop will not be accelerated.
I would like to know if the information provided above is still accurate (I am using the newest Matlab release, R2021a).
  2 Comments
DGM
DGM on 31 Aug 2021
FWIW, 4th ed was circa late 2007, so that's pretty old.

Sign in to comment.

Answers (1)

Jan
Jan on 30 Aug 2021
Edited: Jan on 31 Aug 2021
This is a good question. Matlab's JIT was partially documented in Matlab 6.5 (2001), but afterwards MathWorks avoided to publish details. They have the good reason, that publishing details of the JIT would motivate users to adjust their programs to the JIT, but they want to adjust the JIT to the programs.
Some tests might show, that the mentioned limitations are not matching anymore.
function s = stupidTest(x)
s = 0;
for k = 1:numel(x)
s = s + x(k);
end
end
Calling this with 2D arrays has the same speed as with 3D arrays, if the number of elements is equal.
Impeding the JIT on purpose slows the code down, e.g. by s = eval('0');
Yes, the test case is very basic. But I'd avoid to adjust the code too much to assumed properties of the JIT.
  4 Comments
DGM
DGM on 2 Sep 2021
I figured I'd give it a whirl, since I'm perpetually out-of-date and able to share the perspective that comes with running older versions.
Out of the gate, I don't have enough memory to wrangle that large of a DPFP array without being pushed into swap:
800x800x800 R2019b
Using command sum:
Elapsed time is 47.013714 seconds.
Using linear index, i.e. only one loop:
Elapsed time is 177.289160 seconds.
Using subscripts, i.e. 3 nested loops:
Elapsed time is 197.953229 seconds.
I know well enough that switching to single precision often slows things down, memory usage aside. I decided to try with a smaller array to keep away from swap usage, but my experience is still the opposite of yours for R2019b and R2015b:
600x R2019b
Using command sum:
Elapsed time is 0.133028 seconds.
Using linear index, i.e. only one loop:
Elapsed time is 2.440955 seconds.
Using subscripts, i.e. 3 nested loops:
Elapsed time is 2.539198 seconds.
600x R2015b (using sum(A(:)) )
Using command sum:
Elapsed time is 0.136865 seconds.
Using linear index, i.e. only one loop:
Elapsed time is 4.313646 seconds.
Using subscripts, i.e. 3 nested loops:
Elapsed time is 7.171336 seconds.
But look at good old R2009b doing its own thing:
600x R2009b (using sum(A(:)) )
Using command sum:
Elapsed time is 0.204012 seconds.
Using linear index, i.e. only one loop:
Elapsed time is 4.270620 seconds.
Using subscripts, i.e. 3 nested loops:
Elapsed time is 2.084054 seconds.
I don't know if I'd contribute this all to version differences. Hardware and environment matter too.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!