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