JaffarPlus
High-performance best-first search optimizer for tool-assisted speedruns
Loading...
Searching...
No Matches
jaffar.cpp
Go to the documentation of this file.
1
12#include "driver.hpp"
13#include <argparse/argparse.hpp>
14#include <cstdlib>
15#include <jaffarCommon/json.hpp>
16#include <jaffarCommon/logger.hpp>
17#include <jaffarCommon/string.hpp>
18
33int main(int argc, char* argv[])
34{
35 // Parsing command line arguments
36 argparse::ArgumentParser program("jaffar", "2.0.0");
37
38 program.add_argument("configFile").help("path to the Jaffar configuration script (.jaffar) file to run.").required();
39 program.add_argument("--dryRun").help("Only check for configuration without initializing or running the engine").default_value(false).implicit_value(true);
40
41 // Try to parse arguments
42 try
43 {
44 program.parse_args(argc, argv);
45 }
46 catch (const std::runtime_error& err)
47 {
48 JAFFAR_THROW_LOGIC("%s\n%s", err.what(), program.help().str().c_str());
49 }
50
51 // Getting config file name
52 const std::string configFile = program.get<std::string>("configFile");
53
54 // Getting dry run option
55 const bool isDryRun = program.get<bool>("--dryRun");
56
57 // In dry-run mode we only validate the configuration. Signal this to the construction path (via the
58 // same env var the test harness uses) so host-specific side effects -- the NUMA topology check and
59 // trace-file loading -- are skipped: config validation must not depend on the host or on data files.
60 if (isDryRun) setenv("JAFFAR_IS_DRY_RUN", "1", 1);
61
62 // Reporting script file
63 jaffarCommon::logger::log("[J+] Loading script file: '%s'\n", configFile.c_str());
64
65 // Loading script file contents
66 std::string configFileString;
67 if (jaffarCommon::file::loadStringFromFile(configFileString, configFile) == false)
68 JAFFAR_THROW_RUNTIME("[ERROR] Could not find or read from Jaffar config file: %s\n%s \n", configFile.c_str(), program.help().str().c_str());
69
70 // Parsing JSON from script file
71 nlohmann::json config;
72 try
73 {
74 config = nlohmann::json::parse(configFileString);
75 }
76 catch (const std::exception& err)
77 {
78 JAFFAR_THROW_LOGIC("[ERROR] Parsing configuration file %s. Details:\n%s\n", configFile.c_str(), err.what());
79 }
80
81 // Creating driver to run the Jaffar engine
82 auto d = jaffarPlus::Driver::getDriver(configFile, config);
83
84 // Returning now if dry running
85 if (isDryRun)
86 {
87 jaffarCommon::logger::log("[J+] Finished dry run successfully.\n");
88 return 0;
89 }
90
91 // Initializing driver
92 d->initialize();
93
94 // Running driver
95 auto exitReason = d->run();
96
97 // Printing exit reason
98 std::string exitReasonString;
99 if (exitReason == jaffarPlus::Driver::exitReason_t::winStateFound) exitReasonString = "Solution found.";
100 if (exitReason == jaffarPlus::Driver::exitReason_t::outOfStates) exitReasonString = "Engine ran out of states.";
101 if (exitReason == jaffarPlus::Driver::exitReason_t::maximumStepReached) exitReasonString = "Maximum step count reached.";
102 if (exitReason == jaffarPlus::Driver::exitReason_t::bestBelowReference) exitReasonString = "Best fell below the reference reward floor.";
104 exitReasonString = "Input-history trie neared its hard memory ceiling (use Type \"Raw\" or a smaller State DB).";
105
106 // Getting current step
107 auto finalStep = d->getCurrentStep();
108
109 // Printing exit message
110 jaffarCommon::logger::log("[J+] Step %lu - Exit Reason: %s\n", finalStep, exitReasonString.c_str());
111
112 // Return exit reason
113 return exitReason;
114}
@ outOfStates
Engine ran out of states.
Definition driver.hpp:40
@ bestBelowReference
The best state's reward fell below the reference reward floor at this step.
Definition driver.hpp:44
@ winStateFound
Found a win state.
Definition driver.hpp:38
@ maximumStepReached
Maximum step reached.
Definition driver.hpp:42
@ inputHistoryNearCapacity
The shared input-history trie neared/hit its hard memory ceiling.
Definition driver.hpp:46
static std::unique_ptr< Driver > getDriver(const std::string &configFilePath, const nlohmann::json &config)
Factory that constructs a driver from configuration.
Definition driver.hpp:575
Top-level orchestration of a Jaffar search run: drives the engine's step loop, tracks the best/worst ...
int main(int argc, char *argv[])
Entry point for the jaffar engine executable.
Definition jaffar.cpp:33