mattjj / pyhsmm-autoregressive

autoregressive plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

clang support with openmp

virajmahesh opened this issue · comments

It seems that installing autoregressive on OS X doesn't pass include_dirs to clang.

Here's my output from running pip install autoregressive:

➜  autoregressive git:(master) ✗ pip install autoregressive
Collecting autoregressive
  Using cached autoregressive-0.1.0.tar.gz
Requirement already satisfied (use --upgrade to upgrade): Cython>=0.20.1 in /usr/local/lib/python2.7/site-packages (from autoregressive)
Requirement already satisfied (use --upgrade to upgrade): numpy in /usr/local/lib/python2.7/site-packages (from autoregressive)
Requirement already satisfied (use --upgrade to upgrade): scipy in /usr/local/lib/python2.7/site-packages (from autoregressive)
Requirement already satisfied (use --upgrade to upgrade): matplotlib in /usr/local/lib/python2.7/site-packages (from autoregressive)
Requirement already satisfied (use --upgrade to upgrade): pybasicbayes in /usr/local/lib/python2.7/site-packages (from autoregressive)
Requirement already satisfied (use --upgrade to upgrade): pyhsmm in /usr/local/lib/python2.7/site-packages (from autoregressive)
Requirement already satisfied (use --upgrade to upgrade): pytz in /usr/local/lib/python2.7/site-packages (from matplotlib->autoregressive)
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/local/lib/python2.7/site-packages (from matplotlib->autoregressive)
Requirement already satisfied (use --upgrade to upgrade): cycler in /usr/local/Cellar/matplotlib/1.5.0/libexec/lib/python2.7/site-packages (from matplotlib->autoregressive)
Requirement already satisfied (use --upgrade to upgrade): pyparsing!=2.0.0,!=2.0.4,>=1.5.6 in /usr/local/lib/python2.7/site-packages (from matplotlib->autoregressive)
Requirement already satisfied (use --upgrade to upgrade): future in /usr/local/lib/python2.7/site-packages (from pybasicbayes->autoregressive)
Requirement already satisfied (use --upgrade to upgrade): nose in /usr/local/Cellar/matplotlib/1.5.0/libexec/lib/python2.7/site-packages (from pybasicbayes->autoregressive)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /usr/local/Cellar/matplotlib/1.5.0/libexec/lib/python2.7/site-packages (from python-dateutil->matplotlib->autoregressive)
Building wheels for collected packages: autoregressive
  Running setup.py bdist_wheel for autoregressive ... error
  Complete output from command /usr/local/opt/python/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/x9/fz1dz2n573x2pyfbgy358dph0000gn/T/pip-build-bV2uEr/autoregressive/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d /var/folders/x9/fz1dz2n573x2pyfbgy358dph0000gn/T/tmp8vEcKCpip-wheel- --python-tag cp27:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.11-x86_64-2.7
  creating build/lib.macosx-10.11-x86_64-2.7/autoregressive
  copying autoregressive/__init__.py -> build/lib.macosx-10.11-x86_64-2.7/autoregressive
  copying autoregressive/distributions.py -> build/lib.macosx-10.11-x86_64-2.7/autoregressive
  copying autoregressive/models.py -> build/lib.macosx-10.11-x86_64-2.7/autoregressive
  copying autoregressive/util.py -> build/lib.macosx-10.11-x86_64-2.7/autoregressive
  running build_ext
  building 'autoregressive.messages' extension
  creating build/temp.macosx-10.11-x86_64-2.7
  creating build/temp.macosx-10.11-x86_64-2.7/autoregressive
  clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c autoregressive/messages.cpp -o build/temp.macosx-10.11-x86_64-2.7/autoregressive/messages.o -O2 -fopenmp -std=c++11 -DEIGEN_NO_MALLOC -DNDEBUG -w
  autoregressive/messages.cpp:346:10: fatal error: 'numpy/arrayobject.h' file not found
  #include "numpy/arrayobject.h"
           ^
  1 error generated.
  error: command 'clang' failed with exit status 1

  ----------------------------------------
  Failed building wheel for autoregressive
  Running setup.py clean for autoregressive
