载入中...
搜索中...
未找到
ProcgenSystem.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <chrono>
5#include <utility>
6
7namespace eve::procgen {
8
9namespace {
10uint64_t monotonicNanoseconds() {
11 return uint64_t(std::chrono::duration_cast<std::chrono::nanoseconds>(
12 std::chrono::steady_clock::now().time_since_epoch())
13 .count());
14}
15} // namespace
16
17ProcgenContext::ProcgenContext(std::string systemName, uint32_t seed, std::string buildKey, bool cacheHit)
18 : name_(std::move(systemName)),
19 seed_(seed ? seed : 1u),
20 active_(!cacheHit),
21 cacheHit_(cacheHit),
22 buildKey_(std::move(buildKey)) {}
23
24std::string ProcgenContext::getName() const { return name_; }
25uint32_t ProcgenContext::getSeed() const { return seed_; }
26uint32_t ProcgenContext::seedFor(const std::string& scope) const { return deriveSeed(seed_, scope); }
27bool ProcgenContext::isActive() const { return active_; }
28bool ProcgenContext::hasFailed() const { return !error_.empty(); }
29bool ProcgenContext::isCacheHit() const { return cacheHit_; }
30std::string ProcgenContext::getError() const { return error_; }
31std::string ProcgenContext::getBuildKey() const { return buildKey_; }
32
33bool ProcgenContext::publish(const std::string& outputName, PointSet* points) {
34 if (!active_) {
35 error_ = "publish: transaction is closed";
36 return false;
37 }
38 if (outputName.empty()) {
39 error_ = "publish: output name is empty";
40 return false;
41 }
42 if (!points) {
43 error_ = "publish: null PointSet";
44 return false;
45 }
46 if (outputs_.find(outputName) == outputs_.end()) outputOrder_.push_back(outputName);
47 outputs_[outputName] = *points;
48 return true;
49}
50
51bool ProcgenContext::hasOutput(const std::string& outputName) const {
52 return outputs_.find(outputName) != outputs_.end();
53}
54
55int ProcgenContext::getOutputCount() const { return int(outputOrder_.size()); }
56
57std::string ProcgenContext::getOutputName(int index) const {
58 return index >= 0 && index < int(outputOrder_.size()) ? outputOrder_[size_t(index)] : std::string();
59}
60
61PointSet* ProcgenContext::getOutput(const std::string& outputName) const {
62 const auto found = outputs_.find(outputName);
63 return found == outputs_.end() ? nullptr : new PointSet(found->second);
64}
65
66bool ProcgenContext::captureDebug(const std::string& stageName, PointSet* points) {
67 if (!active_) {
68 error_ = "captureDebug: transaction is closed";
69 return false;
70 }
71 if (stageName.empty()) {
72 error_ = "captureDebug: stage name is empty";
73 return false;
74 }
75 if (!points) {
76 error_ = "captureDebug: null PointSet";
77 return false;
78 }
79 if (debugStages_.find(stageName) == debugStages_.end()) debugStageOrder_.push_back(stageName);
80 debugStages_[stageName] = *points;
81 return true;
82}
83
84int ProcgenContext::getDebugStageCount() const { return int(debugStageOrder_.size()); }
85
87 return index >= 0 && index < int(debugStageOrder_.size()) ? debugStageOrder_[size_t(index)] : std::string();
88}
89
90PointSet* ProcgenContext::getDebugStage(const std::string& stageName) const {
91 const auto found = debugStages_.find(stageName);
92 return found == debugStages_.end() ? nullptr : new PointSet(found->second);
93}
94
95PointSet* ProcgenContext::reuseStage(const std::string& stageName, const std::string& cacheKey) {
96 if (!active_ || stageName.empty() || cacheKey.empty()) return nullptr;
97 const auto found = stageCache_.find(stageName);
98 if (found == stageCache_.end() || found->second.cacheKey != cacheKey) {
99 ++stageCacheMisses_;
100 return nullptr;
101 }
102 ++stageCacheHits_;
103 return new PointSet(found->second.points);
104}
105
106bool ProcgenContext::cacheStage(const std::string& stageName, const std::string& cacheKey, PointSet* points) {
107 if (!active_) {
108 error_ = "cacheStage: transaction is closed";
109 return false;
110 }
111 if (stageName.empty() || cacheKey.empty()) {
112 error_ = "cacheStage: stage name and cache key are required";
113 return false;
114 }
115 if (!points) {
116 error_ = "cacheStage: null PointSet";
117 return false;
118 }
119 stageCache_[stageName] = {cacheKey, *points};
120 return true;
121}
122
123int ProcgenContext::getStageCacheHitCount() const { return stageCacheHits_; }
124int ProcgenContext::getStageCacheMissCount() const { return stageCacheMisses_; }
125
126void ProcgenContext::trace(const std::string& stageName, int inputCount, int outputCount, float milliseconds) {
127 if (!active_ || stageName.empty()) return;
128 traces_.push_back({stageName, std::max(0, inputCount), std::max(0, outputCount), std::max(0.f, milliseconds)});
129}
130
131bool ProcgenContext::beginTrace(const std::string& stageName, int inputCount) {
132 if (!active_) {
133 error_ = "beginTrace: transaction is closed";
134 return false;
135 }
136 if (stageName.empty()) {
137 error_ = "beginTrace: stage name is empty";
138 return false;
139 }
140 openTraces_.push_back({stageName, std::max(0, inputCount), monotonicNanoseconds()});
141 return true;
142}
143
144bool ProcgenContext::endTrace(int outputCount) {
145 if (!active_) {
146 error_ = "endTrace: transaction is closed";
147 return false;
148 }
149 if (openTraces_.empty()) {
150 error_ = "endTrace: no stage timer is active";
151 return false;
152 }
153 const auto open = std::move(openTraces_.back());
154 openTraces_.pop_back();
155 const float milliseconds = float(monotonicNanoseconds() - open.startedNanoseconds) / 1000000.f;
156 traces_.push_back({open.name, open.inputCount, std::max(0, outputCount), milliseconds});
157 return true;
158}
159
160int ProcgenContext::getOpenTraceCount() const { return int(openTraces_.size()); }
161
162int ProcgenContext::getTraceCount() const { return int(traces_.size()); }
163
164std::string ProcgenContext::getTraceName(int index) const {
165 return index >= 0 && index < int(traces_.size()) ? traces_[size_t(index)].name : std::string();
166}
167
169 return index >= 0 && index < int(traces_.size()) ? traces_[size_t(index)].inputCount : 0;
170}
171
173 return index >= 0 && index < int(traces_.size()) ? traces_[size_t(index)].outputCount : 0;
174}
175
177 return index >= 0 && index < int(traces_.size()) ? traces_[size_t(index)].milliseconds : 0.f;
178}
179
180void ProcgenContext::fail(const std::string& error) {
181 if (active_) error_ = error.empty() ? "generation failed" : error;
182}
183
185 if (!active_) return;
186 error_ = error_.empty() ? "transaction aborted" : error_;
187 close();
188}
189
190void ProcgenContext::close() { active_ = false; }
191
192} // namespace eve::procgen
JobScope scope
double & milliseconds
Definition OnnxGpgpu.cpp:19
std::string error
Definition Package.cpp:60
std::uint32_t seed
Definition PointSet.cpp:807
std::shared_ptr< const std::vector< glm::vec2 > > points
bool found
std::uint32_t count
uint32_t index
Script-friendly collection of attributed 3D samples.
Definition PointSet.h:59
int getStageCacheHitCount() const
Returns the stage cache hit count.
void trace(const std::string &stageName, int inputCount, int outputCount, float milliseconds)
Trace.
bool publish(const std::string &outputName, PointSet *points)
Publish.
bool endTrace(int outputCount)
Finish the most recently started timer and append its measured trace.
PointSet * reuseStage(const std::string &stageName, const std::string &cacheKey)
Reuse stage.
int getOutputCount() const
Returns the output count.
bool beginTrace(const std::string &stageName, int inputCount)
Start an automatically timed diagnostic stage. Timers may be nested.
uint32_t seedFor(const std::string &scope) const
Seed for.
std::string getName() const
Returns the name.
int getOpenTraceCount() const
Number of automatic timers that have not yet been finished.
bool cacheStage(const std::string &stageName, const std::string &cacheKey, PointSet *points)
Cache stage.
bool isActive() const
True when active.
bool isCacheHit() const
True when cache hit.
uint32_t getSeed() const
Returns the seed.
int getTraceOutputCount(int index) const
Returns the trace output count.
float getTraceMilliseconds(int index) const
Returns the trace milliseconds.
ProcgenContext(std::string systemName, uint32_t seed, std::string buildKey={}, bool cacheHit=false)
Procgen context.
int getTraceCount() const
Returns the trace count.
void fail(const std::string &error)
Fail.
int getDebugStageCount() const
Returns the debug stage count.
bool captureDebug(const std::string &stageName, PointSet *points)
Capture debug.
int getTraceInputCount(int index) const
Returns the trace input count.
bool hasFailed() const
True when failed.
std::string getError() const
Returns the error.
std::string getOutputName(int index) const
Returns the output name.
std::string getDebugStageName(int index) const
Returns the debug stage name.
int getStageCacheMissCount() const
Returns the stage cache miss count.
std::string getTraceName(int index) const
Returns the trace name.
PointSet * getDebugStage(const std::string &stageName) const
Returns the debug stage.
PointSet * getOutput(const std::string &outputName) const
Returns the output.
std::string getBuildKey() const
Returns the build key.
bool hasOutput(const std::string &outputName) const
True when output.
uint32_t deriveSeed(uint32_t parent, const std::string &scope)
Stable label-based seed derivation; independent pipeline branches do not perturb each other.
Definition PointSet.cpp:459