diff --git a/cpp/include/cudf/column/column_view.hpp b/cpp/include/cudf/column/column_view.hpp index d3d64eb21df..90823f22775 100644 --- a/cpp/include/cudf/column/column_view.hpp +++ b/cpp/include/cudf/column/column_view.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/cpp/src/column/column.cu b/cpp/src/column/column.cu index 6fb66b6001a..fc1a0871e66 100644 --- a/cpp/src/column/column.cu +++ b/cpp/src/column/column.cu @@ -32,6 +32,8 @@ #include #include +#include + #include #include @@ -221,10 +223,9 @@ struct create_column_from_view { template ()> * = nullptr> std::unique_ptr operator()() { - std::vector> children; - for (size_type i = 0; i < view.num_children(); ++i) { - children.emplace_back(std::make_unique(view.child(i), stream, mr)); - } + auto op = [&](auto const &child) { return std::make_unique(child, stream, mr); }; + auto begin = thrust::make_transform_iterator(view.child_begin(), op); + auto children = std::vector>(begin, begin + view.num_children()); return std::make_unique( view.type(), diff --git a/cpp/src/column/column_device_view.cu b/cpp/src/column/column_device_view.cu index 8791942f420..7c5d39c6f38 100644 --- a/cpp/src/column/column_device_view.cu +++ b/cpp/src/column/column_device_view.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ */ #include #include +#include #include #include @@ -44,12 +45,10 @@ std::unique_ptr> create_device_view_from_view(ColumnView const& source, rmm::cuda_stream_view stream) { size_type num_children = source.num_children(); - // First calculate the size of memory needed to hold the - // child columns. This is done by calling extent() - // for each of the children. - auto get_extent = thrust::make_transform_iterator( - thrust::make_counting_iterator(0), - [&source](auto i) { return ColumnDeviceView::extent(source.child(i)); }); + // First calculate the size of memory needed to hold the child columns. This is done by calling + // extent() for each of the children. + auto get_extent = cudf::detail::make_counting_transform_iterator( + 0, [&source](auto i) { return ColumnDeviceView::extent(source.child(i)); }); // pad the allocation for aligning the first pointer auto const descendant_storage_bytes = std::accumulate( diff --git a/cpp/src/column/column_view.cpp b/cpp/src/column/column_view.cpp index a4af76cd493..3e784401be2 100644 --- a/cpp/src/column/column_view.cpp +++ b/cpp/src/column/column_view.cpp @@ -20,7 +20,10 @@ #include #include +#include + #include +#include #include namespace cudf { @@ -124,11 +127,9 @@ mutable_column_view::operator column_view() const size_type count_descendants(column_view parent) { - size_type count{parent.num_children()}; - for (size_type i = 0; i < parent.num_children(); ++i) { - count += count_descendants(parent.child(i)); - } - return count; + auto descendants = [](auto const& child) { return count_descendants(child); }; + auto begin = thrust::make_transform_iterator(parent.child_begin(), descendants); + return std::accumulate(begin, begin + parent.num_children(), size_type{parent.num_children()}); } column_view logical_cast(column_view const& input, data_type type) diff --git a/cpp/src/copying/copy.cpp b/cpp/src/copying/copy.cpp index 811c9b6e42d..50bf168037d 100644 --- a/cpp/src/copying/copy.cpp +++ b/cpp/src/copying/copy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,8 @@ #include +#include + #include namespace cudf { @@ -58,11 +60,9 @@ std::unique_ptr allocate_like(column_view const& input, CUDF_EXPECTS(is_fixed_width(input.type()), "Expects only fixed-width type column"); mask_state allocate_mask = should_allocate_mask(mask_alloc, input.nullable()); - std::vector> children{}; - children.reserve(input.num_children()); - for (size_type index = 0; index < input.num_children(); index++) { - children.emplace_back(allocate_like(input.child(index), size, mask_alloc, stream, mr)); - } + auto op = [&](auto const& child) { return allocate_like(child, size, mask_alloc, stream, mr); }; + auto begin = thrust::make_transform_iterator(input.child_begin(), op); + std::vector> children(begin, begin + input.num_children()); return std::make_unique(input.type(), size, diff --git a/cpp/src/copying/slice.cpp b/cpp/src/copying/slice.cpp index f0db64550ee..2fd656ac804 100644 --- a/cpp/src/copying/slice.cpp +++ b/cpp/src/copying/slice.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -33,30 +34,27 @@ std::vector slice(column_view const& input, { CUDF_EXPECTS(indices.size() % 2 == 0, "indices size must be even"); - if (indices.empty()) { return {}; } + if (indices.empty()) return {}; auto null_counts = cudf::detail::segmented_count_unset_bits(input.null_mask(), indices, stream); + auto const children = std::vector(input.child_begin(), input.child_end()); - std::vector children{}; - for (size_type i = 0; i < input.num_children(); i++) { children.push_back(input.child(i)); } - - std::vector result{}; - for (size_t i = 0; i < indices.size() / 2; i++) { + auto op = [&](auto i) { auto begin = indices[2 * i]; auto end = indices[2 * i + 1]; CUDF_EXPECTS(begin >= 0, "Starting index cannot be negative."); CUDF_EXPECTS(end >= begin, "End index cannot be smaller than the starting index."); CUDF_EXPECTS(end <= input.size(), "Slice range out of bounds."); - result.emplace_back(input.type(), - end - begin, - input.head(), - input.null_mask(), - null_counts[i], - input.offset() + begin, - children); - } - - return result; + return column_view{input.type(), + end - begin, + input.head(), + input.null_mask(), + null_counts[i], + input.offset() + begin, + children}; + }; + auto begin = cudf::detail::make_counting_transform_iterator(0, op); + return std::vector{begin, begin + indices.size() / 2}; } } // namespace detail @@ -75,15 +73,13 @@ std::vector slice(cudf::table_view const& input, CUDF_EXPECTS(indices.size() % 2 == 0, "indices size must be even"); if (indices.empty()) { return {}; } - // 2d arrangement of column_views that represent the outgoing table_views - // sliced_table[i][j] + // 2d arrangement of column_views that represent the outgoing table_views sliced_table[i][j] // where i is the i'th column of the j'th table_view - std::vector> sliced_table; + auto op = [&indices](auto const& c) { return cudf::slice(c, indices); }; + auto f = thrust::make_transform_iterator(input.begin(), op); + + auto sliced_table = std::vector>(f, f + input.num_columns()); sliced_table.reserve(indices.size() + 1); - std::transform(input.begin(), - input.end(), - std::back_inserter(sliced_table), - [&indices](cudf::column_view const& c) { return cudf::slice(c, indices); }); std::vector result{}; // distribute columns into outgoing table_views diff --git a/cpp/src/interop/to_arrow.cpp b/cpp/src/interop/to_arrow.cpp index 37c5c93264a..7daffc1a3c3 100644 --- a/cpp/src/interop/to_arrow.cpp +++ b/cpp/src/interop/to_arrow.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, NVIDIA CORPORATION. + * Copyright (c) 2020-2021, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -101,17 +101,15 @@ struct dispatch_to_arrow { rmm::cuda_stream_view stream) { std::vector> child_arrays; - std::vector child_indices(input_view.num_children()); - std::iota(child_indices.begin(), child_indices.end(), 0); - std::transform(child_indices.begin(), - child_indices.end(), - metadata.begin(), - std::back_inserter(child_arrays), - [&input_view, &ar_mr, &stream](auto const& i, auto const& meta) { - auto c = input_view.child(i); - return type_dispatcher( - c.type(), dispatch_to_arrow{}, c, c.type().id(), meta, ar_mr, stream); - }); + std::transform( + input_view.child_begin(), + input_view.child_end(), + metadata.begin(), + std::back_inserter(child_arrays), + [&ar_mr, &stream](auto const& child, auto const& meta) { + return type_dispatcher( + child.type(), dispatch_to_arrow{}, child, child.type().id(), meta, ar_mr, stream); + }); return child_arrays; }