[How To] Make Matlab (Mostly) Functional

FriedEngineer

New member
Joined
May 3, 2020
Messages
5
With a lot of insight from gtrfxdso, I went down the rabbit hole of trying to get Matlab to work. I originally posted this in the old forum but I think it would probably be useful to have here so we can continue discussions and development on it, so I copied and updated it a bit for clarity.

A few notes first
  • This solution is a bit messy but it seems to solve most issues that I've seen.
  • I have tested this on 2020a (current release as of writing) and 2019b. The same instructions work for both.
  • In my testing, this solution does survive incremental updates (from 2020a update 4 to 2020a update 5 for example) but if you do a major version change (from 2019b to 2020a for example) you will likely have to redo it, assuming that it will even work on the new version.
  • I am happy to receive any comments and criticism. If you have any ideas to improve it or if you find any issues, please post a reply!
The basics of what I had to do
  • Swap the libiomp5.dylib
  • Download and compile openBLAS with the AMD ZEN flag (TARGET=ZEN, which the OpenBLAS developers note is pretty much the same as the Haswell optimization)
  • Swap the compiled dylib with the default mkl.dylib in MATLAB/sys/os/maci64
  • Modify the blas.spec and lapack.spec files to allow it to recognize an AMD processors instead of Intel
  • Install gcc7 and change its link location in /usr/local/opt
    • The install installs gcc7 to [email protected] and openBLAS looks for it in gcc not [email protected] I don't like this manually renaming because then you can't have newer gcc versions there but I don't have a better solution right now.
Known Issues
Here's a the issues I've found that I haven't figure out how to resolve. I welcome any comments/suggestions.
  • I sometimes get an error that says Cannot untransform a point, matrix may be invalid or singular. It seems to when using math functions with super small numbers and in some GUI functions.
  • Some graphics libraries cause Matlab to crash. Seems there's some incompatibilty with the LAPCK portion of OpenBLAS. Ex: using "surf" and then trying to move the plot around.
    • Potential Workaround: If you don't modify the lapack.spec file, Matlab will just spit out a bunch of warnings (stating that it can't find the mllapack.dylib) instead of fully crashing. These can be supressed with the command warning('off'). However certain functions (ex: imfilter) won't work at all.
So you're still interested? Here's the full instructions

Two Quick Notes
  • The required precompiled libraries are in the attached zip.
  • I use "(MATLAB)" in a few places below. This is a generic placeholder for the location of your Matlab installation. This will likely be "/Applications/MATLAB_R2020a" or "/Applications/MATLAB_R2019b" (depending on what Matlab version you have)
To launch Matlab
This will allow Matlab to launch and give you a lot of functionality, but not everything (no real "math functions" that require the MKL)
  1. Open the "(MATLAB)/sys/os/maci64" (to get here right click on the application and click "show contents" and then navigate to this folder)
  2. Rename "libiomp5.dylib" to "libiomp5.dylib.bak"
  3. Copy the included "libiomp5.dylib" to the same location
