device_manager.cc 26.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
#include "paddle/phi/backends/device_manager.h"
16
#include "paddle/phi/common/complex.h"
17 18 19 20 21 22 23 24 25 26

#if !defined(_WIN32)
#include <dirent.h>
#else

#endif

#include <functional>
#include <regex>

27
#include "glog/logging.h"
28
#include "paddle/utils/string/split.h"
29

30
namespace phi {
31

32
void Device::CheckInitialized() {
33 34 35 36
  std::call_once(initialized_once_flag_, [&]() {
    this->impl_->InitDevice(dev_id_);
    this->initialized_ = true;
  });
37 38
}

39 40 41 42 43
Device::~Device() {
  if (initialized_) {
    impl_->DeInitDevice(dev_id_);
  }
}
44

45 46 47
void Device::CreateStream(stream::Stream* stream,
                          const stream::Stream::Priority& priority,
                          const stream::Stream::Flag& flag) {
48
  CheckInitialized();
49 50 51 52
  impl_->CreateStream(dev_id_, stream, priority, flag);
}

void Device::DestroyStream(stream::Stream* stream) {
53
  CheckInitialized();
54 55 56 57
  impl_->DestroyStream(dev_id_, stream);
}

void Device::SynchronizeStream(const stream::Stream* stream) {
58
  CheckInitialized();
59 60 61 62
  impl_->SynchronizeStream(dev_id_, stream);
}

bool Device::QueryStream(const stream::Stream* stream) {
63
  CheckInitialized();
64 65 66 67 68
  return impl_->QueryStream(dev_id_, stream);
}

void Device::AddCallback(stream::Stream* stream,
                         stream::Stream::Callback* callback) {
69
  CheckInitialized();
70 71 72 73
  impl_->AddCallback(dev_id_, stream, callback);
}

void Device::CreateEvent(event::Event* event, event::Event::Flag flags) {
74
  CheckInitialized();
75 76 77 78
  impl_->CreateEvent(dev_id_, event, flags);
}

void Device::DestroyEvent(event::Event* event) {
79
  CheckInitialized();
80 81 82 83 84
  impl_->DestroyEvent(dev_id_, event);
}

void Device::RecordEvent(const event::Event* event,
                         const stream::Stream* stream) {
85
  CheckInitialized();
86 87 88 89
  impl_->RecordEvent(dev_id_, event, stream);
}

void Device::SynchronizeEvent(const event::Event* event) {
90
  CheckInitialized();
91 92 93 94
  impl_->SynchronizeEvent(dev_id_, event);
}

bool Device::QueryEvent(const event::Event* event) {
95
  CheckInitialized();
96 97 98 99 100
  return impl_->QueryEvent(dev_id_, event);
}

void Device::StreamWaitEvent(const stream::Stream* stream,
                             const event::Event* event) {
101
  CheckInitialized();
102 103 104
  impl_->StreamWaitEvent(dev_id_, stream, event);
}

105 106 107
void Device::MemoryCopyH2D(void* dst,
                           const void* src,
                           size_t size,
108
                           const stream::Stream* stream) {
109
  CheckInitialized();
110 111 112
  impl_->MemoryCopyH2D(dev_id_, dst, src, size, stream);
}

113 114 115
void Device::MemoryCopyD2H(void* dst,
                           const void* src,
                           size_t size,
116
                           const stream::Stream* stream) {
117
  CheckInitialized();
118 119 120
  impl_->MemoryCopyD2H(dev_id_, dst, src, size, stream);
}

121 122 123
void Device::MemoryCopyD2D(void* dst,
                           const void* src,
                           size_t size,
124
                           const stream::Stream* stream) {
125
  CheckInitialized();
126 127 128
  impl_->MemoryCopyD2D(dev_id_, dst, src, size, stream);
}

129 130 131 132 133
void Device::MemoryCopyP2P(const Place& dst_place,
                           void* dst,
                           const void* src,
                           size_t size,
                           const stream::Stream* stream) {
134
  CheckInitialized();
135 136 137 138
  impl_->MemoryCopyP2P(dst_place, dst, dev_id_, src, size, stream);
}

void* Device::MemoryAllocate(size_t size) {
139
  CheckInitialized();
140 141 142 143
  return impl_->MemoryAllocate(dev_id_, size);
}

void Device::MemoryDeallocate(void* ptr, size_t size) {
144
  CheckInitialized();
145 146 147 148
  impl_->MemoryDeallocate(dev_id_, ptr, size);
}

void* Device::MemoryAllocateHost(size_t size) {
149
  CheckInitialized();
150 151 152 153
  return impl_->MemoryAllocateHost(dev_id_, size);
}

void Device::MemoryDeallocateHost(void* ptr, size_t size) {
154
  CheckInitialized();
155 156 157 158
  impl_->MemoryDeallocateHost(dev_id_, ptr, size);
}

void* Device::MemoryAllocateUnified(size_t size) {
159
  CheckInitialized();
160 161 162 163
  return impl_->MemoryAllocateUnified(dev_id_, size);
}

void Device::MemoryDeallocateUnified(void* ptr, size_t size) {
164
  CheckInitialized();
165 166 167 168
  impl_->MemoryDeallocateUnified(dev_id_, ptr, size);
}

void Device::MemorySet(void* ptr, uint8_t value, size_t size) {
169
  CheckInitialized();
170 171 172
  impl_->MemorySet(dev_id_, ptr, value, size);
}

173 174 175 176 177 178 179
template <typename T>
void Device::BlasAXPBY(const stream::Stream& stream,
                       size_t numel,
                       float alpha,
                       const T* x,
                       float beta,
                       T* y) {
180
  CheckInitialized();
181 182
  impl_->BlasAXPBY(dev_id_,
                   stream,
183
                   phi::CppTypeToDataType<T>::Type(),
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
                   numel,
                   alpha,
                   reinterpret_cast<void*>(const_cast<T*>(x)),
                   beta,
                   reinterpret_cast<void*>(y));
}

template void Device::BlasAXPBY<paddle::float16>(const stream::Stream& stream,
                                                 size_t numel,
                                                 float alpha,
                                                 const paddle::float16* x,
                                                 float beta,
                                                 paddle::float16* y);
template void Device::BlasAXPBY<float>(const stream::Stream& stream,
                                       size_t numel,
                                       float alpha,
                                       const float* x,
                                       float beta,
                                       float* y);
template void Device::BlasAXPBY<double>(const stream::Stream& stream,
                                        size_t numel,
                                        float alpha,
                                        const double* x,
                                        float beta,
                                        double* y);
template void Device::BlasAXPBY<int8_t>(const stream::Stream& stream,
                                        size_t numel,
                                        float alpha,
                                        const int8_t* x,
                                        float beta,
                                        int8_t* y);
template void Device::BlasAXPBY<int16_t>(const stream::Stream& stream,
                                         size_t numel,
                                         float alpha,
                                         const int16_t* x,
                                         float beta,
                                         int16_t* y);
template void Device::BlasAXPBY<int32_t>(const stream::Stream& stream,
                                         size_t numel,
                                         float alpha,
                                         const int32_t* x,
                                         float beta,
                                         int32_t* y);
template void Device::BlasAXPBY<int64_t>(const stream::Stream& stream,
                                         size_t numel,
                                         float alpha,
                                         const int64_t* x,
                                         float beta,
                                         int64_t* y);
template void Device::BlasAXPBY<phi::dtype::complex<float>>(
    const stream::Stream& stream,
    size_t numel,
    float alpha,
    const phi::dtype::complex<float>* x,
    float beta,
    phi::dtype::complex<float>* y);
template void Device::BlasAXPBY<phi::dtype::complex<double>>(
    const stream::Stream& stream,
    size_t numel,
    float alpha,
    const phi::dtype::complex<double>* x,
    float beta,
    phi::dtype::complex<double>* y);

248 249
std::string Device::Type() { return impl_->Type(); }

250
static phi::RWLock _global_device_manager_rw_lock;
251 252

bool DeviceManager::Register(std::unique_ptr<DeviceInterface> device_impl) {
253
  phi::AutoWRLock lock(&_global_device_manager_rw_lock);
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
  VLOG(4) << "Register Device - " << device_impl->Type();
  auto device_type = device_impl->Type();
  auto& dev_impl_map = Instance().device_impl_map_;
  auto& dev_map = Instance().device_map_;

  if (dev_impl_map.find(device_type) == dev_impl_map.end()) {
    dev_impl_map.insert(
        std::pair<std::string, std::unique_ptr<DeviceInterface>>(
            device_type, std::move(device_impl)));
    auto& dev_impl = dev_impl_map[device_type];
    auto& dev_vec = dev_map[device_type];
    VLOG(4) << "GetDeviceCount is " << dev_impl->GetDeviceCount();
    for (size_t i = 0; i < dev_impl->GetDeviceCount(); ++i) {
      dev_vec.emplace_back(new Device(i, dev_impl.get()));
    }
  } else {
    auto& plat = dev_impl_map[device_type];
    if (plat->IsCustom() && plat->Priority() > device_impl->Priority()) {
      dev_impl_map[device_type] = std::move(device_impl);
      auto& dev_impl = dev_impl_map[device_type];
      auto& dev_vec = dev_map[device_type];
      dev_vec.clear();
      VLOG(4) << "GetDeviceCount is " << dev_impl->GetDeviceCount();
      for (size_t i = 0; i < dev_impl->GetDeviceCount(); ++i) {
        dev_vec.emplace_back(new Device(i, dev_impl.get()));
      }
    } else {
      return false;
    }
  }
  return true;
}

DeviceInterface* DeviceManager::GetDeviceInterfaceWithType(
    const std::string& device_type) {
289
  phi::AutoRDLock lock(&_global_device_manager_rw_lock);
290 291

  auto& dev_impl_map = Instance().device_impl_map_;
292 293 294 295 296
  PADDLE_ENFORCE_NE(
      dev_impl_map.find(device_type),
      dev_impl_map.end(),
      phi::errors::NotFound("%s interface not found.", device_type));
  return dev_impl_map.at(device_type).get();
297 298 299
}

Device* DeviceManager::GetDeviceWithPlace(const Place& place) {
300
  phi::AutoRDLock lock(&_global_device_manager_rw_lock);
301 302

  auto& dev_map = Instance().device_map_;
303 304 305 306 307 308
  auto dev_type = place.GetDeviceType();
  auto dev_id = place.GetDeviceId();
  PADDLE_ENFORCE_NE(
      dev_map.find(dev_type),
      dev_map.end(),
      phi::errors::NotFound("Unable to find Device with type %s.", dev_type));
309 310
  auto& dev_vec = dev_map[dev_type];
  PADDLE_ENFORCE_LT(
311 312 313
      dev_id,
      dev_vec.size(),
      phi::errors::OutOfRange(
314
          "The visible devices count of type %s is %d, but dev_id is %d.",
315 316 317
          dev_type,
          dev_vec.size(),
          dev_id));
318 319 320 321
  return dev_vec[dev_id].get();
}

std::vector<std::string> DeviceManager::GetAllDeviceTypes() {
322
  phi::AutoRDLock lock(&_global_device_manager_rw_lock);
323 324
  auto& dev_impl_map = Instance().device_impl_map_;
  std::vector<std::string> devices;
325 326
  for (const auto& map_item : dev_impl_map) {
    devices.push_back(map_item.first);
327 328 329 330 331
  }
  return devices;
}

std::vector<std::string> DeviceManager::GetAllCustomDeviceTypes() {
332
  phi::AutoRDLock lock(&_global_device_manager_rw_lock);
333 334
  auto& dev_impl_map = Instance().device_impl_map_;
  std::vector<std::string> devices;
335 336 337
  for (const auto& map_item : dev_impl_map) {
    if (map_item.second->IsCustom()) {
      devices.push_back(map_item.first);
338 339 340 341 342 343
    }
  }
  return devices;
}

std::vector<std::string> DeviceManager::GetAllDeviceList() {
344
  phi::AutoRDLock lock(&_global_device_manager_rw_lock);
345 346
  auto& dev_impl_map = Instance().device_impl_map_;
  std::vector<std::string> devices;
347 348 349
  for (const auto& map_item : dev_impl_map) {
    size_t device_count = map_item.second->GetDeviceCount();
    std::string dev_type = map_item.second->Type();
350 351 352 353 354 355 356 357 358 359 360 361
    if (device_count == 1) {
      devices.push_back(dev_type);
    } else {
      for (size_t i = 0; i < device_count; ++i) {
        devices.push_back(dev_type + ":" + std::to_string(i));
      }
    }
  }
  return devices;
}

std::vector<std::string> DeviceManager::GetAllCustomDeviceList() {
362
  phi::AutoRDLock lock(&_global_device_manager_rw_lock);
363 364
  auto& dev_impl_map = Instance().device_impl_map_;
  std::vector<std::string> devices;
365 366 367 368
  for (const auto& map_item : dev_impl_map) {
    size_t device_count = map_item.second->GetDeviceCount();
    std::string dev_type = map_item.second->Type();
    if (map_item.second->IsCustom()) {
369 370 371 372 373 374 375 376 377 378 379 380 381
      if (device_count == 1) {
        devices.push_back(dev_type);
      } else {
        for (size_t i = 0; i < device_count; ++i) {
          devices.push_back(dev_type + ":" + std::to_string(i));
        }
      }
    }
  }
  return devices;
}

bool DeviceManager::HasDeviceType(const std::string& device_type) {
382 383 384
  phi::AutoRDLock lock(&_global_device_manager_rw_lock);
  auto& dev_impl_map = Instance().device_impl_map_;
  return dev_impl_map.find(device_type) != dev_impl_map.end();
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
}

bool DeviceManager::IsCustom(const std::string& device_type) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  return dev_impl->IsCustom();
}

