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.

circular_pool.cc 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #include "minddata/dataset/util/circular_pool.h"
  17. #include <algorithm>
  18. #include <limits>
  19. #include <utility>
  20. #include "./securec.h"
  21. #include "minddata/dataset/util/log_adapter.h"
  22. #include "minddata/dataset/util/system_pool.h"
  23. namespace mindspore {
  24. namespace dataset {
  25. Status CircularPool::AddOneArena() {
  26. Status rc;
  27. std::shared_ptr<Arena> b;
  28. #ifdef ENABLE_GPUQUE
  29. RETURN_IF_NOT_OK(Arena::CreateArena(&b, arena_size_, is_cuda_malloc_));
  30. #else
  31. RETURN_IF_NOT_OK(Arena::CreateArena(&b, arena_size_));
  32. #endif
  33. tail_ = b.get();
  34. cur_size_in_mb_ += arena_size_;
  35. mem_segments_.push_back(std::move(b));
  36. return Status::OK();
  37. }
  38. ListOfArenas::iterator CircularPool::CircularIterator::Next() {
  39. ListOfArenas::iterator it = dp_->mem_segments_.begin();
  40. uint32_t size = dp_->mem_segments_.size();
  41. // This is what we return
  42. it += cur_;
  43. // Prepare for the next round
  44. cur_++;
  45. if (cur_ == size) {
  46. if (start_ == 0) {
  47. has_next_ = false;
  48. } else {
  49. wrap_ = true;
  50. cur_ = 0;
  51. }
  52. } else if (cur_ == start_) {
  53. has_next_ = false;
  54. }
  55. return it;
  56. }
  57. bool CircularPool::CircularIterator::has_next() const { return has_next_; }
  58. void CircularPool::CircularIterator::Reset() {
  59. wrap_ = false;
  60. has_next_ = false;
  61. if (!dp_->mem_segments_.empty()) {
  62. // Find the buddy arena that corresponds to the tail.
  63. cur_tail_ = dp_->tail_;
  64. auto list_end = dp_->mem_segments_.end();
  65. auto it = std::find_if(dp_->mem_segments_.begin(), list_end,
  66. [this](const std::shared_ptr<Arena> &b) { return b.get() == cur_tail_; });
  67. MS_ASSERT(it != list_end);
  68. start_ = std::distance(dp_->mem_segments_.begin(), it);
  69. cur_ = start_;
  70. has_next_ = true;
  71. }
  72. }
  73. CircularPool::CircularIterator::CircularIterator(CircularPool *dp) : dp_(dp) { Reset(); }
  74. Status CircularPool::Allocate(size_t n, void **p) {
  75. if (p == nullptr) {
  76. RETURN_STATUS_UNEXPECTED("p is null");
  77. }
  78. Status rc;
  79. void *ptr = nullptr;
  80. do {
  81. SharedLock lock_s(&rw_lock_);
  82. int prevSzInMB = cur_size_in_mb_;
  83. bool move_tail = false;
  84. CircularIterator cirIt(this);
  85. while (cirIt.has_next()) {
  86. auto it = cirIt.Next();
  87. Arena *ba = it->get();
  88. if (ba->get_max_size() < n) {
  89. return Status(StatusCode::kMDOutOfMemory);
  90. }
  91. // If we are asked to move forward the tail
  92. if (move_tail) {
  93. Arena *expected = cirIt.cur_tail_;
  94. (void)atomic_compare_exchange_weak(&tail_, &expected, ba);
  95. move_tail = false;
  96. }
  97. rc = ba->Allocate(n, &ptr);
  98. if (rc.IsOk()) {
  99. *p = ptr;
  100. break;
  101. } else if (rc == StatusCode::kMDOutOfMemory) {
  102. // Make the next arena a new tail and continue.
  103. move_tail = true;
  104. } else {
  105. return rc;
  106. }
  107. }
  108. // Handle the case we have done one round robin search.
  109. if (ptr == nullptr) {
  110. // If we have room to expand.
  111. if (unlimited_ || cur_size_in_mb_ < max_size_in_mb_) {
  112. // lock in exclusively mode.
  113. lock_s.Upgrade();
  114. // Check again if someone has already expanded.
  115. if (cur_size_in_mb_ == prevSzInMB) {
  116. RETURN_IF_NOT_OK(AddOneArena());
  117. }
  118. // Re-acquire the shared lock and try again
  119. lock_s.Downgrade();
  120. } else {
  121. return Status(StatusCode::kMDOutOfMemory, __LINE__, __FILE__);
  122. }
  123. }
  124. } while (ptr == nullptr);
  125. return rc;
  126. }
  127. void CircularPool::Deallocate(void *p) {
  128. // Lock in the chain in shared mode and find out which
  129. // segment it comes from
  130. SharedLock lock(&rw_lock_);
  131. auto it = std::find_if(mem_segments_.begin(), mem_segments_.end(), [this, p](std::shared_ptr<Arena> &b) -> bool {
  132. char *q = reinterpret_cast<char *>(p);
  133. auto *base = reinterpret_cast<const char *>(b->get_base_addr());
  134. return (q > base && q < base + arena_size_ * 1048576L);
  135. });
  136. lock.Unlock();
  137. MS_ASSERT(it != mem_segments_.end());
  138. it->get()->Deallocate(p);
  139. }
  140. Status CircularPool::Reallocate(void **pp, size_t old_sz, size_t new_sz) {
  141. // Lock in the chain in shared mode and find out which
  142. // segment it comes from
  143. if (pp == nullptr) {
  144. RETURN_STATUS_UNEXPECTED("pp is null");
  145. }
  146. void *p = *pp;
  147. SharedLock lock(&rw_lock_);
  148. auto it = std::find_if(mem_segments_.begin(), mem_segments_.end(), [this, p](std::shared_ptr<Arena> &b) -> bool {
  149. char *q = reinterpret_cast<char *>(p);
  150. auto *base = reinterpret_cast<const char *>(b->get_base_addr());
  151. return (q > base && q < base + arena_size_ * 1048576L);
  152. });
  153. lock.Unlock();
  154. MS_ASSERT(it != mem_segments_.end());
  155. Arena *ba = it->get();
  156. Status rc = ba->Reallocate(pp, old_sz, new_sz);
  157. if (rc == StatusCode::kMDOutOfMemory) {
  158. // The current arena has no room for the bigger size.
  159. // Allocate free space from another arena and copy
  160. // the content over.
  161. void *q = nullptr;
  162. rc = this->Allocate(new_sz, &q);
  163. RETURN_IF_NOT_OK(rc);
  164. errno_t err = memcpy_s(q, new_sz, p, old_sz);
  165. if (err) {
  166. this->Deallocate(q);
  167. RETURN_STATUS_UNEXPECTED(std::to_string(err));
  168. }
  169. *pp = q;
  170. ba->Deallocate(p);
  171. }
  172. return Status::OK();
  173. }
  174. uint64_t CircularPool::get_max_size() const { return mem_segments_.front()->get_max_size(); }
  175. int CircularPool::PercentFree() const {
  176. int percent_free = 0;
  177. int num_arena = 0;
  178. for (auto const &p : mem_segments_) {
  179. percent_free += p->PercentFree();
  180. num_arena++;
  181. }
  182. if (num_arena) {
  183. return percent_free / num_arena;
  184. } else {
  185. return 100;
  186. }
  187. }
  188. #ifdef ENABLE_GPUQUE
  189. CircularPool::CircularPool(int max_size_in_gb, int arena_size, bool is_cuda_malloc)
  190. : unlimited_(max_size_in_gb <= 0),
  191. max_size_in_mb_(unlimited_ ? std::numeric_limits<int32_t>::max() : max_size_in_gb * 1024),
  192. arena_size_(arena_size),
  193. is_cuda_malloc_(is_cuda_malloc),
  194. cur_size_in_mb_(0) {}
  195. #else
  196. CircularPool::CircularPool(int max_size_in_gb, int arena_size)
  197. : unlimited_(max_size_in_gb <= 0),
  198. max_size_in_mb_(unlimited_ ? std::numeric_limits<int32_t>::max() : max_size_in_gb * 1024),
  199. arena_size_(arena_size),
  200. cur_size_in_mb_(0) {}
  201. #endif
  202. #ifdef ENABLE_GPUQUE
  203. Status CircularPool::CreateCircularPool(std::shared_ptr<MemoryPool> *out_pool, int max_size_in_gb, int arena_size,
  204. bool createOneArena, bool is_cuda_malloc) {
  205. Status rc;
  206. if (out_pool == nullptr) {
  207. RETURN_STATUS_UNEXPECTED("pPool is null");
  208. }
  209. auto pool = new (std::nothrow) CircularPool(max_size_in_gb, arena_size, is_cuda_malloc);
  210. if (pool == nullptr) {
  211. return Status(StatusCode::kMDOutOfMemory);
  212. }
  213. if (createOneArena) {
  214. rc = pool->AddOneArena();
  215. }
  216. if (rc.IsOk()) {
  217. (*out_pool).reset(pool);
  218. } else {
  219. delete pool;
  220. }
  221. return rc;
  222. }
  223. #else
  224. Status CircularPool::CreateCircularPool(std::shared_ptr<MemoryPool> *out_pool, int max_size_in_gb, int arena_size,
  225. bool createOneArena) {
  226. Status rc;
  227. if (out_pool == nullptr) {
  228. RETURN_STATUS_UNEXPECTED("pPool is null");
  229. }
  230. auto pool = new (std::nothrow) CircularPool(max_size_in_gb, arena_size);
  231. if (pool == nullptr) {
  232. return Status(StatusCode::kMDOutOfMemory);
  233. }
  234. if (createOneArena) {
  235. rc = pool->AddOneArena();
  236. }
  237. if (rc.IsOk()) {
  238. (*out_pool).reset(pool);
  239. } else {
  240. delete pool;
  241. }
  242. return rc;
  243. }
  244. #endif
  245. CircularPool::~CircularPool() = default;
  246. } // namespace dataset
  247. } // namespace mindspore