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 : #ifndef TASK_SERVICE_H
12 : #define TASK_SERVICE_H
13 :
14 : #include <functional>
15 : #include <unordered_map>
16 : #include <string>
17 : #include <cstdint>
18 : #include "hccl_types.h"
19 : #include "task_param.h"
20 :
21 : namespace Hccl {
22 : using CallbackTemplate = std::function<int32_t(uint64_t, int32_t)>;
23 : using ProfCallbackTemplate = std::function<HcclResult(const TaskParam&, uint64_t)>;
24 : using ReportCallbackTemplate = std::function<HcclResult()>;
25 : /**
26 : * 1. 使用共享 HBM 内存传递任务信息和数据
27 : * 2. 内存布局(shmemPtr_)分为两块等长区域:
28 : * - NPU -> DPU (npu2dpuShmem)
29 : * +----------------+---------------------------+------------------+--------------------+----------------+
30 : * | flag (uint8_t) | taskType (256字节定长空间) | msgId (uint32_t) | timeout (uint32_t) | data |
31 : * +----------------+---------------------------+------------------+--------------------+----------------+
32 : * - DPU -> NPU (dpu2npuShmem)
33 : * +----------------+---------------------------+------------------+--------------------+
34 : * | flag (uint8_t) | taskType (256字节定长空间) | msgId (uint32_t) | timeout (uint32_t) |
35 : * +----------------+---------------------------+------------------+--------------------+
36 : * 3. host侧内存装载npu2dpuShmem 的 data部分
37 : * +----------------+
38 : * | data |
39 : * +----------------+
40 : */
41 : class TaskService {
42 : public:
43 5 : TaskService() = default;
44 : TaskService(
45 : void* deviceMem, int32_t deviceMemSize, void* hostMem, int32_t hostMemSize, std::string commId, uint32_t devId);
46 : HcclResult TaskRun();
47 : HcclResult TaskRegister(std::string taskType, CallbackTemplate callback);
48 : HcclResult TaskUnRegister(std::string taskType);
49 : HcclResult TaskProfRegister(ProfCallbackTemplate profCallback);
50 : HcclResult TaskReportRegister(ReportCallbackTemplate reportCallback);
51 :
52 : private:
53 : HcclResult WriteFlag(uint8_t* flagPtr, uint8_t newFlag) const;
54 : HcclResult ReadFlag(uint8_t* ctrlHdr, uint64_t hdrLen, uint8_t& flag) const;
55 : HcclResult ReadTaskType(
56 : const uint8_t* ctrlHdr, uint64_t hdrLen, const uint8_t* srcTaskTypePtr, std::string& taskTypeStr) const;
57 : HcclResult ExecuteTask(uint8_t* ctrlHdr, uint64_t hdrLen, uint8_t* srcPtr, std::string taskTypeStr);
58 : HcclResult SynchronizeControlInfo(uint8_t* ctrlHdr, [[maybe_unused]] uint64_t hdrLen);
59 : HcclResult ProcessTaskOk(uint8_t* ctrlHdr, uint64_t hdrLen, uint8_t* srcFlagPtr, uint8_t* srcTaskTypePtr);
60 : HcclResult ExecuteTaskClean() const;
61 : HcclResult ExecuteTaskexception(int32_t ret);
62 : HcclResult ExecuteExit(uint8_t* srcFlagPtr) const;
63 :
64 : private:
65 : std::unordered_map<std::string, CallbackTemplate> callbacks_;
66 : ProfCallbackTemplate profCallback_{nullptr};
67 : ReportCallbackTemplate reportCallback_{nullptr};
68 : void* npu2dpuMem_{nullptr};
69 : void* dpu2npuMem_{nullptr};
70 : int32_t shmemSize_{0};
71 : int32_t leftSize_{0}; // npu2dpuMem_中除去控制信息后剩余的可用空间大小
72 : void* hostMem_{nullptr};
73 : int32_t hostMemSize_{0};
74 : std::string commId_{};
75 : uint32_t devId_{UINT32_MAX};
76 : };
77 : } // namespace Hccl
78 :
79 : #endif // TASK_SERVICE_H
|