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 HCCL_OPRETRY_MANAGER_H
12 : #define HCCL_OPRETRY_MANAGER_H
13 : #include <thread>
14 : #include <mutex>
15 : #include "opretry_base.h"
16 :
17 : namespace hccl {
18 : struct RetryCtrl {
19 : std::unique_ptr<std::thread> thread;
20 : std::shared_ptr<RetryContext> retryCtx = nullptr;
21 : bool startExec = false;
22 : };
23 :
24 : class OpRetryManager {
25 : public:
26 0 : OpRetryManager() = default;
27 0 : ~OpRetryManager()
28 : {
29 0 : HCCL_DEBUG("Destroy OpRetryManager");
30 0 : (void)DeInit();
31 0 : }
32 : HcclResult RegisterOpRetryMachine(
33 : OpRetryAgentParam& agentParam, u32 rankSize, bool isRoot,
34 : std::map<u32, std::shared_ptr<HcclSocket>>& serverConnections, const OpRetryServerInfo& serverInfo);
35 : HcclResult UnRegisterOpRetryManager(const std::string& group);
36 :
37 : static HcclResult AddLinkInfoByIdentifier(
38 : s32 deviceLogicID, const std::string& identifier, const std::string& newTag, std::vector<u32>& remoteRankList,
39 : bool incre = false);
40 : static HcclResult GetLinkInfoByIdentifier(
41 : s32 deviceLogicID, const std::string& identifier, const std::string& newTag, std::vector<u32>& remoteRankList,
42 : bool isGetGroupAllRemoteRank = false);
43 : static HcclResult DeleteLinkInfoByIdentifier(s32 deviceLogicID, const std::string& identifier);
44 : HcclResult SetRetryStateToWaitResume(const std::string& group, bool isRoot);
45 : HcclResult
46 : ExitWaitResumeState(const std::string& group, bool isRoot, bool haveCommEnableBackupLink, bool& isChangedLink);
47 : bool IsPaused(const std::string& group);
48 : bool IsResumed(const std::string& group);
49 :
50 : private:
51 : HcclResult Init();
52 0 : HcclResult DeInit()
53 : {
54 0 : std::unique_lock<std::mutex> lock(ProcessLock_);
55 0 : if (initialized_) {
56 0 : initialized_ = false;
57 0 : for (auto it = agentOpRetry_.begin(); it != agentOpRetry_.end(); ++it) {
58 0 : if (it->second.thread != nullptr && it->second.thread->joinable()) {
59 0 : it->second.thread->join();
60 : }
61 : }
62 0 : agentOpRetry_.clear();
63 :
64 0 : for (auto it = serverOpRetry.begin(); it != serverOpRetry.end(); ++it) {
65 0 : if (it->second.thread != nullptr && it->second.thread->joinable()) {
66 0 : it->second.thread->join();
67 : }
68 : }
69 0 : serverOpRetry.clear();
70 0 : HCCL_INFO("OpRetryManager DeInit success");
71 : }
72 0 : return HCCL_SUCCESS;
73 0 : }
74 : HcclResult RegisterAgentRetryMachine(OpRetryAgentParam& agentParam);
75 : HcclResult RegisterServerRetryMachine(
76 : const std::string& group, std::map<u32, std::shared_ptr<HcclSocket>>& serverConnections,
77 : const OpRetryAgentInfo& agentInfo);
78 : void RetryStateMonitor(
79 : const std::string& group, std::shared_ptr<RetryContext> retryCtx, const bool& startExec, HcclRtContext rtCtx_);
80 :
81 : private:
82 : std::map<std::string, RetryCtrl> serverOpRetry;
83 : std::map<std::string, RetryCtrl> agentOpRetry_;
84 : bool initialized_ = false;
85 : std::mutex ProcessLock_;
86 : };
87 : } // namespace hccl
88 : #endif
|