| 441 | } |
| 442 | |
| 443 | void InitMemoryMethod() { |
| 444 | std::call_once(memory_method_init_flag, [&]() { |
| 445 | auto &memory_utils = phi::MemoryUtils::Instance(); |
| 446 | auto memory_method = std::make_unique<phi::MemoryInterface>(); |
| 447 | memory_method->alloc = paddle::memory::Alloc; |
| 448 | memory_method->alloc_with_stream = paddle::memory::Alloc; |
| 449 | memory_method->alloc_shared = paddle::memory::AllocShared; |
| 450 | memory_method->alloc_shared_with_stream = paddle::memory::AllocShared; |
| 451 | memory_method->in_same_stream = paddle::memory::InSameStream; |
| 452 | memory_method->allocation_deleter = |
| 453 | paddle::memory::allocation::Allocator::AllocationDeleter; |
| 454 | #if defined(PADDLE_WITH_CUSTOM_DEVICE) || defined(PADDLE_WITH_CUDA) || \ |
| 455 | defined(PADDLE_WITH_HIP) || defined(PADDLE_WITH_XPU) |
| 456 | memory_method->copy_with_stream = |
| 457 | paddle::memory::Copy<phi::Place, phi::Place>; |
| 458 | #endif |
| 459 | memory_method->copy = paddle::memory::Copy<phi::Place, phi::Place>; |
| 460 | memory_method->device_memory_stat_current_value = |
| 461 | paddle::memory::DeviceMemoryStatCurrentValue; |
| 462 | #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) |
| 463 | memory_method->gpu_memory_usage = paddle::platform::GpuMemoryUsage; |
| 464 | #endif |
| 465 | |
| 466 | #if (defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)) && \ |
| 467 | (defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)) |
| 468 | // TODO(GhostScreaming): Use phi methods later. |
| 469 | memory_method->get_allocator = |
| 470 | [](int device_id, phi::gpuStream_t stream) -> phi::Allocator * { |
| 471 | return paddle::memory::allocation::AllocatorFacade::Instance() |
| 472 | .GetAllocator(phi::GPUPlace(device_id), stream) |
| 473 | .get(); |
| 474 | }; |
| 475 | memory_method->get_host_allocator = []() -> phi::Allocator * { |
| 476 | return paddle::memory::allocation::AllocatorFacade::Instance() |
| 477 | .GetAllocator(phi::CPUPlace()) |
| 478 | .get(); |
| 479 | }; |
| 480 | memory_method->get_zero_allocator = [](int device_id) -> phi::Allocator * { |
| 481 | return paddle::memory::allocation::AllocatorFacade::Instance() |
| 482 | .GetZeroAllocator(phi::GPUPlace(device_id)) |
| 483 | .get(); |
| 484 | }; |
| 485 | memory_method->get_host_zero_allocator = []() -> phi::Allocator * { |
| 486 | return paddle::memory::allocation::AllocatorFacade::Instance() |
| 487 | .GetZeroAllocator(phi::CPUPlace()) |
| 488 | .get(); |
| 489 | }; |
| 490 | memory_method->get_pinned_allocator = []() -> phi::Allocator * { |
| 491 | return paddle::memory::allocation::AllocatorFacade::Instance() |
| 492 | .GetAllocator(phi::GPUPinnedPlace()) |
| 493 | .get(); |
| 494 | }; |
| 495 | memory_method->get_new_cuda_event = [](int device_id) { |
| 496 | return paddle::platform::CudaEventResourcePool::Instance().New(device_id); |
| 497 | }; |
| 498 | #elif (defined(PADDLE_WITH_XPU) && defined(PADDLE_WITH_XPU_BKCL)) |
| 499 | // TODO(ZibinGuo): Use phi methods later. |
| 500 | memory_method->get_allocator = |