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 "engine_ctxs.h"
12 : #include "log.h"
13 : #include "hcomm_c_adpt.h"
14 : #include "comm_engine_utils.h"
15 :
16 : namespace hccl {
17 105 : EngineCtxs::EngineCtxs()
18 : {
19 105 : }
20 :
21 105 : EngineCtxs::~EngineCtxs()
22 : {
23 105 : }
24 :
25 7 : HcclResult EngineCtxs::CreateCommEngineCtx(const std::string &tag, CommEngine engine, uint64_t size, void **ctx)
26 : {
27 7 : std::lock_guard<std::mutex> lock(mutex_);
28 : // 阻止重复创建
29 7 : if (contextMap_.find(tag) != contextMap_.end()) {
30 1 : auto engineCtxMap = contextMap_[tag];
31 1 : 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 1 : }
35 :
36 7 : CHK_RET(static_cast<HcclResult>(HcommEngineCtxCreate(engine, size, ctx)));
37 7 : contextMap_[tag][engine] = {HCCL_MEM_TYPE_NUM, *ctx, size}; // type不需要使用
38 7 : HCCL_INFO("[%s]create context success, tag[%s], engine[%s]", __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
39 7 : return HCCL_SUCCESS;
40 7 : }
41 :
42 1 : HcclResult EngineCtxs::GetCommEngineCtx(const std::string &tag, CommEngine engine, void **ctx, uint64_t *size)
43 : {
44 1 : std::lock_guard<std::mutex> lock(mutex_);
45 : // Ctx未创建返回
46 1 : const auto &tagIter = contextMap_.find(tag);
47 1 : if (tagIter == contextMap_.end()) {
48 0 : HCCL_INFO("[%s] not exist a context with tag[%s]", __func__, tag.c_str());
49 0 : return HCCL_E_NOT_FOUND;
50 : }
51 :
52 1 : const auto &engineCtxMap = tagIter->second;
53 1 : const auto &engineIter = engineCtxMap.find(engine);
54 1 : if (engineIter == engineCtxMap.end()) {
55 0 : HCCL_INFO("[%s] not exist a context with tag[%s], engine[%s]", __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
56 0 : return HCCL_E_NOT_FOUND;
57 : }
58 :
59 1 : const auto &ctxRes = engineIter->second;
60 1 : *ctx = ctxRes.addr;
61 1 : *size = ctxRes.size;
62 1 : HCCL_INFO("[%s] get context success, tag[%s], engine[%s]", __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
63 1 : return HCCL_SUCCESS;
64 1 : }
65 :
66 1 : HcclResult EngineCtxs::CopyCommEngineCtx(const std::string &tag, CommEngine engine, const void *srcCtx,
67 : uint64_t size, uint64_t dstCtxOffset)
68 : {
69 : void *dstCtx;
70 1 : uint64_t dstSize = 0;
71 1 : CHK_RET(GetCommEngineCtx(tag, engine, &dstCtx, &dstSize));
72 1 : CHK_PRT_RET(dstCtxOffset + size > dstSize,
73 : HCCL_ERROR("[%s]Copy engine ctx failed: buffer overflow detected. tag[%s], engine[%s], "
74 : "dstSize[%llu], dstCtxOffset[%llu], copySize[%llu]",
75 : __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), dstSize, dstCtxOffset, size), HCCL_E_PARA);
76 0 : CHK_RET(static_cast<HcclResult>(HcommEngineCtxCopy(
77 : engine, reinterpret_cast<uint8_t*>(dstCtx) + dstCtxOffset, srcCtx, size))); // 增加大小判断,增加强转
78 0 : HCCL_INFO("[%s]copy engine ctx success, tag[%s], engine[%s]", __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
79 0 : return HCCL_SUCCESS;
80 : }
81 :
82 9 : HcclResult EngineCtxs::DestroyEngineCtx(const std::string &tag, CommEngine engine)
83 : {
84 9 : std::lock_guard<std::mutex> lock(mutex_);
85 : // Ctx不存在返回错误
86 9 : if (contextMap_.find(tag) == contextMap_.end()) {
87 1 : HCCL_ERROR("[%s] not exist a context with tag[%s]", __func__, tag.c_str());
88 1 : return HCCL_E_PARA;
89 : }
90 8 : auto& engineCtxMap = contextMap_[tag];
91 8 : if (engineCtxMap.find(engine) == engineCtxMap.end()) {
92 1 : HCCL_ERROR("[%s] not exist a context with tag[%s], engine[%s]", __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
93 1 : return HCCL_E_PARA;
94 : }
95 : // 获取内存信息
96 7 : HcclMem& memInfo = engineCtxMap[engine];
97 7 : CHK_RET(static_cast<HcclResult>(HcommEngineCtxDestroy(engine, memInfo.addr)));
98 : // 从映射中移除
99 7 : engineCtxMap.erase(engine);
100 7 : if (engineCtxMap.empty()) {
101 6 : contextMap_.erase(tag);
102 : }
103 :
104 7 : HCCL_INFO("[%s]destroy context success, tag[%s], engine[%s]", __func__, tag.c_str(), GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
105 7 : return HCCL_SUCCESS;
106 9 : }
107 : }
|