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_buffer_mgr.h 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_BUFFER_MGR_H_
  17. #define MINDSPORE_CCSRC_DEVICE_GPU_GPU_BUFFER_MGR_H_
  18. #include <unistd.h>
  19. #include <cstring>
  20. #include <iostream>
  21. #include <functional>
  22. #include <map>
  23. #include <vector>
  24. #include <string>
  25. #include <memory>
  26. #include "device/gpu/blocking_queue.h"
  27. #define EXPORT __attribute__((visibility("default")))
  28. namespace mindspore {
  29. namespace device {
  30. static const unsigned int MAX_WAIT_TIME_IN_SEC = 60;
  31. class Semaphore {
  32. public:
  33. explicit Semaphore(int count = 0) : count_(count) {}
  34. inline void Signal() {
  35. std::unique_lock<std::mutex> lock(mutex_);
  36. ++count_;
  37. cv_.notify_one();
  38. }
  39. inline bool Wait() {
  40. std::unique_lock<std::mutex> lock(mutex_);
  41. while (count_ == 0) {
  42. if (cv_.wait_for(lock, std::chrono::seconds(MAX_WAIT_TIME_IN_SEC)) == std::cv_status::timeout) {
  43. return false;
  44. }
  45. }
  46. --count_;
  47. return true;
  48. }
  49. private:
  50. std::mutex mutex_;
  51. std::condition_variable cv_;
  52. int count_;
  53. };
  54. class HandleMgr {
  55. public:
  56. static const unsigned int MAX_HANDLE_NUM = 32;
  57. static const unsigned int INVALID_HANDLE = 0xffffffffUL;
  58. unsigned int AllocHandle();
  59. void FreeHandle(unsigned int);
  60. private:
  61. bool handle_list_[MAX_HANDLE_NUM];
  62. };
  63. class GpuBufferMgr {
  64. public:
  65. EXPORT GpuBufferMgr() : cur_dev_id_(0), init_(false), closed_(false), open_by_dataset_(0) {}
  66. EXPORT virtual ~GpuBufferMgr() = default;
  67. EXPORT static GpuBufferMgr &GetInstance() noexcept;
  68. EXPORT BlockQueueStatus_T Create(unsigned int device_id, const std::string &channel_name, void *addr,
  69. const std::vector<size_t> &shape, const size_t &capacity);
  70. // call for Push thread
  71. EXPORT unsigned int Open(unsigned int device_id, const std::string &channel_name, const std::vector<size_t> &shape,
  72. std::function<void(void *)> func);
  73. // call for Front/Pop thread
  74. EXPORT unsigned int Open(unsigned int device_id, const std::string &channel_name, const std::vector<size_t> &shape);
  75. EXPORT BlockQueueStatus_T Push(unsigned int handle, const std::vector<DataItemGpu> &data,
  76. unsigned int timeout_in_sec);
  77. EXPORT BlockQueueStatus_T Front(unsigned int handle, void **addr, size_t *len);
  78. EXPORT BlockQueueStatus_T Pop(unsigned int handle);
  79. EXPORT void set_device_id(int device_id);
  80. EXPORT void Close(unsigned int handle) noexcept;
  81. EXPORT bool IsInit() const;
  82. EXPORT bool IsClosed() const;
  83. EXPORT bool Destroy();
  84. // call for Release GPU Resources
  85. EXPORT bool CloseNotify();
  86. // call for dataset send thread
  87. EXPORT void CloseConfirm();
  88. private:
  89. void set_device() const;
  90. int cur_dev_id_;
  91. bool init_;
  92. bool closed_;
  93. std::mutex mutex_;
  94. std::mutex close_mutex_;
  95. // how many queues opened by dataset
  96. int open_by_dataset_;
  97. Semaphore sema;
  98. HandleMgr handle_mgr_;
  99. std::map<unsigned int, std::shared_ptr<BlockingQueue>> handle_queue_map_;
  100. std::map<std::string, std::shared_ptr<BlockingQueue>> name_queue_map_;
  101. inline bool isCreated(unsigned int device_id, const std::string &channel_name);
  102. GpuBufferMgr(const GpuBufferMgr &) = delete;
  103. GpuBufferMgr &operator=(const GpuBufferMgr &) = delete;
  104. };
  105. } // namespace device
  106. } // namespace mindspore
  107. #endif // MINDSPORE_CCSRC_DEVICE_GPU_GPU_BUFFER_MGR_H_