开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 3

xiongying/Halide

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
main
分支 (1093)
标签 (17)
main
xtensa-codegen
vksnk/dma-limit-channels
rootjalex/trs-codegen-cross
abadams/fix_7374
abadams/remove_hack_from_gpu_only_aottest
srj/gpu-cache
srj/generator_aot_gpu_multi_context_threaded
srj/xtensa-merge
abadams/vector_scan
abadams/fix_7365
darya-ver/ir-viz
vulkan-phase2-runtime
srj/param-map-deprecation
srj/rt-return-types
srj/main-vs2022
release/15.x
srj/param-map
abadams/ir_builder_unique_ptr
vksnk/restrict
v14.0.0
v13.0.4
v13.0.3
v13.0.2
v13.0.1
v13.0.0
v12.0.1
v12.0.0
v11.0.1
v11.0.0
v10.0.1
v10.0.0
release_2019_08_27
release_8.0.0
v8.0.0
release_2018_02_15
release_2013_11_11
main
分支 (1093)
标签 (17)
main
xtensa-codegen
vksnk/dma-limit-channels
rootjalex/trs-codegen-cross
abadams/fix_7374
abadams/remove_hack_from_gpu_only_aottest
srj/gpu-cache
srj/generator_aot_gpu_multi_context_threaded
srj/xtensa-merge
abadams/vector_scan
abadams/fix_7365
darya-ver/ir-viz
vulkan-phase2-runtime
srj/param-map-deprecation
srj/rt-return-types
srj/main-vs2022
release/15.x
srj/param-map
abadams/ir_builder_unique_ptr
vksnk/restrict
v14.0.0
v13.0.4
v13.0.3
v13.0.2
v13.0.1
v13.0.0
v12.0.1
v12.0.0
v11.0.1
v11.0.0
v10.0.1
v10.0.0
release_2019_08_27
release_8.0.0
v8.0.0
release_2018_02_15
release_2013_11_11
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
main
分支 (1093)
标签 (17)
main
xtensa-codegen
vksnk/dma-limit-channels
rootjalex/trs-codegen-cross
abadams/fix_7374
abadams/remove_hack_from_gpu_only_aottest
srj/gpu-cache
srj/generator_aot_gpu_multi_context_threaded
srj/xtensa-merge
abadams/vector_scan
abadams/fix_7365
darya-ver/ir-viz
vulkan-phase2-runtime
srj/param-map-deprecation
srj/rt-return-types
srj/main-vs2022
release/15.x
srj/param-map
abadams/ir_builder_unique_ptr
vksnk/restrict
v14.0.0
v13.0.4
v13.0.3
v13.0.2
v13.0.1
v13.0.0
v12.0.1
v12.0.0
v11.0.1
v11.0.0
v10.0.1
v10.0.0
release_2019_08_27
release_8.0.0
v8.0.0
release_2018_02_15
release_2013_11_11
Halide
/
src
/
LowerParallelTasks.cpp
Halide
/
src
/
LowerParallelTasks.cpp
LowerParallelTasks.cpp 17.27 KB
一键复制 编辑 原始数据 按行查看 历史
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
#include "LowerParallelTasks.h"
#include <string>
#include "Argument.h"
#include "Closure.h"
#include "DebugArguments.h"
#include "ExprUsesVar.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "Module.h"
#include "Param.h"
#include "Simplify.h"
namespace Halide {
namespace Internal {
namespace {
LoweredArgument make_scalar_arg(const std::string &name, const Type &type) {
return LoweredArgument(name, Argument::Kind::InputScalar, type, 0, ArgumentEstimates());
}
template<typename T>
LoweredArgument make_scalar_arg(const std::string &name) {
return make_scalar_arg(name, type_of<T>());
}
std::string task_debug_name(const std::pair<std::string, int> &prefix) {
if (prefix.second <= 1) {
return prefix.first;
} else {
return prefix.first + "_" + std::to_string(prefix.second - 1);
}
}
void add_fork(std::pair<std::string, int> &prefix) {
if (prefix.second == 0) {
prefix.first += ".fork";
}
prefix.second++;
}
void add_suffix(std::pair<std::string, int> &prefix, const std::string &suffix) {
if (prefix.second > 1) {
prefix.first += "_" + std::to_string(prefix.second - 1);
prefix.second = 0;
}
prefix.first += suffix;
}
// TODO(zvookin|abadams): This makes multiple passes over the
// IR to cover each node. (One tree walk produces the min
// thread count for all nodes, but we redo each subtree when
// compiling a given node.) Ideally we'd move to a lowering pass
// that converts our parallelism constructs to Call nodes, or
// direct hardware operations in some cases.
// Also, this code has to exactly mirror the logic in get_parallel_tasks.
// It would be better to do one pass on the tree and centralize the task
// deduction logic in one place.
class MinThreads : public IRVisitor {
using IRVisitor::visit;
std::pair<Stmt, int> skip_acquires(Stmt first) {
int count = 0;
while (first.defined()) {
const Acquire *acq = first.as<Acquire>();
if (acq == nullptr) {
break;
}
count++;
first = acq->body;
}
return {first, count};
}
void visit(const Fork *op) override {
int total_threads = 0;
int direct_acquires = 0;
// Take the sum of min threads across all
// cascaded Fork nodes.
const Fork *node = op;
while (node != nullptr) {
result = 0;
auto after_acquires = skip_acquires(node->first);
direct_acquires += after_acquires.second;
after_acquires.first.accept(this);
total_threads += result;
const Fork *continued_branches = node->rest.as<Fork>();
if (continued_branches == nullptr) {
result = 0;
after_acquires = skip_acquires(node->rest);
direct_acquires += after_acquires.second;
after_acquires.first.accept(this);
total_threads += result;
}
node = continued_branches;
}
if (direct_acquires == 0 && total_threads == 0) {
result = 0;
} else {
result = total_threads + 1;
}
}
void visit(const For *op) override {
result = 0;
if (op->for_type == ForType::Parallel) {
IRVisitor::visit(op);
if (result > 0) {
result += 1;
}
} else if (op->for_type == ForType::Serial) {
auto after_acquires = skip_acquires(op->body);
if (after_acquires.second > 0 &&
!expr_uses_var(op->body.as<Acquire>()->count, op->name)) {
after_acquires.first.accept(this);
result++;
} else {
IRVisitor::visit(op);
}
} else {
IRVisitor::visit(op);
}
}
// This is a "standalone" Acquire and will result in its own task.
// Treat it requiring one more thread than its body.
void visit(const Acquire *op) override {
result = 0;
auto after_inner_acquires = skip_acquires(op);
after_inner_acquires.first.accept(this);
result = result + 1;
}
void visit(const Block *op) override {
result = 0;
op->first.accept(this);
int result_first = result;
result = 0;
op->rest.accept(this);
result = std::max(result, result_first);
}
public:
int result = 0;
};
int calculate_min_threads(const Stmt &body) {
MinThreads min_threads;
body.accept(&min_threads);
return min_threads.result;
}
struct LowerParallelTasks : public IRMutator {
/** Codegen a call to do_parallel_tasks */
struct ParallelTask {
Stmt body;
struct SemAcquire {
Expr semaphore;
Expr count;
};
std::vector<SemAcquire> semaphores;
std::string loop_var;
Expr min, extent;
Expr serial;
std::string name;
};
using IRMutator::visit;
Stmt visit(const For *op) override {
const Acquire *acquire = op->body.as<Acquire>();
if (op->for_type == ForType::Parallel ||
(op->for_type == ForType::Serial &&
acquire &&
!expr_uses_var(acquire->count, op->name))) {
return do_as_parallel_task(op);
}
return IRMutator::visit(op);
}
Stmt visit(const Acquire *op) override {
return do_as_parallel_task(op);
}
Stmt visit(const Fork *op) override {
return do_as_parallel_task(op);
}
Stmt rewrite_parallel_tasks(const std::vector<ParallelTask> &tasks) {
Stmt body;
Closure closure;
for (const auto &t : tasks) {
Stmt s = t.body;
if (!t.loop_var.empty()) {
s = LetStmt::make(t.loop_var, 0, s);
}
closure.include(s);
}
// The same name can appear as a var and a buffer. Remove the var name in this case.
for (auto const &b : closure.buffers) {
closure.vars.erase(b.first);
}
int num_tasks = (int)(tasks.size());
std::vector<Expr> tasks_array_args;
tasks_array_args.reserve(num_tasks * 9);
std::string closure_name = unique_name("parallel_closure");
Expr closure_struct_allocation = closure.pack_into_struct();
Expr closure_struct = Variable::make(Handle(), closure_name);
const bool has_task_parent = !task_parents.empty() && task_parents.top_ref().defined();
Expr result;
for (int i = 0; i < num_tasks; i++) {
ParallelTask t = tasks[i];
const int min_threads = calculate_min_threads(t.body);
// Decide if we're going to call do_par_for or
// do_parallel_tasks. halide_do_par_for is simpler, but
// assumes a bunch of things. Programs that don't use async
// can also enter the task system via do_par_for.
const bool use_parallel_for = (num_tasks == 1 &&
min_threads == 0 &&
t.semaphores.empty() &&
!has_task_parent);
Expr closure_task_parent;
const std::string closure_arg_name = unique_name("closure_arg");
auto closure_arg = make_scalar_arg<uint8_t *>(closure_arg_name);
Type closure_function_type;
std::vector<LoweredArgument> closure_args(use_parallel_for ? 3 : 5);
closure_args[0] = make_scalar_arg<void *>("__user_context");
if (use_parallel_for) {
// The closure will be a halide_task_t, with arguments like:
//
// typedef int (*halide_task_t)(void *user_context, int task_number, uint8_t *closure);
//
closure_function_type = type_of<halide_task_t>();
closure_args[1] = make_scalar_arg<int32_t>(t.loop_var);
closure_args[2] = closure_arg;
// closure_task_parent remains undefined here.
} else {
// The closure will be a halide_loop_task_t, with arguments like:
//
// typedef int (*halide_loop_task_t)(void *user_context, int min, int extent, uint8_t *closure, void *task_parent);
//
closure_function_type = type_of<halide_loop_task_t>();
const std::string closure_task_parent_name = unique_name("__task_parent");
closure_task_parent = Variable::make(type_of<void *>(), closure_task_parent_name);
// We peeled off a loop. Wrap a new loop around the body
// that just does the slice given by the arguments.
std::string loop_min_name = unique_name('t');
std::string loop_extent_name = unique_name('t');
if (!t.loop_var.empty()) {
t.body = For::make(t.loop_var,
Variable::make(Int(32), loop_min_name),
Variable::make(Int(32), loop_extent_name),
ForType::Serial,
DeviceAPI::None,
t.body);
} else {
internal_assert(is_const_one(t.extent));
}
closure_args[1] = make_scalar_arg<int32_t>(loop_min_name);
closure_args[2] = make_scalar_arg<int32_t>(loop_extent_name);
closure_args[3] = closure_arg;
closure_args[4] = make_scalar_arg<void *>(closure_task_parent_name);
}
{
ScopedValue<std::string> save_name(function_name, t.name);
task_parents.push(closure_task_parent);
t.body = mutate(t.body);
task_parents.pop();
}
const std::string new_function_name = c_print_name(unique_name(t.name), false);
{
Expr closure_arg_var = Variable::make(closure_struct_allocation.type(), closure_arg_name);
Stmt wrapped_body = closure.unpack_from_struct(closure_arg_var, t.body);
// TODO(zvookin): Figure out how we want to handle name mangling of closures.
// For now, the C++ backend makes them extern "C" so they have to be NameMangling::C.
LoweredFunc closure_func{new_function_name, closure_args, std::move(wrapped_body), LinkageType::Internal, NameMangling::C};
if (target.has_feature(Target::Debug)) {
debug_arguments(&closure_func, target);
}
closure_implementations.emplace_back(std::move(closure_func));
}
// Codegen will add user_context for us
// Prefix the function name with "::" as we would in C++ to make
// it clear we're talking about something in global scope in
// case some joker names an intermediate Func or Var the same
// name as the pipeline. This prefix works transparently in the
// C++ backend.
Expr new_function_name_arg = Variable::make(closure_function_type, "::" + new_function_name);
Expr closure_struct_arg = Cast::make(type_of<uint8_t *>(), closure_struct);
if (use_parallel_for) {
std::vector<Expr> args = {
std::move(new_function_name_arg),
t.min,
t.extent,
std::move(closure_struct_arg)};
result = Call::make(Int(32), "halide_do_par_for", args, Call::Extern);
} else {
const int semaphores_size = (int)t.semaphores.size();
std::vector<Expr> semaphore_args(semaphores_size * 2);
for (int i = 0; i < semaphores_size; i++) {
semaphore_args[i * 2] = t.semaphores[i].semaphore;
semaphore_args[i * 2 + 1] = t.semaphores[i].count;
}
Expr semaphores_array = Call::make(type_of<halide_semaphore_acquire_t *>(), Call::make_struct, semaphore_args, Call::PureIntrinsic);
tasks_array_args.emplace_back(std::move(new_function_name_arg));
tasks_array_args.emplace_back(std::move(closure_struct_arg));
tasks_array_args.emplace_back(StringImm::make(t.name));
tasks_array_args.emplace_back(std::move(semaphores_array));
tasks_array_args.emplace_back((int)t.semaphores.size());
tasks_array_args.emplace_back(t.min);
tasks_array_args.emplace_back(t.extent);
tasks_array_args.emplace_back(min_threads);
tasks_array_args.emplace_back(Cast::make(Bool(), t.serial));
}
}
if (!tasks_array_args.empty()) {
// Allocate task list array
Expr tasks_list = Call::make(type_of<halide_parallel_task_t *>(), Call::make_struct, tasks_array_args, Call::PureIntrinsic);
Expr user_context = Call::make(type_of<void *>(), Call::get_user_context, {}, Call::PureIntrinsic);
Expr task_parent = has_task_parent ? task_parents.top() : make_zero(Handle());
result = Call::make(Int(32), "halide_do_parallel_tasks",
{user_context, make_const(Int(32), num_tasks), tasks_list, task_parent},
Call::Extern);
}
std::string closure_result_name = unique_name("closure_result");
Expr closure_result = Variable::make(Int(32), closure_result_name);
Stmt stmt = AssertStmt::make(closure_result == 0, closure_result);
stmt = LetStmt::make(closure_result_name, result, stmt);
stmt = LetStmt::make(closure_name, closure_struct_allocation, stmt);
return stmt;
}
void get_parallel_tasks(const Stmt &s, std::vector<ParallelTask> &result, std::pair<std::string, int> prefix) {
const For *loop = s.as<For>();
const Acquire *acquire = loop ? loop->body.as<Acquire>() : s.as<Acquire>();
if (const Fork *f = s.as<Fork>()) {
add_fork(prefix);
get_parallel_tasks(f->first, result, prefix);
get_parallel_tasks(f->rest, result, prefix);
} else if (!loop && acquire) {
const Variable *v = acquire->semaphore.as<Variable>();
internal_assert(v);
add_suffix(prefix, "." + v->name);
ParallelTask t{s, {}, "", 0, 1, const_false(), task_debug_name(prefix)};
while (acquire) {
t.semaphores.push_back({acquire->semaphore, acquire->count});
t.body = acquire->body;
acquire = t.body.as<Acquire>();
}
result.emplace_back(std::move(t));
} else if (loop && loop->for_type == ForType::Parallel) {
add_suffix(prefix, ".par_for." + loop->name);
ParallelTask t{loop->body, {}, loop->name, loop->min, loop->extent, const_false(), task_debug_name(prefix)};
result.emplace_back(std::move(t));
} else if (loop &&
loop->for_type == ForType::Serial &&
acquire &&
!expr_uses_var(acquire->count, loop->name)) {
const Variable *v = acquire->semaphore.as<Variable>();
internal_assert(v);
add_suffix(prefix, ".for." + v->name);
ParallelTask t{loop->body, {}, loop->name, loop->min, loop->extent, const_true(), task_debug_name(prefix)};
while (acquire) {
t.semaphores.push_back({acquire->semaphore, acquire->count});
t.body = acquire->body;
acquire = t.body.as<Acquire>();
}
result.emplace_back(std::move(t));
} else {
add_suffix(prefix, "." + std::to_string(result.size()));
ParallelTask t{s, {}, "", 0, 1, const_false(), task_debug_name(prefix)};
result.emplace_back(std::move(t));
}
}
Stmt do_as_parallel_task(const Stmt &s) {
std::vector<ParallelTask> tasks;
get_parallel_tasks(s, tasks, {function_name, 0});
return rewrite_parallel_tasks(tasks);
}
LowerParallelTasks(const std::string &name, const Target &t)
: function_name(name), target(t) {
}
std::string function_name;
const Target &target;
std::vector<LoweredFunc> closure_implementations;
SmallStack<Expr> task_parents;
};
} // namespace
Stmt lower_parallel_tasks(const Stmt &s, std::vector<LoweredFunc> &closure_implementations,
const std::string &name, const Target &t) {
LowerParallelTasks lowering_mutator(name, t);
Stmt result = lowering_mutator.mutate(s);
// Main body will be dumped as part of standard lowering debugging, but closures will not be.
if (debug::debug_level() >= 2) {
for (const auto &lf : lowering_mutator.closure_implementations) {
debug(2) << "lower_parallel_tasks generated closure lowered function " << lf.name << ":\n"
<< lf.body << "\n\n";
}
}
// Append to the end rather than replacing the list entirely.
closure_implementations.insert(closure_implementations.end(),
lowering_mutator.closure_implementations.begin(),
lowering_mutator.closure_implementations.end());
return result;
}
} // namespace Internal
} // namespace Halide
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

MIT计算机科学和人工智能实验室的研究人员创造出一种专门设计简化图像处理的程序语言Halide,源代码托管在GitHub上,目前二进制程序只支持Mac OS X和Ubuntu 12
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/VisionDeveloper/Halide.git
git@gitee.com:VisionDeveloper/Halide.git
VisionDeveloper
Halide
Halide
main
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /