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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "backend/optimizer/mem_reuse/mem_reuse.h"
  22. #include "backend/optimizer/mem_reuse/mem_reuse_allocator.h"
  23. #include "backend/optimizer/somas/somas.h"
  24. namespace mindspore {
  25. namespace device {
  26. enum MemType { kStaticMem, kDynamicMem, kReuseDynamicMem, kSomasReuseDynamicMem, kReuseDynamicCommMem };
  27. const int kGetAllOuts = -1;
  28. const uint64_t kMemAlignSize = 512;
  29. using MemReuseUtilPtr = mindspore::memreuse::MemReuseUtilPtr;
  30. using SomasPtr = mindspore::somas::SomasPtr;
  31. class MemoryManager {
  32. public:
  33. MemoryManager() = default;
  34. virtual ~MemoryManager() = default;
  35. virtual void MallocDeviceMemory() = 0;
  36. virtual void FreeDeviceMemory() = 0;
  37. virtual void ResetDynamicMemory() {
  38. total_dynamic_size_ = 0;
  39. dynamic_mem_offset_ = 0;
  40. }
  41. virtual void ClearGlobalIdleMem() {}
  42. void MallocReusedDynamicMem(const session::KernelGraph *graph);
  43. virtual void MallocSomasDynamicMem(const session::KernelGraph *graph);
  44. uint8_t *MallocOutputMem(const AnfNodePtr &node, size_t index, MemType type, size_t size,
  45. const DeviceAddressPtr &address, bool comm_mem);
  46. uint8_t *MallocWorkSpaceMem(const AnfNodePtr &node, size_t index, MemType type, size_t size);
  47. virtual uint8_t *MallocMem(MemType type, size_t size, const DeviceAddressPtr &address,
  48. uint32_t graph_id = kInvalidGraphId);
  49. virtual bool MallocMemFromMemPool(const DeviceAddressPtr address, size_t size);
  50. virtual void *MallocMemFromMemPool(size_t size);
  51. virtual void FreeMemFromMemPool(const DeviceAddressPtr address);
  52. virtual void FreeMemFromMemPool(void *device_ptr);
  53. virtual bool MallocContinuousMemFromMemPool(const DeviceAddressPtrList addr_list, size_t total_size,
  54. std::vector<size_t> size_list);
  55. virtual std::vector<void *> MallocContinuousMemFromMemPool(size_t total_size, std::vector<size_t> size_list);
  56. size_t GetCommonAlignSize(size_t input_size) const;
  57. size_t GetCommunicationAlignSize(size_t input_size) const;
  58. protected:
  59. virtual uint8_t *MallocStaticMem(size_t size, bool communication_mem, uint32_t graph_id = kInvalidGraphId);
  60. virtual uint8_t *MallocDynamicMem(size_t size, bool communication_mem);
  61. uint8_t *device_mem_base_{nullptr};
  62. uint64_t device_mem_size_{0};
  63. uint64_t dynamic_mem_offset_{0};
  64. uint64_t static_mem_offset_{0};
  65. size_t total_static_size_ = 0;
  66. size_t total_dynamic_size_ = 0;
  67. MemReuseUtilPtr mem_reuse_util_ptr_{nullptr};
  68. SomasPtr somas_reuse_util_ptr_{nullptr};
  69. };
  70. } // namespace device
  71. } // namespace mindspore
  72. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_MEMORY_MANAGER_H_