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