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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*
* Copyright (C) 2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "level_zero/driver_experimental/zex_graph.h"
#include "level_zero/core/source/cmdlist/cmdlist.h"
#include "level_zero/core/source/context/context.h"
#include "level_zero/experimental/source/graph/graph.h"
#include "level_zero/experimental/source/graph/graph_export.h"
namespace L0 {
ze_result_t ZE_APICALL zeGraphCreateExp(ze_context_handle_t hContext, ze_graph_handle_t *phGraph, void *pNext) {
if (nullptr != pNext) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
auto ctx = L0::Context::fromHandle(hContext);
if (nullptr == ctx) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
if (nullptr == phGraph) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
*phGraph = new Graph(ctx, true);
return ZE_RESULT_SUCCESS;
}
ze_result_t ZE_APICALL zeCommandListBeginGraphCaptureExp(ze_command_list_handle_t hCommandList, void *pNext) {
if (nullptr != pNext) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
auto cmdList = L0::CommandList::fromHandle(hCommandList);
if (nullptr == cmdList) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
if (cmdList->isCapturing() || !cmdList->isImmediateType() || cmdList->isInSynchronousMode()) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
ze_context_handle_t hCtx = nullptr;
cmdList->getContextHandle(&hCtx);
auto ctx = L0::Context::fromHandle(hCtx);
auto graph = new Graph(ctx, false);
cmdList->setCaptureTarget(graph);
graph->startCapturingFrom(*cmdList, false);
return ZE_RESULT_SUCCESS;
}
ze_result_t ZE_APICALL zeCommandListBeginCaptureIntoGraphExp(ze_command_list_handle_t hCommandList, ze_graph_handle_t hGraph, void *pNext) {
if (nullptr != pNext) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
auto cmdList = L0::CommandList::fromHandle(hCommandList);
if (nullptr == cmdList) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
if (cmdList->isCapturing()) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
auto graph = L0::Graph::fromHandle(hGraph);
if (nullptr == graph || !graph->empty()) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
cmdList->setCaptureTarget(graph);
graph->startCapturingFrom(*cmdList, false);
return ZE_RESULT_SUCCESS;
}
ze_result_t ZE_APICALL zeCommandListEndGraphCaptureExp(ze_command_list_handle_t hCommandList, ze_graph_handle_t *phGraph, void *pNext) {
if (nullptr != pNext) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
auto cmdList = L0::CommandList::fromHandle(hCommandList);
if (nullptr == cmdList) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
auto *graph = cmdList->getCaptureTarget();
if (nullptr == graph || graph->hasUnjoinedForks()) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
cmdList->getCaptureTarget()->stopCapturing();
if (nullptr == phGraph) {
if (graph->wasPreallocated()) {
cmdList->setCaptureTarget(nullptr);
return ZE_RESULT_SUCCESS;
} else {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
} else {
*phGraph = graph->toHandle();
cmdList->setCaptureTarget(nullptr);
}
return ZE_RESULT_SUCCESS;
}
ze_result_t ZE_APICALL zeCommandListInstantiateGraphExp(ze_graph_handle_t hGraph, ze_executable_graph_handle_t *phExecutableGraph, void *pNext) {
if (nullptr != pNext) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
auto virtualGraph = L0::Graph::fromHandle(hGraph);
if (nullptr == virtualGraph) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
if (nullptr == phExecutableGraph) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
if (false == virtualGraph->validForInstantiation()) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
auto execGraph = std::make_unique<ExecutableGraph>();
GraphInstatiateSettings settings{pNext};
auto ret = execGraph->instantiateFrom(*virtualGraph, settings);
if (ret != ZE_RESULT_SUCCESS) {
return ret;
}
*phExecutableGraph = execGraph.release();
return ZE_RESULT_SUCCESS;
}
ze_result_t ZE_APICALL zeCommandListAppendGraphExp(ze_command_list_handle_t hCommandList, ze_executable_graph_handle_t hGraph, void *pNext,
ze_event_handle_t hSignalEvent, uint32_t numWaitEvents, ze_event_handle_t *phWaitEvents) {
if (nullptr != pNext) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
auto cmdList = L0::CommandList::fromHandle(hCommandList);
if (nullptr == cmdList) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
auto graph = L0::ExecutableGraph::fromHandle(hGraph);
if (nullptr == graph) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
return graph->execute(cmdList, pNext, hSignalEvent, numWaitEvents, phWaitEvents);
}
ze_result_t ZE_APICALL zeGraphDestroyExp(ze_graph_handle_t hGraph) {
auto graph = L0::Graph::fromHandle(hGraph);
if (nullptr == graph) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
delete graph;
return ZE_RESULT_SUCCESS;
}
ze_result_t ZE_APICALL zeExecutableGraphDestroyExp(ze_executable_graph_handle_t hGraph) {
auto graph = L0::ExecutableGraph::fromHandle(hGraph);
if (nullptr == graph) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
delete graph;
return ZE_RESULT_SUCCESS;
}
ze_result_t ZE_APICALL zeCommandListIsGraphCaptureEnabledExp(ze_command_list_handle_t hCommandList) {
auto cmdList = L0::CommandList::fromHandle(hCommandList);
if (nullptr == cmdList) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
return cmdList->getCaptureTarget() ? ZE_RESULT_QUERY_TRUE : ZE_RESULT_QUERY_FALSE;
}
ze_result_t ZE_APICALL zeGraphIsEmptyExp(ze_graph_handle_t hGraph) {
auto graph = L0::Graph::fromHandle(hGraph);
if (nullptr == graph) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
if (false == graph->valid()) {
return ZE_RESULT_ERROR_INVALID_GRAPH;
}
return graph->empty() ? ZE_RESULT_QUERY_TRUE : ZE_RESULT_QUERY_FALSE;
}
ze_result_t ZE_APICALL zeGraphDumpContentsExp(ze_graph_handle_t hGraph, const char *filePath, void *pNext) {
auto *graph = L0::Graph::fromHandle(hGraph);
if (nullptr == graph) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
if (nullptr == filePath) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
L0::GraphExportStyle exportStyle = L0::GraphExportStyle::detailed;
const ze_base_desc_t *desc = reinterpret_cast<const ze_base_desc_t *>(pNext);
if (desc != nullptr) {
if (desc->stype == ZE_STRUCTURE_TYPE_RECORD_REPLAY_GRAPH_EXP_DUMP_DESC) {
const auto *dumpDesc = reinterpret_cast<const ze_record_replay_graph_exp_dump_desc_t *>(desc);
if (dumpDesc->mode == ZE_RECORD_REPLAY_GRAPH_EXP_DUMP_MODE_SIMPLE) {
exportStyle = L0::GraphExportStyle::simple;
} else if (dumpDesc->mode == ZE_RECORD_REPLAY_GRAPH_EXP_DUMP_MODE_DETAILED) {
exportStyle = L0::GraphExportStyle::detailed;
} else {
PRINT_DEBUG_STRING(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "Could not recognize provided graph dump mode, mode: 0x%x.\n",
dumpDesc->mode);
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
} else {
PRINT_DEBUG_STRING(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "Could not recognize provided extension, stype: 0x%x.\n",
desc->stype);
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
}
L0::GraphDotExporter exporter{exportStyle};
return exporter.exportToFile(*graph, filePath);
}
} // namespace L0
#if defined(__cplusplus)
extern "C" {
#endif
ZE_APIEXPORT ze_result_t ZE_APICALL zeGraphCreateExp(ze_context_handle_t hContext, ze_graph_handle_t *phGraph, void *pNext) {
return L0::zeGraphCreateExp(hContext, phGraph, pNext);
}
ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListBeginGraphCaptureExp(ze_command_list_handle_t hCommandList, void *pNext) {
return L0::zeCommandListBeginGraphCaptureExp(hCommandList, pNext);
}
ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListBeginCaptureIntoGraphExp(ze_command_list_handle_t hCommandList, ze_graph_handle_t hGraph, void *pNext) {
return L0::zeCommandListBeginCaptureIntoGraphExp(hCommandList, hGraph, pNext);
}
ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListEndGraphCaptureExp(ze_command_list_handle_t hCommandList, ze_graph_handle_t *phGraph, void *pNext) {
return L0::zeCommandListEndGraphCaptureExp(hCommandList, phGraph, pNext);
}
ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListInstantiateGraphExp(ze_graph_handle_t hGraph, ze_executable_graph_handle_t *phExecutableGraph, void *pNext) {
return L0::zeCommandListInstantiateGraphExp(hGraph, phExecutableGraph, pNext);
}
ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListAppendGraphExp(ze_command_list_handle_t hCommandList, ze_executable_graph_handle_t hGraph, void *pNext,
ze_event_handle_t hSignalEvent, uint32_t numWaitEvents, ze_event_handle_t *phWaitEvents) {
return L0::zeCommandListAppendGraphExp(hCommandList, hGraph, pNext, hSignalEvent, numWaitEvents, phWaitEvents);
}
ZE_APIEXPORT ze_result_t ZE_APICALL zeGraphDestroyExp(ze_graph_handle_t hGraph) {
return L0::zeGraphDestroyExp(hGraph);
}
ZE_APIEXPORT ze_result_t ZE_APICALL zeExecutableGraphDestroyExp(ze_executable_graph_handle_t hGraph) {
return L0::zeExecutableGraphDestroyExp(hGraph);
}
ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListIsGraphCaptureEnabledExp(ze_command_list_handle_t hCommandList) {
return L0::zeCommandListIsGraphCaptureEnabledExp(hCommandList);
}
ZE_APIEXPORT ze_result_t ZE_APICALL zeGraphIsEmptyExp(ze_graph_handle_t hGraph) {
return L0::zeGraphIsEmptyExp(hGraph);
}
ZE_APIEXPORT ze_result_t ZE_APICALL zeGraphDumpContentsExp(ze_graph_handle_t hGraph, const char *filePath, void *pNext) {
return L0::zeGraphDumpContentsExp(hGraph, filePath, pNext);
}
#if defined(__cplusplus)
} // extern "C"
#endif
|