channel_test.cc 9.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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. */

#include "paddle/framework/channel.h"

C
chengduo 已提交
17 18 19
#include <chrono>
#include <thread>

20 21
#include "gtest/gtest.h"

C
chengduo 已提交
22 23 24 25
using paddle::framework::Channel;
using paddle::framework::MakeChannel;
using paddle::framework::CloseChannel;

26
TEST(Channel, MakeAndClose) {
C
chengduo 已提交
27 28 29 30 31
  using paddle::framework::details::Buffered;
  using paddle::framework::details::UnBuffered;
  {
    // MakeChannel should return a buffered channel is buffer_size > 0.
    auto ch = MakeChannel<int>(10);
32 33
    EXPECT_NE(dynamic_cast<Buffered<int> *>(ch), nullptr);
    EXPECT_EQ(dynamic_cast<UnBuffered<int> *>(ch), nullptr);
C
chengduo 已提交
34 35 36 37 38 39
    CloseChannel(ch);
    delete ch;
  }
  {
    // MakeChannel should return an un-buffered channel is buffer_size = 0.
    auto ch = MakeChannel<int>(0);
40 41
    EXPECT_EQ(dynamic_cast<Buffered<int> *>(ch), nullptr);
    EXPECT_NE(dynamic_cast<UnBuffered<int> *>(ch), nullptr);
C
chengduo 已提交
42 43 44 45 46 47 48 49 50
    CloseChannel(ch);
    delete ch;
  }
}

TEST(Channel, SufficientBufferSizeDoesntBlock) {
  const size_t buffer_size = 10;
  auto ch = MakeChannel<size_t>(buffer_size);
  for (size_t i = 0; i < buffer_size; ++i) {
51
    EXPECT_EQ(ch->Send(&i), true);  // should not block
C
chengduo 已提交
52 53 54 55
  }

  size_t out;
  for (size_t i = 0; i < buffer_size; ++i) {
56
    EXPECT_EQ(ch->Receive(&out), true);  // should not block
C
chengduo 已提交
57 58 59 60 61 62
    EXPECT_EQ(out, i);
  }
  CloseChannel(ch);
  delete ch;
}

63 64 65 66 67 68 69 70 71 72
TEST(Channel, SendOnClosedChannelPanics) {
  const size_t buffer_size = 10;
  auto ch = MakeChannel<size_t>(buffer_size);
  size_t i = 5;
  EXPECT_EQ(ch->Send(&i), true);  // should not block or panic
  CloseChannel(ch);
  EXPECT_EQ(ch->Send(&i), false);  // should panic
  delete ch;
}

Y
Yi Wang 已提交
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
TEST(Channel, ReceiveFromBufferedChannelReturnResidualValuesTest) {
  const size_t buffer_size = 10;
  auto ch = MakeChannel<size_t>(buffer_size);

  for (size_t i = 0; i < buffer_size; ++i) {
    EXPECT_EQ(ch->Send(&i), true);  // sending should not block
  }

  size_t out;
  for (size_t i = 0; i < buffer_size / 2; ++i) {
    EXPECT_EQ(ch->Receive(&out), true);  // receiving should not block
    EXPECT_EQ(out, i);
  }

  CloseChannel(ch);

  for (size_t i = buffer_size / 2; i < buffer_size; ++i) {
    EXPECT_EQ(ch->Receive(&out),
              true);  // receving should return residual values.
    EXPECT_EQ(out, i);
  }

  for (size_t i = 0; i < buffer_size; ++i) {
    EXPECT_EQ(ch->Receive(&out),
              false);  // after receiving residual values, return zeros.
    // Note: we cannot check EXPECT_EQ(out, 0), because C++ doesn't
    // define zero values like Go does.
  }
  delete ch;
}

