|
8 | 8 | #include <level_zero/ze_api.h> |
9 | 9 |
|
10 | 10 | #include <cstring> |
| 11 | +#include <fstream> |
11 | 12 | #include <iostream> |
12 | 13 | #include <limits> |
| 14 | +#include <memory> |
13 | 15 | #include <vector> |
14 | 16 |
|
15 | 17 | #define VALIDATECALL(myZeCall) \ |
@@ -104,8 +106,67 @@ int main(int argc, char *argv[]) { |
104 | 106 | memset(srcBuffer, val, allocSize); |
105 | 107 | memset(dstBuffer, 0, allocSize); |
106 | 108 |
|
107 | | - // Perform a GPU copy |
108 | | - VALIDATECALL(zeCommandListAppendMemoryCopy(cmdList, dstBuffer, srcBuffer, allocSize, nullptr, 0, nullptr)); |
| 109 | + ze_module_handle_t module = nullptr; |
| 110 | + ze_kernel_handle_t kernel = nullptr; |
| 111 | + |
| 112 | + std::ifstream file("copy_buffer_to_buffer.spv", std::ios::binary); |
| 113 | + |
| 114 | + if (file.is_open()) { |
| 115 | + file.seekg(0, file.end); |
| 116 | + auto length = file.tellg(); |
| 117 | + file.seekg(0, file.beg); |
| 118 | + |
| 119 | + std::unique_ptr<char[]> spirvInput(new char[length]); |
| 120 | + file.read(spirvInput.get(), length); |
| 121 | + |
| 122 | + ze_module_desc_t moduleDesc = {}; |
| 123 | + ze_module_build_log_handle_t buildlog; |
| 124 | + moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV; |
| 125 | + moduleDesc.pInputModule = reinterpret_cast<const uint8_t *>(spirvInput.get()); |
| 126 | + moduleDesc.inputSize = length; |
| 127 | + moduleDesc.pBuildFlags = ""; |
| 128 | + |
| 129 | + if (zeModuleCreate(context, device, &moduleDesc, &module, &buildlog) != ZE_RESULT_SUCCESS) { |
| 130 | + size_t szLog = 0; |
| 131 | + zeModuleBuildLogGetString(buildlog, &szLog, nullptr); |
| 132 | + |
| 133 | + char *strLog = (char *)malloc(szLog); |
| 134 | + zeModuleBuildLogGetString(buildlog, &szLog, strLog); |
| 135 | + std::cout << "Build log:" << strLog << std::endl; |
| 136 | + |
| 137 | + free(strLog); |
| 138 | + } |
| 139 | + VALIDATECALL(zeModuleBuildLogDestroy(buildlog)); |
| 140 | + |
| 141 | + ze_kernel_desc_t kernelDesc = {}; |
| 142 | + kernelDesc.pKernelName = "CopyBufferToBufferBytes"; |
| 143 | + VALIDATECALL(zeKernelCreate(module, &kernelDesc, &kernel)); |
| 144 | + |
| 145 | + uint32_t groupSizeX = 32u; |
| 146 | + uint32_t groupSizeY = 1u; |
| 147 | + uint32_t groupSizeZ = 1u; |
| 148 | + VALIDATECALL(zeKernelSuggestGroupSize(kernel, allocSize, 1U, 1U, &groupSizeX, &groupSizeY, &groupSizeZ)); |
| 149 | + VALIDATECALL(zeKernelSetGroupSize(kernel, groupSizeX, groupSizeY, groupSizeZ)); |
| 150 | + |
| 151 | + uint32_t offset = 0; |
| 152 | + VALIDATECALL(zeKernelSetArgumentValue(kernel, 1, sizeof(dstBuffer), &dstBuffer)); |
| 153 | + VALIDATECALL(zeKernelSetArgumentValue(kernel, 0, sizeof(srcBuffer), &srcBuffer)); |
| 154 | + VALIDATECALL(zeKernelSetArgumentValue(kernel, 2, sizeof(uint32_t), &offset)); |
| 155 | + VALIDATECALL(zeKernelSetArgumentValue(kernel, 3, sizeof(uint32_t), &offset)); |
| 156 | + VALIDATECALL(zeKernelSetArgumentValue(kernel, 4, sizeof(uint32_t), &offset)); |
| 157 | + |
| 158 | + ze_group_count_t dispatchTraits; |
| 159 | + dispatchTraits.groupCountX = allocSize / groupSizeX; |
| 160 | + dispatchTraits.groupCountY = 1u; |
| 161 | + dispatchTraits.groupCountZ = 1u; |
| 162 | + |
| 163 | + VALIDATECALL(zeCommandListAppendLaunchKernel(cmdList, kernel, &dispatchTraits, |
| 164 | + nullptr, 0, nullptr)); |
| 165 | + file.close(); |
| 166 | + } else { |
| 167 | + // Perform a GPU copy |
| 168 | + VALIDATECALL(zeCommandListAppendMemoryCopy(cmdList, dstBuffer, srcBuffer, allocSize, nullptr, 0, nullptr)); |
| 169 | + } |
109 | 170 |
|
110 | 171 | // Close list and submit for execution |
111 | 172 | VALIDATECALL(zeCommandListClose(cmdList)); |
|
0 commit comments