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

Optimize FixConservatives #5633

Merged

Conversation

nilsdeppe
Copy link
Member

@nilsdeppe nilsdeppe commented Nov 20, 2023

Proposed changes

note: I've left fixup commits that I will squash with the goal of making the PR easier to review.

  • Optimize dot_product for magnitude computation
  • SIMD FixConservatives
  • Optimize FixConservatives
  • Simplify FixConservatives if there's no magnetic field

Upgrade instructions

Code review checklist

  • The code is documented and the documentation renders correctly. Run
    make doc to generate the documentation locally into BUILD_DIR/docs/html.
    Then open index.html.
  • The code follows the stylistic and code quality guidelines listed in the
    code review guide.
  • The PR lists upgrade instructions and is labeled bugfix or
    new feature if appropriate.

Further comments

if (const auto tilde_d_mask =
rest_mass_density_times_lorentz_factor <
rest_mass_density_times_lorentz_factor_cutoff_;
simd::any(tilde_d_mask)) {
needed_fixing = true;
Copy link
Member

Choose a reason for hiding this comment

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

I believe this block should be masking modifications with tilde_d_mask.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is done mostly correctly in " fixup: FixConservatives simd " (I'll squash this into the other commit where it is wrong). However, the rest_mass_density_times_lorentz_factor was not set correctly with a mask, like you said. I'm doing that in a fixup. Would you mind just double checking in the final code to make sure?

Copy link
Member

Choose a reason for hiding this comment

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

This looks good now.

b_tilde_squared * one_over_sqrt_det_g * one_over_d_tilde;
const SimdType s_dot_tilde_b_tilde = get(tilde_s_dot_tilde_b)[s];
Copy link
Member

Choose a reason for hiding this comment

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

It seems like s_tilde_dot_b_tilde matches the other names better.

static std::array<T, 4> nonzero_bound(
const T& b_squared_over_d, const T& tau_over_d,
const T& normalized_s_dot_b, const T& lower_bound,
const T& sqr_normalized_s_dot_b_times_b_squared_over_d) {
return std::array{
-0.5 * b_squared_over_d *
(1.0 +
square(normalized_s_dot_b) * tau_over_d * (tau_over_d + 2.0)),
Copy link
Member

Choose a reason for hiding this comment

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

This is probably better? Unless there are significant speed differences in operations that I don't know about.

-0.5 * (b_squared_over_d +
        sqr_normalized_s_dot_b_times_b_squared_over_d * tau_over_d * (tau_over_d + 2.0))

Comment on lines 234 to 238
const bool non_zero_mag =
magnetic_field_treatment_ == hydro::MagneticFieldTreatment::AssumeNonZero
? true
: (magnetic_field_treatment_ !=
hydro::MagneticFieldTreatment::AssumeZero);
Copy link
Member

Choose a reason for hiding this comment

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

Equivalent: const bool non_zero_mag = magnetic_field_treatment_ != hydro::MagneticFieldTreatment::AssumeZero (or == AssumeNonZero since you overwrite it in the third case).

Comment on lines 376 to 380
return square(1.0 + local_lorentz_factor_minus_one) *
local_lorentz_factor_minus_one *
(2.0 + local_lorentz_factor_minus_one) /
square(1.0 + local_lorentz_factor_minus_one) *
square(d_tilde);
Copy link
Member

Choose a reason for hiding this comment

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

The square(1.0 + local_lorentz_factor_minus_one) factors cancel.

Copy link
Member Author

@nilsdeppe nilsdeppe 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 the reviews and sorry for the delay! I pushed a couple fixups. I'll squash all the SIMD stuff once you're happy :)

if (const auto tilde_d_mask =
rest_mass_density_times_lorentz_factor <
rest_mass_density_times_lorentz_factor_cutoff_;
simd::any(tilde_d_mask)) {
needed_fixing = true;
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is done mostly correctly in " fixup: FixConservatives simd " (I'll squash this into the other commit where it is wrong). However, the rest_mass_density_times_lorentz_factor was not set correctly with a mask, like you said. I'm doing that in a fixup. Would you mind just double checking in the final code to make sure?

wthrowe
wthrowe previously approved these changes Jan 17, 2024
Copy link
Member

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

if (const auto tilde_d_mask =
rest_mass_density_times_lorentz_factor <
rest_mass_density_times_lorentz_factor_cutoff_;
simd::any(tilde_d_mask)) {
needed_fixing = true;
Copy link
Member

Choose a reason for hiding this comment

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

This looks good now.

@nilsdeppe nilsdeppe force-pushed the optimize_fix_conservatives branch 2 times, most recently from 0caab91 to 269b890 Compare January 18, 2024 15:03
@nilsdeppe
Copy link
Member Author

This is now rebased and squashed :) Thanks for the reviews!

@kidder kidder merged commit 22cad5d into sxs-collaboration:develop Jan 19, 2024
22 checks passed
@nilsdeppe nilsdeppe deleted the optimize_fix_conservatives branch April 11, 2024 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants