|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include <level_zero/ze_api.h> |
| 9 | + |
| 10 | +#include "zello_common.h" |
| 11 | + |
| 12 | +#include <fstream> |
| 13 | +#include <iostream> |
| 14 | +#include <memory> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +extern bool verbose; |
| 18 | +bool verbose = false; |
| 19 | + |
| 20 | +void createCmdQueueAndCmdList(ze_device_handle_t &device, |
| 21 | + ze_context_handle_t &context, |
| 22 | + ze_command_queue_handle_t &cmdqueue, |
| 23 | + ze_command_list_handle_t &cmdList) { |
| 24 | + // Create commandQueue and cmdList |
| 25 | + ze_command_queue_desc_t cmdQueueDesc = {ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC}; |
| 26 | + cmdQueueDesc.ordinal = getCommandQueueOrdinal(device); |
| 27 | + cmdQueueDesc.index = 0; |
| 28 | + cmdQueueDesc.mode = ZE_COMMAND_QUEUE_MODE_ASYNCHRONOUS; |
| 29 | + SUCCESS_OR_TERMINATE(zeCommandQueueCreate(context, device, &cmdQueueDesc, &cmdqueue)); |
| 30 | + SUCCESS_OR_TERMINATE(createCommandList(context, device, cmdList)); |
| 31 | +} |
| 32 | + |
| 33 | +// Test Device Signal and Device wait followed by Host Wait |
| 34 | +bool testEventsDeviceSignalDeviceWait(ze_context_handle_t &context, ze_device_handle_t &device) { |
| 35 | + ze_command_queue_handle_t cmdQueue; |
| 36 | + ze_command_list_handle_t cmdList; |
| 37 | + |
| 38 | + // Create commandQueue and cmdList |
| 39 | + createCmdQueueAndCmdList(device, context, cmdQueue, cmdList); |
| 40 | + |
| 41 | + // Create two shared buffers |
| 42 | + constexpr size_t allocSize = 4096; |
| 43 | + ze_device_mem_alloc_desc_t deviceDesc = {ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC}; |
| 44 | + deviceDesc.flags = ZE_DEVICE_MEM_ALLOC_FLAG_BIAS_UNCACHED; |
| 45 | + deviceDesc.ordinal = 0; |
| 46 | + |
| 47 | + ze_host_mem_alloc_desc_t hostDesc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC}; |
| 48 | + hostDesc.flags = ZE_HOST_MEM_ALLOC_FLAG_BIAS_UNCACHED; |
| 49 | + |
| 50 | + void *srcBuffer = nullptr; |
| 51 | + SUCCESS_OR_TERMINATE(zeMemAllocShared(context, &deviceDesc, &hostDesc, allocSize, 1, device, &srcBuffer)); |
| 52 | + |
| 53 | + void *dstBuffer = nullptr; |
| 54 | + SUCCESS_OR_TERMINATE(zeMemAllocShared(context, &deviceDesc, &hostDesc, allocSize, 1, device, &dstBuffer)); |
| 55 | + |
| 56 | + // Create Event Pool and kernel launch event |
| 57 | + ze_event_pool_handle_t eventPoolDevice, eventPoolHost; |
| 58 | + uint32_t numEvents = 2; |
| 59 | + std::vector<ze_event_handle_t> deviceEvents(numEvents), hostEvents(numEvents); |
| 60 | + createEventPoolAndEvents(context, device, eventPoolDevice, |
| 61 | + (ze_event_pool_flag_t)0, |
| 62 | + numEvents, deviceEvents.data(), |
| 63 | + ZE_EVENT_SCOPE_FLAG_SUBDEVICE, |
| 64 | + (ze_event_scope_flag_t)0); |
| 65 | + createEventPoolAndEvents(context, device, eventPoolHost, |
| 66 | + (ze_event_pool_flag_t)(ZE_EVENT_POOL_FLAG_HOST_VISIBLE), |
| 67 | + numEvents, hostEvents.data(), |
| 68 | + ZE_EVENT_SCOPE_FLAG_HOST, |
| 69 | + (ze_event_scope_flag_t)0); |
| 70 | + |
| 71 | + //Initialize memory |
| 72 | + uint8_t dstValue = 0; |
| 73 | + uint8_t srcValue = 55; |
| 74 | + SUCCESS_OR_TERMINATE(zeCommandListAppendMemoryFill(cmdList, dstBuffer, reinterpret_cast<void *>(&dstValue), |
| 75 | + sizeof(dstValue), allocSize, deviceEvents[0], 0, nullptr)); |
| 76 | + SUCCESS_OR_TERMINATE(zeCommandListAppendMemoryFill(cmdList, srcBuffer, reinterpret_cast<void *>(&srcValue), |
| 77 | + sizeof(srcValue), allocSize, deviceEvents[1], 1, &deviceEvents[0])); |
| 78 | + SUCCESS_OR_TERMINATE(zeCommandListAppendMemoryCopy(cmdList, dstBuffer, srcBuffer, allocSize, hostEvents[0], 1, &deviceEvents[1])); |
| 79 | + |
| 80 | + SUCCESS_OR_TERMINATE(zeCommandListClose(cmdList)); |
| 81 | + |
| 82 | + SUCCESS_OR_TERMINATE(zeCommandQueueExecuteCommandLists(cmdQueue, 1, &cmdList, nullptr)); |
| 83 | + |
| 84 | + SUCCESS_OR_TERMINATE(zeEventHostSynchronize(hostEvents[0], std::numeric_limits<uint64_t>::max())); |
| 85 | + |
| 86 | + // Validate |
| 87 | + bool outputValidationSuccessful = true; |
| 88 | + if (memcmp(dstBuffer, srcBuffer, allocSize)) { |
| 89 | + outputValidationSuccessful = false; |
| 90 | + uint8_t *srcCharBuffer = static_cast<uint8_t *>(srcBuffer); |
| 91 | + uint8_t *dstCharBuffer = static_cast<uint8_t *>(dstBuffer); |
| 92 | + for (size_t i = 0; i < allocSize; i++) { |
| 93 | + if (srcCharBuffer[i] != dstCharBuffer[i]) { |
| 94 | + std::cout << "srcBuffer[" << i << "] = " << static_cast<unsigned int>(srcCharBuffer[i]) << " not equal to " |
| 95 | + << "dstBuffer[" << i << "] = " << static_cast<unsigned int>(dstCharBuffer[i]) << "\n"; |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Cleanup |
| 102 | + for (auto event : hostEvents) { |
| 103 | + SUCCESS_OR_TERMINATE(zeEventDestroy(event)); |
| 104 | + } |
| 105 | + for (auto event : deviceEvents) { |
| 106 | + SUCCESS_OR_TERMINATE(zeEventDestroy(event)); |
| 107 | + } |
| 108 | + |
| 109 | + SUCCESS_OR_TERMINATE(zeEventPoolDestroy(eventPoolHost)); |
| 110 | + SUCCESS_OR_TERMINATE(zeEventPoolDestroy(eventPoolDevice)); |
| 111 | + SUCCESS_OR_TERMINATE(zeMemFree(context, dstBuffer)); |
| 112 | + SUCCESS_OR_TERMINATE(zeMemFree(context, srcBuffer)); |
| 113 | + SUCCESS_OR_TERMINATE(zeCommandListDestroy(cmdList)); |
| 114 | + SUCCESS_OR_TERMINATE(zeCommandQueueDestroy(cmdQueue)); |
| 115 | + |
| 116 | + return outputValidationSuccessful; |
| 117 | +} |
| 118 | + |
| 119 | +// Test Device Signal and Host wait |
| 120 | +bool testEventsDeviceSignalHostWait(ze_context_handle_t &context, ze_device_handle_t &device) { |
| 121 | + ze_command_queue_handle_t cmdQueue; |
| 122 | + ze_command_list_handle_t cmdList; |
| 123 | + |
| 124 | + // Create commandQueue and cmdList |
| 125 | + createCmdQueueAndCmdList(device, context, cmdQueue, cmdList); |
| 126 | + |
| 127 | + // Create two shared buffers |
| 128 | + constexpr size_t allocSize = 4096; |
| 129 | + ze_device_mem_alloc_desc_t deviceDesc = {ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC}; |
| 130 | + deviceDesc.flags = ZE_DEVICE_MEM_ALLOC_FLAG_BIAS_UNCACHED; |
| 131 | + deviceDesc.ordinal = 0; |
| 132 | + |
| 133 | + ze_host_mem_alloc_desc_t hostDesc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC}; |
| 134 | + hostDesc.flags = ZE_HOST_MEM_ALLOC_FLAG_BIAS_UNCACHED; |
| 135 | + |
| 136 | + void *srcBuffer = nullptr; |
| 137 | + SUCCESS_OR_TERMINATE(zeMemAllocShared(context, &deviceDesc, &hostDesc, allocSize, 1, device, &srcBuffer)); |
| 138 | + |
| 139 | + void *dstBuffer = nullptr; |
| 140 | + SUCCESS_OR_TERMINATE(zeMemAllocShared(context, &deviceDesc, &hostDesc, allocSize, 1, device, &dstBuffer)); |
| 141 | + |
| 142 | + // Initialize memory |
| 143 | + constexpr uint8_t val = 55; |
| 144 | + memset(srcBuffer, val, allocSize); |
| 145 | + memset(dstBuffer, 0, allocSize); |
| 146 | + |
| 147 | + // Create Event Pool and kernel launch event |
| 148 | + ze_event_pool_handle_t eventPool; |
| 149 | + uint32_t numEvents = 2; |
| 150 | + std::vector<ze_event_handle_t> events(numEvents); |
| 151 | + createEventPoolAndEvents(context, device, eventPool, |
| 152 | + (ze_event_pool_flag_t)(ZE_EVENT_POOL_FLAG_HOST_VISIBLE), |
| 153 | + numEvents, events.data(), |
| 154 | + ZE_EVENT_SCOPE_FLAG_HOST, |
| 155 | + (ze_event_scope_flag_t)0); |
| 156 | + |
| 157 | + SUCCESS_OR_TERMINATE(zeCommandListAppendMemoryCopy(cmdList, dstBuffer, srcBuffer, allocSize, events[0], 0, nullptr)); |
| 158 | + |
| 159 | + SUCCESS_OR_TERMINATE(zeCommandListClose(cmdList)); |
| 160 | + |
| 161 | + SUCCESS_OR_TERMINATE(zeCommandQueueExecuteCommandLists(cmdQueue, 1, &cmdList, nullptr)); |
| 162 | + |
| 163 | + SUCCESS_OR_TERMINATE(zeEventHostSynchronize(events[0], std::numeric_limits<uint64_t>::max())); |
| 164 | + |
| 165 | + // Validate |
| 166 | + bool outputValidationSuccessful = true; |
| 167 | + if (memcmp(dstBuffer, srcBuffer, allocSize)) { |
| 168 | + outputValidationSuccessful = false; |
| 169 | + uint8_t *srcCharBuffer = static_cast<uint8_t *>(srcBuffer); |
| 170 | + uint8_t *dstCharBuffer = static_cast<uint8_t *>(dstBuffer); |
| 171 | + for (size_t i = 0; i < allocSize; i++) { |
| 172 | + if (srcCharBuffer[i] != dstCharBuffer[i]) { |
| 173 | + std::cout << "srcBuffer[" << i << "] = " << static_cast<unsigned int>(srcCharBuffer[i]) << " not equal to " |
| 174 | + << "dstBuffer[" << i << "] = " << static_cast<unsigned int>(dstCharBuffer[i]) << "\n"; |
| 175 | + break; |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // Cleanup |
| 181 | + for (auto event : events) { |
| 182 | + SUCCESS_OR_TERMINATE(zeEventDestroy(event)); |
| 183 | + } |
| 184 | + |
| 185 | + SUCCESS_OR_TERMINATE(zeEventPoolDestroy(eventPool)); |
| 186 | + SUCCESS_OR_TERMINATE(zeMemFree(context, dstBuffer)); |
| 187 | + SUCCESS_OR_TERMINATE(zeMemFree(context, srcBuffer)); |
| 188 | + SUCCESS_OR_TERMINATE(zeCommandListDestroy(cmdList)); |
| 189 | + SUCCESS_OR_TERMINATE(zeCommandQueueDestroy(cmdQueue)); |
| 190 | + |
| 191 | + return outputValidationSuccessful; |
| 192 | +} |
| 193 | + |
| 194 | +void printResult(bool outputValidationSuccessful, std::string ¤tTest) { |
| 195 | + std::cout << "\nZello Events: " << currentTest.c_str() |
| 196 | + << " Results validation " |
| 197 | + << (outputValidationSuccessful ? "PASSED" : "FAILED") |
| 198 | + << std::endl |
| 199 | + << std::endl; |
| 200 | +} |
| 201 | + |
| 202 | +int main(int argc, char *argv[]) { |
| 203 | + bool outputValidationSuccessful; |
| 204 | + verbose = isVerbose(argc, argv); |
| 205 | + |
| 206 | + ze_context_handle_t context = nullptr; |
| 207 | + ze_driver_handle_t driverHandle = nullptr; |
| 208 | + auto devices = zelloInitContextAndGetDevices(context, driverHandle); |
| 209 | + auto device = devices[0]; |
| 210 | + |
| 211 | + ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES}; |
| 212 | + SUCCESS_OR_TERMINATE(zeDeviceGetProperties(device, &deviceProperties)); |
| 213 | + std::cout << "Device : \n" |
| 214 | + << " * name : " << deviceProperties.name << "\n" |
| 215 | + << " * vendorId : " << std::hex << deviceProperties.vendorId << "\n"; |
| 216 | + |
| 217 | + std::string currentTest; |
| 218 | + |
| 219 | + currentTest = "Device signal and host wait test"; |
| 220 | + outputValidationSuccessful = testEventsDeviceSignalHostWait(context, device); |
| 221 | + printResult(outputValidationSuccessful, currentTest); |
| 222 | + |
| 223 | + currentTest = "Device signal and device wait test"; |
| 224 | + outputValidationSuccessful = testEventsDeviceSignalDeviceWait(context, device); |
| 225 | + printResult(outputValidationSuccessful, currentTest); |
| 226 | + |
| 227 | + SUCCESS_OR_TERMINATE(zeContextDestroy(context)); |
| 228 | + |
| 229 | + return outputValidationSuccessful ? 0 : 1; |
| 230 | +} |
0 commit comments