Failed to build autoregressive
Installing collected packages: autoregressive
  Running setup.py install for autoregressive ... error
    Complete output from command /usr/local/opt/python/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/x9/fz1dz2n573x2pyfbgy358dph0000gn/T/pip-build-bV2uEr/autoregressive/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/x9/fz1dz2n573x2pyfbgy358dph0000gn/T/pip-5d8KWZ-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.11-x86_64-2.7
    creating build/lib.macosx-10.11-x86_64-2.7/autoregressive
    copying autoregressive/__init__.py -> build/lib.macosx-10.11-x86_64-2.7/autoregressive
    copying autoregressive/distributions.py -> build/lib.macosx-10.11-x86_64-2.7/autoregressive
    copying autoregressive/models.py -> build/lib.macosx-10.11-x86_64-2.7/autoregressive
    copying autoregressive/util.py -> build/lib.macosx-10.11-x86_64-2.7/autoregressive
    running build_ext
    building 'autoregressive.messages' extension
    creating build/temp.macosx-10.11-x86_64-2.7
    creating build/temp.macosx-10.11-x86_64-2.7/autoregressive
    clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c autoregressive/messages.cpp -o build/temp.macosx-10.11-x86_64-2.7/autoregressive/messages.o -O2 -fopenmp -std=c++11 -DEIGEN_NO_MALLOC -DNDEBUG -w
    autoregressive/messages.cpp:346:10: fatal error: 'numpy/arrayobject.h' file not found
    #include "numpy/arrayobject.h"
             ^
    1 error generated.
    error: command 'clang' failed with exit status 1

    ----------------------------------------
Command "/usr/local/opt/python/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/x9/fz1dz2n573x2pyfbgy358dph0000gn/T/pip-build-bV2uEr/autoregressive/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/x9/fz1dz2n573x2pyfbgy358dph0000gn/T/pip-5d8KWZ-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/x9/fz1dz2n573x2pyfbgy358dph0000gn/T/pip-build-bV2uEr/autoregressive

How do I fix this? I tried cloning the repo and manually running python setup.py install but it doesn't seem to work.

I also tried searching for using include_dirs with setup but there doesn't seem to be much advice about configuration

The compiled code is actually completely optional, so if you just want to try this library out, I recommend skipping the compilation. Things will still be quite fast; the compiled code in this library is mostly just for OpenMP parallelization. One way to skip the compilation is not to use setup.py and just to symlink the library onto your PYTHONPATH. Another way might be to try the branch I just pushed, which should add a --no-compile option to setup.py. A third way would be to edit the setup.py yourself to pass no ext_modules argument to setup.

But are you able to build pyhsmm and pybasicbayes (without any warnings printing out)? Even though those compilations are also optional, you probably do want pyhsmm to build for a big performance boost, and I'm guessing that this error would pop up there as well (though the setup.py will just print a warning if it fails to build low-level code).

The current install works on my OS X machine and my coworkers' machines, so this may be a problem with your current environment. In particular, are you able to build any cython code that depends on numpy?

To see if you can build any cython numpy code, try this test gist, following the commands in the comment at the bottom. That code has nothing to do with pyhsmm, but it will test if your environment is able to build cython code with numpy in it.

Hi Matt,

Thanks for your response. I was ultimately able to build the library, although it did take a few tweaks.
I can share the build process here, if you think it might benefit others trying to build the library.

Yes please! I'd like to make the build work on as many environments as possible.

Here's the changes I made to get this to compile on OS X with clang:

  • Modify your setup file so that setuptools can locate dependencies while building extensions. This should help with the missing numpy header files. This is my setup.py
  • Then run python setup.py install
  • Expect an error message along the lines of:
    autoregressive/messages.h:3:10: fatal error: 'omp.h' file not found
  • This is because, if you're using clang the omp.h headerfile should be included using #include <libiomp/omp.h>. Here is my new messages.h
  • Now rerun python setup.py install. The installion process should now succeed.

Additionally, if you can't figure out how to install the clang version of OpenMP and you are using brew, you can run brew install clang-omp to install it.

Hope this helps!

Thanks!

OpenMP is why I only "officially" support GCC builds, but it looks like I don't mention that (or much of anything) in this project's README. However, it's great to know that using OpenMP with clang is getting easier. (GCC also tends to generate better numerical code, so it might still be preferable, at least for now.)

I'm confused as to why you needed to pass include_dirs to Extension instead of just to setup. I pass it as a setup arg in this and several other projects and I've never had a problem building (including on OS X). Changing that argument to setup definitely changes the -I arguments on the compiler command line. Do you have any idea why that was necessary for you? (I'm typing this on a mac right now and I haven't been able to reproduce the problem.)

The Clang OpenMP docs I've been able to find suggest that #include <omp.h> with -fopenmp=libomp is preferred to using Intel's libiomp (libiomp was considered in 3.5 but replaced in 3.7). In fact, a blog post suggests just -fopenmp would be fine, but the 3.7.0 release notes say -fopenmp=libomp is necessary. Using Intel's runtime via the gcc compatibility layer, as I think you did with #include <libiomp/omp.h>, probably works but may not be future-proof.

I think the best way forward to support Clang builds is to detect it in setup.py and pass -fopenmp=libomp (assuming it's 3.7.0 or later). I'll leave this issue open as a reminder to look into that.

Thanks again for your work on this, @virajmahesh. Also, I just noticed you're at Cal; I was EECS '08 there! Go Bears!

You're welcome @mattjj. And yes; Go Bears!