]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/performance/fiber/skynet_stealing_detach.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / fiber / performance / fiber / skynet_stealing_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 <queue>
18 #include <iostream>
19 #include <memory>
20 #include <mutex>
21 #include <numeric>
22 #include <random>
23 #include <sstream>
24 #include <vector>
25
26 #include <boost/fiber/all.hpp>
27 #include <boost/predef.h>
28
29 #include "barrier.hpp"
30
31 using clock_type = std::chrono::steady_clock;
32 using duration_type = clock_type::duration;
33 using time_point_type = clock_type::time_point;
34 using channel_type = boost::fibers::buffered_channel< std::uint64_t >;
35 using allocator_type = boost::fibers::fixedsize_stack;
36 using lock_type = std::unique_lock< std::mutex >;
37
38 static bool done = false;
39 static std::mutex mtx{};
40 static boost::fibers::condition_variable_any cnd{};
41
42 // microbenchmark
43 void skynet( allocator_type & salloc, channel_type & c, std::size_t num, std::size_t size, std::size_t div) {
44 if ( 1 == size) {
45 c.push( num);
46 } else {
47 channel_type rc{ 16 };
48 for ( std::size_t i = 0; i < div; ++i) {
49 auto sub_num = num + i * size / div;
50 boost::fibers::fiber{ boost::fibers::launch::dispatch,
51 std::allocator_arg, salloc,
52 skynet,
53 std::ref( salloc), std::ref( rc), sub_num, size / div, div }.detach();
54 }
55 std::uint64_t sum{ 0 };
56 for ( std::size_t i = 0; i < div; ++i) {
57 sum += rc.value_pop();
58 }
59 c.push( sum);
60 }
61 }
62
63 void thread( std::uint32_t thread_count, barrier * b) {
64 // thread registers itself at work-stealing scheduler
65 boost::fibers::use_scheduling_algorithm< boost::fibers::algo::work_stealing >( thread_count);
66 b->wait();
67 lock_type lk( mtx);
68 cnd.wait( lk, [](){ return done; });
69 BOOST_ASSERT( done);
70 }
71
72 int main() {
73 try {
74 // count of logical cpus
75 std::uint32_t thread_count = std::thread::hardware_concurrency();
76 // main-thread registers itself at work-stealing scheduler
77 boost::fibers::use_scheduling_algorithm< boost::fibers::algo::work_stealing >( thread_count);
78 barrier b{ thread_count };
79 std::size_t size{ 1000000 };
80 std::size_t div{ 10 };
81 // Windows 10 and FreeBSD require a fiber stack of 8kb
82 // otherwise the stack gets exhausted
83 // stack requirements must be checked for other OS too
84 #if BOOST_OS_WINDOWS || BOOST_OS_BSD
85 allocator_type salloc{ 2*allocator_type::traits_type::page_size() };
86 #else
87 allocator_type salloc{ allocator_type::traits_type::page_size() };
88 #endif
89 std::uint64_t result{ 0 };
90 channel_type rc{ 2 };
91 std::vector< std::thread > threads;
92 for ( std::uint32_t i = 1 /* count main-thread */; i < thread_count; ++i) {
93 // spawn thread
94 threads.emplace_back( thread, thread_count, & b);
95 }
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 }