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

Move align_ptr_for_type() from cuda.cuh to alignment.hpp #6859

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions conda/recipes/libcudf/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ test:
- test -f $PREFIX/include/cudf/detail/transform.hpp
- test -f $PREFIX/include/cudf/detail/transpose.hpp
- test -f $PREFIX/include/cudf/detail/unary.hpp
- test -f $PREFIX/include/cudf/detail/utilities/alignment.hpp
- test -f $PREFIX/include/cudf/detail/utilities/integer_utils.hpp
- test -f $PREFIX/include/cudf/detail/utilities/int_fastdiv.h
- test -f $PREFIX/include/cudf/dictionary/detail/concatenate.hpp
Expand Down
2 changes: 1 addition & 1 deletion cpp/include/cudf/column/column_device_view.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <algorithm>
#include <cudf/column/column_view.hpp>
#include <cudf/detail/utilities/cuda.cuh>
#include <cudf/detail/utilities/alignment.hpp>
#include <cudf/fixed_point/fixed_point.hpp>
#include <cudf/lists/list_view.cuh>
#include <cudf/strings/string_view.cuh>
Expand Down
46 changes: 46 additions & 0 deletions cpp/include/cudf/detail/utilities/alignment.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <memory>

namespace cudf {
namespace detail {

/**
* @brief Returns the aligned address for holding array of type T in pre-allocated memory.
*
* @tparam T The data type to align upon.
*
* @param destination pointer to pre-allocated contiguous storage to store type T.
* @return Pointer of type T, aligned to alignment of type T.
*/
template <typename T>
T* align_ptr_for_type(void* destination)
{
constexpr std::size_t bytes_needed{sizeof(T)};
constexpr std::size_t alignment{alignof(T)};

// pad the allocation for aligning the first pointer
auto padded_bytes_needed = bytes_needed + (alignment - 1);
// std::align captures last argument by reference and modifies it, but we don't want it modified
return reinterpret_cast<T*>(
std::align(alignment, bytes_needed, destination, padded_bytes_needed));
}

} // namespace detail
} // namespace cudf
18 changes: 0 additions & 18 deletions cpp/include/cudf/detail/utilities/cuda.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,5 @@ void device_single_thread(Functor functor, rmm::cuda_stream_view stream = rmm::c
single_thread_kernel<<<1, 1, 0, stream.value()>>>(functor);
}

/**
* @brief Returns the aligned address for holding array of type T in pre-allocated memory
* @param destination pointer to pre-allocated contiguous storage to store type T.
* @return Pointer of type T, aligned to alignment of type T.
*/
template <typename T>
T* align_ptr_for_type(void* destination)
{
constexpr std::size_t bytes_needed{sizeof(T)};
constexpr std::size_t alignment{alignof(T)};

// pad the allocation for aligning the first pointer
auto padded_bytes_needed = bytes_needed + (alignment - 1);
// std::align captures last argument by reference and modifies it, but we don't want it modified
return reinterpret_cast<T*>(
std::align(alignment, bytes_needed, destination, padded_bytes_needed));
}

} // namespace detail
} // namespace cudf