main.cpp 2.66 KB
Newer Older
1 2 3 4
#include "pls/internal/scheduling/scheduler.h"
#include "pls/internal/scheduling/parallel_result.h"
#include "pls/internal/scheduling/scheduler_memory.h"
using namespace pls::internal::scheduling;
5 6 7 8 9 10 11 12 13 14

#include "node.h"

const int SEED = 42;
const int ROOT_CHILDREN = 140;
const double Q = 0.124875;
const int NORMAL_CHILDREN = 8;

const int NUM_NODES = 71069;

15
parallel_result<int> count_child_nodes(uts::node &node) {
16 17 18 19 20 21 22 23 24 25
  int child_count = 1;
  std::vector<uts::node> children = node.spawn_child_nodes();

  if (children.empty()) {
    return child_count;
  }

  std::vector<int> results(children.size());
  for (size_t i = 0; i < children.size(); i++) {
    size_t index = i;
26 27 28 29 30
    auto lambda = [&, index] {
      results[index] = count_child_nodes(children[index]);
    };
    using child_type = pls::lambda_task_by_value<typeof(lambda)>;
    pls::scheduler::spawn_child<child_type>(lambda);
31
  }
32
  pls::scheduler::wait_for_all();
33 34 35 36 37 38 39
  for (auto result : results) {
    child_count += result;
  }

  return child_count;
}

40
parallel_result<int> unbalanced_tree_search(int seed, int root_children, double q, int normal_children) {
41 42 43 44 45 46
  int result;

  auto lambda = [&] {
    uts::node root(seed, root_children, q, normal_children);
    result = count_child_nodes(root);
  };
47 48
  using child_type = pls::lambda_task_by_reference<typeof(lambda)>;
  pls::scheduler::spawn_child<child_type>(lambda);
49
  pls::scheduler::wait_for_all();
50 51 52 53

  return result;
}

54
constexpr size_t MAX_NUM_THREADS = 5;
55

56
constexpr size_t MAX_NUM_TASKS = 128;
57

58
constexpr size_t MAX_NUM_CONTS = 128;
59 60 61
constexpr size_t MAX_CONT_SIZE = 512;

volatile int result;
62 63
int main() {
  PROFILE_ENABLE
64 65 66
  static_scheduler_memory<MAX_NUM_THREADS,
                          MAX_NUM_TASKS,
                          MAX_NUM_CONTS,
67 68
                          MAX_CONT_SIZE> static_scheduler_memory;

69
  scheduler scheduler{static_scheduler_memory, MAX_NUM_THREADS};
70 71 72 73 74 75 76 77 78 79 80

  scheduler.perform_work([&]() {
    return scheduler::par([&]() {
      return unbalanced_tree_search(SEED, ROOT_CHILDREN, Q, NORMAL_CHILDREN);
    }, []() {
      return parallel_result<int>{0};
    }).then([](int a, int) {
      result = a;
      return parallel_result<int>{0};
    });
  });
81 82 83

  PROFILE_SAVE("test_profile.prof")
}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

//int main() {
//  PROFILE_ENABLE
//  pls::malloc_scheduler_memory my_scheduler_memory{8, 2u << 18};
//  pls::scheduler scheduler{&my_scheduler_memory, 8};
//
//  scheduler.perform_work([&] {
//    PROFILE_MAIN_THREAD
//    for (int i = 0; i < 50; i++) {
//      PROFILE_WORK_BLOCK("Top Level")
//      int result = unbalanced_tree_search(SEED, ROOT_CHILDREN, Q, NORMAL_CHILDREN);
//      std::cout << result << std::endl;
//    }
//  });
//
//  PROFILE_SAVE("test_profile.prof")
//}