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

Kahan and Klein summation #695

Open
zerothi opened this issue Feb 21, 2023 · 15 comments
Open

Kahan and Klein summation #695

zerothi opened this issue Feb 21, 2023 · 15 comments
Labels
idea Proposition of an idea and opening an issue to discuss it topic: algorithms searching and sorting, merging, ... topic: mathematics linear algebra, sparse matrices, special functions, FFT, random numbers, statistics, ...

Comments

@zerothi
Copy link

zerothi commented Feb 21, 2023

Motivation

Precision summations are necessary for certain operations (also suggested for cumsum)

Would you accept a type(Kahan(dp)) and type(Klein(dp)), and precision variants for inclusion in the stdlib?

Prior Art

No response

Additional Information

No response

@zerothi zerothi added the idea Proposition of an idea and opening an issue to discuss it label Feb 21, 2023
@zerothi
Copy link
Author

zerothi commented Feb 21, 2023

Instead of having the type in the name, I would suggest we name them:

type(Kahan_real(dp)) :: rsum
type(Kahan_cmplx(dp)) :: csum

to assign data-type?

@milancurcic
Copy link
Member

Thanks for this proposal. I think it's in scope and needed. I know of Kahan sum but not Klein--can you provide some reference for each here?

Regarding the API, can you explain the need for using derived types here? Could we not implement this as generic functions, i.e function sum_kahan(x)?

@milancurcic milancurcic added topic: algorithms searching and sorting, merging, ... topic: mathematics linear algebra, sparse matrices, special functions, FFT, random numbers, statistics, ... labels Feb 22, 2023
@zerothi
Copy link
Author

zerothi commented Feb 22, 2023

  1. The Klein summation is a 2nd order improvement of the Kahan summation. In fact there is a simple extension of the Kahan summation that fixes cases where the input value is larger than the currently summed quantity (e.g. single values are much much larger than the rest) or similar. Perhaps we can start with the regular Kahan, and then decide on how to add more advanced schemes, see for instance https://en.wikipedia.org/wiki/Kahan_summation_algorithm, I just call it Klein to not have excessively long names
  2. The point is that Kahan summation might be needed on non-array summations, consider complex arithmetic on various array combinations a(:) * b(:) * sqrt(c(:) + a2(:,1) * a2(:,2), in this case I see a great value in enabling a user-defined type that sums arrays.
  3. I do plan (and have a code) that adds the functions for regular arrays, as your sum_kahan. But I think both are necessary.

@arjenmarkus
Copy link
Member

arjenmarkus commented Feb 22, 2023 via email

@zerothi
Copy link
Author

zerothi commented Feb 22, 2023

I agree that compilation problems are vital.
However, we could add a test when compiling it to see if optimizations turn them off. And/or change the default flags for GNU/Intel compilers to obey code layout for this code only. Again, since incremental summations might be useful I think the type is necessary. As for the array summation, agreed that could be split into pairwise, etc. algorithms, but that isn't the point here, and those should probably be put in another module (stdlib_reduce for instance which would also accommodate norms etc.) :)

@arjenmarkus
Copy link
Member

arjenmarkus commented Feb 22, 2023 via email

@zerothi
Copy link
Author

zerothi commented Feb 22, 2023

I agree that the compiler problem might be a blocker, but at least having the algorithm in-place and warning users about possible problems might be the only way to have the possibility in stdlib.. hmm...

@arjenmarkus
Copy link
Member

arjenmarkus commented Feb 22, 2023 via email

@zerothi
Copy link
Author

zerothi commented Feb 22, 2023

Ah yes, good point. So bottom line:

  1. Implement kahan summation (original version)
  2. use volatile attributes
  3. use types for custom summation paths
  4. an array summation with kahan for easy solution

Anything else?

@ivan-pi
Copy link
Member

ivan-pi commented Mar 1, 2023

Is this issue similar (or a duplicate) to #402?

@ivan-pi
Copy link
Member

ivan-pi commented Mar 1, 2023

A pitfall of this algorithm is that compilers may decide that the parentheses are superfluous and thereby destroy the effect. How do you guard against that?

In principle you could parse the contents of compiler_options() intrinsic function and issue a warning to the error_unit, say if -ffast-math was detected with gfortran.

But in general I'd just go with a warning in the documentation.

@zerothi
Copy link
Author

zerothi commented Mar 2, 2023

Yeah, it is.
So is this something I should do, or?

@ivan-pi
Copy link
Member

ivan-pi commented Mar 2, 2023

If your reply is aimed at the compiler_options() comment, personally I wouldn't do it. Extra complexity for little benefit.

@zerothi
Copy link
Author

zerothi commented Mar 2, 2023

No, I am questioning whether I should make a PR, the details of how to fix things is something we could take in the draft (IMHO).

@jalvesz
Copy link
Contributor

jalvesz commented Nov 14, 2023

If of interest for this discussion, I have an implementation of the kahan summation on top of a chunks-based sum here: https://github.com/jalvesz/fast_math/blob/main/src/fast_sum.f90 (if aggressive optimizations are activated, the chunks approach would still give an accuracy that should be at least 1 order of magnitude better than intrinsic)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idea Proposition of an idea and opening an issue to discuss it topic: algorithms searching and sorting, merging, ... topic: mathematics linear algebra, sparse matrices, special functions, FFT, random numbers, statistics, ...
Projects
None yet
Development

No branches or pull requests

5 participants