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.h 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef MINDSPORE_CCSRC_DEVICE_GPU_GPU_DEVICE_MANAGER_H_
  17. #define MINDSPORE_CCSRC_DEVICE_GPU_GPU_DEVICE_MANAGER_H_
  18. #include <cudnn.h>
  19. #include <cublas_v2.h>
  20. #include <vector>
  21. #include <memory>
  22. #include "device/gpu/cuda_driver.h"
  23. #include "device/gpu/gpu_memory_allocator.h"
  24. namespace mindspore {
  25. namespace device {
  26. namespace gpu {
  27. class GPUDeviceManager {
  28. public:
  29. void InitDevice();
  30. void ReleaseDevice();
  31. int device_count() const;
  32. bool set_cur_device_id(uint32_t device_id);
  33. uint32_t cur_device_id() const;
  34. bool is_device_id_init() const;
  35. bool CreateStream(DeviceStream *stream);
  36. bool SyncStream(const DeviceStream &stream) const;
  37. const DeviceStream &default_stream() const;
  38. const cudnnHandle_t &GetCudnnHandle() const;
  39. const cublasHandle_t &GetCublasHandle() const;
  40. bool CopyDeviceMemToHost(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size) const;
  41. bool CopyHostMemToDevice(const DeviceMemPtr &dst, const void *src, size_t size) const;
  42. bool CopyDeviceMemToHostAsync(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size, DeviceStream stream) const;
  43. bool CopyHostMemToDeviceAsync(const DeviceMemPtr &dst, const void *src, size_t size, DeviceStream stream) const;
  44. static GPUDeviceManager &GetInstance() {
  45. static GPUDeviceManager instance;
  46. return instance;
  47. }
  48. private:
  49. GPUDeviceManager() : dev_id_init_(false), cur_dev_id_(0) {}
  50. ~GPUDeviceManager() = default;
  51. GPUDeviceManager(const GPUDeviceManager &) = delete;
  52. GPUDeviceManager &operator=(const GPUDeviceManager &) = delete;
  53. // default CUDA stream used for all the kernels.
  54. DeviceStream default_stream_{nullptr};
  55. // all gpu CUDA streams including default_stream_.
  56. std::vector<DeviceStream> gpu_streams_;
  57. // handle used for cuDNN kernels.
  58. cudnnHandle_t cudnn_handle_{nullptr};
  59. // handle used for cuBLAS kernels.
  60. cublasHandle_t cublas_handle_{nullptr};
  61. bool dev_id_init_;
  62. uint32_t cur_dev_id_;
  63. };
  64. } // namespace gpu
  65. } // namespace device
  66. } // namespace mindspore
  67. #endif // MINDSPORE_CCSRC_DEVICE_GPU_GPU_DEVICE_MANAGER_H_