-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathasync_utils.cpp
236 lines (214 loc) · 7.92 KB
/
async_utils.cpp
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
#include "taichi/program/async_utils.h"
#include "taichi/ir/analysis.h"
#include "taichi/ir/ir.h"
#include "taichi/ir/statements.h"
#include "taichi/program/ir_bank.h"
#include "taichi/program/kernel.h"
TLANG_NAMESPACE_BEGIN
std::unique_ptr<IRNode> IRHandle::clone() const {
TI_AUTO_PROF
// TODO: remove get_kernel() here
return irpass::analysis::clone(const_cast<IRNode *>(ir_), ir_->get_kernel());
}
TaskLaunchRecord::TaskLaunchRecord() : kernel(nullptr), ir_handle(nullptr, 0) {
}
std::atomic<int> TaskLaunchRecord::task_counter = 0;
TaskLaunchRecord::TaskLaunchRecord(Context context,
Kernel *kernel,
IRHandle ir_handle)
: context(context), kernel(kernel), ir_handle(ir_handle) {
id = task_counter++;
TI_ASSERT(ir_handle.ir()->get_kernel() != nullptr);
}
OffloadedStmt *TaskLaunchRecord::stmt() const {
TI_ASSERT(ir_handle.ir());
return const_cast<IRNode *>(ir_handle.ir())->as<OffloadedStmt>();
}
bool TaskLaunchRecord::empty() const {
return ir_handle.ir() == nullptr;
}
void TaskMeta::print() const {
fmt::print("TaskMeta\n name {}\n", name);
fmt::print(" type {}\n", OffloadedStmt::task_type_name(type));
if (snode != nullptr) {
fmt::print(" snode {}\n", snode->get_node_type_name_hinted());
} else {
fmt::print(" snode nullptr\n");
}
if (!input_states.empty()) {
fmt::print(" input states:\n ");
for (auto s : input_states) {
fmt::print("{} ", s.name());
}
fmt::print("\n");
}
if (!output_states.empty()) {
fmt::print(" output states:\n ");
for (auto s : output_states) {
fmt::print("{} ", s.name());
}
fmt::print("\n");
}
std::vector<SNode *> element_wise_snodes, non_element_wise_snodes;
for (auto s : element_wise) {
if (s.second) {
element_wise_snodes.push_back(s.first);
} else {
non_element_wise_snodes.push_back(s.first);
}
}
if (!element_wise_snodes.empty()) {
fmt::print(" element-wise snodes:\n ");
for (auto s : element_wise_snodes) {
fmt::print("{} ", s->get_node_type_name_hinted());
}
fmt::print("\n");
}
if (!non_element_wise_snodes.empty()) {
fmt::print(" non-element-wise snodes:\n ");
for (auto s : non_element_wise_snodes) {
fmt::print("{} ", s->get_node_type_name_hinted());
}
fmt::print("\n");
}
}
TaskMeta *get_task_meta(IRBank *ir_bank, const TaskLaunchRecord &t) {
TI_AUTO_PROF
// TODO: this function should ideally take only an IRNode
static std::mutex mut;
std::lock_guard<std::mutex> guard(mut);
auto &meta_bank = ir_bank->meta_bank_;
if (meta_bank.find(t.ir_handle) != meta_bank.end()) {
return &meta_bank[t.ir_handle];
}
using namespace irpass::analysis;
TaskMeta meta;
// TODO: this is an abuse since it gathers nothing...
auto *root_stmt = t.stmt();
meta.name = t.kernel->name + "_" +
OffloadedStmt::task_type_name(root_stmt->task_type);
meta.type = root_stmt->task_type;
gather_statements(root_stmt, [&](Stmt *stmt) {
if (auto global_load = stmt->cast<GlobalLoadStmt>()) {
if (auto ptr = global_load->ptr->cast<GlobalPtrStmt>()) {
for (auto &snode : ptr->snodes.data) {
meta.input_states.emplace(snode, AsyncState::Type::value);
}
}
}
// Note: since global store may only partially modify a value state, the
// result (which contains the modified and unmodified part) actually needs a
// read from the previous version of the value state.
//
// I.e.,
// output_value_state = merge(input_value_state, written_part)
//
// Therefore we include the value state in input_states.
//
// The only exception is that the task may completely overwrite the value
// state (e.g., for i in x: x[i] = 0). However, for now we are not yet
// able to detect that case, so we are being conservative here.
if (auto global_store = stmt->cast<GlobalStoreStmt>()) {
if (auto ptr = global_store->ptr->cast<GlobalPtrStmt>()) {
for (auto &snode : ptr->snodes.data) {
meta.input_states.emplace(snode, AsyncState::Type::value);
meta.output_states.emplace(snode, AsyncState::Type::value);
}
}
}
if (auto global_atomic = stmt->cast<AtomicOpStmt>()) {
if (auto ptr = global_atomic->dest->cast<GlobalPtrStmt>()) {
for (auto &snode : ptr->snodes.data) {
meta.input_states.emplace(snode, AsyncState::Type::value);
meta.output_states.emplace(snode, AsyncState::Type::value);
}
}
}
if (auto ptr = stmt->cast<GlobalPtrStmt>()) {
if (ptr->activate) {
for (auto &snode : ptr->snodes.data) {
auto s = snode;
while (s) {
if (!s->is_path_all_dense) {
meta.input_states.emplace(s, AsyncState::Type::mask);
meta.output_states.emplace(s, AsyncState::Type::mask);
}
s = s->parent;
}
}
}
for (auto &snode : ptr->snodes.data) {
if (ptr->is_element_wise(snode)) {
if (meta.element_wise.find(snode) == meta.element_wise.end()) {
meta.element_wise[snode] = true;
}
} else {
meta.element_wise[snode] = false;
}
}
}
if (auto clear_list = stmt->cast<ClearListStmt>()) {
meta.output_states.emplace(clear_list->snode, AsyncState::Type::list);
}
// TODO: handle SNodeOpStmt etc.
return false;
});
if (root_stmt->task_type == OffloadedStmt::listgen) {
TI_ASSERT(root_stmt->snode->parent);
meta.snode = root_stmt->snode;
meta.input_states.emplace(root_stmt->snode->parent, AsyncState::Type::list);
meta.input_states.emplace(root_stmt->snode, AsyncState::Type::list);
meta.input_states.emplace(root_stmt->snode, AsyncState::Type::mask);
meta.output_states.emplace(root_stmt->snode, AsyncState::Type::list);
} else if (root_stmt->task_type == OffloadedStmt::struct_for) {
meta.snode = root_stmt->snode;
meta.input_states.emplace(root_stmt->snode, AsyncState::Type::list);
}
meta_bank[t.ir_handle] = meta;
return &meta_bank[t.ir_handle];
}
TaskFusionMeta get_task_fusion_meta(IRBank *bank, const TaskLaunchRecord &t) {
TI_AUTO_PROF
// TODO: this function should ideally take only an IRNode
auto &fusion_meta_bank = bank->fusion_meta_bank_;
if (fusion_meta_bank.find(t.ir_handle) != fusion_meta_bank.end()) {
return fusion_meta_bank[t.ir_handle];
}
TaskFusionMeta meta{};
if (t.kernel->is_accessor) {
// SNode accessors can't be fused.
// TODO: just avoid snode accessors going into the async engine
return fusion_meta_bank[t.ir_handle] = TaskFusionMeta();
}
meta.kernel = t.kernel;
if (t.kernel->args.empty() && t.kernel->rets.empty()) {
meta.kernel = nullptr;
}
auto *task = t.stmt();
meta.type = task->task_type;
if (task->task_type == OffloadedStmt::struct_for) {
meta.snode = task->snode;
meta.block_dim = task->block_dim;
} else if (task->task_type == OffloadedStmt::range_for) {
// TODO: a few problems with the range-for test condition:
// 1. This could incorrectly fuse two range-for kernels that have
// different sizes, but then the loop ranges get padded to the same
// power-of-two (E.g. maybe a side effect when a struct-for is demoted
// to range-for).
// 2. It has also fused range-fors that have the same linear range,
// but are of different dimensions of loop indices, e.g. (16, ) and
// (4, 4).
if (!task->const_begin || !task->const_end) {
// Do not fuse range-for tasks with variable ranges for now.
return fusion_meta_bank[t.ir_handle] = TaskFusionMeta();
}
meta.begin_value = task->begin_value;
meta.end_value = task->end_value;
} else if (task->task_type != OffloadedStmt::serial) {
// Do not fuse gc/listgen tasks.
return fusion_meta_bank[t.ir_handle] = TaskFusionMeta();
}
meta.fusible = true;
return fusion_meta_bank[t.ir_handle] = meta;
}
TLANG_NAMESPACE_END