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