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
17 #include "runtime/device/gpu/gpu_device_manager.h"
18 #include "runtime/device/gpu/gpu_common.h"
19 #include "utils/log_adapter.h"
20 #include "utils/convert_utils.h"
21 #include "runtime/device/gpu/gpu_buffer_mgr.h"
22
23 namespace mindspore {
24 namespace device {
25 namespace gpu {
InitDevice()26 void GPUDeviceManager::InitDevice() {
27 CHECK_OP_RET_WITH_EXCEPT(CudaDriver::SetDevice(SizeToInt(cur_dev_id_)), "Failed to set current device id");
28 if (dev_alive_) {
29 return;
30 }
31 CHECK_OP_RET_WITH_EXCEPT(CreateStream(&default_stream_), "Failed to create CUDA stream.");
32 CHECK_CUDNN_RET_WITH_EXCEPT_NOTRACE(cudnnCreate(&cudnn_handle_), "Failed to create cuDNN handle");
33 CHECK_CUDNN_RET_WITH_EXCEPT_NOTRACE(cudnnSetStream(cudnn_handle_, reinterpret_cast<cudaStream_t>(default_stream())),
34 "Failed to set stream for cuDNN handle.");
35 CHECK_CUBLAS_RET_WITH_EXCEPT_NOTRACE(cublasCreate(&cublas_handle_), "Failed to create cuBLAS handle.");
36 CHECK_CUBLAS_RET_WITH_EXCEPT_NOTRACE(
37 cublasSetStream(cublas_handle_, reinterpret_cast<cudaStream_t>(default_stream())),
38 "Failed to set stream for cuBLAS handle.");
39 CHECK_CUSOLVER_RET_WITH_EXCEPT_NOTRACE(cusolverDnCreate(&cusolver_dn_handle_),
40 "Failed to create cusolver dn handle.");
41 CHECK_CUSOLVER_RET_WITH_EXCEPT_NOTRACE(
42 cusolverDnSetStream(cusolver_dn_handle_, reinterpret_cast<cudaStream_t>(default_stream())),
43 "Failed to set stream for cusolver dn handle");
44 CHECK_OP_RET_WITH_EXCEPT(GPUMemoryAllocator::GetInstance().Init(), "Failed to Init gpu memory allocator")
45 dev_alive_ = true;
46 }
47
ReleaseDevice()48 void GPUDeviceManager::ReleaseDevice() {
49 // Avoid repeated release device resource.
50 if (!dev_alive_) {
51 return;
52 }
53
54 for (CudaDeviceStream stream : gpu_streams_) {
55 if (stream != nullptr) {
56 CHECK_OP_RET_WITH_ERROR(CudaDriver::DestroyStream(stream), "Failed to destroy CUDA stream.");
57 }
58 }
59 if (cudnn_handle_ != nullptr) {
60 CHECK_CUDNN_RET_WITH_ERROR_NOTRACE(cudnnDestroy(cudnn_handle_), "Failed to destroy cuDNN handle");
61 }
62 if (cublas_handle_ != nullptr) {
63 CHECK_CUBLAS_RET_WITH_ERROR(cublasDestroy(cublas_handle_), "Failed to destroy cuBLAS handle.");
64 }
65 if (cusolver_dn_handle_ != nullptr) {
66 CHECK_CUSOLVER_RET_WITH_ERROR(cusolverDnDestroy(cusolver_dn_handle_), "Failed to destroy cusolver dn handle.");
67 }
68 CHECK_OP_RET_WITH_ERROR(GPUMemoryAllocator::GetInstance().Finalize(), "Failed to destroy gpu memory allocator");
69 dev_alive_ = false;
70 }
71
CreateStream(CudaDeviceStream * stream)72 bool GPUDeviceManager::CreateStream(CudaDeviceStream *stream) {
73 CHECK_OP_RET_WITH_EXCEPT(CudaDriver::CreateStream(stream), "Failed to create CUDA stream");
74 gpu_streams_.emplace_back(*stream);
75 return true;
76 }
77
default_stream() const78 const CudaDeviceStream &GPUDeviceManager::default_stream() const { return default_stream_; }
79
device_count() const80 int GPUDeviceManager::device_count() const { return CudaDriver::device_count(); }
81
set_cur_device_id(uint32_t device_id)82 bool GPUDeviceManager::set_cur_device_id(uint32_t device_id) {
83 if (!dev_id_init_) {
84 dev_id_init_ = true;
85 cur_dev_id_ = device_id;
86 mindspore::device::GpuBufferMgr::GetInstance().set_device_id(UintToInt(device_id));
87 return true;
88 } else {
89 MS_LOG(ERROR) << "Device already been set.";
90 return false;
91 }
92 }
93
cur_device_id() const94 uint32_t GPUDeviceManager::cur_device_id() const { return cur_dev_id_; }
95
is_device_id_init() const96 bool GPUDeviceManager::is_device_id_init() const { return dev_id_init_; }
97
GetCudnnHandle() const98 const cudnnHandle_t &GPUDeviceManager::GetCudnnHandle() const { return cudnn_handle_; }
99
GetCublasHandle() const100 const cublasHandle_t &GPUDeviceManager::GetCublasHandle() const { return cublas_handle_; }
GetCusolverDnHandle() const101 const cusolverDnHandle_t &GPUDeviceManager::GetCusolverDnHandle() const { return cusolver_dn_handle_; }
SyncStream(const CudaDeviceStream & stream) const102 bool GPUDeviceManager::SyncStream(const CudaDeviceStream &stream) const {
103 return dev_alive_ ? CudaDriver::SyncStream(stream) : false;
104 }
105
CopyDeviceMemToHost(const HostMemPtr & dst,const DeviceMemPtr & src,size_t size) const106 bool GPUDeviceManager::CopyDeviceMemToHost(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size) const {
107 return CudaDriver::CopyDeviceMemToHost(dst, src, size);
108 }
109
CopyHostMemToDevice(const DeviceMemPtr & dst,const void * src,size_t size) const110 bool GPUDeviceManager::CopyHostMemToDevice(const DeviceMemPtr &dst, const void *src, size_t size) const {
111 return CudaDriver::CopyHostMemToDevice(dst, src, size);
112 }
113
CopyDeviceMemToHostAsync(const HostMemPtr & dst,const DeviceMemPtr & src,size_t size,CudaDeviceStream stream) const114 bool GPUDeviceManager::CopyDeviceMemToHostAsync(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size,
115 CudaDeviceStream stream) const {
116 return CudaDriver::CopyDeviceMemToHostAsync(dst, src, size, stream);
117 }
118
CopyHostMemToDeviceAsync(const DeviceMemPtr & dst,const void * src,size_t size,CudaDeviceStream stream) const119 bool GPUDeviceManager::CopyHostMemToDeviceAsync(const DeviceMemPtr &dst, const void *src, size_t size,
120 CudaDeviceStream stream) const {
121 return CudaDriver::CopyHostMemToDeviceAsync(dst, src, size, stream);
122 }
123
CopyDeviceMemToDeviceAsync(const DeviceMemPtr & dst,const DeviceMemPtr & src,size_t size,CudaDeviceStream stream) const124 bool GPUDeviceManager::CopyDeviceMemToDeviceAsync(const DeviceMemPtr &dst, const DeviceMemPtr &src, size_t size,
125 CudaDeviceStream stream) const {
126 return CudaDriver::CopyDeviceMemToDeviceAsync(dst, src, size, stream);
127 }
128 } // namespace gpu
129 } // namespace device
130 } // namespace mindspore
131