main.cpp 1.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <pls/pls.h>
#include <iostream>

static pls::static_scheduler_memory<8, 2 << 10> my_scheduler_memory;

static constexpr int CUTOFF = 20;

long fib_serial(long n) {
    if (n == 0) {
        return 0;
    }
    if (n == 1) {
        return 1;
    }

    return fib_serial(n - 1) + fib_serial(n - 2);
}

long fib(long n) {
    if (n <= CUTOFF) {
        return fib_serial(n);
    }

    // Actual 'invoke_parallel' logic/code
    int left, right;
    pls::invoke_parallel(
        [&] { left = fib(n - 1); },
        [&] { right = fib(n - 2); }
    );
    return left + right;
}

int main() {
    pls::scheduler scheduler{&my_scheduler_memory, 8};

    scheduler.perform_work([] {
        auto start = std::chrono::high_resolution_clock::now();

        // Call looks just the same, only requirement is
        // the enclosure in the perform_work lambda.
        long result = fib(30);

        auto end = std::chrono::high_resolution_clock::now();
        long time = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();

        std::cout << "Fib(30)=" << result << std::endl;
        std::cout << "Execution time in us: " << time << std::endl;
    });
}