Skip to content
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
36 changes: 24 additions & 12 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,43 @@ include(cmake/thirdparty/get_nvtx.cmake)
# ##################################################################################################
# * library targets --------------------------------------------------------------------------------

add_library(rmm INTERFACE)
add_library(rmm SHARED src/aligned.cpp)
add_library(rmm::rmm ALIAS rmm)

target_include_directories(
rmm
INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>" "$<INSTALL_INTERFACE:include>")
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
INTERFACE "$<INSTALL_INTERFACE:include>")

if(CUDA_STATIC_RUNTIME)
Comment thread
robertmaynard marked this conversation as resolved.
Resolved
message(STATUS "RMM: Enabling static linking of cudart")
target_link_libraries(rmm INTERFACE CUDA::cudart_static)
target_link_libraries(rmm PUBLIC CUDA::cudart_static)
else()
target_link_libraries(rmm INTERFACE CUDA::cudart)
target_link_libraries(rmm PUBLIC CUDA::cudart)
endif()

target_link_libraries(rmm INTERFACE CCCL::CCCL)
target_link_libraries(rmm INTERFACE dl)
target_link_libraries(rmm INTERFACE nvtx3::nvtx3-cpp)
target_link_libraries(rmm INTERFACE rapids_logger::rapids_logger)
target_compile_features(rmm INTERFACE cxx_std_17 $<BUILD_INTERFACE:cuda_std_17>)
target_compile_definitions(rmm INTERFACE LIBCUDACXX_ENABLE_EXPERIMENTAL_MEMORY_RESOURCE)
target_link_libraries(rmm PUBLIC CCCL::CCCL ${CMAKE_DL_LIBS} nvtx3::nvtx3-cpp
rapids_logger::rapids_logger)

set_target_properties(
rmm
PROPERTIES BUILD_RPATH "\$ORIGIN"
INSTALL_RPATH "\$ORIGIN"
# set target compile options
Comment thread
bdice marked this conversation as resolved.
Resolved
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CXX_VISIBILITY_PRESET hidden
CUDA_STANDARD 17
Comment thread
bdice marked this conversation as resolved.
Resolved
CUDA_STANDARD_REQUIRED ON
CUDA_VISIBILITY_PRESET hidden
POSITION_INDEPENDENT_CODE ON
INTERFACE_POSITION_INDEPENDENT_CODE ON)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may be able to drop this eventually depending on how much of rmm stops being header-only. Ideally we wouldn't be proscribing this for consumers I think, @robertmaynard any thoughts?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally once RMM is a proper library we can drop INTERFACE_POSITION_INDEPENDENT_CODE but currently having it won't hurt downstream projects

target_compile_definitions(rmm PUBLIC LIBCUDACXX_ENABLE_EXPERIMENTAL_MEMORY_RESOURCE)

# Enable NVTX if necessary
if(RMM_NVTX)
target_compile_definitions(rmm INTERFACE RMM_NVTX)
target_compile_definitions(rmm PUBLIC RMM_NVTX)
endif()

# ##################################################################################################
Expand Down
38 changes: 8 additions & 30 deletions cpp/include/rmm/aligned.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2024, NVIDIA CORPORATION.
* Copyright (c) 2020-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,10 +49,7 @@ static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT{256};
*
* @return True if the input is a power of two with non-negative integer exponent, false otherwise.
*/
[[nodiscard]] constexpr bool is_pow2(std::size_t value) noexcept
{
return (value != 0U) && ((value & (value - 1)) == 0U);
}
[[nodiscard]] RMM_EXPORT bool is_pow2(std::size_t value) noexcept;

/**
* @brief Returns whether or not `alignment` is a valid memory alignment.
Expand All @@ -61,10 +58,7 @@ static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT{256};
*
* @return True if the alignment is valid, false otherwise.
*/
[[nodiscard]] constexpr bool is_supported_alignment(std::size_t alignment) noexcept
{
return is_pow2(alignment);
}
[[nodiscard]] RMM_EXPORT bool is_supported_alignment(std::size_t alignment) noexcept;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I believe RMM_NAMESPACE already contains the RMM_EXPORT macro, but it's not bad to be explicit. If we wanted to, we could then go back to just saying namespace rmm in this file.

We should, however, probably add RMM_EXPORT to the static symbols as well in that case.