void DeviceManager::Initialize(const std::string& device_type) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->Initialize();
}

void DeviceManager::Finalize(const std::string& device_type) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->Finalize();
}

void DeviceManager::SynchronizeDevice(const Place& place) {
403 404
  auto device_type = place.GetDeviceType();
  auto device_id = place.GetDeviceId();
405 406 407 408 409 410 411 412 413 414 415
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->SynchronizeDevice(device_id);
}

void DeviceManager::SetDevice(const std::string& device_type,
                              size_t device_id) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->SetDevice(device_id);
}

void DeviceManager::SetDevice(const Place& place) {
416 417
  auto device_type = place.GetDeviceType();
  auto device_id = place.GetDeviceId();
418 419 420 421 422 423 424 425 426
  DeviceManager::SetDevice(device_type, device_id);
}

int DeviceManager::GetDevice(const std::string& device_type) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  return dev_impl->GetDevice();
}

size_t DeviceManager::GetMinChunkSize(const Place& place) {
427 428
  auto device_type = place.GetDeviceType();
  auto device_id = place.GetDeviceId();
429 430 431 432 433
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  return dev_impl->GetMinChunkSize(device_id);
}

size_t DeviceManager::GetMaxChunkSize(const Place& place) {
434 435
  auto device_type = place.GetDeviceType();
  auto device_id = place.GetDeviceId();
436 437 438 439 440
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  return dev_impl->GetMaxChunkSize(device_id);
}

size_t DeviceManager::GetMaxAllocSize(const Place& place) {
441 442
  auto device_type = place.GetDeviceType();
  auto device_id = place.GetDeviceId();
443 444 445 446 447
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  return dev_impl->GetMaxAllocSize(device_id);
}

size_t DeviceManager::GetInitAllocSize(const Place& place) {
448 449
  auto device_type = place.GetDeviceType();
  auto device_id = place.GetDeviceId();
450 451 452 453 454
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  return dev_impl->GetInitAllocSize(device_id);
}

size_t DeviceManager::GetReallocSize(const Place& place) {
455 456
  auto device_type = place.GetDeviceType();
  auto device_id = place.GetDeviceId();
457 458 459 460 461
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  return dev_impl->GetReallocSize(device_id);
}

size_t DeviceManager::GetExtraPaddingSize(const Place& place) {
462 463
  auto device_type = place.GetDeviceType();
  auto device_id = place.GetDeviceId();
464 465 466 467
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  return dev_impl->GetExtraPaddingSize(device_id);
}

468 469
void DeviceManager::MemoryStats(const Place& place,
                                size_t* total,
470
                                size_t* free) {
471 472
  auto device_type = place.GetDeviceType();
  auto device_id = place.GetDeviceId();
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->MemoryStats(device_id, total, free);
}

