lesgourg / class_public

Public repository of the Cosmic Linear Anisotropy Solving System (master for the most recent version of the standard code; GW_CLASS to include Cosmic Gravitational Wave Background anisotropies; classnet branch for acceleration with neutral networks; ExoCLASS branch for exotic energy injection; class_matter branch for FFTlog)
220 stars 291 forks source link

Python wrapper version 3.2.0 does not work with Apple M1 chip #527

Open tanveerkarim opened 1 year ago

tanveerkarim commented 1 year ago

I am currently trying to install CLASS and the associated Python wrapper using the details provided in the documentation. My OS is Mac Ventura 13.4. I was able to install the base version of CLASS by first installing the following:

brew install llvm brew install libomp

changing the following in the Makefile:

gcc = ${HOMEBREW_PREFIX}/opt/llvm/bin/clang

However, when I tried to install the Python wrapper by running the setup.py file, I kept getting the error symbol not found in flat namespace (_background_at_tau as seen in #384. I tried the workarounds suggested in that thread and it did not work. I also tried the hack in #462 and that did not work either.

I eventually did pip uninstall classy and then installed the 2.9.4 version from PyPi using pip install classy==2.9.4. That version works properly. I did test the 3.2.0 and 3.2.0.1 from PyPi and those do not work either. So something in the newer versions of the wrapper is not working whereas the 2.9.4 version seems to work fine. I think it would be useful to investigate this issue and introduce those fixes for users with Apple M1 MacBooks.

ntessore commented 1 year ago

The 2.9.4 release on PyPI was made with the changes in #371, which were necessary to get a properly distributable package. But these were never merged into the repository, and are out of date now.

tanveerkarim commented 1 year ago

The 2.9.4 release on PyPI was made with the changes in #371, which were necessary to get a properly distributable package. But these were never merged into the repository, and are out of date now.

I feel like these changes should be reintroduced in the current and future packages. Compared to CAMB, I have had a lot of difficulty installing the Python version of CLASS. I think something more Pythonic and intuitive like PyPi or Conda installation could alleviate a lot of confusions.

brinckmann commented 5 months ago

Since mac installation difficulties seem to come up every 6 months or so with different changes necessary and I just went through the process of getting class 3.2.2 and classy to work on Sonoma 14.3 on M2 here's what I did on a clean install using clang and OpenMP:

1) installed xcode, upgraded pip, installed brew

2) used pip (instead of anaconda) for python packages python3 -m pip install 'setuptools' 'wheel' 'numpy>=1.19.0' 'Cython>=0.29.21,<3' 'scipy' 'matplotlib' Note the old Cython version is due to #531 (thanks!!), which I guess won't be needed much longer, alternatively you can apply the manual fix instead and use a newer Cython.

3) cloned class from the github and followed instructions on issue #405 by defjaf (thanks!!) on changing the Makefile and installing libomp (I did not install llvm)

4) when installing libomp with brew it asked me to add the following lines to the .zshrc

export LDFLAGS="-L/opt/homebrew/opt/libomp/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libomp/include"

I also added export CFLAGS="-I/opt/homebrew/opt/libomp/include" I'm not sure why, but class didn't seem to pick up on this, so I added it to the Makefile, which did the trick

LDFLAG += "-L/opt/homebrew/opt/libomp/lib"
CCFLAG += "-I/opt/homebrew/opt/libomp/include"

In addition to what was suggested on issue #405 by defjaf, i.e.,

CC       = clang
...
OMPFLAG   = -Xclang -fopenmp
...
LDFLAG += -lomp

plus remembering to change PYTHON ?= python3 Now class works.

5) followed instructions on issue #405 by echaussidon (thanks!!) on changing python/setup.py : extra_link_args=['-lgomp'] to extra_link_args=['-lomp'] Note the export variables added for libomp to .zshrc were necessary for the python wrapper to find the libraries (I had commented them since class wasn't finding them anyway). Now classy also works.

Best, Thejs

mloverde commented 4 months ago

Thanks! These detailed instructions are super helpful. I have an older mac so I had to change the flags slightly (given below if it helps anyone) LDFLAG +="-L/usr/local/opt/libomp/lib" CPPFLAG +="-I/usr/local/opt/libomp/include"

But I am still having issues and unable to get the newest version of CLASS 3.2.2 to run on my Mac (regardless of the openmp flags being commented).

I think it's a Mac issue but I have errors about finding stdio.h and atomic. ../include/common.h:3:19: fatal error: stdio.h: No such file or directory

include "stdio.h"

./include/parallel.h:46:10: fatal error: 'atomic' file not found

include

I have had this problem in the past and found solutions changing the compiler to use an updated version gcc instead of clang, but this is not working for me now (I have gcc-13). For stdio.h I think the issue is with c++ , whereas the issue seems to be with clang. Has anyone had these errors and found a fix?