To use MKL based functions
This will give you most of the remaining functionality as we are providing Matlab with some AMD compatible libraries for those functions.
  1. Install homebrew (See https://brew.sh/for installation details).
    • This makes installing the required gcc 7 easier.
    • You can start this and then do 2-3 below while it runs in the background.
    • Note: it will also install the Xcode Command Line Tools, if you don't already have it.
  2. Add the AMD compatible OpenBLAS library (note: the included library is OpenBLAS v0.3.9)
    1. Navigate to "(MATLAB)/bin/maci64"
    2. Rename "mkl.dylib" that is already there to "mkl.dylib.bak"
    3. Copy the included "mkl.dylib" to the same location
  3. Tell Matlab to use the OpenBLAS library when it finds an AMD based system (instead of the default mkl.dylib, which is Intel only)
    1. Navigate to "(MATLAB)/bin/maci64" (same folder as above, so you can just stay there)
    2. Open the files "blas.spec" and "lapack.spec" in TextEdit (or your preferred text editor)
    3. Change GenuineIntel to AuthenticAMD in both files
      • This tells Matlab what to do when it encounters an AMD processor (as it currently only knows how to handle Intel).
  4. Install gcc 7 (required by OpenBLAS)
    1. Once homebrew is finished installing (from step 1), in a terminal window (which you probably have open from installing homebrew) and type the command "brew install [email protected]" to install gcc version 7
    2. Go to /usr/local/opt (this is a system folder, not inside of the Matlab installation, so it's hidden and so I'd recommend you use Finder's "Go to Folder" command to get there)
    3. Rename the "[email protected]" folder there to "gcc".
      • This "folder" is actually just a link to the actual location gcc 7 was installed in but OpenBLAS looks in "/usr/local/opt/gcc" instead of "/usr/local/opt/[email protected]" for the gcc 7 library it needs so we need to rename it.
Change Log
Things I've changed in these instructions since originally posting them
  • 1 Sept 2020: Modified the instructions and ZIP a bit to just replace the default (Intel) MKL entirely instead of having them both be present.
    • I originally had you just copy the libopenblas.dylib library (what the compiler names it by default) and add lines to the .spec files to point to it but it seems some functions reference mkl.dylib directly (I assume those that aren't actually blas or lapack functions but do reference the mkl library), so this was not sufficient.
 

Attachments

  • Matlab on AMD.zip
    5.5 MB · Views: 58
Last edited:

PasiA

New member
Joined
Oct 19, 2020
Messages
4
Moi


This was about the only Matlab thread here. I installed opencore catalina following the opencore instructions for Ryzen. Tested Matlab, segfault 11. Followed the instructions, but it seems that in 2020b there exist some other Intel things...


Pasis-iMac-Pro:bin ppaalto$ ./matlab -nodisplay -nojvm -Dlldb
(lldb) target create "/Applications/MATLAB_R2020b.app/bin/maci64/MATLAB"
Current executable set to '/Applications/MATLAB_R2020b.app/bin/maci64/MATLAB' (x86_64).
(lldb) settings set -- target.run-args "-nodisplay" "-nojvm"
(lldb) run
Process 2249 launched: '/Applications/MATLAB_R2020b.app/bin/maci64/MATLAB' (x86_64)
Process 2249 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8)
frame #0: 0x000000010422cdc1 libtbbmalloc.dylib`_intel_fast_memset.A + 1
libtbbmalloc.dylib`_intel_fast_memset.A:
-> 0x10422cdc1 <+1>: callq 0x10422ce10 ; _intel_fast_memset
0x10422cdc6 <+6>: popq %rcx
0x10422cdc7 <+7>: retq
0x10422cdc8 <+8>: nopl (%rax,%rax)
Target 0: (MATLAB) stopped.
 

FriedEngineer

New member
Joined
May 3, 2020
Messages
5
This was about the only Matlab thread here. I installed opencore catalina following the opencore instructions for Ryzen. Tested Matlab, segfault 11. Followed the instructions, but it seems that in 2020b there exist some other Intel things...
Yeah, that's possible; I haven't tested 2020b yet. I'm still on 2020a (and I don't really plan on updating as currently it's mostly stable and I need it that way for grad school) and I haven't seen that error before. If you could PM me the code that triggered this so I can replicate it that would help a lot.
 

PasiA

New member
Joined
Oct 19, 2020
Messages
4
Yeah, that's possible; I haven't tested 2020b yet. I'm still on 2020a (and I don't really plan on updating as currently it's mostly stable and I need it that way for grad school) and I haven't seen that error before. If you could PM me the code that triggered this so I can replicate it that would help a lot.
The segfault comes when I start Matlab. According to the debug log the reason is usage of intel_fast_memset. Most likely I have a problem with the Catalina installation... The same thing happens with all resonable new Matlab versions.
 

PasiA

New member
Joined
Oct 19, 2020
Messages
4
The segfault comes when I start Matlab. According to the debug log the reason is usage of intel_fast_memset. Most likely I have a problem with the Catalina installation... The same thing happens with all resonable new Matlab versions.

Sorry, the last sentence is wrong. 2020a runs with your instructions, great. So only problem will be the file libtbbmalloc.dylib in 2020b...
 

FriedEngineer

New member
Joined
May 3, 2020
Messages
5
The segfault comes when I start Matlab. According to the debug log the reason is usage of intel_fast_memset.
Okay, I misunderstood then. I thought it was an issue after launch.
Sorry, the last sentence is wrong. 2020a runs with your instructions, great. So only problem will be the file libtbbmalloc.dylib in 2020b...
The error you saw does indicate that, so I'll try and look into it.

Most likely I have a problem with the Catalina installation
What makes you think that?
 

PasiA

New member
Joined
Oct 19, 2020
Messages
4
What makes you think that?

Sorry, nothing any more. I guess with Matlab the issue is the same as with some Adobe products, the usage of Intel specific libraries. OpenMP, BLAS and LAPACK are still easy to fix since they are commonly available. However Matlab's own libraries are not available as source...
 

zero_b

New member
Joined
Apr 14, 2021
Messages
2
Hey, thanks very much for your contribution! I'm not sure why but I could not install [email protected] with Homebrew it shows the error below.

Do you know any workarounds? I've tried to google but found nothing similar. Also tried to install gcc through some other ways but it seems the gcc folder structure has changed..

 

zero_b

New member
Joined
Apr 14, 2021
Messages
2
Hey, thanks very much for your contribution! I'm not sure why but I could not install [email protected] with Homebrew it shows the error below.

Do you know any workarounds? I've tried to google but found nothing similar. Also tried to install gcc through some other ways but it seems the gcc folder structure has changed..

Solved it. It is a BigSur specific problem. run brew doctor and then run the command it provides allows the install.
 

jackokie

New member
Joined
Nov 16, 2020
Messages
1
Hi, the matlab can work after following your instructions. However, there's still a problem that confusing me:
================================================================================
MATLAB_maci64(82710,0x700008540000) malloc: can't allocate region
:*** mach_vm_map(size=1122635731701760, flags: 100) failed (error code=3)
MATLAB_maci64(82710,0x700008540000) malloc: *** set a breakpoint in malloc_error_break to debug
Memery is not enough。

Error: sqrtm (line 40)
[Q, T] = schur(A);
================================================================================

If the dimension of A is (2*2), then the schur function can work.
If I cnange the dimension to (3*3), it will throw this error.
This is really important to me, could you please help me to solve this problem?
 
Top Bottom