size_t DeviceManager::GetDeviceCount(const std::string& device_type) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  return dev_impl->GetDeviceCount();
}

std::vector<size_t> DeviceManager::GetDeviceList(
    const std::string& device_type) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  return dev_impl->GetDeviceList();
}

488 489
std::vector<size_t> DeviceManager::GetSelectedDeviceList(
    const std::string& device_type) {
490 491 492 493 494 495 496 497 498 499 500
  static std::unordered_map<std::string, std::vector<size_t>> device_list_map;
  if (device_list_map.find(device_type) == device_list_map.end()) {
    std::vector<size_t>& device_list = device_list_map[device_type];
    std::string FLAGS = "FLAGS_selected_" + device_type + "s";
    auto FLAGS_selected_devices = getenv(FLAGS.c_str());
    if (FLAGS_selected_devices) {
      auto devices_str = paddle::string::Split(FLAGS_selected_devices, ',');
      for (auto id : devices_str) {
        device_list.push_back(atoi(id.c_str()));
      }
    } else {
501
      int count = static_cast<int>(DeviceManager::GetDeviceCount(device_type));
502 503 504
      for (int i = 0; i < count; ++i) {
        device_list.push_back(i);
      }
505 506
    }
  }
507
  return device_list_map[device_type];
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
}

