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

Unify alignPtrTo implementation #2275

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 7 additions & 30 deletions common/src/KokkosKernels_Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1527,41 +1527,18 @@ struct array_sum_reduce {
}
};

/* Several alternatives were considered for SYCL, including
unsigned int f1(unsigned int i, unsigned int align)
{
return ((i + align - 1) / align * align);
}
unsigned int f2(unsigned int i, unsigned int align)
{
return (i + align - 1) & (-align);
}
f1 should be equivalent to the below, but it produces incorrect results on SYCL
f2 is how GCC does std::align, but it also produces incorrect results on SYCL
possibly alignof(T) is not a power-of-2 on SYCL? Or a compiler error.
*/
#if defined(KOKKOS_ENABLE_SYCL)
template <typename T, typename InPtr>
KOKKOS_INLINE_FUNCTION T *alignPtrTo(InPtr p) {
std::uintptr_t ptrVal = reinterpret_cast<std::uintptr_t>(p);
while (ptrVal % alignof(T)) {
++ptrVal;
}
return reinterpret_cast<T *>(ptrVal);
}
#else
template <typename T, typename InPtr>
KOKKOS_INLINE_FUNCTION T *alignPtrTo(InPtr p) {
KOKKOS_INLINE_FUNCTION T *alignPtrTo(InPtr *p) {
// ugly but computationally free and the "right" way to do this in C++
std::uintptr_t ptrVal = reinterpret_cast<std::uintptr_t>(p);
const std::uintptr_t ptrVal = reinterpret_cast<std::uintptr_t>(p);
// ptrVal + (align - 1) lands inside the next valid aligned scalar_t,
// and the mask produces the start of that scalar_t.
return reinterpret_cast<T *>((ptrVal + alignof(T) - 1) & (~(alignof(T) - 1)));
const std::uintptr_t ptrValNew =
(ptrVal + alignof(T) - 1) & (~(alignof(T) - 1));
return reinterpret_cast<T *>(
reinterpret_cast<char *>(const_cast<std::remove_cv_t<InPtr> *>(p)) +
(ptrValNew - ptrVal));
}
#endif

} // namespace Impl
} // namespace KokkosKernels
Expand Down
Loading