Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiling mex on M1 Mac #130

Open
alecjacobson opened this issue Feb 25, 2022 · 13 comments
Open

Compiling mex on M1 Mac #130

alecjacobson opened this issue Feb 25, 2022 · 13 comments

Comments

@alecjacobson
Copy link
Owner

alecjacobson commented Feb 25, 2022

Matlab currently runs on M1 Macs as using Rosetta https://www.mathworks.com/matlabcentral/answers/641925-is-matlab-supported-on-apple-silicon-macs

Unfortunately, this means that building the mex files as a native arm64 binaries will not work. :-(

Fortunately, it's not too hard to build the mex functions using rosetta. This requires a (still in PR) version of libigl which builds gmp and mpfr from source (rather than relying a system install from, say, homebrew which would just include arm64 binaries).

The upshot is that as of 36df062 you can build mex on m1 macs using rosetta with:

cd mex/
mkdir build
cd build
cmake ../ -DLIBIGL_RESTRICTED_MATLAB=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_OSX_ARCHITECTURES=x86_64 -DMatlab_MEX_EXTENSION="mexmaci64"
arch -x86_64 make -j8

This hasn't yet been documented in the mex/README.md

@paul0noah
Copy link

paul0noah commented Jan 5, 2023

Compiling gptoolbox for M1 Mac with Matlab R2022b beta (native apple silicon support):

I tried compiling gptoolbox/mex for Matlab beta (as it is faster than rosetta version). Unfortunately, this is not working out of the box. Here the steps i followed

  1. Apply the following changes to file gptoobox/mex/cmake/findMATLAB.cmake:
if(APPLE)
  set(_matlab_bin_prefix "mac") # i should be for intel
  set(_matlab_bin_suffix_32bits "a") # previously was: set(_matlab_bin_suffix_32bits "i")
  set(_matlab_bin_suffix_64bits "a64")  # previously was: set(_matlab_bin_suffix_64bits "i64")
  1. call cmake as follows:
cmake .. -DMatlab_ROOT_DIR="/path/to/MATLAB_R2022b_Beta.app/" -DMatlab_MEX_EXTENSION="mexmaca64" -DMATLAB_ADDITIONAL_VERSIONS="R2022b=9.13"

@alecjacobson
Copy link
Owner Author

cool. Thanks for trying this. I haven't tried the beta matlab silicon version yet.

what errors are you running into?

@paul0noah
Copy link

paul0noah commented Jan 5, 2023

the main errors basically are that findMATLAB does not support the matlab silicon version, this includes:

  • matlab bin prefix can not be identified (fixed with step 1)
  • matlab mex extension not supported (fixed with -DMatlab_MEX_EXTENSION="mexmaca64")
  • matlab R2022b version not supported (fixed with -DMATLAB_ADDITIONAL_VERSIONS="R2022b=9.13")

additionally i had an issue with the cmake build system:
my clang compiler did not support -march=native (for fix i upgraded clang)

@alecjacobson
Copy link
Owner Author

alecjacobson commented Jan 5, 2023 via email

@paul0noah
Copy link

Yes, sorry that my post did not make this clear enough.

@alecjacobson
Copy link
Owner Author

Great! Want to submit a PR ? Then I can just make sure it's still working elsewhere.

@scarfelab
Copy link

Hi. I am attempting to compile the gptoolbox Mex with Matlab 2023b (Arm Version) on an Apple M3 MacBook Pro. I have added "R2023b=23.2" to the MATLAB_VERSIONS_MAPPING in FindMATLAB.cmake.

If I run

cmake .. -DMatlab_ROOT_DIR="/Applications/MATLAB_R2023b.app/" -DMatlab_MEX_EXTENSION="mexmaca64" -DMATLAB_ADDITIONAL_VERSIONS="R2023b=23.2"

I get

CMake Error at /Applications/CMake.app/Contents/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find Matlab (missing: Matlab_MEX_LIBRARY MEX_COMPILER MX_LIBRARY
ENG_LIBRARY) (found version "23.2")
Call Stack (most recent call first):
/Applications/CMake.app/Contents/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
cmake/FindMatlab.cmake:1535 (find_package_handle_standard_args)
CMakeLists.txt:80 (find_package)

If I manually specify the paths with

cmake .. -DMatlab_ROOT_DIR="/Applications/MATLAB_R2023b.app/" -DMatlab_MEX_EXTENSION="mexmaca64" -DMATLAB_ADDITIONAL_VERSIONS="R2023b=23.2.0" -DMatlab_MEX_LIBRARY="/Applications/MATLAB_R2023b.app/bin/maca64/libmex.dylib" -DMatlab_MEX_COMPILER="/Applications/MATLAB_R2023b.app/bin/maca64/mex" -DMatlab_MX_LIBRARY="/Applications/MATLAB_R2023b.app/bin/maca64/libmx.dylib" -DMatlab_ENG_LIBRARY="/Applications/MATLAB_R2023b.app/bin/maca64/libeng.dylib"

Things seems to work in that I get

-- Found Matlab: /Applications/MATLAB_R2023b.app/extern/include (found version "23.2") found components: MEX_COMPILER MX_LIBRARY ENG_LIBRARY

Boost is fetched etc. But a CMake error occurs

-- Creating target: igl_restricted::matlab (igl_restricted_matlab)
CMake Error at /Applications/CMake.app/Contents/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find Matlab (missing: Matlab_MEX_LIBRARY MAT_LIBRARY) (found
version "23.2")
Call Stack (most recent call first):
/Applications/CMake.app/Contents/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
cmake/FindMatlab.cmake:1535 (find_package_handle_standard_args)
build/_deps/libigl-src/cmake/igl/modules/restricted/matlab.cmake:18 (find_package)
build/_deps/libigl-src/cmake/igl/igl_include.cmake:32 (include)
build/_deps/libigl-src/cmake/igl/igl_include.cmake:40 (_igl_include_full)
build/_deps/libigl-src/cmake/libigl.cmake:32 (igl_include_optional)
build/_deps/libigl-src/CMakeLists.txt:151 (include)

I am a little lost as to how to fix this issue.

@paul0noah
Copy link

paul0noah commented Nov 20, 2023

Hi @scarfelab,

I did some debugging in the the findMATLAB.cmake file shipped with gptoolbox and found that CMAKE_OSX_ARCHITECTURES was unset which caused that the findMATLAB script was searching inside /Applications/MATLAB_R2023b.app/bin/maci64 instead of /Applications/MATLAB_R2023b.app/bin/maca64 folder.

For me this command worked, i.e. adding -DCMAKE_OSX_ARCHITECTURES="arm64" to your cmake call:

cmake .. -DMatlab_ROOT_DIR="/Applications/MATLAB_R2023b.app/" -DMatlab_MEX_EXTENSION="mexmaca64" -DMATLAB_ADDITIONAL_VERSIONS="R2023b=23.2" -DCMAKE_OSX_ARCHITECTURES="arm64"

I hope this helps

Paul

@alecjacobson
Copy link
Owner Author

alecjacobson commented Nov 21, 2023 via email

@scarfelab
Copy link

@paul0noah Many thanks for your help. Really appreciated. I am not very familiar with cmake, hence my difficulties. Following your instructions has worked much better, but I still get an error.

[ 45%] Performing configure step for 'gmp'
configure: WARNING: unrecognized options: --disable-debug, --disable-dependency-tracking
configure: error: cannot run /bin/sh /Users/peterscarfe/Documents/GitHub Local/gptoolbox/mex/build/_deps/gmp/src/gmp/config.sub
make[2]: *** [_deps/gmp/src/gmp-stamp/gmp-configure] Error 1
make[1]: *** [_deps/libigl-build/CMakeFiles/gmp.dir/all] Error 2
make: *** [all] Error 2

I have checked in the "_deps" folder created by cmake and I can see a gmp folder with various files in, suggesting that it has downloaded. However, I notice that unlike most of the others, there is not "-build" "-src" and "-subbuild" variants. See attached screenshot. The same is also true of mpfr.

_deps

I notice that the instructions suggest I might need to install boost, mpfr and gmp. However, boost seems to have been dowloaded ok as has a "-build" "cmake-build" "-src" and "-subbuild" pattern similar to the other downloads.

Many thanks again for your help. It is really appreciated.

Peter

@paul0noah
Copy link

Hi Peter,

the folder strucutre looks similar on my machine and I also encounter compile errors with gmp stuff and I think @alecjacobson himself has to have a look at this.

Workaround to get parts of mex files built: Since i never needed these parts of gptoolbox my "workaround" was to compile with -i option, i.e. call make -i which has the effect to ignore errors and continue. Since gptoolbox consists of a lot of targets (e.g. decimate_libgl, exact_geodesic, etc. etc.) all of these targets will build successfully which covers most of gptoolboxes features.

Best
Paul

@scarfelab
Copy link

@paul0noah Awesome. Thanks again Paul. This has indeed worked for me i.e. goes through everything building when it can. I really will have to get more familiar with cmake. Glad I was not doing anything wrong r.e. the issue I had with gmp mpfr.

Visually it looks like a fair number of the Mex files cannot be built. However, for me at the moment the key things were related to Embree, which seems to have been built ok. Though I still need to have a more extensive play.

@alecjacobson happy to test any modified version if it would be of any help.

Thank-you both again for your help. Really appreciated it.

@scarfelab
Copy link

Hi both @alecjacobson @paul0noah

It turned out some of the crucial mex files I needed were not built. Sigh... However, I have made a little more progress, all be it of dubious nature. I installed gmp and mpfr in the default /usr/local/ location. Installation and check went fine. I then re-ran

cmake .. -DMatlab_ROOT_DIR="/Applications/MATLAB_R2023b.app/" -DMatlab_MEX_EXTENSION="mexmaca64" -DMATLAB_ADDITIONAL_VERSIONS="R2023b=23.2" -DCMAKE_OSX_ARCHITECTURES="arm64"

Turns out this does not seem to locate either gmp or mpfr in /usr/local/.

However, the cmake command now progresses itself and gets further in grabbing and installing gmp and mpfr itself. Now many more of the mex files get built with make -i. Including some of the critical ones I need.

Thus, something appears to have changed in terms of cmake being able to find online what it needs in terms of gmp and mpfr. Indeed, I can completely remove my installed versions in /usr/local/ and things work just as well.

So, progress, but no thanks to my own efforts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants