You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

fixture.cpp 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * \file dnn/test/rocm/fixture.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "hcc_detail/hcc_defs_prologue.h"
  12. #include "test/rocm/fixture.h"
  13. #include "src/rocm/handle.h"
  14. #include "test/common/memory_manager.h"
  15. #include "test/common/random_state.h"
  16. #include "test/common/utils.h"
  17. #include "src/rocm/utils.h"
  18. #include <cstdlib>
  19. #include "hip_header.h"
  20. using namespace megdnn;
  21. using namespace test;
  22. namespace {
  23. void setup_device() {
  24. #if !defined(WIN32)
  25. auto device_id_env = std::getenv("MEGDNN_DEVICE_ID");
  26. int device_id = -1;
  27. if (device_id_env) {
  28. device_id = std::atoi(device_id_env);
  29. std::cout << "Select device " << device_id
  30. << " because MEGDNN_DEVICE_ID is set." << std::endl;
  31. }
  32. auto pci_bus_id_env = std::getenv("MEGDNN_PCI_BUS_ID");
  33. if (pci_bus_id_env) {
  34. megdnn_assert(hipSuccess ==
  35. hipDeviceGetByPCIBusId(&device_id, pci_bus_id_env));
  36. std::cout << "Select device " << pci_bus_id_env << " (" << device_id
  37. << ") because MEGDNN_PCI_BUS_ID is set." << std::endl;
  38. }
  39. if (device_id_env && pci_bus_id_env) {
  40. std::cout << "MEGDNN_DEVICE_ID and MEGDNN_PCI_BUS_ID should not "
  41. "be set simultaneously."
  42. << std::endl;
  43. exit(1);
  44. }
  45. if (device_id_env || pci_bus_id_env) {
  46. megdnn_assert(hipSuccess == hipSetDevice(device_id));
  47. }
  48. #endif
  49. }
  50. } // anonymous namespace
  51. void ROCM::SetUp() {
  52. RandomState::reset();
  53. setup_device();
  54. megcoreDeviceHandle_t dev_handle;
  55. megcore_check(megcoreCreateDeviceHandle(&dev_handle, megcorePlatformROCM));
  56. megcoreComputingHandle_t comp_handle;
  57. megcore_check(megcoreCreateComputingHandle(&comp_handle, dev_handle));
  58. m_handle_rocm = Handle::make(comp_handle);
  59. megdnn_assert(m_handle_rocm);
  60. }
  61. Handle* ROCM::handle_naive(bool check_dispatch) {
  62. if (!m_handle_naive)
  63. m_handle_naive = create_cpu_handle(2, check_dispatch);
  64. return m_handle_naive.get();
  65. }
  66. void ROCM::TearDown() {
  67. m_handle_naive.reset();
  68. m_handle_rocm.reset();
  69. MemoryManagerHolder::instance()->clear();
  70. }
  71. void ROCM_ERROR_INFO::SetUp() {
  72. setup_device();
  73. megcoreDeviceHandle_t dev_handle;
  74. megcore_check(megcoreCreateDeviceHandle(&dev_handle, megcorePlatformROCM));
  75. m_error_info_dev = nullptr;
  76. void* ptr;
  77. hip_check(hipMalloc(&ptr, sizeof(megcore::AsyncErrorInfo)));
  78. hip_check(hipMemset(ptr, 0, sizeof(megcore::AsyncErrorInfo)));
  79. hip_check(hipDeviceSynchronize());
  80. m_error_info_dev = static_cast<megcore::AsyncErrorInfo*>(ptr);
  81. // create handle bind with error_info
  82. megcoreComputingHandle_t comp_handle;
  83. megcore_check(megcore::createComputingHandleWithROCMContext(
  84. &comp_handle, dev_handle, 0, {nullptr, m_error_info_dev}));
  85. m_handle_rocm = Handle::make(comp_handle);
  86. megdnn_assert(static_cast<bool>(m_handle_rocm));
  87. }
  88. void ROCM_ERROR_INFO::TearDown() {
  89. if (m_error_info_dev) {
  90. hip_check(hipFree(m_error_info_dev));
  91. }
  92. m_handle_rocm.reset();
  93. MemoryManagerHolder::instance()->clear();
  94. }
  95. megcore::AsyncErrorInfo ROCM_ERROR_INFO::get_error_info() {
  96. megcore::AsyncErrorInfo ret;
  97. auto stream = rocm::hip_stream(m_handle_rocm.get());
  98. hip_check(hipMemcpyAsync(&ret, m_error_info_dev, sizeof(ret),
  99. hipMemcpyDeviceToHost, stream));
  100. hip_check(hipStreamSynchronize(stream));
  101. return ret;
  102. }
  103. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台