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

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