graph_utils.cpp 2.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <string.h>
#include "graph_utils.h"

namespace sccl {
namespace hardware {
namespace topology {
namespace graph {

scclTopoGraph::scclTopoGraph(int nRanks) : nRanks(nRanks), transport_map(nullptr, 0) {
    // 分配transport_map的内存
    uint8_t* raw_transport_map = static_cast<uint8_t*>(calloc(nRanks * nRanks, sizeof(uint8_t)));
    if(raw_transport_map == nullptr) {
        // 处理内存分配失败的情况
        throw std::bad_alloc();
    }
    // 使用ByteSpanArray初始化transport_map
    transport_map = ByteSpanArray<uint8_t>(raw_transport_map, nRanks * nRanks);
}

scclTopoGraph::~scclTopoGraph() {
    // 释放transport_map的内存
    free(transport_map.data());
}

// 打印transport_map
scclResult_t scclTopoGraph::printTransportMap() {
    for(int i = 0; i < this->nRanks; ++i) {
        for(int j = 0; j < this->nRanks; ++j) {
            uint8_t* value = this->getTransportMapData(i, j);
            if(value != nullptr) {
                printf("%d ", *value);
            } else {
                printf("nullptr ");
            }
        }
        printf("\n");
    }
    return scclSuccess;
}

// 打印gpu_paths信息的函数
scclResult_t scclTopoGraph::printGPUPaths() {
    for(const auto& start_pair : gpu_paths) {
        uint64_t start_node_id = start_pair.first;
        auto start_node_it     = graph_nodes.find(start_node_id);
        if(start_node_it != graph_nodes.end()) {
            std::cout << "Paths starting from node: ";
            start_node_it->second.printNodeInfo("Start Node");
        } else {
            std::cout << "Start node ID " << start_node_id << " not found in graph nodes." << std::endl;
            continue;
        }

        for(const auto& end_pair : start_pair.second) {
            uint64_t end_node_id = end_pair.first;
            auto end_node_it     = graph_nodes.find(end_node_id);
            if(end_node_it != graph_nodes.end()) {
                std::cout << "  to node: ";
                end_node_it->second.printNodeInfo("End Node");
            } else {
                std::cout << "  End node ID " << end_node_id << " not found in graph nodes." << std::endl;
                continue;
            }

            std::cout << "  Paths:" << std::endl;
            for(const auto& path : end_pair.second) {
                std::cout << "    Path: ";
                for(const auto& node_id : path) {
                    auto node_it = graph_nodes.find(node_id);
                    if(node_it != graph_nodes.end()) {
                        node_it->second.printNodeInfo("    ");
                    } else {
                        std::cout << "    Node ID " << node_id << " not found in graph nodes." << std::endl;
                    }
                }
                std::cout << std::endl;
            }
        }
    }
    return scclSuccess;
}

} // namespace graph
} // namespace topology
} // namespace hardware
} // namespace sccl