main.cpp 2.06 KB
Newer Older
1 2
#include <iostream>
#include <chrono>
3

4 5 6
#include "pls/internal/scheduling/scheduler.h"
#include "pls/internal/scheduling/parallel_result.h"
#include "pls/internal/scheduling/scheduler_memory.h"
7
#include "pls/internal/base/thread.h"
8

9 10
using namespace pls::internal;

11
constexpr size_t NUM_THREADS = 1;
12

13
constexpr size_t NUM_TASKS = 128;
14 15
constexpr size_t MAX_TASK_STACK_SIZE = 0;

16
constexpr size_t NUM_CONTS = 128;
17
constexpr size_t MAX_CONT_SIZE = 256;
18

19
int fib_normal(int n) {
20 21 22 23 24 25 26
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return 1;
  }

27 28
  int result = fib_normal(n - 1) + fib_normal(n - 2);
  return result;
29 30
}

31
scheduling::parallel_result<int> fib(int n) {
32 33 34 35 36 37 38
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return 1;
  }

39 40 41 42 43 44 45
  return scheduling::scheduler::par([=]() {
    return fib(n - 1);
  }, [=]() {
    return fib(n - 2);
  }).then([=](int a, int b) {
    return scheduling::parallel_result<int>{a + b};
  });
46
}
47

48
int main() {
49 50 51 52 53
  scheduling::static_scheduler_memory<NUM_THREADS,
                                      NUM_TASKS,
                                      MAX_TASK_STACK_SIZE,
                                      NUM_CONTS,
                                      MAX_CONT_SIZE> static_scheduler_memory;
54

55
  scheduling::scheduler scheduler{static_scheduler_memory, NUM_THREADS};
56

57
  auto start = std::chrono::steady_clock::now();
58
  std::cout << "fib = " << fib_normal(30) << std::endl;
59
  auto end = std::chrono::steady_clock::now();
60
  std::cout << "Normal:     " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
61
            << std::endl;
62

63 64 65
  start = std::chrono::steady_clock::now();

  scheduler.perform_work([]() {
66 67 68 69 70 71 72 73
    return scheduling::scheduler::par([]() {
      return scheduling::parallel_result<int>(0);
    }, []() {
      return fib(30);
    }).then([](int, int b) {
      std::cout << "fib = " << b << std::endl;
      return scheduling::parallel_result<int>{0};
    });
74
  });
75

76
  end = std::chrono::steady_clock::now();
77
  std::cout << "Framework: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
78

79
  return 0;
80
}