Skip to content
Snippets Groups Projects
Commit ca5bcf5c authored by Florian Schmaus's avatar Florian Schmaus
Browse files

[apps]: Modernize 'fib' app

parent 43e1fbe7
No related branches found
No related tags found
1 merge request!353Work-Stealing queue fill-size stats
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2020 Florian Schmaus
#include <cstdlib> // for exit, EXIT_SUCCESS
// Copyright © 2020-2022 Florian Schmaus
#include <boost/program_options.hpp>
#include <cstdint>
#include <iostream> // for basic_ostream::operator<<
#include <memory>
#include <thread>
#include "BinaryPrivateSemaphore.hpp" // for BPS
#include "CountingPrivateSemaphore.hpp" // for CPS
......@@ -9,18 +12,20 @@
#include "Fiber.hpp" // for Fiber
#include "PrivateSemaphore.hpp" // for PS
#include "Runtime.hpp" // for Runtime
#include "emper-common.h" // for UNUSED_ARG
#include "lib/sync/Semaphore.hpp"
namespace po = boost::program_options;
using fibParams = struct {
int n;
int* result;
uint64_t n;
uint64_t* result;
PS* sem;
};
static void fib(void* voidParams) {
auto* params = static_cast<fibParams*>(voidParams);
int n = params->n;
int* result = params->result;
uint64_t n = params->n;
auto* result = params->result;
PS* sem = params->sem;
if (n < 2) {
......@@ -28,12 +33,15 @@ static void fib(void* voidParams) {
} else {
CPS newSem(2);
int a, b;
uint64_t a, b;
fibParams newParams1;
newParams1.n = n - 1;
newParams1.result = &a;
newParams1.sem = &newSem;
// Note that this is the inefficient spawn/sync variant, we
// usually would compute one previous fib number without spawning.
fibParams newParams2;
newParams2.n = n - 2;
newParams2.result = &b;
......@@ -56,35 +64,59 @@ static void fib(void* voidParams) {
sem->signalAndExit();
}
static void fibKickoff() {
const int fibNum = 4;
int result;
BPS sem;
fibParams params = {fibNum, &result, &sem};
// NOLINTNEXTLINE(bugprone-exception-escape)
auto main(int argc, char* argv[]) -> int {
uint64_t fibNum = -1;
po::options_description desc("Allowed options");
// clang-format off
desc.add_options()
("help", "Show help")
("nthreads", po::value<unsigned int>()->default_value(std::thread::hardware_concurrency()), "Number of worker threads used by EMPER's runtime system")
("fibnum", po::value<uint64_t>(&fibNum)->default_value(12), "The Fibonacci number to compute")
;
// clang-format on
// Make 'fibnum' a positional option.
po::positional_options_description pos_desc;
pos_desc.add("fibnum", -1);
// clang-format off
auto parse_result = po::command_line_parser(argc, argv)
.options(desc)
.positional(pos_desc)
.run()
;
// clang-format on
po::variables_map vm;
po::store(parse_result, vm);
po::notify(vm);
const unsigned nthreads = vm["nthreads"].as<unsigned int>();
fib(&params);
std::cout << "Number of threads: " << nthreads << std::endl;
sem.wait();
Runtime runtime(nthreads);
std::cout << "fib(" << fibNum << ") = " << result << std::endl;
exit(EXIT_SUCCESS);
}
emper::lib::sync::Semaphore semaphore;
auto main(UNUSED_ARG int argc, UNUSED_ARG char* argv[]) -> int {
// const unsigned nthreads = std::thread::hardware_concurrency();
const unsigned nthreads = 2;
Fiber* fibFiber = Fiber::from([&] {
uint64_t result;
BPS sem;
fibParams params = {fibNum, &result, &sem};
std::cout << "Number of threads: " << nthreads << std::endl;
fib(&params);
Runtime runtime(nthreads);
sem.wait();
Fiber* fibFiber = Fiber::from(&fibKickoff);
std::cout << "fib(" << fibNum << ") = " << result << std::endl;
std::cout << "Just alloacted alpha fiber at " << fibFiber << std::endl;
semaphore.notify();
});
runtime.scheduleFromAnywhere(*fibFiber);
runtime.waitUntilFinished();
semaphore.wait();
return 0;
}
boost_program_options_dep = dependency('boost', modules: ['program_options'])
boost_program_options_code = '''
#include <boost/program_options.hpp>
int main(int argc, char* argv[]) {
boost::program_options::options_description desc("Allowed options");
}
'''
cpp_can_link_with_boost_program_options = cpp_compiler.links(
boost_program_options_code,
name: 'boost_progam_options',
dependencies: boost_program_options_dep,
)
if cpp_has_fs_recursive_directory_iterator and cpp_can_link_with_boost_program_options
fsearch_exe = executable(
'fsearch',
......
fib_exe = executable(
'fib',
'Main.cpp',
dependencies: emper_dep,
)
worker_sleep_example_exe = executable(
'worker_sleep_example',
'WorkerSleepExample.cpp',
......@@ -40,4 +34,26 @@ qsort = executable(
dependencies: emper_dep,
)
boost_program_options_dep = dependency('boost', modules: ['program_options'])
boost_program_options_code = '''
#include <boost/program_options.hpp>
int main(int argc, char* argv[]) {
boost::program_options::options_description desc("Allowed options");
}
'''
cpp_can_link_with_boost_program_options = cpp_compiler.links(
boost_program_options_code,
name: 'boost_progam_options',
dependencies: boost_program_options_dep,
)
if cpp_can_link_with_boost_program_options
fib_child_stealing_exe = executable(
'fib-child-stealing',
'FibChildStealing.cpp',
dependencies: [emper_dep, boost_program_options_dep],
)
endif
subdir('fsearch')
......@@ -20,6 +20,7 @@
{ include: ["<boost/program_options/parsers.hpp>", "private", "<boost/program_options.hpp>", "public"], },
{ include: ["<boost/program_options/positional_options.hpp>", "private", "<boost/program_options.hpp>", "public"], },
{ include: ["<boost/type_index/type_index_facade.hpp>", "private", "<boost/program_options.hpp>", "public"], },
{ include: ["<boost/cstdint.hpp>", "private", "<cstdint>", "public"], },
{ symbol: ["__kernel_timespec", "private", "<liburing.h>", "public" ] },
{ symbol: ["std::filesystem", "private", "<filesystem>", "public" ] },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment