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.

queue_map.h 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright 2020 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_MINDDATA_DATASET_UTIL_QUEUE_MAP_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_QUEUE_MAP_H_
  18. #include <deque>
  19. #include <map>
  20. #include <memory>
  21. #include <mutex>
  22. #include "minddata/dataset/util/allocator.h"
  23. #include "minddata/dataset/util/semaphore.h"
  24. #include "minddata/dataset/util/services.h"
  25. namespace mindspore {
  26. namespace dataset {
  27. template <typename K, typename T>
  28. /// \brief QueueMap is like a Queue but instead of there is a map of deque<T>.
  29. /// Consumer will block if the corresponding deque is empty.
  30. /// Producer can add an element of type T with key of type K to the map and
  31. /// wake up any waiting consumer.
  32. /// \tparam K key type
  33. /// \tparam T payload of the map
  34. class QueueMap {
  35. public:
  36. using key_type = K;
  37. using value_type = T;
  38. QueueMap() = default;
  39. virtual ~QueueMap() = default;
  40. /// Add an element <key, T> to the map and wake up any consumer that is waiting
  41. /// \param key
  42. /// \param payload
  43. /// \return Status object
  44. virtual Status Add(key_type key, T &&payload) {
  45. RequestQueue *rq = nullptr;
  46. RETURN_IF_NOT_OK(GetRq(key, &rq));
  47. RETURN_IF_NOT_OK(rq->WakeUpAny(std::move(payload)));
  48. return Status::OK();
  49. }
  50. /// Pop the front of the deque with key. Block if the deque is empty.
  51. virtual Status PopFront(key_type key, T *out) {
  52. RequestQueue *rq = nullptr;
  53. RETURN_IF_NOT_OK(GetRq(key, &rq));
  54. RETURN_IF_NOT_OK(rq->Wait(out));
  55. return Status::OK();
  56. }
  57. protected:
  58. /// This is a handshake structure between producer and consumer
  59. class RequestQueue {
  60. public:
  61. RequestQueue() : use_count_(0) {}
  62. ~RequestQueue() = default;
  63. Status Wait(T *out) {
  64. RETURN_UNEXPECTED_IF_NULL(out);
  65. // Block until the missing row is in the pool.
  66. RETURN_IF_NOT_OK(use_count_.P());
  67. std::unique_lock<std::mutex> lck(dq_mux_);
  68. CHECK_FAIL_RETURN_UNEXPECTED(!row_.empty(), "Programming error");
  69. *out = std::move(row_.front());
  70. row_.pop_front();
  71. return Status::OK();
  72. }
  73. Status WakeUpAny(T &&row) {
  74. std::unique_lock<std::mutex> lck(dq_mux_);
  75. row_.push_back(std::move(row));
  76. // Bump up the use count by 1. This wake up any parallel worker which is waiting
  77. // for this row.
  78. use_count_.V();
  79. return Status::OK();
  80. }
  81. private:
  82. std::mutex dq_mux_;
  83. Semaphore use_count_;
  84. std::deque<T> row_;
  85. };
  86. /// Create or locate an element with matching key
  87. /// \param key
  88. /// \param out
  89. /// \return Status object
  90. Status GetRq(key_type key, RequestQueue **out) {
  91. RETURN_UNEXPECTED_IF_NULL(out);
  92. std::unique_lock<std::mutex> lck(mux_);
  93. auto it = all_.find(key);
  94. if (it != all_.end()) {
  95. *out = it->second.GetMutablePointer();
  96. } else {
  97. // We will create a new one.
  98. auto alloc = Services::GetAllocator<RequestQueue>();
  99. auto r = all_.emplace(key, MemGuard<RequestQueue, Allocator<RequestQueue>>(alloc));
  100. if (r.second) {
  101. auto &mem = r.first->second;
  102. RETURN_IF_NOT_OK(mem.allocate(1));
  103. *out = mem.GetMutablePointer();
  104. } else {
  105. RETURN_STATUS_UNEXPECTED("Map insert fail.");
  106. }
  107. }
  108. return Status::OK();
  109. }
  110. private:
  111. std::mutex mux_;
  112. std::map<K, MemGuard<RequestQueue, Allocator<RequestQueue>>> all_;
  113. };
  114. } // namespace dataset
  115. } // namespace mindspore
  116. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_QUEUE_MAP_H_