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

Upgrade to Cython 3+ in building #57

Merged
merged 5 commits into from Oct 13, 2023
Merged

Conversation

djhoese
Copy link
Member

@djhoese djhoese commented Aug 30, 2023

This PR upgrades the build dependencies to use Cython 3+. It also adds extra annotations required by Cython 3 to avoid extra Python-related checks and stay in C-land longer and with no GIL. As far as I can tell nothing of the generated code should be different to what it was.

Note I had concerns because the modis interpolation has some low-level functions with GIL-related when memory views are returned. I made a stackoverflow question about it and a Cython maintainer responded:

https://stackoverflow.com/questions/76972950/cython-returned-memoryview-is-always-considered-uninitialized

Basically, even though it is checking the memory view for validity/initialization, it is not actually acquiring the GIL except in the failure case which should never happen for us. We could restructure the code to not create a copy of the memory views used but instead adjust the indexing appropriately. The code would be harder to read but would not have the extra memoryview checks. I doubt it would have any performance difference.

I have not run a comparison of the performance, but given the logical changes appearing in the Cython I'm not too worried. I'll have to track down my old benchmarking from the last time the Cython was touched.

@djhoese djhoese added enhancement dependencies Pull requests that update a dependency file labels Aug 30, 2023
@codecov
Copy link

codecov bot commented Aug 30, 2023

Codecov Report

Merging #57 (f763c7d) into main (285b6b4) will increase coverage by 0.26%.
Report is 2 commits behind head on main.
The diff coverage is 87.50%.

@@            Coverage Diff             @@
##             main      #57      +/-   ##
==========================================
+ Coverage   87.81%   88.07%   +0.26%     
==========================================
  Files          21       21              
  Lines        1436     1434       -2     
==========================================
+ Hits         1261     1263       +2     
+ Misses        175      171       -4     
Flag Coverage Δ
unittests 88.07% <87.50%> (+0.26%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
geotiepoints/interpolator.py 84.17% <ø> (ø)
geotiepoints/modisinterpolator.py 100.00% <ø> (ø)
geotiepoints/tests/__init__.py 100.00% <ø> (ø)
geotiepoints/multilinear_cython.pyx 35.71% <75.00%> (+3.17%) ⬆️
geotiepoints/_modis_interpolator.pyx 99.23% <100.00%> (-0.02%) ⬇️
geotiepoints/_modis_utils.pyx 92.30% <100.00%> (+0.06%) ⬆️
geotiepoints/_simple_modis_interpolator.pyx 100.00% <100.00%> (ø)
geotiepoints/multilinear.py 90.62% <100.00%> (ø)

@coveralls
Copy link

coveralls commented Aug 30, 2023

Coverage Status

coverage: 87.219% (+0.2%) from 86.971% when pulling ac8909c on djhoese:feature-cython-3 into 285b6b4 on pytroll:main.

@djhoese
Copy link
Member Author

djhoese commented Aug 30, 2023

So this should be the results of the same MODIS operation I did in my #38 PR:

image

I note that the memory profile looks about the same but increases more at the end than previously...but that is from other parts of Satpy, not the interpolation which is only the blue/green blocks near the beginning. I don't like the dips in CPU usage at the beginning even though execution time is about the same.

Copy link
Member

@mraspaud mraspaud left a comment

Choose a reason for hiding this comment

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

LGTM, even if I haven't checked the new features of Cython 3. Do you want to wait for an answer about the cython 3 issue you found before we merge this?

@djhoese
Copy link
Member Author

djhoese commented Aug 31, 2023

Yeah let's wait a bit. I'm more OK with trollimage being released than with this being merged given the issues.

@djhoese
Copy link
Member Author

djhoese commented Sep 7, 2023

FYI the bug encountered in cython seems to have been fixed, but I don't think it is released yet.

@djhoese
Copy link
Member Author

djhoese commented Sep 8, 2023

FYI I did a test compile with Cython's main branch and CPU usage seems to be much better. Not perfect, but I think it's as good as we're going to get. I'll try to skim through the C code and see if I can find any GIL acquires that I don't agree with.

@djhoese
Copy link
Member Author

djhoese commented Sep 8, 2023

Nevermind, this is another major one: cython/cython#5688

@djhoese
Copy link
Member Author

djhoese commented Sep 8, 2023

Nevermind on that one, but this one is still a thing: cython/cython#5685

Also revert noexcept change on functions that return a memoryview. In current versions of Cython 3 this marks the function in a way that means it needs error checking and the GIL which can cause GIL usage in other parts of the module.
@djhoese djhoese merged commit 2944321 into pytroll:main Oct 13, 2023
15 of 17 checks passed
@djhoese djhoese deleted the feature-cython-3 branch October 13, 2023 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Upgrade to Cython 3.0 and check annotations
3 participants