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.h 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 DATASET_UTIL_CIRCULAR_POOL_H_
  17. #define DATASET_UTIL_CIRCULAR_POOL_H_
  18. #include <atomic>
  19. #include <memory>
  20. #include <vector>
  21. #include "dataset/util/memory_pool.h"
  22. #include "dataset/util/arena.h"
  23. #include "dataset/util/lock.h"
  24. namespace mindspore {
  25. namespace dataset {
  26. using ListOfArenas = std::vector<std::shared_ptr<Arena>>;
  27. // This is a dynamic memory pool built on top of memory
  28. // segment each of which is 4G in size. Initially we start
  29. // with one segment, and gradually add segments (not
  30. // guaranteed contiguous) until we reach 32G in size. There
  31. // is an assumption about this kind of memory pool. Allocated
  32. // memory is not held for the whole duration of the pool and
  33. // will be released soon. Based on this assumption, memory is
  34. // obtained from the tail while allocated memory is returned
  35. // to the head of the pool.
  36. class CircularPool : public MemoryPool {
  37. public:
  38. class CircularIterator {
  39. friend class CircularPool;
  40. public:
  41. explicit CircularIterator(CircularPool *dp);
  42. ~CircularIterator() = default;
  43. bool has_next() const;
  44. ListOfArenas::iterator Next();
  45. void Reset();
  46. private:
  47. CircularPool *dp_;
  48. Arena *cur_tail_{};
  49. uint32_t start_{};
  50. uint32_t cur_{};
  51. bool wrap_{};
  52. bool has_next_{};
  53. };
  54. CircularPool(const CircularPool &) = delete;
  55. CircularPool &operator=(const CircularPool &) = delete;
  56. ~CircularPool() override;
  57. Status Allocate(size_t n, void **) override;
  58. Status Reallocate(void **, size_t old_size, size_t new_size) override;
  59. void Deallocate(void *) override;
  60. uint64_t get_max_size() const override;
  61. int PercentFree() const override;
  62. friend std::ostream &operator<<(std::ostream &os, const CircularPool &s) {
  63. int i = 0;
  64. for (auto it = s.mem_segments_.begin(); it != s.mem_segments_.end(); ++it, ++i) {
  65. os << "Dumping segment " << i << "\n" << *(it->get());
  66. }
  67. return os;
  68. }
  69. static Status CreateCircularPool(std::shared_ptr<MemoryPool> *out_pool, int max_size_in_gb = -1,
  70. int arena_size = 4096, bool create_one_arena = false);
  71. private:
  72. ListOfArenas mem_segments_;
  73. std::atomic<Arena *> tail_{};
  74. bool unlimited_;
  75. int max_size_in_mb_;
  76. int arena_size_;
  77. int cur_size_in_mb_;
  78. RWLock rw_lock_;
  79. // We can take negative or 0 as input which means unlimited.
  80. CircularPool(int max_size_in_gb, int arena_size);
  81. Status AddOneArena();
  82. };
  83. } // namespace dataset
  84. } // namespace mindspore
  85. #endif // DATASET_UTIL_CIRCULAR_POOL_H_