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

std::align for pointers #937

Open
neoblizz opened this issue Feb 1, 2022 · 0 comments
Open

std::align for pointers #937

neoblizz opened this issue Feb 1, 2022 · 0 comments
Labels
🐙 c++ C++ related issues. 🍃 cuda & nvcc CUDA/NVCC related issues.

Comments

@neoblizz
Copy link
Member

neoblizz commented Feb 1, 2022

Code from RXMesh: https://github.com/owensgroup/RXMesh/blob/memcpy_async/include/rxmesh/util/util.h#L390-L411

/**
 * @brief given a pointer, this function returns a pointer to the first location
 * at the boundary of a given alignment size. This what std:align does but it
 * does not work with CUDA so this a stripped down version of it.
 * @tparam T type of the pointer
 * @param byte_alignment number of bytes to get the pointer to be aligned to
 * @param ptr input/output pointer pointing at first usable location. On return,
 * it will be properly aligned to the beginning of the first element that is
 * aligned to alignment
 */
template <typename T>
__device__ __host__ __inline__ void align(const std::size_t byte_alignment,
                                          T*&               ptr) noexcept
{
    const uint64_t intptr    = reinterpret_cast<uint64_t>(ptr);
    const uint64_t remainder = intptr % byte_alignment;
    if (remainder == 0) {
        return;
    }
    const uint64_t aligned = intptr + byte_alignment - remainder;
    ptr                    = reinterpret_cast<T*>(aligned);
}

https://nvidia.github.io/libcudacxx/extended_api/shapes/aligned_size_t.html
https://stackoverflow.com/questions/70765553/cuda-shared-memory-alignement-in-documentation

@neoblizz neoblizz added 🍃 cuda & nvcc CUDA/NVCC related issues. 🐙 c++ C++ related issues. labels Feb 1, 2022
@neoblizz neoblizz changed the title std::align for pointers std::align for pointers Feb 1, 2022
@neoblizz neoblizz transferred this issue from gunrock/essentials Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐙 c++ C++ related issues. 🍃 cuda & nvcc CUDA/NVCC related issues.
Projects
None yet
Development

No branches or pull requests

1 participant