File: fabric.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (160 lines) | stat: -rw-r--r-- 7,236 bytes parent folder | download | duplicates (2)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2024 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "level_zero/core/source/fabric/fabric.h"

#include "shared/source/helpers/debug_helpers.h"

#include <algorithm>
#include <cstring>
#include <deque>
#include <limits>
#include <map>
#include <string>
#include <vector>

namespace L0 {

void FabricEdge::createEdgesFromVertices(const std::vector<FabricVertex *> &vertices, std::vector<FabricEdge *> &edges, std::vector<FabricEdge *> &indirectEdges) {

    // Get all vertices and sub-vertices
    std::vector<FabricVertex *> allVertices = {};
    for (auto &fabricVertex : vertices) {
        allVertices.push_back(fabricVertex);
        for (auto &fabricSubVertex : fabricVertex->subVertices) {
            allVertices.push_back(fabricSubVertex);
        }
    }

    // Get direct physical edges between all vertices
    std::map<uint32_t, std::vector<std::pair<uint32_t, ze_fabric_edge_exp_properties_t *>>> adjacentVerticesMap;
    std::map<uint32_t, std::vector<uint32_t>> nonAdjacentVerticesMap;
    for (uint32_t vertexAIndex = 0; vertexAIndex < allVertices.size(); vertexAIndex++) {
        for (uint32_t vertexBIndex = vertexAIndex + 1; vertexBIndex < allVertices.size(); vertexBIndex++) {
            bool isAdjacent = false;
            auto vertexA = allVertices[vertexAIndex];
            auto vertexB = allVertices[vertexBIndex];
            ze_fabric_edge_exp_properties_t edgeProperty = {};

            for (auto const &fabricDeviceInterface : vertexA->pFabricDeviceInterfaces) {
                bool isConnected =
                    fabricDeviceInterface.second->getEdgeProperty(vertexB, edgeProperty);
                if (isConnected) {
                    edges.push_back(create(vertexA, vertexB, edgeProperty));
                    adjacentVerticesMap[vertexAIndex].emplace_back(vertexBIndex, &edges.back()->properties);
                    adjacentVerticesMap[vertexBIndex].emplace_back(vertexAIndex, &edges.back()->properties);
                    isAdjacent = true;
                }
            }
            if (!isAdjacent) {
                auto &subVerticesOfA = vertexA->subVertices;
                if (std::find(subVerticesOfA.begin(), subVerticesOfA.end(), vertexB) == subVerticesOfA.end()) {
                    nonAdjacentVerticesMap[vertexAIndex].push_back(vertexBIndex);
                    nonAdjacentVerticesMap[vertexBIndex].push_back(vertexAIndex);
                }
            }
        }
    }

    // Find logical multi-hop edges between vertices not directly connected
    for (const auto &[vertexAIndex, nonAdjacentVertices] : nonAdjacentVerticesMap) {
        for (auto vertexBIndex : nonAdjacentVertices) {
            std::map<uint32_t, uint32_t> visited;
            visited[vertexAIndex] = vertexAIndex;

            std::deque<uint32_t> toVisit;
            toVisit.push_back(vertexAIndex);

            uint32_t currVertexIndex = vertexAIndex;

            while (true) {
                std::deque<uint32_t> toVisitIaf, toVisitMdfi;
                while (!toVisit.empty()) {
                    currVertexIndex = toVisit.front();
                    toVisit.pop_front();
                    if (currVertexIndex == vertexBIndex) {
                        break;
                    }

                    for (auto [vertexIndex, edgeProperty] : adjacentVerticesMap[currVertexIndex]) {
                        if (visited.find(vertexIndex) == visited.end()) {
                            if (strncmp(edgeProperty->model, "XeLink", 7) == 0) {
                                toVisitIaf.push_back(vertexIndex);
                            } else {
                                DEBUG_BREAK_IF(strncmp(edgeProperty->model, "MDFI", 5) != 0);
                                toVisitMdfi.push_back(vertexIndex);
                            }
                            visited[vertexIndex] = currVertexIndex;
                        }
                    }
                }

                if (currVertexIndex != vertexBIndex) {
                    if (toVisitIaf.size() + toVisitMdfi.size() != 0) {
                        toVisit.insert(toVisit.end(), toVisitMdfi.begin(), toVisitMdfi.end());
                        toVisit.insert(toVisit.end(), toVisitIaf.begin(), toVisitIaf.end());
                    } else {
                        break;
                    }
                } else {
                    bool hasMdfi = false;
                    std::string path = "";
                    ze_fabric_edge_exp_properties_t properties = {};
                    properties.stype = ZE_STRUCTURE_TYPE_FABRIC_EDGE_EXP_PROPERTIES;
                    properties.pNext = nullptr;
                    memset(properties.uuid.id, 0, ZE_MAX_UUID_SIZE);
                    memset(properties.model, 0, ZE_MAX_FABRIC_EDGE_MODEL_EXP_SIZE);
                    properties.bandwidth = std::numeric_limits<uint32_t>::max();
                    properties.bandwidthUnit = ZE_BANDWIDTH_UNIT_BYTES_PER_NANOSEC;
                    properties.latency = 0;
                    properties.latencyUnit = ZE_LATENCY_UNIT_HOP;
                    properties.duplexity = ZE_FABRIC_EDGE_EXP_DUPLEXITY_FULL_DUPLEX;

                    while (true) {
                        const auto parentIndex = visited[currVertexIndex];
                        ze_fabric_edge_exp_properties_t *currEdgeProperty = nullptr;
                        for (const auto &[vertexIndex, edgeProperty] : adjacentVerticesMap[parentIndex]) {
                            if (vertexIndex == currVertexIndex) {
                                currEdgeProperty = edgeProperty;
                                break;
                            }
                        }
                        UNRECOVERABLE_IF(currEdgeProperty == nullptr);
                        path = std::string(currEdgeProperty->model) + path;
                        if (strncmp(currEdgeProperty->model, "XeLink", 7) == 0) {
                            if (currEdgeProperty->bandwidth < properties.bandwidth) {
                                properties.bandwidth = currEdgeProperty->bandwidth;
                            }
                            properties.latency += currEdgeProperty->latency;
                        }

                        if (strncmp(currEdgeProperty->model, "MDFI", 5) == 0) {
                            hasMdfi = true;
                        }

                        currVertexIndex = parentIndex;
                        if (currVertexIndex == vertexAIndex) {
                            path.resize(ZE_MAX_FABRIC_EDGE_MODEL_EXP_SIZE - 1, '\0');
                            path.copy(properties.model, path.size());
                            break;
                        } else {
                            path = '-' + path;
                        }
                    }
                    if (hasMdfi) {
                        properties.latency = 0;
                        properties.latencyUnit = ZE_LATENCY_UNIT_UNKNOWN;
                    }
                    indirectEdges.push_back(create(allVertices[vertexAIndex], allVertices[vertexBIndex], properties));
                    break;
                }
            }
        }
    }
}

} // namespace L0