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 701 : ContextManager::ContextManager()
18 : {
19 702 : }
20 :
21 702 : ContextManager::~ContextManager()
22 : {
23 702 : }
24 :
25 23 : HcclResult ContextManager::CreateCommEngineCtx(const std::string &tag, CommEngine engine, uint64_t size, void **ctx)
26 : {
27 23 : std::lock_guard<std::mutex> lock(mutex_);
28 : // 阻止重复创建
29 23 : if (contextMap_.find(tag) != contextMap_.end()) {
30 3 : auto engineCtxMap = contextMap_[tag];
31 3 : CHK_PRT_RET(engineCtxMap.find(engine) != engineCtxMap.end(),
32 : HCCL_ERROR("[%s] already exist a context with same key, tag[%s], engine[%s]",
33 : __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str()), HCCL_E_PARA);
34 3 : }
35 :
36 21 : void* ctxData = nullptr;
37 : // 区分设备类型
38 : HcclMemType type;
39 21 : if (engine == COMM_ENGINE_CPU || engine == COMM_ENGINE_CPU_TS
40 6 : || engine == COMM_ENGINE_CCU) {
41 15 : type = HCCL_MEM_TYPE_HOST;
42 15 : ctxData = malloc(size);
43 15 : CHK_PTR_NULL(ctxData);
44 15 : s32 sRet = memset_s(ctxData, size, 0, size);
45 15 : if (sRet != EOK) {
46 0 : HCCL_ERROR("[%s] memset_s failed, ret[%d]", __func__, sRet);
47 0 : free(ctxData);
48 0 : ctxData = nullptr;
49 0 : return HCCL_E_INTERNAL;
50 : }
51 21 : } else if (engine == COMM_ENGINE_AICPU || engine == COMM_ENGINE_AICPU_TS
52 1 : || engine == COMM_ENGINE_AIV) {
53 5 : type = HCCL_MEM_TYPE_DEVICE;
54 5 : CHK_RET(hrtMalloc(&ctxData, size));
55 4 : } else {
56 1 : HCCL_ERROR("[%s] not support engine type[%s]", __func__, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
57 1 : return HCCL_E_PARA;
58 : }
59 :
60 19 : contextMap_[tag][engine] = {type, ctxData, size};
61 19 : *ctx = contextMap_[tag][engine].addr;
62 19 : HCCL_INFO("[%s]create context success, tag[%s], engine[%s]", __func__, tag.c_str(), 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("[%s] not exist a context with tag[%s], engine[%s]", __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
78 3 : return HCCL_E_PARA;
79 : }
80 7 : }
81 :
82 4 : *ctx = contextMap_[tag][engine].addr;
83 4 : *size = contextMap_[tag][engine].size;
84 4 : HCCL_INFO("[%s]get context success, tag[%s], engine[%s]", __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
85 4 : return HCCL_SUCCESS;
86 9 : }
87 :
88 0 : HcclResult ContextManager::CopyCommEngineCtx(const std::string &tag, CommEngine engine, const void *srcCtx,
89 : uint64_t size, uint64_t dstCtxOffset)
90 : {
91 : void *dstCtx;
92 0 : uint64_t dstSize = 0;
93 0 : if (engine == COMM_ENGINE_AICPU_TS || engine == COMM_ENGINE_AICPU
94 0 : || engine == COMM_ENGINE_AIV) {
95 0 : CHK_RET(GetCommEngineCtx(tag, engine, &dstCtx, &dstSize));
96 : // 从Host内存拷贝到Device Context内存上
97 0 : CHK_RET(hrtMemSyncCopy(reinterpret_cast<uint8_t*>(dstCtx) + dstCtxOffset, size, srcCtx, size,
98 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
99 0 : } else if (engine == COMM_ENGINE_CPU || engine == COMM_ENGINE_CPU_TS
100 0 : || engine == COMM_ENGINE_CCU) {
101 0 : CHK_RET(GetCommEngineCtx(tag, engine, &dstCtx, &dstSize));
102 0 : (void)memcpy_s(reinterpret_cast<uint8_t*>(dstCtx) + dstCtxOffset, size, srcCtx, size);
103 0 : } else {
104 0 : HCCL_ERROR("[%s]copy engine ctx failed, Unsupported engine[%s], tag[%s]", __func__, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), tag.c_str());
105 0 : return HCCL_E_PARA;
106 : }
107 0 : HCCL_INFO("[%s]copy engine ctx success, tag[%s], engine[%s]", __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
108 0 : return HCCL_SUCCESS;
109 : }
110 :
111 20 : HcclResult ContextManager::DestroyCommEngineCtx(const std::string &tag, CommEngine engine)
112 : {
113 20 : std::lock_guard<std::mutex> lock(mutex_);
114 : // Ctx不存在返回错误
115 20 : if (contextMap_.find(tag) == contextMap_.end()) {
116 2 : HCCL_ERROR("[%s] not exist a context with tag[%s]", __func__, tag.c_str());
117 2 : return HCCL_E_PARA;
118 : }
119 18 : auto& engineCtxMap = contextMap_[tag];
120 18 : if (engineCtxMap.find(engine) == engineCtxMap.end()) {
121 1 : HCCL_ERROR("[%s] not exist a context with tag[%s], engine[%s]", __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
122 1 : return HCCL_E_PARA;
123 : }
124 : // 获取内存信息
125 17 : HcclMem& memInfo = engineCtxMap[engine];
126 : // 释放内存
127 17 : if (memInfo.type == HCCL_MEM_TYPE_HOST) {
128 13 : free(memInfo.addr);
129 4 : } else if (memInfo.type == HCCL_MEM_TYPE_DEVICE) {
130 4 : CHK_RET(hrtFree(memInfo.addr));
131 : } else {
132 0 : HCCL_ERROR("[%s] invalid memory type[%d]", __func__, memInfo.type);
133 0 : return HCCL_E_PARA;
134 : }
135 : // 从映射中移除
136 17 : engineCtxMap.erase(engine);
137 17 : if (engineCtxMap.empty()) {
138 16 : contextMap_.erase(tag);
139 : }
140 :
141 17 : HCCL_INFO("[%s]destroy context success, tag[%s], engine[%s]", __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
142 17 : return HCCL_SUCCESS;
143 20 : }
144 : }
|