-
Notifications
You must be signed in to change notification settings - Fork 1k
Add a public API for copying a table_view to device array #18450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rapids-bot
merged 27 commits into
rapidsai:branch-25.06
from
Matt711:fea/cpp/table-to-device-array
May 7, 2025
Merged
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
e201564
Add a public API for converting a table_view to device array
Matt711 6e9289e
support decimals and add more tests
Matt711 21a9201
Merge branch 'branch-25.06' into fea/cpp/table-to-device-array
Matt711 7eb3690
fallback if cuda version < 12.8
Matt711 854cbbf
Merge branch 'branch-25.06' into fea/cpp/table-to-device-array
Matt711 5a9d195
clean up
Matt711 e5e65cc
address reviews
Matt711 0343bb3
address review
Matt711 7530ecf
use snake case
Matt711 c7ab103
Merge branch 'branch-25.06' into fea/cpp/table-to-device-array
Matt711 eec23b5
Merge branch 'branch-25.06' into fea/cpp/table-to-device-array
Matt711 f027616
address reviews
Matt711 b3251fe
clean up
Matt711 f5a0096
Merge branch 'branch-25.06' into fea/cpp/table-to-device-array
Matt711 96ef619
address review
Matt711 82c5b22
pass a device_span instead of a raw pointer
Matt711 4399938
sort file names
Matt711 737fa22
Merge branch 'branch-25.06' into fea/cpp/table-to-device-array
Matt711 36abd46
Merge branch 'branch-25.06' into fea/cpp/table-to-device-array
Matt711 1fb9563
Merge branch 'branch-25.06' into fea/cpp/table-to-device-array
Matt711 c6785c7
add other impl for benchmarking purposes
Matt711 04c837f
clean up
Matt711 1530b5f
Merge branch 'branch-25.06' into fea/cpp/table-to-device-array
Matt711 fd0506b
address reviews
Matt711 db78162
address review
Matt711 f5bf21c
Merge branch 'branch-25.06' into fea/cpp/table-to-device-array
Matt711 b71ec3a
address review
Matt711 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Matt711 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /* | ||
| * Copyright (c) 2025, 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. | ||
| */ | ||
|
|
||
| #include <cudf/detail/nvtx/ranges.hpp> | ||
| #include <cudf/reshape.hpp> | ||
| #include <cudf/utilities/default_stream.hpp> | ||
| #include <cudf/utilities/error.hpp> | ||
| #include <cudf/utilities/type_dispatcher.hpp> | ||
|
|
||
| #include <rmm/cuda_stream_view.hpp> | ||
| #include <rmm/device_uvector.hpp> | ||
|
|
||
| #include <cub/device/device_memcpy.cuh> | ||
| #include <cuda_runtime.h> | ||
| #include <thrust/device_vector.h> | ||
| #include <thrust/iterator/counting_iterator.h> | ||
| #include <thrust/iterator/transform_iterator.h> | ||
|
|
||
| namespace cudf { | ||
| namespace { | ||
|
|
||
| // template <typename T> | ||
| // void _table_to_device_array(cudf::table_view const& input, | ||
| // void* output, | ||
| // rmm::cuda_stream_view stream) | ||
| // { | ||
| // auto const num_columns = input.num_columns(); | ||
| // auto const num_rows = input.num_rows(); | ||
| // auto const item_size = sizeof(T); | ||
|
|
||
| // std::vector<void*> dsts(num_columns); | ||
| // std::vector<void const*> srcs(num_columns); | ||
| // std::vector<size_t> sizes(num_columns, item_size * num_rows); | ||
|
|
||
| // auto* base_ptr = static_cast<uint8_t*>(output); | ||
|
|
||
| // for (int i = 0; i < num_columns; ++i) { | ||
| // auto const& col = input.column(i); | ||
| // CUDF_EXPECTS(col.type() == input.column(0).type(), "All columns must have the same dtype"); | ||
|
|
||
| // auto* src_ptr = static_cast<void const*>(col.data<T>()); | ||
| // auto* dst_ptr = base_ptr + i * item_size * num_rows; | ||
|
|
||
| // srcs[i] = src_ptr; | ||
| // dsts[i] = dst_ptr; | ||
| // } | ||
|
|
||
| // cudaMemcpyAttributes attr{}; | ||
| // attr.srcAccessOrder = cudaMemcpySrcAccessOrderStream; | ||
| // std::vector<cudaMemcpyAttributes> attrs{attr}; | ||
| // std::vector<size_t> attr_idxs{0}; | ||
| // size_t fail_idx = SIZE_MAX; | ||
|
|
||
| // CUDF_CUDA_TRY(cudaMemcpyBatchAsync(dsts.data(), | ||
| // const_cast<void**>(srcs.data()), | ||
| // sizes.data(), | ||
| // num_columns, | ||
| // attrs.data(), | ||
| // attr_idxs.data(), | ||
| // attrs.size(), | ||
| // &fail_idx, | ||
| // stream.value())); | ||
| // } | ||
|
|
||
| template <typename T> | ||
| void _table_to_device_array(cudf::table_view const& input, | ||
| void* output, | ||
| rmm::cuda_stream_view stream) | ||
| { | ||
| auto const num_columns = input.num_columns(); | ||
| auto const num_rows = input.num_rows(); | ||
| auto const item_size = sizeof(T); | ||
|
|
||
| std::vector<void*> dsts(num_columns); | ||
| std::vector<void const*> srcs(num_columns); | ||
| std::vector<size_t> sizes(num_columns, item_size * num_rows); | ||
|
|
||
| auto* base_ptr = static_cast<uint8_t*>(output); | ||
Matt711 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for (int i = 0; i < num_columns; ++i) { | ||
| auto const& col = input.column(i); | ||
| CUDF_EXPECTS(col.type() == input.column(0).type(), "All columns must have the same dtype"); | ||
|
|
||
| auto* src_ptr = static_cast<void const*>(col.data<T>()); | ||
| auto* dst_ptr = base_ptr + i * item_size * num_rows; | ||
|
|
||
| srcs[i] = src_ptr; | ||
| dsts[i] = dst_ptr; | ||
| } | ||
|
|
||
| #if defined(CUDA_VERSION) && CUDA_VERSION >= 12080 | ||
| cudaMemcpyAttributes attr{}; | ||
| attr.srcAccessOrder = cudaMemcpySrcAccessOrderStream; | ||
| std::vector<cudaMemcpyAttributes> attrs{attr}; | ||
| std::vector<size_t> attr_idxs{0}; | ||
| size_t fail_idx = SIZE_MAX; | ||
Matt711 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| CUDF_CUDA_TRY(cudaMemcpyBatchAsync(dsts.data(), | ||
Matt711 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const_cast<void**>(srcs.data()), | ||
| sizes.data(), | ||
| num_columns, | ||
| attrs.data(), | ||
| attr_idxs.data(), | ||
| attrs.size(), | ||
| &fail_idx, | ||
| stream.value())); | ||
| #else | ||
| for (int i = 0; i < num_columns; ++i) { | ||
| CUDF_CUDA_TRY(cudaMemcpyAsync(dsts[i], srcs[i], sizes[i], cudaMemcpyDeviceToDevice, stream.value())); | ||
| } | ||
| #endif | ||
Matt711 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
|
|
||
| struct TableToArrayDispatcher { | ||
Matt711 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| table_view const& input; | ||
| void* output; | ||
| rmm::cuda_stream_view stream; | ||
|
|
||
| template <typename T, CUDF_ENABLE_IF(is_fixed_width<T>() || is_fixed_point<T>())> | ||
| void operator()() const | ||
| { | ||
| if constexpr (is_fixed_point<T>()) { | ||
| using StorageType = cudf::device_storage_type_t<T>; | ||
| _table_to_device_array<StorageType>(input, output, stream); | ||
| } else { | ||
| _table_to_device_array<T>(input, output, stream); | ||
| } | ||
| } | ||
|
|
||
| template <typename T, CUDF_ENABLE_IF(!is_fixed_width<T>() && !is_fixed_point<T>())> | ||
| void operator()() const | ||
| { | ||
| CUDF_FAIL("Unsupported dtype"); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| void table_to_device_array(table_view const& input, | ||
Matt711 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| void* output, | ||
| data_type output_dtype, | ||
| rmm::cuda_stream_view stream, | ||
| rmm::device_async_resource_ref) | ||
| { | ||
| CUDF_FUNC_RANGE(); | ||
| cudf::type_dispatcher(output_dtype, TableToArrayDispatcher{input, output, stream}); | ||
| } | ||
|
|
||
| } // namespace cudf | ||
Matt711 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Matt711 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * Copyright (c) 2025, 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. | ||
| */ | ||
|
|
||
| #include <cudf_test/base_fixture.hpp> | ||
| #include <cudf_test/column_wrapper.hpp> | ||
| #include <cudf_test/type_list_utilities.hpp> | ||
| #include <cudf_test/type_lists.hpp> | ||
|
|
||
| #include <cudf/column/column_factories.hpp> | ||
| #include <cudf/reshape.hpp> | ||
| #include <cudf/table/table.hpp> | ||
| #include <cudf/table/table_view.hpp> | ||
| #include <cudf/utilities/default_stream.hpp> | ||
| #include <cudf/utilities/type_dispatcher.hpp> | ||
|
|
||
| #include <rmm/cuda_stream_view.hpp> | ||
| #include <rmm/device_buffer.hpp> | ||
|
|
||
| template <typename T> | ||
| struct TableToDeviceArrayTypedTest : public cudf::test::BaseFixture {}; | ||
|
|
||
| using SupportedTypes = cudf::test::Types<int8_t, | ||
| int16_t, | ||
| int32_t, | ||
| int64_t, | ||
| uint8_t, | ||
| uint16_t, | ||
| uint32_t, | ||
| uint64_t, | ||
| float, | ||
| double, | ||
| cudf::timestamp_D, | ||
| cudf::timestamp_s, | ||
| cudf::timestamp_ms, | ||
| cudf::timestamp_us, | ||
| cudf::timestamp_ns, | ||
| cudf::duration_D, | ||
| cudf::duration_s, | ||
| cudf::duration_ms, | ||
| cudf::duration_us, | ||
| cudf::duration_ns>; | ||
|
|
||
| TYPED_TEST_SUITE(TableToDeviceArrayTypedTest, SupportedTypes); | ||
|
|
||
| TYPED_TEST(TableToDeviceArrayTypedTest, SupportedTypes) | ||
| { | ||
| using T = TypeParam; | ||
| auto stream = cudf::get_default_stream(); | ||
|
|
||
| auto const dtype = cudf::data_type{cudf::type_to_id<T>()}; | ||
|
|
||
| auto const col0 = cudf::test::make_type_param_vector<T>({1, 2, 3}); | ||
| auto const col1 = cudf::test::make_type_param_vector<T>({4, 5, 6}); | ||
| auto const col2 = cudf::test::make_type_param_vector<T>({7, 8, 9}); | ||
| auto const col3 = cudf::test::make_type_param_vector<T>({10, 11, 12}); | ||
|
|
||
| std::vector<std::unique_ptr<cudf::column>> cols; | ||
| auto make_col = [&](auto const& data) { | ||
| return std::make_unique<cudf::column>( | ||
| cudf::test::fixed_width_column_wrapper<T>(data.begin(), data.end())); | ||
| }; | ||
|
|
||
| cols.push_back(make_col(col0)); | ||
| cols.push_back(make_col(col1)); | ||
| cols.push_back(make_col(col2)); | ||
| cols.push_back(make_col(col3)); | ||
|
|
||
| cudf::table_view input({cols[0]->view(), cols[1]->view(), cols[2]->view(), cols[3]->view()}); | ||
| size_t num_elements = 3 * 4; | ||
Matt711 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| rmm::device_buffer output(num_elements * sizeof(T), stream); | ||
|
|
||
| cudf::table_to_device_array( | ||
| input, output.data(), dtype, stream, rmm::mr::get_current_device_resource()); | ||
|
|
||
| std::vector<T> host_result(num_elements); | ||
| CUDF_CUDA_TRY(cudaMemcpy( | ||
Matt711 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| host_result.data(), output.data(), num_elements * sizeof(T), cudaMemcpyDeviceToHost)); | ||
|
|
||
| auto const expected_data = | ||
| cudf::test::make_type_param_vector<T>({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); | ||
| std::vector<T> expected(expected_data.begin(), expected_data.end()); | ||
|
|
||
| EXPECT_EQ(host_result, expected); | ||
| } | ||
|
|
||
| template <typename T> | ||
| struct FixedPointTableToDeviceArrayTest : public cudf::test::BaseFixture {}; | ||
|
|
||
| TYPED_TEST_SUITE(FixedPointTableToDeviceArrayTest, cudf::test::FixedPointTypes); | ||
|
|
||
| TYPED_TEST(FixedPointTableToDeviceArrayTest, SupportedFixedPointTypes) | ||
| { | ||
| using decimalXX = TypeParam; | ||
| using RepType = cudf::device_storage_type_t<decimalXX>; | ||
| using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>; | ||
|
|
||
| auto stream = cudf::get_default_stream(); | ||
| auto scale = numeric::scale_type{-2}; | ||
| auto dtype = cudf::data_type{cudf::type_to_id<decimalXX>(), scale}; | ||
|
|
||
| fp_wrapper col0({123, 456, 789}, scale); | ||
| fp_wrapper col1({321, 654, 987}, scale); | ||
|
|
||
| cudf::table_view input({col0, col1}); | ||
| rmm::device_buffer output(2 * 3 * sizeof(RepType), stream); | ||
|
|
||
| cudf::table_to_device_array( | ||
| input, output.data(), dtype, stream, rmm::mr::get_current_device_resource()); | ||
|
|
||
| std::vector<RepType> host_result(6); | ||
| CUDF_CUDA_TRY(cudaMemcpy(host_result.data(), | ||
| output.data(), | ||
| host_result.size() * sizeof(RepType), | ||
| cudaMemcpyDeviceToHost)); | ||
|
|
||
| std::vector<RepType> expected{123, 456, 789, 321, 654, 987}; | ||
| EXPECT_EQ(host_result, expected); | ||
| } | ||
|
|
||
| struct TableToDeviceArrayTest : public cudf::test::BaseFixture {}; | ||
|
|
||
| TEST(TableToDeviceArrayTest, UnsupportedStringType) | ||
| { | ||
| auto stream = cudf::get_default_stream(); | ||
| auto col = cudf::test::strings_column_wrapper({"a", "b", "c"}); | ||
| cudf::table_view input_table({col}); | ||
| rmm::device_buffer output(3 * sizeof(int32_t), stream); | ||
|
|
||
| EXPECT_THROW(cudf::table_to_device_array(input_table, | ||
| output.data(), | ||
| cudf::data_type{cudf::type_id::STRING}, | ||
| stream, | ||
| rmm::mr::get_current_device_resource()), | ||
| cudf::logic_error); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.