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.

gpu_device_manager.cc 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "device/gpu/gpu_device_manager.h"
  17. #include "device/gpu/gpu_common.h"
  18. #include "utils/log_adapter.h"
  19. #include "utils/convert_utils.h"
  20. #include "device/gpu/gpu_buffer_mgr.h"
  21. namespace mindspore {
  22. namespace device {
  23. namespace gpu {
  24. void GPUDeviceManager::InitDevice() {
  25. CHECK_OP_RET_WITH_EXCEPT(CudaDriver::set_current_device(SizeToInt(cur_dev_id_)), "Failed to set current device id");
  26. CHECK_OP_RET_WITH_EXCEPT(CreateStream(&default_stream_), "Failed to create CUDA stream.");
  27. CHECK_CUDNN_RET_WITH_EXCEPT(cudnnCreate(&cudnn_handle_), "Failed to create cuDNN handle");
  28. CHECK_CUDNN_RET_WITH_EXCEPT(cudnnSetStream(cudnn_handle_, reinterpret_cast<cudaStream_t>(default_stream())),
  29. "Failed to set stream for cuDNN handle.");
  30. CHECK_CUBLAS_RET_WITH_EXCEPT(cublasCreate(&cublas_handle_), "Failed to create cuBLAS handle.");
  31. CHECK_CUBLAS_RET_WITH_EXCEPT(cublasSetStream(cublas_handle_, reinterpret_cast<cudaStream_t>(default_stream())),
  32. "Failed to set stream for cuBLAS handle.");
  33. CHECK_OP_RET_WITH_EXCEPT(GPUMemoryAllocator::GetInstance().Init(), "Failed to Init gpu memory allocator")
  34. }
  35. void GPUDeviceManager::ReleaseDevice() {
  36. for (DeviceStream stream : gpu_streams_) {
  37. if (stream != nullptr) {
  38. CHECK_OP_RET_WITH_ERROR(CudaDriver::DestroyStream(stream), "Failed to destroy CUDA stream.");
  39. }
  40. }
  41. if (cudnn_handle_ != nullptr) {
  42. CHECK_CUDNN_RET_WITH_ERROR(cudnnDestroy(cudnn_handle_), "Failed to destroy cuDNN handle");
  43. }
  44. if (cublas_handle_ != nullptr) {
  45. CHECK_CUBLAS_RET_WITH_ERROR(cublasDestroy(cublas_handle_), "Failed to destroy cuBLAS handle.");
  46. }
  47. CHECK_OP_RET_WITH_ERROR(GPUMemoryAllocator::GetInstance().Finalize(), "Failed to destroy gpu memory allocator");
  48. }
  49. bool GPUDeviceManager::CreateStream(DeviceStream *stream) {
  50. CHECK_OP_RET_WITH_EXCEPT(CudaDriver::CreateStream(stream), "Failed to create CUDA stream");
  51. gpu_streams_.emplace_back(*stream);
  52. return true;
  53. }
  54. const DeviceStream &GPUDeviceManager::default_stream() const { return default_stream_; }
  55. int GPUDeviceManager::device_count() const { return CudaDriver::device_count(); }
  56. bool GPUDeviceManager::set_cur_device_id(uint32_t device_id) {
  57. if (!dev_id_init_) {
  58. dev_id_init_ = true;
  59. cur_dev_id_ = device_id;
  60. mindspore::device::GpuBufferMgr::GetInstance().set_device_id(UintToInt(device_id));
  61. return true;
  62. } else {
  63. MS_LOG(ERROR) << "Device already been set.";
  64. return false;
  65. }
  66. }
  67. uint32_t GPUDeviceManager::cur_device_id() const { return cur_dev_id_; }
  68. bool GPUDeviceManager::is_device_id_init() const { return dev_id_init_; }
  69. const cudnnHandle_t &GPUDeviceManager::GetCudnnHandle() const { return cudnn_handle_; }
  70. const cublasHandle_t &GPUDeviceManager::GetCublasHandle() const { return cublas_handle_; }
  71. bool GPUDeviceManager::SyncStream(const DeviceStream &stream) const { return CudaDriver::SyncStream(stream); }
  72. bool GPUDeviceManager::CopyDeviceMemToHost(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size) const {
  73. return CudaDriver::CopyDeviceMemToHost(dst, src, size);
  74. }
  75. bool GPUDeviceManager::CopyHostMemToDevice(const DeviceMemPtr &dst, const void *src, size_t size) const {
  76. return CudaDriver::CopyHostMemToDevice(dst, src, size);
  77. }
  78. bool GPUDeviceManager::CopyDeviceMemToHostAsync(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size,
  79. DeviceStream stream) const {
  80. return CudaDriver::CopyDeviceMemToHostAsync(dst, src, size, stream);
  81. }
  82. bool GPUDeviceManager::CopyHostMemToDeviceAsync(const DeviceMemPtr &dst, const void *src, size_t size,
  83. DeviceStream stream) const {
  84. return CudaDriver::CopyHostMemToDeviceAsync(dst, src, size, stream);
  85. }
  86. } // namespace gpu
  87. } // namespace device
  88. } // namespace mindspore