@bdice bdice Apr 25, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation! I was unsure about this. I will think some more about what solution seems the most natural. Related to the other comment below, I would like it if we could use either RMM_NAMESPACE or namespace rmm in both headers and implementation files -- but that is tricky with the need to export things.

What do you think of this proposal:

  1. get rid of RMM_NAMESPACE
  2. don't put RMM_EXPORT on every symbol, just the namespaces as we currently do
  3. use namespace RMM_EXPORT rmm in the headers and namespace rmm in the implementation files

@bdice bdice Apr 25, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the topic of namespacing, I think there will also be a lot of detail functions that we can move from headers to implementation files in anonymous namespaces. (Currently we can't use anonymous namespaces because it's header-only and that's not good practice.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrt the proposal, that sounds reasonable. Ideally we move to a situation where everything in the detail namespace is __attribute__((visibility("hidden"))), similar to cudf last year:

I suppose your anonymous namespace suggestion would have the same effect.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for sure. That will likely be for a later pass. I haven't touched any "detail" headers yet.


/**
* @brief Align up to nearest multiple of specified power of 2
Expand All @@ -74,11 +68,7 @@ static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT{256};
*
* @return the aligned value
*/
[[nodiscard]] constexpr std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
{
assert(is_supported_alignment(alignment));
return (value + (alignment - 1)) & ~(alignment - 1);
}
[[nodiscard]] RMM_EXPORT std::size_t align_up(std::size_t value, std::size_t alignment) noexcept;

/**
* @brief Align down to the nearest multiple of specified power of 2
Expand All @@ -88,11 +78,7 @@ static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT{256};
*
* @return the aligned value
*/
[[nodiscard]] constexpr std::size_t align_down(std::size_t value, std::size_t alignment) noexcept
{
assert(is_supported_alignment(alignment));
return value & ~(alignment - 1);
}
[[nodiscard]] RMM_EXPORT std::size_t align_down(std::size_t value, std::size_t alignment) noexcept;

/**
* @brief Checks whether a value is aligned to a multiple of a specified power of 2
Expand All @@ -102,11 +88,7 @@ static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT{256};
*
* @return true if aligned
*/
[[nodiscard]] constexpr bool is_aligned(std::size_t value, std::size_t alignment) noexcept
{
assert(is_supported_alignment(alignment));
return value == align_down(value, alignment);
}
[[nodiscard]] RMM_EXPORT bool is_aligned(std::size_t value, std::size_t alignment) noexcept;

/**
* @brief Checks whether the provided pointer is aligned to a specified @p alignment
Expand All @@ -116,12 +98,8 @@ static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT{256};
*
* @return true if the pointer is aligned
*/
[[nodiscard]] inline bool is_pointer_aligned(
void* ptr, std::size_t alignment = CUDA_ALLOCATION_ALIGNMENT) noexcept
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return is_aligned(reinterpret_cast<std::uintptr_t>(ptr), alignment);
}
[[nodiscard]] RMM_EXPORT bool is_pointer_aligned(
void* ptr, std::size_t alignment = CUDA_ALLOCATION_ALIGNMENT) noexcept;

/** @} */ // end of group

Expand Down
53 changes: 53 additions & 0 deletions cpp/src/aligned.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2020-2025, 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.
*/

#include <rmm/aligned.hpp>

#include <cassert>
#include <cstddef>
#include <cstdint>

namespace RMM_NAMESPACE {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe here we should be using namespace rmm since it is the header file which defines our public exported API.


bool is_pow2(std::size_t value) noexcept { return (value != 0U) && ((value & (value - 1)) == 0U); }

bool is_supported_alignment(std::size_t alignment) noexcept { return is_pow2(alignment); }

std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
{
assert(is_supported_alignment(alignment));
return (value + (alignment - 1)) & ~(alignment - 1);
}

std::size_t align_down(std::size_t value, std::size_t alignment) noexcept
{
assert(is_supported_alignment(alignment));
return value & ~(alignment - 1);
}

bool is_aligned(std::size_t value, std::size_t alignment) noexcept
{
assert(is_supported_alignment(alignment));
return value == align_down(value, alignment);
}

bool is_pointer_aligned(void* ptr, std::size_t alignment) noexcept
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return is_aligned(reinterpret_cast<std::uintptr_t>(ptr), alignment);
}

} // namespace RMM_NAMESPACE