main.cpp 2.12 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/data_structures/bounded_trading_deque.h"
8

9 10
using namespace pls::internal;

11
constexpr size_t NUM_THREADS = 1;
12

13
constexpr size_t NUM_TASKS = 128;
14
static constexpr int NUM_ITERATIONS = 100;
15

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
static volatile int result;
49
int main() {
50 51 52 53
  scheduling::static_scheduler_memory<NUM_THREADS,
                                      NUM_TASKS,
                                      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 59 60
  for (int i = 0; i < NUM_ITERATIONS; i++) {
    result = fib_normal(30);
  }
61
  auto end = std::chrono::steady_clock::now();
62
  std::cout << "Normal:     " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
63
            << std::endl;
64

65 66
  start = std::chrono::steady_clock::now();

67 68 69 70 71 72 73 74 75 76
  for (int i = 0; i < NUM_ITERATIONS; i++) {
    scheduler.perform_work([]() {
      return scheduling::scheduler::par([]() {
        return scheduling::parallel_result<int>(0);
      }, []() {
        return fib(30);
      }).then([](int, int b) {
        result = b;
        return scheduling::parallel_result<int>{0};
      });
77
    });
78
  }
79

80
  end = std::chrono::steady_clock::now();
81
  std::cout << "Framework: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
82

83
  return 0;
84
}