• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 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 
17 #ifndef MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_ASCEND_GMEM_ADAPTER_H_
18 #define MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_ASCEND_GMEM_ADAPTER_H_
19 
20 #include <atomic>
21 #include <memory>
22 
23 #include "acl/acl.h"
24 #include "utils/dlopen_macro.h"
25 #include "utils/hash_map.h"
26 #include "utils/log_adapter.h"
27 
28 #include "include/backend/mem_reuse/mem_dynamic_allocator.h"
29 #include "plugin/device/ascend/hal/common/ascend_utils.h"
30 
31 namespace mindspore {
32 namespace device {
33 namespace ascend {
34 #define CONCAT(l, r) l##r
35 // Function Object definition marco.
36 #define LIB_FUNC(func_name) CONCAT(func_name, FunObj)
37 // Function definition marco, and then can ues `LIB_FUNC(func_name)`.
38 #define DEFINE_LIB_METHOD(func_name, ...) ORIGIN_METHOD(func_name, __VA_ARGS__)
39 
40 // GMem mem free eager function name. Need to use origin name when export symbol from lib.
41 #define GMEM_FREE_EAGER gmemFreeEager
42 // Definition for GMem lib function : GMEM_FREE_EAGER.
43 DEFINE_LIB_METHOD(GMEM_FREE_EAGER, size_t, uint64_t, size_t, void *);
44 
45 class AscendGmemAdapter {
46  public:
GetInstance()47   static AscendGmemAdapter &GetInstance() {
48     static AscendGmemAdapter instance{};
49     return instance;
50   }
51 
AscendGmemAdapter()52   AscendGmemAdapter() { LoadGMemLib(); }
~AscendGmemAdapter()53   ~AscendGmemAdapter() { UnloadGMemLib(); }
54 
55  public:
56   const size_t GetRoundUpAlignSize(size_t input_size) const;
57   const size_t GetRoundDownAlignSize(size_t input_size) const;
58 
59   size_t AllocDeviceMem(size_t size, DeviceMemPtr *addr) const;
60   size_t EagerFreeDeviceMem(const DeviceMemPtr addr, const size_t size) const;
61 
62   uint8_t *MmapMemory(size_t size, void *addr) const;
63   bool MunmapMemory(void *addr, const size_t size) const;
64 
is_eager_free_enabled()65   inline const bool is_eager_free_enabled() const { return is_eager_free_enabled_; }
66 
67  private:
68   void LoadGMemLib() noexcept;
69   void UnloadGMemLib() noexcept;
70 
71   bool is_eager_free_enabled_{false};
72   void *gmem_handle_{nullptr};
73   // Function for eager free.
74   LIB_FUNC(GMEM_FREE_EAGER) free_eager_;
75 };
76 }  // namespace ascend
77 }  // namespace device
78 }  // namespace mindspore
79 
80 #endif
81