main.cpp 1.71 KB
Newer Older
1
// Headers are available because we added the pls target
2 3 4 5 6 7
#include <iostream>
#include <functional>
#include <array>
#include <atomic>

#include <pls/pls.h>
8
#include <pls/internal/base/prohibit_new.h>
9 10 11 12 13 14

using namespace pls;

// Example for static memory allocation (no malloc or free required)
static static_scheduler_memory<8, 2 << 12> my_scheduler_memory;

15
class fib: public fork_join_sub_task {
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
    static constexpr int CUTOFF = 20;

    int num_;
    int* result_;

public:
    fib(int num, int* result): num_{num}, result_{result} {}

private:
    static int fib_serial(int num) {
        if (num == 0) {
            return 0;
        }
        if (num == 1) {
            return 1;
        }

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

protected:
    void execute_internal() override {
        if (num_ <= CUTOFF) {
            *result_ = fib_serial(num_);
            return;
        }

        int left_result;
        int right_result;

46 47
        spawn_child(fib{num_ - 1, &left_result});
        spawn_child(fib{num_ - 2, &right_result});
48 49 50 51 52 53

        wait_for_all();
        *result_ = left_result + right_result;
    }
};

54 55

int main() {
56
    scheduler my_scheduler{&my_scheduler_memory, 4};
57 58 59 60 61 62

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

        fib fib_sub_task{45, &result};
63
        fork_join_task tbb_task{&fib_sub_task, task_id{1}};
64 65 66 67 68 69 70
        scheduler::execute_task(tbb_task);

        std::cout << "Result: " << result << std::endl;
    });
    auto end = std::chrono::high_resolution_clock::now();
    long time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    std::cout << "Startup time in us: " << time << std::endl;
71
}