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

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
using namespace pls::internal;

constexpr size_t NUM_THREADS = 1;

constexpr size_t NUM_TASKS = 64;
constexpr size_t MAX_TASK_STACK_SIZE = 0;

constexpr size_t NUM_CONTS = 64;
constexpr size_t MAX_CONT_SIZE = 128;

scheduling::parallel_result<int> fib(int n) {
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return 1;
  }

  return scheduling::scheduler::par([=]() {
    return fib(n - 1);
  }, [=]() {
    return fib(n - 2);
  }).then([](int a, int b) {
    return a + b;
  });
}

int fib_normal(int n) {
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return 1;
  }

  return fib_normal(n - 1) + fib_normal(n - 2);
}
45

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

53
  scheduling::scheduler scheduler{static_scheduler_memory, NUM_THREADS};
54

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

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

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

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

77
  return 0;
78
}