hibtc / cpymad

Cython binding to MAD-X
http://hibtc.github.io/cpymad/
Other
27 stars 18 forks source link

Error building cpymad on M1 Apple Silicon - "-march=core2" not supported #120

Closed GuillaumeRD closed 1 year ago

GuillaumeRD commented 1 year ago

I followed the new installation procedure for Apple Silicon (M1 in this case) and successfully built MAD-X, however when running:

python setup.py build_ext -lblas -llapack

I get the following error message:

running build_ext building 'cpymad.libmadx' extension clang-14 -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /Users/[$username]/miniforge3/envs/xsuite-arm/include -arch arm64 -fPIC -O2 -isystem /Users/[$username]/miniforge3/envs/xsuite-arm/include -arch arm64 -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -isystem /Users/[$username]/miniforge3/include -D_FORTIFY_SOURCE=2 -isystem /Users/[$username]/miniforge3/include -I/[$MADXDIR]/include -I/Users/[$username]/miniforge3/envs/xsuite-arm/include/python3.10 -c src/cpymad/libmadx.c -o build/temp.macosx-11.0-arm64-cpython-310/src/cpymad/libmadx.o -std=gnu99 clang-14: error: the clang compiler does not support '-march=core2' error: command '/Users/[$username]/miniforge3/envs/xsuite-arm/bin/clang-14' failed with exit code 1

Some context:

coldfix commented 1 year ago

Hey,

I followed the new installation procedure for Apple Silicon

you mean the one outlined in #114?

I'm not good with Apple, but regarding the last part of the question:

Any chance I can edit those out somewhere and replace them both with -mcpu=apple-m1 as I was able to do when building MAD-X?

You can try simply invoking the compiler manually, just copy the whole line and replace what you think needs to be changed. If you succeed in building the .so file and place in the correct location under build/ as given by the command line above, setup.py will automatically pick up the file without trying to rebuild (if it's newer than the .c file and so on). That's also the procedure we're currently using for makig the windows wheels.

I'm currently also working on getting M1 wheels via cross-compilation as recommended in #113.

GuillaumeRD commented 1 year ago

I actually followed the instructions listed here:

https://xsuite.readthedocs.io/en/latest/installation.html#install-on-apple-silicon

along with what can be found here:

https://github.com/hibtc/cpymad/blob/master/doc/installation/macos.rst

since the mention of running pip install -U cython wheel setuptools delocate could not be found at the first link and made one of the scripts look for libmadx.c - which is not available through git clone, only libmadx.pyx can be found.

I tried your suggestion above and ran the compiler manually: I got 6 warning messages related to CYTHON_FALLTHROUGH and it generated a libmadx.o file, not libmadx.so. I moved it into the temporary build/ directory regardless, but upon running setup.py it still gives me the -march error message as it seems to not see the file I just built...

coldfix commented 1 year ago

I'm not sure I can help here, however I have succeeded in cross-compiling for M1 now. You may try the M1 wheels that will be arriving shortly with cpymad 1.12.0.

The main thing that needed to be changed with respect to the x86_64 build was to change the relevant argument to

- -DCMAKE_OSX_ARCHITECTURES=x86_64
+ -DCMAKE_OSX_ARCHITECTURES=arm64

in the cmake command. Other than that, make to install a suitable gfortran compiler, set FC accordingly. In the GitHub Actions cross-compilation environment I also had to setup a bit of environment, but some of these settings may not be necessary for a native build on a well-configured platform:

sudo xcode-select -switch /Applications/Xcode_12.5.1.app
export SDKROOT="$(xcrun --show-sdk-path)"

export ARCHFLAGS="-arch arm64"
export _PYTHON_HOST_PLATFORM="macosx-11.0-arm64"
export MACOSX_DEPLOYMENT_TARGET="11.0"

LIBDIR="path to folder that contains libgfortran.so"
export LDFLAGS="-L$LIBDIR -Wl,-rpath,$LIBDIR"

Some care needs to be taken to use compatible versions of deployment target, xcode version and gfortran.

See also the updated github action starting here: https://github.com/hibtc/cpymad/blob/c8156cbfa4cdb7bda4544e853bb406a5f6569a40/.github/workflows/build.yml#L185-L223

I tried your suggestion above and ran the compiler manually: I got 6 warning messages related to CYTHON_FALLTHROUGH and it generated a libmadx.o file, not libmadx.so. I moved it into the temporary build/ directory regardless, but upon running setup.py it still gives me the -march error message as it seems to not see the file I just built...

If you take the compiler line from above and just modify the parts that need changing, it should place the .o file in the correct location (see -o argument) if it succeeds. An additional link command is required afterwards. Have you tried simply setting architecture to arm64 instead?

Anyway, this route is not really recommended if it can be avoided in any way. Otherwise, see how we do it for the windows build: https://github.com/hibtc/cpymad/blob/c8156cbfa4cdb7bda4544e853bb406a5f6569a40/.github/build/windows/cpymad.sh#L64-L86

GuillaumeRD commented 1 year ago

Thanks for the detailed reply; turns out that it had to do with the environment the conda install had set up: when checking on env, I noticed that I had to manually update the following variables

CFLAGS=-march=core2 -mtune=haswell [...] CONDA_BACKUP_DEBUG_CFLAGS=-march=core2 -mtune=haswell [...] DEBUG_CFLAGS=-march=core2 -mtune=haswell [...] CONDA_BACKUP_CFLAGS=-march=core2 -mtune=haswell [...]

by replacing -march=core2 -mtune=haswell with -mcpu=apple-m1. I re-did the full cpymad install from scratch following your updated commands, and as soon as I changed the 4 CFLAGS above the installation proceeded flawlessly. Running the test via python -m pytest test returned 67 passed, 2 xpassed, 4 warnings.

coldfix commented 1 year ago

Ok, so I assume this is resolved (except maybe improved installation instructions)?