Skip to content

BLD Add Meson OpenMP checks #29762

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

Merged
merged 18 commits into from
Sep 5, 2024
Merged

Conversation

lesteve
Copy link
Member

@lesteve lesteve commented Sep 1, 2024

This avoids to mess up Meson OpenMP to try to alleviate @adrinjalali's concern and avoid missing OpenMP dependencies as in #29694.

Summary:

  1. a git grep command is used to figure out which Cython files are using OpenMP
  2. meson introspect produces a json that shows the ninja.build information in particular the compiler flags, so we can figure out which Cython module use OpenMP flags.

The script makes sure that 1. and 2. match.

The script also found some cases where openmp_dep was added to an extension module dependency in meson.build and the Cython file was not using any OpenMP features (based on git grep regex) so this PR fixes this.

Copy link

github-actions bot commented Sep 1, 2024

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: de18c4d. Link to the linter CI: here

@lesteve lesteve changed the title Add Meson OpenMP checks BLD Add Meson OpenMP checks Sep 2, 2024
@lesteve
Copy link
Member Author

lesteve commented Sep 2, 2024

@adrinjalali do you want to have a quick look and see whether that seems to go in the right direction to make sure we correctly define OpenMP dependencies. I need to clean up a few things but I think this is pretty much working right now.



def get_canonical_name_meson(target, build_path):
# TODO taking [0] I don't expect to have 2 filenames here
Copy link
Member

Choose a reason for hiding this comment

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

Can we add an assert to make check this expectation?

Suggested change
# TODO taking [0] I don't expect to have 2 filenames here
# TODO taking [0] I don't expect to have 2 filenames here
assert len(target["filename"]) == 1

@@ -40,6 +40,11 @@ jobs:
- bash: |
./build_tools/linting.sh
displayName: Run linters
- bash: |
pip install ninja meson scipy
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised that SciPy is required for this check. Is this required by meson?

Copy link
Member Author

Choose a reason for hiding this comment

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

Since #28721 we are checking the scipy version in sklearn/meson.build (the build dependencies versions in general), so you need scipy to run meson setup.

scipy_version = run_command(py,
['-c', 'import scipy; print(scipy.__version__)'], check: true).stdout().strip()
scipy_min_version = run_command(py, ['_min_dependencies.py', 'scipy'], check: true).stdout().strip()
if not scipy_version.version_compare('>=' + scipy_min_version)
error('scikit-learn requires scipy>=' + scipy_min_version + ', got ' + scipy_version + ' instead')

I agree it is a bit surprising but I am not sure this is worth complicating the meson.build.

}

foreach ext_name, ext_dict : cluster_extension_metadata
py.extension_module(
ext_name,
[ext_dict.get('sources'), utils_cython_tree],
dependencies: [np_dep, openmp_dep],
dependencies: [np_dep] + ext_dict.get('dependencies', []),
Copy link
Member

Choose a reason for hiding this comment

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

does this now make things more specific since we have the linter?

Copy link
Member Author

@lesteve lesteve Sep 2, 2024

Choose a reason for hiding this comment

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

The linter will catch if an extension module use OpenMP flags but does not use it, where "using OpenMP" is defined by the following git grep regexp. I think it makes sense but please double-check:

git grep -lP "cython.*parallel|_openmp_helpers"

https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/cluster/_dbscan_inner.pyx and https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/cluster/_hierarchical_fast.pyx do not use OpenMP AFAICT.

dependencies: [np_dep, openmp_dep],
dependencies: [np_dep],
Copy link
Member

Choose a reason for hiding this comment

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

curious why you thought these are needed before then. And are they really not needed?

Copy link
Member Author

@lesteve lesteve Sep 2, 2024

Choose a reason for hiding this comment

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

curious why you thought these are needed before then.

This is the most complicated meson.build we have and I would not be surprised if I cut some corners when I put it together.

And are they really not needed?

I am pretty sure they are not needed since there is nothing related to OpenMP in https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/metrics/_pairwise_distances_reduction/_datasets_pair.pyx.tp.

from pathlib import Path


def uses_openmp(targets):
Copy link
Member

Choose a reason for hiding this comment

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

a bit of comment as what each function does would make reading the code easier, but not a big deal.

Copy link
Member Author

Choose a reason for hiding this comment

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

That was part of the clean-up I had in mind indeed 😉

@lesteve
Copy link
Member Author

lesteve commented Sep 3, 2024

OK I did the clean-up I had in mind, let me know if you think more doc/clarification is needed.

Copy link
Member

@adrinjalali adrinjalali left a comment

Choose a reason for hiding this comment

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

Love this! 🥳

Copy link
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

No time to review in details nor to test that the script works as expected myself but I spotted a quick typo:

Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
Copy link
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

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

I could get what happens by reading the file so I'm pretty happy with the script.

@glemaitre glemaitre merged commit 6032c95 into scikit-learn:main Sep 5, 2024
30 checks passed
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Sep 9, 2024
Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
glemaitre pushed a commit that referenced this pull request Sep 11, 2024
Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
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