Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 <cstring>
12 :
13 : #include "hcomm_c_adpt.h"
14 : #include "hcomm_res_defs.h"
15 : #include "log.h"
16 : #include "param_check_pub.h"
17 : #include "hcom_common.h"
18 : #include "comm_engine_utils.h"
19 :
20 11 : HcommResult HcommEngineCtxCreate(CommEngine engine, uint64_t size, void** ctx)
21 : {
22 11 : CHK_PTR_NULL(ctx);
23 10 : if (engine == COMM_ENGINE_CPU || engine == COMM_ENGINE_CPU_TS || engine == COMM_ENGINE_CCU) {
24 7 : *ctx = malloc(size);
25 7 : CHK_PTR_NULL(*ctx);
26 7 : auto ret = memset_s(*ctx, size, 0, size);
27 7 : if (ret != EOK) {
28 0 : HCCL_ERROR("[%s] memset_s failed, ret[%d]", __func__, ret);
29 0 : free(*ctx);
30 0 : *ctx = nullptr;
31 0 : return HCCL_E_INTERNAL;
32 : }
33 10 : } else if (engine == COMM_ENGINE_AICPU || engine == COMM_ENGINE_AICPU_TS || engine == COMM_ENGINE_AIV) {
34 2 : CHK_RET(hrtMalloc(ctx, size));
35 2 : } else {
36 1 : HCCL_ERROR(
37 : "[%s] not support engine type[%s]", __func__, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
38 1 : return HCCL_E_PARA;
39 : }
40 9 : return HCCL_SUCCESS;
41 : }
42 :
43 12 : HcommResult HcommEngineCtxDestroy(CommEngine engine, void* ctx)
44 : {
45 12 : CHK_PTR_NULL(ctx);
46 11 : if (engine == COMM_ENGINE_CPU || engine == COMM_ENGINE_CPU_TS || engine == COMM_ENGINE_CCU) {
47 8 : free(ctx);
48 3 : } else if (engine == COMM_ENGINE_AICPU || engine == COMM_ENGINE_AICPU_TS || engine == COMM_ENGINE_AIV) {
49 2 : CHK_RET(hrtFree(ctx));
50 2 : } else {
51 1 : HCCL_ERROR("[%s] invalid engine[%s]", __func__, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
52 1 : return HCCL_E_PARA;
53 : }
54 10 : return HCCL_SUCCESS;
55 : }
56 :
57 6 : HcommResult HcommEngineCtxCopy(CommEngine engine, void* dstCtx, const void* srcCtx, uint64_t size)
58 : {
59 6 : CHK_PTR_NULL(dstCtx);
60 5 : CHK_PTR_NULL(srcCtx);
61 4 : if (engine == COMM_ENGINE_AICPU_TS || engine == COMM_ENGINE_AICPU || engine == COMM_ENGINE_AIV) {
62 : // 从Host内存拷贝到Device Context内存上
63 0 : CHK_RET(hrtMemSyncCopy(
64 : reinterpret_cast<uint8_t*>(dstCtx), size, srcCtx, size,
65 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
66 4 : } else if (engine == COMM_ENGINE_CPU || engine == COMM_ENGINE_CPU_TS || engine == COMM_ENGINE_CCU) {
67 4 : CHK_SAFETY_FUNC_RET(memcpy_s(reinterpret_cast<uint8_t*>(dstCtx), size, srcCtx, size));
68 4 : } else {
69 0 : HCCL_ERROR(
70 : "[%s]copy engine ctx failed, Unsupported engine[%s]", __func__,
71 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
72 0 : return HCCL_E_PARA;
73 : }
74 4 : HCCL_INFO(
75 : "[%s]copy engine ctx success, engine[%s]", __func__,
76 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
77 4 : return HCCL_SUCCESS;
78 : }
|