Skip to content

DOC: Add concrete Meson build example for NumPy C ufunc extension#30008

Merged
rgommers merged 2 commits intonumpy:mainfrom
Aniketsy:doc/meson-ufunc-example
Nov 10, 2025
Merged

DOC: Add concrete Meson build example for NumPy C ufunc extension#30008
rgommers merged 2 commits intonumpy:mainfrom
Aniketsy:doc/meson-ufunc-example

Conversation

@Aniketsy
Copy link
Copy Markdown
Contributor

#28614
This PR adds a new documentation page, ufunc_meson_example.rst

Please let me know if my approach or fix needs any improvements . I’m open to feedback and happy to make changes based on suggestions.
Thankyou !

@Aniketsy
Copy link
Copy Markdown
Contributor Author

@mhvk please review these changes when you get chance, and let me know if this needs improvement. I have created a new file and don't know if this is best approach .

Copy link
Copy Markdown
Member

@mattip mattip left a comment

Choose a reason for hiding this comment

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

I think the added examples are missing. The other code examples are all in f2py/code, maybe put them under a ufunc subdirectory?

@Aniketsy
Copy link
Copy Markdown
Contributor Author

@mattip I've updated with changes.

Copy link
Copy Markdown
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

@Aniketsy - this looks fine but it is in the wrong place! I think this should be part of https://numpy.org/doc/stable/user/c-info.ufunc-tutorial.html -- and ideally where it introduces how to compile it using setuptools, you give an alternative with meson. A big advantage is that then you can just adjust the sample for the ufunc c code that is already introduced there.

What would be brilliant is to use the same kind of switchable display as on https://numpy.org/doc/stable/building/index.html#building-from-source, where one has blocks different OS, and for different ways to create virtual environments, etc. Here, one could have one for setuptools and one for meson. Note, however, that I have not looked at the source code to see if it would be very tricky!

@Aniketsy
Copy link
Copy Markdown
Contributor Author

@mhvk i have updated, the changes as per suggestion, please let me know if it needs improvement.

@Aniketsy Aniketsy marked this pull request as draft October 21, 2025 14:00
@Aniketsy Aniketsy marked this pull request as ready for review October 21, 2025 14:47
@ngoldbaum
Copy link
Copy Markdown
Member

Awesome:

image

I didn't manually test that the meson example works but it looks correct at a quick glance.

My only suggestion is, IMO, we should be suggesting meson by default in the tab picker.

@ngoldbaum
Copy link
Copy Markdown
Member

Also probably worth suggesting writing a pyproject.toml and picking meson-python as the build backend to make your code pip-installable.

@ngoldbaum
Copy link
Copy Markdown
Member

The numpy-user-dtypes repo has a number of meson/meson-python examples that use the numpy C API you can take inspiration from: https://github.com/numpy/numpy-user-dtypes

@Aniketsy
Copy link
Copy Markdown
Contributor Author

@ngoldbaum Thanks! I've updated as per suggestions, please let me know if this need improvement.

Copy link
Copy Markdown
Member

@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.

Thanks for working on this @Aniketsy! I added some comments aiming to make the examples as idiomatic as possible.

@Aniketsy
Copy link
Copy Markdown
Contributor Author

@rgommers Thanks! I've updated with changes as per suggestion, please let me know if this need improvement.

Copy link
Copy Markdown
Member

@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.

This is getting closer. If you look at the rendered page (see here for the latest version), the page doesn't flow very well though; it contains duplicate setup.py entries now above the .. note:: blocks. Those can be deleted. The extra docstring content is mostly obsolete, except for the numpy-specific paragraphs like this one:

    setup.py file for single_type_logit.c
    Note that since this is a numpy extension
    we add an include_dirs=[get_include()] so that the
    extension is built with numpy's C/C++ header files.

Can you move those parts into the setup.py example in the tabs, and delete the rest (e.g., everything related to running python setup.py xxx is obsolete)?

There is only last issue with libraries=["npymath"]. I think we need to leave that as is but open a follow-up issue to change the example and remove those docs. We should stop encouraging anyone to use libnpymath, since we want to get rid of that. To do so, the example has to be changed to not use half-precision dtypes (it's a known issue that float16 et al routines aren't available through the regular C API).


For modern Python packaging, it is recommended to use a `pyproject.toml` and select
either `meson-python` or `scikit-build-core` as your build backend. Both are well-designed,
well-maintained, and make your extension pip-installable and compatible with current Python packaging standards.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This note doesn't need to be repeated three times, can you just leave the first one and delete the other ones?

@Aniketsy
Copy link
Copy Markdown
Contributor Author

@rgommers I've update as per suggestion, please let me know if this needs further improvements. Thanks!

@rgommers
Copy link
Copy Markdown
Member

We're getting close! There's a rendering issue, see the double indent here:

image

Copy link
Copy Markdown
Member

@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.

A few more small comments. Once those are resolved and the rendered version looks good, I'll do some local testing.

@Aniketsy
Copy link
Copy Markdown
Contributor Author

@rgommers done with changes, please let me know if this needs further improvements. Thanks!

@Aniketsy
Copy link
Copy Markdown
Contributor Author

There is only last issue with libraries=["npymath"]. I think we need to leave that as is but open a follow-up issue to change the example and remove those docs. We should stop encouraging anyone to use libnpymath, since we want to get rid of that. To do so, the example has to be changed to not use half-precision dtypes (it's a known issue that float16 et al routines aren't available through the regular C API).

