File: TestUnorderedMap.hpp

package info (click to toggle)
kokkos 4.7.01-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,636 kB
  • sloc: cpp: 223,676; sh: 2,446; makefile: 2,437; python: 91; fortran: 4; ansic: 2
file content (565 lines) | stat: -rw-r--r-- 17,802 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//@HEADER
// ************************************************************************
//
//                        Kokkos v. 4.0
//       Copyright (2022) National Technology & Engineering
//               Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER

#ifndef KOKKOS_TEST_UNORDERED_MAP_HPP
#define KOKKOS_TEST_UNORDERED_MAP_HPP

#include <gtest/gtest.h>
#include <iostream>
#include <Kokkos_UnorderedMap.hpp>

namespace Test {

namespace Impl {

template <typename MapType,
          typename InsertOp = typename MapType::default_op_type,
          bool Near = false, bool CheckValues = false>
struct TestInsert {
  using map_type        = MapType;
  using execution_space = typename map_type::execution_space;
  using value_type      = uint32_t;

  struct ExpectedValues {
    unsigned map_idx;
    typename map_type::value_type v;
  };
  using expected_values_type = Kokkos::View<ExpectedValues *, execution_space>;
  expected_values_type expected_values;

  map_type map;
  uint32_t inserts;
  uint32_t collisions;
  InsertOp insert_op;

  TestInsert(map_type arg_map, uint32_t arg_inserts, uint32_t arg_collisions)
      : map(arg_map), inserts(arg_inserts), collisions(arg_collisions) {
    auto len = map.capacity() > arg_inserts ? map.capacity() : arg_inserts;
    expected_values = expected_values_type("ExpectedValues", len);
  }

  void testit(bool rehash_on_fail = true) {
    execution_space().fence();

    uint32_t failed_count = 0;
    do {
      failed_count = 0;
      Kokkos::parallel_reduce(inserts, *this, failed_count);

      if (rehash_on_fail && failed_count > 0u) {
        const uint32_t new_capacity = map.capacity() +
                                      ((map.capacity() * 3ull) / 20u) +
                                      failed_count / collisions;
        map.rehash(new_capacity);
      }
    } while (rehash_on_fail && failed_count > 0u);

    // Trigger the m_size mutable bug.
    auto map_h = create_mirror(map);
    execution_space().fence();
    Kokkos::deep_copy(map_h, map);
    execution_space().fence();
    ASSERT_EQ(map_h.size(), map.size());

    if (!rehash_on_fail && CheckValues) {
      typename expected_values_type::HostMirror expected_values_h =
          create_mirror_view(expected_values);
      Kokkos::deep_copy(expected_values_h, expected_values);
      for (unsigned i = 0; i < map_h.size(); i++) {
        auto map_idx = expected_values_h(i).map_idx;
        if (map_idx != static_cast<unsigned>(~0)) {
          ASSERT_EQ(expected_values_h(map_idx).v, map_h.value_at(map_idx));
        }
      }
    }

    const unsigned int old_size = map_h.size();
    map_h.clear();
    ASSERT_EQ(map.size(), old_size);
    ASSERT_EQ(map_h.size(), 0u);
  }

  KOKKOS_INLINE_FUNCTION
  void init(value_type &failed_count) const { failed_count = 0; }

  KOKKOS_INLINE_FUNCTION
  void join(value_type &failed_count, const value_type &count) const {
    failed_count += count;
  }

  template <typename UmapOpType = InsertOp>
  KOKKOS_FORCEINLINE_FUNCTION bool is_op_noop() const {
    using vt             = typename map_type::value_type;
    using Device         = typename map_type::device_type;
    using UmapOpTypeArg1 = Kokkos::View<
        std::remove_const_t<std::conditional_t<std::is_void_v<vt>, int, vt>> *,
        Device>;
    return std::is_base_of_v<
        InsertOp, typename Kokkos::UnorderedMapInsertOpTypes<UmapOpTypeArg1,
                                                             uint32_t>::NoOp>;
  }

  template <typename UmapOpType = InsertOp>
  KOKKOS_FORCEINLINE_FUNCTION bool is_op_atomic_add() const {
    using vt             = typename map_type::value_type;
    using Device         = typename map_type::device_type;
    using UmapOpTypeArg1 = Kokkos::View<
        std::remove_const_t<std::conditional_t<std::is_void_v<vt>, int, vt>> *,
        Device>;
    return std::is_base_of_v<UmapOpType,
                             typename Kokkos::UnorderedMapInsertOpTypes<
                                 UmapOpTypeArg1, uint32_t>::AtomicAdd>;
  }

  KOKKOS_INLINE_FUNCTION
  void operator()(uint32_t i, value_type &failed_count) const {
    const uint32_t key = Near ? i / collisions : i % (inserts / collisions);
    auto ret           = map.insert(key, i, insert_op);
    if (ret.failed()) {
      ++failed_count;
      expected_values(i).map_idx = static_cast<unsigned>(~0);
    } else if (CheckValues) {
      auto map_idx                     = map.find(key);
      expected_values(map_idx).map_idx = map_idx;
      auto ptr                         = expected_values.data();
      if (is_op_atomic_add()) {
        Kokkos::atomic_add(&((ptr + map_idx)[0].v), i);
      } else if (ret.success() && is_op_noop()) {
        Kokkos::atomic_store(&((ptr + map_idx)[0].v), i);
      }
    }
  }
};

template <typename MapType, bool Near>
struct TestErase {
  using self_type = TestErase<MapType, Near>;

  using map_type        = MapType;
  using execution_space = typename MapType::execution_space;

  map_type m_map;
  uint32_t m_num_erase;
  uint32_t m_num_duplicates;

  TestErase(map_type map, uint32_t num_erases, uint32_t num_duplicates)
      : m_map(map), m_num_erase(num_erases), m_num_duplicates(num_duplicates) {}

  void testit() {
    execution_space().fence();
    Kokkos::parallel_for(m_num_erase, *this);
    execution_space().fence();
  }

  KOKKOS_INLINE_FUNCTION
  void operator()(typename execution_space::size_type i) const {
    if (Near) {
      m_map.erase(i / m_num_duplicates);
    } else {
      m_map.erase(i % (m_num_erase / m_num_duplicates));
    }
  }
};

template <typename MapType>
struct TestFind {
  using map_type        = MapType;
  using execution_space = typename MapType::execution_space::execution_space;
  using value_type      = uint32_t;

  map_type m_map;
  uint32_t m_num_insert;
  uint32_t m_num_duplicates;
  uint32_t m_max_key;

  TestFind(map_type map, uint32_t num_inserts, uint32_t num_duplicates)
      : m_map(map),
        m_num_insert(num_inserts),
        m_num_duplicates(num_duplicates),
        m_max_key(((num_inserts + num_duplicates) - 1) / num_duplicates) {}

  void testit(value_type &errors) {
    execution_space().fence();
    Kokkos::parallel_reduce(m_map.capacity(), *this, errors);
    execution_space().fence();
  }

  KOKKOS_INLINE_FUNCTION
  static void init(value_type &dst) { dst = 0; }

  KOKKOS_INLINE_FUNCTION
  static void join(value_type &dst, const value_type &src) { dst += src; }

