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

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