Skip to content

py-numpy: add headers attribute#32835

Closed
adamjstewart wants to merge 1 commit intospack:developfrom
adamjstewart:packages/py-numpy
Closed

py-numpy: add headers attribute#32835
adamjstewart wants to merge 1 commit intospack:developfrom
adamjstewart:packages/py-numpy

Conversation

@adamjstewart
Copy link
Copy Markdown
Member

Copy-n-pasted this from the PythonPackage base class. For some reason, it doesn't work if it's in the base class, see #28527 (comment)

Copy link
Copy Markdown
Contributor

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

numpy doesn't have any Python dependencies, so there should be no need to look for any headers in platlib. We should only need Python.h I think. Does this actually fix something that is not working right now? The linked comment seems to be about py-torch. I thought the other problem was in locating the numpy headers themselves. In that case, shouldn't this patch like in the package that needs those numpy headers?

@adamjstewart
Copy link
Copy Markdown
Member Author

The headers property of a package defines where to look for headers for that package. So with this PR, spec['py-numpy'].headers will tell you where the headers can be found. We could define this in the package that needs to find the headers, but if there are more than one, we would need to copy-n-paste it every time. So the idea is to define it only once and let packages reuse it. It was supposed to work by defining it in the base class, but it isn't working and I don't know why.

Btw, py-petsc4py is the package that has trouble finding these headers without adding them to CPATH @balay

@rgommers
Copy link
Copy Markdown
Contributor

Ah okay, that is helpful context info. In that case, recursively searching platlib seems watertight. The numpy headers should never be landing in <prefix>/include.

@adamjstewart
Copy link
Copy Markdown
Member Author

@spackbot run pipeline

@spackbot-app
Copy link
Copy Markdown

spackbot-app bot commented Sep 27, 2022

I've started that pipeline for you!

@balay
Copy link
Copy Markdown
Contributor

balay commented Sep 28, 2022

Btw, py-petsc4py is the package that has trouble finding these headers without adding them to CPATH @balay

Looks like petsc4py is querying numpy directly to get this info

>>> import numpy
>>> numpy.get_include()
'/usr/lib64/python3.10/site-packages/numpy/core/include'

So assuming petsc4py will be unaffected by this change..

@cosmicexplorer
Copy link
Copy Markdown
Contributor

I'm seeing this pattern used in the py-gpaw, vapor, and precice packages to get a particular numpy/core/include subdirectory of platlib, which in precice's case at least is used to populate a specific -DNumPy_INCLUDE_DIR CMake var:

if "+python" in spec:
python_library = spec["python"].libs[0]
python_include = spec["python"].headers.directories[0]
numpy_include = join_path(
spec["py-numpy"].prefix, spec["python"].package.platlib, "numpy", "core", "include"
)
if xsdk_mode:
cmake_args.append("-DTPL_ENABLE_PYTHON:BOOL=ON")
else:
cmake_args.append("-D%s:BOOL=ON" % python_option)
cmake_args.extend(
[
"-DPYTHON_INCLUDE_DIR=%s" % python_include,
"-DNumPy_INCLUDE_DIR=%s" % numpy_include,
"-DPYTHON_LIBRARY=%s" % python_library,
]
)

Outside of py-matplotlib and py-torchvision, I wasn't able to see any packages specifically requesting py-numpy's .headers (which I guess makes sense if #28527 was just merged). The reason I mention this is: if multiple packages are trying to get at that precise subdirectory of numpy's includes, would it make sense to add that particular platlib subdirectory as a property of the py-numpy package as well here?

@cosmicexplorer
Copy link
Copy Markdown
Contributor

cosmicexplorer commented Oct 4, 2022

@adamjstewart: I don't fully understand what function that inspect.getmodule(self).include (added in #28527) is expected to invoke, and by inspecting the callstack, inspect.getmodule(self).include seemed to be calling _headers_default_handler() in spec.py (which I still do not understand whatsoever), which is why you saw both the default and the build_systems/python.py method being called in order.

However, I was regardless able to make py-scipy find the headers for py-numpy by taking the exact code you used in this PR, and instead moving it into build_systems/python.py's def headers(self) method. The diff is here adamjstewart/spack@packages/py-numpy...cosmicexplorer:spack:packages/py-numpy, but it's just one commit that does exactly that. Please let me know if this also works for you.

@adamjstewart
Copy link
Copy Markdown
Member Author

would it make sense to add that particular platlib subdirectory as a property of the py-numpy package as well here?

That's the point of using spec['py-numpy'].headers.directories[0], we should replace the hacks in those package with this if we can get it working.

I don't fully understand what function that inspect.getmodule(self).include (added in #28527) is expected to invoke

It's supposed to access spec['python'].package.include, but I guess it's actually named python_include, not include. Let me try that. I'll try your patch as well.

@adamjstewart
Copy link
Copy Markdown
Member Author

Yep, that seems to work, nice find! I'll open a separate PR for this.

@adamjstewart adamjstewart deleted the packages/py-numpy branch October 4, 2022 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants