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.

graph_mem_allocator.h 4.2 kB

5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Copyright 2019-2020 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 GE_GRAPH_MANAGER_GRAPH_MEM_ALLOCATOR_H_
  17. #define GE_GRAPH_MANAGER_GRAPH_MEM_ALLOCATOR_H_
  18. #include <iostream>
  19. #include <map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <string>
  23. #include <vector>
  24. #include "framework/common/debug/ge_log.h"
  25. #include "framework/common/ge_inner_error_codes.h"
  26. #include "graph/node.h"
  27. #include "runtime/mem.h"
  28. namespace ge {
  29. class MemoryInfo {
  30. public:
  31. MemoryInfo() : memory_addr_(nullptr), memory_size_(0), memory_used_num_(0), device_id_(0) {}
  32. MemoryInfo(uint8_t *memory_addr, size_t memory_size)
  33. : memory_addr_(memory_addr), memory_size_(memory_size), memory_used_num_(0), device_id_(0) {}
  34. MemoryInfo(uint8_t *memory_addr, size_t memory_size, uint32_t device_id)
  35. : memory_addr_(memory_addr), memory_size_(memory_size), device_id_(device_id), memory_used_num_(0) {}
  36. MemoryInfo &operator=(const MemoryInfo &op) {
  37. if (&op == this) {
  38. return *this;
  39. }
  40. this->memory_addr_ = op.memory_addr_;
  41. this->memory_size_ = op.memory_size_;
  42. this->device_id_ = op.device_id_;
  43. return *this;
  44. }
  45. MemoryInfo(const MemoryInfo &op) {
  46. this->memory_addr_ = op.memory_addr_;
  47. this->memory_size_ = op.memory_size_;
  48. this->memory_used_num_ = op.memory_used_num_;
  49. this->device_id_ = op.device_id_;
  50. }
  51. virtual ~MemoryInfo() = default;
  52. uint8_t *memory_addr_;
  53. uint64_t memory_size_;
  54. int32_t memory_used_num_;
  55. uint32_t device_id_;
  56. };
  57. class MemoryAllocator {
  58. public:
  59. explicit MemoryAllocator(rtMemType_t memory_type) : memory_type_(memory_type), mem_malloced_(false) {}
  60. virtual ~MemoryAllocator() = default;
  61. ///
  62. /// @ingroup ge_graph
  63. /// @brief memory allocator init
  64. /// @param [in] options user config params
  65. /// @return Status of init
  66. ///
  67. Status Initialize(uint32_t device_id = 0);
  68. ///
  69. /// @ingroup ge_graph
  70. /// @brief memory allocator finalize
  71. /// @return void
  72. ///
  73. void Finalize(uint32_t device_id = 0);
  74. ///
  75. /// @ingroup ge_graph
  76. /// @brief malloc memory
  77. /// @param [in] purpose memory usage
  78. /// @param [in] size memory size
  79. /// @param [in] device_id device id
  80. /// @return memory address
  81. ///
  82. uint8_t *MallocMemory(const string &purpose, size_t memory_size, uint32_t device_id = 0) const;
  83. ///
  84. /// @ingroup ge_graph
  85. /// @brief free memory
  86. /// @param [in] device_id device id
  87. /// @param [out] memory_ptr memory address ptr
  88. /// @return Status result of function
  89. ///
  90. Status FreeMemory(uint8_t *memory_addr, uint32_t device_id = 0) const;
  91. ///
  92. /// @ingroup ge_graph
  93. /// @brief malloc memory
  94. /// @param [in] purpose memory usage
  95. /// @param [in] memory_key memory key
  96. /// @param [in] size memory size
  97. /// @param [in] device_id device id
  98. /// @return memory address
  99. ///
  100. uint8_t *MallocMemory(const string &purpose, const string &memory_key, size_t memory_size,
  101. uint32_t device_id = 0);
  102. ///
  103. /// @ingroup ge_graph
  104. /// @brief free memory
  105. /// @param [in] memory_key memory key
  106. /// @param [in] device_id device id
  107. /// @return Status result of function
  108. ///
  109. Status FreeMemory(const string &memory_key, uint32_t device_id = 0);
  110. ///
  111. /// @ingroup ge_graph
  112. /// @brief get memory address
  113. /// @param [in] memory_key memory key
  114. /// @param [in] device_id device id
  115. /// @return memory address (must not free memory by it)
  116. ///
  117. uint8_t *GetMemoryAddr(const string &memory_key, uint32_t device_id = 0);
  118. private:
  119. rtMemType_t memory_type_;
  120. bool mem_malloced_;
  121. map<uint32_t, map<string, MemoryInfo>> deviceid_2_memory_bases_map_;
  122. };
  123. } // namespace ge
  124. #endif // GE_GRAPH_MANAGER_GRAPH_MEM_ALLOCATOR_H_

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示