This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch.cpp
171 lines (133 loc) · 3.81 KB
/
launch.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <csignal>
#include <boost/mpi.hpp>
#include <boost/mpi/timer.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/program_options.hpp>
#include "launch.h"
using namespace std;
namespace mpi = boost::mpi;
namespace po = boost::program_options;
static volatile sig_atomic_t interrupt_exec = 0;
static void
interrupt(int signum)
{
if (signum == SIGUSR1) {
interrupt_exec = 1;
}
}
static void
catch_signals()
{
struct sigaction action;
action.sa_handler = interrupt;
action.sa_flags = 0;
sigemptyset(&action.sa_mask);
sigaction(SIGUSR1, &action, NULL);
}
std::string
launch::parse_launch_options(int argc, char **argv)
{
string taskfile;
po::options_description options("Options");
options.add_options()
("help,h", "show this help message");
po::options_description args("Arguments");
args.add_options()
("taskfile", po::value<string>(&taskfile), "tasks file");
po::options_description desc;
desc.add(args).add(options);
po::positional_options_description p;
p.add("taskfile", 1);
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv)
.options(desc)
.positional(p).run(), vm);
if (vm.count("help") or !vm.count("taskfile")) {
cout << "Usage: dextr [options] launch taskfile" << "\n";
cout << "Arguments:" << "\n";
cout << " taskfile file containing tasks to launch" << "\n";
cout << options;
exit(EXIT_FAILURE);
}
po::notify(vm);
} catch (const po::error &e) {
cerr << e.what() << "\n";
exit(EXIT_FAILURE);
}
if (vm.count("taskfile")) {
taskfile = vm["taskfile"].as<string>();
}
return taskfile;
}
std::vector<std::string>
launch::read_tasks(std::string taskfile)
{
std::ifstream in(taskfile.c_str());
if (!in) {
cerr << "Cannot open " << taskfile << "\n";
exit(EXIT_FAILURE);
}
vector<string> tasks;
std::string line;
while (std::getline(in, line)) {
if (line.size() > 0)
tasks.push_back(line);
}
in.close();
return tasks;
}
void
launch::exec_task(std::string task, int rank)
{
mpi::timer timer;
int status = system(task.c_str());
double elapsed_time = timer.elapsed();
if (WIFEXITED(status)) {
status = WEXITSTATUS(status);
cout << "# [dextr]"
<< " executed by process " << rank
<< " in " << elapsed_time << "s"
<< " with status " << status
<< ": " << task << "\n";
} else {
if (WIFSIGNALED(status)) {
status = WTERMSIG(status);
cout << "# [dextr]"
<< " executed by process " << rank
<< " in " << elapsed_time << "s"
<< " killed by signal " << status
<< ": " << task << "\n";
}
}
}
void
launch::run_cmd(std::string taskfile)
{
catch_signals();
mpi::environment env;
mpi::communicator world;
vector<vector<string>> chunks(world.size(), vector<string>(0));
if (world.rank() == 0) {
vector<string> tasks = read_tasks(taskfile);
for (int i = 0; i < tasks.size(); ++i) {
chunks.at(i % world.size()).push_back(tasks[i]);
}
}
vector<string> received_tasks;
mpi::scatter(world, chunks, received_tasks, 0);
for (auto &task : received_tasks) {
if (interrupt_exec) {
cout << "# [dextr]"
<< " signal received by process " << world.rank()
<< ": execution interrupted" << "\n";
return;
} else {
exec_task(task, world.rank());
}
}
}