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

ENH: Move cython build to c++ #648

Merged
merged 35 commits into from Feb 23, 2017
Merged

ENH: Move cython build to c++ #648

merged 35 commits into from Feb 23, 2017

Conversation

nonhermitian
Copy link
Member

I have managed to get everything moved over to c++. Should address #647. There are still a few issues to address, but the tests pass on unix based systems.

  • CFLAGS need to be set for visual studio builds.
  • zspmv src files need to be platform dependent (i.e. win/unix versions). This is mainly due to a few minor variations in how compiler hinting is done. No big deal. Also, when moving to openmp, visual studio uses v2.0, which is a bit dated.

@nonhermitian nonhermitian self-assigned this Feb 15, 2017
Copy link
Member

@ajgpitch ajgpitch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks great to me. I can hardly believe you managed to get this all implemented and working so quickly

pyximport.install(setup_args={'include_dirs': [numpy.get_include()]})
del pyximport
import qutip.cy.pyxbuilder as pbldr
os.environ['CFLAGS'] = '-O1 -w -ffast-math'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question: why do we have a reduced set of flags here compared to _compiler_flags in cy/setup.py?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we are just gluing things together here. Setting optimizations to high here increases runtime build time for no performance gains.

@@ -35,7 +35,7 @@ cimport numpy as np
cimport cython
cimport libc.math

cdef extern from "src/zspmv.h" nogil:
cdef extern from "src/zspmv.hpp" nogil:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we do a conditional import here based on compiler / platform

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Maybe src/win_zspmv or something on win platforms.

@@ -11,23 +11,24 @@
exts = ['spmatfuncs', 'stochastic', 'sparse_utils', 'graph_utils', 'interpolate',
'spmath', 'heom', 'math', 'spconvert', 'ptrace', 'testing']

_compiler_flags = ['-w', '-ffast-math', '-O3', '-march=native', '-funroll-loops']
_compiler_flags = ['-w', '-std=c++11', '-ffast-math', '-O3', '-march=native', '-funroll-loops']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we add platform / compiler specific flags in here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the format for visual studio compiler args is different.

@ajgpitch
Copy link
Member

There may be some who would wish continue using the mingw compiler on Windows, as it is a smaller download. I guess we could just say that we only support MS compilers on Windows.
Would the zspmv.cpp need to be platform or compiler specific?
Is it possible to choose flags / source based on which compiler is being used? I have seen in the cython generated C that there are compiler specific conditionals in the directives.

@ajgpitch
Copy link
Member

I have built this branch and tested on my linux machine here. All tests pass.
I am assuming that you need to make another commit before it's ready for me to test on Windows?

@ajgpitch
Copy link
Member

Meant to add: I get this warning in build_ext and in all str format td tests
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++

@nonhermitian
Copy link
Member Author

Everything should work using mingw. The dwfault Anaconda mingw is 4.7.1, that supports the c++11 features we need. Mingwpy is 4.9 and even supports openmp. So eveything should be fine. But I have not checked.

The command line warning you see is something that Cython is emitting, not QuTiP. Perhaps this is a bug on their end? I dont know how to remove the flags that Cython specifies. The dash -w should block all warnings though.

@nonhermitian
Copy link
Member Author

And yes, will figure out the win stuff today at work.

@nonhermitian
Copy link
Member Author

There is an issue with numpy.distutils on win. To get this done anytime soon, I think we need to move to setuptools.

@ajgpitch
Copy link
Member

Do you mean the issue with not being able to find vcvarsall.bat? mentioned in:
https://github.com/cython/cython/wiki/CythonExtensionsOnWindows

In my test project I used the workaround they suggest:

try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension

And this seems to work. Is it more complicated than that for us?

@nonhermitian
Copy link
Member Author

Nope, it is something else. I have a workaround similar to what you did. Everything seems to ve working as the tests run fine. Awesome stuff.

@nonhermitian
Copy link
Member Author

@ajgpitch, please try this latest update on Win. I had everything up and running at work today. The only thing I didn't have was a spmv source file that compiled under GCC, CLANG, and VS. I think the file I have up now should work, but as I don't have Win at home, I need a verification.

The only thing missing from the pull is the optional fortran extension. I will add that on Monday.

@ajgpitch
Copy link
Member

Attempt to install qutip in python2.7

