File: test_taskdag.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 (235 lines) | stat: -rw-r--r-- 6,758 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
//@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 <iostream>

#include <Kokkos_Core.hpp>

#if !defined(KOKKOS_ENABLE_TASKDAG) || \
    defined(KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_THREADS)

int main() { return 0; }

#else

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <limits>

#include <Kokkos_Timer.hpp>

#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
// We allow using deprecated classes in this file
KOKKOS_IMPL_DISABLE_DEPRECATED_WARNINGS_PUSH()
#endif

using ExecSpace = Kokkos::DefaultExecutionSpace;

inline long eval_fib(long n) {
  constexpr long mask = 0x03;

  long fib[4] = {0, 1, 0, 0};

  for (long i = 2; i <= n; ++i) {
    fib[i & mask] = fib[(i - 1) & mask] + fib[(i - 2) & mask];
  }

  return fib[n & mask];
}

inline long fib_alloc_count(long n) {
  constexpr long mask = 0x03;

  long count[4] = {1, 1, 0, 0};

  for (long i = 2; i <= n; ++i) {
    count[i & mask] = 2  // this task plus the 'when_all' task
                      + count[(i - 1) & mask] + count[(i - 2) & mask];
  }

  return count[n & mask];
}

template <class Scheduler>
struct TestFib {
  using MemorySpace = typename Scheduler::memory_space;
  using MemberType  = typename Scheduler::member_type;
  using FutureType  = Kokkos::BasicFuture<long, Scheduler>;

  using value_type = long;

  FutureType dep[2];
  const value_type n;

  KOKKOS_INLINE_FUNCTION
  TestFib(const value_type arg_n) : dep{}, n(arg_n) {}

  KOKKOS_INLINE_FUNCTION
  void operator()(MemberType& member, value_type& result) noexcept {
    auto& sched = member.scheduler();
    if (n < 2) {
      result = n;
    } else if (!dep[0].is_null() && !dep[1].is_null()) {
      result = dep[0].get() + dep[1].get();
    } else {
      // Spawn new children and respawn myself to sum their results.
      // Spawn lower value at higher priority as it has a shorter
      // path to completion.

      dep[1] = Kokkos::task_spawn(
          Kokkos::TaskSingle(sched, Kokkos::TaskPriority::High),
          TestFib(n - 2));

      dep[0] = Kokkos::task_spawn(Kokkos::TaskSingle(sched), TestFib(n - 1));

      auto fib_all = sched.when_all(dep, 2);

      if (!dep[0].is_null() && !dep[1].is_null() && !fib_all.is_null()) {
        // High priority to retire this branch.
        Kokkos::respawn(this, fib_all, Kokkos::TaskPriority::High);
      } else {
        Kokkos::abort("Failed nested task spawn (allocation)");
      }
    }
  }
};

int main(int argc, char* argv[]) {
  static const char help[]         = "--help";
  static const char alloc_size[]   = "--alloc_size=";
  static const char super_size[]   = "--super_size=";
  static const char repeat_outer[] = "--repeat_outer=";
  static const char input_value[]  = "--input=";

  long total_alloc_size   = 1000000;
  int min_superblock_size = 10000;
  int test_repeat_outer   = 1;
  int fib_input           = 4;

  int ask_help = 0;

  for (int i = 1; i < argc; i++) {
    const char* const a = argv[i];

    if (!strncmp(a, help, strlen(help))) ask_help = 1;

    if (!strncmp(a, alloc_size, strlen(alloc_size)))
      total_alloc_size = atol(a + strlen(alloc_size));

    if (!strncmp(a, super_size, strlen(super_size)))
      min_superblock_size = std::stoi(a + strlen(super_size));

    if (!strncmp(a, repeat_outer, strlen(repeat_outer)))
      test_repeat_outer = std::stoi(a + strlen(repeat_outer));

    if (!strncmp(a, input_value, strlen(input_value)))
      fib_input = std::stoi(a + strlen(input_value));
  }

  const long fib_output   = eval_fib(fib_input);
  const long number_alloc = fib_alloc_count(fib_input);

  const unsigned min_block_size = 32;
  const unsigned max_block_size = 128;

  long task_count_max   = 0;
  long task_count_accum = 0;
  long test_result      = 0;

  if (ask_help) {
    std::cout << "command line options:"
              << " " << help << " " << alloc_size << "##"
              << " " << super_size << "##"
              << " " << input_value << "##"
              << " " << repeat_outer << "##" << std::endl;
    return -1;
  }

  using Scheduler = Kokkos::TaskSchedulerMultiple<ExecSpace>;

  using Functor = TestFib<Scheduler>;

  Kokkos::initialize(argc, argv);

  {
    Scheduler sched(Functor::MemorySpace(), total_alloc_size, min_block_size,
                    max_block_size, min_superblock_size);

    Functor::FutureType f =
        Kokkos::host_spawn(Kokkos::TaskSingle(sched), Functor(fib_input));

    Kokkos::wait(sched);

    test_result = f.get();

    // task_count_max   = sched.allocated_task_count_max();
    // task_count_accum = sched.allocated_task_count_accum();

    // if ( number_alloc != task_count_accum ) {
    //  std::cout << " number_alloc( " << number_alloc << " )"
    //            << " != task_count_accum( " << task_count_accum << " )"
    //            << std::endl ;
    //}

    if (fib_output != test_result) {
      std::cout << " answer( " << fib_output << " )"
                << " != result( " << test_result << " )" << std::endl;
    }

    if (fib_output != test_result) {  // || number_alloc != task_count_accum ) {
      printf("  TEST FAILED\n");
      return -1;
    }

    double min_time = std::numeric_limits<double>::max();
    double time_sum = 0;

    for (int i = 0; i < test_repeat_outer; ++i) {
      Kokkos::Timer timer;

      Functor::FutureType ftmp =
          Kokkos::host_spawn(Kokkos::TaskSingle(sched), Functor(fib_input));

      Kokkos::wait(sched);
      auto this_time = timer.seconds();
      min_time       = std::min(min_time, this_time);
      time_sum += this_time;
    }

    auto avg_time = time_sum / test_repeat_outer;

    printf(
        "\"taskdag: alloc super repeat input output task-accum task-max\" %ld "
        "%d %d %d %ld %ld %ld\n",
        total_alloc_size, min_superblock_size, test_repeat_outer, fib_input,
        fib_output, task_count_accum, task_count_max);

    printf("\"taskdag: time (min, avg)\" %g %g\n", min_time, avg_time);
    printf("\"taskdag: tasks per second (max, avg)\" %g %g\n",
           number_alloc / min_time, number_alloc / avg_time);
  }  // end scope to destroy scheduler prior to finalize

  Kokkos::finalize();

  return 0;
}

#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
KOKKOS_IMPL_DISABLE_DEPRECATED_WARNINGS_POP()
#endif

#endif