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 "dpu_kernel_entrance.h"
12 : #include "log.h"
13 : #include "acl/acl_rt.h"
14 :
15 : using namespace Hccl;
16 : using u64 = unsigned long long;
17 : std::unordered_map<std::string, std::unordered_map<uint32_t, std::unique_ptr<Hccl::TaskService>>> g_taskServiceMap;
18 : std::unordered_map<std::string, std::unordered_map<uint32_t, void*>> g_taskExpMemMap;
19 : std::mutex g_serMapMutex;
20 : extern "C" {
21 1 : __attribute__((visibility("default"))) uint32_t RunDpuRpcSrvLaunch(const uint64_t args)
22 : {
23 3 : HCCL_INFO("[%s] Launch Dpu Kernel: 0x%lx", __func__, args);
24 1 : if (args == 0) {
25 0 : HCCL_ERROR("[%s] args is null.", __func__);
26 0 : return HCCL_E_PARA;
27 : }
28 :
29 1 : if (reinterpret_cast<uint64_t>(args) + sizeof(DpuKernelLaunchParam) < reinterpret_cast<uint64_t>(args)) {
30 0 : HCCL_ERROR("[%s] Invalid args address.", __func__);
31 0 : return HCCL_E_PARA;
32 : }
33 : // 解析参数信息
34 1 : DpuKernelLaunchParam *params = reinterpret_cast<DpuKernelLaunchParam *>(args);
35 :
36 3 : HCCL_RUN_INFO("[%s] DpuKernelLaunchParam{commId:%s; memorySize:%lu; deviceMem:%p; hostMem:%p, taskExpMem:%p; devId:%u}",
37 : __func__, params->commId.c_str(), params->memorySize, params->deviceMem, params->hostMem, params->taskExpMem, params->deviceId);
38 :
39 1 : if (params->memorySize == 0) {
40 0 : HCCL_ERROR("[%s] memorySize is 0.", __func__);
41 0 : return HCCL_E_PARA;
42 : }
43 1 : if (params->deviceMem == nullptr || params->hostMem == nullptr || params->taskExpMem == nullptr) {
44 3 : HCCL_ERROR("[%s] deviceMem[%p] or hostMem[%p] or taskExpMem[%p] is nullptr.",
45 : __func__, params->deviceMem, params->hostMem, params->taskExpMem);
46 1 : return HCCL_E_PARA;
47 : }
48 :
49 : // 实例化TaskService
50 0 : std::unique_ptr<Hccl::TaskService> taskService = std::make_unique<Hccl::TaskService>(params->deviceMem, params->memorySize,
51 0 : params->hostMem, params->memorySize, params->commId, params->deviceId);
52 :
53 0 : aclError ret = aclrtSetDevice(params->deviceId);
54 0 : if (ret != ACL_SUCCESS) {
55 0 : HCCL_ERROR("[%s] set device fail. DeviceId: %u.", __func__, params->deviceId);
56 0 : return HCCL_E_RUNTIME;
57 : }
58 :
59 : // 设置到通信域中保存 map<commId, map<devid, TaskService>>与map<commId, map<devid,taskExpMem>>
60 0 : HCCL_INFO("[%s] save TaskService", __func__);
61 0 : Hccl::TaskService *svcPtr = nullptr;
62 : {
63 0 : std::lock_guard<std::mutex> lock(g_serMapMutex);
64 0 : g_taskServiceMap[params->commId][params->deviceId] = std::move(taskService);
65 0 : g_taskExpMemMap[params->commId][params->deviceId] = params->taskExpMem;
66 0 : svcPtr = g_taskServiceMap[params->commId][params->deviceId].get();
67 0 : }
68 :
69 : // Run
70 0 : HCCL_INFO("[%s] start to TaskRun", __func__);
71 0 : HcclResult hcclRet = svcPtr->TaskRun();
72 0 : if (hcclRet != HCCL_SUCCESS) {
73 0 : uint8_t newFlag = TASK_TERMINATE_RESPONSE;
74 0 : errno_t cpyRet = memcpy_s(static_cast<uint8_t *>(params->deviceMem), sizeof(newFlag), &newFlag, sizeof(newFlag));
75 0 : if (cpyRet != EOK) {
76 0 : HCCL_ERROR("set eixt flag failed: %d", cpyRet);
77 0 : return HCCL_E_INTERNAL;
78 : }
79 : }
80 :
81 : // End
82 0 : return hcclRet;
83 0 : }
84 : }
|