|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "level_zero/core/source/fabric/fabric.h" |
| 9 | + |
| 10 | +#include "shared/source/helpers/debug_helpers.h" |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | +#include <cstring> |
| 14 | +#include <deque> |
| 15 | +#include <limits> |
| 16 | +#include <map> |
| 17 | +#include <string> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +namespace L0 { |
| 21 | + |
| 22 | +void FabricEdge::createEdgesFromVertices(const std::vector<FabricVertex *> &vertices, std::vector<FabricEdge *> &edges, std::vector<FabricEdge *> &indirectEdges) { |
| 23 | + |
| 24 | + // Get all vertices and sub-vertices |
| 25 | + std::vector<FabricVertex *> allVertices = {}; |
| 26 | + for (auto &fabricVertex : vertices) { |
| 27 | + allVertices.push_back(fabricVertex); |
| 28 | + for (auto &fabricSubVertex : fabricVertex->subVertices) { |
| 29 | + allVertices.push_back(fabricSubVertex); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // Get direct physical edges between all vertices |
| 34 | + std::map<uint32_t, std::vector<std::pair<uint32_t, ze_fabric_edge_exp_properties_t *>>> adjacentVerticesMap; |
| 35 | + std::map<uint32_t, std::vector<uint32_t>> nonAdjacentVerticesMap; |
| 36 | + for (uint32_t vertexAIndex = 0; vertexAIndex < allVertices.size(); vertexAIndex++) { |
| 37 | + for (uint32_t vertexBIndex = vertexAIndex + 1; vertexBIndex < allVertices.size(); vertexBIndex++) { |
| 38 | + bool isAdjacent = false; |
| 39 | + auto vertexA = allVertices[vertexAIndex]; |
| 40 | + auto vertexB = allVertices[vertexBIndex]; |
| 41 | + ze_fabric_edge_exp_properties_t edgeProperty = {}; |
| 42 | + |
| 43 | + for (auto const &fabricDeviceInterface : vertexA->pFabricDeviceInterfaces) { |
| 44 | + bool isConnected = |
| 45 | + fabricDeviceInterface.second->getEdgeProperty(vertexB, edgeProperty); |
| 46 | + if (isConnected) { |
| 47 | + edges.push_back(create(vertexA, vertexB, edgeProperty)); |
| 48 | + adjacentVerticesMap[vertexAIndex].emplace_back(vertexBIndex, &edges.back()->properties); |
| 49 | + adjacentVerticesMap[vertexBIndex].emplace_back(vertexAIndex, &edges.back()->properties); |
| 50 | + isAdjacent = true; |
| 51 | + } |
| 52 | + } |
| 53 | + if (!isAdjacent) { |
| 54 | + auto &subVerticesOfA = vertexA->subVertices; |
| 55 | + if (std::find(subVerticesOfA.begin(), subVerticesOfA.end(), vertexB) == subVerticesOfA.end()) { |
| 56 | + nonAdjacentVerticesMap[vertexAIndex].push_back(vertexBIndex); |
| 57 | + nonAdjacentVerticesMap[vertexBIndex].push_back(vertexAIndex); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Find logical multi-hop edges between vertices not directly connected |
| 64 | + for (const auto &[vertexAIndex, nonAdjacentVertices] : nonAdjacentVerticesMap) { |
| 65 | + for (auto vertexBIndex : nonAdjacentVertices) { |
| 66 | + std::map<uint32_t, uint32_t> visited; |
| 67 | + visited[vertexAIndex] = vertexAIndex; |
| 68 | + |
| 69 | + std::deque<uint32_t> toVisit; |
| 70 | + toVisit.push_back(vertexAIndex); |
| 71 | + |
| 72 | + uint32_t currVertexIndex = vertexAIndex; |
| 73 | + |
| 74 | + while (true) { |
| 75 | + std::deque<uint32_t> toVisitIaf, toVisitMdfi; |
| 76 | + while (!toVisit.empty()) { |
| 77 | + currVertexIndex = toVisit.front(); |
| 78 | + toVisit.pop_front(); |
| 79 | + if (currVertexIndex == vertexBIndex) { |
| 80 | + break; |
| 81 | + } |
| 82 | + |
| 83 | + for (auto [vertexIndex, edgeProperty] : adjacentVerticesMap[currVertexIndex]) { |
| 84 | + if (visited.find(vertexIndex) == visited.end()) { |
| 85 | + if (strncmp(edgeProperty->model, "XeLink", 7) == 0) { |
| 86 | + toVisitIaf.push_back(vertexIndex); |
| 87 | + } else { |
| 88 | + DEBUG_BREAK_IF(strncmp(edgeProperty->model, "MDFI", 5) != 0); |
| 89 | + toVisitMdfi.push_back(vertexIndex); |
| 90 | + } |
| 91 | + visited[vertexIndex] = currVertexIndex; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (currVertexIndex != vertexBIndex) { |
| 97 | + if (toVisitIaf.size() + toVisitMdfi.size() != 0) { |
| 98 | + toVisit = toVisitMdfi; |
| 99 | + toVisit.insert(toVisit.end(), toVisitIaf.begin(), toVisitIaf.end()); |
| 100 | + } else { |
| 101 | + break; |
| 102 | + } |
| 103 | + } else { |
| 104 | + std::string path = ""; |
| 105 | + ze_fabric_edge_exp_properties_t properties = {}; |
| 106 | + properties.stype = ZE_STRUCTURE_TYPE_FABRIC_EDGE_EXP_PROPERTIES; |
| 107 | + properties.pNext = nullptr; |
| 108 | + memset(properties.uuid.id, 0, ZE_MAX_UUID_SIZE); |
| 109 | + memset(properties.model, 0, ZE_MAX_FABRIC_EDGE_MODEL_EXP_SIZE); |
| 110 | + properties.bandwidth = std::numeric_limits<uint32_t>::max(); |
| 111 | + properties.bandwidthUnit = ZE_BANDWIDTH_UNIT_BYTES_PER_NANOSEC; |
| 112 | + properties.latency = std::numeric_limits<uint32_t>::max(); |
| 113 | + properties.latencyUnit = ZE_LATENCY_UNIT_UNKNOWN; |
| 114 | + properties.duplexity = ZE_FABRIC_EDGE_EXP_DUPLEXITY_FULL_DUPLEX; |
| 115 | + |
| 116 | + while (true) { |
| 117 | + const auto parentIndex = visited[currVertexIndex]; |
| 118 | + ze_fabric_edge_exp_properties_t *currEdgeProperty = nullptr; |
| 119 | + for (const auto &[vertexIndex, edgeProperty] : adjacentVerticesMap[parentIndex]) { |
| 120 | + if (vertexIndex == currVertexIndex) { |
| 121 | + currEdgeProperty = edgeProperty; |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + UNRECOVERABLE_IF(currEdgeProperty == nullptr); |
| 126 | + path = std::string(currEdgeProperty->model) + path; |
| 127 | + if ((strncmp(currEdgeProperty->model, "XeLink", 7) == 0) && |
| 128 | + (currEdgeProperty->bandwidth < properties.bandwidth)) { |
| 129 | + properties.bandwidth = currEdgeProperty->bandwidth; |
| 130 | + } |
| 131 | + |
| 132 | + currVertexIndex = parentIndex; |
| 133 | + if (currVertexIndex == vertexAIndex) { |
| 134 | + path.resize(ZE_MAX_FABRIC_EDGE_MODEL_EXP_SIZE - 1, '\0'); |
| 135 | + path.copy(properties.model, path.size()); |
| 136 | + break; |
| 137 | + } else { |
| 138 | + path = '-' + path; |
| 139 | + } |
| 140 | + } |
| 141 | + indirectEdges.push_back(create(allVertices[vertexAIndex], allVertices[vertexBIndex], properties)); |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +} // namespace L0 |
0 commit comments