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.

blocking_queue.h 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_RUNTIME_DEVICE_GPU_BLOCKING_QUEUE_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_BLOCKING_QUEUE_H_
  18. #include <unistd.h>
  19. #include <cuda_runtime_api.h>
  20. #include <iostream>
  21. #include <memory>
  22. #include <mutex>
  23. #include <cstring>
  24. #include <string>
  25. #include <vector>
  26. #include <condition_variable>
  27. #include <functional>
  28. namespace mindspore {
  29. namespace device {
  30. enum BlockQueueStatus_T : int { SUCCESS = 0, QUEUE_EXIST, HANDLE_NOT_EXIST, ERROR_INPUT, INTERNAL_ERROR, TIMEOUT };
  31. struct DataItemGpu {
  32. int32_t worker_id_;
  33. std::string data_type_;
  34. size_t data_len_;
  35. void *data_ptr_;
  36. };
  37. class GpuQueue {
  38. public:
  39. GpuQueue(void *addr, const std::vector<size_t> &shape, const size_t &capacity);
  40. virtual ~GpuQueue();
  41. void RegisterRelease(const std::function<void(void *, int32_t)> &func) { host_release_ = func; }
  42. inline bool IsEmpty() const { return size_ == 0; }
  43. inline bool IsFull() const { return size_ == capacity_; }
  44. BlockQueueStatus_T Push(const std::vector<DataItemGpu> &data);
  45. BlockQueueStatus_T Front(void **ptr, size_t *len) const;
  46. BlockQueueStatus_T Pop();
  47. bool Destroy();
  48. size_t Size() { return size_; }
  49. size_t Capacity() { return capacity_; }
  50. private:
  51. struct NodeInfo {
  52. std::unique_ptr<cudaEvent_t> event_;
  53. std::vector<DataItemGpu> data_;
  54. };
  55. void *buffer_;
  56. size_t head_;
  57. size_t tail_;
  58. std::vector<size_t> shape_;
  59. size_t len_;
  60. size_t size_;
  61. size_t capacity_;
  62. cudaStream_t stream_;
  63. std::unique_ptr<NodeInfo[]> node_info_;
  64. std::function<void(void *, int32_t)> host_release_;
  65. GpuQueue(const GpuQueue &) = delete;
  66. GpuQueue &operator=(const GpuQueue &) = delete;
  67. };
  68. class BlockingQueue {
  69. public:
  70. BlockingQueue() : queue_(nullptr) {}
  71. ~BlockingQueue() = default;
  72. BlockQueueStatus_T Create(void *addr, const std::vector<size_t> &shape, const size_t &capacity);
  73. void RegisterRelease(const std::function<void(void *, int32_t)> &func);
  74. BlockQueueStatus_T Push(const std::vector<DataItemGpu> &data, unsigned int timeout_in_sec);
  75. BlockQueueStatus_T Front(void **ptr, size_t *len);
  76. BlockQueueStatus_T Pop();
  77. bool Destroy();
  78. size_t Size() { return queue_->Size(); }
  79. size_t Capacity() { return queue_->Capacity(); }
  80. private:
  81. std::mutex mutex_;
  82. std::condition_variable not_full_cond_;
  83. std::condition_variable not_empty_cond_;
  84. std::shared_ptr<GpuQueue> queue_;
  85. };
  86. } // namespace device
  87. } // namespace mindspore
  88. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_BLOCKING_QUEUE_H_