6#include <condition_variable>
29 std::atomic<long long>
started{0}, finished{0};
31 std::array<Timing, 48> timings;
32 size_t timingCount = 0;
33 static long long timestamp()
noexcept {
34 return std::chrono::duration_cast<std::chrono::microseconds>(
35 std::chrono::steady_clock::now().time_since_epoch())
39 std::condition_variable ready;
41 bool stopping =
false;
43 std::vector<std::thread> workers;
44 std::atomic<size_t> running{0}, peak{0};
45 void stop()
noexcept {
47 std::lock_guard lock(
mutex);
51 for (
auto& worker : workers)
52 if (worker.joinable()) worker.join();
59 for (uint32_t i = 0; i <
count; ++i)
60 workers.emplace_back([
this] {
65 std::unique_lock lock(mutex);
66 ready.wait(lock, [this] { return stopping || !jobs.empty(); });
67 if (jobs.empty()) return;
68 job = std::move(jobs.front());
72 const auto n = ++running;
74 while (p < n && !peak.compare_exchange_weak(p, n)) {
81 std::lock_guard lock(mutex);
103 auto* timing = timingCount < timings.size() ? &timings[timingCount] :
nullptr;
105 if (timing) timing->enqueued = timestamp();
108 if (timing) timing->started = timestamp();
112 if (timing) timing->finished = timestamp();
114 } completion{timing};
115 const auto start = std::chrono::steady_clock::now();
117 if (!
result.ok())
throw std::runtime_error(
result.error()->message() +
"\nCompute source:\n" +
source);
120 std::move(
result.value()),
121 std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() -
start).count()};
123 auto future = job.get_future();
126 std::lock_guard lock(
mutex);
127 if (stopping || jobs.size() >= 48)
throw std::runtime_error(
"ONNX compiler queue unavailable");
128 jobs.push_back(std::move(job));
138 std::unique_lock lock(
mutex);
139 ready.wait(lock, [
this] {
return jobs.empty() && inFlight == 0; });
142 if (workers.size() > 1 && timingCount > 1 && peak.load() == 1) {
143 const auto count = std::min(timingCount, timings.size());
144 const auto origin = timings[0].enqueued;
145 for (
size_t i = 0; i <
count; ++i) {
146 const auto&
t = timings[i];
149 "ONNX_COMPILER_TIMING job=%zu workers=%zu peak=1 enqueue_us=%lld start_us=%lld end_us=%lld\n", i,
158 for (
auto& timing : timings) {
std::future< JobOutput > future
CompilerQueue public API.
size_t peakWorkers() const noexcept
Peak workers.
~CompilerQueue()
Releases CompilerQueue resources.
void waitIdle() noexcept
Waits idle.
std::future< CompiledProgram > enqueue(std::string source)
Enqueue CPU-only shader compilation. @lifetime Captured timing slots belong to this queue....
void resetPeak() noexcept
Resets peak.
CompilerQueue(uint32_t count)
Constructs a CompilerQueue.
EVENGINE_API_WORLD Result< std::vector< uint32_t > > compileComputeSpirv(const std::string &source)
Compile GLSL compute source to owning SPIR-V without accessing Graphics or a GPU.
CompiledProgram public API.
std::vector< uint32_t > words