forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_Serial_Task.hpp
222 lines (169 loc) · 7.13 KB
/
Kokkos_Serial_Task.hpp
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
//@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_IMPL_SERIAL_TASK_HPP
#define KOKKOS_IMPL_SERIAL_TASK_HPP
#include <Kokkos_Macros.hpp>
#if defined(KOKKOS_ENABLE_TASKDAG)
#include <Kokkos_TaskScheduler_fwd.hpp>
#include <Serial/Kokkos_Serial.hpp>
#include <impl/Kokkos_HostThreadTeam.hpp>
#include <impl/Kokkos_TaskQueue.hpp>
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
namespace Kokkos {
namespace Impl {
template <class QueueType>
class TaskQueueSpecialization<SimpleTaskScheduler<Kokkos::Serial, QueueType>> {
public:
// Note: Scheduler may be an incomplete type at class scope (but not inside
// of the methods, obviously)
using execution_space = Kokkos::Serial;
using memory_space = Kokkos::HostSpace;
using scheduler_type = SimpleTaskScheduler<Kokkos::Serial, QueueType>;
using member_type =
TaskTeamMemberAdapter<HostThreadTeamMember<Kokkos::Serial>,
scheduler_type>;
static void execute(scheduler_type const& scheduler) {
using task_base_type = typename scheduler_type::task_base_type;
auto const& serial_execution_space = scheduler.get_execution_space();
// Set default buffers
serial_execution_space.impl_internal_space_instance()
->resize_thread_team_data(0, /* global reduce buffer */
512, /* team reduce buffer */
0, /* team shared buffer */
0 /* thread local buffer */
);
auto& self = serial_execution_space.impl_internal_space_instance()
->m_thread_team_data;
auto& queue = scheduler.queue();
auto team_scheduler = scheduler.get_team_scheduler(0);
member_type member(scheduler, self);
auto current_task = OptionalRef<task_base_type>(nullptr);
while (!queue.is_done()) {
// Each team lead attempts to acquire either a thread team task
// or a single thread task for the team.
// pop a task off
current_task = queue.pop_ready_task(team_scheduler.team_scheduler_info());
// run the task
if (current_task) {
current_task->as_runnable_task().run(member);
// Respawns are handled in the complete function
queue.complete((*std::move(current_task)).as_runnable_task(),
team_scheduler.team_scheduler_info());
}
}
}
static constexpr uint32_t get_max_team_count(
execution_space const&) noexcept {
return 1;
}
template <typename TaskType>
static void get_function_pointer(typename TaskType::function_type& ptr,
typename TaskType::destroy_type& dtor) {
ptr = TaskType::apply;
dtor = TaskType::destroy;
}
};
//----------------------------------------------------------------------------
template <class Scheduler>
class TaskQueueSpecializationConstrained<
Scheduler,
std::enable_if_t<std::is_same<typename Scheduler::execution_space,
Kokkos::Serial>::value>> {
public:
// Note: Scheduler may be an incomplete type at class scope (but not inside
// of the methods, obviously)
using execution_space = Kokkos::Serial;
using memory_space = Kokkos::HostSpace;
using scheduler_type = Scheduler;
using member_type =
TaskTeamMemberAdapter<HostThreadTeamMember<Kokkos::Serial>,
scheduler_type>;
static void iff_single_thread_recursive_execute(
scheduler_type const& scheduler) {
using task_base_type = TaskBase;
using queue_type = typename scheduler_type::queue_type;
auto* const end = reinterpret_cast<task_base_type*>(task_base_type::EndTag);
execution_space serial_execution_space;
auto& data = serial_execution_space.impl_internal_space_instance()
->m_thread_team_data;
member_type exec(scheduler, data);
// Loop until no runnable task
task_base_type* task = end;
auto* const queue = scheduler.m_queue;
do {
task = end;
for (int i = 0; i < queue_type::NumQueue && end == task; ++i) {
for (int j = 0; j < 2 && end == task; ++j) {
task = queue_type::pop_ready_task(&queue->m_ready[i][j]);
}
}
if (end == task) break;
(*task->m_apply)(task, &exec);
queue->complete(task);
} while (1);
}
static void execute(scheduler_type const& scheduler) {
using task_base_type = TaskBase;
using queue_type = typename scheduler_type::queue_type;
auto* const end = reinterpret_cast<task_base_type*>(task_base_type::EndTag);
execution_space serial_execution_space;
// Set default buffers
serial_execution_space.impl_internal_space_instance()
->resize_thread_team_data(0, /* global reduce buffer */
512, /* team reduce buffer */
0, /* team shared buffer */
0 /* thread local buffer */
);
auto* const queue = scheduler.m_queue;
auto& data = serial_execution_space.impl_internal_space_instance()
->m_thread_team_data;
member_type exec(scheduler, data);
// Loop until all queues are empty
while (0 < queue->m_ready_count) {
task_base_type* task = end;
for (int i = 0; i < queue_type::NumQueue && end == task; ++i) {
for (int j = 0; j < 2 && end == task; ++j) {
task = queue_type::pop_ready_task(&queue->m_ready[i][j]);
}
}
if (end != task) {
// pop_ready_task resulted in lock == task->m_next
// In the executing state
(*task->m_apply)(task, &exec);
// If a respawn then re-enqueue otherwise the task is complete
// and all tasks waiting on this task are updated.
queue->complete(task);
} else if (0 != queue->m_ready_count) {
Kokkos::abort("TaskQueue<Serial>::execute ERROR: ready_count");
}
}
}
template <typename TaskType>
static void get_function_pointer(typename TaskType::function_type& ptr,
typename TaskType::destroy_type& dtor) {
ptr = TaskType::apply;
dtor = TaskType::destroy;
}
};
extern template class TaskQueue<Kokkos::Serial,
typename Kokkos::Serial::memory_space>;
} // namespace Impl
} // namespace Kokkos
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#endif /* #if defined( KOKKOS_ENABLE_TASKDAG ) */
#endif /* #ifndef KOKKOS_IMPL_SERIAL_TASK_HPP */