File: TestKernelNames.cpp

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 (219 lines) | stat: -rw-r--r-- 7,793 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
//@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

#include <Kokkos_Core.hpp>

#include <gtest/gtest.h>

namespace {

#ifdef KOKKOS_ENABLE_IMPL_TYPEINFO
template <class T>
std::string typeid_name(T const&) {
  return std::string(Kokkos::Impl::TypeInfo<T>::name());
}
#else
template <class T>
std::string typeid_name(T const&) {
  return typeid(T).name();
}
#endif

std::string last_parallel_for;
std::string last_parallel_reduce;
std::string last_parallel_scan;

void get_parallel_for_kernel_name(char const* kernelName, uint32_t /*deviceID*/,
                                  uint64_t* /*kernelID*/) {
  last_parallel_for = kernelName;
}

void get_parallel_reduce_kernel_name(char const* kernelName,
                                     uint32_t /*deviceID*/,
                                     uint64_t* /*kernelID*/) {
  last_parallel_reduce = kernelName;
}

void get_parallel_scan_kernel_name(char const* kernelName,
                                   uint32_t /*deviceID*/,
                                   uint64_t* /*kernelID*/) {
  last_parallel_scan = kernelName;
}

struct WorkTag {};

void test_kernel_name_parallel_for() {
  Kokkos::Tools::Experimental::set_begin_parallel_for_callback(
      get_parallel_for_kernel_name);

  using ExecutionSpace = Kokkos::DefaultExecutionSpace;

  {
    std::string const my_label = "my_parallel_for_range_policy";

    auto const my_lambda = KOKKOS_LAMBDA(int){};
    Kokkos::parallel_for(my_label, Kokkos::RangePolicy<ExecutionSpace>(0, 1),
                         my_lambda);
    ASSERT_EQ(last_parallel_for, my_label);

    Kokkos::parallel_for(Kokkos::RangePolicy<ExecutionSpace>(0, 1), my_lambda);
    ASSERT_EQ(last_parallel_for, typeid_name(my_lambda));
#ifndef KOKKOS_ENABLE_CXX17
    ASSERT_FALSE(last_parallel_for.starts_with("const "))
        << last_parallel_for << " is const-qualified";
#endif

    auto const my_lambda_with_tag = KOKKOS_LAMBDA(WorkTag, int){};
    Kokkos::parallel_for(my_label,
                         Kokkos::RangePolicy<ExecutionSpace, WorkTag>(0, 1),
                         my_lambda_with_tag);
    ASSERT_EQ(last_parallel_for, my_label);

    Kokkos::parallel_for(Kokkos::RangePolicy<ExecutionSpace, WorkTag>(0, 1),
                         my_lambda_with_tag);
    ASSERT_EQ(last_parallel_for,
              typeid_name(my_lambda_with_tag) + "/" + typeid_name(WorkTag{}));
#ifndef KOKKOS_ENABLE_CXX17
    ASSERT_FALSE(last_parallel_for.starts_with("const "))
        << last_parallel_for << " is const-qualified";
#endif
  }

  Kokkos::Tools::Experimental::set_begin_parallel_for_callback(nullptr);
}

void test_kernel_name_parallel_reduce() {
  Kokkos::Tools::Experimental::set_begin_parallel_reduce_callback(
      get_parallel_reduce_kernel_name);

  using ExecutionSpace = Kokkos::DefaultExecutionSpace;

  {
    std::string const my_label = "my_parallel_reduce_range_policy";
    float my_result;

    auto const my_lambda = KOKKOS_LAMBDA(int, float&){};
    Kokkos::parallel_reduce(my_label, Kokkos::RangePolicy<ExecutionSpace>(0, 1),
                            my_lambda, my_result);
    ASSERT_EQ(last_parallel_reduce, my_label);

    Kokkos::parallel_reduce(Kokkos::RangePolicy<ExecutionSpace>(0, 1),
                            my_lambda, my_result);
#ifndef KOKKOS_COMPILER_MSVC
    ASSERT_NE(last_parallel_reduce.find(typeid_name(my_lambda)),
              std::string::npos)
        << last_parallel_reduce << " does not contain "
        << typeid_name(
               my_lambda);  // internally using Impl::CombinedFunctorReducer
                            // but the name should still include the lambda as
                            // template parameter
#endif
#ifndef KOKKOS_ENABLE_CXX17
    ASSERT_FALSE(last_parallel_reduce.starts_with("const "))
        << last_parallel_reduce << " is const-qualified";
#endif

    auto const my_lambda_with_tag = KOKKOS_LAMBDA(WorkTag, int, float&){};
    Kokkos::parallel_reduce(my_label,
                            Kokkos::RangePolicy<ExecutionSpace, WorkTag>(0, 1),
                            my_lambda_with_tag, my_result);
    ASSERT_EQ(last_parallel_reduce, my_label);

    Kokkos::parallel_reduce(Kokkos::RangePolicy<ExecutionSpace, WorkTag>(0, 1),
                            my_lambda_with_tag, my_result);
    auto const suffix = std::string("/") + typeid_name(WorkTag{});
    ASSERT_EQ(last_parallel_reduce.find(suffix),
              last_parallel_reduce.length() - suffix.length());
#ifndef KOKKOS_ENABLE_CXX17
    ASSERT_FALSE(last_parallel_reduce.starts_with("const "))
        << last_parallel_reduce << " is const-qualified";
#endif
  }

  Kokkos::Tools::Experimental::set_begin_parallel_reduce_callback(nullptr);
}

void test_kernel_name_parallel_scan() {
  Kokkos::Tools::Experimental::set_begin_parallel_scan_callback(
      get_parallel_scan_kernel_name);

  using ExecutionSpace = Kokkos::DefaultExecutionSpace;

  {
    std::string const my_label = "my_parallel_scan_range_policy";

    auto const my_lambda = KOKKOS_LAMBDA(int, float&, bool){};
    Kokkos::parallel_scan(my_label, Kokkos::RangePolicy<ExecutionSpace>(0, 1),
                          my_lambda);
    ASSERT_EQ(last_parallel_scan, my_label);

    Kokkos::parallel_scan(Kokkos::RangePolicy<ExecutionSpace>(0, 1), my_lambda);
    ASSERT_EQ(last_parallel_scan, typeid_name(my_lambda));
#ifndef KOKKOS_ENABLE_CXX17
    ASSERT_FALSE(last_parallel_scan.starts_with("const "))
        << last_parallel_scan << " is const-qualified";
#endif

    auto const my_lambda_with_tag = KOKKOS_LAMBDA(WorkTag, int, float&, bool){};
    Kokkos::parallel_scan(my_label,
                          Kokkos::RangePolicy<ExecutionSpace, WorkTag>(0, 1),
                          my_lambda_with_tag);
    ASSERT_EQ(last_parallel_scan, my_label);

    Kokkos::parallel_scan(Kokkos::RangePolicy<ExecutionSpace, WorkTag>(0, 1),
                          my_lambda_with_tag);
    ASSERT_EQ(last_parallel_scan,
              typeid_name(my_lambda_with_tag) + "/" + typeid_name(WorkTag{}));
#ifndef KOKKOS_ENABLE_CXX17
    ASSERT_FALSE(last_parallel_scan.starts_with("const "))
        << last_parallel_scan << " is const-qualified";
#endif
  }

  Kokkos::Tools::Experimental::set_begin_parallel_scan_callback(nullptr);
}

TEST(kokkosp, kernel_name_parallel_for) { test_kernel_name_parallel_for(); }

TEST(kokkosp, kernel_name_parallel_reduce) {
  test_kernel_name_parallel_reduce();
}

TEST(kokkosp, kernel_name_parallel_scan) { test_kernel_name_parallel_scan(); }

TEST(kokkosp, kernel_name_internal) {
  struct ThisType {};
  {
    std::string const label("my_label");
    Kokkos::Impl::ParallelConstructName<ThisType, void> pcn(label);
    ASSERT_EQ(pcn.get(), label);
    std::string const empty_label("");
    Kokkos::Impl::ParallelConstructName<ThisType, void> empty_pcn(empty_label);
    ASSERT_EQ(empty_pcn.get(), typeid_name(ThisType{}));
  }
  {
    std::string const label("my_label");
    Kokkos::Impl::ParallelConstructName<ThisType, WorkTag> pcn(label);
    ASSERT_EQ(pcn.get(), label);
    std::string const empty_label("");
    Kokkos::Impl::ParallelConstructName<ThisType, WorkTag> empty_pcn(
        empty_label);
    ASSERT_EQ(empty_pcn.get(),
              typeid_name(ThisType{}) + "/" + typeid_name(WorkTag{}));
  }
}

}  // namespace