开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 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
/
runtime
/
device_interface.cpp
Halide
/
src
/
runtime
/
device_interface.cpp
device_interface.cpp 28.96 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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
#include "device_interface.h"
#include "HalideRuntime.h"
#include "device_buffer_utils.h"
#include "printer.h"
#include "scoped_mutex_lock.h"
extern "C" {
extern void *malloc(size_t);
extern void free(void *);
}
namespace Halide {
namespace Runtime {
namespace Internal {
struct device_handle_wrapper {
uint64_t device_handle;
const halide_device_interface_t *interface;
};
// TODO: Coarser grained locking, also consider all things that need
// to be atomic with respect to each other. At present only
// halide_copy_to_host, halide_copy_to_device, and halide_buffer_copy
// are atomic with respect to each other. halide_device_malloc and
// halide_device_free are also candidates, but to do so they likely
// need to be able to do a copy internaly as well.
WEAK halide_mutex device_copy_mutex;
WEAK int copy_to_host_already_locked(void *user_context, struct halide_buffer_t *buf) {
if (!buf->device_dirty()) {
return 0; // my, that was easy
}
debug(user_context) << "copy_to_host_already_locked " << buf << " dev_dirty is true\n";
const halide_device_interface_t *interface = buf->device_interface;
if (buf->host_dirty()) {
debug(user_context) << "copy_to_host_already_locked " << buf << " dev_dirty and host_dirty are true\n";
return halide_error_code_copy_to_host_failed;
}
if (interface == nullptr) {
debug(user_context) << "copy_to_host_already_locked " << buf << " interface is nullptr\n";
return halide_error_code_no_device_interface;
}
int result = interface->impl->copy_to_host(user_context, buf);
if (result != 0) {
debug(user_context) << "copy_to_host_already_locked " << buf << " device copy_to_host returned an error\n";
return halide_error_code_copy_to_host_failed;
}
buf->set_device_dirty(false);
(void)halide_msan_annotate_buffer_is_initialized(user_context, buf); // ignore errors
return result;
}
} // namespace Internal
} // namespace Runtime
} // namespace Halide
namespace {
ALWAYS_INLINE int debug_log_and_validate_buf(void *user_context, const halide_buffer_t *buf_arg,
const char *routine) {
if (buf_arg == nullptr) {
return halide_error_buffer_is_null(user_context, routine);
}
const halide_buffer_t &buf(*buf_arg);
debug(user_context) << routine << " validating input buffer: " << buf << "\n";
bool device_interface_set = (buf.device_interface != nullptr);
bool device_set = (buf.device != 0);
if (device_set && !device_interface_set) {
return halide_error_no_device_interface(user_context);
}
if (device_interface_set && !device_set) {
return halide_error_device_interface_no_device(user_context);
}
bool host_dirty = buf.host_dirty();
bool device_dirty = buf.device_dirty();
if (host_dirty && device_dirty) {
return halide_error_host_and_device_dirty(user_context);
}
/* TODO: we could test:
* (device_set || !device_dirty)
* and:
* (buf.host != nullptr || !host_dirty)
* but these conditions can occur when freeing a buffer.
* It is perhaps prudent to mandate reseting the dirty bit when freeing
* the host field and setting it to nullptr, I am not convinced all code
* does that at present. The same could occur on the device side, though
* it is much more unlikely as halide_device_free does clear device_dirty.
* At present we're taking the side of caution and not adding these to the
* assertion.
*/
return 0;
}
} // namespace
extern "C" {
/** Release all data associated with the current GPU backend, in particular
* all resources (memory, texture, context handles) allocated by Halide. Must
* be called explicitly when using AOT compilation. */
WEAK void halide_device_release(void *user_context, const halide_device_interface_t *device_interface) {
device_interface->impl->device_release(user_context);
}
/** Copy image data from device memory to host memory. This must be called
* explicitly to copy back the results of a GPU-based filter. */
WEAK int halide_copy_to_host(void *user_context, struct halide_buffer_t *buf) {
ScopedMutexLock lock(&device_copy_mutex);
int result = debug_log_and_validate_buf(user_context, buf, "halide_copy_to_host");
if (result != 0) {
return result;
}
return copy_to_host_already_locked(user_context, buf);
}
/** Copy image data from host memory to device memory. This should not be
* called directly; Halide handles copying to the device automatically. */
WEAK int copy_to_device_already_locked(void *user_context,
struct halide_buffer_t *buf,
const halide_device_interface_t *device_interface) {
int result = 0;
result = debug_log_and_validate_buf(user_context, buf, "halide_copy_to_device");
if (result != 0) {
return result;
}
if (device_interface == nullptr) {
debug(user_context) << "halide_copy_to_device " << buf << " interface is nullptr\n";
if (buf->device_interface == nullptr) {
return halide_error_no_device_interface(user_context);
}
device_interface = buf->device_interface;
}
if (buf->device && buf->device_interface != device_interface) {
halide_error(user_context, "halide_copy_to_device does not support switching interfaces\n");
return halide_error_code_incompatible_device_interface;
}
if (buf->device == 0) {
result = halide_device_malloc(user_context, buf, device_interface);
if (result != 0) {
debug(user_context) << "halide_copy_to_device " << buf
<< " halide_copy_to_device call to halide_device_malloc failed\n";
return result;
}
}
if (buf->host_dirty()) {
debug(user_context) << "halide_copy_to_device " << buf << " host is dirty\n";
if (buf->device_dirty()) {
debug(user_context) << "halide_copy_to_device " << buf << " dev_dirty is true error\n";
return halide_error_code_copy_to_device_failed;
} else {
debug(user_context) << "halide_copy_to_device " << buf << " calling copy_to_device()\n";
result = device_interface->impl->copy_to_device(user_context, buf);
if (result == 0) {
buf->set_host_dirty(false);
} else {
debug(user_context) << "halide_copy_to_device "
<< buf << "device copy_to_device returned an error\n";
return halide_error_code_copy_to_device_failed;
}
}
} else {
debug(user_context) << "halide_copy_to_device " << buf << " skipped (host is not dirty)\n";
}
return 0;
}
WEAK int halide_copy_to_device(void *user_context,
struct halide_buffer_t *buf,
const halide_device_interface_t *device_interface) {
ScopedMutexLock lock(&device_copy_mutex);
return copy_to_device_already_locked(user_context, buf, device_interface);
}
/** Wait for current GPU operations to complete. Calling this explicitly
* should rarely be necessary, except maybe for profiling. */
WEAK int halide_device_sync(void *user_context, struct halide_buffer_t *buf) {
int result = debug_log_and_validate_buf(user_context, buf, "halide_device_sync");
if (result != 0) {
return result;
}
const halide_device_interface_t *device_interface = buf->device_interface;
if (device_interface == nullptr) {
return halide_error_no_device_interface(user_context);
}
result = device_interface->impl->device_sync(user_context, buf);
if (result) {
return halide_error_code_device_sync_failed;
} else {
return 0;
}
}
/** Allocate device memory to back a halide_buffer_t. */
WEAK int halide_device_malloc(void *user_context, struct halide_buffer_t *buf,
const halide_device_interface_t *device_interface) {
int result = debug_log_and_validate_buf(user_context, buf, "halide_device_malloc");
if (result != 0) {
return result;
}
debug(user_context) << "halide_device_malloc: target device interface " << device_interface << "\n";
const halide_device_interface_t *current_interface = buf->device_interface;
// halide_device_malloc does not support switching interfaces.
if (current_interface != nullptr && current_interface != device_interface) {
halide_error(user_context, "halide_device_malloc doesn't support switching interfaces\n");
return halide_error_code_incompatible_device_interface;
}
// Ensure code is not freed prematurely.
// TODO: Exception safety...
device_interface->impl->use_module();
result = device_interface->impl->device_malloc(user_context, buf);
device_interface->impl->release_module();
if (result) {
return halide_error_code_device_malloc_failed;
} else {
return 0;
}
}
/** Free any device memory associated with a halide_buffer_t. */
WEAK int halide_device_free(void *user_context, struct halide_buffer_t *buf) {
int result = debug_log_and_validate_buf(user_context, buf, "halide_device_free");
if (result != 0) {
return result;
}
const halide_device_interface_t *device_interface = buf->device_interface;
if (device_interface != nullptr) {
// Ensure interface is not freed prematurely.
// TODO: Exception safety...
device_interface->impl->use_module();
result = device_interface->impl->device_free(user_context, buf);
device_interface->impl->release_module();
halide_abort_if_false(user_context, buf->device == 0);
if (result) {
return halide_error_code_device_free_failed;
} else {
return 0;
}
}
buf->set_device_dirty(false);
return 0;
}
/** Free any device memory associated with a halide_buffer_t and ignore any
* error. Used when freeing as a destructor on an error. */
WEAK void halide_device_free_as_destructor(void *user_context, void *obj) {
struct halide_buffer_t *buf = (struct halide_buffer_t *)obj;
(void)halide_device_free(user_context, buf); // ignore errors
}
/** Allocate host and device memory to back a halide_buffer_t. Ideally this
* will be a zero copy setup, but the default implementation may
* separately allocate the host memory using halide_malloc and the
* device memory using halide_device_malloc. */
WEAK int halide_device_and_host_malloc(void *user_context, struct halide_buffer_t *buf,
const halide_device_interface_t *device_interface) {
int result = debug_log_and_validate_buf(user_context, buf, "halide_device_and_host_malloc");
if (result != 0) {
return result;
}
debug(user_context) << "halide_device_and_host_malloc: target device interface " << device_interface << "\n";
const halide_device_interface_t *current_interface = buf->device_interface;
// halide_device_malloc does not support switching interfaces.
if (current_interface != nullptr && current_interface != device_interface) {
halide_error(user_context, "halide_device_and_host_malloc doesn't support switching interfaces\n");
return halide_error_code_incompatible_device_interface;
}
// Ensure code is not freed prematurely.
// TODO: Exception safety...
device_interface->impl->use_module();
result = device_interface->impl->device_and_host_malloc(user_context, buf);
device_interface->impl->release_module();
if (result != 0) {
halide_error(user_context, "allocating host and device memory failed\n");
return halide_error_code_device_malloc_failed;
}
return 0;
}
/** Free host and device memory associated with a halide_buffer_t. */
WEAK int halide_device_and_host_free(void *user_context, struct halide_buffer_t *buf) {
int result = debug_log_and_validate_buf(user_context, buf, "halide_device_and_host_free");
if (result != 0) {
return result;
}
const halide_device_interface_t *device_interface = buf->device_interface;
if (device_interface != nullptr) {
// Ensure interface is not freed prematurely.
// TODO: Exception safety...
device_interface->impl->use_module();
result = device_interface->impl->device_and_host_free(user_context, buf);
device_interface->impl->release_module();
halide_abort_if_false(user_context, buf->device == 0);
if (result) {
return halide_error_code_device_free_failed;
} else {
return 0;
}
} else if (buf->host) {
// device_free must have been called on this buffer (which
// must be legal for the device interface that was
// used). We'd better still free the host pointer.
halide_free(user_context, buf->host);
buf->host = nullptr;
}
buf->set_device_dirty(false);
return 0;
}
WEAK int halide_default_device_and_host_malloc(void *user_context, struct halide_buffer_t *buf,
const halide_device_interface_t *device_interface) {
int result = debug_log_and_validate_buf(user_context, buf, "halide_default_device_and_host_malloc");
if (result != 0) {
return result;
}
size_t size = buf->size_in_bytes();
buf->host = (uint8_t *)halide_malloc(user_context, size);
if (buf->host == nullptr) {
return -1;
}
result = halide_device_malloc(user_context, buf, device_interface);
if (result != 0) {
halide_free(user_context, buf->host);
buf->host = nullptr;
}
return result;
}
WEAK int halide_default_device_and_host_free(void *user_context, struct halide_buffer_t *buf,
const halide_device_interface_t *device_interface) {
int result = debug_log_and_validate_buf(user_context, buf, "halide_default_device_and_host_free");
if (result != 0) {
return result;
}
result = halide_device_free(user_context, buf);
if (buf->host) {
halide_free(user_context, buf->host);
buf->host = nullptr;
}
buf->set_host_dirty(false);
buf->set_device_dirty(false);
return result;
}
WEAK int halide_device_wrap_native(void *user_context, struct halide_buffer_t *buf, uint64_t handle,
const halide_device_interface_t *device_interface) {
int result = debug_log_and_validate_buf(user_context, buf, "halide_device_wrap_native");
if (result != 0) {
return result;
}
const halide_device_interface_t *current_interface = buf->device_interface;
if (current_interface != nullptr && current_interface != device_interface) {
halide_error(user_context, "halide_device_wrap_native doesn't support switching interfaces\n");
return halide_error_code_incompatible_device_interface;
}
device_interface->impl->use_module();
buf->device_interface = device_interface;
result = device_interface->impl->wrap_native(user_context, buf, handle);
device_interface->impl->release_module();
if (result) {
return halide_error_code_device_malloc_failed;
}
return 0;
}
WEAK int halide_device_detach_native(void *user_context, struct halide_buffer_t *buf) {
int result = debug_log_and_validate_buf(user_context, buf, "halide_device_detach_native");
if (result != 0) {
return result;
}
const halide_device_interface_t *device_interface = buf->device_interface;
if (device_interface != nullptr) {
device_interface->impl->use_module();
result = device_interface->impl->detach_native(user_context, buf);
device_interface->impl->release_module();
halide_abort_if_false(user_context, buf->device == 0);
if (result) {
result = halide_error_code_device_detach_native_failed;
}
}
return result;
}
WEAK int halide_default_device_wrap_native(void *user_context, struct halide_buffer_t *buf, uint64_t handle) {
// No: this can return halide_error_no_device_interface for OGLC and HVX,
// since `device_interface` may be set but `device` may not be. Instead,
// just return halide_error_code_device_wrap_native_failed if buf->device isn't set.
// (And *don't* halide_assert() here, as that will abort. Just return an error and let the caller handle it.)
//
// int result = debug_log_and_validate_buf(user_context, buf, "halide_default_device_wrap_native");
// if (result != 0) {
// return result;
// }
if (buf->device != 0) {
return halide_error_code_device_wrap_native_failed;
}
buf->device_interface->impl->use_module();
buf->device = handle;
return 0;
}
WEAK int halide_default_device_detach_native(void *user_context, struct halide_buffer_t *buf) {
int result = debug_log_and_validate_buf(user_context, buf, "halide_default_device_detach_native");
if (result != 0) {
return result;
}
if (buf->device == 0) {
return 0;
}
buf->device_interface->impl->release_module();
buf->device = 0;
buf->device_interface = nullptr;
return 0;
}
/** Free any host and device memory associated with a halide_buffer_t and ignore any
* error. Used when freeing as a destructor on an error. */
WEAK void halide_device_and_host_free_as_destructor(void *user_context, void *obj) {
struct halide_buffer_t *buf = (struct halide_buffer_t *)obj;
halide_device_and_host_free(user_context, buf);
}
/** TODO: Find a way to elide host free without this hack. */
WEAK void halide_device_host_nop_free(void *user_context, void *obj) {
}
WEAK int halide_default_buffer_copy(void *user_context, struct halide_buffer_t *src,
const struct halide_device_interface_t *dst_device_interface,
struct halide_buffer_t *dst) {
debug(user_context)
<< "halide_default_buffer_copy\n"
<< " source: " << *src << "\n"
<< " dst_device_interface: " << (void *)dst_device_interface << "\n"
<< " dst: " << *dst << "\n";
// The right thing is that all devices have to support
// device-to-device and device-to/from-arbitrarty-pointer. This
// means there will always have to be a device specifc version of
// this function and the default can go away or fail. At present
// there are some devices, e.g. OpenGL and OpenGLCompute, for which
// this is not yet implemented.
return halide_error_code_device_buffer_copy_failed;
}
WEAK int halide_buffer_copy_already_locked(void *user_context, struct halide_buffer_t *src,
const struct halide_device_interface_t *dst_device_interface,
struct halide_buffer_t *dst) {
debug(user_context) << "halide_buffer_copy_already_locked called.\n";
int err = 0;
if (dst_device_interface && dst->device_interface &&
dst_device_interface != dst->device_interface) {
halide_error(user_context, "halide_buffer_copy does not support switching device interfaces");
return halide_error_code_incompatible_device_interface;
}
if (dst_device_interface && !dst->device) {
debug(user_context) << "halide_buffer_copy_already_locked: calling halide_device_malloc.\n";
err = halide_device_malloc(user_context, dst, dst_device_interface);
if (err) {
return err;
}
}
// First goal is correctness, the more interesting parts of which are:
// 1) Respect dirty bits so data is valid.
// 2) Don't infinitely recurse.
// Second goal is efficiency:
// 1) Try to do device-to-device if possible
// 2) Minimum number of copies and minimum amount of copying otherwise.
// 2a) e.g. for a device to different device buffer copy call where the copy must
// go through host memory, the src buffer may be left in device dirty state
// with the data copied through the destination host buffer to reduce the size
// of the copy.
// The device specifc runtime routine may return an error for the
// device to device case with separate devices. This code will attempt
// to decompose the call via bouncing through host memory.
//
// At present some cases, such as different devices where there is
// no host buffer, will return an error. Some of these could be
// handled by allocating temporary host memory.
//
// It is assumed that if two device runtimes have copy compatible buffers
// both will handle a copy between their types of buffers.
// Give more descriptive names to conditions.
const bool from_device_valid = (src->device != 0) &&
(src->host == nullptr || !src->host_dirty());
const bool to_device = dst_device_interface != nullptr;
const bool to_host = dst_device_interface == nullptr;
const bool from_host_exists = src->host != nullptr;
const bool from_host_valid = from_host_exists &&
(!src->device_dirty() || (src->device_interface == nullptr));
const bool to_host_exists = dst->host != nullptr;
if (to_host && !to_host_exists) {
return halide_error_code_host_is_null;
}
// If a device to device copy is requested, try to do it directly.
err = halide_error_code_incompatible_device_interface;
if (from_device_valid && to_device) {
debug(user_context) << "halide_buffer_copy_already_locked: device to device case.\n";
err = dst_device_interface->impl->buffer_copy(user_context, src, dst_device_interface, dst);
}
if (err == halide_error_code_incompatible_device_interface) {
// Return an error for a case that cannot make progress without a temporary allocation.
// TODO: go ahead and do the temp allocation.
if (!from_host_exists && !to_host_exists) {
debug(user_context) << "halide_buffer_copy_already_locked: failing due to need for temp buffer.\n";
return halide_error_code_incompatible_device_interface;
}
if (to_host && from_host_valid) {
device_copy c = make_buffer_copy(src, true, dst, true);
copy_memory(c, user_context);
err = 0;
} else if (to_host && from_device_valid) {
debug(user_context) << "halide_buffer_copy_already_locked: to host case.\n";
err = src->device_interface->impl->buffer_copy(user_context, src, nullptr, dst);
// Return on success or an error indicating something other
// than not handling this case went wrong.
if (err == halide_error_code_incompatible_device_interface) {
err = copy_to_host_already_locked(user_context, src);
if (!err) {
err = halide_buffer_copy_already_locked(user_context, src, nullptr, dst);
}
}
} else {
if (from_device_valid && to_host_exists) {
debug(user_context) << "halide_buffer_copy_already_locked: from_device_valid && to_host_exists case.\n";
// dev -> dev via dst host memory
debug(user_context) << " device -> device via dst host memory\n";
err = src->device_interface->impl->buffer_copy(user_context, src, nullptr, dst);
if (err == 0) {
dst->set_host_dirty(true);
err = copy_to_device_already_locked(user_context, dst, dst_device_interface);
}
} else if (to_device) {
debug(user_context) << "halide_buffer_copy_already_locked: dev -> dev via src host memory.\n";
// dev -> dev via src host memory.
err = copy_to_host_already_locked(user_context, src);
if (err == 0) {
err = dst_device_interface->impl->buffer_copy(user_context, src, dst_device_interface, dst);
}
} else {
debug(user_context) << "halide_buffer_copy_already_locked: no valid copy mode found, failing.\n";
}
}
}
if (err != 0) {
debug(user_context) << "halide_buffer_copy_already_locked: got error " << err << ".\n";
}
if (err == 0 && dst != src) {
if (dst_device_interface) {
debug(user_context) << "halide_buffer_copy_already_locked: setting device dirty.\n";
dst->set_host_dirty(false);
dst->set_device_dirty(true);
} else {
debug(user_context) << "halide_buffer_copy_already_locked: setting host dirty.\n";
dst->set_host_dirty(true);
dst->set_device_dirty(false);
}
}
return err;
}
WEAK int halide_buffer_copy(void *user_context, struct halide_buffer_t *src,
const struct halide_device_interface_t *dst_device_interface,
struct halide_buffer_t *dst) {
debug(user_context) << "halide_buffer_copy:\n"
<< " src " << *src << "\n"
<< " interface " << dst_device_interface << "\n"
<< " dst " << *dst << "\n";
ScopedMutexLock lock(&device_copy_mutex);
if (dst_device_interface) {
dst_device_interface->impl->use_module();
}
if (src->device_interface) {
src->device_interface->impl->use_module();
}
int err = halide_buffer_copy_already_locked(user_context, src, dst_device_interface, dst);
if (dst_device_interface) {
dst_device_interface->impl->release_module();
}
if (src->device_interface) {
src->device_interface->impl->release_module();
}
return err;
}
WEAK int halide_default_device_crop(void *user_context,
const struct halide_buffer_t *src,
struct halide_buffer_t *dst) {
halide_error(user_context, "device_interface does not support cropping\n");
return halide_error_code_device_crop_unsupported;
}
WEAK int halide_default_device_slice(void *user_context,
const struct halide_buffer_t *src,
int slice_dim, int slice_pos,
struct halide_buffer_t *dst) {
halide_error(user_context, "device_interface does not support slicing\n");
return halide_error_code_device_crop_unsupported;
}
WEAK int halide_device_crop(void *user_context,
const struct halide_buffer_t *src,
struct halide_buffer_t *dst) {
ScopedMutexLock lock(&device_copy_mutex);
if (!src->device) {
return 0;
}
if (dst->device) {
halide_error(user_context, "destination buffer already has a device allocation\n");
return halide_error_code_device_crop_failed;
}
if (src->dimensions != dst->dimensions) {
halide_error(user_context, "src and dst must have identical dimensionality\n");
return halide_error_code_device_crop_failed;
}
src->device_interface->impl->use_module();
int err = src->device_interface->impl->device_crop(user_context, src, dst);
debug(user_context) << "halide_device_crop "
<< "\n"
<< " src: " << *src << "\n"
<< " dst: " << *dst << "\n";
return err;
}
WEAK int halide_device_slice(void *user_context,
const struct halide_buffer_t *src,
int slice_dim, int slice_pos,
struct halide_buffer_t *dst) {
ScopedMutexLock lock(&device_copy_mutex);
if (!src->device) {
return 0;
}
if (dst->device) {
halide_error(user_context, "destination buffer already has a device allocation\n");
return halide_error_code_device_crop_failed;
}
if (src->dimensions != dst->dimensions + 1) {
halide_error(user_context, "dst must have exactly one fewer dimension than src\n");
return halide_error_code_device_crop_failed;
}
src->device_interface->impl->use_module();
int err = src->device_interface->impl->device_slice(user_context, src, slice_dim, slice_pos, dst);
debug(user_context) << "halide_device_crop "
<< "\n"
<< " src: " << *src << "\n"
<< " dst: " << *dst << "\n";
return err;
}
WEAK int halide_default_device_release_crop(void *user_context,
struct halide_buffer_t *buf) {
if (!buf->device) {
return 0;
}
halide_error(user_context, "device_interface does not support cropping\n");
return halide_error_code_device_crop_unsupported;
}
WEAK int halide_device_release_crop(void *user_context,
struct halide_buffer_t *buf) {
if (buf->device) {
ScopedMutexLock lock(&device_copy_mutex);
const struct halide_device_interface_t *interface = buf->device_interface;
int result = interface->impl->device_release_crop(user_context, buf);
buf->device = 0;
interface->impl->release_module();
buf->device_interface = nullptr;
return result;
}
return 0;
}
} // extern "C" linkage
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 によって変換されたページ (->オリジナル) /