This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_worker.cc
166 lines (144 loc) · 5.44 KB
/
test_worker.cc
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
#include <array>
#include <chrono>
#include <thread>
#include <absl/synchronization/notification.h>
#include <catch2/catch.hpp>
#include "controller.h"
#include "worker_impl.h"
TEST_CASE("worker") {
auto controller = elf::create_controller();
cudaSetDevice(0);
elf::Worker w(controller.get());
auto allreduce_op = w.add_global_variable("var1");
auto broadcast_op = w.add_weight_variable("var1");
w.commit_and_join();
bool should_continue;
bool needs_broadcast;
int64_t shard_number;
std::tie(should_continue, needs_broadcast, shard_number) = w.begin_batch();
CHECK(should_continue);
REQUIRE_FALSE(needs_broadcast);
auto H = std::array<float, 4>{140, 114, 91, 178};
gpu_array<float, 4> Dsrc, Ddst;
Dsrc = H;
SECTION("allreduce") {
absl::Notification done;
allreduce_op->execute_async(
Dsrc.data(), Ddst.data(), 4, elf::Communicator::f32, [&done]() { done.Notify(); });
done.WaitForNotification();
CHECK(Dsrc.cpu() == H);
CHECK(Ddst.cpu() == H);
}
H = {10, 8, 36, 254};
Dsrc = H;
SECTION("broadcast") {
absl::Notification done;
broadcast_op->execute_async(
Dsrc.data(), Ddst.data(), 4, elf::Communicator::f32, [&done]() { done.Notify(); });
done.WaitForNotification();
CHECK(Dsrc.cpu() == H);
CHECK(Ddst.cpu() == H);
}
}
TEST_CASE("2 workers", "[!hide]") {
// this test is buggy
absl::Mutex mu;
auto controller = elf::create_controller();
absl::Notification worker1_joined;
absl::Notification test_done;
cudaSetDevice(0);
elf::Worker w1(controller.get());
std::thread t1([&]() { // worker 1
auto broadcast_op = w1.add_global_variable("var1");
auto allreduce_op = w1.add_weight_variable("var1");
w1.commit_and_join();
bool should_continue, requires_broadcast;
int64_t shard_number;
std::tie(should_continue, requires_broadcast, shard_number) = w1.begin_batch();
CHECK(should_continue);
CHECK_FALSE(requires_broadcast);
worker1_joined.Notify();
while (true) {
std::tie(should_continue, requires_broadcast, shard_number) = w1.begin_batch();
{ // broadcast
auto H = std::array<float, 4>{26, 83, 62, 30};
gpu_array<float, 4> Dsrc, Ddst;
Dsrc = H;
absl::Notification done;
broadcast_op->execute_async(Dsrc.data(), Ddst.data(), 4, elf::Communicator::f32,
[&done]() { done.Notify(); });
done.WaitForNotification();
{
absl::MutexLock l(&mu);
CHECK(Dsrc.cpu() == H);
}
}
{ // allreduce
auto H = std::array<float, 4>{61, 48, 81, 76};
gpu_array<float, 4> Dsrc, Ddst;
Dsrc = H;
absl::Notification done;
allreduce_op->execute_async(Dsrc.data(), Ddst.data(), 4, elf::Communicator::f32,
[&done]() { done.Notify(); });
done.WaitForNotification();
{
absl::MutexLock l(&mu);
CHECK(Dsrc.cpu() == H);
}
}
// let the controller take a breath
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
});
{ // worker 2
worker1_joined.WaitForNotification();
cudaSetDevice(1);
elf::Worker w2(controller.get());
auto broadcast_op = w2.add_global_variable("var1");
auto allreduce_op = w2.add_weight_variable("var1");
w2.commit_and_join();
while (true) {
bool should_continue, requires_broadcast;
int64_t shard_number;
std::tie(should_continue, requires_broadcast, shard_number) = w2.begin_batch();
if (!should_continue) {
break;
}
{ // broadcast
auto H = std::array<float, 4>{9700, 800, 4900, 7202};
gpu_array<float, 4> Dsrc, Ddst;
Dsrc = H;
absl::Notification done;
broadcast_op->execute_async(Dsrc.data(), Ddst.data(), 4, elf::Communicator::f32,
[&done]() { done.Notify(); });
done.WaitForNotification();
{
absl::MutexLock l(&mu);
CHECK(Dsrc.cpu() == H);
CHECK(Ddst.cpu() == std::array<float, 4>{26, 83, 62, 30});
}
}
{ // allreduce
auto H = std::array<float, 4>{100, 200, 300, 400};
gpu_array<float, 4> Dsrc, Ddst;
Dsrc = H;
absl::Notification done;
allreduce_op->execute_async(Dsrc.data(), Ddst.data(), 4, elf::Communicator::f32,
[&done]() { done.Notify(); });
done.WaitForNotification();
{
absl::MutexLock l(&mu);
CHECK(Dsrc.cpu() == H);
CHECK(Ddst.cpu() == std::array<float, 4>{161, 248, 381, 476});
}
}
// let the controller take a breath
std::this_thread::sleep_for(std::chrono::milliseconds(50));
w2.leave();
}
std::cerr << "worker2 test complete\n";
}
test_done.Notify();
std::cerr << "test_done signalled\n";
t1.join();
}