Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "independent_op_context_manager.h"
12 : #include "log.h"
13 : #include "adapter_rts_common.h"
14 : #include "comm_engine_utils.h"
15 :
16 : namespace hccl {
17 798 : ContextManager::ContextManager() {}
18 :
19 799 : ContextManager::~ContextManager() {}
20 :
21 23 : HcclResult ContextManager::CreateCommEngineCtx(const std::string& tag, CommEngine engine, uint64_t size, void** ctx)
22 : {
23 23 : std::lock_guard<std::mutex> lock(mutex_);
24 : // 阻止重复创建
25 23 : if (contextMap_.find(tag) != contextMap_.end()) {
26 3 : auto engineCtxMap = contextMap_[tag];
27 3 : CHK_PRT_RET(
28 : engineCtxMap.find(engine) != engineCtxMap.end(),
29 : HCCL_ERROR(
30 : "[%s] already exist a context with same key, tag[%s], engine[%s]", __func__, tag.c_str(),
31 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str()),
32 : HCCL_E_PARA);
33 3 : }
34 :
35 21 : void* ctxData = nullptr;
36 : // 区分设备类型
37 : HcclMemType type;
38 21 : if (engine == COMM_ENGINE_CPU || engine == COMM_ENGINE_CPU_TS || engine == COMM_ENGINE_CCU) {
39 15 : type = HCCL_MEM_TYPE_HOST;
40 15 : ctxData = malloc(size);
41 15 : CHK_PTR_NULL(ctxData);
42 15 : s32 sRet = memset_s(ctxData, size, 0, size);
43 15 : if (sRet != EOK) {
44 0 : HCCL_ERROR("[%s] memset_s failed, ret[%d]", __func__, sRet);
45 0 : free(ctxData);
46 0 : ctxData = nullptr;
47 0 : return HCCL_E_INTERNAL;
48 : }
49 21 : } else if (engine == COMM_ENGINE_AICPU || engine == COMM_ENGINE_AICPU_TS || engine == COMM_ENGINE_AIV) {
50 5 : type = HCCL_MEM_TYPE_DEVICE;
51 5 : CHK_RET(hrtMalloc(&ctxData, size));
52 4 : } else {
53 1 : HCCL_ERROR(
54 : "[%s] not support engine type[%s]", __func__, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
55 1 : return HCCL_E_PARA;
56 : }
57 :
58 19 : contextMap_[tag][engine] = {type, ctxData, size};
59 19 : *ctx = contextMap_[tag][engine].addr;
60 19 : HCCL_INFO(
61 : "[%s]create context success, tag[%s], engine[%s]", __func__, tag.c_str(),
62 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
63 :
64 19 : return HCCL_SUCCESS;
65 23 : }
66 :
67 9 : HcclResult ContextManager::GetCommEngineCtx(const std::string& tag, CommEngine engine, void** ctx, uint64_t* size)
68 : {
69 9 : std::lock_guard<std::mutex> lock(mutex_);
70 : // Ctx未创建返回
71 9 : if (contextMap_.find(tag) == contextMap_.end()) {
72 2 : HCCL_INFO("[%s] not exist a context with tag[%s]", __func__, tag.c_str());
73 2 : return HCCL_E_PARA;
74 : } else {
75 7 : auto engineCtxMap = contextMap_[tag];
76 7 : if (engineCtxMap.find(engine) == engineCtxMap.end()) {
77 3 : HCCL_INFO(
78 : "[%s] not exist a context with tag[%s], engine[%s]", __func__, tag.c_str(),
79 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
80 3 : return HCCL_E_PARA;
81 : }
82 7 : }
83 :
84 4 : *ctx = contextMap_[tag][engine].addr;
85 4 : *size = contextMap_[tag][engine].size;
86 4 : HCCL_INFO(
87 : "[%s]get context success, tag[%s], engine[%s]", __func__, tag.c_str(),
88 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
89 4 : return HCCL_SUCCESS;
90 9 : }
91 :
92 0 : HcclResult ContextManager::CopyCommEngineCtx(
93 : const std::string& tag, CommEngine engine, const void* srcCtx, uint64_t size, uint64_t dstCtxOffset)
94 : {
95 : void* dstCtx;
96 0 : uint64_t dstSize = 0;
97 0 : if (engine == COMM_ENGINE_AICPU_TS || engine == COMM_ENGINE_AICPU || engine == COMM_ENGINE_AIV) {
98 0 : CHK_RET(GetCommEngineCtx(tag, engine, &dstCtx, &dstSize));
99 : // 从Host内存拷贝到Device Context内存上
100 0 : CHK_RET(hrtMemSyncCopy(
101 : reinterpret_cast<uint8_t*>(dstCtx) + dstCtxOffset, size, srcCtx, size,
102 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
103 0 : } else if (engine == COMM_ENGINE_CPU || engine == COMM_ENGINE_CPU_TS || engine == COMM_ENGINE_CCU) {
104 0 : CHK_RET(GetCommEngineCtx(tag, engine, &dstCtx, &dstSize));
105 0 : (void)memcpy_s(reinterpret_cast<uint8_t*>(dstCtx) + dstCtxOffset, size, srcCtx, size);
106 0 : } else {
107 0 : HCCL_ERROR(
108 : "[%s]copy engine ctx failed, Unsupported engine[%s], tag[%s]", __func__,
109 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), tag.c_str());
110 0 : return HCCL_E_PARA;
111 : }
112 0 : HCCL_INFO(
113 : "[%s]copy engine ctx success, tag[%s], engine[%s]", __func__, tag.c_str(),
114 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
115 0 : return HCCL_SUCCESS;
116 : }
117 :
118 20 : HcclResult ContextManager::DestroyCommEngineCtx(const std::string& tag, CommEngine engine)
119 : {
120 20 : std::lock_guard<std::mutex> lock(mutex_);
121 : // Ctx不存在返回错误
122 20 : if (contextMap_.find(tag) == contextMap_.end()) {
123 2 : HCCL_ERROR("[%s] not exist a context with tag[%s]", __func__, tag.c_str());
124 2 : return HCCL_E_PARA;
125 : }
126 18 : auto& engineCtxMap = contextMap_[tag];
127 18 : if (engineCtxMap.find(engine) == engineCtxMap.end()) {
128 1 : HCCL_ERROR(
129 : "[%s] not exist a context with tag[%s], engine[%s]", __func__, tag.c_str(),
130 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
131 1 : return HCCL_E_PARA;
132 : }
133 : // 获取内存信息
134 17 : HcclMem& memInfo = engineCtxMap[engine];
135 : // 释放内存
136 17 : if (memInfo.type == HCCL_MEM_TYPE_HOST) {
137 13 : free(memInfo.addr);
138 4 : } else if (memInfo.type == HCCL_MEM_TYPE_DEVICE) {
139 4 : CHK_RET(hrtFree(memInfo.addr));
140 : } else {
141 0 : HCCL_ERROR("[%s] invalid memory type[%d]", __func__, memInfo.type);
142 0 : return HCCL_E_PARA;
143 : }
144 : // 从映射中移除
145 17 : engineCtxMap.erase(engine);
146 17 : if (engineCtxMap.empty()) {
147 16 : contextMap_.erase(tag);
148 : }
149 :
150 17 : HCCL_INFO(
151 : "[%s]destroy context success, tag[%s], engine[%s]", __func__, tag.c_str(),
152 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
153 17 : return HCCL_SUCCESS;
154 20 : }
155 : } // namespace hccl
|