table_device_view.cuh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-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  * http://www.apache.org/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 #pragma once
17 
19 #include <cudf/detail/utilities/cuda_memcpy.hpp>
20 #include <cudf/detail/utilities/vector_factories.hpp>
22 #include <cudf/types.hpp>
24 
25 #include <rmm/cuda_stream_view.hpp>
26 #include <rmm/device_buffer.hpp>
27 
28 #include <cassert>
29 #include <memory>
30 #include <numeric>
31 
37 namespace CUDF_EXPORT cudf {
38 namespace detail {
39 
56 template <typename ColumnDeviceView, typename HostTableView>
58  public:
59  table_device_view_base() = delete;
60  ~table_device_view_base() = default;
75 
81  __device__ ColumnDeviceView* begin() const noexcept { return _columns; }
82 
91  __device__ ColumnDeviceView* end() const noexcept { return _columns + _num_columns; }
92 
99  __device__ ColumnDeviceView const& column(size_type column_index) const noexcept
100  {
101  assert(column_index >= 0);
102  assert(column_index < _num_columns);
103  return _columns[column_index];
104  }
105 
112  __device__ ColumnDeviceView& column(size_type column_index) noexcept
113  {
114  assert(column_index >= 0);
115  assert(column_index < _num_columns);
116  return _columns[column_index];
117  }
118 
124  [[nodiscard]] __host__ __device__ size_type num_columns() const noexcept { return _num_columns; }
125 
131  [[nodiscard]] __host__ __device__ size_type num_rows() const noexcept { return _num_rows; }
132 
139  void destroy();
140 
141  private:
142  ColumnDeviceView* _columns{};
143  size_type _num_rows{};
144  size_type _num_columns{};
145 
146  protected:
153  table_device_view_base(HostTableView source_view, rmm::cuda_stream_view stream);
154 
156  rmm::device_buffer* _descendant_storage{};
157 };
158 } // namespace detail
159 
163 class table_device_view : public detail::table_device_view_base<column_device_view, table_view> {
164  public:
179  static auto create(table_view source_view,
181  {
182  auto deleter = [](table_device_view* t) { t->destroy(); };
183  return std::unique_ptr<table_device_view, decltype(deleter)>{
184  new table_device_view(source_view, stream), deleter};
185  }
186 
187  private:
189  : detail::table_device_view_base<column_device_view, table_view>(source_view, stream)
190  {
191  }
192 };
193 
200  : public detail::table_device_view_base<mutable_column_device_view, mutable_table_view> {
201  public:
216  static auto create(mutable_table_view source_view,
218  {
219  auto deleter = [](mutable_table_device_view* t) { t->destroy(); };
220  return std::unique_ptr<mutable_table_device_view, decltype(deleter)>{
221  new mutable_table_device_view(source_view, stream), deleter};
222  }
223 
224  private:
226  : detail::table_device_view_base<mutable_column_device_view, mutable_table_view>(source_view,
227  stream)
228  {
229  }
230 };
231 
241 template <typename ColumnDeviceView, typename HostTableView>
242 auto contiguous_copy_column_device_views(HostTableView source_view, rmm::cuda_stream_view stream)
243 {
244  // First calculate the size of memory needed to hold the
245  // table's ColumnDeviceViews. This is done by calling extent()
246  // for each of the table's ColumnViews columns.
247  std::size_t views_size_bytes = std::accumulate(
248  source_view.begin(), source_view.end(), std::size_t{0}, [](std::size_t init, auto col) {
249  return init + ColumnDeviceView::extent(col);
250  });
251  // pad the allocation for aligning the first pointer
252  auto padded_views_size_bytes = views_size_bytes + std::size_t{alignof(ColumnDeviceView) - 1};
253  // A buffer of CPU memory is allocated to hold the ColumnDeviceView
254  // objects. Once filled, the CPU memory is then copied to device memory
255  // and the pointer is set in the d_columns member.
256  auto h_buffer = cudf::detail::make_host_vector<int8_t>(padded_views_size_bytes, stream);
257  // Each ColumnDeviceView instance may have child objects which may
258  // require setting some internal device pointers before being copied
259  // from CPU to device.
260  // Allocate the device memory to be used in the result.
261  // We need this pointer in order to pass it down when creating the
262  // ColumnDeviceViews so the column can set the pointer(s) for any
263  // of its child objects.
264  // align both h_ptr, d_ptr
265  auto descendant_storage = std::make_unique<rmm::device_buffer>(padded_views_size_bytes, stream);
266  void* h_ptr = detail::align_ptr_for_type<ColumnDeviceView>(h_buffer.data());
267  void* d_ptr = detail::align_ptr_for_type<ColumnDeviceView>(descendant_storage->data());
268  auto d_columns = detail::child_columns_to_device_array<ColumnDeviceView>(
269  source_view.begin(), source_view.end(), h_ptr, d_ptr);
270 
271  auto const h_span = host_span<int8_t const>{h_buffer}.subspan(
272  static_cast<int8_t const*>(h_ptr) - h_buffer.data(), views_size_bytes);
273  auto const d_span = device_span<int8_t>{static_cast<int8_t*>(d_ptr), views_size_bytes};
274  cudf::detail::cuda_memcpy(d_span, h_span, stream);
275  return std::make_tuple(std::move(descendant_storage), d_columns);
276 }
277 
278 } // namespace CUDF_EXPORT cudf
An immutable, non-owning view of device data as a column of elements that is trivially copyable and u...
Base class for a device table of ColumnDeviceViews.
size_type num_rows() const noexcept
Returns the number of rows.
ColumnDeviceView const & column(size_type column_index) const noexcept
Returns a reference to the view of the specified column.
table_device_view_base(table_device_view_base &&)=default
Move constructor.
ColumnDeviceView * begin() const noexcept
Returns an iterator to the first view in the table.
void destroy()
Destroy the table_device_view object.
table_device_view_base(table_device_view_base const &)=default
Copy constructor.
size_type num_columns() const noexcept
Returns the number of columns.
table_device_view_base & operator=(table_device_view_base const &)=default
Copy assignment operator.
ColumnDeviceView * end() const noexcept
Returns an iterator one past the last column view in the table.
table_device_view_base(HostTableView source_view, rmm::cuda_stream_view stream)
Construct a new table device view base object from host table_view.
table_device_view_base & operator=(table_device_view_base &&)=default
Move assignment operator.
ColumnDeviceView & column(size_type column_index) noexcept
Returns a reference to the view of the specified column.
A mutable, non-owning view of device data as a column of elements that is trivially copyable and usab...
Mutable table device view that is usable in device memory.
static auto create(mutable_table_view source_view, rmm::cuda_stream_view stream=cudf::get_default_stream())
Factory to construct a mutable table device view that is usable in device memory.
A set of mutable_column_views of the same size.
Definition: table_view.hpp:268
Table device view that is usable in device memory.
static auto create(table_view source_view, rmm::cuda_stream_view stream=cudf::get_default_stream())
Factory to construct a table device view that is usable in device memory.
A set of cudf::column_view's of the same size.
Definition: table_view.hpp:200
Column device view class definitions.
rmm::cuda_stream_view const get_default_stream()
Get the current default stream.
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:95
cuDF interfaces
Definition: host_udf.hpp:39
auto contiguous_copy_column_device_views(HostTableView source_view, rmm::cuda_stream_view stream)
Copies the contents of a table_view to a column device view in contiguous device memory.
Device version of C++20 std::span with reduced feature set.
Definition: span.hpp:346
C++20 std::span with reduced feature set.
Definition: span.hpp:194
constexpr host_span subspan(typename base::size_type offset, typename base::size_type count) const noexcept
Obtains a span that is a view over the count elements of this span starting at offset.
Definition: span.hpp:314
Class definitions for (mutable)_table_view
Type declarations for libcudf.