resource_ref.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/detail/cccl_adaptors.hpp>
8 #include <rmm/detail/error.hpp>
9 #include <rmm/detail/export.hpp>
11 
12 namespace RMM_NAMESPACE {
13 
25  detail::cccl_resource_ref<cuda::mr::synchronous_resource_ref<cuda::mr::device_accessible>>;
26 
32  detail::cccl_async_resource_ref<cuda::mr::resource_ref<cuda::mr::device_accessible>>;
33 
39  detail::cccl_resource_ref<cuda::mr::synchronous_resource_ref<cuda::mr::host_accessible>>;
40 
46  detail::cccl_async_resource_ref<cuda::mr::resource_ref<cuda::mr::host_accessible>>;
47 
52 using host_device_resource_ref = detail::cccl_resource_ref<
53  cuda::mr::synchronous_resource_ref<cuda::mr::host_accessible, cuda::mr::device_accessible>>;
54 
59 using host_device_async_resource_ref = detail::cccl_async_resource_ref<
60  cuda::mr::resource_ref<cuda::mr::host_accessible, cuda::mr::device_accessible>>;
61 
71 template <class Resource>
73 {
74  RMM_EXPECTS(res, "Unexpected null resource pointer.");
75  return device_async_resource_ref{*res};
76 }
77 
78 // Verify that the device_memory_resource bridge constructors work correctly.
79 // These assertions validate that resource_ref types can be constructed from raw
80 // pointers/references.
81 static_assert(
82  std::is_constructible_v<device_resource_ref, mr::device_memory_resource*>,
83  "device_resource_ref must be constructible from device_memory_resource* (via bridge)");
84 static_assert(
85  std::is_constructible_v<device_async_resource_ref, mr::device_memory_resource*>,
86  "device_async_resource_ref must be constructible from device_memory_resource* (via bridge)");
87 static_assert(
88  std::is_constructible_v<device_resource_ref, mr::device_memory_resource&>,
89  "device_resource_ref must be constructible from device_memory_resource& (via bridge)");
90 static_assert(
91  std::is_constructible_v<device_async_resource_ref, mr::device_memory_resource&>,
92  "device_async_resource_ref must be constructible from device_memory_resource& (via bridge)");
93 
94 // Verify that RMM resource_ref types can be constructed from corresponding CCCL resource_ref types.
95 static_assert(
96  std::is_constructible_v<device_resource_ref,
97  cuda::mr::synchronous_resource_ref<cuda::mr::device_accessible>>,
98  "device_resource_ref must be constructible from CCCL "
99  "synchronous_resource_ref<device_accessible>");
100 static_assert(
101  std::is_constructible_v<device_async_resource_ref,
102  cuda::mr::resource_ref<cuda::mr::device_accessible>>,
103  "device_async_resource_ref must be constructible from CCCL resource_ref<device_accessible>");
104 static_assert(
105  std::is_constructible_v<host_resource_ref,
106  cuda::mr::synchronous_resource_ref<cuda::mr::host_accessible>>,
107  "host_resource_ref must be constructible from CCCL synchronous_resource_ref<host_accessible>");
108 static_assert(
109  std::is_constructible_v<host_async_resource_ref,
110  cuda::mr::resource_ref<cuda::mr::host_accessible>>,
111  "host_async_resource_ref must be constructible from CCCL resource_ref<host_accessible>");
112 static_assert(
113  std::is_constructible_v<
115  cuda::mr::synchronous_resource_ref<cuda::mr::host_accessible, cuda::mr::device_accessible>>,
116  "host_device_resource_ref must be constructible from CCCL "
117  "synchronous_resource_ref<host_accessible, device_accessible>");
118 static_assert(
119  std::is_constructible_v<
121  cuda::mr::resource_ref<cuda::mr::host_accessible, cuda::mr::device_accessible>>,
122  "host_device_async_resource_ref must be constructible from CCCL resource_ref<host_accessible, "
123  "device_accessible>");
124 
125 // Verify that host_device resource refs can be converted to device-only and host-only resource
126 // refs. This is needed because a resource that is both host and device accessible can be used in
127 // contexts that only require one or the other.
128 static_assert(
129  std::is_constructible_v<device_async_resource_ref, host_device_async_resource_ref>,
130  "device_async_resource_ref must be constructible from host_device_async_resource_ref");
131 static_assert(std::is_constructible_v<device_resource_ref, host_device_resource_ref>,
132  "device_resource_ref must be constructible from host_device_resource_ref");
133 static_assert(std::is_constructible_v<host_async_resource_ref, host_device_async_resource_ref>,
134  "host_async_resource_ref must be constructible from host_device_async_resource_ref");
135 static_assert(std::is_constructible_v<host_resource_ref, host_device_resource_ref>,
136  "host_resource_ref must be constructible from host_device_resource_ref");
137  // end of group
139 } // namespace RMM_NAMESPACE
detail::cccl_resource_ref< cuda::mr::synchronous_resource_ref< cuda::mr::device_accessible > > device_resource_ref
Alias for a cuda::mr::resource_ref with the property cuda::mr::device_accessible.
Definition: resource_ref.hpp:25
detail::cccl_resource_ref< cuda::mr::synchronous_resource_ref< cuda::mr::host_accessible, cuda::mr::device_accessible > > host_device_resource_ref
Alias for a cuda::mr::resource_ref with the properties cuda::mr::host_accessible and cuda::mr::device...
Definition: resource_ref.hpp:53
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::host_accessible, cuda::mr::device_accessible > > host_device_async_resource_ref
Alias for a cuda::mr::async_resource_ref with the properties cuda::mr::host_accessible and cuda::mr::...
Definition: resource_ref.hpp:60
detail::cccl_resource_ref< cuda::mr::synchronous_resource_ref< cuda::mr::host_accessible > > host_resource_ref
Alias for a cuda::mr::resource_ref with the property cuda::mr::host_accessible.
Definition: resource_ref.hpp:39
device_async_resource_ref to_device_async_resource_ref_checked(Resource *res)
Convert pointer to memory resource into device_async_resource_ref, checking for nullptr
Definition: resource_ref.hpp:72
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::host_accessible > > host_async_resource_ref
Alias for a cuda::mr::async_resource_ref with the property cuda::mr::host_accessible.
Definition: resource_ref.hpp:46
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::device_accessible > > device_async_resource_ref
Alias for a cuda::mr::async_resource_ref with the property cuda::mr::device_accessible.
Definition: resource_ref.hpp:32