Skip to content

Commit f0eb83d

Browse files
committed
Add new priority class for inference that can tolerate starvation.
1 parent eec9a7d commit f0eb83d

19 files changed

+118
-55
lines changed

SerialPrograms/Source/CommonFramework/Environment/Environment_Linux.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ enum class ThreadPriority{
2020
Min,
2121
};
2222

23-
constexpr ThreadPriority DEFAULT_PRIORITY_REALTIME = ThreadPriority::Max;
24-
constexpr ThreadPriority DEFAULT_PRIORITY_INFERENCE = ThreadPriority::Max;
25-
constexpr ThreadPriority DEFAULT_PRIORITY_COMPUTE = ThreadPriority::Min;
23+
constexpr ThreadPriority DEFAULT_PRIORITY_REALTIME = ThreadPriority::Max;
24+
constexpr ThreadPriority DEFAULT_PRIORITY_REALTIME_INFERENCE = ThreadPriority::Max;
25+
constexpr ThreadPriority DEFAULT_PRIORITY_NORMAL_INFERENCE = ThreadPriority::Min;
26+
constexpr ThreadPriority DEFAULT_PRIORITY_COMPUTE = ThreadPriority::Min;
2627

2728

2829

SerialPrograms/Source/CommonFramework/Environment/Environment_Windows.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ enum class ThreadPriority{
2222
Low,
2323
};
2424

25-
constexpr ThreadPriority DEFAULT_PRIORITY_REALTIME = ThreadPriority::High;
26-
constexpr ThreadPriority DEFAULT_PRIORITY_INFERENCE = ThreadPriority::AboveNormal;
27-
constexpr ThreadPriority DEFAULT_PRIORITY_COMPUTE = ThreadPriority::BelowNormal;
25+
constexpr ThreadPriority DEFAULT_PRIORITY_REALTIME = ThreadPriority::High;
26+
constexpr ThreadPriority DEFAULT_PRIORITY_REALTIME_INFERENCE = ThreadPriority::AboveNormal;
27+
constexpr ThreadPriority DEFAULT_PRIORITY_NORMAL_INFERENCE = ThreadPriority::BelowNormal;
28+
constexpr ThreadPriority DEFAULT_PRIORITY_COMPUTE = ThreadPriority::BelowNormal;
2829

2930

