forked from jegonzal/PowerGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_test.cxx
295 lines (243 loc) · 8.72 KB
/
scheduler_test.cxx
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
/**
* Copyright (c) 2009 Carnegie Mellon University.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* For more about this software visit:
*
* http://www.graphlab.ml.cmu.edu
*
*/
#include <fstream>
#include <vector>
#include <graphlab/scheduler/scheduler_includes.hpp>
#include <graphlab/rpc/async_consensus.hpp>
#include <graphlab/parallel/atomic.hpp>
#include <cxxtest/TestSuite.h>
using namespace graphlab;
distributed_control dc;
struct message_type {
int value;
double pr;
explicit message_type(int value = 0, double pr = 0):value(value),pr(pr) { }
double priority() const {
return pr;
}
message_type& operator+=(const message_type& other) {
value += other.value;
pr = other.value;
return *this;
}
};
const size_t NCPUS = 4;
const size_t NUM_VERTICES = 101;
std::vector<atomic<int> > correctness_counter;
template <typename SchedulerType>
void test_scheduler_basic_functionality_single_threaded() {
graphlab_options opts;
opts.set_ncpus(NCPUS);
SchedulerType sched(NUM_VERTICES, opts);
const size_t target_value = 100;
// inject a sequence of messages which will sum to 100 per vertex
for (size_t c = 0;c < target_value; ++c) {
for (size_t i = 0; i < NUM_VERTICES; ++i) {
sched.schedule(i, message_type(1, 1.0));
}
}
correctness_counter.clear();
correctness_counter.resize(NUM_VERTICES, atomic<int>(0));
sched.start();
// pull stuff out
bool allcpus_done = false;
while(!allcpus_done) {
allcpus_done = true;
for (size_t i = 0; i < NCPUS; ++i) {
vertex_id_type v; message_type m;
sched_status::status_enum ret = sched.get_next(i, v, m);
if (ret == sched_status::NEW_TASK) {
allcpus_done = false;
correctness_counter[v].inc(m.value);
}
}
}
// check the counters
for(size_t i = 0; i < NUM_VERTICES; ++i) {
TS_ASSERT_EQUALS(correctness_counter[i].value, (int)target_value);
}
}
template <typename SchedulerType>
void test_basic_functionality_thread(SchedulerType& sched,
async_consensus& consensus,
size_t schedule_count,
size_t threadid) {
size_t c = 0;
vertex_id_type v; message_type m;
while(1) {
// process as many tasks as I can
while(1) {
sched_status::status_enum ret = sched.get_next(threadid, v, m);
if (ret == sched_status::NEW_TASK) {
correctness_counter[v].inc(m.value);
}
else {
break;
}
}
// schedule 1 cycle. If I schedule stuff I go back to processing tasks
if (c < schedule_count) {
for (size_t i = 0; i < NUM_VERTICES; ++i) {
sched.schedule(i, message_type(1, 1.0));
consensus.cancel();
}
++c;
continue;
}
// nothing to schedule, nothing to run. try to quit
consensus.begin_done_critical_section(threadid);
sched_status::status_enum ret = sched.get_next(threadid, v, m);
if (ret == sched_status::NEW_TASK) {
// there is task. cancel, process it, and look back
consensus.cancel_critical_section(threadid);
correctness_counter[v].inc(m.value);
}
else {
// no more tasks try to finish up
bool ret = consensus.end_done_critical_section(threadid);
if (ret) break;
}
}
}
template <typename SchedulerType>
void test_scheduler_basic_functionality_parallel() {
graphlab_options opts;
opts.set_ncpus(NCPUS);
SchedulerType sched(NUM_VERTICES, opts);
async_consensus consensus(dc, NCPUS);
const size_t schedule_count = 10000;
const size_t target_value = schedule_count * NCPUS + 1;
correctness_counter.clear();
correctness_counter.resize(NUM_VERTICES, atomic<int>(0));
// inject a sequence of messages which will sum to 100 per vertex
for (size_t i = 0; i < NUM_VERTICES; ++i) {
sched.schedule(i, message_type(1, 1.0));
}
sched.start();
thread_group group;
for (size_t i = 0;i < NCPUS;++i) {
group.launch(boost::bind(test_basic_functionality_thread<SchedulerType>,
boost::ref(sched), boost::ref(consensus), schedule_count, i));
}
group.join();
// check the counters
for(size_t i = 0; i < NUM_VERTICES; ++i) {
TS_ASSERT_EQUALS(correctness_counter[i].value, (int)target_value);
}
}
/*
* Like test_basic_functionality_thread, but only increments the
* correctness_counter by 1, and checks to make sure the priority is at least
* 100.0
*/
template <typename SchedulerType>
void test_scheduler_min_priority_thread(SchedulerType& sched,
async_consensus& consensus,
size_t schedule_count,
size_t threadid) {
size_t c = 0;
vertex_id_type v; message_type m;
while(1) {
// process as many tasks as I can
while(1) {
sched_status::status_enum ret = sched.get_next(threadid, v, m);
if (ret == sched_status::NEW_TASK) {
TS_ASSERT_LESS_THAN_EQUALS(100.0, m.priority());
correctness_counter[v].inc(1);
}
else {
break;
}
}
// schedule 1 cycle. If I schedule stuff I go back to processing tasks
if (c < schedule_count) {
for (size_t i = 0; i < NUM_VERTICES; ++i) {
sched.schedule(i, message_type(1, 1.0));
consensus.cancel();
}
++c;
continue;
}
// nothing to schedule, nothing to run. try to quit
consensus.begin_done_critical_section(threadid);
sched_status::status_enum ret = sched.get_next(threadid, v, m);
if (ret == sched_status::NEW_TASK) {
TS_ASSERT_LESS_THAN_EQUALS(100.0, m.priority());
// there is task. cancel, process it, and look back
consensus.cancel_critical_section(threadid);
correctness_counter[v].inc(1);
}
else {
// no more tasks try to finish up
bool ret = consensus.end_done_critical_section(threadid);
if (ret) break;
}
}
}
template <typename SchedulerType>
void test_scheduler_min_priority_parallel() {
graphlab_options opts;
opts.set_ncpus(NCPUS);
opts.get_scheduler_args().set_option("min_priority", 100.0);
SchedulerType sched(NUM_VERTICES, opts);
async_consensus consensus(dc, NCPUS);
const size_t schedule_count = 10000;
const size_t maximum_value = (size_t)((schedule_count * NCPUS + 101.0) / 100.0);
correctness_counter.clear();
correctness_counter.resize(NUM_VERTICES, atomic<int>(0));
// inject a sequence of messages which will sum to 100 per vertex
for (size_t i = 0; i < NUM_VERTICES; ++i) {
sched.schedule(i, message_type(1, 101.0));
}
sched.start();
thread_group group;
for (size_t i = 0;i < NCPUS;++i) {
group.launch(boost::bind(test_scheduler_min_priority_thread<SchedulerType>,
boost::ref(sched), boost::ref(consensus), schedule_count, i));
}
group.join();
// check the counters
for(size_t i = 0; i < NUM_VERTICES; ++i) {
TS_ASSERT_LESS_THAN_EQUALS(correctness_counter[i].value, (int)maximum_value);
}
}
class SerializeTestSuite : public CxxTest::TestSuite {
public:
void test_scheduler_basic_single_threaded() {
test_scheduler_basic_functionality_single_threaded<sweep_scheduler<message_type> >();
test_scheduler_basic_functionality_single_threaded<fifo_scheduler<message_type> >();
test_scheduler_basic_functionality_single_threaded<priority_scheduler<message_type> >();
test_scheduler_basic_functionality_single_threaded<queued_fifo_scheduler<message_type> >();
}
void test_scheduler_basic_parallel() {
test_scheduler_basic_functionality_parallel<sweep_scheduler<message_type> >();
test_scheduler_basic_functionality_parallel<fifo_scheduler<message_type> >();
test_scheduler_basic_functionality_parallel<priority_scheduler<message_type> >();
test_scheduler_basic_functionality_parallel<queued_fifo_scheduler<message_type> >();
}
void test_scheduler_min_priority() {
test_scheduler_min_priority_parallel<sweep_scheduler<message_type> >();
test_scheduler_min_priority_parallel<fifo_scheduler<message_type> >();
test_scheduler_min_priority_parallel<priority_scheduler<message_type> >();
test_scheduler_min_priority_parallel<queued_fifo_scheduler<message_type> >();
}
};