|
| 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 <test_utils.hpp> |
| 18 | + |
| 19 | +#include <cuco/bloom_filter.cuh> |
| 20 | + |
| 21 | +#include <cuda/functional> |
| 22 | +#include <thrust/device_vector.h> |
| 23 | +#include <thrust/execution_policy.h> |
| 24 | +#include <thrust/fill.h> |
| 25 | +#include <thrust/iterator/constant_iterator.h> |
| 26 | +#include <thrust/iterator/counting_iterator.h> |
| 27 | +#include <thrust/sequence.h> |
| 28 | + |
| 29 | +#include <catch2/catch_template_test_macros.hpp> |
| 30 | +#include <catch2/generators/catch_generators.hpp> |
| 31 | + |
| 32 | +#include <cstdint> |
| 33 | +#include <exception> |
| 34 | + |
| 35 | +using size_type = int32_t; |
| 36 | + |
| 37 | +template <int32_t AddCGSize, int32_t ContainsCGSize, typename Filter> |
| 38 | +void test_variable_cg_size(Filter& filter, size_type num_keys) |
| 39 | +{ |
| 40 | + constexpr int32_t block_size = 128; |
| 41 | + constexpr int32_t grid_size = 128; |
| 42 | + |
| 43 | + using Key = typename Filter::key_type; |
| 44 | + |
| 45 | + auto ref = filter.ref(); |
| 46 | + |
| 47 | + // Generate keys |
| 48 | + thrust::device_vector<Key> keys(num_keys); |
| 49 | + thrust::sequence(thrust::device, keys.begin(), keys.end()); |
| 50 | + |
| 51 | + thrust::device_vector<bool> contained(num_keys, false); |
| 52 | + |
| 53 | + auto const always_true = thrust::constant_iterator<bool>{true}; |
| 54 | + |
| 55 | + SECTION("Check if fallback kernels work for varying combinations of CG sizes.") |
| 56 | + { |
| 57 | + cuco::detail::bloom_filter_ns::add_if_n<AddCGSize, block_size> |
| 58 | + <<<grid_size, block_size>>>(keys.begin(), num_keys, always_true, cuda::std::identity{}, ref); |
| 59 | + cuco::detail::bloom_filter_ns::contains_if_n<ContainsCGSize, block_size> |
| 60 | + <<<grid_size, block_size>>>( |
| 61 | + keys.begin(), num_keys, always_true, cuda::std::identity{}, contained.begin(), ref); |
| 62 | + REQUIRE(cuco::test::all_of(contained.begin(), contained.end(), cuda::std::identity{})); |
| 63 | + } |
| 64 | + |
| 65 | + filter.clear(); |
| 66 | + thrust::fill(contained.begin(), contained.end(), false); // reset output vector |
| 67 | + |
| 68 | + SECTION("Check if adaptive add kernel works with fallback contains kernel.") |
| 69 | + { |
| 70 | + cuco::detail::bloom_filter_ns::add<block_size> |
| 71 | + <<<grid_size, block_size>>>(keys.begin(), num_keys, ref); |
| 72 | + cuco::detail::bloom_filter_ns::contains_if_n<ContainsCGSize, block_size> |
| 73 | + <<<grid_size, block_size>>>( |
| 74 | + keys.begin(), num_keys, always_true, cuda::std::identity{}, contained.begin(), ref); |
| 75 | + REQUIRE(cuco::test::all_of(contained.begin(), contained.end(), cuda::std::identity{})); |
| 76 | + } |
| 77 | + |
| 78 | + // TODO adaptive vs. adaptive and fallback add vs. adaptive contains (requires #673) |
| 79 | +} |
| 80 | + |
| 81 | +TEMPLATE_TEST_CASE_SIG( |
| 82 | + "bloom_filter variable CG size tests", |
| 83 | + "", |
| 84 | + ((int32_t AddCGSize, int32_t ContainsCGSize, class Key, class Policy), |
| 85 | + AddCGSize, |
| 86 | + ContainsCGSize, |
| 87 | + Key, |
| 88 | + Policy), |
| 89 | + (1, 4, int32_t, cuco::default_filter_policy<cuco::xxhash_64<int32_t>, uint32_t, 1>), |
| 90 | + (1, 4, int32_t, cuco::default_filter_policy<cuco::xxhash_64<int32_t>, uint32_t, 8>), |
| 91 | + (1, 4, int32_t, cuco::default_filter_policy<cuco::xxhash_64<int32_t>, uint64_t, 1>), |
| 92 | + (1, 4, int32_t, cuco::default_filter_policy<cuco::xxhash_64<int32_t>, uint64_t, 8>), |
| 93 | + (4, 1, int32_t, cuco::default_filter_policy<cuco::xxhash_64<int32_t>, uint32_t, 1>), |
| 94 | + (4, 1, int32_t, cuco::default_filter_policy<cuco::xxhash_64<int32_t>, uint32_t, 8>), |
| 95 | + (4, 1, int32_t, cuco::default_filter_policy<cuco::xxhash_64<int32_t>, uint64_t, 1>), |
| 96 | + (4, 1, int32_t, cuco::default_filter_policy<cuco::xxhash_64<int32_t>, uint64_t, 8>)) |
| 97 | +{ |
| 98 | + using filter_type = |
| 99 | + cuco::bloom_filter<Key, cuco::extent<size_t>, cuda::thread_scope_device, Policy>; |
| 100 | + constexpr size_type num_keys{400}; |
| 101 | + |
| 102 | + uint32_t pattern_bits = Policy::words_per_block + GENERATE(0, 1, 2, 3, 4); |
| 103 | + |
| 104 | + // some parameter combinations might be invalid so we skip them |
| 105 | + try { |
| 106 | + [[maybe_unused]] auto policy = Policy{pattern_bits}; |
| 107 | + } catch (std::exception const& e) { |
| 108 | + SKIP(e.what()); |
| 109 | + } |
| 110 | + |
| 111 | + auto filter = filter_type{1000, {}, {pattern_bits}}; |
| 112 | + |
| 113 | + test_variable_cg_size<AddCGSize, ContainsCGSize>(filter, num_keys); |
| 114 | +} |
0 commit comments