3031

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@ class PerformanceOptions : public GroupOption{
4040
"Restart the program for this to fully take effect.",
4141
DEFAULT_PRIORITY_REALTIME
4242
)
43-
, INFERENCE_PRIORITY(
43+
, REALTIME_INFERENCE_PRIORITY(
4444
"<b>Inference Priority:</b><br>"
45-
"Thread priority of inference threads. (image/sound recognition)",
46-
DEFAULT_PRIORITY_INFERENCE
45+
"Thread priority of realtime inference threads that must run fast "
46+
"enough to keep a program working properly.",
47+
DEFAULT_PRIORITY_REALTIME_INFERENCE
48+
)
49+
, NORMAL_INFERENCE_PRIORITY(
50+
"<b>normal Inference Priority:</b><br>"
51+
"Thread priority of non-realtime inference threads that can be slow "
52+
"without negatively affecting a program.",
53+
DEFAULT_PRIORITY_NORMAL_INFERENCE
4754
)
4855
, COMPUTE_PRIORITY(
4956
"<b>Compute Priority:</b><br>"
@@ -53,7 +60,8 @@ class PerformanceOptions : public GroupOption{
5360
{
5461
PA_ADD_OPTION(PRECISE_WAKE_MARGIN);
5562
PA_ADD_OPTION(REALTIME_THREAD_PRIORITY);
56-
PA_ADD_OPTION(INFERENCE_PRIORITY);
63+
PA_ADD_OPTION(REALTIME_INFERENCE_PRIORITY);
64+
PA_ADD_OPTION(NORMAL_INFERENCE_PRIORITY);
5765
PA_ADD_OPTION(COMPUTE_PRIORITY);
5866
PA_ADD_OPTION(PROCESSOR_LEVEL);
5967
}
@@ -62,7 +70,8 @@ class PerformanceOptions : public GroupOption{
6270
MicrosecondsOption PRECISE_WAKE_MARGIN;
6371

6472
ThreadPriorityOption REALTIME_THREAD_PRIORITY;
65-
ThreadPriorityOption INFERENCE_PRIORITY;
73+
ThreadPriorityOption REALTIME_INFERENCE_PRIORITY;
74+
ThreadPriorityOption NORMAL_INFERENCE_PRIORITY;
6675
ThreadPriorityOption COMPUTE_PRIORITY;
6776

6877
ProcessorLevelOption PROCESSOR_LEVEL;

SerialPrograms/Source/CommonFramework/Tools/ProgramEnvironment.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ struct ProgramEnvironmentData{
2121
const ProgramInfo& m_program_info;
2222

2323
AsyncDispatcher m_realtime_dispatcher;
24-
AsyncDispatcher m_inference_dispatcher;
24+
AsyncDispatcher m_realtime_inference_dispatcher;
25+
AsyncDispatcher m_normal_inference_dispatcher;
2526

2627
ProgramEnvironmentData(
2728
const ProgramInfo& program_info
@@ -33,9 +34,15 @@ struct ProgramEnvironmentData{
3334
},
3435
0
3536
)
36-
, m_inference_dispatcher(
37+
, m_realtime_inference_dispatcher(
3738
[](){
38-
GlobalSettings::instance().PERFORMANCE->INFERENCE_PRIORITY.set_on_this_thread();
39+
GlobalSettings::instance().PERFORMANCE->REALTIME_INFERENCE_PRIORITY.set_on_this_thread();
40+
},
41+
0
42+
)
43+
, m_normal_inference_dispatcher(
44+
[](){
45+
GlobalSettings::instance().PERFORMANCE->NORMAL_INFERENCE_PRIORITY.set_on_this_thread();
3946
},
4047
0
4148
)
@@ -66,8 +73,11 @@ const ProgramInfo& ProgramEnvironment::program_info() const{
6673
AsyncDispatcher& ProgramEnvironment::realtime_dispatcher(){
6774
return m_data->m_realtime_dispatcher;
6875
}
69-
AsyncDispatcher& ProgramEnvironment::inference_dispatcher(){
70-
return m_data->m_inference_dispatcher;
76+
AsyncDispatcher& ProgramEnvironment::realtime_inference_dispatcher(){
77+
return m_data->m_realtime_inference_dispatcher;
78+
}
79+
AsyncDispatcher& ProgramEnvironment::normal_inference_dispatcher(){
80+
return m_data->m_normal_inference_dispatcher;
7181
}
7282

7383

SerialPrograms/Source/CommonFramework/Tools/ProgramEnvironment.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,18 @@ class ProgramEnvironment{
4141

4242
public:
4343
// Thread Pools
44+
45+
// A high-priority dispatcher for program and logic threads that cannot
46+
// tolerate being delayed.
4447
AsyncDispatcher& realtime_dispatcher();
45-
AsyncDispatcher& inference_dispatcher();
48+
49+
// A high-priority dispatcher for inference where starvation may cause the
50+
// program to encounter issues.
51+
AsyncDispatcher& realtime_inference_dispatcher();
52+
53+
// A low-priority dispatcher for inference where starvation will not affect
54+
// the functionality of the program.
55+
AsyncDispatcher& normal_inference_dispatcher();
4656

4757
public:
4858
// Stats Management

SerialPrograms/Source/CommonTools/GlobalInferenceRunner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace PokemonAutomation{
1515
ParallelTaskRunner& global_inference_runner(){
1616
static ParallelTaskRunner runner(
1717
[](){
18-
GlobalSettings::instance().PERFORMANCE->INFERENCE_PRIORITY.set_on_this_thread();
18+
GlobalSettings::instance().PERFORMANCE->NORMAL_INFERENCE_PRIORITY.set_on_this_thread();
1919
},
2020
0, std::thread::hardware_concurrency()
2121
);

SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase3_ProController.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ ProController_SysbotBase3::~ProController_SysbotBase3(){
3737

3838
const ControllerFeatures& ProController_SysbotBase3::controller_features() const{
3939
static const ControllerFeatures features{
40-
ControllerFeature::TickPrecise, // TODO: Prove it is tick precise.
40+
ControllerFeature::TickPrecise,
4141
ControllerFeature::TimingFlexibleMilliseconds,
4242
ControllerFeature::NintendoSwitch_ProController,
43-
ControllerFeature::NintendoSwitch_DateSkip, // TODO: Prove this works
43+
ControllerFeature::NintendoSwitch_DateSkip,
4444
};
4545
return features;
4646
}

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
325325
ProControllerContext context(scope, console.pro_controller());
326326
VideoOverlaySet overlays(overlay);
327327

328+
FastCodeEntry::numberpad_enter_code(console, context, "708538991006", true);
329+
328330

329331

330332
#if 0
@@ -416,14 +418,14 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
416418
#endif
417419

418420

419-
#if 0
420-
HomeMenuDetector detector0;
421-
StartGameUserSelectDetector detector1;
422-
UpdatePopupDetector detector2;
423-
detector0.make_overlays(overlays);
424-
detector1.make_overlays(overlays);
421+
#if 1
422+
HomeMenuDetector detector0(console);
423+
StartGameUserSelectDetector detector1(console);
424+
UpdatePopupDetector detector2(console);
425+
// detector0.make_overlays(overlays);
426+
// detector1.make_overlays(overlays);
425427
detector2.make_overlays(overlays);
426-
cout << detector1.detect(feed.snapshot()) << endl;
428+
cout << detector2.detect(feed.snapshot()) << endl;
427429
#endif
428430

429431

SerialPrograms/Source/NintendoSwitch/NintendoSwitch_MultiSwitchProgram.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ MultiSwitchProgramEnvironment::MultiSwitchProgramEnvironment(
3131
, consoles(std::move(p_switches))
3232
{
3333
for (ConsoleHandle& console : consoles){
34-
console.initialize_inference_threads(scope, inference_dispatcher());
34+
console.initialize_inference_threads(scope, realtime_inference_dispatcher());
3535
}
3636
}
3737

SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class SingleSwitchProgramEnvironment : public ProgramEnvironment{
4444
: ProgramEnvironment(program_info, session, current_stats, historical_stats)
4545
, console(0, std::forward<Args>(args)...)
4646
{
47-
console.initialize_inference_threads(scope, inference_dispatcher());
47+
console.initialize_inference_threads(scope, realtime_inference_dispatcher());
4848
}
4949
};
5050

0 commit comments

Comments
 (0)