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