载入中...
搜索中...
未找到
OnnxCompiler.h
浏览该文件的文档.
1#pragma once
2#include <algorithm>
3#include <array>
4#include <atomic>
5#include <chrono>
6#include <condition_variable>
7#include <cstdio>
8#include <deque>
9#include <future>
10#include <mutex>
11#include <thread>
13
15// Session-owned CPU workers. Jobs own their source and never capture Graphics,
16// Run, buffers or callbacks. Joining the workers needs no device access.
19 std::vector<uint32_t> words;
20 double milliseconds = 0;
21};
24 using Job = std::packaged_task<CompiledProgram()>;
25 // Keep the first bounded segment's timestamps in memory. Worker-side I/O
26 // would perturb precisely the overlap that a low-peak failure needs to explain.
27 struct Timing {
28 long long enqueued = 0;
29 std::atomic<long long> started{0}, finished{0};
30 };
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())
36 .count();
37 }
38 std::mutex mutex;
39 std::condition_variable ready;
40 std::deque<Job> jobs;
41 bool stopping = false;
42 size_t inFlight = 0;
43 std::vector<std::thread> workers;
44 std::atomic<size_t> running{0}, peak{0};
45 void stop() noexcept {
46 {
47 std::lock_guard lock(mutex);
48 stopping = true;
49 }
50 ready.notify_all();
51 for (auto& worker : workers)
52 if (worker.joinable()) worker.join();
53 }
54
55public:
57 explicit CompilerQueue(uint32_t count) {
58 try {
59 for (uint32_t i = 0; i < count; ++i)
60 workers.emplace_back([this] {
61 for (;;) {
62 Job job;
63 {
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());
69 jobs.pop_front();
70 ++inFlight;
71 }
72 const auto n = ++running;
73 auto p = peak.load();
74 while (p < n && !peak.compare_exchange_weak(p, n)) {
75 }
77 job(); // packaged_task transports failures to the owning device thread.
78 --running;
79 {
81 std::lock_guard lock(mutex);
82 --inFlight;
83 }
84 ready.notify_all();
85 }
86 });
87 } catch (...) {
89 stop();
90 throw;
91 }
92 }
94 ~CompilerQueue() { stop(); }
95 // Called on the device thread. Run bounds each segment to 48 operations;
96 // this independent queue bound also protects accidental future callers.
102 std::future<CompiledProgram> enqueue(std::string source) {
103 auto* timing = timingCount < timings.size() ? &timings[timingCount] : nullptr;
104 ++timingCount;
105 if (timing) timing->enqueued = timestamp();
107 Job job([timing, source = std::move(source)] {
108 if (timing) timing->started = timestamp();
109 struct Completion {
110 Timing* timing;
111 ~Completion() {
112 if (timing) timing->finished = timestamp();
113 }
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);
118 return CompiledProgram{
120 std::move(result.value()),
121 std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count()};
122 });
123 auto future = job.get_future();
124 {
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));
129 }
130 ready.notify_one();
131 return future;
132 }
134 size_t peakWorkers() const noexcept { return peak.load(); }
136 void waitIdle() noexcept {
138 std::unique_lock lock(mutex);
139 ready.wait(lock, [this] { return jobs.empty() && inFlight == 0; });
140 // Report only after the workers are idle, without changing the peak or
141 // waiting for overlap. This also covers failed/abandoned graph cleanup.
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];
147 std::fprintf(
148 stderr,
149 "ONNX_COMPILER_TIMING job=%zu workers=%zu peak=1 enqueue_us=%lld start_us=%lld end_us=%lld\n", i,
150 workers.size(), t.enqueued - origin, t.started.load() - origin, t.finished.load() - origin);
151 }
152 }
153 }
155 void resetPeak() noexcept {
156 peak = 0;
157 timingCount = 0;
158 for (auto& timing : timings) {
159 timing.enqueued = 0;
160 timing.started = 0;
161 timing.finished = 0;
162 }
163 }
164};
165} // namespace eve::tensor::onnx_detail
Duration start
std::mutex mutex
bool enqueued
std::future< JobOutput > future
Poco::JSON::Object::Ptr result
glm::dvec3 origin
float t
std::string_view source
int count
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.
Definition Gpgpu.cpp:294
bool started
Definition Graphics.cpp:200