MATLAB Answers

What, if anything, can be done to optimize performance for modern AMD CPU's?

296 views (last 30 days)
Wick
Wick on 20 Apr 2018
Commented: Wick on 6 Sep 2020 at 22:50
I have a AMD Threadripper CPU. I've seen suggestions that AMD-based computers will benefit from changing the math library from Intel's MKL to the AMD Core Math Library (ACML) along with recommendations to change the BLAS.SPEC file. Any threads relating to this seem quite old and it appears that AMD might have changed the name of its math library.
Is it still possible to optimize the math library for AMD's CPUs? If so, what do I need to do?
Thanks!

  26 Comments

Ned Flanders
Ned Flanders on 29 Mar 2020
@wick
Apparently 2020a includes the Workaround but setting the workaround does not work on all systems reliably yet. Can you confirm it works on your build? You possibly need to remove the sys variable before you upgrade to 2020a.
Ned
Wick
Wick on 6 Sep 2020 at 22:50
@Ned Flanders; I haven't kept my subscription up so I haven't updated for a while. Sorry.

Sign in to comment.

Accepted Answer

Ned Flanders
Ned Flanders on 18 Nov 2019
Edited: Ned Flanders on 2 Dec 2019
You should read it if you want to understand the background.
EDIT: Before you start I have a short request for you that you could help me with and serve your own interest. Matlab will not implement this. If you think that Matlab should offer a permanent solution that serves all users independent of whether they use Intel or AMD CPUs, please make a feature request at Matlab to implement a nummeric library (e.g. OpenBLAS) that does not discriminate against non Intel CPUs. Mathworks will not make this change without people advocating for it. Thanks!
tl;dr
Solution 1 (Windows): Create a .bat file using e.g. "Notepad" with the following lines to start Matlab in AVX2 Mode.
@echo off
set MKL_DEBUG_CPU_TYPE=5
matlab.exe
Save as .txt and rename to .bat. If you double-click that file, Matlab will start the MKL in AVX2 Mode. If you start it the normal way, it will remain as always.
You can also download the .bat file from my highdrive if you trust me (which typically you should not as I am a random guy from the internet). If you delete the startup batch file provided in the download or the one you created yourself, its gone and your computer will be as it has been before.
Solution 2 (Windows): If you are happy with the results (which you surely will be :-)), you should make the setting permanent by entering MKL_DEBUG_CPU_TYPE=5 into the System Environment Variables. This has several advantages, one of them being that it applies to all instances of Matlab and not just the one opened using the .bat file.
Doing this will make the change permanent and available to ALL Programs using the MKL on your system until you delete the entry again.
LINUX: (Thanks to foreignrobot)
Simply type in a terminal:
export MKL_DEBUG_CPU_TYPE=5
and then run matlab from the same terminal.
For benchmarking, you can use this script:
To make it permanent in Linux edit your shell's configuration scripts (~/.bashrc for bash, ~/.zshrc for zsh etc) adding the line export MKL_DEBUG_CPU_TYPE=5. That'll apply in any newly opened shell and to apply it in an already open one simply do . ~/.bashrc or whatever your config script name is. (Thanks to lowpolybutt)
P.S.: In case you are on an older AMD FX CPU, you could test whether MKL_DEBUG_CPU_TYPE=4 works for you. This should enable AVX, but I haven't tested this.

  15 Comments

Wick
Wick on 4 Dec 2019
@PatQuillen thanks. That would explain why BLAS_VERSION cannot be set to something within the "Program Files" directory - the default location for MKL. It's breaking the parse at the space in the folder name. I had to copy the mkl.dll to another folder to try setting it as the fall-over library.
Yair Altman
Yair Altman on 17 Dec 2019
@Wick - try setting the Program Files path within quotation marks, as in "C:\Program Files\bla\bla\blas.dll";"C:\Program Files\bla2\bla2\blast.dll"
Aldo Corbellini
Aldo Corbellini on 4 Apr 2020
@Wick - Where did you find BLIS and/or OpenBLAS .dll for win64? I would like to test these libraries on my new AMD TR. All I was able to find are some build recipes to Linux GCC compilers!
[Hi, Yair!]

Sign in to comment.

More Answers (2)

Wick
Wick on 4 Dec 2019
Edited: Wick on 4 Dec 2019
The Accepted Answer by @NedFlanders is the best solution so far. Use that one.
But in the comments of that answer, MathWorks staff member @PatQuillen shared some useful undocumented environment variables that allow anyone to hook into any external BLAS library. This has the potential to be very useful for getting other BLAS packages such as OpenBLAS or BLIS working within MATLAB. Those comments are already buried in that thread so I'm pulling them out as a stand-alone solution. I'll repeat the warning from those comments: "This comes with the same caveats that MathWorks has not qualified our products against alternate BLAS implementations, so as above, we can't confirm that using your own BLAS, such as OpenBLAS, will work correctly throughout our products."
We're going to be setting environment variables for the host operating system. This can be done in a temporary or permanent way outside of MATLAB for both Linux and Windows as can be seen in @NedFlander's reddit thread. However, I'm only going to use the setenv command to set these for only one instance of MATLAB.
MATLAB loads the BLAS library the first time it tries to execute a computation so you can make changes before that point. Once the BLAS library is loaded, there's no way to unload it. You'll have to restart.
The environment variable BLAS_VERBOSITY will set MATLAB to detail information to the Command Window about the loading process. A value of 1 is minimal information, 2 is more.
setenv('BLAS_VERBOSITY','1')
The environment variable BLAS_VERSION, if defined, will direct MATLAB to load the BLAS library from the specified file(s) rather than use its built-in default. The files should be the .so/.dll for the BLAS implementation you plan to use that follows the symbols defined by https://netlib.org/blas/.
setenv('BLAS_VERSION','c:\temp\mkl.dll')
Would load a copy of the MKL library copied into c:\temp. You can use a semicolon, comma, or space to join multiple file locations into a single definition of BLAS_VERSION. So if you're writing your own BLAS that doesn't support all the possible methods you could roll over to MKL as a failsafe, e.g.

  0 Comments

Sign in to comment.


Ken M.
Ken M. on 9 Nov 2019
You are saying don't use windows for matlab.
Since that is the easy way around, people stop complaining about it after the switch.
Many programs just work faster with linux so if speed is important then linux is an obvious choice also for matlab.

  7 Comments

Show 4 older comments
Tyler Moskov
Tyler Moskov on 23 Apr 2020
Could you go into more detail on adding export commands to link the AMD Ubuntu binaries or post a link on how to?
Thank you
Ken M.
Ken M. on 24 Apr 2020
@Tyler as of 2020a the MKL_DEBUG_CPU_TYPE=5 workaround should not longer be needed for AMD CPU's
Tyler Moskov
Tyler Moskov on 24 Apr 2020
That's what I've heard, but I still have 2019b and want to see if there are any added benified of using AMD's AOCL.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!

Translated by