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

Allow Cython module components to be compiled independently #1334

Merged
merged 8 commits into from
Jul 10, 2022

Conversation

speth
Copy link
Member

@speth speth commented Jul 6, 2022

Changes proposed in this pull request

  • Separate includes (both C++ and Cython) needed by each .pyx file so they can be compiled independently
  • Combine the separately compiled submodules into a single shared library
  • Use the solution from this StackOverflow post to import the resulting submodules from the same shared library

If applicable, provide an example illustrating new features this pull request is introducing

This change substantially speeds up compilation of the Cython module when compiling with multiple cores, or when making changes that only affect a subset of the .pyx files.

Timings for the current main branch:

# Compile everything
$ time scons build -j4 &>/dev/null

real    2m35.281s
user    5m58.835s
sys     0m18.177s

# Re-cythonize and compile just the Python module
$ rm -rf build/temp-py build/python
$ time scons build -j4 &>/dev/null

real    1m14.313s
user    1m10.681s
sys     0m5.905s

Timings for this PR:

# Compile everything
$ rm -rf build/temp-py build/python
$ time scons build -j4

real    2m5.328s
user    6m42.189s
sys     0m20.347s

# Re-cythonize and compile just the Python module
$ time scons build -j4

real    0m42.615s
user    1m54.346s
sys     0m7.753s

# With a lot of cores
$ time scons build -j16

real    0m26.209s
user    1m55.752s
sys     0m8.489s

# With just 2 cores
$ time scons build -j2
real    1m6.462s
user    1m54.324s
sys     0m7.826s

Note that the total CPU time increases by a fair amount, due to the duplicated work done in compiling some common code for each .pyx file. However, even with 2 cores, the wall-clock time decreases.

In addition, this provides some of the structure envisioned by Cantera/enhancements#153, in that the classes and methods are accessible using names qualified with the corresponding submodule, such as:

from cantera.reactor import IdealGasReactor
from cantera.onedim import FreeFlame

although they are also available directly in the cantera module as well. While I don't want to pursue this as part of the current PR, I think this restructuring also provides most of the necessary machinery to support building only a subset of the C++ source and the corresponding portions of the Python module, such as compiling everything except the reactor/flame models.

Checklist

  • The pull request includes a clear description of this code change
  • Commit messages have short titles and reference relevant issues
  • Build passes (scons build & scons test) and unit tests address code coverage
  • Style & formatting of contributed code follows contributing guidelines
  • The pull request is ready for review

@codecov
Copy link

codecov bot commented Jul 6, 2022

Codecov Report

Merging #1334 (24b5fc6) into main (0925fc0) will decrease coverage by 0.03%.
The diff coverage is 84.92%.

@@            Coverage Diff             @@
##             main    #1334      +/-   ##
==========================================
- Coverage   68.04%   68.00%   -0.04%     
==========================================
  Files         314      318       +4     
  Lines       42007    42007              
  Branches    16880    16880              
==========================================
- Hits        28582    28568      -14     
  Misses      11167    11167              
- Partials     2258     2272      +14     
Impacted Files Coverage Δ
include/cantera/cython/utils_utils.h 63.15% <63.15%> (ø)
include/cantera/cython/kinetics_utils.h 82.85% <82.85%> (ø)
include/cantera/cython/thermo_utils.h 100.00% <100.00%> (ø)
include/cantera/cython/transport_utils.h 100.00% <100.00%> (ø)
include/cantera/cython/wrappers.h 100.00% <100.00%> (+14.84%) ⬆️
include/cantera/cython/funcWrapper.h 72.72% <0.00%> (-12.73%) ⬇️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@speth speth force-pushed the split-cython-compilation branch from 750a87a to 231e839 Compare July 6, 2022 16:58
Copy link
Member

@bryanwweber bryanwweber left a comment

Choose a reason for hiding this comment

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

Just a few minor changes here @speth for consistency/cleanup. Thanks for taking this on!

include/cantera/cython/kinetics_utils.h Outdated Show resolved Hide resolved
interfaces/cython/cantera/_cantera.pyx Outdated Show resolved Hide resolved
interfaces/cython/cantera/_cantera.pyx Outdated Show resolved Hide resolved
interfaces/cython/cantera/_cantera.pyx Outdated Show resolved Hide resolved
interfaces/cython/SConscript Outdated Show resolved Hide resolved
interfaces/cython/cantera/ctcxx.pxd Show resolved Hide resolved
interfaces/cython/cantera/ctcxx.pxd Outdated Show resolved Hide resolved
interfaces/cython/cantera/delegator.pyx Show resolved Hide resolved
interfaces/cython/cantera/solutionbase.pyx Outdated Show resolved Hide resolved
interfaces/cython/cantera/transport.pyx Show resolved Hide resolved
@speth speth force-pushed the split-cython-compilation branch from 231e839 to d5fb15a Compare July 6, 2022 19:44
@speth speth force-pushed the split-cython-compilation branch from d5fb15a to a2f8872 Compare July 6, 2022 21:09
@speth speth requested a review from bryanwweber July 6, 2022 21:10
ischoegl
ischoegl previously approved these changes Jul 9, 2022
Copy link
Member

@ischoegl ischoegl left a comment

Choose a reason for hiding this comment

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

🎉 Looks good to me, but it may be good to time the merge together with a resolution of #1258? Then merge conflicts in other PR’s can be resolved in a single pass.

@speth
Copy link
Member Author

speth commented Jul 10, 2022

I was thinking about tackling #1258 next, but I don't know that there's anything that particularly helps with the merge conflicts in branches/PRs that already exist. Having to resolve merge conflicts in more files at once doesn't really make things simpler. I think the most useful thing to do is merge this now (assuming there are no further comments) so that no new branches/PRs will be created that don't already incorporate these changes.

@ischoegl
Copy link
Member

@speth … good to hear that #1258 is next, so your suggestion is good. @bryanwweber … do you have anything else?

@bryanwweber
Copy link
Member

@speth I added the license header to all the files in the Cython folder, I'm not sure if it was intentional to avoid adding it to the .pxd files, but some of them had it and some of them didn't, so I figured adding it everywhere was the best course. Assuming CI passes, this LGTM

@ischoegl
Copy link
Member

Looks good to me as well. @bryanwweber, please approve if you’re fine with everything

@speth
Copy link
Member Author

speth commented Jul 10, 2022

I thought I had added the license header everywhere. Some of that must have gotten lost when I was fixing the merge conflicts to rebase past #1327 being merged.

Copy link
Member

@bryanwweber bryanwweber left a comment

Choose a reason for hiding this comment

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

Thanks @speth! In it goes!

@bryanwweber bryanwweber merged commit b2a7fd9 into Cantera:main Jul 10, 2022
@speth speth deleted the split-cython-compilation branch July 10, 2022 22:29
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.

3 participants