Skip to content

Commit

Permalink
Unify alignPtrTo implementation (#2275)
Browse files Browse the repository at this point in the history
  • Loading branch information
masterleinad authored Jul 12, 2024
1 parent aaa634b commit e7a4b07
Showing 1 changed file with 7 additions and 30 deletions.
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

0 comments on commit e7a4b07

Please sign in to comment.