C
chengduo 已提交
104 105 106 107 108 109 110
TEST(Channel, ConcurrentSendNonConcurrentReceiveWithSufficientBufferSize) {
  const size_t buffer_size = 10;
  auto ch = MakeChannel<size_t>(buffer_size);
  size_t sum = 0;
  std::thread t([&]() {
    // Try to write more than buffer size.
    for (size_t i = 0; i < 2 * buffer_size; ++i) {
111 112 113 114
      if (i < buffer_size)
        EXPECT_EQ(ch->Send(&i), true);  // should block after 10 iterations
      else
        EXPECT_EQ(ch->Send(&i), false);
C
chengduo 已提交
115 116 117
      sum += i;
    }
  });
C
chengduoZH 已提交
118
  std::this_thread::sleep_for(std::chrono::milliseconds(100));  // wait 0.1 sec
C
chengduo 已提交
119
  EXPECT_EQ(sum, 45U);
120 121

  CloseChannel(ch);
C
chengduo 已提交
122 123
  t.join();
  delete ch;
124
}
125 126 127 128 129 130

TEST(Channel, SimpleUnbufferedChannelTest) {
  auto ch = MakeChannel<int>(0);
  unsigned sum_send = 0;
  std::thread t([&]() {
    for (int i = 0; i < 5; i++) {
131
      EXPECT_EQ(ch->Send(&i), true);
132 133 134 135 136
      sum_send += i;
    }
  });
  for (int i = 0; i < 5; i++) {
    int recv;
137
    EXPECT_EQ(ch->Receive(&recv), true);
138 139 140 141 142 143 144 145
    EXPECT_EQ(recv, i);
  }

  CloseChannel(ch);
  t.join();
  EXPECT_EQ(sum_send, 10U);
  delete ch;
}
146

C
chengduoZH 已提交
147
void ChannelCloseUnblocksReceiversTest(Channel<int> *ch) {
148 149 150 151
  size_t num_threads = 5;
  std::thread t[num_threads];
  bool thread_ended[num_threads];

152
  // Launches threads that try to read and are blocked because of no writers
153 154 155 156 157 158 159 160 161 162
  for (size_t i = 0; i < num_threads; i++) {
    thread_ended[i] = false;
    t[i] = std::thread(
        [&](bool *p) {
          int data;
          EXPECT_EQ(ch->Receive(&data), false);
          *p = true;
        },
        &thread_ended[i]);
  }
C
chengduoZH 已提交
163
  std::this_thread::sleep_for(std::chrono::milliseconds(100));  // wait 0.1 sec
164

C
chengduoZH 已提交
165
  // Verify that all the threads are blocked
166 167 168 169
  for (size_t i = 0; i < num_threads; i++) {
    EXPECT_EQ(thread_ended[i], false);
  }

170
  // Explicitly close the channel
171 172 173
  // This should unblock all receivers
  CloseChannel(ch);

C
chengduoZH 已提交
174
  std::this_thread::sleep_for(std::chrono::milliseconds(100));  // wait 0.1 sec
175 176 177 178 179 180 181 182 183

  // Verify that all threads got unblocked
  for (size_t i = 0; i < num_threads; i++) {
    EXPECT_EQ(thread_ended[i], true);
  }

  for (size_t i = 0; i < num_threads; i++) t[i].join();
}

C
chengduoZH 已提交
184 185 186 187
void ChannelCloseUnblocksSendersTest(Channel<int> *ch) {
  using paddle::framework::details::Buffered;
  using paddle::framework::details::UnBuffered;

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  size_t num_threads = 5;
  std::thread t[num_threads];
  bool thread_ended[num_threads];
  bool send_success[num_threads];

  // Launches threads that try to write and are blocked because of no readers
  for (size_t i = 0; i < num_threads; i++) {
    thread_ended[i] = false;
    send_success[i] = false;
    t[i] = std::thread(
        [&](bool *ended, bool *success) {
          int data = 10;
          *success = ch->Send(&data);
          *ended = true;
        },
        &thread_ended[i], &send_success[i]);
  }
  std::this_thread::sleep_for(std::chrono::milliseconds(100));  // wait

C
chengduoZH 已提交
207 208 209 210 211 212 213 214 215 216 217 218
  if (dynamic_cast<Buffered<int> *>(ch)) {
    // If ch is Buffered, atleast 4 threads must be blocked.
    int ct = 0;
    for (size_t i = 0; i < num_threads; i++) {
      if (!thread_ended[i]) ct++;
    }
    EXPECT_GE(ct, 4);
  } else {
    // If ch is UnBuffered, all the threads should be blocked.
    for (size_t i = 0; i < num_threads; i++) {
      EXPECT_EQ(thread_ended[i], false);
    }
219 220 221 222 223
  }
  // Explicitly close the thread
  // This should unblock all senders
  CloseChannel(ch);

C
chengduoZH 已提交
224
  std::this_thread::sleep_for(std::chrono::milliseconds(100));  // wait
225 226 227 228 229 230

  // Verify that all threads got unblocked
  for (size_t i = 0; i < num_threads; i++) {
    EXPECT_EQ(thread_ended[i], true);
  }

C
chengduoZH 已提交
231 232 233 234 235 236 237 238
  if (dynamic_cast<Buffered<int> *>(ch)) {
    // Verify that only 1 send was successful
    int ct = 0;
    for (size_t i = 0; i < num_threads; i++) {
      if (send_success[i]) ct++;
    }
    // Only 1 send must be successful
    EXPECT_EQ(ct, 1);
239 240 241
  }

  for (size_t i = 0; i < num_threads; i++) t[i].join();
C
chengduoZH 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
}

