|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 <utils.hpp> |
| 18 | + |
| 19 | +#include <cuco/static_map.cuh> |
| 20 | + |
| 21 | +#include <thrust/device_vector.h> |
| 22 | +#include <thrust/functional.h> |
| 23 | +#include <thrust/iterator/discard_iterator.h> |
| 24 | +#include <thrust/sort.h> |
| 25 | + |
| 26 | +#include <catch2/catch.hpp> |
| 27 | + |
| 28 | +TEMPLATE_TEST_CASE_SIG("Duplicate keys", |
| 29 | + "", |
| 30 | + ((typename Key, typename Value), Key, Value), |
| 31 | + (int32_t, int32_t), |
| 32 | + (int32_t, int64_t), |
| 33 | + (int64_t, int32_t), |
| 34 | + (int64_t, int64_t)) |
| 35 | +{ |
| 36 | + constexpr std::size_t num_keys{500'000}; |
| 37 | + cuco::static_map<Key, Value> map{num_keys * 2, -1, -1}; |
| 38 | + |
| 39 | + auto m_view = map.get_device_mutable_view(); |
| 40 | + auto view = map.get_device_view(); |
| 41 | + |
| 42 | + thrust::device_vector<Key> d_keys(num_keys); |
| 43 | + thrust::device_vector<Value> d_values(num_keys); |
| 44 | + |
| 45 | + thrust::sequence(thrust::device, d_keys.begin(), d_keys.end()); |
| 46 | + thrust::sequence(thrust::device, d_values.begin(), d_values.end()); |
| 47 | + |
| 48 | + auto pairs_begin = thrust::make_transform_iterator( |
| 49 | + thrust::make_counting_iterator<int>(0), |
| 50 | + [] __device__(auto i) { return cuco::pair_type<Key, Value>(i / 2, i / 2); }); |
| 51 | + |
| 52 | + thrust::device_vector<Value> d_results(num_keys); |
| 53 | + thrust::device_vector<bool> d_contained(num_keys); |
| 54 | + |
| 55 | + SECTION("Retrieve all entries") |
| 56 | + { |
| 57 | + auto constexpr gold = num_keys / 2; |
| 58 | + thrust::device_vector<Key> unique_keys(gold); |
| 59 | + thrust::device_vector<Key> unique_values(gold); |
| 60 | + |
| 61 | + // Retrieve all from an empty map |
| 62 | + auto [empty_key_end, empty_value_end] = |
| 63 | + map.retrieve_all(unique_keys.begin(), unique_values.begin()); |
| 64 | + REQUIRE(std::distance(unique_keys.begin(), empty_key_end) == 0); |
| 65 | + REQUIRE(std::distance(unique_values.begin(), empty_value_end) == 0); |
| 66 | + |
| 67 | + map.insert(pairs_begin, pairs_begin + num_keys); |
| 68 | + |
| 69 | + auto const num_entries = map.get_size(); |
| 70 | + REQUIRE(num_entries == gold); |
| 71 | + |
| 72 | + auto [key_out_end, value_out_end] = |
| 73 | + map.retrieve_all(unique_keys.begin(), unique_values.begin()); |
| 74 | + REQUIRE(std::distance(unique_keys.begin(), key_out_end) == gold); |
| 75 | + REQUIRE(std::distance(unique_values.begin(), value_out_end) == gold); |
| 76 | + |
| 77 | + thrust::sort(thrust::device, unique_keys.begin(), unique_keys.end()); |
| 78 | + REQUIRE(cuco::test::equal(unique_keys.begin(), |
| 79 | + unique_keys.end(), |
| 80 | + thrust::make_counting_iterator<Key>(0), |
| 81 | + thrust::equal_to<Key>{})); |
| 82 | + } |
| 83 | + |
| 84 | + SECTION("Tests of contains") |
| 85 | + { |
| 86 | + map.insert(pairs_begin, pairs_begin + num_keys); |
| 87 | + map.contains(d_keys.begin(), d_keys.end(), d_contained.begin()); |
| 88 | + |
| 89 | + REQUIRE(cuco::test::all_of(d_contained.begin(), |
| 90 | + d_contained.begin() + num_keys / 2, |
| 91 | + [] __device__(bool const& b) { return b; })); |
| 92 | + |
| 93 | + REQUIRE(cuco::test::none_of(d_contained.begin() + num_keys / 2, |
| 94 | + d_contained.end(), |
| 95 | + [] __device__(bool const& b) { return b; })); |
| 96 | + } |
| 97 | +} |
0 commit comments