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

ENH: minimizer performance improvements #424

Merged

Conversation

bocklund
Copy link
Collaborator

@bocklund bocklund commented Jul 18, 2022

Profiling-guided removal of (relatively) expensive NumPy calls

With a couple ternary benchmark systems, I'm seeing a consistent 2x speedup in the run_loop time and almost all the profiled code that drops back into Python (from Cython) is gone.

Summary of changes:

  1. Don't recompute ALLOWED_MASS_RESIDUAL on the hot loop. We can compute it in the initializer if we assume the prescribed elemental amounts is constant
  2. Replace np.sum with manually computed phase composition sums in SystemState initializer and recompute()
  3. Replace np.all call in consolidation check for remove_and_consolidate_phases
  4. Replace np.take, np.nonzero, np.argsort with loopy equivalents (this is less of a speedup, but we probably just hit this code path less in the benchmark and in practice)

The intent was to preserve exactly the current functionality and just be pure performance improvements (any differences detected in review are not intentional).

Benchmarks

ebcfbdb (develop)

bm-2022-07-18-ebcfbdb4-Cr-Ti-V-profile

b3668f4

bm-2022-07-18-b3668f40-Cr-Ti-V-profile

With profiling, this corresponds to roughly a factor of 2 performance improvement in a ternary benchmark system

1. Don't recomputed ALLOWED_MASS_RESIDUAL on the hot loop. We can compute it in the initializer if we assume the prescribed elemental amounts is constant
2. Replace np.sum() with manually computed phase composition sums in SystemState initializer and recompute()
3. Replace np.all call in consolidation check for remove_and_consolidate_phases

The only thing left showing up in the profile inside the run_loop function is np.take, np.nonzero, and np.argsort, which only appear inside change_phases()
@bocklund bocklund changed the title Enh minimizer performance improvements ENH: minimizer performance improvements Jul 18, 2022
@codecov
Copy link

codecov bot commented Jul 19, 2022

Codecov Report

Merging #424 (b3668f4) into develop (ebcfbdb) will increase coverage by 0.04%.
The diff coverage is 86.53%.

@@             Coverage Diff             @@
##           develop     #424      +/-   ##
===========================================
+ Coverage    90.22%   90.27%   +0.04%     
===========================================
  Files           50       50              
  Lines         7696     7723      +27     
===========================================
+ Hits          6944     6972      +28     
+ Misses         752      751       -1     
Impacted Files Coverage Δ
pycalphad/core/minimizer.pyx 84.33% <86.53%> (+0.86%) ⬆️

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

@bocklund bocklund marked this pull request as ready for review August 3, 2022 20:35
@bocklund
Copy link
Collaborator Author

bocklund commented Aug 3, 2022

@richardotis I'm happy with this PR where it is if you want to take another quick pass before merging. I'm not ready to increase the scope for convergence related things yet, so this one can just be performance focused.

@bocklund bocklund merged commit 04b870e into pycalphad:develop Aug 8, 2022
@bocklund bocklund deleted the ENH-minimizer-performance-improvements branch August 8, 2022 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants