This repository has been archived by the owner on Mar 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
181 lines (150 loc) · 4.3 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string>
#include <iostream>
#include <gasc.hpp>
using namespace mesos;
using std::cerr;
using std::cout;
using std::endl;
using std::string;
bool debug = true;
void usage(string program) {
cerr << "Usage: " << program << " -n <# instances> -c <# cpus> "
<< "-m <# memory> <master> -- <mpirun arguments...>"
<< endl;
cerr << endl;
cerr << "-n <# instances>\tNumber of instances / tasks (long)" << endl;
cerr << "-c <# cpus>\t\tCPU fraction per instance (float)" << endl;
cerr << "-m <# memory>\t\tMemory in megabytes per instance (long)" << endl;
cerr << "<master>\t\tThe address of the Mesos master" << endl;
}
bool expect(string found, string expect) {
if (found != expect) {
cerr << "Could not parse arguments: expected '" << expect
<< "' but found '" << found << "'" << endl;
return false;
}
return true;
}
bool parse(
int argc,
char** argv,
int& instances,
double& cpus,
long& memory,
string& master,
string& command) {
enum {
instancesFlag,
instancesInt,
cpusFlag,
cpusDouble,
memoryFlag,
memoryLong,
masterString,
split,
commandString
} state = instancesFlag;
const int expectedArguments = 10;
if (argc < expectedArguments) {
cerr << "Wrong number of arguments. "
<< "Expected " << expectedArguments << " but found "
<< argc << endl;
return false;
}
// TODO(nnielsen): Dipatch on flag rather than enforcing order.
for (int i = 1; i < argc; i++) {
string argument = argv[i];
if (state == instancesFlag) {
if (!expect(argument, "-n")) {
return false;
}
state = instancesInt;
} else if (state == instancesInt) {
instances = atoi(argument.c_str());
if (instances <= 0) {
cerr << "Number of instances must be greater than zero. "
<< "Found: '" << argument << "'" << endl;
return false;
}
state = cpusFlag;
} else if (state == cpusFlag) {
if (!expect(argument, "-c")) {
return false;
}
state = cpusDouble;
} else if (state == cpusDouble) {
cpus = atof(argument.c_str());
if (cpus <= 0.0) {
cerr << "CPU fraction must be greater than zero. "
<< "Found: '" << argument << "'" << endl;
return false;
}
state = memoryFlag;
} else if (state == memoryFlag) {
if (!expect(argument, "-m")) {
return false;
}
state = memoryLong;
} else if (state == memoryLong) {
memory = atol(argument.c_str());
if (memory <= 0) {
cerr << "Memory (MB) need to be greater than zero. "
<< "Found: '" << argument << "'" << endl;
return false;
}
state = masterString;
} else if (state == masterString) {
master = argument;
state = split;
} else if (state == split) {{
if (!expect(argument, "--")) {
return false;
}
state = commandString;
break;
}
}
}
if (state != commandString) {
return false;
}
// TODO(nnielsen): Get argument start from "--" position.
for (int i = 8; i < argc - 8; i++) {
command += string(argv[0]) + " ";
}
if (debug) {
cout << "Tool command: '" << command << "'" << endl;
}
return true;
}
int main(int argc, char** argv) {
// TODO(nnielsen): Output node list through environment variable or
// argument replacement.
// TODO(nnielsen): Enable framework fail-over!
// TODO(nnielsen): Accept zookeeper string.
// TODO(nnielsen): Enable 'exclusive' instances i.e. only run one instance per
// node.
int instances = 5;
double cpus = 1;
long memory = 128;
string master = "localhost:5050";
string command = "mpirun -f hosts.txt -n 64 ./mpi_hello";
if (!parse(argc, argv, instances, cpus, memory, master, command)) {
usage(argv[0]);
return EXIT_FAILURE;
}
FrameworkInfo framework;
framework.set_user("root");
framework.set_name("GASC: " + command);
GascScheduler scheduler(cpus, memory, instances, command);
MesosSchedulerDriver* driver = new MesosSchedulerDriver(
&scheduler, framework, master);
int status = driver->run() == DRIVER_STOPPED ? 0 : 1;
// Ensure that the driver process terminates.
driver->stop();
delete driver;
return status;
}