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>
24 #include <boost/fiber/all.hpp>
25 #include <boost/predef.h>
27 #include "barrier.hpp"
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
;
36 static bool done
= false;
37 static std::mutex mtx
{};
38 static boost::fibers::condition_variable_any cnd
{};
41 void skynet( allocator_type
& salloc
, channel_type
& c
, std::size_t num
, std::size_t size
, std::size_t div
) {
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
,
51 std::ref( salloc
), std::ref( rc
), sub_num
, size
/ div
, div
}.detach();
53 std::uint64_t sum
{ 0 };
54 for ( std::size_t i
= 0; i
< div
; ++i
) {
55 sum
+= rc
.value_pop();
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
>();
66 cnd
.wait( lk
, [](){ return done
; });
72 boost::fibers::use_scheduling_algorithm
< boost::fibers::algo::shared_work
>();
73 unsigned int n
= std::thread::hardware_concurrency();
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
);
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() };
88 allocator_type salloc
{ allocator_type::traits_type::page_size() };
90 std::uint64_t result
{ 0 };
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");
99 auto duration
= clock_type::now() - start
;
104 for ( std::thread
& t
: threads
) {
107 std::cout
<< "duration: " << duration
.count() / 1000000 << " ms" << std::endl
;
109 } catch ( std::exception
const& e
) {
110 std::cerr
<< "exception: " << e
.what() << std::endl
;
112 std::cerr
<< "unhandled exception" << std::endl
;