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.0 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <vector>
  20. #include <utility>
  21. #include "backend/optimizer/mem_reuse/mem_reuse.h"
  22. #include "backend/optimizer/mem_reuse/mem_reuse_allocator.h"
  23. namespace mindspore {
  24. namespace device {
  25. enum MemType { kStaticMem, kDynamicMem, kReuseDynamicMem, kReuseDynamicCommMem };
  26. const int kGetAllOuts = -1;
  27. const uint64_t kMemAlignSize = 512;
  28. using MemReuseUtilPtr = mindspore::memreuse::MemReuseUtilPtr;
  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. void MallocReusedDynamicMem(const session::KernelGraph *graph);
  40. uint8_t *MallocOutputMem(const AnfNodePtr &node, size_t index, MemType type, size_t size,
  41. const DeviceAddressPtr &address);
  42. uint8_t *MallocWorkSpaceMem(const AnfNodePtr &node, size_t index, MemType type, size_t size);
  43. virtual uint8_t *MallocMem(MemType type, size_t size, const DeviceAddressPtr &address);
  44. virtual bool MallocMemFromMemPool(const DeviceAddressPtr address, size_t size);
  45. virtual void *MallocMemFromMemPool(size_t size);
  46. virtual void FreeMemFromMemPool(const DeviceAddressPtr address);
  47. virtual void FreeMemFromMemPool(void *device_ptr);
  48. virtual bool MallocContinuousMemFromMemPool(const DeviceAddressPtrList addr_list, size_t total_size,
  49. std::vector<size_t> size_list);
  50. virtual std::vector<void *> MallocContinuousMemFromMemPool(size_t total_size, std::vector<size_t> size_list);
  51. size_t GetCommonAlignSize(size_t input_size) const;
  52. size_t GetCommunicationAlignSize(size_t input_size) const;
  53. protected:
  54. virtual uint8_t *MallocStaticMem(size_t size, bool communication_mem);
  55. virtual uint8_t *MallocDynamicMem(size_t size, bool communication_mem);
  56. uint8_t *device_mem_base_{nullptr};
  57. uint64_t device_mem_size_{0};
  58. uint64_t dynamic_mem_offset_{0};
  59. uint64_t static_mem_offset_{0};
  60. size_t total_static_size_ = 0;
  61. size_t total_dynamic_size_ = 0;
  62. MemReuseUtilPtr mem_reuse_util_ptr_{nullptr};
  63. };
  64. } // namespace device
  65. } // namespace mindspore
  66. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_MEMORY_MANAGER_H_