main.cpp 2.33 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 MAX_NUM_THREADS = 1;
12

13 14
constexpr size_t MAX_NUM_TASKS = 128;
static constexpr int NUM_ITERATIONS = 10;
15

16
constexpr size_t MAX_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
  pls::variable<int> i;
  pls::array<int> a{10};
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return 1;
39 40
  }

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

50
static volatile int result;
51
int main() {
52
  PROFILE_ENABLE;
53 54 55
  scheduling::static_scheduler_memory<MAX_NUM_THREADS,
                                      MAX_NUM_TASKS,
                                      MAX_NUM_CONTS,
56
                                      MAX_CONT_SIZE> static_scheduler_memory;
57

58
  scheduling::scheduler scheduler{static_scheduler_memory, MAX_NUM_THREADS};
59

60
  auto start = std::chrono::steady_clock::now();
61
  for (int i = 0; i < NUM_ITERATIONS; i++) {
62
    result = fib_normal(35);
63
  }
64
  auto end = std::chrono::steady_clock::now();
65
  std::cout << "Normal:     " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
66
            << std::endl;
67

68 69
  start = std::chrono::steady_clock::now();

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

87
  end = std::chrono::steady_clock::now();
88
  std::cout << "Framework: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
89

90
  return 0;
91
}