main.cpp 1.8 KB
Newer Older
1
// Headers are available because we added the pls target
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 50 51 52 53 54 55 56 57 58
#include <iostream>
#include <functional>
#include <array>
#include <atomic>

#include <pls/pls.h>

using namespace pls;

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

class fib: public tbb_sub_task {
    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;

        fib left_child{num_ - 1, &left_result};
        spawn_child(left_child);
        fib right_child{num_ - 2, &right_result};
        spawn_child(right_child);

        wait_for_all();
        *result_ = left_result + right_result;
    }
public:
    void test() override {
        std::cout << "Test Override" << std::endl;
    }
};

59 60

int main() {
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    scheduler my_scheduler{&my_scheduler_memory, 1};

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

        fib fib_sub_task{45, &result};
        tbb_task tbb_task{&fib_sub_task};
        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;
76
}