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 3.9 kB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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) {}
  32. MemoryInfo(uint8_t *memory_addr, size_t memory_size)
  33. : memory_addr_(memory_addr), memory_size_(memory_size), memory_used_num_(0) {}
  34. MemoryInfo &operator=(const MemoryInfo &op) {
  35. if (&op == this) {
  36. return *this;
  37. }
  38. this->memory_addr_ = op.memory_addr_;
  39. this->memory_size_ = op.memory_size_;
  40. this->memory_used_num_ = op.memory_used_num_;
  41. return *this;
  42. }
  43. MemoryInfo(const MemoryInfo &op) {
  44. this->memory_addr_ = op.memory_addr_;
  45. this->memory_size_ = op.memory_size_;
  46. this->memory_used_num_ = op.memory_used_num_;
  47. }
  48. virtual ~MemoryInfo() = default;
  49. uint8_t *memory_addr_;
  50. uint64_t memory_size_;
  51. int32_t memory_used_num_;
  52. };
  53. class MemoryAllocator {
  54. public:
  55. explicit MemoryAllocator(rtMemType_t memory_type) : memory_type_(memory_type), mem_malloced_(false) {}
  56. virtual ~MemoryAllocator() = default;
  57. ///
  58. /// @ingroup ge_graph
  59. /// @brief memory allocator init
  60. /// @param [in] options user config params
  61. /// @return Status of init
  62. ///
  63. Status Initialize(uint32_t device_id = 0);
  64. ///
  65. /// @ingroup ge_graph
  66. /// @brief memory allocator finalize
  67. /// @return void
  68. ///
  69. void Finalize(uint32_t device_id = 0);
  70. ///
  71. /// @ingroup ge_graph
  72. /// @brief malloc memory
  73. /// @param [in] purpose memory usage
  74. /// @param [in] size memory size
  75. /// @param [in] device_id device id
  76. /// @return memory address
  77. ///
  78. uint8_t *MallocMemory(const string &purpose, size_t memory_size, uint32_t device_id = 0) const;
  79. ///
  80. /// @ingroup ge_graph
  81. /// @brief free memory
  82. /// @param [in] device_id device id
  83. /// @param [out] memory_ptr memory address ptr
  84. /// @return Status result of function
  85. ///
  86. Status FreeMemory(uint8_t *memory_addr, uint32_t device_id = 0) const;
  87. ///
  88. /// @ingroup ge_graph
  89. /// @brief malloc memory
  90. /// @param [in] purpose memory usage
  91. /// @param [in] memory_key memory key
  92. /// @param [in] size memory size
  93. /// @param [in] device_id device id
  94. /// @return memory address
  95. ///
  96. uint8_t *MallocMemory(const string &purpose, const string &memory_key, size_t memory_size,
  97. uint32_t device_id = 0);
  98. ///
  99. /// @ingroup ge_graph
  100. /// @brief free memory
  101. /// @param [in] memory_key memory key
  102. /// @param [in] device_id device id
  103. /// @return Status result of function
  104. ///
  105. Status FreeMemory(const string &memory_key, uint32_t device_id = 0);
  106. ///
  107. /// @ingroup ge_graph
  108. /// @brief get memory address
  109. /// @param [in] memory_key memory key
  110. /// @param [in] device_id device id
  111. /// @return memory address (must not free memory by it)
  112. ///
  113. uint8_t *GetMemoryAddr(const string &memory_key, uint32_t device_id = 0);
  114. private:
  115. rtMemType_t memory_type_;
  116. bool mem_malloced_;
  117. map<string, MemoryInfo> memory_base_map_;
  118. };
  119. } // namespace ge
  120. #endif // GE_GRAPH_MANAGER_GRAPH_MEM_ALLOCATOR_H_

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