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_v2.h"
12 : #include "communicator_impl.h"
13 : namespace Hccl {
14 :
15 617 : QueueNotifyManager::QueueNotifyManager(const CommunicatorImpl& comm) : comm(const_cast<CommunicatorImpl*>(&comm)) {}
16 :
17 617 : QueueNotifyManager::~QueueNotifyManager() { DECTOR_TRY_CATCH("QueueNotifyManager", Destroy()); }
18 :
19 10 : void QueueNotifyManager::ApplyFor(QId postQid, QId waitQid, u32 topicId)
20 : {
21 10 : if (topicId > MAX_NUM_FOR_QPAIR) {
22 : string msg = StringFormat(
23 : "topicId=%d, Exceed max notify number %d for queue tuple {postQid=%d, waitQid=%d}", topicId,
24 0 : MAX_NUM_FOR_QPAIR, postQid, waitQid);
25 0 : THROW<InvalidParamsException>(msg);
26 0 : }
27 10 : const auto& tuple = std::make_tuple(postQid, waitQid, topicId);
28 10 : if (notifyPool[tuple] == nullptr) {
29 8 : notifyPool[tuple] = std::make_unique<RtsNotify>(comm->GetOpAiCpuTSFeatureFlag()); // 算子粒度
30 : }
31 10 : }
32 :
33 2 : bool QueueNotifyManager::Release(QId postQid, QId waitQid, u32 topicId)
34 : {
35 2 : if (!IsExist(postQid, waitQid, topicId)) {
36 3 : HCCL_WARNING("Notify for postQid[%u] and waitQid[%u] does not exist, no need to release.", postQid, waitQid);
37 1 : return true;
38 : }
39 :
40 1 : notifyPool.erase(std::make_tuple(postQid, waitQid, topicId));
41 1 : return true;
42 : }
43 :
44 618 : bool QueueNotifyManager::Destroy()
45 : {
46 618 : notifyPool.clear();
47 618 : return true;
48 : }
49 :
50 0 : RtsNotify* QueueNotifyManager::Get(QId postQid, QId waitQid, u32 topicId)
51 : {
52 0 : if (!IsExist(postQid, waitQid, topicId)) {
53 0 : HCCL_WARNING("Notify for postQid[%u] and waitQid[%u] does not exist", postQid, waitQid);
54 0 : return nullptr;
55 : }
56 :
57 0 : return notifyPool[std::make_tuple(postQid, waitQid, topicId)].get();
58 : }
59 :
60 2 : bool QueueNotifyManager::IsExist(QId postQid, QId waitQid, u32 topicId)
61 : {
62 2 : return notifyPool.count(std::make_tuple(postQid, waitQid, topicId)) != 0;
63 : }
64 :
65 : constexpr u8 QUEUE_NOTIFY_POST_QID_POS = 0;
66 : constexpr u8 QUEUE_NOTIFY_WAIT_QID_POS = 1;
67 : constexpr u8 QUEUE_NOTIFY_TOPIC_ID_POS = 2;
68 :
69 3 : std::vector<char> QueueNotifyManager::GetPackedData()
70 : {
71 3 : std::vector<char> result;
72 3 : BinaryStream binaryStream;
73 :
74 3 : u32 poolSize = notifyPool.size();
75 3 : binaryStream << poolSize;
76 :
77 6 : for (auto& it : notifyPool) {
78 3 : binaryStream << (std::get<QUEUE_NOTIFY_POST_QID_POS>(it.first));
79 3 : binaryStream << (std::get<QUEUE_NOTIFY_WAIT_QID_POS>(it.first));
80 3 : binaryStream << (std::get<QUEUE_NOTIFY_TOPIC_ID_POS>(it.first));
81 3 : binaryStream << it.second->GetUniqueId();
82 9 : HCCL_INFO(
83 : "QueueNotifyManager::GetPackedData: postQid=%u, waitQid=%u, topicId=%u, %s",
84 : (std::get<QUEUE_NOTIFY_POST_QID_POS>(it.first)), (std::get<QUEUE_NOTIFY_WAIT_QID_POS>(it.first)),
85 : (std::get<QUEUE_NOTIFY_TOPIC_ID_POS>(it.first)), it.second->Describe().c_str());
86 : }
87 3 : binaryStream.Dump(result);
88 3 : return result;
89 3 : }
90 :
91 : } // namespace Hccl
|