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

Combine Shape map calls #6227

Merged
merged 6 commits into from
Sep 16, 2024
Merged

Conversation

nikwit
Copy link
Contributor

@nikwit nikwit commented Aug 16, 2024

Currently, the shape map computes the coordinates, frame velocity and jacobians separately which duplicates a lot of work such as computing the spherical harmonic expansion.

This PR adds a combined call to these three quantities. I found that this speeds up simulations between 16 % and 27% (when combined with #6223)

depends on #6223

image

Copy link
Contributor

@knelli2 knelli2 left a comment

Choose a reason for hiding this comment

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

Thank you for investigating this!

src/Domain/CoordinateMaps/TimeDependent/Shape.cpp Outdated Show resolved Hide resolved
Comment on lines 457 to 484
static constexpr bool use_combined_shape_call =
std::is_same_v<CoordinateMaps::TimeDependent::Shape, Map> and
std::is_same_v<T, DataVector>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of checking if this is a shape map explicitly (and thus adding an strange dependency for the general coord map on the time dependent maps), add a meta function that checks if the Map object has a function named coords_frame_velocity_jacobian that can be called with the given arguments.

Then this easily allows us to edit the other time dependent maps if we want to to add this feature to those as well

Copy link
Member

Choose a reason for hiding this comment

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

Note: I think we could get away with a forward declaration of the map to avoid the coupling. Just providing another option.

Copy link
Member

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

LGTM, no need for my approval to merge.

@@ -258,6 +258,7 @@ class Shape {
size_t l_max_ = 2;
size_t m_max_ = 2;
ylm::Spherepack ylm_{2, 2};
ylm::Spherepack extended_ylm_{2, 2};
Copy link
Member

Choose a reason for hiding this comment

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

3, 3?

Comment on lines 457 to 484
static constexpr bool use_combined_shape_call =
std::is_same_v<CoordinateMaps::TimeDependent::Shape, Map> and
std::is_same_v<T, DataVector>;
Copy link
Member

Choose a reason for hiding this comment

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

Note: I think we could get away with a forward declaration of the map to avoid the coupling. Just providing another option.

@nikwit
Copy link
Contributor Author

nikwit commented Aug 19, 2024

@knelli2 I have factored out large chunks of the jacobian, I do not think this is worth doing for the operator() or frame velocity as it is just two statements. I also avoid duplicate calls to the transition_function in the combined call which I had overlooked before.

Copy link
Contributor

@knelli2 knelli2 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! You can squash this last change in with everything else

src/Domain/CoordinateMaps/TimeDependent/Shape.hpp Outdated Show resolved Hide resolved
Copy link
Contributor

@knelli2 knelli2 left a comment

Choose a reason for hiding this comment

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

Also fix the compilation errors from gcc-9 and the clang-tidy issues. The one about using l as a variable you can ignore or add it to our .clang-tidy file because it's pretty silly

@nikwit nikwit force-pushed the combine-shape-calls branch 4 times, most recently from d7a701e to bf2cf78 Compare August 22, 2024 13:59
@nikwit
Copy link
Contributor Author

nikwit commented Aug 22, 2024

Thanks for your reviews. I changed everything but the long link domain::CoordinateMaps::ShapeMapTransitionFunctions::ShapeMapTransitionFunction::original_radius_over_radius in the documentation Shape.hpp:162 is over 80 characters and not resolved if I break it up over two lines. Does someone know how this can be resolved?

Copy link
Contributor

@knelli2 knelli2 left a comment

Choose a reason for hiding this comment

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

You can squash directly. As for the long link, our commit hooks check for linked that contain either \link or \endlink and ignore those, so just make sure one of those is on the same line as the link.

src/Domain/CoordinateMaps/TimeDependent/Shape.hpp Outdated Show resolved Hide resolved
@nikwit nikwit force-pushed the combine-shape-calls branch from bf2cf78 to 69a5ac0 Compare August 22, 2024 18:42
@nikwit
Copy link
Contributor Author

nikwit commented Aug 22, 2024

all done and squashed in, thank you for the review!

knelli2
knelli2 previously approved these changes Aug 22, 2024
Copy link
Contributor

@knelli2 knelli2 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 optimizing this!

@knelli2 knelli2 enabled auto-merge August 22, 2024 19:06
@knelli2 knelli2 added the auto-merge GitHub's auto-merge has been enabled for this PR. label Aug 22, 2024
@nikwit nikwit requested a review from knelli2 August 23, 2024 08:13
@knelli2
Copy link
Contributor

knelli2 commented Aug 23, 2024

This test failure is concerning me since I (and @nilsvu) have never seen it before and I want to make sure it's not related to the shape map changes. The test failure happened twice in a row with the same error

@nikwit
Copy link
Contributor Author

nikwit commented Aug 23, 2024

oh, that is weird. So it only fails with clang-13 in Release mode. I don't know the executable at all. Would this executable call the coords_framevel_jac function of CoordinateMap? If so, do you know which compile time and runtime path it would take in that function? @nilsvu

@knelli2 knelli2 removed the auto-merge GitHub's auto-merge has been enabled for this PR. label Aug 24, 2024
@knelli2 knelli2 disabled auto-merge August 24, 2024 01:54
@nikwit
Copy link
Contributor Author

nikwit commented Aug 25, 2024

Looks like reducing the LMax reduced the weird test failure of clang-13 in the Xcts executable @knelli2

@nikwit
Copy link
Contributor Author

nikwit commented Aug 28, 2024

@knelli2 @nilsvu @nilsdeppe I looked into the test failure a bit more. It seems that the test failure is unrelated to combining the shape map calls. I can "fix" the failure by simply adding or removing some temporaries in the shape map that change the evaluation order of operations (all additions and multiplications). One change that causes the test to fail is e.g. here, where I just introduce a temporary that should definitely be safe: nikwit@6ecb730

Also, simply inlining the jacobian_helper function causes the test to work.

I am therefore convinced that this PR simply changes some floating point precision that propagates through the elliptic solver and changes the residuals that are computed (which are still convergent). IMO, we should simply raise the tolerance of that test.

@knelli2
Copy link
Contributor

knelli2 commented Sep 5, 2024

@nilsvu I think it is up to you what to do here since this only affects the elliptic solver.

Also @nikwit this has a conflict now and needs a rebase

@nilsvu
Copy link
Member

nilsvu commented Sep 5, 2024

I ran this branch on my machine and the test passes. It looks like specifically clang-13 Release fails, and specifically the first GMRES iteration after AMR iteration is different. The shape map gets evaluated on the new refined grid points, so at least it's plausible that an issue in the shape map can cause this failure. I worries me enough that I wouldn't just reduce the Lmax to "fix" it without checking that BBH evolutions aren't affected. Suggestions:

  • How about adding a regression test to GH/KerrSchild.yaml like the one in Xcts/KerrSchild.yaml, to check that the first couple of time steps remain identical? This should use the same Kerr spin and shape map Lmax that caused issues in XCTS.
  • ylm::Spherepack does some fancy caching in a static thread_local memory pool, maybe that's causing issues somehow? Just speculating. I guess you could try making it a normal member variable by removing the static thread_local just to see if that affects the issue.
  • You could also run a BBH on clang-13 Release to make sure the results are the same before and after this PR (to numerical roundoff error, not just truncation error).

It's a very mysterious issue, and I just want to ensure that it doesn't affect our BBH simulations in mysterious ways 😅

@nikwit
Copy link
Contributor Author

nikwit commented Sep 5, 2024

@nilsvu I think my analysis above shows that the issue is related simply to the order in which expressions are evaluated in the shape map (nikwit@6ecb730 fixes it!). The test appears to blow up a floating point difference to a difference of 1e-9 somehow. I do not see how this can be related to ylm::Spherepack.

My suspicion is that the test is not a fair test. Can you describe where the residuals that are compared in the test come from? It looks like the test uses 4 grid points per element and no h-refinement. How can this lead to a residual of 1e-11?

@nilsvu
Copy link
Member

nilsvu commented Sep 5, 2024

Can you describe where the residuals that are compared in the test come from? It looks like the test uses 4 grid points per element and no h-refinement. How can this lead to a residual of 1e-11?

The refinement of the domain and the DG scheme defines a discretized problem $A(u_q)_p = b_p$ over the grid points (it's a matrix equation if the problem is linear). The residual is the $L_2$ norm $|| A(u_q)_p - b_p||_2$ over the grid points. This discretized problem can be solved for the values $u_p$ down to machine precision (or to 1e-11 in this case), independent of resolution. If the problem is linear it's just a matrix inversion, which can be done to machine precision. This accuracy is only the numerical solution to the discretized problem, it doesn't mean the truncation error is so small as well. The truncation error is limited by how well the discretized problem represents the true problem, and is ~1e-3 in this case (see the "Discretization error" check in KerrSchild.yaml, which compares the solution $u_p$ to the analytic solution at every grid point).

Now I don't see how the numerical algorithm that solves the discretized problem should blow up roundoff error in the shape map to truncation error level differences, though I might be missing something of course.

@nikwit
Copy link
Contributor Author

nikwit commented Sep 6, 2024

alright, I see.

At this point I would argue that since changing the order in which DataVectors are evaluated fixes the test, this must mean that any issue that may or may not be in the code is unrelated to this PR. If anything, the failing test is a symptom of an issue already on develop (though I do not believe there is one).

@nilsvu
Copy link
Member

nilsvu commented Sep 12, 2024

I have now seen two other cases of this test failing on CI, so I agree that it is not directly related to the changes in this PR and I'm happy with this PR getting merged. I still have no idea what causes the issue though :(

@knelli2
Copy link
Contributor

knelli2 commented Sep 12, 2024

@nikwit Just needs another rebase to fix the conflict and then I'll merge this. @nilsvu Thanks for looking into this!

@nikwit nikwit force-pushed the combine-shape-calls branch from eef6f45 to dc14ca4 Compare September 16, 2024 08:35
@nikwit
Copy link
Contributor Author

nikwit commented Sep 16, 2024

rebased. Thank you all for your due diligence!

@knelli2 knelli2 enabled auto-merge September 16, 2024 15:16
@knelli2 knelli2 added the auto-merge GitHub's auto-merge has been enabled for this PR. label Sep 16, 2024
@knelli2 knelli2 merged commit bcc6763 into sxs-collaboration:develop Sep 16, 2024
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-merge GitHub's auto-merge has been enabled for this PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants