2
0
mirror of https://github.com/boostorg/fiber.git synced 2026-02-21 15:02:29 +00:00
Files
fiber/performance/fiber/skynet.cpp
Oliver Kowalke b53e167a68 support for channels refactored
- buffered_channel: MPMC with lock-free guarantees
- unbuffered_channel: rendezvous point
2016-10-30 19:33:49 +01:00

64 lines
2.1 KiB
C++

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <numeric>
#include <vector>
#include <boost/fiber/all.hpp>
using allocator_type = boost::fibers::fixedsize_stack;
using channel_type = boost::fibers::buffered_channel< std::uint64_t >;
using clock_type = std::chrono::steady_clock;
using duration_type = clock_type::duration;
using time_point_type = clock_type::time_point;
// microbenchmark
void skynet( allocator_type & salloc, channel_type & c, std::size_t num, std::size_t size, std::size_t div) {
if ( 1 == size) {
c.push( num);
} else {
channel_type rc{ 16 };
for ( std::size_t i = 0; i < div; ++i) {
auto sub_num = num + i * size / div;
boost::fibers::fiber{ boost::fibers::launch::dispatch,
std::allocator_arg, salloc,
skynet,
std::ref( salloc), std::ref( rc), sub_num, size / div, div }.detach();
}
std::uint64_t sum{ 0 };
for ( std::size_t i = 0; i < div; ++i) {
sum += rc.value_pop();
}
c.push( sum);
}
}
int main() {
try {
std::size_t stack_size{ 4048 };
std::size_t size{ 100000 };
std::size_t div{ 10 };
allocator_type salloc{ stack_size };
std::uint64_t result{ 0 };
duration_type duration{ duration_type::zero() };
channel_type rc{ 2 };
time_point_type start{ clock_type::now() };
skynet( salloc, rc, 0, size, div);
result = rc.value_pop();
duration = clock_type::now() - start;
std::cout << "Result: " << result << " in " << duration.count() / 1000000 << " ms" << std::endl;
std::cout << "done." << std::endl;
return EXIT_SUCCESS;
} catch ( std::exception const& e) {
std::cerr << "exception: " << e.what() << std::endl;
} catch (...) {
std::cerr << "unhandled exception" << std::endl;
}
return EXIT_FAILURE;
}