Skip to content

Vectorize "sum" function - #10992

Merged
alexey-milovidov merged 8 commits into
masterfrom
vectorize-sum
May 21, 2020
Merged

Vectorize "sum" function#10992
alexey-milovidov merged 8 commits into
masterfrom
vectorize-sum

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented May 18, 2020

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Make queries with sum aggregate function and without GROUP BY keys to run multiple times faster.

Detailed description / Documentation draft:
The performance of sum and sumKahan is increased multiple times.
TODO: Do the same for avg, min, max.

@alexey-milovidov
alexey-milovidov requested a review from amosbird May 18, 2020 03:53
@blinkov blinkov added the pr-performance Pull request with some performance improvements label May 18, 2020
void NO_INLINE addMany(const Value * __restrict ptr, size_t count)
{
/// Compiler cannot unroll this loop, do it manually.
/// (at least for floats, most likely due to the lack of -fassociative-math)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why not add -fassociative-math then?

@alexey-milovidov alexey-milovidov May 18, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It will make Kahan summation algorithm to have no effect.

But we can enable

-fno-math-errno
-fno-rounding-math
-fno-signaling-nans
-fno-signed-zeros
-fno-trapping-math
-fassociative-math
-freciprocal-math

for all codebase and disable it (with pragma or function attributes) whenever something like Kahan summation is used.
More care should be taken to figure out these places.

@KochetovNicolai KochetovNicolai self-assigned this May 18, 2020
@alexey-milovidov
alexey-milovidov marked this pull request as ready for review May 18, 2020 16:31
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Performance — 13 faster

  • it's a clear win.

@alexey-milovidov

alexey-milovidov commented May 18, 2020

Copy link
Copy Markdown
Member Author

Next steps (in subsequent PRs):

  1. Check that min/max are already vectorized for all data types.
  2. Implement the same method for avg.
  3. Implement specialization for Nullable for min/max/avg.


/// Vectorized version
template <typename Value>
void NO_INLINE addMany(const Value * __restrict ptr, size_t count)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we can make this algorithm more public. Probably, move it to ColumnsCommon.h or somewhere to Common.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As I've googled, __restrict means that data accessed by ptr can't be changed externally.
Probably, we may add comment about it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think we can make this algorithm more public.

Whenever we will have a second use case.

Comment thread src/AggregateFunctions/AggregateFunctionSum.h
{
auto raw_sum = to_sum + from_sum;
auto rhs_compensated = raw_sum - to_sum;
auto compensations = ((from_sum - rhs_compensated) + (to_sum - (raw_sum - rhs_compensated))) + compensation + from_compensation;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've tried to understand what is going on here and gave up.
Can we simplify it, or add a comment with formula and idea how do we merge?
E.g. why it is not just

addImpl(to_sum, to_compensation, from_sum);
addImpl(to_sum, to_compensation, -from_compensation);

Or maybe it is reasonable to choose which sum is bigger?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's tricky due to non-associative float math. And Kahan summation algorithm depends on it.
We cannot reorder or simplify expressions.

I'll add a comment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, I understand that Kahan summation depends on non-associative float math.
I just don't understand what this code does. And why, e.g., it's hot just two calls of addImpl.
Code in addImpl also depends on non-associative math, but I can understand it (at least by reading wikipedia article).
New comments also don't let me understand code better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-docs-needed pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants