File: TestEventCorrectness.hpp

package info (click to toggle)
kokkos 5.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 15,148 kB
  • sloc: cpp: 225,388; sh: 1,250; python: 78; makefile: 16; fortran: 4; ansic: 2
file content (732 lines) | stat: -rw-r--r-- 26,597 bytes parent folder | download
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project
#include <iostream>
#include <gtest/gtest.h>
#include "Kokkos_Core.hpp"

#include <impl/Kokkos_Stacktrace.hpp>
#include <vector>
#include <algorithm>
#include "Kokkos_Core_fwd.hpp"
#include "include/ToolTestingUtilities.hpp"
namespace Kokkos {
class Serial;
class OpenMP;
class Cuda;
class Threads;
class HIP;
class SYCL;
namespace Experimental {
class OpenMPTarget;
class HPX;
}  // namespace Experimental
}  // namespace Kokkos
namespace Test {
struct FencePayload {
  std::string name;
  enum distinguishable_devices { yes, no };
  distinguishable_devices distinguishable;
  uint32_t dev_id;
};

std::vector<FencePayload> found_payloads;
template <typename Lambda>
void expect_fence_events(std::vector<FencePayload>& expected, Lambda lam) {
  found_payloads = {};
  Kokkos::Tools::Experimental::set_begin_fence_callback(
      [](const char* name, const uint32_t dev_id, uint64_t*) {
        found_payloads.push_back(
            FencePayload{std::string(name),
                         FencePayload::distinguishable_devices::no, dev_id});
      });
  Kokkos::Tools::Experimental::set_begin_parallel_for_callback(
      [](const char* name, const uint32_t dev_id, uint64_t*) {
        found_payloads.push_back(
            FencePayload{std::string(name),
                         FencePayload::distinguishable_devices::no, dev_id});
      });
  lam();
  for (auto& entry : expected) {
    std::cout << "Ref: " << entry.dev_id << std::endl;
    std::cout << "Ref: " << entry.name << std::endl;
    auto search = std::find_if(
        found_payloads.begin(), found_payloads.end(),
        [&](const auto& found_entry) {
          auto name_match =
              (found_entry.name.find(entry.name) != std::string::npos);
          auto id_match = (entry.dev_id == found_entry.dev_id);
          std::cout << found_entry.dev_id << std::endl;
          std::cout << found_entry.name << std::endl;
          if (!name_match) {
            std::cout << "Miss on name\n";
          }
          if (!id_match) {
            std::cout << "Miss on id\n";
          }
          return (name_match && id_match);
        });
    auto found = (search != found_payloads.end());
    ASSERT_TRUE(found);
  }
  Kokkos::Tools::Experimental::set_begin_fence_callback(
      [](const char*, const uint32_t, uint64_t*) {});
  Kokkos::Tools::Experimental::set_begin_parallel_for_callback(
      [](const char*, const uint32_t, uint64_t*) {});
}

template <class>
struct increment {
  constexpr static const int size = 0;
};
int num_instances = 1;
using index_type  = Kokkos::RangePolicy<>::index_type;
struct TestFunctor {
  KOKKOS_FUNCTION void operator()(const index_type) const {}
};
struct TestReduceFunctor {
  using value_type = int;
  KOKKOS_FUNCTION void operator()(const index_type, value_type&) const {}
};
struct TestScanFunctor {
  using value_type = int;
  KOKKOS_FUNCTION void operator()(const index_type, value_type&, bool) const {}
};

template <typename Lambda>
void test_wrapper(const Lambda& lambda) {
  if (!std::is_same_v<Kokkos::DefaultExecutionSpace, Kokkos::Serial>) {
    lambda();
  }
}
/**
 * Test that fencing an instance with a name yields a fence
 * event of that name, and the correct device ID
 */
TEST(kokkosp, test_named_instance_fence) {
  test_wrapper([&]() {
    auto root = Kokkos::Tools::Experimental::device_id_root<
        Kokkos::DefaultExecutionSpace>();
    std::vector<FencePayload> expected{

        {"named_instance", FencePayload::distinguishable_devices::no,
         root + num_instances}};
    expect_fence_events(expected, [=]() {
      Kokkos::DefaultExecutionSpace ex;
      ex.fence("named_instance");
    });
    num_instances += increment<Kokkos::DefaultExecutionSpace>::size;
  });
}
/**
 * Test that fencing an instance without a name yields a fence
 * event of a correct name, and the correct device ID
 */
TEST(kokkosp, test_unnamed_instance_fence) {
  test_wrapper([&]() {
    auto root = Kokkos::Tools::Experimental::device_id_root<
        Kokkos::DefaultExecutionSpace>();
    std::vector<FencePayload> expected{

        {"Unnamed Instance Fence", FencePayload::distinguishable_devices::no,
         root + num_instances}};
    expect_fence_events(expected, [=]() {
      Kokkos::DefaultExecutionSpace ex;
      ex.fence();
    });
    num_instances += increment<Kokkos::DefaultExecutionSpace>::size;
  });
}

/**
 * Test that invoking a global fence with a name yields a fence
 * event of a correct name, and fences the root of the default device
 */
TEST(kokkosp, test_named_global_fence) {
  test_wrapper([&]() {
    auto root = Kokkos::Tools::Experimental::device_id_root<
        Kokkos::DefaultExecutionSpace>();

    std::vector<FencePayload> expected{

        {"test global fence", FencePayload::distinguishable_devices::no, root}};
    expect_fence_events(expected,
                        [=]() { Kokkos::fence("test global fence"); });
  });
}

/**
 * Test that invoking a global fence with no name yields a fence
 * event of a correct name, and fences the root of the default device
 */
TEST(kokkosp, test_unnamed_global_fence) {
  test_wrapper([&]() {
    auto root = Kokkos::Tools::Experimental::device_id_root<
        Kokkos::DefaultExecutionSpace>();

    std::vector<FencePayload> expected{

        {"Unnamed Global Fence", FencePayload::distinguishable_devices::no,
         root}};
    expect_fence_events(expected, [=]() { Kokkos::fence(); });
    num_instances += increment<Kokkos::DefaultExecutionSpace>::size;
  });
}
/**
 * Test that creating two default instances and fencing both yields
 * fence on the same device ID, as these should yield the same instance
 */
TEST(kokkosp, test_multiple_default_instances) {
  test_wrapper([&]() {
    std::vector<FencePayload> expected{};
    expect_fence_events(expected, [=]() {
      Kokkos::DefaultExecutionSpace ex1;
      Kokkos::DefaultExecutionSpace ex2;
      ex1.fence("named_instance_fence_one");
      ex2.fence("named_instance_fence_two");
    });
    ASSERT_EQ(found_payloads[0].dev_id, found_payloads[1].dev_id);
  });
}

/**
 * Test that device_id() and identifier_from_devid(id) are reciprocal
 * operations
 */
TEST(kokkosp, test_id_gen) {
  using namespace Kokkos::Tools::Experimental;
  using Kokkos::Tools::Experimental::DeviceTypeTraits;
  test_wrapper([&]() {
    Kokkos::DefaultExecutionSpace ex;
    auto id     = device_id(ex);
    auto id_ref = identifier_from_devid(id);
    ASSERT_EQ(DeviceTypeTraits<decltype(ex)>::id, id_ref.type);
    ASSERT_EQ(id_ref.instance_id, ex.impl_instance_id());
  });
}

/**
 * Test that fencing and kernels yield events on the correct device ID's
 */
TEST(kokkosp, test_kernel_sequence) {
  test_wrapper([&]() {
    Kokkos::DefaultExecutionSpace ex;
    auto root = Kokkos::Tools::Experimental::device_id_root<
        Kokkos::DefaultExecutionSpace>();
    std::vector<FencePayload> expected{

        {"named_instance", FencePayload::distinguishable_devices::no,
         root + num_instances},
        {"test_kernel", FencePayload::distinguishable_devices::no,
         Kokkos::Tools::Experimental::device_id(ex)}

    };
    expect_fence_events(expected, [=]() {
      TestFunctor tf;
      ex.fence("named_instance");
      Kokkos::parallel_for(
          "test_kernel",
          Kokkos::RangePolicy<Kokkos::DefaultExecutionSpace>(ex, 0, 1), tf);
    });
    num_instances += increment<Kokkos::DefaultExecutionSpace>::size;
  });
}
#ifdef KOKKOS_ENABLE_CUDA
/**
 * CUDA ONLY: test that creating instances from streams leads to events
 * on different device ID's
 */
TEST(kokkosp, test_streams) {
  test_wrapper([&]() {
    // auto root = Kokkos::Tools::Experimental::device_id_root<
    //    Kokkos::DefaultExecutionSpace>();
    std::vector<FencePayload> expected{};
    expect_fence_events(expected, [=]() {
      cudaStream_t s1, s2;
      cudaStreamCreate(&s1);
      cudaStreamCreate(&s2);
      Kokkos::Cuda default_space;
      Kokkos::Cuda space_s1(s1);
      Kokkos::Cuda space_s2(s2);
      default_space.fence();
      space_s1.fence();
      space_s2.fence();
    });
    num_instances += increment<Kokkos::DefaultExecutionSpace>::size;
    found_payloads.erase(
        std::remove_if(found_payloads.begin(), found_payloads.end(),
                       [&](const auto& entry) {
                         return (entry.name.find("Unnamed Instance Fence") ==
                                 std::string::npos);
                       }),
        found_payloads.end());
    ASSERT_NE(found_payloads[0].dev_id, found_payloads[1].dev_id);
    ASSERT_NE(found_payloads[2].dev_id, found_payloads[1].dev_id);
    ASSERT_NE(found_payloads[2].dev_id, found_payloads[0].dev_id);
  });
}

#endif
TEST(kokkosp, async_deep_copy) {
// FIXME_OPENMPTARGET
#ifdef KOKKOS_ENABLE_OPENMPTARGET
  if (std::is_same_v<Kokkos::DefaultExecutionSpace,
                     Kokkos::Experimental::OpenMPTarget>)
    GTEST_SKIP()
        << "skipping since the OpenMPTarget backend has unexpected fences";
#endif

  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableFences());
  Kokkos::View<float*> left("left", 5), right("right", 5);

