-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecutor.cpp
289 lines (221 loc) · 8.87 KB
/
Executor.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// Created by Writwick Das on 21/06/2022.
//
#include <sstream>
#include <algorithm>
#include <cmath>
#include "Executor.h"
using namespace std;
// static variable to store return values that are not directly allocated into the array
static vector<int> const_result;
// executes the command in local context and sends back the result
int *Executor::execute_command(string command, int &count) {
// convert command to lowercase first
transform(command.begin(), command.end(), command.begin(),
[](unsigned char c) { return tolower(c); });
stringstream res_str;
istringstream string_stream(command);
string op, sub_op, row_str, row_end_str;
// split the command into op, sub op and row index strings
getline(string_stream, op, ' ');
getline(string_stream, sub_op, ' ');
getline(string_stream, row_str, '-');
getline(string_stream, row_end_str, ' ');
// try to parse the row value into integer
stringstream row_stream(row_str);
int row(0);
row_stream >> row;
// try to parse the row end value into integer
stringstream row_end_stream(row_end_str);
int row_end(0);
row_end_stream >> row_end;
// try to find operator in special operator map keys
auto sp_op_element = Executor::special_op_map.find(op);
if (sp_op_element != Executor::special_op_map.end()) {
// it's a valid special operator so return its result directly
return sp_op_element->second(this, count);
}
// try to find operator in operator map keys and range operator map keys
auto op_element = op_map.find(op);
auto range_op_element = range_op_map.find(op);
if (op_element == op_map.end() && range_op_element == range_op_map.end()) {
// operator is not valid
res_str << "error: operator \"" << op << "\" is invalid.";
cout << "rank " << this->rank << " >> " << res_str.str() << endl;
const_result = vector<int>{-1};
count = const_result.size();
return const_result.data();
}
// we disregard if the range parse was successful or not so that
// we don't call a normal function when there is an invalid row end value
// and this is another way to check if there was any value passed as a
// possible row end value
if (row_end_str.empty()) {
// not a range operator call
const auto sub_op_map = op_element->second;
auto sub_op_element = sub_op_map.find(sub_op);
if (sub_op_element == sub_op_map.end()) {
// sub-operator is not valid for operator
res_str << "error: sub operator \"" << sub_op << "\" is invalid for operator \"" << op << "\".";
cout << "rank " << this->rank << " >> " << res_str.str() << endl;
const_result = vector<int>{-1};
count = const_result.size();
return const_result.data();
}
const auto op_func = sub_op_element->second;
if (row_stream.fail()) {
res_str << "error: failed to parse row index.";
cout << "rank " << this->rank << " >> " << res_str.str() << endl;
const_result = vector<int>{-1};
count = const_result.size();
return const_result.data();
}
// call the op_func to execute the command
return op_func(this, row, count);
} else {
const auto range_sub_op_map = range_op_element->second;
auto range_sub_op_element = range_sub_op_map.find(sub_op);
if (range_sub_op_element == range_sub_op_map.end()) {
// sub-operator is not valid for operator
res_str << "error: sub operator \"" << sub_op << "\" is invalid for range operator \"" << op << "\".";
cout << "rank " << this->rank << " >> " << res_str.str() << endl;
const_result = vector<int>{-1};
count = const_result.size();
return const_result.data();
}
const auto range_op_func = range_sub_op_element->second;
if (row_stream.fail()) {
res_str << "error: failed to parse row index.";
cout << "rank " << this->rank << " >> " << res_str.str() << endl;
const_result = vector<int>{-1};
count = const_result.size();
return const_result.data();
}
if (row_end_stream.fail()) {
res_str << "error: failed to parse row end index.";
cout << "rank " << this->rank << " >> " << res_str.str() << endl;
const_result = vector<int>{-1};
count = const_result.size();
return const_result.data();
}
// call the range_op_func to execute the command
return range_op_func(this, row, row_end, count);
}
}
// parses the command and returns the parsing result
// returns a list of sub commands to send to other ranks in sub_command_map
P_RESULT Executor::parse_command(const string &command, map<int, pair<int, int>> &sub_command_map,
string &command_prefix) const {
istringstream string_stream(command);
stringstream prefix_stream;
string op, sub_op, row_str, row_end_str;
// split the command into op, sub op and row index strings
getline(string_stream, op, ' ');
getline(string_stream, sub_op, ' ');
getline(string_stream, row_str, '-');
getline(string_stream, row_end_str, ' ');
prefix_stream << op << " " << sub_op << " ";
command_prefix = prefix_stream.str();
if (row_str.substr(0, 3) == "all") {
row_str = "0";
row_end_str = to_string(this->N);
}
// try to parse the row value into integer
stringstream row_stream(row_str);
int row(-3);
row_stream >> row;
// try to parse the row end value into integer
stringstream row_end_stream(row_end_str);
int row_end(-3);
row_end_stream >> row_end;
if (row_stream.fail()) {
// if the operator is empty, ignore
if (op.empty()) {
return EMPTY_OP;
}
// operator is available in special operator map
// it should be sent to all ranks
if (Executor::special_op_map.find(op) != Executor::special_op_map.end()) {
return SPECIAL_OPERATOR;
}
// otherwise just show error message
return ERROR_OPERATOR;
}
if (row > this->N || row < 0) {
// row out of range
sub_command_map.clear();
sub_command_map.insert({-1, {row / this->N1, row}});
}
if (row_end_str.length() > 0) {
// it is a range command
if (row_end_stream.fail()) {
// failed to parse row end
return ERROR_OPERATOR;
}
if (row_end > this->N || row_end < 0) {
// end row out of range
sub_command_map.clear();
sub_command_map.insert({-1, {row / this->N1, row}});
sub_command_map.insert({-2, {row_end / this->N1, row_end}});
}
}
if (!sub_command_map.empty()) {
return ROW_OUT_OF_RANGE;
}
if (row_end_str.length() > 0) {
if (row > row_end) {
// end_row is non-empty so the range is wrong
sub_command_map.clear();
sub_command_map.insert({-1, {row, row_end}});
return NEGATIVE_ROW_RANGE;
}
sub_command_map.clear();
for (int rnk = row / this->N1;
row_end % this->N1 == 0 ? rnk < row_end / this->N1 : rnk <= row_end / this->N1; rnk++) {
int rnk_row_start = max(0, row - rnk * this->N1), rnk_row_end = min(row_end - rnk * this->N1, this->N1);
sub_command_map.insert({rnk, {rnk_row_start, rnk_row_end}});
}
} else {
int rnk = row / this->N1;
int rnk_row = max(0, row - rnk * this->N1);
sub_command_map.insert({rnk, {rnk_row, -1}});
}
return SUCCESS;
}
int *Executor::get_row(Executor *executor, int row, int &count) {
stringstream res_str;
count = executor->array_part[row].size();
return executor->array_part[row].data();
}
int *Executor::get_aggr_range(Executor *executor, int row_start, int row_end, int &count) {
stringstream res_str;
int aggr = 0;
for (int row = row_start; row < row_end; ++row) {
for (const auto &r: executor->array_part[row]) {
aggr += r;
}
}
const_result = vector<int>{aggr};
count = const_result.size();
return const_result.data();
}
int *Executor::get_aggr(Executor *executor, int row, int &count) {
stringstream res_str;
int aggr = 0;
for (const auto &r: executor->array_part[row]) {
aggr += r;
}
const_result = vector<int>{aggr};
count = const_result.size();
return const_result.data();
}
int *Executor::exit(Executor *executor, int &count) {
count = 1;
cout << "rank " << executor->rank << " >> exited" << endl;
const_result = vector<int>{-1};
count = const_result.size();
return const_result.data();
}
map<string, int *(*)(Executor *, int &)> Executor::special_op_map = {
{"exit", exit}
};