File: TestCuda_DebugSerialExecution.cpp

package info (click to toggle)
kokkos 5.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,148 kB
  • sloc: cpp: 225,388; sh: 1,250; python: 78; makefile: 16; fortran: 4; ansic: 2
file content (161 lines) | stat: -rw-r--r-- 4,656 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
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project

#include <Kokkos_Macros.hpp>
#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
import kokkos.core;
#else
#include <Kokkos_Core.hpp>
#endif
#include <TestCuda_Category.hpp>

namespace Test {

using ViewType = Kokkos::View<double*>;

struct TestForFunctor {
  ViewType a;
  ViewType b;

  TestForFunctor(int N) : a(ViewType("A", N)), b(ViewType("B", N)) {}

  KOKKOS_INLINE_FUNCTION
  void operator()(int i) const { a(i) = b(i); }

  double time_par_for() {
    Kokkos::Timer timer;
    Kokkos::parallel_for("CudaDebugSerialExecution::par_for", a.extent(0),
                         *this);
    Kokkos::fence();
    return timer.seconds();
  }
};

struct TestRedFunctor {
  ViewType a;
  ViewType b;

  TestRedFunctor(int N) : a(ViewType("A", N)), b(ViewType("B", N)) {}

  KOKKOS_INLINE_FUNCTION
  void operator()(int i, double& val) const { val += a(i) * b(i); }

  double time_par_red() {
    Kokkos::Timer timer;
    double dot;
    Kokkos::parallel_reduce("CudaDebugSerialExecution::par_red", a.extent(0),
                            *this, dot);
    Kokkos::fence();
    return timer.seconds();
  }
};

struct TestScanFunctor {
  ViewType a;
  ViewType b;

  TestScanFunctor(int N) : a(ViewType("A", N)), b(ViewType("B", N)) {}

  KOKKOS_INLINE_FUNCTION
  void operator()(int i, double& val, bool final) const {
    val += b(i);
    if (final) a(i) = val;
  }

  double time_par_scan() {
    Kokkos::Timer timer;
    double dot;
    Kokkos::parallel_scan("CudaDebugSerialExecution::par_scan", a.extent(0),
                          *this, dot);
    Kokkos::fence();
    return timer.seconds();
  }
};

TEST(cuda, debug_serial_execution) {
  double time_par_for_1, time_par_for_2, time_par_for_serial;
  double time_par_red_1, time_par_red_2, time_par_red_serial;
  double time_par_scan_1, time_par_scan_2, time_par_scan_serial;

  int N = 10000000;
  {
    TestForFunctor f(N);
    f.time_par_for();
    time_par_for_1 = f.time_par_for();
#ifdef KOKKOS_IMPL_DEBUG_CUDA_SERIAL_EXECUTION
    kokkos_impl_cuda_set_serial_execution(true);
#endif
    time_par_for_serial = f.time_par_for();
#ifdef KOKKOS_IMPL_DEBUG_CUDA_SERIAL_EXECUTION
    kokkos_impl_cuda_set_serial_execution(false);
#endif
    time_par_for_2 = f.time_par_for();

    bool passed_par_for =
#ifdef KOKKOS_IMPL_DEBUG_CUDA_SERIAL_EXECUTION
        (time_par_for_serial > time_par_for_1 * 20.0) &&
        (time_par_for_serial > time_par_for_2 * 20.0);
#else
        (time_par_for_serial < time_par_for_1 * 2.0) &&
        (time_par_for_serial < time_par_for_2 * 2.0);
#endif
    if (!passed_par_for)
      printf("Time For1: %lf For2: %lf ForSerial: %lf\n", time_par_for_1,
             time_par_for_2, time_par_for_serial);
    ASSERT_TRUE(passed_par_for);
  }
  {
    TestRedFunctor f(N);
    f.time_par_red();
    time_par_red_1 = f.time_par_red();
#ifdef KOKKOS_IMPL_DEBUG_CUDA_SERIAL_EXECUTION
    kokkos_impl_cuda_set_serial_execution(true);
#endif
    time_par_red_serial = f.time_par_red();
#ifdef KOKKOS_IMPL_DEBUG_CUDA_SERIAL_EXECUTION
    kokkos_impl_cuda_set_serial_execution(false);
#endif
    time_par_red_2 = f.time_par_red();

    bool passed_par_red =
#ifdef KOKKOS_IMPL_DEBUG_CUDA_SERIAL_EXECUTION
        (time_par_red_serial > time_par_red_1 * 2.0) &&
        (time_par_red_serial > time_par_red_2 * 2.0);
#else
        (time_par_red_serial < time_par_red_1 * 2.0) &&
        (time_par_red_serial < time_par_red_2 * 2.0);
#endif
    if (!passed_par_red)
      printf("Time Red1: %lf Red2: %lf RedSerial: %lf\n", time_par_red_1,
             time_par_red_2, time_par_red_serial);
    ASSERT_TRUE(passed_par_red);
  }
  {
    TestScanFunctor f(N);
    f.time_par_scan();
    time_par_scan_1 = f.time_par_scan();
#ifdef KOKKOS_IMPL_DEBUG_CUDA_SERIAL_EXECUTION
    kokkos_impl_cuda_set_serial_execution(true);
#endif
    time_par_scan_serial = f.time_par_scan();
#ifdef KOKKOS_IMPL_DEBUG_CUDA_SERIAL_EXECUTION
    kokkos_impl_cuda_set_serial_execution(false);
#endif
    time_par_scan_2 = f.time_par_scan();

    bool passed_par_scan =
#ifdef KOKKOS_IMPL_DEBUG_CUDA_SERIAL_EXECUTION
        (time_par_scan_serial > time_par_scan_1 * 2.0) &&
        (time_par_scan_serial > time_par_scan_2 * 2.0);
#else
        (time_par_scan_serial < time_par_scan_1 * 2.0) &&
        (time_par_scan_serial < time_par_scan_2 * 2.0);
#endif
    if (!passed_par_scan)
      printf("Time Scan1: %lf Scan2: %lf ScanSerial: %lf\n", time_par_scan_1,
             time_par_scan_2, time_par_scan_serial);
    ASSERT_TRUE(passed_par_scan);
  }
}

}  // namespace Test