main.cpp 1.79 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 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

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;

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

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

58 59

int main() {
60
    scheduler my_scheduler{&my_scheduler_memory, 4};
61 62 63 64 65 66

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

        fib fib_sub_task{45, &result};
67
        tbb_task tbb_task{&fib_sub_task, task_id{1}};
68 69 70 71 72 73 74
        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;
75
}