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.5 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
6 years ago
6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 uint8_t *MallocCommunicationMemFromMemPool(size_t size) { return nullptr; }
  52. virtual void FreeMemFromMemPool(const DeviceAddressPtr address);
  53. virtual void FreeMemFromMemPool(void *device_ptr);
  54. virtual bool MallocContinuousMemFromMemPool(const DeviceAddressPtrList addr_list, size_t total_size,
  55. std::vector<size_t> size_list);
  56. virtual std::vector<void *> MallocContinuousMemFromMemPool(size_t total_size, std::vector<size_t> size_list);
  57. static size_t GetCommonAlignSize(size_t input_size);
  58. size_t GetCommunicationAlignSize(size_t input_size) const;
  59. protected:
  60. virtual uint8_t *MallocStaticMem(size_t size, bool communication_mem, uint32_t graph_id = kInvalidGraphId) = 0;
  61. virtual uint8_t *MallocDynamicMem(size_t size, bool communication_mem);
  62. uint8_t *device_mem_base_{nullptr};
  63. uint64_t device_mem_size_{0};
  64. uint64_t dynamic_mem_offset_{0};
  65. uint64_t static_mem_offset_{0};
  66. size_t total_static_size_ = 0;
  67. size_t total_dynamic_size_ = 0;
  68. MemReuseUtilPtr mem_reuse_util_ptr_{nullptr};
  69. SomasPtr somas_reuse_util_ptr_{nullptr};
  70. };
  71. } // namespace device
  72. } // namespace mindspore
  73. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_MEMORY_MANAGER_H_