|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-2025 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "framework/sycl/sycl.h" |
| 9 | +#include "framework/test_case/register_test_case.h" |
| 10 | +#include "framework/utility/timer.h" |
| 11 | + |
| 12 | +#include "definitions/memcpy_execute.h" |
| 13 | + |
| 14 | +#include <mutex> |
| 15 | +#include <shared_mutex> |
| 16 | +#include <thread> |
| 17 | + |
| 18 | +static auto inOrder = sycl::property::queue::in_order(); |
| 19 | +static const sycl::property_list queueProps[] = { |
| 20 | + sycl::property_list{}, |
| 21 | + sycl::property_list{inOrder}}; |
| 22 | + |
| 23 | +static TestResult run(const MemcpyExecuteArguments &arguments, Statistics &statistics) { |
| 24 | + MeasurementFields typeSelector(MeasurementUnit::Microseconds, MeasurementType::Cpu); |
| 25 | + |
| 26 | + if (isNoopRun()) { |
| 27 | + statistics.pushUnitAndType(typeSelector.getUnit(), typeSelector.getType()); |
| 28 | + return TestResult::Nooped; |
| 29 | + } |
| 30 | + |
| 31 | + bool inOrderQueue = arguments.inOrderQueue; |
| 32 | + bool measureCompletionTime = arguments.measureCompletionTime; |
| 33 | + size_t numOpsPerThread = arguments.numOpsPerThread; |
| 34 | + size_t numThreads = arguments.numThreads; |
| 35 | + size_t allocSize = arguments.allocSize; |
| 36 | + bool useEvents = arguments.useEvents; |
| 37 | + bool useQueuePerThread = arguments.useQueuePerThread; |
| 38 | + bool srcUSM = arguments.srcUSM; |
| 39 | + bool dstUSM = arguments.dstUSM; |
| 40 | + bool useBarrier = arguments.useBarrier; |
| 41 | + size_t arraySize = allocSize / sizeof(int); |
| 42 | + |
| 43 | + if (!inOrderQueue) { |
| 44 | + std::cerr << "Out of order mode not supported yet" << std::endl; |
| 45 | + return TestResult::Error; |
| 46 | + } |
| 47 | + |
| 48 | + // Setup |
| 49 | + Timer timer; |
| 50 | + |
| 51 | + const size_t gws = arraySize; |
| 52 | + const size_t lws = 1u; |
| 53 | + sycl::nd_range<1> range(gws, lws); |
| 54 | + |
| 55 | + auto queuePropsIndex = 0; |
| 56 | + queuePropsIndex |= arguments.inOrderQueue ? 0x1 : 0; |
| 57 | + |
| 58 | + std::vector<std::vector<void *>> usm(numThreads); |
| 59 | + std::vector<sycl::queue> queues; |
| 60 | + |
| 61 | + // Setup queues (or a single queue if !useQueuePerThread) |
| 62 | + if (!useQueuePerThread) { |
| 63 | + sycl::queue singleQueue{queueProps[queuePropsIndex]}; |
| 64 | + for (size_t i = 0; i < numThreads; i++) { |
| 65 | + queues.push_back(singleQueue); |
| 66 | + } |
| 67 | + } else { |
| 68 | + for (size_t i = 0; i < numThreads; i++) { |
| 69 | + queues.emplace_back(queueProps[queuePropsIndex]); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + void *src_buffer; |
| 74 | + std::vector<void *> dst_buffers; |
| 75 | + |
| 76 | + if (srcUSM) { |
| 77 | + src_buffer = sycl::malloc_host(allocSize, queues[0].get_context()); |
| 78 | + } else { |
| 79 | + src_buffer = malloc(allocSize); |
| 80 | + } |
| 81 | + |
| 82 | + if (src_buffer == nullptr) { |
| 83 | + std::cerr << "Failed to allocate memory for src_buffer" << std::endl; |
| 84 | + return TestResult::Error; |
| 85 | + } |
| 86 | + |
| 87 | + memset(src_buffer, 99, allocSize); |
| 88 | + |
| 89 | + // Setup USM allocations |
| 90 | + for (size_t i = 0; i < numThreads; i++) { |
| 91 | + for (size_t j = 0; j < numOpsPerThread; j++) { |
| 92 | + usm[i].push_back(sycl::malloc_device(allocSize, queues[i].get_device(), queues[i].get_context())); |
| 93 | + } |
| 94 | + |
| 95 | + dst_buffers.emplace_back(); |
| 96 | + |
| 97 | + if (dstUSM) { |
| 98 | + dst_buffers.back() = sycl::malloc_host(allocSize * numOpsPerThread, queues[0].get_context()); |
| 99 | + } else { |
| 100 | + dst_buffers.back() = malloc(allocSize * numOpsPerThread); |
| 101 | + } |
| 102 | + if (dst_buffers.back() == nullptr) { |
| 103 | + std::cerr << "Failed to allocate memory for dst_buffer" << std::endl; |
| 104 | + return TestResult::Error; |
| 105 | + } |
| 106 | + memset(dst_buffers.back(), 0, allocSize * numOpsPerThread); |
| 107 | + } |
| 108 | + |
| 109 | + auto worker = [&](size_t thread_id, Timer &timer) { |
| 110 | + timer.measureStart(); |
| 111 | + |
| 112 | + auto &queue = queues[thread_id]; |
| 113 | + for (size_t i = 0; i < numOpsPerThread; i++) { |
| 114 | + int *usm_ptr = (int *)usm[thread_id][i]; |
| 115 | + auto host_dst = ((char *)dst_buffers[thread_id]) + i * allocSize; |
| 116 | + |
| 117 | + if (useEvents) { |
| 118 | + queue.memcpy(usm_ptr, src_buffer, allocSize); |
| 119 | + queue.parallel_for(sycl::range<1>{arraySize}, [usm_ptr](sycl::item<1> itemId) { |
| 120 | + auto id = itemId.get_id(0); |
| 121 | + usm_ptr[id] = 1; |
| 122 | + }); |
| 123 | + queue.memcpy(host_dst, usm_ptr, allocSize); |
| 124 | + |
| 125 | + if (useBarrier) { |
| 126 | + queue.ext_oneapi_submit_barrier(); |
| 127 | + } |
| 128 | + } else { |
| 129 | + sycl::ext::oneapi::experimental::memcpy(queue, usm_ptr, src_buffer, allocSize); |
| 130 | + sycl::ext::oneapi::experimental::nd_launch(queue, range, [usm_ptr](sycl::nd_item<1> itemId) { |
| 131 | + auto id = itemId.get_global_id(0); |
| 132 | + usm_ptr[id] = 1; |
| 133 | + }); |
| 134 | + sycl::ext::oneapi::experimental::memcpy(queue, host_dst, usm_ptr, allocSize); |
| 135 | + |
| 136 | + if (useBarrier) { |
| 137 | + queue.ext_oneapi_submit_barrier(); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (!measureCompletionTime) |
| 143 | + timer.measureEnd(); |
| 144 | + |
| 145 | + queue.wait(); |
| 146 | + |
| 147 | + if (measureCompletionTime) |
| 148 | + timer.measureEnd(); |
| 149 | + }; |
| 150 | + |
| 151 | + // warmup |
| 152 | + for (auto iteration = 0u; iteration < arguments.numThreads; iteration++) { |
| 153 | + std::vector<std::thread> threads; |
| 154 | + for (size_t j = 0u; j < arguments.numThreads; j++) { |
| 155 | + threads.emplace_back([&, j] { |
| 156 | + Timer dummyTimer; |
| 157 | + worker(j, dummyTimer); |
| 158 | + }); |
| 159 | + } |
| 160 | + for (auto &thread : threads) { |
| 161 | + thread.join(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // Benchmark |
| 166 | + for (size_t i = 0u; i < arguments.iterations; i++) { |
| 167 | + std::shared_mutex barrier; |
| 168 | + std::vector<std::thread> threads; |
| 169 | + std::vector<Timer> timers(arguments.numThreads); |
| 170 | + |
| 171 | + std::unique_lock<std::shared_mutex> lock(barrier); |
| 172 | + for (size_t j = 0u; j < arguments.numThreads; j++) { |
| 173 | + threads.emplace_back([&, j] { |
| 174 | + std::shared_lock<std::shared_mutex> lock(barrier); |
| 175 | + worker(j, timers[j]); |
| 176 | + }); |
| 177 | + } |
| 178 | + lock.unlock(); |
| 179 | + |
| 180 | + auto aggregatedTime = std::chrono::high_resolution_clock::duration(0); |
| 181 | + for (size_t j = 0u; j < arguments.numThreads; j++) { |
| 182 | + threads[j].join(); |
| 183 | + aggregatedTime += timers[j].get(); |
| 184 | + } |
| 185 | + auto avgTime = aggregatedTime / arguments.numThreads; |
| 186 | + |
| 187 | +#ifndef NDEBUG |
| 188 | + auto res = verifyResults(numThreads, numOpsPerThread, allocSize, dst_buffers, 1); |
| 189 | + if (res != TestResult::Success) |
| 190 | + return res; |
| 191 | +#endif |
| 192 | + |
| 193 | + statistics.pushValue(avgTime, typeSelector.getUnit(), typeSelector.getType()); |
| 194 | + } |
| 195 | + |
| 196 | + if (srcUSM) { |
| 197 | + sycl::free(src_buffer, queues[0].get_context()); |
| 198 | + } else { |
| 199 | + free(src_buffer); |
| 200 | + } |
| 201 | + |
| 202 | + return TestResult::Success; |
| 203 | +} |
| 204 | + |
| 205 | +[[maybe_unused]] static RegisterTestCaseImplementation<MemcpyExecute> registerTestCase(run, Api::SYCL); |
0 commit comments