// This tests that closing a buffered channel also unblocks
//  any receivers waiting on the channel
TEST(Channel, BufferedChannelCloseUnblocksReceiversTest) {
  auto ch = MakeChannel<int>(1);
  ChannelCloseUnblocksReceiversTest(ch);
  delete ch;
}

// This tests that closing a buffered channel also unblocks
//  any senders waiting for channel to have write space
TEST(Channel, BufferedChannelCloseUnblocksSendersTest) {
  auto ch = MakeChannel<int>(1);
  ChannelCloseUnblocksSendersTest(ch);
257 258 259
  delete ch;
}

260 261 262 263
// This tests that closing an unbuffered channel also unblocks
//  unblocks any receivers waiting for senders
TEST(Channel, UnbufferedChannelCloseUnblocksReceiversTest) {
  auto ch = MakeChannel<int>(0);
C
chengduoZH 已提交
264
  ChannelCloseUnblocksReceiversTest(ch);
265 266 267 268 269 270 271
  delete ch;
}

// This tests that closing an unbuffered channel also unblocks
//  unblocks any senders waiting for senders
TEST(Channel, UnbufferedChannelCloseUnblocksSendersTest) {
  auto ch = MakeChannel<int>(0);
C
chengduoZH 已提交
272
  ChannelCloseUnblocksReceiversTest(ch);
273 274 275
  delete ch;
}

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
TEST(Channel, UnbufferedLessReceiveMoreSendTest) {
  auto ch = MakeChannel<int>(0);
  unsigned sum_send = 0;
  // Send should block after three iterations
  // since we only have three receivers.
  std::thread t([&]() {
    // Try to send more number of times
    // than receivers
    for (int i = 0; i < 4; i++) {
      ch->Send(&i);
      sum_send += i;
    }
  });
  for (int i = 0; i < 3; i++) {
    int recv;
    ch->Receive(&recv);
    EXPECT_EQ(recv, i);
  }
  std::this_thread::sleep_for(std::chrono::milliseconds(100));  // wait 0.5 sec
  EXPECT_EQ(sum_send, 3U);

  CloseChannel(ch);
  t.join();
  delete ch;
}
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

TEST(Channel, UnbufferedMoreReceiveLessSendTest) {
  auto ch = MakeChannel<int>(0);
  unsigned sum_send = 0;
  unsigned sum_receive = 0;
  // The receiver should block after 5
  // iterations, since there are only 5 senders.
  std::thread t([&]() {
    for (int i = 0; i < 8; i++) {
      int recv;
      ch->Receive(&recv);  // should block after the fifth iteration.
      EXPECT_EQ(recv, i);
      sum_receive += i;
    }
  });
  for (int i = 0; i < 5; i++) {
    ch->Send(&i);
    sum_send += i;
  }
  std::this_thread::sleep_for(std::chrono::milliseconds(500));  // wait 0.5 sec
  EXPECT_EQ(sum_send, 10U);
  EXPECT_EQ(sum_receive, 10U);
  // send three more elements
  for (int i = 5; i < 8; i++) {
    ch->Send(&i);
    sum_send += i;
  }

  CloseChannel(ch);
  t.join();
  EXPECT_EQ(sum_send, 28U);
  EXPECT_EQ(sum_receive, 28U);
  delete ch;
}