Skip to content

Conversation

KofClubs
Copy link
Contributor

Related to #14105

@KofClubs
Copy link
Contributor Author

KofClubs commented Jun 20, 2024

I don't handle KahanSumHint in Copy*, Equals etc.

I don't apply Kahan summation algorithm to other operations, i.e., subtraction, multiplication, and division.

@KofClubs KofClubs force-pushed the kahan-sum-histogram branch from 79f52ae to 0b472a8 Compare June 20, 2024 17:21
@KofClubs KofClubs changed the title [WIP] promql(histogram): apply kahan summation algorithm to native histograms promql(histogram): apply kahan summation algorithm to native histograms Jun 20, 2024
@KofClubs KofClubs marked this pull request as ready for review June 20, 2024 17:28
@beorn7 beorn7 self-requested a review June 26, 2024 17:22
@beorn7
Copy link
Member

beorn7 commented Jun 27, 2024

Sorry, I will only get to this next week.

@KofClubs KofClubs force-pushed the kahan-sum-histogram branch from 0b472a8 to 56b0f37 Compare June 28, 2024 08:16
@KofClubs KofClubs requested a review from roidelapluie as a code owner June 28, 2024 08:16
Signed-off-by: Zhang Zhanpeng <zhangzhanpeng.zzp@alibaba-inc.com>
@KofClubs KofClubs force-pushed the kahan-sum-histogram branch from 56b0f37 to 0898d62 Compare June 28, 2024 08:37
Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

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

Thank for doing this, but as said in the comments below, we need Kahan summation for all the fields of the histogram that are involved in a summation.

We also don't want additional fields in the FloatHistogram struct as this struct is already big enough.

This is definitely not as easy as it might have looked. I'll try to write down a few ideas later.

@@ -30,6 +30,8 @@ import (
type FloatHistogram struct {
// Counter reset information.
CounterResetHint CounterResetHint
// KahanSumHint runs compensation for lost low-order bits.
KahanSumHint float64
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should add a field to every single histogram. It takes another 8 bytes for every histogram, including those that never need the summation.

Also, just one Kahan hint isn't enough, see below.

Comment on lines +358 to +361
y := other.Sum - h.KahanSumHint
t := h.Sum + y
h.KahanSumHint = (t - h.Sum) - y
h.Sum = t
Copy link
Member

Choose a reason for hiding this comment

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

Note that there already is a function kahanSumInc in promql/engine.go. (Which cannot simply be used here because it would cause a circular package dependency. Let's discuss that later.)

More importantly: This only handles the Sum field, but Kahan summation should be used on all the fields that get added up. I think we need a very different approach here.

@beorn7
Copy link
Member

beorn7 commented Jul 3, 2024

Broadly, I believe we need an equivalent of the kahanSumInc function for FloatHistogram.

Maybe it could be a method for FloatHistogram with a signature like func (h *FloatHistogram) KahanAdd(inc *FloatHistogram, c *FloatHistogram) (newSum *FloatHistogram, newC *FloatHistogram, err error) (where the receiving histogram takes the role of sum). So the Kahan compensation term also has to be a histogram. The tricky part is that it has to handle all the bucket merging appropriately. (This needs quite deep understanding of native histograms and the Kahan summation.)

Once we have this, we can use the new KahanAdd for histograms in all situation where the existing kahanSumInc is used for floats (and there is a corresponding operation for histograms).

@KofClubs as alluded to above, this is way harder than anticipated. It would be totally OK if you preferred to tackle a more light-weight issue first.

@beorn7
Copy link
Member

beorn7 commented Jul 23, 2024

I'm closing this PR as the approach we have to take will be quite different and should probably happen in a separate PR, see comments above.

@beorn7 beorn7 closed this Jul 23, 2024
@KofClubs
Copy link
Contributor Author

Ah, I'm actually still working on this issue. I shall open a new PR later:)

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.

2 participants