@rgommers I’d love to work on this issue if you allow. Please let me know if I can go ahead with this!

@rgommers
Copy link
Copy Markdown
Member

rgommers commented Nov 3, 2025

After 3 rounds of review there was still a lot of straightforward things to fix, like duplication of content and added examples that invalidated the existing text above it. So I pushed the needed changes for those directly.

The meson.build file for Example NumPy ufunc with multiple dtypes isn't correct right now, since it does lot link libnpymath. I am thinking we need the following change there:

  1. Remove float16 support from the C example, to get the actual happy path multi-dtype support correct
  2. Adjust the setup.py file to match
  3. Then add a new example (or a diff) with float16, that then (a) does link libnpymath and (b) has a warning about that not being future-proof or super robust (since one sometimes needs to match the compilers used by numpy itself at wheel build time, link to ENH: Future of npymath library #20880) but currently NPY_HALF & friends are not exposed in the regular NumPy API so this is your only choice currently.
    • Add to that that float16 != bfloat16 and hence not super useful, please think twice about adding this support because you very likely do not need it.

@mhvk @ngoldbaum thoughts on that approach?

Copy link
Copy Markdown
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Just had a quick look. Overall, looks very nice, just a few small comments on the existing changes.

On the later question of float16, I think for these examples it is also fine to not go on to an example that includes float16, but just skip to describing what that is tricky. In the end, these examples are meant to be for people writing their first ufunc, it does not have to be too elaborate.

project('spam', 'c')

py = import('python').find_installation()
np_dep = dependency('numpy')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This particular example actually doesn't use the numpy dependency, so this is not necessary.

extension_module = py.extension_module(
'spam',
['spammodule.c'],
dependencies: [np_dep],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This can go too.

.. code-block:: python

from setuptools import setup, Extension
import numpy as np
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And this one.

@rgommers
Copy link
Copy Markdown
Member

rgommers commented Nov 5, 2025

On the later question of float16, I think for these examples it is also fine to not go on to an example that includes float16, but just skip to describing what that is tricky. In the end, these examples are meant to be for people writing their first ufunc, it does not have to be too elaborate.

Agreed, that sounds like a good way to go.

@Aniketsy if you're up for giving that a go, removing all float16 related content from that example and ensuring it builds, then adding a sentence at the bottom as @mhvk suggests, please go for it.

@Aniketsy Aniketsy force-pushed the doc/meson-ufunc-example branch from 781cc6e to f859953 Compare November 8, 2025 12:59
@Aniketsy
Copy link
Copy Markdown
Contributor Author

Aniketsy commented Nov 8, 2025

I apologize for the late response, I was in the hospital.
@mhvk , @rgommers thank you for the review. I’ve updated the PR with the suggested changes. Please let me know if this needs further improvements.

@mhvk mhvk force-pushed the doc/meson-ufunc-example branch from f859953 to d39130b Compare November 9, 2025 19:13
@mhvk
Copy link
Copy Markdown
Contributor

mhvk commented Nov 9, 2025

@Aniketsy - I had a look and tried everything. Some things still failed, in part because the pyproject.toml sample, as given, was not quite complete. So, I added a minimal complete one. While doing this, I saw other things to fix (including removing the last part of the file, the point of which completely escaped me), so I ended up thinking it was easier to just follow @rgommers's example and push my fixes directly to your branch. I also took the liberty to squash your commits together. Anyway, this ended up a larger refactoring than any of us thought, but I think the result is good! Thanks for starting it!!

@rgommers - I think this is now ready...

p.s. I am not sure the docs build can be viewed anywhere, but I checked it locally, and it looks good.

@rgommers
Copy link
Copy Markdown
Member

I am not sure the docs build can be viewed anywhere,

The link ci/circleci: build artifact in the list of CI jobs gets you to the preview of the html docs built in CI. You then still need to navigate to the actual page,

@rgommers
Copy link
Copy Markdown
Member

Thanks for another push on this @mhvk!

I was in the hospital.

I hope you are feeling better @Aniketsy!

@rgommers rgommers added this to the 2.4.0 release milestone Nov 10, 2025
Copy link
Copy Markdown
Member

@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.

This is in pretty good shape now, let's declare victory and get this in. Thanks @Aniketsy and @mhvk!

@rgommers rgommers merged commit 933fd54 into numpy:main Nov 10, 2025
4 checks passed
@mhvk
Copy link
Copy Markdown
Contributor

mhvk commented Nov 10, 2025

Great! And the result of a nice three-way collaboration, thanks both!

@Aniketsy
Copy link
Copy Markdown
Contributor Author

I hope you are feeling better @Aniketsy!
@rgommers Thanks! I’m good for now :)

@mhvk Thank you for the guidance and corrections.

cakedev0 pushed a commit to cakedev0/numpy that referenced this pull request Dec 5, 2025
…mpy#30008)

Co-authored-by: Marten Henric van Kerkwijk <mhvk@astro.utoronto.ca>
Co-authored-by: Ralf Gommers <ralf.gommers@gmail.com>
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
…mpy#30008)

Co-authored-by: Marten Henric van Kerkwijk <mhvk@astro.utoronto.ca>
Co-authored-by: Ralf Gommers <ralf.gommers@gmail.com>
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.

5 participants