5

A question asked today gave a surprising result with respect to implicit array creation:

array1 = 5*rand(496736,1);
array2 = 25*rand(9286,1);
output = zeros(numel(array1), numel(array2)); % Requires 34GB RAM
output = zeros(numel(array1), numel(array2),'logical'); % Requires 4.3GB RAM
output = abs(bsxfun(@minus, array1.', array2)) <= 2; % Requires 34GB RAM
output = pdist2(array1(:), array2(:)) <= 2; % Requires 34GB RAM

So far so good. An array containing 496736*9286 double values should be 34GB, and a logical array containing the same amount of elements requires just 4.3GB (8 times smaller). This happens for the latter two because they use an intermediate matrix containing all the distance pairs in double precision, requiring the full 34GB, whereas a logical matrix is directly pre-allocated as just logicals, and needs 4.3GB.

The surprising part comes here:

output = abs(array1.' - array2); % Requires 34GB RAM
output = abs(array1.' - array2) <= 2; % Requires 4.3GB RAM ?!?

What?!? Why doesn't implicit expansion require the same 34GB RAM due to creation of the intermediate double matrix output = abs(array1.' - array2)?

This is especially strange since implicit expansion is, as I understood it, a short way of writing the old bsxfun solutions. Thus why does bsxfun create the full, 34GB, matrix, whereas implicit expansion does not?

Does MATLAB somehow recognise the output of the operation should be a logical matrix?


All tests performed on MATLAB R2018b, Ubuntu 18.04, 16GB RAM (i.e. 34GB arrays error out)

12
  • 4
    Some glorious hint from the bsxfun doc: Compared to using bsxfun, implicit expansion offers faster speed of execution, better memory usage, and improved readability of code. Guess, MathWorks put the magic right there, and of course, there's no documentation on that. Maybe predicting the proper output type from parsing arguments from outer to inner is a thing now!? Hopefully, some non-MathWorks people can provide some more information.
    – HansHirse
    Sep 16, 2019 at 11:32
  • 1
    @HansHirse That's a very interesting hint indeed. However, as for speed of execution I don't see a big difference. I tested that in the past, and the difference was small, and only noticeable for small arrays. This is in agreement with what Steve Eddins said. I've just repeated the tests in R2019a, and the results are the same
    – Luis Mendo
    Sep 16, 2019 at 13:02
  • 5
    Is it possible that the JIT computes compound statements element-wise? (I mean, d=a+b+c not being computed as tmp=a+b; d=tmp+c, but rather as d(i)=a(i)+b(i)+c(i)? Anyone with the ability to test this (max memory usage for example)? Sep 16, 2019 at 13:29
  • 1
    trimming array1 to fourth of its length allows executing the code on my machine, and it does NOT use the same RAM, lots of RAM for lots of output, much less RAM for logical output. I'd guess element-wise theory is true Sep 16, 2019 at 13:46
  • 1
    Some hints from julia.Maybe MATLAB has chosen other methods like splitting data and computing chunk by chunk.
    – rahnema1
    Sep 16, 2019 at 14:44

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.