void DeviceManager::CCLDestroyComm(const std::string& device_type,
                                   ccl::CCLComm ccl_comm) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLDestroyComm(ccl_comm);
}

void DeviceManager::CCLCommInitRank(const std::string& device_type,
                                    size_t num_ranks,
                                    ccl::CCLRootId* root_id,
                                    size_t rank_id,
                                    ccl::CCLComm* ccl_comm) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLCommInitRank(num_ranks, root_id, rank_id, ccl_comm);
}

void DeviceManager::CCLGetUniqueId(const std::string& device_type,
                                   ccl::CCLRootId* root_id) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLGetUniqueId(root_id);
}

void DeviceManager::CCLBroadcast(const std::string& device_type,
                                 void* data,
                                 size_t num,
                                 ccl::CCLDataType data_type,
                                 size_t root_id,
                                 const ccl::CCLComm& ccl_comm,
                                 const stream::Stream& stream) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLBroadcast(data, num, data_type, root_id, ccl_comm, stream);
}

void DeviceManager::CCLAllReduce(const std::string& device_type,
                                 void* in_data,
                                 void* out_data,
                                 size_t num,
                                 ccl::CCLDataType data_type,
                                 ccl::CCLReduceOp reduce_op,
                                 const ccl::CCLComm& ccl_comm,
                                 const stream::Stream& stream) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLAllReduce(
      in_data, out_data, num, data_type, reduce_op, ccl_comm, stream);
}

void DeviceManager::CCLReduce(const std::string& device_type,
                              void* in_data,
                              void* out_data,
                              size_t num,
                              ccl::CCLDataType data_type,
                              ccl::CCLReduceOp reduce_op,
561
                              size_t root_id,
562 563 564 565
                              const ccl::CCLComm& ccl_comm,
                              const stream::Stream& stream) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLReduce(
566
      in_data, out_data, num, data_type, reduce_op, root_id, ccl_comm, stream);
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
}

void DeviceManager::CCLAllGather(const std::string& device_type,
                                 void* in_data,
                                 void* out_data,
                                 size_t num,
                                 ccl::CCLDataType data_type,
                                 const ccl::CCLComm& ccl_comm,
                                 const stream::Stream& stream) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLAllGather(in_data, out_data, num, data_type, ccl_comm, stream);
}

void DeviceManager::CCLReduceScatter(const std::string& device_type,
                                     void* in_data,
                                     void* out_data,
                                     size_t num,
                                     ccl::CCLDataType data_type,
                                     ccl::CCLReduceOp op,
                                     const ccl::CCLComm& ccl_comm,
                                     const stream::Stream& stream) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLReduceScatter(
      in_data, out_data, num, data_type, op, ccl_comm, stream);
}

void DeviceManager::CCLGroupStart(const std::string& device_type) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLGroupStart();
}

void DeviceManager::CCLGroupEnd(const std::string& device_type) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLGroupEnd();
}

void DeviceManager::CCLSend(const std::string& device_type,
                            void* sendbuf,
                            size_t num,
                            ccl::CCLDataType data_type,
                            size_t dst_rank,
                            const ccl::CCLComm& ccl_comm,
                            const stream::Stream& stream) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLSend(sendbuf, num, data_type, dst_rank, ccl_comm, stream);
}

