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 4.2 kB

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