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.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. static GPUDeviceManager &GetInstance() {
  43. static GPUDeviceManager instance;
  44. return instance;
  45. }
  46. private:
  47. GPUDeviceManager() : dev_id_init_(false), cur_dev_id_(0) {}
  48. ~GPUDeviceManager() = default;
  49. GPUDeviceManager(const GPUDeviceManager &) = delete;
  50. GPUDeviceManager &operator=(const GPUDeviceManager &) = delete;
  51. // default CUDA stream used for all the kernels.
  52. DeviceStream default_stream_{nullptr};
  53. // all gpu CUDA streams including default_stream_.
  54. std::vector<DeviceStream> gpu_streams_;
  55. // handle used for cuDNN kernels.
  56. cudnnHandle_t cudnn_handle_{nullptr};
  57. // handle used for cuBLAS kernels.
  58. cublasHandle_t cublas_handle_{nullptr};
  59. bool dev_id_init_;
  60. uint32_t cur_dev_id_;
  61. };
  62. } // namespace gpu
  63. } // namespace device
  64. } // namespace mindspore
  65. #endif // MINDSPORE_CCSRC_DEVICE_GPU_GPU_DEVICE_MANAGER_H_