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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_BLOCKING_QUEUE_H_
  17. #define MINDSPORE_CCSRC_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 <condition_variable>
  26. #include <functional>
  27. namespace mindspore {
  28. namespace device {
  29. enum BlockQueueStatus_T : int { SUCCESS = 0, QUEUE_NOT_EXIST, HANDLE_NOT_EXIST, ERROR_INPUT, INTERNAL_ERROR, TIMEOUT };
  30. class GpuQueue {
  31. public:
  32. GpuQueue(void* addr, size_t feature_size, size_t label_size, size_t capacity);
  33. virtual ~GpuQueue();
  34. void RegisterRelease(const std::function<void(void*)>& func) { host_release_ = func; }
  35. inline bool IsEmpty() const { return head_ == tail_; }
  36. inline bool IsFull() const { return head_ == ((tail_ + 1) % (capacity_)); }
  37. BlockQueueStatus_T Push(void* feature_addr, size_t feature_size, void* label_addr, size_t label_size);
  38. BlockQueueStatus_T Front(void** feature_addr, size_t* feature_size, void** label_addr, size_t* label_size) const;
  39. BlockQueueStatus_T Pop();
  40. bool Destroy();
  41. private:
  42. struct NodeInfo {
  43. std::unique_ptr<cudaEvent_t> event_;
  44. void* host_feature_addr_;
  45. void* host_label_addr_;
  46. };
  47. void* buffer_;
  48. size_t head_;
  49. size_t tail_;
  50. size_t feature_size_;
  51. size_t label_size_;
  52. size_t capacity_;
  53. cudaStream_t stream_;
  54. std::unique_ptr<NodeInfo[]> node_info_;
  55. std::function<void(void*)> host_release_;
  56. GpuQueue(const GpuQueue&) = delete;
  57. GpuQueue& operator=(const GpuQueue&) = delete;
  58. };
  59. class BlockingQueue {
  60. public:
  61. BlockingQueue() : queue_(nullptr) {}
  62. ~BlockingQueue() = default;
  63. BlockQueueStatus_T Create(void* addr, size_t feature_size, size_t label_size, size_t capacity);
  64. void RegisterRelease(const std::function<void(void*)>& func);
  65. BlockQueueStatus_T Push(void* feature_addr, size_t feature_size, void* label_addr, size_t label_size,
  66. unsigned int timeout_in_sec);
  67. BlockQueueStatus_T Front(void** feature_addr, size_t* feature_size, void** label_addr, size_t* label_size);
  68. BlockQueueStatus_T Pop();
  69. bool Destroy();
  70. private:
  71. std::mutex mutex_;
  72. std::condition_variable not_full_cond_;
  73. std::condition_variable not_empty_cond_;
  74. std::shared_ptr<GpuQueue> queue_;
  75. };
  76. } // namespace device
  77. } // namespace mindspore
  78. #endif // MINDSPORE_CCSRC_DEVICE_GPU_BLOCKING_QUEUE_H_