|
| 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 | + |
| 17 | +#include <tests/groupby/groupby_test_util.hpp> |
| 18 | + |
| 19 | +#include <cudf_test/base_fixture.hpp> |
| 20 | +#include <cudf_test/column_wrapper.hpp> |
| 21 | +#include <cudf_test/debug_utilities.hpp> |
| 22 | +#include <cudf_test/iterator_utilities.hpp> |
| 23 | +#include <cudf_test/type_lists.hpp> |
| 24 | + |
| 25 | +#include <cudf/column/column_factories.hpp> |
| 26 | +#include <cudf/detail/aggregation/aggregation.hpp> |
| 27 | + |
| 28 | +#include <rmm/exec_policy.hpp> |
| 29 | + |
| 30 | +#include <thrust/iterator/counting_iterator.h> |
| 31 | +#include <thrust/transform.h> |
| 32 | + |
| 33 | +using namespace cudf::test::iterators; |
| 34 | + |
| 35 | +struct test : public cudf::test::BaseFixture {}; |
| 36 | + |
| 37 | +std::unique_ptr<cudf::column> double_sqr(cudf::column_view const& values, |
| 38 | + cudf::device_span<cudf::size_type const> group_offsets, |
| 39 | + cudf::device_span<cudf::size_type const> group_labels, |
| 40 | + cudf::size_type num_groups, |
| 41 | + rmm::cuda_stream_view stream, |
| 42 | + rmm::device_async_resource_ref mr) |
| 43 | +{ |
| 44 | + auto output = cudf::make_numeric_column( |
| 45 | + cudf::data_type{cudf::type_id::INT32}, values.size(), cudf::mask_state::UNALLOCATED, stream); |
| 46 | + thrust::transform(rmm::exec_policy(stream), |
| 47 | + thrust::make_counting_iterator(0), |
| 48 | + thrust::make_counting_iterator(values.size()), |
| 49 | + output->mutable_view().begin<int>(), |
| 50 | + [values = values.begin<int>()] __device__(int idx) -> int { |
| 51 | + return 2 * values[idx] * values[idx]; |
| 52 | + }); |
| 53 | + return output; |
| 54 | +} |
| 55 | + |
| 56 | +std::unique_ptr<cudf::column> triple_sqr(cudf::column_view const& values, |
| 57 | + cudf::device_span<cudf::size_type const> group_offsets, |
| 58 | + cudf::device_span<cudf::size_type const> group_labels, |
| 59 | + cudf::size_type num_groups, |
| 60 | + rmm::cuda_stream_view stream, |
| 61 | + rmm::device_async_resource_ref mr) |
| 62 | +{ |
| 63 | + auto output = cudf::make_numeric_column( |
| 64 | + cudf::data_type{cudf::type_id::INT32}, values.size(), cudf::mask_state::UNALLOCATED, stream); |
| 65 | + thrust::transform(rmm::exec_policy(stream), |
| 66 | + thrust::make_counting_iterator(0), |
| 67 | + thrust::make_counting_iterator(values.size()), |
| 68 | + output->mutable_view().begin<int>(), |
| 69 | + [values = values.begin<int>()] __device__(int idx) -> int { |
| 70 | + return 3 * values[idx] * values[idx]; |
| 71 | + }); |
| 72 | + return output; |
| 73 | +} |
| 74 | + |
| 75 | +TEST_F(test, double_sqr) |
| 76 | +{ |
| 77 | + cudf::test::fixed_width_column_wrapper<int> keys{1, 1, 1, 1, 1}; |
| 78 | + cudf::test::fixed_width_column_wrapper<int> vals{0, 1, 2, 3, 4}; |
| 79 | + |
| 80 | + auto agg = cudf::make_host_udf_aggregation<cudf::groupby_aggregation>(double_sqr); |
| 81 | + std::vector<cudf::groupby::aggregation_request> requests; |
| 82 | + requests.emplace_back(); |
| 83 | + requests[0].values = vals; |
| 84 | + requests[0].aggregations.push_back(std::move(agg)); |
| 85 | + cudf::groupby::groupby gb_obj( |
| 86 | + cudf::table_view({keys}), cudf::null_policy::INCLUDE, cudf::sorted::NO, {}, {}); |
| 87 | + |
| 88 | + auto result = gb_obj.aggregate(requests, cudf::test::get_default_stream()); |
| 89 | + |
| 90 | + // Got output: 0,2,8,18,32 |
| 91 | + cudf::test::print(*result.second[0].results[0]); |
| 92 | +} |
| 93 | + |
| 94 | +TEST_F(test, triple_sqr) |
| 95 | +{ |
| 96 | + cudf::test::fixed_width_column_wrapper<int> keys{1, 1, 1, 1, 1}; |
| 97 | + cudf::test::fixed_width_column_wrapper<int> vals{0, 1, 2, 3, 4}; |
| 98 | + |
| 99 | + auto agg = cudf::make_host_udf_aggregation<cudf::groupby_aggregation>(triple_sqr); |
| 100 | + std::vector<cudf::groupby::aggregation_request> requests; |
| 101 | + requests.emplace_back(); |
| 102 | + requests[0].values = vals; |
| 103 | + requests[0].aggregations.push_back(std::move(agg)); |
| 104 | + cudf::groupby::groupby gb_obj( |
| 105 | + cudf::table_view({keys}), cudf::null_policy::INCLUDE, cudf::sorted::NO, {}, {}); |
| 106 | + |
| 107 | + auto result = gb_obj.aggregate(requests, cudf::test::get_default_stream()); |
| 108 | + |
| 109 | + // Got output: 0,3,12,27,48 |
| 110 | + cudf::test::print(*result.second[0].results[0]); |
| 111 | +} |
0 commit comments