]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/performance/fiber/skynet_shared_detach.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / fiber / performance / fiber / skynet_shared_detach.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 for ( std::size_t i = 0; i < div; ++i) {
47 auto sub_num = num + i * size / div;
48 boost::fibers::fiber{ boost::fibers::launch::dispatch,
49 std::allocator_arg, salloc,
50 skynet,
51 std::ref( salloc), std::ref( rc), sub_num, size / div, div }.detach();
52 }
53 std::uint64_t sum{ 0 };
54 for ( std::size_t i = 0; i < div; ++i) {
55 sum += rc.value_pop();
56 }
57 c.push( sum);
58 }
59 }
60
61 void thread( unsigned int idx, barrier * b) {
62 boost::fibers::numa::pin_thread( idx);
63 boost::fibers::use_scheduling_algorithm< boost::fibers::algo::shared_work >();
64 b->wait();
65 lock_type lk( mtx);
66 cnd.wait( lk, [](){ return done; });
67 BOOST_ASSERT( done);
68 }
69
70 int main() {
71 try {
72 boost::fibers::use_scheduling_algorithm< boost::fibers::algo::shared_work >();
73 unsigned int n = std::thread::hardware_concurrency();
74 barrier b( n);
75 boost::fibers::numa::pin_thread( n - 1);
76 std::size_t size{ 1000000 };
77 std::size_t div{ 10 };
78 std::vector< std::thread > threads;
79 for ( unsigned int i = 1; i < n; ++i) {
80 threads.emplace_back( thread, i - 1, & b);
81 };
82 // Windows 10 and FreeBSD require a fiber stack of 8kb
83 // otherwise the stack gets exhausted
84 // stack requirements must be checked for other OS too
85 #if BOOST_OS_WINDOWS || BOOST_OS_BSD
86 allocator_type salloc{ 2*allocator_type::traits_type::page_size() };
87 #else
88 allocator_type salloc{ allocator_type::traits_type::page_size() };
89 #endif
90 std::uint64_t result{ 0 };
91 channel_type rc{ 2 };
92 b.wait();
93 time_point_type start{ clock_type::now() };
94 skynet( salloc, rc, 0, size, div);
95 result = rc.value_pop();
96 if ( 499999500000 != result) {
97 throw std::runtime_error("invalid result");
98 }
99 auto duration = clock_type::now() - start;
100 lock_type lk( mtx);
101 done = true;
102 lk.unlock();
103 cnd.notify_all();
104 for ( std::thread & t : threads) {
105 t.join();
106 }
107 std::cout << "duration: " << duration.count() / 1000000 << " ms" << std::endl;
108 return EXIT_SUCCESS;
109 } catch ( std::exception const& e) {
110 std::cerr << "exception: " << e.what() << std::endl;
111 } catch (...) {
112 std::cerr << "unhandled exception" << std::endl;
113 }
114 return EXIT_FAILURE;
115 }