[How To] Make Matlab (Mostly) Functional

FriedEngineer

New member
AMD OS X 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 gcc@7 and openBLAS looks for it in gcc not gcc@7. 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 gcc@7" 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 "gcc@7" 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/gcc@7" 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: 94
Last edited:

RyzenPro

Member
AMD OS X Member
Joined
Aug 23, 2020
Messages
31
Hey, thanks dude
 

PasiA

New member
AMD OS X 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
AMD OS X 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
AMD OS X 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
AMD OS X 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
AMD OS X 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
AMD OS X 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
AMD OS X Member
Joined
Apr 14, 2021
Messages
2
Hey, thanks very much for your contribution! I'm not sure why but I could not install gcc@7 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
AMD OS X Member
Joined
Apr 14, 2021
Messages
2
Hey, thanks very much for your contribution! I'm not sure why but I could not install gcc@7 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
AMD OS X Member
Joined
Nov 16, 2020
Messages
3
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?
 

tomnic

Member
AMD OS X Member
Joined
Nov 1, 2020
Messages
42
1781426711_MLinstaller.png.7ae6bac11eaced840f9eeb93cc38e48b.pngML amd.png1235328986_MLinstalled.png.e801273c7c6fde0c884c800b97f99377.png

Hi everybody! Everything works patching directly the native libtbb.dylib, libtbbmalloc.dylib, libiomp5.dylib, mkl.dylib and changing the string GenuineIntel to AuthenticAMD in blas.spec and lapack.spec. As soon as possible I'll post the patched libs. No need for openblas and gcc!
 
Last edited:

tomnic

Member
AMD OS X Member
Joined
Nov 1, 2020
Messages
42
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?

With native patched libs your function works:

Schermata 2021-10-31 alle 17.32.38.png
 

abduu

New member
AMD OS X Member
Joined
Dec 26, 2020
Messages
3
Last edited:

tomnic

Member
AMD OS X Member
Joined
Nov 1, 2020
Messages
42
The fix is divided in two parts, as a prerequisite disable SIP for now:


1. Fix the installer

A. Download pacifist https://www.charlessoft.com/cgi-bin/pacifist_download.cgi?vers=3.6.2&type=zip install it and after mounting the matlab_R2021b_maci64.dmg go inside the icon InstallForMacOSX.app -> Show package contents -> Contents -> MacOS, open InstallForMacOSX with Pacifist;

B. Drag the InstallForMacOSX root folder to your desktop and rename it to InstallForMacOSX;

C. Overwrite the two files libtbb.dylib and libtbbmalloc.dylib inside Desktop -> InstallForMacOSX -> bin -> maci64 with my two patched revisions attached here;

D. Launch InstallForMacOSX.app inside your extracted desktop InstallForMacOSX folder and install whatever component you want.



2. Fix the actual installed app

A. Go inside your Applications folder -> MATLAB_R2021b.app -> sys -> os -> maci64 and replace the libiomp5.dylib with my patched revision attached here;

B. Again go inside the Application folder -> MATLAB_R2021b.app -> bin -> maci64 and replace libtbb.dylib, libtbbmalloc.dylib, mkl.dylib, blas.spec and lapack.spec too;



Launch MATLAB_R2021b.app and issue the command t=bench to execute a functional test and a benchmark for the most common functions.



Feel free to post your screenshots here for comparison, this is mine after the latest tweaking:

ML.thumb.png.b13ec51c8072e5e62e79fbdcf3f4cc18.png


Notes:

1. The patched libs are the original libs supplied with this particular version of Matlab but patched to only redirect properly the Intel Fastmem / Fastmemcpy routines on AMD Hackintoshes and to mimic a "true" Intel CPU;

2. The new fixes make this function perfectly working, even when A is a 3x3 matrix:

[Q, T] = schur(A);
 

Attachments

  • blas.spec.zip
    733 bytes · Views: 28
  • lapack.spec.zip
    742 bytes · Views: 28
  • libiomp5.dylib.zip
    1,012.3 KB · Views: 32
  • libtbb.dylib.zip
    197.7 KB · Views: 36
  • libtbbmalloc.dylib.zip
    111.8 KB · Views: 33
  • mkl.dylib.zip
    22.7 MB · Views: 32

rmarmmzmxmr

New member
AMD OS X Member
Joined
Oct 29, 2021
Messages
1
The fix is divided in two parts, as a prerequisite disable SIP for now:


1. Fix the installer

A. Download pacifist https://www.charlessoft.com/cgi-bin/pacifist_download.cgi?vers=3.6.2&type=zip install it and after mounting the matlab_R2021b_maci64.dmg go inside the icon InstallForMacOSX.app -> Show package contents -> Contents -> MacOS, open InstallForMacOSX with Pacifist;

B. Drag the InstallForMacOSX root folder to your desktop and rename it to InstallForMacOSX;

C. Overwrite the two files libtbb.dylib and libtbbmalloc.dylib inside Desktop -> InstallForMacOSX -> bin -> maci64 with my two patched revisions attached here;

D. Launch InstallForMacOSX.app inside your extracted desktop InstallForMacOSX folder and install whatever component you want.



2. Fix the actual installed app

A. Go inside your Applications folder -> MATLAB_R2021b.app -> sys -> os -> maci64 and replace the libiomp5.dylib with my patched revision attached here;

B. Again go inside the Application folder -> MATLAB_R2021b.app -> bin -> maci64 and replace libtbb.dylib, libtbbmalloc.dylib, mkl.dylib, blas.spec and lapack.spec too;



Launch MATLAB_R2021b.app and issue the command t=bench to execute a functional test and a benchmark for the most common functions.



Feel free to post your screenshots here for comparison, this is mine after the latest tweaking:

ML.thumb.png.b13ec51c8072e5e62e79fbdcf3f4cc18.png


Notes:

1. The patched libs are the original libs supplied with this particular version of Matlab but patched to only redirect properly the Intel Fastmem / Fastmemcpy routines on AMD Hackintoshes and to mimic a "true" Intel CPU;

2. The new fixes make this function perfectly working, even when A is a 3x3 matrix:

[Q, T] = schur(A);
tomnic you're the best! I randomly stumbled on this thread today after giving up trying to install Matlab last week, having already accepted my destiny of having to use matlab online.
 

abduu

New member
AMD OS X Member
Joined
Dec 26, 2020
Messages
3
Hello,
Thank you for this patch. But when I load a mat file using " load(x.mat) " or by just double-clicking it MATLAB crashes and closes. I tried with the secure boot both enabled and disabled.
I don't know if there is something I did wrong. but I will try this again sometime, maybe I did something wrong.
 

Allubz

New member
AMD OS X Member
Joined
May 4, 2020
Messages
11
@tomnic

Hmm, anyone got a clue what may be going on with my scores? This is 64x 4.0GHz @ 1.2V, 128GB DDR4-3600C16, tested stable and scores ~62-65K CB23 (macOS vs. Windows).

It seems FFT and 2-D aren't scoring great.

I didn't see proper power draw either in the AMD app (excpecting ~330W instead of ~150W).
 

Attachments

  • Screenshot 2021-11-23 at 18.56.17.png
    Screenshot 2021-11-23 at 18.56.17.png
    834.3 KB · Views: 9
Last edited:
Top Bottom
  AdBlock Detected
Sure, ad-blocking software does a great job at blocking ads, but it also blocks some useful and important features of our website. For the best possible site experience please take a moment to disable your AdBlocker.