Skip to content

Commit b7c33b9

Browse files
authored
option for mining hours (#136)
1 parent e4c09e1 commit b7c33b9

File tree

8 files changed

+78
-15
lines changed

8 files changed

+78
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ To install or update LODA, please follow the [installation instructions](https:/
22

33
## [Unreleased]
44

5+
### Features
6+
7+
* Added option `-H` for settings the number of mining hours (useful for BOINC)
8+
59
# v22.4.17
610

711
### Enhancements

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Options:
5757
-i <string> Name of miner configuration from miners.json
5858
-p Parallel mining using default number of instances
5959
-P <number> Parallel mining using custom number of instances
60+
-H <number> Number of mining hours (default unlimited)
6061
```
6162

6263
### Core Commands

src/commands.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ void Commands::help() {
4848
<< std::endl;
4949

5050
std::cout << std::endl << "OEIS Commands:" << std::endl;
51-
std::cout
52-
<< " mine Mine programs for OEIS sequences (see -i,-p)"
53-
<< std::endl;
51+
std::cout << " mine Mine programs for OEIS sequences (see "
52+
"-i,-p,-P,-H)"
53+
<< std::endl;
5454
std::cout << " check <program> Check a program for an OEIS sequence "
5555
"(see -b)"
5656
<< std::endl;
@@ -95,6 +95,9 @@ void Commands::help() {
9595
std::cout << " -P <number> Parallel mining using custom number of "
9696
"instances"
9797
<< std::endl;
98+
std::cout
99+
<< " -H <number> Number of mining hours (default unlimited)"
100+
<< std::endl;
98101
}
99102

100103
std::pair<std::string, size_t> getProgramPathAndSeqId(std::string arg) {

src/include/miner.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Miner {
3131
void submit(const std::string &path, std::string id);
3232

3333
private:
34-
void checkRegularTasks();
34+
bool checkRegularTasks();
3535

3636
void reload();
3737

@@ -58,6 +58,7 @@ class Miner {
5858
int64_t num_new;
5959
int64_t num_updated;
6060
int64_t num_removed;
61+
int64_t num_hours;
6162
int64_t current_fetch;
6263
std::map<std::string, int64_t> num_received_per_profile;
6364
};

src/include/util.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Settings {
6767
bool parallel_mining;
6868
bool report_cpu_hours;
6969
int64_t num_miner_instances;
70+
int64_t num_mine_hours;
7071
std::string miner_profile;
7172

7273
// flag and offset for printing evaluation results in b-file format

src/main.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ void mineParallel(const Settings& settings,
6868
num_instances = Setup::getMaxInstances();
6969
}
7070
const bool has_miner_profile = settings.miner_profile.empty();
71+
const bool restart_miners = (settings.num_mine_hours <= 0);
7172

7273
// prepare instance settings
7374
auto instance_settings = settings;
@@ -79,22 +80,34 @@ void mineParallel(const Settings& settings,
7980
AdaptiveScheduler cpuhours_scheduler(3600); // 1 hour (fixed!!)
8081
ApiClient api_client;
8182

82-
// run miner processes and monitor them
8383
Log::get().info("Starting parallel mining using " +
84-
std::to_string(num_instances) + " miner instances");
85-
while (true) {
86-
// check if miner processes are alive
84+
std::to_string(num_instances) + " instances");
85+
const auto start_time = std::chrono::steady_clock::now();
86+
87+
// run miner processes and monitor them
88+
bool finished = false;
89+
while (!finished) {
90+
// check if miner processes are alive, restart them if should not stop
91+
finished = true;
8792
for (size_t i = 0; i < children_pids.size(); i++) {
88-
if (!isChildProcessAlive(children_pids[i])) {
93+
const auto pid = children_pids[i];
94+
if (pid != 0 && isChildProcessAlive(pid)) {
95+
// still alive
96+
finished = false;
97+
} else if (pid == 0 || restart_miners) {
98+
// restart process
8999
if (has_miner_profile) {
90100
instance_settings.miner_profile = std::to_string(i);
91101
}
92102
children_pids[i] = fork(instance_settings, args);
93103
std::this_thread::sleep_for(std::chrono::seconds(5));
104+
finished = false;
94105
}
95106
}
107+
96108
// wait some time
97109
std::this_thread::sleep_for(std::chrono::seconds(10));
110+
98111
// report CPU hours
99112
if (cpuhours_scheduler.isTargetReached()) {
100113
cpuhours_scheduler.reset();
@@ -106,6 +119,14 @@ void mineParallel(const Settings& settings,
106119
}
107120
}
108121
}
122+
123+
// finished log message
124+
auto cur_time = std::chrono::steady_clock::now();
125+
int64_t mins =
126+
std::chrono::duration_cast<std::chrono::minutes>(cur_time - start_time)
127+
.count();
128+
Log::get().info("Finished parallel mining after " + std::to_string(mins) +
129+
" minutes");
109130
}
110131

111132
int dispatch(Settings settings, const std::vector<std::string>& args) {

src/miner.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Miner::Miner(const Settings &settings)
3030
num_new(0),
3131
num_updated(0),
3232
num_removed(0),
33+
num_hours(0),
3334
current_fetch(0) {}
3435

3536
void Miner::reload() {
@@ -49,13 +50,16 @@ void Miner::reload() {
4950
}
5051

5152
void Miner::mine() {
52-
if (!manager) {
53-
reload();
54-
}
5553
Parser parser;
5654
std::stack<Program> progs;
5755
Sequence norm_seq;
5856
Program program;
57+
const auto start_time = std::chrono::steady_clock::now();
58+
59+
// load manager
60+
if (!manager) {
61+
reload();
62+
}
5963

6064
// print info
6165
Log::get().info("Mining programs for OEIS sequences in " +
@@ -143,11 +147,22 @@ void Miner::mine() {
143147
}
144148

145149
num_processed++;
146-
checkRegularTasks();
150+
if (!checkRegularTasks()) {
151+
break;
152+
}
147153
}
154+
155+
// finish with log message
156+
auto cur_time = std::chrono::steady_clock::now();
157+
int64_t mins =
158+
std::chrono::duration_cast<std::chrono::minutes>(cur_time - start_time)
159+
.count();
160+
Log::get().info("Finished mining after " + std::to_string(mins) + " minutes");
148161
}
149162

150-
void Miner::checkRegularTasks() {
163+
bool Miner::checkRegularTasks() {
164+
bool result = true;
165+
151166
// regular task: log info about generated programs
152167
if (log_scheduler.isTargetReached()) {
153168
log_scheduler.reset();
@@ -195,13 +210,19 @@ void Miner::checkRegularTasks() {
195210
if (Setup::shouldReportCPUHours() && settings.report_cpu_hours) {
196211
api_client->postCPUHour();
197212
}
213+
num_hours++;
214+
if (settings.num_mine_hours > 0 && num_hours >= settings.num_mine_hours) {
215+
result = false;
216+
}
198217
}
199218

200219
// regular task: reload oeis manager and generators
201220
if (reload_scheduler.isTargetReached()) {
202221
reload_scheduler.reset();
203222
reload();
204223
}
224+
225+
return result;
205226
}
206227

207228
void Miner::submit(const std::string &path, std::string id) {

src/util.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ Settings::Settings()
204204
parallel_mining(false),
205205
report_cpu_hours(true),
206206
num_miner_instances(0),
207+
num_mine_hours(0),
207208
print_as_b_file(false),
208209
print_as_b_file_offset(0) {}
209210

@@ -214,6 +215,7 @@ enum class Option {
214215
MAX_CYCLES,
215216
B_FILE_OFFSET,
216217
NUM_INSTANCES,
218+
NUM_MINE_HOURS,
217219
MINER,
218220
LOG_LEVEL
219221
};
@@ -225,7 +227,7 @@ std::vector<std::string> Settings::parseArgs(int argc, char *argv[]) {
225227
std::string arg(argv[i]);
226228
if (option == Option::NUM_TERMS || option == Option::MAX_MEMORY ||
227229
option == Option::MAX_CYCLES || option == Option::B_FILE_OFFSET ||
228-
option == Option::NUM_INSTANCES) {
230+
option == Option::NUM_INSTANCES || option == Option::NUM_MINE_HOURS) {
229231
std::stringstream s(arg);
230232
int64_t val;
231233
s >> val;
@@ -250,6 +252,9 @@ std::vector<std::string> Settings::parseArgs(int argc, char *argv[]) {
250252
case Option::NUM_INSTANCES:
251253
num_miner_instances = val;
252254
break;
255+
case Option::NUM_MINE_HOURS:
256+
num_mine_hours = val;
257+
break;
253258
case Option::LOG_LEVEL:
254259
case Option::MINER:
255260
case Option::NONE:
@@ -291,6 +296,8 @@ std::vector<std::string> Settings::parseArgs(int argc, char *argv[]) {
291296
} else if (opt == "P") {
292297
parallel_mining = true;
293298
option = Option::NUM_INSTANCES;
299+
} else if (opt == "H") {
300+
option = Option::NUM_MINE_HOURS;
294301
} else if (opt == "b") {
295302
print_as_b_file = true;
296303
} else if (opt == "B") {
@@ -332,6 +339,10 @@ void Settings::printArgs(std::vector<std::string> &args) {
332339
if (parallel_mining) {
333340
args.push_back("-p");
334341
}
342+
if (num_mine_hours > 0) {
343+
args.push_back("-H");
344+
args.push_back(std::to_string(num_mine_hours));
345+
}
335346
if (!report_cpu_hours) {
336347
args.push_back("--no-report-cpu-hours");
337348
}

0 commit comments

Comments
 (0)