main.cpp 1.92 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 56 57 58 59
  auto start = std::chrono::steady_clock::now();
  std::cout << "fib = " << fib_normal(41) << std::endl;
  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 65 66 67 68 69 70
  start = std::chrono::steady_clock::now();

  scheduler.perform_work([]() {
    return scheduling::scheduler::par([]() {
      return fib(41);
    }, []() {
      return scheduling::parallel_result<int>{0};
    }).then([](int a, int b) {
      std::cout << "fib = " << a << std::endl;
    });
71
  });
72

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

76
  return 0;
77
}