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