  KOKKOS_INLINE_FUNCTION
  void operator()(typename execution_space::size_type i,
                  value_type &errors) const {
    const bool expect_to_find_i =
        (i < typename execution_space::size_type(m_max_key));

    const bool exists = m_map.exists(i);

    if (expect_to_find_i && !exists) ++errors;
    if (!expect_to_find_i && exists) ++errors;
  }
};

}  // namespace Impl

// MSVC reports a syntax error for this test.
// WORKAROUND MSVC
#ifndef _WIN32
template <typename Device, class map_type, class const_map_type,
          class insert_op_type, bool check_values = false>
void test_insert(uint32_t num_nodes, uint32_t num_inserts,
                 uint32_t num_duplicates, bool near) {
  const uint32_t expected_inserts =
      (num_inserts + num_duplicates - 1u) / num_duplicates;
  typename map_type::size_type arg_capacity_hint = 0;
  typename map_type::hasher_type arg_hasher;
  typename map_type::equal_to_type arg_equal_to;

  map_type map(arg_capacity_hint, arg_hasher, arg_equal_to);
  map.rehash(num_nodes, false);

  if (near) {
    Impl::TestInsert<map_type, insert_op_type, true, check_values> test_insert(
        map, num_inserts, num_duplicates);
    test_insert.testit();
  } else {
    Impl::TestInsert<map_type, insert_op_type, false, check_values> test_insert(
        map, num_inserts, num_duplicates);
    test_insert.testit();
  }

  const bool print_list = false;
  if (print_list) {
    Kokkos::Impl::UnorderedMapPrint<map_type> f(map);
    f.apply();
  }

  const uint32_t map_size = map.size();

  ASSERT_FALSE(map.failed_insert());
  {
    EXPECT_EQ(expected_inserts, map_size);

    {
      uint32_t find_errors = 0;
      Impl::TestFind<map_type> test_find(map, num_inserts, num_duplicates);
      test_find.testit(find_errors);
      EXPECT_EQ(0u, find_errors);
    }

    map.begin_erase();
    Impl::TestErase<map_type, false> test_erase(map, num_inserts,
                                                num_duplicates);
    test_erase.testit();
    map.end_erase();
    EXPECT_EQ(0u, map.size());
  }

  // Check the values from the insert operation
  {
    Impl::TestInsert<map_type, insert_op_type, true> test_insert(
        map, num_inserts, num_duplicates);
    test_insert.testit(false);
  }
}

template <typename Device>
void test_inserts(uint32_t num_nodes, uint32_t num_inserts,
                  uint32_t num_duplicates, bool near) {
  using key_type        = uint32_t;
  using value_type      = uint32_t;
  using value_view_type = Kokkos::View<value_type *, Device>;
  using size_type       = uint32_t;
  using hasher_type     = typename Kokkos::pod_hash<key_type>;
  using equal_to_type   = typename Kokkos::pod_equal_to<key_type>;

  using map_op_type =
      Kokkos::UnorderedMapInsertOpTypes<value_view_type, size_type>;
  using noop_type = typename map_op_type::NoOp;

  using map_type = Kokkos::UnorderedMap<key_type, value_type, Device,
                                        hasher_type, equal_to_type>;
  using const_map_type =
      Kokkos::UnorderedMap<const key_type, const value_type, Device,
                           hasher_type, equal_to_type>;

  test_insert<Device, map_type, const_map_type, noop_type>(
      num_nodes, num_inserts, num_duplicates, near);
}

template <typename Device>
void test_all_insert_ops(uint32_t num_nodes, uint32_t num_inserts,
                         uint32_t num_duplicates, bool near) {
  using key_type        = uint32_t;
  using value_type      = uint32_t;
  using value_view_type = Kokkos::View<value_type *, Device>;
  using size_type       = uint32_t;
  using hasher_type     = typename Kokkos::pod_hash<key_type>;
  using equal_to_type   = typename Kokkos::pod_equal_to<key_type>;

  using map_op_type =
      Kokkos::UnorderedMapInsertOpTypes<value_view_type, size_type>;
  using noop_type       = typename map_op_type::NoOp;
  using atomic_add_type = typename map_op_type::AtomicAdd;

  using map_type = Kokkos::UnorderedMap<key_type, value_type, Device,
                                        hasher_type, equal_to_type>;
  using const_map_type =
      Kokkos::UnorderedMap<const key_type, const value_type, Device,
                           hasher_type, equal_to_type>;

  test_insert<Device, map_type, const_map_type, noop_type, true>(
      num_nodes, num_inserts, num_duplicates, near);
  test_insert<Device, map_type, const_map_type, atomic_add_type, true>(
      num_nodes, num_inserts, num_duplicates, near);
}
#endif

template <typename Device>
void test_failed_insert(uint32_t num_nodes) {
  using map_type = Kokkos::UnorderedMap<uint32_t, uint32_t, Device>;

  map_type map(num_nodes);
  Impl::TestInsert<map_type> test_insert(map, 2u * num_nodes, 1u);
  test_insert.testit(false /*don't rehash on fail*/);
  typename Device::execution_space().fence();

  EXPECT_TRUE(map.failed_insert());
}

template <typename Device>
void test_deep_copy(uint32_t num_nodes) {
  using map_type = Kokkos::UnorderedMap<uint32_t, uint32_t, Device>;
  using const_map_type =
      Kokkos::UnorderedMap<const uint32_t, const uint32_t, Device>;

  using host_map_type = typename map_type::HostMirror;

  map_type map;
  map.rehash(num_nodes, false);

  {
    Impl::TestInsert<map_type> test_insert(map, num_nodes, 1);
    test_insert.testit();
    ASSERT_EQ(map.size(), num_nodes);
    ASSERT_FALSE(map.failed_insert());
    {
      uint32_t find_errors = 0;
      Impl::TestFind<map_type> test_find(map, num_nodes, 1);
      test_find.testit(find_errors);
      EXPECT_EQ(find_errors, 0u);
    }
  }

  auto hmap = create_mirror(map);
  Kokkos::deep_copy(hmap, map);

  ASSERT_EQ(map.size(), hmap.size());
  ASSERT_EQ(map.capacity(), hmap.capacity());
  {
    uint32_t find_errors = 0;
    Impl::TestFind<host_map_type> test_find(hmap, num_nodes, 1);
    test_find.testit(find_errors);
    EXPECT_EQ(find_errors, 0u);
  }

  map_type mmap;
  mmap.allocate_view(hmap);
  Kokkos::deep_copy(mmap, hmap);

  const_map_type cmap = mmap;

  EXPECT_EQ(cmap.size(), num_nodes);

  {
    uint32_t find_errors = 0;
    Impl::TestFind<const_map_type> test_find(cmap, num_nodes, 1);
    test_find.testit(find_errors);
    EXPECT_EQ(find_errors, 0u);
  }
}

#if !defined(_WIN32)
TEST(TEST_CATEGORY, UnorderedMap_insert) {
  for (int i = 0; i < 500; ++i) {
    test_inserts<TEST_EXECSPACE>(100000, 90000, 100, true);
    test_inserts<TEST_EXECSPACE>(100000, 90000, 100, false);
  }
  for (int i = 0; i < 5; ++i) {
    test_all_insert_ops<TEST_EXECSPACE>(1000, 900, 10, true);
    test_all_insert_ops<TEST_EXECSPACE>(1000, 900, 10, false);
  }
}
#endif

TEST(TEST_CATEGORY, UnorderedMap_failed_insert) {
  for (int i = 0; i < 1000; ++i) test_failed_insert<TEST_EXECSPACE>(10000);
}

TEST(TEST_CATEGORY, UnorderedMap_deep_copy) {
  for (int i = 0; i < 2; ++i) test_deep_copy<TEST_EXECSPACE>(10000);
}

TEST(TEST_CATEGORY, UnorderedMap_valid_empty) {
  using Key   = int;
  using Value = int;
  using Map   = Kokkos::UnorderedMap<Key, Value, TEST_EXECSPACE>;

  Map m{};
  Map n{};
  n = Map{m.capacity()};
  n.rehash(m.capacity());
  n.create_copy_view(m);
  ASSERT_TRUE(m.is_allocated());
  ASSERT_TRUE(n.is_allocated());
}

/**
 * This helper is needed because NVCC does not like extended lambdas
 * in private member functions.
 * Google Test bodies are private member functions. So it is incompatible.
 * See also https://github.com/google/googletest/issues/4104.
 */
template <typename map_type>
struct UnorderedMapInsert {
  //! Type of range-for policy and its index type.
  using range_policy_t =
      Kokkos::RangePolicy<typename map_type::execution_space,
                          Kokkos::IndexType<unsigned short int>>;
  using index_t = typename range_policy_t::index_type;

