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 "opbase_stream_manager.h"
12 : #include "log.h"
13 : #include "exception_util.h"
14 : #include "communicator_impl.h"
15 : #include "invalid_params_exception.h"
16 :
17 : namespace Hccl {
18 :
19 465 : OpbaseStreamManager::~OpbaseStreamManager() { DECTOR_TRY_CATCH("OpbaseStreamManager", Clear()); }
20 :
21 17 : void OpbaseStreamManager::ReplaceMaster(std::unique_ptr<Stream> stream)
22 : {
23 17 : master = std::move(stream);
24 17 : master->SetStmMode(HrtStreamGetMode(master->GetPtr()));
25 17 : }
26 :
27 23 : void OpbaseStreamManager::RegisterMaster(std::unique_ptr<Stream> stream)
28 : {
29 69 : HCCL_INFO("[OpbaseStreamManager::%s] start.", __func__);
30 :
31 23 : if (master == nullptr) {
32 16 : ReplaceMaster(std::move(stream));
33 48 : HCCL_INFO("[OpbaseStreamManager::%s] end, master is nullptr.", __func__);
34 16 : return;
35 : }
36 7 : if (*master == *stream) {
37 18 : HCCL_INFO("[OpbaseStreamManager::%s] end, master is same as stream.", __func__);
38 6 : return;
39 : }
40 1 : ReplaceMaster(std::move(stream));
41 :
42 3 : HCCL_INFO("[OpbaseStreamManager::%s] end.", __func__);
43 : }
44 :
45 466 : void OpbaseStreamManager::Clear() { slaves.clear(); }
46 :
47 16 : Stream* OpbaseStreamManager::GetOrCreateSlave()
48 : {
49 48 : HCCL_INFO("[OpbaseStreamManager::%s] start.", __func__);
50 :
51 : // 如果从流不够用,就申请一个新的从流
52 16 : u32 slavesSize = slaves.size();
53 48 : HCCL_INFO("[OpbaseStreamManager::%s] slavesSize[%u] slaveIndex[%u]", __func__, slavesSize, slaveIndex);
54 16 : if (slaveIndex >= slavesSize) {
55 16 : slaves.emplace_back(std::make_unique<Stream>(comm->GetOpAiCpuTSFeatureFlag(), false)); // 算子粒度
56 16 : if (master != nullptr && !comm->GetOpAiCpuTSFeatureFlag()) { // 算子粒度
57 15 : slaves[slaveIndex]->SetStmMode(master->GetMode());
58 : }
59 : }
60 :
61 48 : HCCL_INFO("[OpbaseStreamManager::%s] end, get index[%u] slave stream", __func__, slaveIndex);
62 16 : return slaves[slaveIndex++].get();
63 : }
64 :
65 15 : Stream* OpbaseStreamManager::GetSlave(u32 index) const
66 : {
67 15 : if (index >= slaves.size()) {
68 0 : THROW<InvalidParamsException>(StringFormat("[OpbaseStreamManager::%s] index[%u] is invalid.", __func__, index));
69 : }
70 15 : return slaves[index].get();
71 : }
72 :
73 : } // namespace Hccl
|