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 "stream_lite_mgr.h"
11 : #include "binary_stream.h"
12 : #include "task_exception_func.h"
13 :
14 : namespace Hccl {
15 29 : StreamLite *StreamLiteMgr::GetMaster()
16 : {
17 29 : if (!streams.empty()) {
18 26 : return streams[0].get();
19 : }
20 3 : return nullptr;
21 : }
22 :
23 36 : StreamLite *StreamLiteMgr::GetSlave(u32 index)
24 : {
25 36 : if (streams.size() > index + 1) {
26 35 : return streams[index + 1].get();
27 : }
28 1 : return nullptr;
29 : }
30 :
31 6 : u32 StreamLiteMgr::SizeOfSlaves()
32 : {
33 6 : return streams.size() > 1 ? streams.size() - 1 : 0;
34 : }
35 :
36 2 : void StreamLiteMgr::Reset()
37 : {
38 5 : for (auto &streamLite : streams) {
39 3 : TaskExceptionFunc::GetInstance().UnRegister(streamLite.get());
40 : }
41 2 : streams.clear();
42 2 : }
43 :
44 2 : void StreamLiteMgr::ParseLiteData(std::vector<char> &data, u32 num, u32 sizePerDto)
45 : {
46 2 : u32 size = streams.size();
47 5 : for (u32 idx = 0; idx < num; idx++) {
48 3 : auto start = data.begin() + idx * sizePerDto;
49 3 : auto end = start + sizePerDto;
50 3 : std::vector<char> uniqueId(start, end);
51 3 : if (idx >= size) {
52 9 : HCCL_INFO("Make new Stream Lite idx=%u, size=%u", idx, size);
53 3 : auto stream = std::make_unique<StreamLite>(uniqueId);
54 3 : TaskExceptionFunc::GetInstance().Register(stream.get());
55 3 : streams.push_back(std::move(stream));
56 3 : }
57 3 : }
58 2 : }
59 :
60 3 : void StreamLiteMgr::ParsePackedData(std::vector<char> &givenData)
61 : {
62 : u32 num;
63 3 : BinaryStream binaryStream(givenData);
64 3 : std::vector<char> data;
65 3 : binaryStream >> num;
66 3 : binaryStream >> data;
67 9 : HCCL_INFO("StreamLiteMgr, num=%u, data=%s", num, Bytes2hex(data.data(), data.size()).c_str());
68 3 : u32 sizePerDto = data.size() / num;
69 3 : if (streams.size() >= num) { // 已经解析出stream,并且已有stream数量大于需要解包的数量,则直接返回
70 1 : return;
71 : }
72 :
73 2 : ParseLiteData(data, num, sizePerDto);
74 4 : }
75 :
76 122 : StreamLiteMgr::~StreamLiteMgr()
77 : {
78 137 : for (auto &streamLite : streams) {
79 15 : TaskExceptionFunc::GetInstance().UnRegister(streamLite.get());
80 : }
81 122 : streams.clear();
82 122 : }
83 :
84 8 : std::vector<StreamLite*> StreamLiteMgr::GetAllStreams()
85 : {
86 8 : std::vector<StreamLite*> result;
87 8 : result.reserve(streams.size());
88 8 : std::transform(streams.begin(), streams.end(), std::back_inserter(result),
89 0 : [](const auto& ptr) { return ptr.get(); });
90 8 : return result;
91 0 : }
92 :
93 : } // namespace Hccl
|