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