]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/performance/fiber/skynet_shared_join.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / fiber / performance / fiber / skynet_shared_join.cpp
1
2 // Copyright Oliver Kowalke 2015.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 // based on https://github.com/atemerev/skynet from Alexander Temerev
8
9 #include <algorithm>
10 #include <cassert>
11 #include <chrono>
12 #include <cmath>
13 #include <condition_variable>
14 #include <cstddef>
15 #include <cstdint>
16 #include <cstdlib>
17 #include <deque>
18 #include <iostream>
19 #include <memory>
20 #include <mutex>
21 #include <numeric>
22 #include <vector>
23
24 #include <boost/fiber/all.hpp>
25 #include <boost/predef.h>
26
27 #include "barrier.hpp"
28
29 using allocator_type = boost::fibers::fixedsize_stack;
30 using channel_type = boost::fibers::buffered_channel< std::uint64_t >;
31 using clock_type = std::chrono::steady_clock;
32 using duration_type = clock_type::duration;
33 using lock_type = std::unique_lock< std::mutex >;
34 using time_point_type = clock_type::time_point;
35
36 static bool done = false;
37 static std::mutex mtx{};
38 static boost::fibers::condition_variable_any cnd{};
39
40 // microbenchmark
41 void skynet( allocator_type & salloc, channel_type & c, std::size_t num, std::size_t size, std::size_t div) {
42 if ( 1 == size) {
43 c.push( num);
44 } else {
45 channel_type rc{ 16 };
46 std::vector< boost::fibers::fiber > fibers;
47 for ( std::size_t i = 0; i < div; ++i) {
48 auto sub_num = num + i * size / div;
49 fibers.emplace_back( boost::fibers::launch::dispatch,
50 std::allocator_arg, salloc,
51 skynet,
52 std::ref( salloc), std::ref( rc), sub_num, size / div, div);
53 }
54 for ( auto & f: fibers) {
55 f.join();
56 }
57 std::uint64_t sum{ 0 };
58 for ( std::size_t i = 0; i < div; ++i) {
59 sum += rc.value_pop();
60 }
61 c.push( sum);
62 }
63 }
64
65 void thread( unsigned int idx, barrier * b) {
66 boost::fibers::numa::pin_thread( idx);
67 boost::fibers::use_scheduling_algorithm< boost::fibers::algo::shared_work >();
68 b->wait();
69 lock_type lk( mtx);
70 cnd.wait( lk, [](){ return done; });
71 BOOST_ASSERT( done);
72 }
73
74 int main() {
75 try {
76 boost::fibers::use_scheduling_algorithm< boost::fibers::algo::shared_work >();
77 unsigned int n = std::thread::hardware_concurrency();
78 barrier b( n);
79 boost::fibers::numa::pin_thread( n - 1);
80 std::size_t size{ 1000000 };
81 std::size_t div{ 10 };
82 std::vector< std::thread > threads;
83 for ( unsigned int i = 1; i < n; ++i) {
84 threads.emplace_back( thread, i - 1, & b);
85 };
86 // Windows 10 and FreeBSD require a fiber stack of 8kb
87 // otherwise the stack gets exhausted
88 // stack requirements must be checked for other OS too
89 #if BOOST_OS_WINDOWS || BOOST_OS_BSD
90 allocator_type salloc{ 2*allocator_type::traits_type::page_size() };
91 #else
92 allocator_type salloc{ allocator_type::traits_type::page_size() };
93 #endif
94 std::uint64_t result{ 0 };
95 channel_type rc{ 2 };
96 b.wait();
97 time_point_type start{ clock_type::now() };
98 skynet( salloc, rc, 0, size, div);
99 result = rc.value_pop();
100 if ( 499999500000 != result) {
101 throw std::runtime_error("invalid result");
102 }
103 auto duration = clock_type::now() - start;
104 lock_type lk( mtx);
105 done = true;
106 lk.unlock();
107 cnd.notify_all();
108 for ( std::thread & t : threads) {
109 t.join();
110 }
111 std::cout << "duration: " << duration.count() / 1000000 << " ms" << std::endl;
112 return EXIT_SUCCESS;
113 } catch ( std::exception const& e) {
114 std::cerr << "exception: " << e.what() << std::endl;
115 } catch (...) {
116 std::cerr << "unhandled exception" << std::endl;
117 }
118 return EXIT_FAILURE;
119 }