Skip to content

Commit

Permalink
[DO NOT MERGE] PoC for the new design of cuda::mr::memory_resource
Browse files Browse the repository at this point in the history
  • Loading branch information
miscco committed Sep 21, 2022
1 parent d212232 commit 0b91187
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 2 deletions.
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ rapids_find_package(
rapids_cpm_init()
include(cmake/thirdparty/get_spdlog.cmake)
include(cmake/thirdparty/get_thrust.cmake)
include(cmake/thirdparty/get_libcudacxx.cmake)

# library targets
add_library(rmm INTERFACE)
add_library(rmm::rmm ALIAS rmm)

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

if(CUDA_STATIC_RUNTIME)
message(STATUS "RMM: Enabling static linking of cudart")
Expand Down Expand Up @@ -109,6 +112,10 @@ include(CPack)
# install export targets
install(TARGETS rmm EXPORT rmm-exports)
install(DIRECTORY include/rmm/ DESTINATION include/rmm)
install(
DIRECTORY ${RMM_GENERATED_INCLUDE_DIR}/include/libcxx
${RMM_GENERATED_INCLUDE_DIR}/include/libcudacxx
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/rmm)
install(FILES ${RMM_BINARY_DIR}/include/rmm/version_config.hpp DESTINATION include/rmm)

set(doc_string
Expand Down
33 changes: 33 additions & 0 deletions cmake/thirdparty/get_libcudacxx.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# =============================================================================
# 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.
# =============================================================================

# Use CPM to find or clone libcudacxx
function(find_and_configure_libcudacxx VERSION)
rapids_cpm_find(
libcudacxx ${VERSION}
GIT_REPOSITORY https://github.com/miscco/libcudacxx.git
GIT_TAG memory_resource # ${VERSION}
GIT_SHALLOW TRUE DOWNLOAD_ONLY TRUE)

set(LIBCUDACXX_INCLUDE_DIR
"${libcudacxx_SOURCE_DIR}/include"
PARENT_SCOPE)
set(LIBCXX_INCLUDE_DIR
"${libcudacxx_SOURCE_DIR}/libcxx/include"
PARENT_SCOPE)
endfunction()

set(RMM_MIN_VERSION_libcudacxx 1.5.0)

find_and_configure_libcudacxx(${RMM_MIN_VERSION_libcudacxx})
6 changes: 6 additions & 0 deletions include/rmm/cuda_stream_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cuda/stream_ref>

namespace rmm {

Expand Down Expand Up @@ -60,6 +61,11 @@ class cuda_stream_view {
* @brief Implicit conversion to cudaStream_t.
*/
constexpr operator cudaStream_t() const noexcept { return value(); }

/**
* @brief Implicit conversion to stream_ref.
*/
operator cuda::stream_ref() const noexcept { return value(); }

/**
* @brief Return true if the wrapped stream is the CUDA per-thread default stream.
Expand Down
44 changes: 44 additions & 0 deletions include/rmm/mr/device/device_memory_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <cstddef>
#include <utility>

#include <cuda/memory_resource>

namespace rmm::mr {

/**
Expand Down Expand Up @@ -148,6 +150,42 @@ class device_memory_resource {
{
return do_is_equal(other);
}

/**
* @brief Compare this resource to another.
*
* Two device_memory_resources compare equal if and only if memory allocated
* from one device_memory_resource can be deallocated from the other and vice
* versa.
*
* By default, simply checks if \p *this and \p other refer to the same
* object, i.e., does not check if they are two objects of the same class.
*
* @param other The other resource to compare to
* @returns If the two resources are equivalent
*/
[[nodiscard]] bool operator==(device_memory_resource const& other) const noexcept
{
return do_is_equal(other);
}

/**
* @brief Compare this resource to another.
*
* Two device_memory_resources compare equal if and only if memory allocated
* from one device_memory_resource can be deallocated from the other and vice
* versa.
*
* By default, simply checks if \p *this and \p other do not refer to the same
* object, i.e., does not check if they are two objects of the same class.
*
* @param other The other resource to compare to
* @returns If the two resources are equivalent
*/
[[nodiscard]] bool operator!=(device_memory_resource const& other) const noexcept
{
return !do_is_equal(other);
}

/**
* @brief Query whether the resource supports use of non-null CUDA streams for
Expand Down Expand Up @@ -176,6 +214,12 @@ class device_memory_resource {
{
return do_get_mem_info(stream);
}

/**
* @brief Signal that this resource allocates device accessible memory.
*/
friend void get_property(device_memory_resource const&, cuda::mr::device_accessible) noexcept
{}

private:
// All allocations are padded to a multiple of allocation_size_alignment bytes.
Expand Down
45 changes: 45 additions & 0 deletions include/rmm/mr/host/host_memory_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <cstddef>
#include <utility>

#include <cuda/memory_resource>

namespace rmm::mr {

/**
Expand Down Expand Up @@ -107,6 +109,49 @@ class host_memory_resource {
{
return do_is_equal(other);
}


/**
* @brief Compare this resource to another.
*
* Two host_memory_resource compare equal if and only if memory allocated
* from one device_memory_resource can be deallocated from the other and vice
* versa.
*
* By default, simply checks if \p *this and \p other refer to the same
* object, i.e., does not check if they are two objects of the same class.
*
* @param other The other resource to compare to
* @returns If the two resources are equivalent
*/
[[nodiscard]] bool operator==(host_memory_resource const& other) const noexcept
{
return do_is_equal(other);
}

/**
* @brief Compare this resource to another.
*
* Two host_memory_resource compare equal if and only if memory allocated
* from one device_memory_resource can be deallocated from the other and vice
* versa.
*
* By default, simply checks if \p *this and \p other do not refer to the same
* object, i.e., does not check if they are two objects of the same class.
*
* @param other The other resource to compare to
* @returns If the two resources are equivalent
*/
[[nodiscard]] bool operator!=(host_memory_resource const& other) const noexcept
{
return !do_is_equal(other);
}

/**
* @brief Signal that this resource allocates host accessible memory.
*/
friend void get_property(host_memory_resource const&, cuda::mr::host_accessible) noexcept
{}

private:
/**
Expand Down
9 changes: 9 additions & 0 deletions tests/mr/device/adaptor_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ template class rmm::mr::tracking_resource_adaptor<cuda_mr>;

namespace rmm::test {

static_assert(cuda::mr::resource_with<rmm::mr::aligned_resource_adaptor<cuda_mr>, cuda::mr::device_accessible>, "");
static_assert(cuda::mr::resource_with<rmm::mr::failure_callback_resource_adaptor<cuda_mr>, cuda::mr::device_accessible>, "");
static_assert(cuda::mr::resource_with<rmm::mr::limiting_resource_adaptor<cuda_mr>, cuda::mr::device_accessible>, "");
static_assert(cuda::mr::resource_with<rmm::mr::logging_resource_adaptor<cuda_mr>, cuda::mr::device_accessible>, "");
static_assert(cuda::mr::resource_with<rmm::mr::owning_wrapper<cuda_mr>, cuda::mr::device_accessible>, "");
static_assert(cuda::mr::resource_with<rmm::mr::statistics_resource_adaptor<cuda_mr>, cuda::mr::device_accessible>, "");
static_assert(cuda::mr::resource_with<rmm::mr::thread_safe_resource_adaptor<cuda_mr>, cuda::mr::device_accessible>, "");
static_assert(cuda::mr::resource_with<rmm::mr::tracking_resource_adaptor<cuda_mr>, cuda::mr::device_accessible>, "");

using adaptors = ::testing::Types<aligned_resource_adaptor<cuda_mr>,
failure_callback_resource_adaptor<cuda_mr>,
limiting_resource_adaptor<cuda_mr>,
Expand Down
3 changes: 3 additions & 0 deletions tests/mr/host/mr_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ struct MRTest : public ::testing::Test {
MRTest() : mr{new MemoryResourceType} {}
};

static_assert(cuda::mr::resource_with<rmm::mr::new_delete_resource, cuda::mr::host_accessible>, "");
static_assert(cuda::mr::resource_with<rmm::mr::pinned_memory_resource, cuda::mr::host_accessible>, "");

using resources = ::testing::Types<rmm::mr::new_delete_resource, rmm::mr::pinned_memory_resource>;

TYPED_TEST_CASE(MRTest, resources);
Expand Down

0 comments on commit 0b91187

Please sign in to comment.