  auto success = validate_absence(
      [&]() {
        Kokkos::deep_copy(Kokkos::DefaultExecutionSpace(), left, right);
      },
      [&](BeginFenceEvent begin) {
        if (begin.deviceID !=
            Kokkos::DefaultExecutionSpace().impl_instance_id()) {
          std::stringstream error_message;
          error_message
              << "Fence encountered outside of the default instance, default: "
              << Kokkos::DefaultExecutionSpace().impl_instance_id()
              << ", encountered " << begin.deviceID << " , fence name "
              << begin.name;
          return MatchDiagnostic{true, {error_message.str()}};
        }
        return MatchDiagnostic{false};
      });
  ASSERT_TRUE(success);
}
TEST(kokkosp, parallel_for) {
  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableKernels());
  auto success = validate_event_set(
      [=]() {
        TestFunctor tf;
        Kokkos::parallel_for("dogs", Kokkos::RangePolicy<>(0, 1), tf);
      },
      [=](BeginParallelForEvent begin_event, EndParallelForEvent end_event) {
        if (begin_event.name != "dogs") {
          return MatchDiagnostic{false, {"No match on BeginParallelFor name"}};
        }
        if (end_event.kID != ((begin_event.kID))) {
          return MatchDiagnostic{false, {"No match on kID's"}};
        }
        return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}

#ifndef KOKKOS_ENABLE_OPENACC
// FIXME_OPENACC: not supported reducer type
TEST(kokkosp, parallel_reduce) {
  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableKernels());
  auto success = validate_event_set(
      [=]() {
        TestReduceFunctor tf;
        int result;
        Kokkos::parallel_reduce("dogs", Kokkos::RangePolicy<>(0, 1), tf,
                                Kokkos::Sum<int>(result));
      },
      [=](BeginParallelReduceEvent begin_event,
          EndParallelReduceEvent end_event) {
        if (begin_event.name != "dogs") {
          return MatchDiagnostic{false,
                                 {"No match on BeginParallelReduce name"}};
        }
        if (end_event.kID != ((begin_event.kID))) {
          return MatchDiagnostic{false, {"No match on kID's"}};
        }
        return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}
#endif

#ifndef KOKKOS_ENABLE_OPENACC
// FIXME_OPENACC: parallel_scan not implemented yet
TEST(kokkosp, parallel_scan) {
  // FIXME_OPENMPTARGET
#ifdef KOKKOS_ENABLE_OPENMPTARGET
  if (std::is_same_v<Kokkos::DefaultExecutionSpace,
                     Kokkos::Experimental::OpenMPTarget>)
    GTEST_SKIP()
        << "skipping since the OpenMPTarget backend reports unexpected events";
#endif

  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableKernels());
  auto success = validate_event_set(
      [=]() {
        TestScanFunctor tf;
        Kokkos::parallel_scan("dogs", Kokkos::RangePolicy<>(0, 1), tf);
      },
      [=](BeginParallelScanEvent begin_event, EndParallelScanEvent end_event) {
        if (begin_event.name != "dogs") {
          return MatchDiagnostic{false, {"No match on BeginParallelScan name"}};
        }
        if (end_event.kID != ((begin_event.kID))) {
          return MatchDiagnostic{false, {"No match on kID's"}};
        }
        return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}

TEST(kokkosp, parallel_scan_no_fence) {
  // FIXME_THREADS
#ifdef KOKKOS_ENABLE_THREADS
  if (std::is_same_v<Kokkos::DefaultExecutionSpace, Kokkos::Threads>)
    GTEST_SKIP() << "skipping since the Thread backend always fences";
#endif
#if defined(KOKKOS_ENABLE_HPX) && \
    !defined(KOKKOS_ENABLE_IMPL_HPX_ASYNC_DISPATCH)
  if (std::is_same_v<Kokkos::DefaultExecutionSpace, Kokkos::Experimental::HPX>)
    GTEST_SKIP() << "skipping since the HPX backend always fences with async "
                    "dispatch disabled";
#endif
    // FIXME_OPENMPTARGET
#ifdef KOKKOS_ENABLE_OPENMPTARGET
  if (std::is_same_v<Kokkos::DefaultExecutionSpace,
                     Kokkos::Experimental::OpenMPTarget>)
    GTEST_SKIP()
        << "skipping since the OpenMPTarget backend has unexpected fences";
#endif

  // Execute the parallel_scan first without looking for fence events.
  // Depending on the backend implementation and the order of tests,
  // it might be that the first call to parallel_scan is reallocating scratch
  // memory which implies a fence when deallocating. We are not interested in
  // detecting this event.
  TestScanFunctor tf;
  Kokkos::parallel_scan("dogs", Kokkos::RangePolicy<>(0, 1), tf);

  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableKernels(),
                     Config::EnableFences());
  auto success = validate_absence(
      [=]() { Kokkos::parallel_scan("dogs", Kokkos::RangePolicy<>(0, 1), tf); },
      [=](BeginFenceEvent begin_event) {
        if (begin_event.name.find("Debug Only Check for Execution Error") !=
                std::string::npos ||
            begin_event.name.find("Kokkos Profile Tool Fence") !=
                std::string::npos)
          return MatchDiagnostic{false};
        else
          return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}

TEST(kokkosp, parallel_scan_no_fence_view) {
  // FIXME_THREADS
#ifdef KOKKOS_ENABLE_THREADS
  if (std::is_same_v<Kokkos::DefaultExecutionSpace, Kokkos::Threads>)
    GTEST_SKIP() << "skipping since the Thread backend always fences";
#endif
#if defined(KOKKOS_ENABLE_HPX) && \
    !defined(KOKKOS_ENABLE_IMPL_HPX_ASYNC_DISPATCH)
  if (std::is_same_v<Kokkos::DefaultExecutionSpace, Kokkos::Experimental::HPX>)
    GTEST_SKIP() << "skipping since the HPX backend always fences with async "
                    "dispatch disabled";
#endif
    // FIXME_OPENMPTARGET
#ifdef KOKKOS_ENABLE_OPENMPTARGET
  if (std::is_same_v<Kokkos::DefaultExecutionSpace,
                     Kokkos::Experimental::OpenMPTarget>)
    GTEST_SKIP()
        << "skipping since the OpenMPTarget backend has unexpected fences";
#endif

  // Execute the parallel_scan first without looking for fence events.
  // Depending on the backend implementation and the order of tests,
  // it might be that the first call to parallel_scan is reallocating scratch
  // memory which implies a fence when deallocating. We are not interested in
  // detecting this event.
  TestScanFunctor tf;
  Kokkos::View<typename TestScanFunctor::value_type> v("scan_result");
  Kokkos::parallel_scan("dogs", Kokkos::RangePolicy<>(0, 1), tf, v);

  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableKernels(),
                     Config::EnableFences());
  auto success = validate_absence(
      [=]() {
        Kokkos::parallel_scan("dogs", Kokkos::RangePolicy<>(0, 1), tf, v);
      },
      [=](BeginFenceEvent begin_event) {
        if (begin_event.name.find("Debug Only Check for Execution Error") !=
                std::string::npos ||
            begin_event.name.find("Kokkos Profile Tool Fence") !=
                std::string::npos)
          return MatchDiagnostic{false};
        else
          return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}
#endif

TEST(kokkosp, regions) {
  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableRegions());
  auto success = validate_event_set(
      [=]() {
        Kokkos::Tools::pushRegion("dogs");
        Kokkos::Tools::popRegion();
      },
      [=](PushRegionEvent push_event, PopRegionEvent) {
        if (push_event.name != "dogs") {
          return MatchDiagnostic{false, {"No match on PushRegion name"}};
        }

        return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}

TEST(kokkosp, fences) {
  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableFences());
  auto success = validate_event_set(
      [=]() { Kokkos::DefaultExecutionSpace().fence("dogs"); },
      [=](BeginFenceEvent begin_event, EndFenceEvent end_event) {
        if (begin_event.name != "dogs") {
          return MatchDiagnostic{false, {"No match on BeginFence name"}};
        }
        if (end_event.kID != ((begin_event.kID))) {
          return MatchDiagnostic{false, {"No match on kID's"}};
        }
        return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}

TEST(kokkosp, raw_allocation) {
  // FIXME_OPENMPTARGET
#ifdef KOKKOS_ENABLE_OPENMPTARGET
  if (std::is_same_v<Kokkos::DefaultExecutionSpace,
                     Kokkos::Experimental::OpenMPTarget>)
    GTEST_SKIP()
        << "skipping since the OpenMPTarget backend reports unexpected events";
#endif

  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableAllocs());
  auto success = validate_event_set(
      [=]() {
        void* foo =
            Kokkos::kokkos_malloc<Kokkos::DefaultExecutionSpace::memory_space>(
                "dogs", 1000);
        Kokkos::kokkos_free(foo);
      },
      [=](AllocateDataEvent alloc, DeallocateDataEvent free) {
        if (alloc.name != "dogs") {
          return MatchDiagnostic{false, {"No match on alloc name"}};
        }
        if (alloc.size != 1000) {
          return MatchDiagnostic{false, {"No match on alloc size"}};
        }
        if (alloc.ptr != free.ptr) {
          return MatchDiagnostic{false, {"No match on pointers"}};
        }
        if (free.name != "dogs") {
          return MatchDiagnostic{false, {"No match on free name"}};
        }
        if (free.size != 1000) {
          return MatchDiagnostic{false, {"No match on free size"}};
        }
        return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}

TEST(kokkosp, view) {
// FIXME_OPENMPTARGET
#ifdef KOKKOS_ENABLE_OPENMPTARGET
  if (std::is_same_v<Kokkos::DefaultExecutionSpace,
                     Kokkos::Experimental::OpenMPTarget>)
    GTEST_SKIP()
        << "skipping since the OpenMPTarget backend reports unexpected events";
#endif

  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableAllocs());
  auto success = validate_event_set(
      [=]() { Kokkos::View<float*> dogs("dogs", 1000); },
      [=](AllocateDataEvent alloc, DeallocateDataEvent free) {
        if (alloc.name != "dogs") {
          return MatchDiagnostic{false, {"No match on alloc name"}};
        }
        if (alloc.size != 1000 * sizeof(float)) {
          return MatchDiagnostic{false, {"No match on alloc size"}};
        }
        if (alloc.ptr != free.ptr) {
          return MatchDiagnostic{false, {"No match on pointers"}};
        }
        if (free.name != "dogs") {
          return MatchDiagnostic{false, {"No match on free name"}};
        }
        if (free.size != 1000 * sizeof(float)) {
          return MatchDiagnostic{false, {"No match on free size"}};
        }
        return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}

TEST(kokkosp, sections) {
  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableSections());
  auto success = validate_event_set(
      [=]() {
        uint32_t section_id;
        Kokkos::Tools::createProfileSection("dogs", &section_id);
        Kokkos::Tools::startSection(section_id);
        Kokkos::Tools::stopSection(section_id);
        Kokkos::Tools::destroyProfileSection(section_id);
      },
      [=](CreateProfileSectionEvent create, StartProfileSectionEvent start,
          StopProfileSectionEvent stop, DestroyProfileSectionEvent destroy) {
        if (create.name != "dogs") {
          return MatchDiagnostic{false, {"No match on section name"}};
        }
        if ((create.id != start.id) || (stop.id != start.id) ||
            (stop.id != destroy.id)) {
          return MatchDiagnostic{false, {"No match on section IDs"}};
        }
        return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}

TEST(kokkosp, metadata) {
  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableMetadata());
  auto success = validate_event_set(
      [=]() {
        /** Attempts to decrease the value of dog_goodness will be rejected on
         * review */
        Kokkos::Tools::declareMetadata("dog_goodness", "infinity");
      },
      [=](DeclareMetadataEvent meta) {
        if (meta.key != "dog_goodness") {
          return MatchDiagnostic{false, {"No match on metadata key"}};
        }
        if (meta.value != "infinity") {
          return MatchDiagnostic{false, {"No match on metadata value"}};
        }
        return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}

TEST(kokkosp, profile_events) {
  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableProfileEvents());
  auto success = validate_event_set(
      [=]() { Kokkos::Tools::markEvent("dog_goodness>=infinity"); },
      [=](ProfileEvent event) {
        if (event.name != "dog_goodness>=infinity") {
          return MatchDiagnostic{false, {"No match on profiled event name"}};
        }
        return MatchDiagnostic{true};
      });
  ASSERT_TRUE(success);
}
#if defined(KOKKOS_ENABLE_TUNING)
TEST(kokkosp, tuning_sequence) {
  using namespace Kokkos::Test::Tools;
  listen_tool_events(Config::DisableAll(), Config::EnableTuning());
  size_t input_id, output_id;
  Kokkos::Tools::Experimental::VariableInfo input_info;
  input_info.type = Kokkos::Tools::Experimental::ValueType::kokkos_value_int64;
  input_info.category = Kokkos::Tools::Experimental::StatisticalCategory::
      kokkos_value_categorical;
  input_info.valueQuantity =
      Kokkos::Tools::Experimental::CandidateValueType::kokkos_value_unbounded;
  Kokkos::Tools::Experimental::VariableInfo output_info = input_info;
  output_info.valueQuantity =
      Kokkos::Tools::Experimental::CandidateValueType::kokkos_value_set;
  std::vector<int64_t> values{1, 2, 3, 4, 5};
  output_info.candidates = Kokkos::Tools::Experimental::make_candidate_set(
      values.size(), values.data());

  auto success = validate_event_set(
      [&]() {
        input_id = Kokkos::Tools::Experimental::declare_input_type("input.dogs",
                                                                   input_info);
        output_id = Kokkos::Tools::Experimental::declare_output_type(
            "output.dogs", output_info);
        auto next_context = Kokkos::Tools::Experimental::get_new_context_id();
        Kokkos::Tools::Experimental::begin_context(next_context);
        Kokkos::Tools::Experimental::VariableValue feature_value =
            Kokkos::Tools::Experimental::make_variable_value(input_id,
                                                             int64_t(0));
        Kokkos::Tools::Experimental::VariableValue tuning_value =
            Kokkos::Tools::Experimental::make_variable_value(output_id,
                                                             int64_t(1));
        Kokkos::Tools::Experimental::set_input_values(next_context, 1,
                                                      &feature_value);
        Kokkos::Tools::Experimental::request_output_values(next_context, 1,
                                                           &tuning_value);
        Kokkos::Tools::Experimental::end_context(next_context);
      },
      [&](DeclareInputTypeEvent input, DeclareOutputTypeEvent output) {
        if (input.variable_id != input_id) {
          return MatchDiagnostic{false, {"No match on input id"}};
        }
        if (output.variable_id != output_id) {
          return MatchDiagnostic{false, {"No match on output id"}};
        }
        if (output.info.candidates.set.size != 5) {
          return MatchDiagnostic{
              false, {"Candidates not properly passed through tuning system"}};
        }
        return MatchDiagnostic{true};
      },
      [=](BeginContextEvent) { return MatchDiagnostic{true}; },
      [&](RequestOutputValuesEvent value_request) {
        if (value_request.inputs[0].metadata->type != input_info.type) {
          return MatchDiagnostic{false, {"No match on input in request"}};
        }
        if (value_request.outputs[0].metadata->type != output_info.type) {
          return MatchDiagnostic{false, {"No match on output in request"}};
        }
        return MatchDiagnostic{true};
      },
      [=](EndContextEvent) { return MatchDiagnostic{true}; });
  ASSERT_TRUE(success);
}
#endif
TEST(kokkosp, no_init_kernel) {
  using namespace Kokkos::Test::Tools;

  listen_tool_events(Config::DisableAll(), Config::EnableKernels());
  auto success = validate_absence(
      [=]() {
        Kokkos::View<float*> not_inited(
            Kokkos::ViewAllocateWithoutInitializing("no_inits_here_dog"), 100);
      },
      [=](BeginParallelForEvent) {
        return MatchDiagnostic{true, {"Found begin event"}};
      },
      [=](EndParallelForEvent) {
        return MatchDiagnostic{true, {"Found end event"}};
      });
  ASSERT_TRUE(success);
}

TEST(kokkosp, get_events) {
  using namespace Kokkos::Test::Tools;
  auto event_vector = get_event_set([=]() {
    Kokkos::Tools::pushRegion("dogs");
    Kokkos::Tools::popRegion();
  });
  for (const auto& ptr : event_vector) {
    ASSERT_FALSE(is_a<BeginParallelForEvent>(ptr));
  }
}
}  // namespace Test