main.cpp 2.23 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 = 4;
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
  if (n <= 10) {
    return fib_normal(n);
34 35
  }

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

45
static volatile int result;
46
int main() {
47
  PROFILE_ENABLE;
48 49 50 51
  scheduling::static_scheduler_memory<NUM_THREADS,
                                      NUM_TASKS,
                                      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
  for (int i = 0; i < NUM_ITERATIONS; i++) {
57
    result = fib_normal(35);
58
  }
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
  start = std::chrono::steady_clock::now();

65 66
  for (int i = 0; i < NUM_ITERATIONS; i++) {
    scheduler.perform_work([]() {
67
      PROFILE_MAIN_THREAD;
68 69 70
      return scheduling::scheduler::par([]() {
        return scheduling::parallel_result<int>(0);
      }, []() {
71
        return fib(35);
72 73
      }).then([](int, int b) {
        result = b;
74
        PROFILE_LOCK("DONE");
75 76
        return scheduling::parallel_result<int>{0};
      });
77
    });
78
    PROFILE_LOCK("DONE");
79
  }
80
  PROFILE_SAVE("test_profile.prof");
81

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

85
  return 0;
86
}