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 "queue_notify_manager.h"
12 : #include <algorithm>
13 : #include "device_capacity.h"
14 : #include "adapter_rts_common.h"
15 :
16 : namespace hccl {
17 : constexpr u32 NOTIFY_MAX_NUM = 2048;
18 : const std::string HCCL_ALLTOALL = "ALLTOALL";
19 1050 : QueueNotifyManager::QueueNotifyManager() {}
20 :
21 1050 : QueueNotifyManager::~QueueNotifyManager()
22 : {
23 1050 : HcclResult ret = Destroy();
24 1050 : if (ret != HCCL_SUCCESS) {
25 0 : HCCL_WARNING("destroy QueueNotifyManager resources failed, ret[%d]", ret);
26 : }
27 1050 : }
28 :
29 1050 : HcclResult QueueNotifyManager::Init()
30 : {
31 1050 : notifies_.reserve(NOTIFY_MAX_NUM);
32 1050 : notifiesForA2A_.reserve(NOTIFY_MAX_NUM);
33 1050 : deviceNotifies_.reserve(NOTIFY_MAX_NUM);
34 1050 : return HCCL_SUCCESS;
35 : }
36 :
37 116 : HcclResult QueueNotifyManager::Alloc(
38 : const std::string& tag, u32 notifyNum, std::vector<std::shared_ptr<LocalNotify>>& localNotifys,
39 : const NotifyLoadType type)
40 : {
41 116 : if (type == NotifyLoadType::HOST_NOTIFY) {
42 110 : std::string upTag = tag;
43 112 : std::transform(upTag.begin(), upTag.end(), upTag.begin(), ::toupper);
44 109 : bool hasAlltoAll = upTag.find(HCCL_ALLTOALL) != std::string::npos;
45 110 : HCCL_INFO("RegisterOp hasAlltoAll[%d]", hasAlltoAll);
46 112 : NotifyPoolNoIPC& notifies = hasAlltoAll ? notifiesForA2A_ : notifies_;
47 112 : CHK_RET(AllocNotifies(type, notifies, notifyNum));
48 112 : localNotifys.assign(notifies.begin(), notifies.begin() + notifyNum);
49 118 : } else if (type == NotifyLoadType::DEVICE_NOTIFY) { // 申请device上使用的notify资源
50 6 : CHK_RET(AllocNotifies(type, deviceNotifies_, notifyNum));
51 6 : localNotifys.assign(deviceNotifies_.begin(), deviceNotifies_.begin() + notifyNum);
52 : }
53 118 : return HCCL_SUCCESS;
54 : }
55 :
56 118 : HcclResult QueueNotifyManager::AllocNotifies(const NotifyLoadType type, NotifyPoolNoIPC& notifies, u32 notifyNum)
57 : {
58 118 : if (notifies.size() < notifyNum) {
59 580 : for (u32 i = notifies.size(); i < notifyNum; i++) {
60 520 : notifies.emplace_back(nullptr);
61 517 : CHK_RET(CreateNotify(notifies[i], type));
62 520 : CHK_SMART_PTR_NULL(notifies[i]);
63 : }
64 : }
65 118 : return HCCL_SUCCESS;
66 : }
67 :
68 517 : HcclResult QueueNotifyManager::CreateNotify(std::shared_ptr<LocalNotify>& localNotify, const NotifyLoadType type)
69 : {
70 517 : EXCEPTION_CATCH((localNotify = std::make_shared<LocalNotify>()), return HCCL_E_PTR);
71 :
72 514 : HcclResult ret = HCCL_SUCCESS;
73 514 : bool errorFlag = false;
74 : do {
75 514 : ret = localNotify->Init(type);
76 516 : CHK_PRT_BREAK(
77 : ret != HCCL_SUCCESS,
78 : HCCL_ERROR(
79 : "[QueueNotifyManager][CreateNotify]localNotify init failed, "
80 : "ret[%d]",
81 : ret),
82 : errorFlag = true);
83 :
84 516 : HCCL_DEBUG("Is310PDevice[%d]", Is310PDevice());
85 520 : if (Is310PDevice()) {
86 0 : ret = localNotify->SetIpc();
87 0 : CHK_PRT_BREAK(
88 : ret != HCCL_SUCCESS,
89 : HCCL_ERROR(
90 : "[QueueNotifyManager][CreateNotify]localNotify "
91 : "set ipc failed, ret[%d]",
92 : ret),
93 : errorFlag = true);
94 : }
95 : } while (0);
96 :
97 520 : if (errorFlag) {
98 0 : HCCL_ERROR("[QueueNotifyManager][CreateNotify]localNotify create failed ,ret[%d]", ret);
99 0 : localNotify = nullptr;
100 0 : return ret;
101 : }
102 :
103 520 : return HCCL_SUCCESS;
104 : }
105 :
106 1050 : HcclResult QueueNotifyManager::Destroy()
107 : {
108 1050 : HCCL_INFO("QueueNotifyManager Destroy.");
109 :
110 1050 : CHK_RET(DestroyNotifies(notifies_));
111 1050 : CHK_RET(DestroyNotifies(deviceNotifies_));
112 1050 : CHK_RET(DestroyNotifies(notifiesForA2A_));
113 :
114 1049 : HCCL_INFO("QueueNotifyManager Destroy success.");
115 1050 : return HCCL_SUCCESS;
116 : }
117 :
118 3150 : HcclResult QueueNotifyManager::DestroyNotifies(NotifyPoolNoIPC& notifies)
119 : {
120 3670 : for (auto& notify : notifies) {
121 520 : CHK_SMART_PTR_NULL(notify);
122 520 : CHK_RET(notify->Destroy());
123 : }
124 3149 : notifies.clear();
125 3148 : return HCCL_SUCCESS;
126 : }
127 :
128 0 : HcclResult QueueNotifyManager::ResetNotify()
129 : {
130 0 : for (auto& localNotify : deviceNotifies_) {
131 0 : CHK_SMART_PTR_NULL(localNotify);
132 0 : CHK_RET(hrtNotifyReset(localNotify->ptr()));
133 : }
134 0 : return HCCL_SUCCESS;
135 : }
136 : } // namespace hccl
|