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.

memory_manager.h 4.9 kB

6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
5 years ago
6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_RUNTIME_DEVICE_MEMORY_MANAGER_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_DEVICE_MEMORY_MANAGER_H_
  18. #include <memory>
  19. #include <utility>
  20. #include <vector>
  21. #include <map>
  22. #include <queue>
  23. #include "backend/optimizer/mem_reuse/mem_reuse.h"
  24. #include "backend/optimizer/somas/somas.h"
  25. #include "runtime/device/memory_scheduler.h"
  26. namespace mindspore {
  27. namespace device {
  28. enum MemType { kStaticMem, kDynamicMem, kSomasReuseDynamicMem };
  29. constexpr int kGetAllOuts = -1;
  30. constexpr uint64_t kMemAlignSize = 512;
  31. constexpr uint64_t kTwiceMemAlignSize = kMemAlignSize << 1;
  32. using SomasPtr = mindspore::somas::SomasPtr;
  33. class MemoryManager : public MemHandler {
  34. public:
  35. MemoryManager() = default;
  36. virtual ~MemoryManager() = default;
  37. virtual void MallocDeviceMemory() = 0;
  38. virtual void FreeDeviceMemory() = 0;
  39. virtual void ResetDynamicMemory() {}
  40. virtual void ClearGlobalIdleMem() {}
  41. virtual void MallocSomasDynamicMem(const session::KernelGraph &graph);
  42. uint8_t *MallocOutputMem(const AnfNodePtr &node, size_t index, MemType type, size_t size,
  43. const DeviceAddressPtr &address, bool comm_mem);
  44. uint8_t *MallocWorkSpaceMem(const AnfNodePtr &node, size_t index, MemType type, size_t size);
  45. virtual uint8_t *MallocMem(MemType type, size_t size, const DeviceAddressPtr &address,
  46. uint32_t graph_id = kInvalidGraphId);
  47. virtual bool MallocMemFromMemPool(const DeviceAddressPtr address, size_t size);
  48. virtual void *MallocMemFromMemPool(size_t size);
  49. virtual uint8_t *MallocCommunicationMemFromMemPool(size_t size) { return nullptr; }
  50. virtual void FreeMemFromMemPool(const DeviceAddressPtr address);
  51. virtual void FreeMemFromMemPool(void *device_ptr);
  52. virtual bool MallocContinuousMemFromMemPool(const DeviceAddressPtrList &addr_list, size_t total_size,
  53. std::vector<size_t> size_list);
  54. virtual std::vector<void *> MallocContinuousMemFromMemPool(size_t total_size, std::vector<size_t> size_list);
  55. static size_t GetCommonAlignSize(size_t input_size);
  56. static size_t GetCommunicationAlignSize(size_t input_size);
  57. // swap manager interface
  58. void *MallocDevice(size_t mem_size) override { return MallocMemFromMemPool(mem_size); }
  59. void FreeDevice(void *ptr) override {
  60. MS_EXCEPTION_IF_NULL(ptr);
  61. FreeMemFromMemPool(ptr);
  62. }
  63. void *MallocHost(size_t mem_size) override {
  64. auto &mem_que = cached_host_mem_[mem_size];
  65. if (!mem_que.empty()) {
  66. auto ret = mem_que.front();
  67. mem_que.pop();
  68. return ret;
  69. }
  70. auto block = std::make_shared<std::vector<uint8_t>>();
  71. try {
  72. block->resize(mem_size, 0);
  73. auto ptr = block->data();
  74. host_mem_block_map_[ptr] = block;
  75. return ptr;
  76. } catch (const std::exception &e) {
  77. MS_LOG(EXCEPTION) << "Malloc memory failed: size " << mem_size;
  78. }
  79. }
  80. void FreeHost(void *ptr) override {
  81. MS_EXCEPTION_IF_NULL(ptr);
  82. auto iter = host_mem_block_map_.find(ptr);
  83. if (iter == host_mem_block_map_.end()) {
  84. MS_LOG(ERROR) << "Free ptr not be created from manager!";
  85. }
  86. auto mem_size = iter->second->size();
  87. cached_host_mem_[mem_size].emplace(iter->first);
  88. }
  89. void SwapIn(const void *host_ptr, void *device_ptr, size_t mem_size, void *stream) override {
  90. MS_LOG(INFO) << "Call default swap in " << host_ptr << "," << device_ptr << "," << mem_size << "," << stream;
  91. }
  92. void SwapOut(const void *device_ptr, void *host_ptr, size_t mem_size, void *stream) override {
  93. MS_LOG(INFO) << "Call default swap out " << host_ptr << "," << device_ptr << "," << mem_size << "," << stream;
  94. }
  95. size_t GetAvailableMemSize() override {
  96. MS_LOG(ERROR) << "Return default 0 mem size!";
  97. return 0;
  98. }
  99. protected:
  100. virtual uint8_t *MallocStaticMem(size_t size, bool communication_mem, uint32_t graph_id = kInvalidGraphId) = 0;
  101. virtual uint8_t *MallocDynamicMem(size_t size, bool communication_mem);
  102. SomasPtr somas_reuse_util_ptr_{nullptr};
  103. std::map<size_t, std::queue<void *>> cached_host_mem_;
  104. std::map<void *, std::shared_ptr<std::vector<uint8_t>>> host_mem_block_map_;
  105. };
  106. } // namespace device
  107. } // namespace mindspore
  108. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_MEMORY_MANAGER_H_