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

WIP: Piecewise cubic monotone interpolation #874

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

BenWibking
Copy link
Collaborator

@BenWibking BenWibking commented May 4, 2023

PR Summary

Adds monotone interpolation using cubic Hermite polynomials that can be used on device. However, the MonotoneInterpolator itself must be constructed on the host, with the contents of the views passed to it accessible from both host and device (if used).

This depends on Kokkos 4 support in order to use SharedHostPinnedSpace (kokkos/kokkos#5405).

Reference: "Monotone piecewise cubic interpolation," SIAM J. Numer. Anal., Vol. 17, No. 2, April 1980. https://doi.org/10.1137/0717021

PR Checklist

  • Code passes cpplint
  • New features are documented.
  • Adds a test for any bugs fixed. Adds tests for new features.
  • Code is formatted
  • Changes are summarized in CHANGELOG.md
  • CI has been triggered on Darwin for performance regression tests.
  • Docs build
  • (@lanl.gov employees) Update copyright on changed files

@BenWibking BenWibking changed the title Piecewise cubic monotone interpolation WIP: piecewise cubic monotone interpolation May 4, 2023
Copy link
Collaborator

@brryan brryan left a comment

Choose a reason for hiding this comment

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

This is neat! Two quick thoughts now (mostly because I saw the include guard and got to thinking about that more generally)

src/utils/interpolate.hpp Outdated Show resolved Hide resolved
src/utils/interpolate.hpp Outdated Show resolved Hide resolved
Copy link
Collaborator

@Yurlungur Yurlungur left a comment

Choose a reason for hiding this comment

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

I think this is useful overall... I assuem a fleshed out PR would have tests. My one big concern is you show using it for both std::vector and a kokkos view... those types have different kinds of semantics: vectors are value semantics and views reference semantics. I worry that could get one in to trouble... but maybe it's fine.

private:
Real x_min_{};
Real x_max_{}; // x_min and x_max are the min and max of x_vec_
VectorContainer x_vec_{};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are these assumed to have reference semantics?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think so... Can you provide an example of how this could cause trouble?

Copy link
Collaborator

Choose a reason for hiding this comment

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

you're constructing x_vec_ by copy when you call the interpolator, so you're copying around the data unless VectorContainer has refernece semantics. That may be a feature or a bug. If VectoContainer is always a Kokkos::View, then we're fine though.

Comment on lines +101 to +102
// to avoid branchy code, do a linear search for the segment I_i
// where x_{i} <= x < x_{i+1}.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Linear search seems like a bad idea IMO. Probably better to just branch.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can replace this with a binary search.

Comment on lines 48 to 52
template <>
auto MonotoneInterpolator<PinnedArray1D<Real>>::ConstructVectorContainer(size_t size)
-> PinnedArray1D<Real> {
return PinnedArray1D<Real>("d", size);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if this can be generalized using @lroberts36 concepts so all view types could be specialized for at once.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would definitely be nice to have.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently PinnedArray1D is a typedef for SharedHostPinnedSpace in Kokkos 4, so this adds a requirement for Kokkos 4 if we have to explicitly specialize for common vector containers/views.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this should just work, but Luke should comment

template<typename VectorContainer, class = ENABLEIF(implements<kokkos_view(VectorContainer)>::value)>
auto MonotoneInterpolator<VectorContainer>::ConstructVectorContainer(size_t size) {
  return VectorContainer("d", size);
}

you'll need to include utils/concepts_lite.hpp

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that should work, but it is probably more consistent to use REQUIRES instead

template<typename VectorContainer, REQUIRES(implements<kokkos_view(VectorContainer)>::value)>
auto MonotoneInterpolator<VectorContainer>::ConstructVectorContainer(size_t size) {
  return VectorContainer("d", size);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can't get this to compile on my machine. I don't fully understand concepts, so I'm not sure why it's complaining:

/Users/benwibking/parthenon/src/utils/interpolate.hpp:54:45: error: nested name specifier 'MonotoneInterpolator<VectorContainer>::' for declaration does not refer into a class, class template or class template partial specialization
auto MonotoneInterpolator<VectorContainer>::ConstructVectorContainer(size_t size) {
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@BenWibking
Copy link
Collaborator Author

@Yurlungur Yes, planning to add tests.

src/utils/interpolate.hpp Outdated Show resolved Hide resolved
@BenWibking BenWibking changed the title WIP: piecewise cubic monotone interpolation WIP: Piecewise cubic monotone interpolation Feb 2, 2024
Copy link
Collaborator

@Yurlungur Yurlungur left a comment

Choose a reason for hiding this comment

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

Tests look good, but it would be good to also test on device if you plan to use this tool on GPUs.

private:
Real x_min_{};
Real x_max_{}; // x_min and x_max are the min and max of x_vec_
VectorContainer x_vec_{};
Copy link
Collaborator

Choose a reason for hiding this comment

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

you're constructing x_vec_ by copy when you call the interpolator, so you're copying around the data unless VectorContainer has refernece semantics. That may be a feature or a bug. If VectoContainer is always a Kokkos::View, then we're fine though.

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.

None yet

4 participants