Skip to content

Commit a4697a6

Browse files
committed
Refactor out snapshot logic. Add basic infra for async frame conversions.
1 parent af8c59a commit a4697a6

19 files changed

+421
-125
lines changed

Common/Cpp/Concurrency/ParallelTaskRunner.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void ParallelTaskRunner::wait_for_everything(){
5454
});
5555
}
5656

57-
std::shared_ptr<AsyncTask> ParallelTaskRunner::dispatch(std::function<void()>&& func){
57+
std::shared_ptr<AsyncTask> ParallelTaskRunner::blocking_dispatch(std::function<void()>&& func){
5858
std::shared_ptr<AsyncTask> task(new AsyncTask(std::move(func)));
5959

6060
std::unique_lock<std::mutex> lg(m_lock);
@@ -74,6 +74,26 @@ std::shared_ptr<AsyncTask> ParallelTaskRunner::dispatch(std::function<void()>&&
7474

7575
return task;
7676
}
77+
std::shared_ptr<AsyncTask> ParallelTaskRunner::try_dispatch(std::function<void()>& func){
78+
std::lock_guard<std::mutex> lg(m_lock);
79+
80+
if (m_queue.size() + m_busy_count >= m_max_threads){
81+
return nullptr;
82+
}
83+
84+
std::shared_ptr<AsyncTask> task(new AsyncTask(std::move(func)));
85+
86+
// Enqueue task.
87+
m_queue.emplace_back(task);
88+
89+
if (m_queue.size() + m_busy_count > m_threads.size()){
90+
m_threads.emplace_back(run_with_catch, "ParallelTaskRunner::thread_loop()", [this]{ thread_loop(); });
91+
}
92+
93+
m_thread_cv.notify_one();
94+
95+
return task;
96+
}
7797

7898

7999
void ParallelTaskRunner::run_in_parallel(
@@ -89,7 +109,7 @@ void ParallelTaskRunner::run_in_parallel(
89109

90110
std::vector<std::shared_ptr<AsyncTask>> tasks;
91111
for (size_t c = 0; c < blocks; c++){
92-
tasks.emplace_back(dispatch([=, &func]{
112+
tasks.emplace_back(blocking_dispatch([=, &func]{
93113
size_t s = start + c * block_size;
94114
size_t e = std::min(s + block_size, end);
95115
// cout << "Running: [" << s << "," << e << ")" << endl;
@@ -108,9 +128,6 @@ void ParallelTaskRunner::run_in_parallel(
108128

109129

110130
void ParallelTaskRunner::thread_loop(){
111-
//#if _WIN32
112-
// SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE);
113-
//#endif
114131
if (m_new_thread_callback){
115132
m_new_thread_callback();
116133
}

Common/Cpp/Concurrency/ParallelTaskRunner.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ class ParallelTaskRunner{
2323

2424
void wait_for_everything();
2525

26-
std::shared_ptr<AsyncTask> dispatch(std::function<void()>&& func);
26+
// Dispatch the function. If there are no threads available, it waits until
27+
// there are.
28+
std::shared_ptr<AsyncTask> blocking_dispatch(std::function<void()>&& func);
29+
30+
// Dispatch the function. Returns null if no threads are available.
31+
// "func" will be moved-from only on success.
32+
std::shared_ptr<AsyncTask> try_dispatch(std::function<void()>& func);
2733

2834
void run_in_parallel(
2935
const std::function<void(size_t index)>& func,

SerialPrograms/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ file(GLOB MAIN_SOURCES
402402
Source/CommonFramework/Options/ResolutionOption.cpp
403403
Source/CommonFramework/Options/ResolutionOption.h
404404
Source/CommonFramework/Options/ScreenshotFormatOption.h
405+
Source/CommonFramework/Options/ThreadPoolOption.cpp
406+
Source/CommonFramework/Options/ThreadPoolOption.h
405407
Source/CommonFramework/Panels/PanelDescriptor.cpp
406408
Source/CommonFramework/Panels/PanelDescriptor.h
407409
Source/CommonFramework/Panels/PanelInstance.cpp
@@ -449,6 +451,8 @@ file(GLOB MAIN_SOURCES
449451
Source/CommonFramework/Tools/ErrorDumper.h
450452
Source/CommonFramework/Tools/FileDownloader.cpp
451453
Source/CommonFramework/Tools/FileDownloader.h
454+
Source/CommonFramework/Tools/GlobalThreadPools.cpp
455+
Source/CommonFramework/Tools/GlobalThreadPools.h
452456
Source/CommonFramework/Tools/ProgramEnvironment.cpp
453457
Source/CommonFramework/Tools/ProgramEnvironment.h
454458
Source/CommonFramework/Tools/StatAccumulator.cpp
@@ -464,6 +468,8 @@ file(GLOB MAIN_SOURCES
464468
Source/CommonFramework/VideoPipeline/Backends/MediaServicesQt6.cpp
465469
Source/CommonFramework/VideoPipeline/Backends/MediaServicesQt6.h
466470
Source/CommonFramework/VideoPipeline/Backends/QVideoFrameCache.h
471+
Source/CommonFramework/VideoPipeline/Backends/SnapshotManager.cpp
472+
Source/CommonFramework/VideoPipeline/Backends/SnapshotManager.h
467473
Source/CommonFramework/VideoPipeline/Backends/VideoFrameQt.h
468474
Source/CommonFramework/VideoPipeline/CameraInfo.h
469475
Source/CommonFramework/VideoPipeline/Stats/CpuUtilizationStats.h

SerialPrograms/Source/CommonFramework/Globals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace PokemonAutomation{
2626
const bool IS_BETA_VERSION = true;
2727
const int PROGRAM_VERSION_MAJOR = 0;
2828
const int PROGRAM_VERSION_MINOR = 54;
29-
const int PROGRAM_VERSION_PATCH = 9;
29+
const int PROGRAM_VERSION_PATCH = 10;
3030

3131
const std::string PROGRAM_VERSION_BASE =
3232
"v" + std::to_string(PROGRAM_VERSION_MAJOR) +

SerialPrograms/Source/CommonFramework/Options/Environment/PerformanceOptions.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "Common/Cpp/Options/GroupOption.h"
1111
#include "Common/Cpp/Options/TimeDurationOption.h"
12+
#include "CommonFramework/Options/ThreadPoolOption.h"
1213
#include "ProcessPriorityOption.h"
1314
#include "ProcessorLevelOption.h"
1415

@@ -47,7 +48,7 @@ class PerformanceOptions : public GroupOption{
4748
DEFAULT_PRIORITY_REALTIME_INFERENCE
4849
)
4950
, NORMAL_INFERENCE_PRIORITY(
50-
"<b>normal Inference Priority:</b><br>"
51+
"<b>Normal Inference Priority:</b><br>"
5152
"Thread priority of non-realtime inference threads that can be slow "
5253
"without negatively affecting a program.",
5354
DEFAULT_PRIORITY_NORMAL_INFERENCE
@@ -57,12 +58,28 @@ class PerformanceOptions : public GroupOption{
5758
"Thread priority of computation threads.",
5859
DEFAULT_PRIORITY_COMPUTE
5960
)
61+
, THREAD_POOL_REALTIME_INFERENCE(
62+
"Thread Pool: Real-time Inference",
63+
DEFAULT_PRIORITY_REALTIME_INFERENCE,
64+
0.5
65+
)
66+
, THREAD_POOL_NORMAL_INFERENCE(
67+
"Thread Pool: Normal Inference",
68+
DEFAULT_PRIORITY_NORMAL_INFERENCE,
69+
1.0
70+
)
6071
{
6172
PA_ADD_OPTION(PRECISE_WAKE_MARGIN);
73+
6274
PA_ADD_OPTION(REALTIME_THREAD_PRIORITY);
6375
PA_ADD_OPTION(REALTIME_INFERENCE_PRIORITY);
6476
PA_ADD_OPTION(NORMAL_INFERENCE_PRIORITY);
6577
PA_ADD_OPTION(COMPUTE_PRIORITY);
78+
79+
// TODO: REMOVE: Enable when these are actually used.
80+
// PA_ADD_OPTION(THREAD_POOL_REALTIME_INFERENCE);
81+
// PA_ADD_OPTION(THREAD_POOL_NORMAL_INFERENCE);
82+
6683
PA_ADD_OPTION(PROCESSOR_LEVEL);
6784
}
6885

@@ -74,6 +91,9 @@ class PerformanceOptions : public GroupOption{
7491
ThreadPriorityOption NORMAL_INFERENCE_PRIORITY;
7592
ThreadPriorityOption COMPUTE_PRIORITY;
7693

94+
ThreadPoolOption THREAD_POOL_REALTIME_INFERENCE;
95+
ThreadPoolOption THREAD_POOL_NORMAL_INFERENCE;
96+
7797
ProcessorLevelOption PROCESSOR_LEVEL;
7898
};
7999

SerialPrograms/Source/CommonFramework/Options/Environment/ProcessPriorityOption.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#define PokemonAutomation_ProcessPriorityOption_H
99

1010
#include "Common/Cpp/Options/EnumDropdownOption.h"
11-
#include "Common/Cpp/Options/GroupOption.h"
11+
//#include "Common/Cpp/Options/GroupOption.h"
1212
#include "CommonFramework/Environment/Environment.h"
1313

1414
namespace PokemonAutomation{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Thread Pool Option
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include <thread>
8+
#include "ThreadPoolOption.h"
9+
10+
namespace PokemonAutomation{
11+
12+
13+
ThreadPoolOption::ThreadPoolOption(
14+
std::string label,
15+
ThreadPriority default_priority,
16+
double default_max_thread_ratio
17+
)
18+
: GroupOption(std::move(label), LockMode::UNLOCK_WHILE_RUNNING)
19+
, m_default_max_threads(
20+
std::max(
21+
(size_t)(std::thread::hardware_concurrency() * default_max_thread_ratio),
22+
(size_t)1
23+
)
24+
)
25+
, HARDWARE_THREADS(
26+
"<b>Hardware Threads:</b>",
27+
LockMode::UNLOCK_WHILE_RUNNING,
28+
std::thread::hardware_concurrency()
29+
)
30+
, PRIORITY("<b>Thread Priority:</b>", default_priority)
31+
, MAX_THREADS(
32+
"<b>Maximum Threads:</b>",
33+
LockMode::UNLOCK_WHILE_RUNNING,
34+
m_default_max_threads
35+
)
36+
{
37+
PA_ADD_OPTION(HARDWARE_THREADS);
38+
PA_ADD_OPTION(PRIORITY);
39+
PA_ADD_OPTION(MAX_THREADS);
40+
HARDWARE_THREADS.set_visibility(ConfigOptionState::HIDDEN);
41+
}
42+
43+
void ThreadPoolOption::load_json(const JsonValue& json){
44+
GroupOption::load_json(json);
45+
46+
// Reset the max threads if the hardware threads has changed.
47+
if (HARDWARE_THREADS != std::thread::hardware_concurrency()){
48+
MAX_THREADS.set(m_default_max_threads);
49+
}
50+
}
51+
52+
53+
54+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Thread Pool Option
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_Options_ThreadPoolOption_H
8+
#define PokemonAutomation_Options_ThreadPoolOption_H
9+
10+
#include "Common/Cpp/Options/SimpleIntegerOption.h"
11+
#include "Common/Cpp/Options/GroupOption.h"
12+
#include "Environment/ProcessPriorityOption.h"
13+
14+
namespace PokemonAutomation{
15+
16+
17+
class ThreadPoolOption : public GroupOption{
18+
public:
19+
ThreadPoolOption(
20+
std::string label,
21+
ThreadPriority default_priority,
22+
double default_max_thread_ratio = 1.0
23+
);
24+
25+
virtual void load_json(const JsonValue& json) override;
26+
27+
private:
28+
const size_t m_default_max_threads;
29+
SimpleIntegerOption<size_t> HARDWARE_THREADS;
30+
31+
public:
32+
ThreadPriorityOption PRIORITY;
33+
SimpleIntegerOption<size_t> MAX_THREADS;
34+
};
35+
36+
37+
38+
}
39+
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Global Thread Pools
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "CommonFramework/GlobalSettingsPanel.h"
8+
#include "CommonFramework/Options/Environment/PerformanceOptions.h"
9+
#include "GlobalThreadPools.h"
10+
11+
namespace PokemonAutomation{
12+
namespace GlobalThreadPools{
13+
14+
15+
16+
ParallelTaskRunner& realtime_inference(){
17+
static ParallelTaskRunner runner(
18+
[](){
19+
GlobalSettings::instance().PERFORMANCE->THREAD_POOL_REALTIME_INFERENCE.PRIORITY.set_on_this_thread();
20+
},
21+
0, GlobalSettings::instance().PERFORMANCE->THREAD_POOL_REALTIME_INFERENCE.MAX_THREADS
22+
);
23+
return runner;
24+
}
25+
26+
27+
28+
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Global Thread Pools
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_CommonTools_GlobalThreadPools_H
8+
#define PokemonAutomation_CommonTools_GlobalThreadPools_H
9+
10+
#include "Common/Cpp/Concurrency/ParallelTaskRunner.h"
11+
12+
namespace PokemonAutomation{
13+
namespace GlobalThreadPools{
14+
15+
16+
17+
ParallelTaskRunner& realtime_inference();
18+
19+
20+
21+
}
22+
}
23+
#endif

0 commit comments

Comments
 (0)