  const map_type m_map;

  //! Ensure shared ownership of @ref m_map.
  UnorderedMapInsert(map_type map) : m_map(std::move(map)) {}

  //! Insert a single value.
  template <typename T>
  void insert_single(const T &arg) const {
    Kokkos::parallel_for(
        Kokkos::RangePolicy<typename map_type::execution_space>(0, 1),
        // NOLINTNEXTLINE(kokkos-implicit-this-capture)
        KOKKOS_CLASS_LAMBDA(const index_t) { m_map.insert(arg); });
  }

  //! Insert multiple values.
  template <typename... Args>
  void insert(Args &&...args) const {
    static_assert(sizeof...(Args) > 1, "Prefer the single value version");
    constexpr size_t size = sizeof...(Args);
    Kokkos::Array<typename map_type::key_type, size> values{
        std::forward<Args>(args)...};
    Kokkos::parallel_for(
        Kokkos::RangePolicy<typename map_type::execution_space>(0, size),
        // NOLINTNEXTLINE(kokkos-implicit-this-capture)
        KOKKOS_CLASS_LAMBDA(const index_t i) { m_map.insert(values[i]); });
  }
};

TEST(TEST_CATEGORY, UnorderedMap_clear_zero_size) {
  using map_type = Kokkos::UnorderedMap<int, void, TEST_EXECSPACE>;

  map_type m(11);

  ASSERT_EQ(0u, m.size());

  UnorderedMapInsert<map_type>(m).insert(2, 3, 5, 7);

  ASSERT_EQ(4u, m.size());
  m.rehash(0);
  ASSERT_EQ(128u, m.capacity());
  ASSERT_EQ(4u, m.size());

  m.clear();
  ASSERT_EQ(0u, m.size());
}

TEST(TEST_CATEGORY, UnorderedMap_consistent_size) {
  using map_type = Kokkos::UnorderedMap<int, void, TEST_EXECSPACE>;

  map_type m(11);
  UnorderedMapInsert<map_type> inserter(m);

  inserter.insert_single(7);

  ASSERT_EQ(1u, m.size());

  {
    auto m_copy = m;
    UnorderedMapInsert<decltype(m_copy)> inserter_copy(m_copy);
    inserter_copy.insert_single(2);
    // This line triggers modified flags to be cleared in both m and m2
    const auto sz = m_copy.size();
    ASSERT_EQ(2u, sz);
  }

  ASSERT_EQ(2u, m.size());
}

struct TestMapCopy {
  using map_type = Kokkos::UnorderedMap<int, void, TEST_EXECSPACE>;
  map_type m_map;

