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

Copy scalars in vector operations to make compiler optimize more #14253

Merged
merged 2 commits into from Sep 12, 2022

Conversation

kronbichler
Copy link
Member

If we keep the scalar variables, say a in an update a . x + y, as class variables, the compiler cannot prove that the variable a does not alias with one of the vector arrays. As a result, it keeps re-loading the variable over and over again, even though we know that it won't change. While the compiler can do that for simple code, it does not here because the functors get passed around between multiple functions and call variants, including TBB parallelization. In this PR, it is solved in two different way for the two use cases:

  • For the typical vector update loops, we simply create a local copy inside the part that runs the loop on a subrange, which will then signal to the compiler that its value is indeed constant throughout the loop. (Note that the compiler does not rely on the function being marked const, it really needs the local scope.)
  • For the reduction operations, we instead copy the functor (and with it, the value of the scalar) as we move into the worker routine. As the functors are small with only some pointer variables, this is cheap enough.

To make sure we do not lose these optimizations and their reason, I added some comments.

Copy link
Member

@drwells drwells left a comment

Choose a reason for hiding this comment

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

Nice.

Could you add your explanation of why some functors are passed by value and others by reference to the header?

@kronbichler
Copy link
Member Author

Could you add your explanation of why some functors are passed by value and others by reference to the header?

I will: In general, the rule is to copy the functors that act on individual elements (reductions), but not the ones still defining loops (vector-add-style functions).

@drwells drwells merged commit fb2242d into dealii:master Sep 12, 2022
@kronbichler kronbichler deleted the avoid_aliasing branch August 10, 2023 16:41
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.

None yet

4 participants