|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | + |
| 17 | +#include <cudf/detail/nvtx/ranges.hpp> |
| 18 | +#include <cudf/detail/reshape.hpp> |
| 19 | +#include <cudf/detail/utilities/batched_memcpy.hpp> |
| 20 | +#include <cudf/detail/utilities/vector_factories.hpp> |
| 21 | +#include <cudf/reshape.hpp> |
| 22 | +#include <cudf/types.hpp> |
| 23 | +#include <cudf/utilities/default_stream.hpp> |
| 24 | +#include <cudf/utilities/error.hpp> |
| 25 | +#include <cudf/utilities/span.hpp> |
| 26 | +#include <cudf/utilities/type_checks.hpp> |
| 27 | +#include <cudf/utilities/type_dispatcher.hpp> |
| 28 | + |
| 29 | +#include <rmm/cuda_stream_view.hpp> |
| 30 | +#include <rmm/device_uvector.hpp> |
| 31 | + |
| 32 | +#include <cub/device/device_memcpy.cuh> |
| 33 | +#include <cuda/functional> |
| 34 | +#include <cuda_runtime.h> |
| 35 | +#include <thrust/device_vector.h> |
| 36 | +#include <thrust/iterator/constant_iterator.h> |
| 37 | +#include <thrust/iterator/counting_iterator.h> |
| 38 | +#include <thrust/iterator/transform_iterator.h> |
| 39 | + |
| 40 | +namespace cudf { |
| 41 | +namespace detail { |
| 42 | +namespace { |
| 43 | + |
| 44 | +template <typename T> |
| 45 | +void table_to_array_impl(table_view const& input, |
| 46 | + device_span<cuda::std::byte> output, |
| 47 | + rmm::cuda_stream_view stream) |
| 48 | +{ |
| 49 | + auto const num_columns = input.num_columns(); |
| 50 | + auto const num_rows = input.num_rows(); |
| 51 | + auto const item_size = sizeof(T); |
| 52 | + auto const total_bytes = static_cast<size_t>(num_columns) * num_rows * item_size; |
| 53 | + |
| 54 | + CUDF_EXPECTS(output.size() >= total_bytes, "Output span is too small", std::invalid_argument); |
| 55 | + CUDF_EXPECTS(cudf::all_have_same_types(input.begin(), input.end()), |
| 56 | + "All columns must have the same data type", |
| 57 | + cudf::data_type_error); |
| 58 | + CUDF_EXPECTS(!cudf::has_nulls(input), "All columns must contain no nulls", std::invalid_argument); |
| 59 | + |
| 60 | + auto* base_ptr = output.data(); |
| 61 | + |
| 62 | + auto h_srcs = make_host_vector<T const*>(num_columns, stream); |
| 63 | + auto h_dsts = make_host_vector<T*>(num_columns, stream); |
| 64 | + |
| 65 | + std::transform(input.begin(), input.end(), h_srcs.begin(), [](auto& col) { |
| 66 | + return const_cast<T*>(col.template data<T>()); |
| 67 | + }); |
| 68 | + |
| 69 | + for (int i = 0; i < num_columns; ++i) { |
| 70 | + h_dsts[i] = reinterpret_cast<T*>(base_ptr + i * item_size * num_rows); |
| 71 | + } |
| 72 | + |
| 73 | + auto const mr = cudf::get_current_device_resource_ref(); |
| 74 | + |
| 75 | + auto d_srcs = cudf::detail::make_device_uvector_async(h_srcs, stream, mr); |
| 76 | + auto d_dsts = cudf::detail::make_device_uvector_async(h_dsts, stream, mr); |
| 77 | + |
| 78 | + thrust::constant_iterator<size_t> sizes(static_cast<size_t>(item_size * num_rows)); |
| 79 | + |
| 80 | + cudf::detail::batched_memcpy_async( |
| 81 | + d_srcs.begin(), d_dsts.begin(), sizes, num_columns, stream.value()); |
| 82 | +} |
| 83 | + |
| 84 | +struct table_to_array_dispatcher { |
| 85 | + table_view const& input; |
| 86 | + device_span<cuda::std::byte> output; |
| 87 | + rmm::cuda_stream_view stream; |
| 88 | + |
| 89 | + template <typename T, CUDF_ENABLE_IF(is_fixed_width<T>())> |
| 90 | + void operator()() const |
| 91 | + { |
| 92 | + table_to_array_impl<T>(input, output, stream); |
| 93 | + } |
| 94 | + |
| 95 | + template <typename T, CUDF_ENABLE_IF(!is_fixed_width<T>())> |
| 96 | + void operator()() const |
| 97 | + { |
| 98 | + CUDF_FAIL("Unsupported dtype"); |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +} // namespace |
| 103 | + |
| 104 | +void table_to_array(table_view const& input, |
| 105 | + device_span<cuda::std::byte> output, |
| 106 | + rmm::cuda_stream_view stream) |
| 107 | +{ |
| 108 | + if (input.num_columns() == 0) return; |
| 109 | + |
| 110 | + auto const dtype = input.column(0).type(); |
| 111 | + |
| 112 | + cudf::type_dispatcher<cudf::dispatch_storage_type>( |
| 113 | + dtype, table_to_array_dispatcher{input, output, stream}); |
| 114 | +} |
| 115 | + |
| 116 | +} // namespace detail |
| 117 | + |
| 118 | +void table_to_array(table_view const& input, |
| 119 | + device_span<cuda::std::byte> output, |
| 120 | + rmm::cuda_stream_view stream) |
| 121 | +{ |
| 122 | + CUDF_FUNC_RANGE(); |
| 123 | + cudf::detail::table_to_array(input, output, stream); |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace cudf |
0 commit comments