running build_ext
building 'qutip.cy.spmatfuncs' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
creating build\temp.win-amd64-2.7\Release\qutip
creating build\temp.win-amd64-2.7\Release\qutip\cy
creating build\temp.win-amd64-2.7\Release\qutip\cy\src
C:\Users\alex\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\Anaconda2\envs\qtpy2\lib\site-packages\numpy\core\include -IC:\Anaconda2\envs\qtpy2\lib\site-packages\numpy\core\include -IC:\Anaconda2\envs\qtpy2\include -IC:\Anaconda2\envs\qtpy2\PC /Tpqutip/cy/spmatfuncs.cpp /Fobuild\temp.win-amd64-2.7\Release\qutip/cy/spmatfuncs.obj -w -std=c++11 -O3 -march=native -funroll-loops
cl : Command line warning D9025 : overriding '/W3' with '/w'
cl : Command line warning D9002 : ignoring unknown option '-std=c++11'
cl : Command line warning D9002 : ignoring unknown option '-O3'
cl : Command line warning D9002 : ignoring unknown option '-march=native'
cl : Command line warning D9002 : ignoring unknown option '-funroll-loops'
spmatfuncs.cpp
c:\anaconda2\envs\qtpy2\lib\site-packages\numpy\core\include\numpy\npy_1_7_deprecated_api.h(12) : Warning Msg: Using deprecated NumPy API, disable it by #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
C:\Users\alex\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Include\xlocale(342) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
c:\users\alex\documents\github\qutip\qutip\cy\src/zspmv.hpp(3) : error C2146: syntax error : missing ',' before identifier 'data'
qutip/cy/spmatfuncs.cpp(2503) : error C2660: 'zspmvpy' : function does not take 7 arguments
qutip/cy/spmatfuncs.cpp(2688) : error C2660: 'zspmvpy' : function does not take 7 arguments
qutip/cy/spmatfuncs.cpp(2923) : error C2660: 'zspmvpy' : function does not take 7 arguments
qutip/cy/spmatfuncs.cpp(4801) : error C2668: 'sqrt' : ambiguous call to overloaded function
        C:\Users\alex\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Include\math.h(581): could be 'long double sqrt(long double)'
        C:\Users\alex\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Include\math.h(533): or       'float sqrt(float)'
        C:\Users\alex\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Include\math.h(128): or       'double sqrt(double)'
        while trying to match the argument list '(int)'
error: command 'C:\\Users\\alex\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\cl.exe' failed with exit status 2

@ajgpitch
Copy link
Member

Attempt in Python3.5

running build_ext
building 'qutip.cy.spmatfuncs' extension
creating build\temp.win-amd64-3.5
creating build\temp.win-amd64-3.5\Release
creating build\temp.win-amd64-3.5\Release\qutip
creating build\temp.win-amd64-3.5\Release\qutip\cy
creating build\temp.win-amd64-3.5\Release\qutip\cy\src
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Anaconda2\envs\qtpy3\lib\site-packages\numpy\core\include -IC:\Anaconda2\envs\qtpy3\lib\site-packages\numpy\core\include -IC:\Anaconda2\envs\qtpy3\include -IC:\Anaconda2\envs\qtpy3\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\winrt" "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\winrt" /EHsc /Tpqutip/cy/spmatfuncs.cpp /Fobuild\temp.win-amd64-3.5\Release\qutip/cy/spmatfuncs.obj /w /Ox
cl : Command line warning D9025 : overriding '/W3' with '/w'
spmatfuncs.cpp
c:\anaconda2\envs\qtpy3\lib\site-packages\numpy\core\include\numpy\npy_1_7_deprecated_api.h(12) : Warning Msg: Using deprecated NumPy API, disable it by #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
c:\users\alex\documents\github\qutip\qutip\cy\src/zspmv.hpp(3): error C2146: syntax error: missing ')' before identifier 'data'
c:\users\alex\documents\github\qutip\qutip\cy\src/zspmv.hpp(3): error C3646: 'data': unknown override specifier
c:\users\alex\documents\github\qutip\qutip\cy\src/zspmv.hpp(3): error C2062: type 'int' unexpected
c:\users\alex\documents\github\qutip\qutip\cy\src/zspmv.hpp(7): error C2059: syntax error: ')'
qutip/cy/spmatfuncs.cpp(2503): error C2660: 'zspmvpy': function does not take 7 arguments
qutip/cy/spmatfuncs.cpp(2688): error C2660: 'zspmvpy': function does not take 7 arguments
qutip/cy/spmatfuncs.cpp(2923): error C2660: 'zspmvpy': function does not take 7 arguments
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2

@nonhermitian
Copy link
Member Author

Py2.7 on Windows needs to always use the GCC. The VS version corresponding to Py27 does not support the functions that we need. The Py3.5 errors mean that my #define did not work. I have put up a different version, and will check it tomorrow at work.

@ajgpitch
Copy link
Member

Okay, I am much happier with the idea that our recommended Python version for qutip on MS Wondows is >=3.5 than the other way round. I would be quite happy to consider dropping support for Python 2 altogether at some point.

@nonhermitian
Copy link
Member Author

Ok, we should be set now. Tested working on GCC, CLANG, mingwpy, and VS 2015.

@ajgpitch
Copy link
Member

I have just finished testing with Vs 2015 as well (win10, Python 3.5). All passed.
I feel this is a momentous occasion. Great work!
I think this is worthy of a 4.1 release. Very keen to get working conda package for Windows out there

@ajgpitch ajgpitch mentioned this pull request Feb 22, 2017
@nonhermitian nonhermitian changed the title ENH(WIP): Move cython build to c++ ENH: Move cython build to c++ Feb 23, 2017
@nonhermitian
Copy link
Member Author

Merging this now.

@nonhermitian nonhermitian merged commit 82e2095 into qutip:master Feb 23, 2017
@nonhermitian nonhermitian mentioned this pull request Feb 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants