|
| 1 | +//===--------- adapter.cpp - Level Zero Adapter ---------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "adapter.hpp" |
| 10 | +#include "ur_level_zero.hpp" |
| 11 | + |
| 12 | +ur_adapter_handle_t_ Adapter{}; |
| 13 | + |
| 14 | +UR_APIEXPORT ur_result_t UR_APICALL |
| 15 | +urInit(ur_device_init_flags_t |
| 16 | + DeviceFlags, ///< [in] device initialization flags. |
| 17 | + ///< must be 0 (default) or a combination of |
| 18 | + ///< ::ur_device_init_flag_t. |
| 19 | + ur_loader_config_handle_t) { |
| 20 | + std::ignore = DeviceFlags; |
| 21 | + |
| 22 | + return UR_RESULT_SUCCESS; |
| 23 | +} |
| 24 | + |
| 25 | +ur_result_t adapterStateTeardown() { |
| 26 | + // reclaim ur_platform_handle_t objects here since we don't have |
| 27 | + // urPlatformRelease. |
| 28 | + for (ur_platform_handle_t Platform : *URPlatformsCache) { |
| 29 | + delete Platform; |
| 30 | + } |
| 31 | + delete URPlatformsCache; |
| 32 | + delete URPlatformsCacheMutex; |
| 33 | + |
| 34 | + bool LeakFound = false; |
| 35 | + |
| 36 | + // Print the balance of various create/destroy native calls. |
| 37 | + // The idea is to verify if the number of create(+) and destroy(-) calls are |
| 38 | + // matched. |
| 39 | + if (ZeCallCount && (UrL0Debug & UR_L0_DEBUG_CALL_COUNT) != 0) { |
| 40 | + // clang-format off |
| 41 | + // |
| 42 | + // The format of this table is such that each row accounts for a |
| 43 | + // specific type of objects, and all elements in the raw except the last |
| 44 | + // one are allocating objects of that type, while the last element is known |
| 45 | + // to deallocate objects of that type. |
| 46 | + // |
| 47 | + std::vector<std::vector<std::string>> CreateDestroySet = { |
| 48 | + {"zeContextCreate", "zeContextDestroy"}, |
| 49 | + {"zeCommandQueueCreate", "zeCommandQueueDestroy"}, |
| 50 | + {"zeModuleCreate", "zeModuleDestroy"}, |
| 51 | + {"zeKernelCreate", "zeKernelDestroy"}, |
| 52 | + {"zeEventPoolCreate", "zeEventPoolDestroy"}, |
| 53 | + {"zeCommandListCreateImmediate", "zeCommandListCreate", "zeCommandListDestroy"}, |
| 54 | + {"zeEventCreate", "zeEventDestroy"}, |
| 55 | + {"zeFenceCreate", "zeFenceDestroy"}, |
| 56 | + {"zeImageCreate", "zeImageDestroy"}, |
| 57 | + {"zeSamplerCreate", "zeSamplerDestroy"}, |
| 58 | + {"zeMemAllocDevice", "zeMemAllocHost", "zeMemAllocShared", "zeMemFree"}, |
| 59 | + }; |
| 60 | + |
| 61 | + // A sample output aimed below is this: |
| 62 | + // ------------------------------------------------------------------------ |
| 63 | + // zeContextCreate = 1 \---> zeContextDestroy = 1 |
| 64 | + // zeCommandQueueCreate = 1 \---> zeCommandQueueDestroy = 1 |
| 65 | + // zeModuleCreate = 1 \---> zeModuleDestroy = 1 |
| 66 | + // zeKernelCreate = 1 \---> zeKernelDestroy = 1 |
| 67 | + // zeEventPoolCreate = 1 \---> zeEventPoolDestroy = 1 |
| 68 | + // zeCommandListCreateImmediate = 1 | |
| 69 | + // zeCommandListCreate = 1 \---> zeCommandListDestroy = 1 ---> LEAK = 1 |
| 70 | + // zeEventCreate = 2 \---> zeEventDestroy = 2 |
| 71 | + // zeFenceCreate = 1 \---> zeFenceDestroy = 1 |
| 72 | + // zeImageCreate = 0 \---> zeImageDestroy = 0 |
| 73 | + // zeSamplerCreate = 0 \---> zeSamplerDestroy = 0 |
| 74 | + // zeMemAllocDevice = 0 | |
| 75 | + // zeMemAllocHost = 1 | |
| 76 | + // zeMemAllocShared = 0 \---> zeMemFree = 1 |
| 77 | + // |
| 78 | + // clang-format on |
| 79 | + |
| 80 | + fprintf(stderr, "ZE_DEBUG=%d: check balance of create/destroy calls\n", |
| 81 | + UR_L0_DEBUG_CALL_COUNT); |
| 82 | + fprintf(stderr, |
| 83 | + "----------------------------------------------------------\n"); |
| 84 | + for (const auto &Row : CreateDestroySet) { |
| 85 | + int diff = 0; |
| 86 | + for (auto I = Row.begin(); I != Row.end();) { |
| 87 | + const char *ZeName = (*I).c_str(); |
| 88 | + const auto &ZeCount = (*ZeCallCount)[*I]; |
| 89 | + |
| 90 | + bool First = (I == Row.begin()); |
| 91 | + bool Last = (++I == Row.end()); |
| 92 | + |
| 93 | + if (Last) { |
| 94 | + fprintf(stderr, " \\--->"); |
| 95 | + diff -= ZeCount; |
| 96 | + } else { |
| 97 | + diff += ZeCount; |
| 98 | + if (!First) { |
| 99 | + fprintf(stderr, " | \n"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + fprintf(stderr, "%30s = %-5d", ZeName, ZeCount); |
| 104 | + } |
| 105 | + |
| 106 | + if (diff) { |
| 107 | + LeakFound = true; |
| 108 | + fprintf(stderr, " ---> LEAK = %d", diff); |
| 109 | + } |
| 110 | + fprintf(stderr, "\n"); |
| 111 | + } |
| 112 | + |
| 113 | + ZeCallCount->clear(); |
| 114 | + delete ZeCallCount; |
| 115 | + ZeCallCount = nullptr; |
| 116 | + } |
| 117 | + if (LeakFound) |
| 118 | + return UR_RESULT_ERROR_INVALID_MEM_OBJECT; |
| 119 | + |
| 120 | + return UR_RESULT_SUCCESS; |
| 121 | +} |
| 122 | + |
| 123 | +UR_APIEXPORT ur_result_t UR_APICALL urTearDown( |
| 124 | + void *Params ///< [in] pointer to tear down parameters |
| 125 | +) { |
| 126 | + std::ignore = Params; |
| 127 | + return UR_RESULT_SUCCESS; |
| 128 | +} |
| 129 | + |
| 130 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet( |
| 131 | + uint32_t NumEntries, ///< [in] the number of platforms to be added to |
| 132 | + ///< phAdapters. If phAdapters is not NULL, then |
| 133 | + ///< NumEntries should be greater than zero, otherwise |
| 134 | + ///< ::UR_RESULT_ERROR_INVALID_SIZE, will be returned. |
| 135 | + ur_adapter_handle_t |
| 136 | + *Adapters, ///< [out][optional][range(0, NumEntries)] array of handle of |
| 137 | + ///< adapters. If NumEntries is less than the number of |
| 138 | + ///< adapters available, then |
| 139 | + ///< ::urAdapterGet shall only retrieve that number of |
| 140 | + ///< platforms. |
| 141 | + uint32_t *NumAdapters ///< [out][optional] returns the total number of |
| 142 | + ///< adapters available. |
| 143 | +) { |
| 144 | + if (NumEntries > 0 && Adapters) { |
| 145 | + std::lock_guard<std::mutex> Lock{Adapter.Mutex}; |
| 146 | + // TODO: Some initialization that happens in urPlatformsGet could be moved |
| 147 | + // here for when RefCount reaches 1 |
| 148 | + Adapter.RefCount++; |
| 149 | + *Adapters = &Adapter; |
| 150 | + } |
| 151 | + |
| 152 | + if (NumAdapters) { |
| 153 | + *NumAdapters = 1; |
| 154 | + } |
| 155 | + |
| 156 | + return UR_RESULT_SUCCESS; |
| 157 | +} |
| 158 | + |
| 159 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) { |
| 160 | + std::lock_guard<std::mutex> Lock{Adapter.Mutex}; |
| 161 | + if (--Adapter.RefCount == 0) { |
| 162 | + return adapterStateTeardown(); |
| 163 | + } |
| 164 | + |
| 165 | + return UR_RESULT_SUCCESS; |
| 166 | +} |
| 167 | + |
| 168 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) { |
| 169 | + std::lock_guard<std::mutex> Lock{Adapter.Mutex}; |
| 170 | + Adapter.RefCount++; |
| 171 | + |
| 172 | + return UR_RESULT_SUCCESS; |
| 173 | +} |
| 174 | + |
| 175 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetLastError( |
| 176 | + ur_adapter_handle_t Adapter, ///< [in] handle of the platform instance |
| 177 | + const char **Message, ///< [out] pointer to a C string where the adapter |
| 178 | + ///< specific error message will be stored. |
| 179 | + int32_t *Error ///< [out] pointer to an integer where the adapter specific |
| 180 | + ///< error code will be stored. |
| 181 | +) { |
| 182 | + std::ignore = Adapter; |
| 183 | + std::ignore = Message; |
| 184 | + std::ignore = Error; |
| 185 | + urPrint("[UR][L0] %s function not implemented!\n", __FUNCTION__); |
| 186 | + return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; |
| 187 | +} |
| 188 | + |
| 189 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t, |
| 190 | + ur_adapter_info_t PropName, |
| 191 | + size_t PropSize, |
| 192 | + void *PropValue, |
| 193 | + size_t *PropSizeRet) { |
| 194 | + UrReturnHelper ReturnValue(PropSize, PropValue, PropSizeRet); |
| 195 | + |
| 196 | + switch (PropName) { |
| 197 | + case UR_ADAPTER_INFO_BACKEND: |
| 198 | + return ReturnValue(UR_ADAPTER_BACKEND_LEVEL_ZERO); |
| 199 | + case UR_ADAPTER_INFO_REFERENCE_COUNT: |
| 200 | + return ReturnValue(Adapter.RefCount.load()); |
| 201 | + default: |
| 202 | + return UR_RESULT_ERROR_INVALID_ENUMERATION; |
| 203 | + } |
| 204 | + |
| 205 | + return UR_RESULT_SUCCESS; |
| 206 | +} |
0 commit comments