Massive slowdown for Apple Silicon in computing SVD

26 views (last 30 days)
I recently notice that there is an extreme slowdown in my version of Matlab while computing an SVD when the size of the matrix crosses some threshold. I came up with the following example that demonstrates my issue:
N = [10000 11000 12000 13000];
for i = 1:4
A = randn(N(i),3);
tic;
[U,S,V] = svd(A,0);
toc;
end
When I run this in Matlab R2024b (macOS Apple silicon), the output is:
Elapsed time is 0.000396 seconds.
Elapsed time is 0.000275 seconds.
Elapsed time is 0.000264 seconds.
Elapsed time is 0.083150 seconds.
Of course the exact numbers vary trial to trial, but the speed for the last run (where N = 13000) is consistently orders of magnitude slower.
When I run this same code on Matlab R2024b (Intel processor) on the same computer, this slow down does not happen. I was able to replicate this issue across two different Macs (one with M1 and another with M3) and different versions of Matlab (going back to R2023b).
Any idea why this might be happening in the silicon version?
Edit: I'm running macOS 15.1.1

Accepted Answer

Mike Croucher
Mike Croucher on 5 Dec 2024
Hi
I have reproduced your times on my M2 MacbookPro using R2024b using both the default BLAS and also the Apple Silicon BLAS as described in my blog post https://blogs.mathworks.com/matlab/2023/12/13/life-in-the-fast-lane-making-matlab-even-faster-on-apple-silicon-with-apple-accelerate/
I am not sure what causes this but have reported it to development.
Thanks for the report.
Mike

More Answers (1)

Heiko Weichelt
Heiko Weichelt on 21 Dec 2024
Thanks for reporting this.
We identified the problem and are working on improving this in a future release.
As a temporary workaround, we recommend replacing:
[U,S,V]=svd(A,0);
with
[Q,R]=qr(A,"econ"); [U,S,V]=svd(R); U=Q*U;
In general, this step is not needed as the SVD performs the QR inside itself. The LAPACK library currently used on Apple Silicon, however, had suboptimal tuning parameters for this case.
On my machine, the time for the largest example improved as following:
>> tic; [U,S,V]=svd(A,0); toc
Elapsed time is 0.086851 seconds.
>> tic; [Q,R]=qr(A,"econ"); [U,S,V]=svd(R); U=Q*U; toc
Elapsed time is 0.000977 seconds.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!