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

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