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 "alg_topo_package_helper.h"
12 : #include "binary_stream.h"
13 :
14 : namespace Hccl {
15 :
16 : template <typename U, typename V>
17 2 : BinaryStream& operator<<(BinaryStream& binaryStream, const std::map<U, V>& m)
18 : {
19 2 : size_t mapSize = m.size();
20 2 : binaryStream << mapSize;
21 4 : for (const auto& mapPair : m) {
22 2 : binaryStream << mapPair.first;
23 2 : binaryStream << mapPair.second;
24 : }
25 2 : return binaryStream;
26 : }
27 :
28 : template <typename U, typename V>
29 2 : BinaryStream& operator>>(BinaryStream& binaryStream, std::map<U, V>& m)
30 : {
31 : size_t mapSize;
32 2 : binaryStream >> mapSize;
33 4 : for (u32 i = 0; i < mapSize; ++i) {
34 : U k;
35 : V v;
36 2 : binaryStream >> k;
37 2 : binaryStream >> v;
38 2 : m.emplace(k, v);
39 : }
40 2 : return binaryStream;
41 : }
42 :
43 5 : std::vector<char> AlgTopoPackageHelper::GetPackedData(const AlgTopoInfo& algTopo) const
44 : {
45 5 : std::vector<char> result;
46 5 : BinaryStream binaryStream;
47 :
48 5 : binaryStream << algTopo.virtRanks;
49 5 : binaryStream << algTopo.virtRankMap;
50 5 : binaryStream << algTopo.vTopo;
51 :
52 5 : binaryStream.Dump(result);
53 5 : return result;
54 5 : }
55 :
56 1 : AlgTopoInfo AlgTopoPackageHelper::GetAlgTopoInfo(std::vector<char>& packedData) const
57 : {
58 1 : AlgTopoInfo algTopo;
59 1 : BinaryStream binaryStream(packedData);
60 :
61 1 : binaryStream >> algTopo.virtRanks;
62 1 : binaryStream >> algTopo.virtRankMap;
63 1 : binaryStream >> algTopo.vTopo;
64 :
65 1 : return algTopo;
66 1 : }
67 :
68 : } // namespace Hccl
|