  KOKKOS_FUNCTION
  void test_insert_to_map_copy(map_type const &input_map, const int i) const {
    auto map = input_map;
    map.insert(i);
  }

  KOKKOS_FUNCTION
  void operator()(const int i) const { test_insert_to_map_copy(m_map, i); }
};

TEST(TEST_CATEGORY, UnorderedMap_shallow_copyable_on_device) {
  TestMapCopy test_map_copy;

  Kokkos::parallel_for(Kokkos::RangePolicy<TEST_EXECSPACE>(0, 1),
                       test_map_copy);
  ASSERT_EQ(1u, test_map_copy.m_map.size());
}

void test_unordered_map_device_capture() {
  TestMapCopy::map_type map;

  Kokkos::parallel_for(
      Kokkos::RangePolicy<TEST_EXECSPACE>(0, 1),
      KOKKOS_LAMBDA(int const i) { map.insert(i); });

  ASSERT_EQ(1u, map.size());
}

TEST(TEST_CATEGORY, UnorderedMap_lambda_capturable) {
  test_unordered_map_device_capture();
}

/**
 * @test This test ensures that an @ref UnorderedMap can be built
 *       with an execution space instance (using @ref view_alloc).
 */
TEST(TEST_CATEGORY, UnorderedMap_constructor_view_alloc) {
  using map_type = Kokkos::UnorderedMap<size_t, void, TEST_EXECSPACE>;
  map_type map(Kokkos::view_alloc(TEST_EXECSPACE{}, "test umap"), 150);
  ASSERT_EQ(map.size(), 0u);
  ASSERT_GE(map.capacity(), 150u);
  ASSERT_TRUE(map.is_allocated());
}

}  // namespace Test

#endif  // KOKKOS_TEST_UNORDERED_MAP_HPP