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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <atomic>
  19. #include <deque>
  20. #include <iostream>
  21. #include <map>
  22. #include <memory>
  23. #include <mutex>
  24. #include "minddata/dataset/util/allocator.h"
  25. #include "minddata/dataset/util/system_pool.h"
  26. #include "minddata/dataset/util/semaphore.h"
  27. #include "minddata/dataset/util/services.h"
  28. namespace mindspore {
  29. namespace dataset {
  30. template <typename K, typename T>
  31. /// \brief QueueMap is like a Queue but instead of there is a map of deque<T>.
  32. /// Consumer will block if the corresponding deque is empty.
  33. /// Producer can add an element of type T with key of type K to the map and
  34. /// wake up any waiting consumer.
  35. /// \tparam K key type
  36. /// \tparam T payload of the map
  37. class QueueMap {
  38. public:
  39. using key_type = K;
  40. using value_type = T;
  41. QueueMap() : num_rows_(0) {}
  42. virtual ~QueueMap() = default;
  43. /// Add an element <key, T> to the map and wake up any consumer that is waiting
  44. /// \param key
  45. /// \param payload
  46. /// \return Status object
  47. virtual Status Add(key_type key, T &&payload) {
  48. RequestQueue *rq = nullptr;
  49. RETURN_IF_NOT_OK(GetRq(key, &rq));
  50. RETURN_IF_NOT_OK(rq->WakeUpAny(std::move(payload)));
  51. ++num_rows_;
  52. return Status::OK();
  53. }
  54. /// Pop the front of the deque with key. Block if the deque is empty.
  55. virtual Status PopFront(key_type key, T *out) {
  56. RequestQueue *rq = nullptr;
  57. RETURN_IF_NOT_OK(GetRq(key, &rq));
  58. RETURN_IF_NOT_OK(rq->Wait(out));
  59. --num_rows_;
  60. return Status::OK();
  61. }
  62. /// Get the number of elements in the container
  63. /// \return The number of elements in the container
  64. int64_t size() const { return num_rows_; }
  65. /// \return if the container is empty
  66. bool empty() const { return num_rows_ == 0; }
  67. /// Print out some useful information about the container
  68. friend std::ostream &operator<<(std::ostream &out, const QueueMap &qm) {
  69. std::unique_lock<std::mutex> lck(qm.mux_);
  70. out << "Number of elements: " << qm.num_rows_ << "\n";
  71. out << "Dumping internal info:\n";
  72. int64_t k = 0;
  73. for (auto &it : qm.all_) {
  74. auto key = it.first;
  75. const RequestQueue *rq = it.second.GetPointer();
  76. out << "(k:" << key << "," << *rq << ") ";
  77. ++k;
  78. if (k % 6 == 0) {
  79. out << "\n";
  80. }
  81. }
  82. return out;
  83. }
  84. protected:
  85. /// This is a handshake structure between producer and consumer
  86. class RequestQueue {
  87. public:
  88. RequestQueue() : use_count_(0) {}
  89. ~RequestQueue() = default;
  90. Status Wait(T *out) {
  91. RETURN_UNEXPECTED_IF_NULL(out);
  92. // Block until the missing row is in the pool.
  93. RETURN_IF_NOT_OK(use_count_.P());
  94. std::unique_lock<std::mutex> lck(dq_mux_);
  95. CHECK_FAIL_RETURN_UNEXPECTED(!row_.empty(), "Programming error");
  96. *out = std::move(row_.front());
  97. row_.pop_front();
  98. return Status::OK();
  99. }
  100. Status WakeUpAny(T &&row) {
  101. std::unique_lock<std::mutex> lck(dq_mux_);
  102. row_.push_back(std::move(row));
  103. // Bump up the use count by 1. This wake up any parallel worker which is waiting
  104. // for this row.
  105. use_count_.V();
  106. return Status::OK();
  107. }
  108. friend std::ostream &operator<<(std::ostream &out, const RequestQueue &rq) {
  109. out << "sz:" << rq.row_.size() << ",uc:" << rq.use_count_.Peek();
  110. return out;
  111. }
  112. private:
  113. mutable std::mutex dq_mux_;
  114. Semaphore use_count_;
  115. std::deque<T> row_;
  116. };
  117. /// Create or locate an element with matching key
  118. /// \param key
  119. /// \param out
  120. /// \return Status object
  121. Status GetRq(key_type key, RequestQueue **out) {
  122. RETURN_UNEXPECTED_IF_NULL(out);
  123. std::unique_lock<std::mutex> lck(mux_);
  124. auto it = all_.find(key);
  125. if (it != all_.end()) {
  126. *out = it->second.GetMutablePointer();
  127. } else {
  128. // We will create a new one.
  129. auto alloc = SystemPool::GetAllocator<RequestQueue>();
  130. auto r = all_.emplace(key, MemGuard<RequestQueue, Allocator<RequestQueue>>(alloc));
  131. if (r.second) {
  132. auto &mem = r.first->second;
  133. RETURN_IF_NOT_OK(mem.allocate(1));
  134. *out = mem.GetMutablePointer();
  135. } else {
  136. RETURN_STATUS_UNEXPECTED("Map insert fail.");
  137. }
  138. }
  139. return Status::OK();
  140. }
  141. private:
  142. mutable std::mutex mux_;
  143. std::map<K, MemGuard<RequestQueue, Allocator<RequestQueue>>> all_;
  144. std::atomic<int64_t> num_rows_;
  145. };
  146. } // namespace dataset
  147. } // namespace mindspore
  148. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_QUEUE_MAP_H_