aligned.hpp
转到此文件的文档。
1 /*
2  * Copyright (c) 2020-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://apache.ac.cn/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <rmm/detail/export.hpp>
20 
21 #include <cassert>
22 #include <cstddef>
23 #include <cstdint>
24 
25 namespace RMM_NAMESPACE {
26 
37 static constexpr std::size_t RMM_DEFAULT_HOST_ALIGNMENT{alignof(std::max_align_t)};
38 
43 static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT{256};
44 
52 [[nodiscard]] constexpr bool is_pow2(std::size_t value) noexcept
53 {
54  return (value != 0U) && ((value & (value - 1)) == 0U);
55 }
56 
64 [[nodiscard]] constexpr bool is_supported_alignment(std::size_t alignment) noexcept
65 {
66  return is_pow2(alignment);
67 }
68 
77 [[nodiscard]] constexpr std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
78 {
79  assert(is_supported_alignment(alignment));
80  return (value + (alignment - 1)) & ~(alignment - 1);
81 }
82 
91 [[nodiscard]] constexpr std::size_t align_down(std::size_t value, std::size_t alignment) noexcept
92 {
93  assert(is_supported_alignment(alignment));
94  return value & ~(alignment - 1);
95 }
96 
105 [[nodiscard]] constexpr bool is_aligned(std::size_t value, std::size_t alignment) noexcept
106 {
107  assert(is_supported_alignment(alignment));
108  return value == align_down(value, alignment);
109 }
110 
119 [[nodiscard]] inline bool is_pointer_aligned(
120  void* ptr, std::size_t alignment = CUDA_ALLOCATION_ALIGNMENT) noexcept
121 {
122  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
123  return is_aligned(reinterpret_cast<std::uintptr_t>(ptr), alignment);
124 }
125  // end of group
127 
128 } // namespace RMM_NAMESPACE
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
CUDA 内存分配使用的默认对齐方式。
定义: aligned.hpp:43
constexpr bool is_aligned(std::size_t value, std::size_t alignment) noexcept
检查值是否按指定 2 的幂的倍数对齐。
定义: aligned.hpp:105
bool is_pointer_aligned(void *ptr, std::size_t alignment=CUDA_ALLOCATION_ALIGNMENT) noexcept
检查提供的指针是否按指定对齐方式对齐。
定义: aligned.hpp:119
static constexpr std::size_t RMM_DEFAULT_HOST_ALIGNMENT
RMM 分配的主机内存使用的默认对齐方式。
定义: aligned.hpp:37
constexpr bool is_supported_alignment(std::size_t alignment) noexcept
返回对齐方式是否为有效的内存对齐方式。
定义: aligned.hpp:64
constexpr std::size_t align_down(std::size_t value, std::size_t alignment) noexcept
向下对齐到指定 2 的幂的最近倍数。
定义: aligned.hpp:91
constexpr std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
向上对齐到指定 2 的幂的最近倍数。
定义: aligned.hpp:77
constexpr bool is_pow2(std::size_t value) noexcept
返回值是否为 2 的幂。
定义: aligned.hpp:52