Skip to content

Commit d6d79d1

Browse files
authored
improve host/device accessors documentation (NVIDIA#4220)
1 parent 1a3ecb1 commit d6d79d1

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

docs/libcudacxx/extended_api/mdspan/host_device_accessor.rst

+34-30
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,35 @@ Features
7171

7272
**Memory spaces**
7373

74-
+--------------------+------------------+-------------------+
75-
| | Host memory | Device memory |
76-
+====================+==================+===================+
77-
| ``host_mdspan`` | Allowed | *Compile error* |
78-
+--------------------+------------------+-------------------+
79-
| ``device_mdspan`` | *Compile error* | Allowed |
80-
+--------------------+------------------+-------------------+
81-
| ``managed_mdspan`` | Allowed ***** | Allowed ***** |
82-
+--------------------+------------------+-------------------+
83-
84-
***** the validity of the *managed* memory space is checked at run-time in debug mode.
74+
*Host*, *device*, and *managed* ``mdspan`` can be created and "sliced" (``cuda::std::submdspan``) on any memory spaces. However, accessing to a specific memory space is restricted to the respective *accessor* type.
75+
76+
+----------------------------------+------------------+-------------------+
77+
| ``mdspan`` / memory space access | Host memory | Device memory |
78+
+==================================+==================+===================+
79+
| ``host_mdspan`` | Allowed | *Compile error* |
80+
+----------------------------------+------------------+-------------------+
81+
| ``device_mdspan`` | *Compile error* | Allowed |
82+
+----------------------------------+------------------+-------------------+
83+
| ``managed_mdspan`` | Allowed ***** | Allowed ***** |
84+
+----------------------------------+------------------+-------------------+
85+
86+
***** the validity of the *managed* memory space is checked at run-time in debug mode (host-side).
8587

8688
**Conversions**
8789

8890
+-----------------------------+------------------+-------------------+---------------------+
8991
| | ``host_mdspan`` | ``device_mdspan`` | ``managed_mdspan`` |
9092
+=============================+==================+===================+=====================+
91-
| ``host_mdspan`` | Allowed ***** | *Compile error* | *Compile error* |
93+
| ``host_mdspan`` | Allowed | *Compile error* | *Compile error* |
9294
+-----------------------------+------------------+-------------------+---------------------+
93-
| ``device_mdspan`` | *Compile error* | Allowed ***** | *Compile error* |
95+
| ``device_mdspan`` | *Compile error* | Allowed | *Compile error* |
9496
+-----------------------------+------------------+-------------------+---------------------+
95-
| ``managed_mdspan`` | Allowed | Allowed | Allowed ***** |
97+
| ``managed_mdspan`` | Allowed | Allowed | Allowed |
9698
+-----------------------------+------------------+-------------------+---------------------+
9799
| Other mdspan | Allowed | Allowed | Allowed |
98100
+-----------------------------+------------------+-------------------+---------------------+
99101

100-
***** the conversion is ``explicit`` if the base accessors are not directly convertible.
102+
*Note:* the conversion is ``explicit`` if the base accessor is not directly convertible.
101103

102104
Example 1
103105
---------
@@ -128,19 +130,20 @@ Example 1
128130
}
129131
130132
int main() {
131-
void* d_ptr;
133+
int* d_ptr;
132134
cudaMalloc(&d_ptr, 4 * sizeof(int));
133-
int h_ptr[4];
134-
cuda::host_mdspan<int, dim> h_md{h_ptr, 4};
135-
cuda::device_mdspan<int, dim> d_md{d_ptr, 4};
135+
int h_ptr[4];
136+
cuda::host_mdspan h_md{h_ptr};
137+
cuda::device_mdspan d_md{d_ptr, 4};
136138
kernel_d<<<1, 1>>>(d_md); // ok
137139
// kernel_d<<<1, 1>>>(h_md); // compile error
138140
host_function_h(h_md); // ok
139141
host_function_d(h_md); // compile error
140142
// host_function_m(h_md); // compile error
143+
cudaFree(d_ptr);
141144
}
142145
143-
`See example 1 on Godbolt 🔗 <https://godbolt.org/z/hW9faqsGW>`_
146+
`See example 1 on Godbolt 🔗 <https://godbolt.org/z/fezxsbjaq>`_
144147

145148
Example 2
146149
---------
@@ -162,17 +165,18 @@ Example 2
162165
}
163166
164167
int main() {
165-
void* m_ptr;
168+
int* m_ptr;
166169
cudaMallocManaged(&m_ptr, 4 * sizeof(int));
167-
cuda::managed_mdspan<int, dim> m_md{m_ptr, 4};
170+
cuda::managed_mdspan m_md{m_ptr, 4};
168171
kernel_d<<<1, 1>>>(m_md); // ok
169172
host_function_h(m_md); // ok
170173
171-
cuda::managed_mdspan<int, dim> m_md2{d_ptr, 4};
174+
cuda::managed_mdspan m_md2{d_ptr, 4};
172175
m_md2[0]; // run-time error
176+
cudaFree(d_ptr);
173177
}
174178
175-
`See example 2 on Godbolt 🔗 <https://godbolt.org/z/WxWfaas5h>`_
179+
`See example 2 on Godbolt 🔗 <https://godbolt.org/z/Kj39Pe4vP>`_
176180

177181

178182
Example 3
@@ -189,13 +193,13 @@ Conversion from other accessors:
189193
int main() {
190194
using cuda::std::layout_right;
191195
using cuda::std::aligned_accessor;
192-
int h_ptr[4];
193-
cuda::std::mdspan<int, dim> md{h_ptr, 4};
194-
cuda::host_mdspan<int, dim> h_md = md; // ok
196+
int h_ptr[4];
197+
cuda::std::mdspan md{h_ptr};
198+
cuda::host_mdspan h_md = md; // ok
195199
196200
cuda::std::mdspan<int, dim, layout_right, aligned_accessor<int, 8>> md_a{h_ptr, 4};
197-
// cuda::host_mdspan<int, dim> h_md = md_a; // compile-error
198-
cuda::host_mdspan<int, dim> h_md{md_a}; // ok
201+
// cuda::host_mdspan h_md = md_a; // compile-error
202+
cuda::host_mdspan h_md{md_a}; // ok
199203
}
200204
201-
`See example 3 on Godbolt 🔗 <https://godbolt.org/z/ja89roofx>`_
205+
`See example 3 on Godbolt 🔗 <https://godbolt.org/z/7dq7vcTWP>`_

0 commit comments

Comments
 (0)