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