-
Notifications
You must be signed in to change notification settings - Fork 2
/
communication.h
367 lines (304 loc) · 8.82 KB
/
communication.h
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
communication.h
does a communication step
*/
#ifndef COMMUNICATION_H_
#define COMMUNICATION_H_
//#pragma once
#include <thrust/version.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <thrust/remove.h>
#include <thrust/reduce.h>
#include <thrust/iterator/discard_iterator.h>
#include "agent.cuh"
#include <stopwatch.h>
#include <stopwatch_win.h>
using namespace ingpu;
namespace ingpu {
// do two agents have the same top level variable (TLV)
struct has_tlv_functor : public thrust::unary_function<equation,bool>
{
int tlv_id;
__host__ __device__
has_tlv_functor(const int _tlv_id) : tlv_id(_tlv_id) {}
__host__ __device__
bool operator()(const equation& eq) const
{
//check if the lhs is a variable
if ( is_variable( get<LHS>(eq) ) ) {
if (get<LHS>(eq).id == tlv_id) {
return true;
}
else {
//check if the rhs is a variable
//Agent r = get_rhs(t);
if (is_variable(get_rhs(eq))) {
return (get_rhs(eq).id == tlv_id); //neither agent is a variable
}
}
}
return false;
}
};
// do two agents have the same top level variable (TLV)
struct find_tlv_functor : public thrust::unary_function<equation,bool>
{
int tlv_id;
__host__ __device__
find_tlv_functor(const int _tlv_id) : tlv_id(tlv_id) {}
__host__ __device__
bool operator()(const equation& eq) const
{
//check if the lhs is a variable
if (!is_agent(get<LHS>(eq))) {
if (get<LHS>(eq).id == tlv_id) {
return true;
}
else {
//check if the rhs is a variable
//Agent r = get_rhs(t);
if (!is_agent(get_rhs(eq))) {
return (get_rhs(eq).id == tlv_id); //neither agent is a variable
}
}
}
return false;
}
};
// does an equation have a TLV
struct is_tlv_equation_functor
{
__host__ __device__
bool operator()(equation t)
{
//check if the lhs is a variable
if (!is_agent(get<LHS>(t))) {
return true;
}
else {
//check if the rhs is a variable
return (!is_agent(get_rhs(t)));
}
}
};
// does an equation have a TLV as its lhs?
struct is_oriented_tlv_equation_functor
{
__host__ __device__
bool operator()(equation t)
{
//check if the lhs is a variable
return is_variable(get<LHS>(t)) && is_agent(get_rhs(t));
}
};
// dummy equation test functor
struct dummy_test_functor
{
__host__ __device__
bool operator()(const Agent t)
{
return (t.name == 'Z');
}
};
struct dummy_test_functor_tuple
{
__host__ __device__
bool operator()(const thrust::tuple<Agent, Agent>& t)
{
return (thrust::get<0>(t).name == 'Z');
}
};
struct valid_tuple_functor
{
__host__ __device__
bool operator()(const thrust::tuple<Agent, Agent>& t)
{
return (thrust::get<0>(t).name != 'Z');
}
};
// do two equations have a common tlv?
// assumes equations are oriented
struct common_tlv_functor : public thrust::binary_function<equation, equation, bool>
{
__host__ __device__
bool operator()(equation t, equation s)
{
//TODO: we assume that no two non-variable agents have distinct ids
// this may cause bugs when ids are assigned to non-variable agents more than once
// (which should not happen)
if (get<LHS>(t).id == get<LHS>(s).id)
return true;
else if (get<LHS>(t).id == get<RHS>(s).id)
return true;
else if (get<RHS>(t).id == get<LHS>(s).id)
return true;
else if (get<RHS>(t).id == get<RHS>(s).id)
return true;
return false;
}
};
struct smaller_tlv_functor : public thrust::binary_function<equation, equation, bool>
{
__host__ __device__
bool operator()(const equation& t, const equation& s)
{
//TODO: consider LHS tlvs only
return get<0>(t).id < get<0>(s).id;
}
};
struct greater_tlv_functor : public thrust::binary_function<equation, equation, bool>
{
__host__ __device__
bool operator()(equation t, equation s)
{
//TODO: consider LHS tlvs only
return get<LHS>(t).id > get<LHS>(s).id;
}
};
struct equal_tlv_functor : public thrust::binary_function<equation, equation, bool>
{
__host__ __device__
bool operator()(equation t, equation s)
{
//TODO: consider LHS tlvs only
return get<LHS>(t).id == get<LHS>(s).id;
}
};
// retuns the tlv id of the LHS
struct get_tlv_functor : public thrust::unary_function<equation, int>
{
__host__ __device__
int operator()(const equation& t)
{
//TODO: consider LHS tlvs only
return get<0>(t).id;
}
};
// retuns the tlv id of the LHS
struct get_tlv_single_functor : public thrust::unary_function<Agent, int>
{
__host__ __device__
int operator()(const Agent& t)
{
//TODO: consider LHS tlvs only
return t.id;
}
};
struct different_tlv_functor : public thrust::binary_function<equation, equation, bool>
{
__host__ __device__
bool operator()(equation t, equation s)
{
//TODO: consider LHS tlvs only
return !(get<LHS>(t).id == get<LHS>(s).id);
}
};
struct comm_functor : public thrust::binary_function<equation, equation, equation>
{
__host__ __device__
equation operator()(equation t, equation s)
{
//TODO: consider LHS tlvs only
const char c = get<RHS>(s).id;
// swap lhs/rhs if rhs is a tlv
if (c >= 'A' && c <= 'Z') {
return thrust::make_tuple<Agent, Agent>(get<RHS>(s), get<RHS>(t));
}
//TODO: more efficient to add 'else' here?
return thrust::make_tuple<Agent, Agent>(get<RHS>(t), get<RHS>(s));
}
};
// performs a single communication
// by resolving a common tlv
struct comm_reduce_functor : public thrust::binary_function<equation, equation, equation>
{
__host__ __device__
equation operator()(const equation& t, const equation& s)
{
if (get<LHS>(t).id == get<LHS>(s).id) {
//orient if necessary
//make the agent with the smaller name the LHS
//this means that if one of them is a tlv and the other an agent,
//the tlv will be on the lhs
if (get<RHS>(t).name < get<RHS>(s).name)
{
return thrust::make_tuple<Agent, Agent>(get<RHS>(s), get<RHS>(t));//thrust::swap(get<RHS>(t), get<RHS>(s));
}
return thrust::make_tuple<Agent, Agent>(get<RHS>(t), get<RHS>(s));
}
else if (get<LHS>(t).id == get<RHS>(s).id) {
//orient if necessary
if (get<RHS>(t).name < get<LHS>(s).name)
{
return thrust::make_tuple<Agent, Agent>( get<LHS>(s), get<RHS>(t));//thrust::swap(get<RHS>(t), get<LHS>(s));
}
return thrust::make_tuple<Agent, Agent>(get<RHS>(t), get<LHS>(s));
}
else if (get<RHS>(t).id == get<LHS>(s).id) {
//orient if necessary
if (get<LHS>(t).name < get<RHS>(s).name)
{
return thrust::make_tuple<Agent, Agent>(get<RHS>(s), get<LHS>(t));//thrust::swap(get<LHS>(t), get<RHS>(s));
}
return thrust::make_tuple<Agent, Agent>(get<LHS>(t), get<RHS>(s));
}
else if (get<RHS>(t).id == get<RHS>(s).id) {
//orient if necessary
if (get<LHS>(t).name < get<LHS>(s).name)
{
return thrust::make_tuple<Agent, Agent>(get<LHS>(s), get<LHS>(t));//thrust::swap(get<LHS>(t), get<LHS>(s));
}
return thrust::make_tuple<Agent, Agent>(get<LHS>(t), get<LHS>(s));
}
// at this point, something has gone wrong
// (no common tlv)
// return an equation of dummy agents
return thrust::make_tuple<Agent, Agent>(Agent(), Agent());
}
};
//clear the additional result arrays
struct clear_functor {
template <typename Tuple>
__host__ __device__
void operator()(Tuple& equations)
{
thrust::get<0>(equations).name = 'Z';
thrust::get<1>(equations).name = 'Z';
thrust::get<2>(equations).name = 'Z';
thrust::get<3>(equations).name = 'Z';
thrust::get<4>(equations).name = 'Z';
thrust::get<5>(equations).name = 'Z';
thrust::get<6>(equations) = false;
}
};
// parallel communication step using reduce_by_key
int comm_step_parallel(equation_iter& start, equation_iter& end, equation_iter& result_start, equation_iter& result_end, const int input_size, const int loop, const unsigned int communication_watch = 0)
{
int comm_counter = 1;
//equation_iter new_end;
//StopWatch::get(communication_watch).start();
thrust::device_vector<int> keys(input_size);
thrust::device_vector<int> keys_out(input_size);
thrust::transform(start, end, keys.begin(), get_tlv_functor());
//thrust::adjacent_difference(ids_begin, ids_end, diffs.begin());
//new_end = thrust::reduce_by_key(start, end, start,
// thrust::make_discard_iterator(), result_start, common_tlv_functor(), comm_reduce_functor()
// ).second;
auto new_end = thrust::reduce_by_key(keys.begin(), keys.end(), start,
keys_out.begin(), result_start, thrust::equal_to<int>(), comm_reduce_functor()
);
//StopWatch::get(communication_watch).stop();
int diff = new_end.first - keys_out.begin();
diff = input_size - diff;
//std::cout << diff << std::endl;
if (diff == 0) {
return 0;
}
result_end = new_end.second;
//end = end - diff;
return diff;
}
}
#endif