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