ngscopeclient v0.2.1
Loading...
Searching...
No Matches
FilterGraphExecutor.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2026 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
37#ifndef FilterGraphExecutor_h
38#define FilterGraphExecutor_h
39
40#include <condition_variable>
41#include <atomic>
42
49{
50public:
51
52 ConcurrentDispatchBatch(bool needBegin, bool needEnd, const std::set<FlowGraphNode*>& nodes)
53 : m_needBegin(needBegin)
54 , m_needEnd(needEnd)
55 , m_nodes(nodes)
56 {}
57
58 bool GetNeedBegin()
59 { return m_needBegin; }
60
61 bool GetNeedEnd()
62 { return m_needEnd; }
63
64 const std::set<FlowGraphNode*>& GetNodes()
65 { return m_nodes; }
66
70 void Run(vk::raii::CommandBuffer& cmdBuf, std::shared_ptr<QueueHandle> queue)
71 {
72 for(auto p : m_nodes)
73 p->Refresh(cmdBuf, queue);
74 }
75
76protected:
77 bool m_needBegin;
78 bool m_needEnd;
79 std::set<FlowGraphNode*> m_nodes;
80};
81
86{
87public:
88 void AddBatch(const ConcurrentDispatchBatch& batch)
89 { m_batches.push_back(batch); }
90
91 void Run(vk::raii::CommandBuffer& cmdBuf, std::shared_ptr<QueueHandle> queue);
92
93 bool empty()
94 { return m_batches.empty(); }
95
96 std::set<FlowGraphNode*> GetNodes();
97
98protected:
99 std::vector<ConcurrentDispatchBatch> m_batches;
100};
101
107{
108public:
109 FilterGraphExecutor(size_t numThreads = 8);
111
112 void RunBlocking(const std::set<FlowGraphNode*>& nodes);
113
115
117 std::map<FlowGraphNode*, int64_t> GetRunTimes()
118 {
119 std::lock_guard<std::mutex> lock(m_perfStatsMutex);
120 return m_lastExecutionTime;
121 }
122
123 std::string GetName(FlowGraphNode* node)
124 {
125 auto f = dynamic_cast<InstrumentChannel*>(node);
126 if(f)
127 return f->GetDisplayName();
128 else
129 return "(non-filter node)";
130 }
131
132protected:
133 void FindConcurrentNodes(
135 std::set<FlowGraphNode*>& workingSet,
136 bool& needBegin,
137 bool& needEnd);
138
139 void MakeBatchForNodes(SubmitBatch& batch, std::set<FlowGraphNode*>& workingSet, bool needBegin, bool needEnd);
141
142 static void ExecutorThread(FilterGraphExecutor* pThis, size_t i);
143 void DoExecutorThread(size_t i);
144
145 void UpdateRunnable();
146
148 std::mutex m_mutex;
149
151 std::set<FlowGraphNode*> m_incompleteNodes;
152
154 std::set<FlowGraphNode*> m_runnableNodes;
155
157 std::set<FlowGraphNode*> m_runningNodes;
158
160 std::vector<std::unique_ptr<std::thread>> m_threads;
161
163 std::condition_variable m_workerCvar;
164
167
169 std::condition_variable m_completionCvar;
170
173
176
179
181 std::map<FlowGraphNode*, int64_t> m_lastExecutionTime;
182
184 std::map<FlowGraphNode*, int64_t> m_currentExecutionTime;
185
188};
189
190#endif
Definition AcceleratorBuffer.h:204
A set of filters that can be issued to the GPU concurrently and have no mutual dependencies.
Definition FilterGraphExecutor.h:49
void Run(vk::raii::CommandBuffer &cmdBuf, std::shared_ptr< QueueHandle > queue)
Enqueue all of the filters in this batch to the command buffer, and possibly submit it.
Definition FilterGraphExecutor.h:70
Execution manager / scheduler for the filter graph.
Definition FilterGraphExecutor.h:107
std::vector< std::unique_ptr< std::thread > > m_threads
Set of thread contexts.
Definition FilterGraphExecutor.h:160
std::map< FlowGraphNode *, int64_t > m_currentExecutionTime
Performance statistics from current execution.
Definition FilterGraphExecutor.h:184
std::condition_variable m_completionCvar
Condition variable for waking up main thread when work is complete.
Definition FilterGraphExecutor.h:169
std::mutex m_perfStatsMutex
Mutex for updating performance statistics.
Definition FilterGraphExecutor.h:187
std::set< FlowGraphNode * > m_incompleteNodes
Nodes that have not yet been updated.
Definition FilterGraphExecutor.h:151
std::map< FlowGraphNode *, int64_t > GetRunTimes()
Get the run times of the most recent filter graph evaluation.
Definition FilterGraphExecutor.h:117
std::map< FlowGraphNode *, int64_t > m_lastExecutionTime
Performance statistics from previous execution.
Definition FilterGraphExecutor.h:181
void UpdateRunnable()
Searches m_incompleteNodes for any that are unblocked, and adds them to m_runnableNodes.
Definition FilterGraphExecutor.cpp:488
SubmitBatch GetNextBatch()
Returns the next batch of filters to run.
Definition FilterGraphExecutor.cpp:267
std::set< FlowGraphNode * > m_runningNodes
Nodes that are actively being run.
Definition FilterGraphExecutor.h:157
std::mutex m_completionCvarMutex
Mutex for access to m_completionCvar.
Definition FilterGraphExecutor.h:172
std::mutex m_mutex
Mutex for access to shared state.
Definition FilterGraphExecutor.h:148
bool FindNextHopNodes(SubmitBatch &batch)
Searches for nodes that will be eligible to run once anything in the batch has run and adds it.
Definition FilterGraphExecutor.cpp:347
bool m_terminating
Shutdown flag.
Definition FilterGraphExecutor.h:178
std::mutex m_workerCvarMutex
Mutex for access to m_workerCvar.
Definition FilterGraphExecutor.h:166
bool m_allWorkersComplete
Indicates that all worker threads have finished executing this pass.
Definition FilterGraphExecutor.h:175
std::set< FlowGraphNode * > m_runnableNodes
Nodes that have no dependencies and are eligible to run now.
Definition FilterGraphExecutor.h:154
static void ExecutorThread(FilterGraphExecutor *pThis, size_t i)
Thread function to handle filter graph execution.
Definition FilterGraphExecutor.cpp:526
std::condition_variable m_workerCvar
Condition variable for waking up worker threads when work arrives.
Definition FilterGraphExecutor.h:163
void RunBlocking(const std::set< FlowGraphNode * > &nodes)
Evaluates the filter graph, blocking until execution has completed.
Definition FilterGraphExecutor.cpp:118
Abstract base class for a node in the signal flow graph.
Definition FlowGraphNode.h:81
A single channel of an instrument.
Definition InstrumentChannel.h:63
A set of filters that can be issued to the GPU in a single submit call, but may have dependencies.
Definition FilterGraphExecutor.h:86