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)
7 // based on https://github.com/atemerev/skynet from Alexander Temerev
13 #include <condition_variable>
26 #include <boost/fiber/all.hpp>
27 #include <boost/predef.h>
29 #include "barrier.hpp"
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
>;
38 static bool done
= false;
39 static std::mutex mtx
{};
40 static boost::fibers::condition_variable_any cnd
{};
43 std::uint64_t skynet(allocator_type
& salloc
, std::uint64_t num
, std::uint64_t size
, std::uint64_t div
) {
47 std::vector
<boost::fibers::future
<std::uint64_t> > results
;
48 results
.reserve( div
);
50 for ( std::uint64_t i
= 0; i
!= div
; ++i
) {
51 std::uint64_t sub_num
= num
+ i
* size
;
52 results
.emplace_back(boost::fibers::async(
53 boost::fibers::launch::dispatch
54 , std::allocator_arg
, salloc
56 , std::ref( salloc
), sub_num
, size
, div
));
59 std::uint64_t sum
= 0;
60 for ( auto& f
: results
)
69 void thread( std::uint32_t thread_count
, barrier
* b
) {
70 // thread registers itself at work-stealing scheduler
71 boost::fibers::use_scheduling_algorithm
< boost::fibers::algo::work_stealing
>( thread_count
);
74 cnd
.wait( lk
, [](){ return done
; });
80 // count of logical ids
81 std::uint32_t thread_count
= std::thread::hardware_concurrency();
82 // main-thread registers itself at work-stealing scheduler
83 boost::fibers::use_scheduling_algorithm
< boost::fibers::algo::work_stealing
>( thread_count
);
84 barrier b
{ thread_count
};
85 std::size_t size
{ 1000000 };
86 std::size_t div
{ 10 };
87 // Windows 10 and FreeBSD require a fiber stack of 8kb
88 // otherwise the stack gets exhausted
89 // stack requirements must be checked for other OS too
90 #if BOOST_OS_WINDOWS || BOOST_OS_BSD
91 allocator_type salloc
{ 2*allocator_type::traits_type::page_size() };
93 allocator_type salloc
{ allocator_type::traits_type::page_size() };
95 std::uint64_t result
{ 0 };
97 std::vector
< std::thread
> threads
;
98 for ( std::uint32_t i
= 1 /* count main-thread */; i
< thread_count
; ++i
) {
100 threads
.emplace_back( thread
, thread_count
, & b
);
103 time_point_type start
{ clock_type::now() };
104 result
= skynet( salloc
, 0, size
, div
);
105 if ( 499999500000 != result
) {
106 throw std::runtime_error("invalid result");
108 auto duration
= clock_type::now() - start
;
113 for ( std::thread
& t
: threads
) {
116 std::cout
<< "duration: " << duration
.count() / 1000000 << " ms" << std::endl
;
118 } catch ( std::exception
const& e
) {
119 std::cerr
<< "exception: " << e
.what() << std::endl
;
121 std::cerr
<< "unhandled exception" << std::endl
;