-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsp.cpp
139 lines (123 loc) · 3.91 KB
/
tsp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <string>
#include <thread>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "config.hpp"
#include "functions.hpp"
#include "jitter.hpp"
#include "locker.hpp"
#include "output_manager.hpp"
#include "proc_affinity.hpp"
#include "run_cmd.hpp"
#include "status_manager.hpp"
constexpr std::chrono::milliseconds base_wait_period{2000};
int main(int argc, char *argv[]) {
auto config = tsp::Config(argc, argv);
auto rerun = (config.get_int("rerun") >= 0);
if (!rerun) {
if (optind == argc) {
std::cerr << std::format(tsp::help, argv[0]) << std::endl;
die_with_err(
"ERROR! Requested to run a command, but no command specified", -1);
}
}
if (config.get_bool("do_fork")) {
auto main_fork_pid = pid_t{fork()};
if (main_fork_pid == -1) {
die_with_err("Unable to fork when forking requested", main_fork_pid);
}
if (main_fork_pid != 0) {
// We're done here
return 0;
}
}
auto stat = tsp::Status_Manager{};
auto cmd = rerun
? tsp::Run_cmd{stat.get_cmd_to_rerun(config.get_int("rerun"))}
: tsp::Run_cmd{argv, optind, argc};
if (rerun) {
// This variant of add_cmd will recover category and nslots from the jobid
stat.add_cmd(cmd, config.get_int("rerun"));
} else {
stat.add_cmd(cmd, config.get_string("category"), config.get_int("nslots"));
}
auto extern_jobid = stat.get_extern_jobid();
std::cout << extern_jobid << std::endl;
auto jitter = tsp::Jitter{tsp::jitter_ms};
std::this_thread::sleep_for(tsp::jitter_ms + jitter.get());
auto binder = tsp::Proc_affinity{stat, config.get_int("nslots"), getpid()};
std::vector<uint32_t> bound_cores;
{
auto locker = tsp::Locker();
for (;;) {
locker.lock();
if (stat.allowed_to_run()) {
break;
}
locker.unlock();
std::this_thread::sleep_for(base_wait_period + jitter.get());
}
stat.job_start();
bound_cores = binder.bind();
}
if (config.get_bool("verbose")) {
std::cout << "Job id " << extern_jobid << ": " << cmd.print()
<< "has started. Job was queued for "
<< format_hh_mm_ss(stat.stime - stat.qtime)
<< "\n Job is bound to physical CPU cores: ";
for (const auto &c : bound_cores) {
std::cout << c << ", ";
}
std::cout << std::endl;
}
if (cmd.is_openmpi) {
cmd.add_rankfile(bound_cores, config.get_int("nslots"));
}
if (rerun) {
const auto ps = stat.get_state(config.get_int("rerun"));
std::filesystem::current_path(ps.wd);
environ = ps.env_ptrs;
}
stat.store_state({environ, std::filesystem::current_path(), {}});
int child_stat;
int ret;
pid_t waited_on_pid;
auto handler =
tsp::Output_handler(config.get_bool("disappear_output"),
config.get_bool("separate_stderr"), stat.jobid, true);
if (0 == (waited_on_pid = fork())) {
if (cmd.is_openmpi) {
setenv("OMPI_MCA_rmaps_base_mapping_policy", "", 1);
setenv("OMPI_MCA_rmaps_rank_file_physical", "true", 1);
}
handler.init_pipes();
ret = execvp(cmd.get_argv_0(), cmd.get_argv());
if (ret != 0) {
die_with_err("Error: could not exec " + std::string(cmd.get_argv_0()),
ret);
}
}
if (waited_on_pid == -1) {
die_with_err("Error: could not fork subprocess to exec", waited_on_pid);
}
for (;;) {
pid_t ret_pid = waitpid(-1, &child_stat, 0);
if (ret_pid < 0) {
if (errno == ECHILD) {
break;
}
}
}
stat.save_output(handler.get_output());
stat.job_end(WEXITSTATUS(child_stat));
if (config.get_bool("verbose")) {
std::cout << "Job id " << extern_jobid << ": " << cmd.print()
<< "finished in " << format_hh_mm_ss(stat.etime - stat.stime)
<< " with status " << WEXITSTATUS(child_stat) << std::endl;
}
return WEXITSTATUS(child_stat);
}