• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_GPU_DEVICE_MANAGER_H_
18 #define MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_GPU_DEVICE_MANAGER_H_
19 
20 #include <cudnn.h>
21 #include <cublas_v2.h>
22 #include <cusolverDn.h>
23 #include <vector>
24 #include <memory>
25 #include "runtime/device/gpu/cuda_driver.h"
26 #include "runtime/device/gpu/gpu_memory_allocator.h"
27 
28 namespace mindspore {
29 namespace device {
30 namespace gpu {
31 class GPUDeviceManager {
32  public:
33   void InitDevice();
34   void ReleaseDevice();
35 
36   int device_count() const;
37   bool set_cur_device_id(uint32_t device_id);
38   uint32_t cur_device_id() const;
39   bool is_device_id_init() const;
40 
41   bool CreateStream(CudaDeviceStream *stream);
42   bool SyncStream(const CudaDeviceStream &stream) const;
43   const CudaDeviceStream &default_stream() const;
44 
45   const cudnnHandle_t &GetCudnnHandle() const;
46   const cublasHandle_t &GetCublasHandle() const;
47   const cusolverDnHandle_t &GetCusolverDnHandle() const;
48 
49   bool CopyDeviceMemToHost(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size) const;
50   bool CopyHostMemToDevice(const DeviceMemPtr &dst, const void *src, size_t size) const;
51 
52   bool CopyDeviceMemToHostAsync(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size,
53                                 CudaDeviceStream stream) const;
54   bool CopyHostMemToDeviceAsync(const DeviceMemPtr &dst, const void *src, size_t size, CudaDeviceStream stream) const;
55   bool CopyDeviceMemToDeviceAsync(const DeviceMemPtr &dst, const DeviceMemPtr &src, size_t size,
56                                   CudaDeviceStream stream) const;
57 
GetInstance()58   static GPUDeviceManager &GetInstance() {
59     static GPUDeviceManager instance;
60     return instance;
61   }
62 
63  private:
GPUDeviceManager()64   GPUDeviceManager() : dev_id_init_(false), cur_dev_id_(0), dev_alive_(false) {}
65   ~GPUDeviceManager() = default;
66   GPUDeviceManager(const GPUDeviceManager &) = delete;
67   GPUDeviceManager &operator=(const GPUDeviceManager &) = delete;
68 
69   // default CUDA stream used for all the kernels.
70   CudaDeviceStream default_stream_{nullptr};
71 
72   // all gpu CUDA streams including default_stream_.
73   std::vector<CudaDeviceStream> gpu_streams_;
74 
75   // handle used for cuDNN kernels.
76   cudnnHandle_t cudnn_handle_{nullptr};
77 
78   // handle used for cuBLAS kernels.
79   cublasHandle_t cublas_handle_{nullptr};
80 
81   // handle used for cusolver dn kernels;
82   cusolverDnHandle_t cusolver_dn_handle_{nullptr};
83   bool dev_id_init_;
84   uint32_t cur_dev_id_;
85   bool dev_alive_;
86 };
87 }  // namespace gpu
88 }  // namespace device
89 }  // namespace mindspore
90 
91 #endif  // MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_GPU_DEVICE_MANAGER_H_
92