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
|
// -----------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
// -----------------------------------------------------------------------------------------------------
#include <gtest/gtest.h>
#include <atomic>
#include <chrono>
#include <numeric>
#include <random>
#include <thread>
#include <seqan3/contrib/parallel/buffer_queue.hpp>
template <typename sequential_push_t, typename sequential_pop_t>
void test_buffer_queue_wait_status()
{
size_t thread_count = std::thread::hardware_concurrency();
// limit thread count as virtualbox (used by Travis) seems to have problems with thread congestion
if (thread_count > 4)
thread_count = 4;
size_t writer_count = thread_count / 2;
if constexpr (sequential_push_t::value)
writer_count = 1;
if constexpr (sequential_pop_t::value)
thread_count = writer_count + 1;
// std::cout << "threads: " << thread_count << ‘\n‘;
// std::cout << "writers: " << writer_count << ‘\n‘;
constexpr size_t size_v = 10000;
seqan3::contrib::dynamic_buffer_queue<uint32_t> queue{100};
std::atomic<uint32_t> cnt{1};
// Define the producer section
auto produce = [&]()
{
while (true)
{
// Load atomic
uint32_t intermediate = cnt.fetch_add(1);
if (intermediate > size_v)
return;
// wait semantics
seqan3::contrib::queue_op_status status = queue.wait_push(intermediate);
if (status != seqan3::contrib::queue_op_status::success)
return;
}
};
// Define the consumer section
std::atomic<uint32_t> sum{0};
auto consume = [&]() mutable
{
uint32_t i = 0;
while (queue.wait_pop(i) != seqan3::contrib::queue_op_status::closed)
sum.fetch_add(i, std::memory_order_relaxed);
};
// Create producer pool
std::vector<std::thread> producer_pool;
for (size_t n = 0; n < writer_count; ++n)
producer_pool.emplace_back(produce);
// Create consumer pool
std::vector<std::thread> consumer_pool;
for (size_t n = 0; n < thread_count - writer_count; ++n)
consumer_pool.emplace_back(consume);
for (auto & t : producer_pool)
{
if (t.joinable())
t.join();
}
// Notify queue that no more work is going to be added.
queue.close();
for (auto & t : consumer_pool)
{
if (t.joinable())
t.join();
}
EXPECT_EQ(sum.load(), (size_v * (size_v + 1)) / 2);
}
TEST(buffer_queue, spsc_sum)
{
test_buffer_queue_wait_status<std::true_type, std::true_type>();
}
TEST(buffer_queue, spmc_sum)
{
test_buffer_queue_wait_status<std::true_type, std::false_type>();
}
TEST(buffer_queue, mpsc_sum)
{
test_buffer_queue_wait_status<std::false_type, std::true_type>();
}
TEST(buffer_queue, mpmc_sum)
{
test_buffer_queue_wait_status<std::false_type, std::false_type>();
}
template <typename sequential_push_t, typename sequential_pop_t, seqan3::contrib::buffer_queue_policy buffer_policy>
void test_buffer_queue_wait_throw(size_t initialCapacity)
{
using queue_t = seqan3::contrib::buffer_queue<size_t, std::vector<size_t>, buffer_policy>;
queue_t queue{initialCapacity};
std::vector<size_t> random;
std::mt19937 rng(0);
size_t chk_sum = 0;
random.resize(100000);
for (size_t i = 0; i < random.size(); ++i)
{
random[i] = rng();
chk_sum ^= random[i];
}
volatile std::atomic<size_t> chk_sum2 = 0;
size_t thread_count = std::thread::hardware_concurrency();
// limit thread count as virtualbox (used by Travis) seems to have problems with thread congestion
if (thread_count > 4)
thread_count = 4;
size_t writer_count = thread_count / 2;
if constexpr (sequential_push_t::value)
writer_count = 1;
if constexpr (sequential_pop_t::value)
thread_count = writer_count + 1;
// std::cout << "threads: " << thread_count << ‘\n‘;
// std::cout << "writers: " << writer_count << ‘\n‘;
ASSERT_GE(thread_count, 2u);
// std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
std::vector<std::thread> workers;
std::atomic<size_t> registered_writer = 0;
std::atomic<size_t> registered_reader = 0;
seqan3::contrib::queue_op_status push_status = seqan3::contrib::queue_op_status::success;
seqan3::contrib::queue_op_status pop_status = seqan3::contrib::queue_op_status::success;
for (size_t tid = 0; tid < thread_count; ++tid)
{
workers.push_back(std::thread([&, tid]()
{
// Become writer!
if (tid < writer_count)
{
{ // Wait until all reader are present.
seqan3::detail::spin_delay delay{};
++registered_writer;
while (registered_reader.load() < (thread_count - writer_count))
delay.wait();
}
// printf("start writer #%ld\n", tid);
size_t offset = tid * (random.size() / writer_count);
size_t offset_end = std::min(static_cast<size_t>((tid + 1) * (random.size() / writer_count)),
random.size());
for (size_t pos = offset; pos != offset_end; ++pos)
{
try
{
queue.push(random[pos]);
}
catch (seqan3::contrib::queue_op_status & ex)
{
push_status = ex;
}
}
// printf("stop writer #%ld %lu\n", tid, offset_end - offset);
// Last writer! No more values will come, so we close the queue.
++registered_writer;
if (registered_writer.load() == (2 * writer_count))
{
queue.close();
// printf("writer #%ld closed the queue\n", tid);
}
}
// Become reader!
if (tid >= writer_count)
{
{ // Wait until all writers are setup.
seqan3::detail::spin_delay delay{};
++registered_reader;
while (registered_writer.load() < writer_count)
delay.wait();
}
// printf("start reader #%lu\n", (long unsigned)tid);
size_t chk_sum_local = 0, cnt = 0;
for (;;)
{
try
{
size_t val = queue.value_pop();
chk_sum_local ^= val;
++cnt;
// if ((cnt & 0xff) == 0)
// printf("%ld ", tid);
}
catch (seqan3::contrib::queue_op_status & ex)
{
pop_status = ex;
break;
}
}
chk_sum2.fetch_xor(chk_sum_local);
// printf("stop reader #%lu %lu\n", static_cast<size_t>(tid), cnt);
}
}));
}
for (auto & t : workers)
{
if (t.joinable())
t.join();
}
// std::chrono::steady_clock::time_point stop = std::chrono::steady_clock::now();
// double time_span = std::chrono::duration_cast<std::chrono::duration<double> >(stop - start).count();
// std::cout << "throughput: " << static_cast<size_t>(random.size() / time_span) << " values/s\n";
EXPECT_EQ(chk_sum, chk_sum2);
EXPECT_TRUE(push_status == seqan3::contrib::queue_op_status::success);
EXPECT_TRUE(pop_status == seqan3::contrib::queue_op_status::closed);
}
TEST(buffer_queue, spsc_dynamicsize)
{
test_buffer_queue_wait_throw<std::true_type, std::true_type, seqan3::contrib::buffer_queue_policy::dynamic>(0u);
}
TEST(buffer_queue, spsc_fixedsize)
{
test_buffer_queue_wait_throw<std::true_type, std::true_type, seqan3::contrib::buffer_queue_policy::fixed>(30u);
}
TEST(buffer_queue, spmc_dynamicsize)
{
test_buffer_queue_wait_throw<std::true_type, std::false_type, seqan3::contrib::buffer_queue_policy::dynamic>(0u);
}
TEST(buffer_queue, spmc_fixedsize)
{
test_buffer_queue_wait_throw<std::true_type, std::false_type, seqan3::contrib::buffer_queue_policy::fixed>(30u);
}
TEST(buffer_queue, mpsc_dynamicsize)
{
test_buffer_queue_wait_throw<std::false_type, std::true_type, seqan3::contrib::buffer_queue_policy::dynamic>(0u);
}
TEST(buffer_queue, mpsc_fixedsize)
{
test_buffer_queue_wait_throw<std::false_type, std::true_type, seqan3::contrib::buffer_queue_policy::fixed>(30u);
}
TEST(buffer_queue, mpmc_dynamicsize)
{
test_buffer_queue_wait_throw<std::false_type, std::false_type, seqan3::contrib::buffer_queue_policy::dynamic>(0u);
}
TEST(buffer_queue, mpmc_fixedsize)
{
test_buffer_queue_wait_throw<std::false_type, std::false_type, seqan3::contrib::buffer_queue_policy::fixed>(30u);
}
|