void DeviceManager::CCLRecv(const std::string& device_type,
                            void* recvbuf,
                            size_t num,
                            ccl::CCLDataType data_type,
                            size_t src_rank,
                            const ccl::CCLComm& ccl_comm,
                            const stream::Stream& stream) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLRecv(recvbuf, num, data_type, src_rank, ccl_comm, stream);
}

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
void DeviceManager::CCLAllToAll(const std::string& device_type,
                                const void** send_buf,
                                const size_t* send_count,
                                const ccl::CCLDataType* send_dtype,
                                void** recv_buf,
                                const size_t* recv_count,
                                const ccl::CCLDataType* recv_dtype,
                                size_t rank,
                                size_t nranks,
                                const ccl::CCLComm& comm,
                                const stream::Stream& stream) {
  auto dev_impl = GetDeviceInterfaceWithType(device_type);
  dev_impl->CCLAllToAll(send_buf,
                        send_count,
                        send_dtype,
                        recv_buf,
                        recv_count,
                        recv_dtype,
                        rank,
                        nranks,
                        comm,
                        stream);
}

649
// profiler
650 651 652
void DeviceManager::ProfilerInitialize(const std::string& dev_type,
                                       phi::TraceEventCollector* collector,
                                       void** context) {
653 654 655 656
  auto dev_impl = GetDeviceInterfaceWithType(dev_type);
  dev_impl->ProfilerInitialize(collector, context);
}

657 658 659
void DeviceManager::ProfilerFinalize(const std::string& dev_type,
                                     phi::TraceEventCollector* collector,
                                     void* context) {
660 661 662 663
  auto dev_impl = GetDeviceInterfaceWithType(dev_type);
  dev_impl->ProfilerFinalize(collector, context);
}

664 665 666
void DeviceManager::ProfilerPrepareTracing(const std::string& dev_type,
                                           phi::TraceEventCollector* collector,
                                           void* context) {
667 668 669 670
  auto dev_impl = GetDeviceInterfaceWithType(dev_type);
  dev_impl->ProfilerPrepareTracing(collector, context);
}

671 672 673
void DeviceManager::ProfilerStartTracing(const std::string& dev_type,
                                         phi::TraceEventCollector* collector,
                                         void* context) {
674 675 676 677
  auto dev_impl = GetDeviceInterfaceWithType(dev_type);
  dev_impl->ProfilerStartTracing(collector, context);
}

678 679 680
void DeviceManager::ProfilerStopTracing(const std::string& dev_type,
                                        phi::TraceEventCollector* collector,
                                        void* context) {
681 682 683 684 685 686
  auto dev_impl = GetDeviceInterfaceWithType(dev_type);
  dev_impl->ProfilerStopTracing(collector, context);
}

void DeviceManager::ProfilerCollectTraceData(
    const std::string& dev_type,
687
    phi::TraceEventCollector* collector,
688 689 690 691 692 693
    uint64_t start_ns,
    void* context) {
  auto dev_impl = GetDeviceInterfaceWithType(dev_type);
  dev_impl->ProfilerCollectTraceData(collector, start_ns, context);
}

694 695 696 697 698
DeviceManager& DeviceManager::Instance() {
  static DeviceManager platform_manager;
  return platform_manager;
}

699 700 701
void DeviceManager::Release() {
  stream::Stream::ReleaseAll();
  event::Event::ReleaseAll();
702 703
  Instance().device_map_.clear();
  Instance().device_impl_map_.clear();
704 705
}

706 707
std::vector<std::string> ListAllLibraries(const std::string& library_dir) {
  std::vector<std::string> libraries;
708 709 710
#if defined(__APPLE__)
  std::regex express(".*\\.dylib");
#else
711
  std::regex express(".*\\.so");
712
#endif
713
  std::match_results<std::string::iterator> results;
714 715

#if !defined(_WIN32)
716 717 718 719 720
  DIR* dir = nullptr;
  dirent* ptr = nullptr;

  dir = opendir(library_dir.c_str());
  if (dir == nullptr) {
721
    VLOG(4) << "Failed to open path: " << library_dir;
722 723 724
  } else {
    while ((ptr = readdir(dir)) != nullptr) {
      std::string filename(ptr->d_name);
725 726
      if (std::regex_match(
              filename.begin(), filename.end(), results, express)) {
727 728
        libraries.push_back(
            std::string(library_dir).append("/").append(filename));
729
        VLOG(4) << "Found lib: " << libraries.back();
730 731 732 733
      }
    }
    closedir(dir);
  }
734
#endif
735 736 737 738

  return libraries;
}

739
}  // namespace phi