main.cpp 2.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <pls/pls.h>
#include <pls/internal/helpers/profiler.h>
#include <pls/internal/helpers/mini_benchmark.h>

#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;

int count_child_nodes(uts::node &node) {
  int child_count = 1;
  std::vector<uts::node> children = node.spawn_child_nodes();

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

22
  auto current_task = pls::task::current();
23 24 25 26
  std::vector<int> results(children.size());
  for (size_t i = 0; i < children.size(); i++) {
    size_t index = i;
    auto lambda = [&, index] { results[index] = count_child_nodes(children[index]); };
27
    pls::lambda_task_by_value<typeof(lambda)> sub_task(lambda);
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    current_task->spawn_child(sub_task);
  }
  current_task->wait_for_all();
  for (auto result : results) {
    child_count += result;
  }

  return child_count;
}

int unbalanced_tree_search(int seed, int root_children, double q, int normal_children) {
  static auto id = pls::unique_id::create(42);
  int result;

  auto lambda = [&] {
    uts::node root(seed, root_children, q, normal_children);
    result = count_child_nodes(root);
  };
46 47
  pls::lambda_task_by_reference<typeof(lambda)> task(lambda);
  pls::lambda_task_by_reference<typeof(lambda)> sub_task(lambda);
48
  pls::task root_task{&sub_task, id};
49 50 51 52
  pls::scheduler::execute_task(root_task);

  return result;
}
53 54 55 56 57 58 59 60 61
//
//int main() {
//  PROFILE_ENABLE
//  pls::internal::helpers::run_mini_benchmark([&] {
//    unbalanced_tree_search(SEED, ROOT_CHILDREN, Q, NORMAL_CHILDREN);
//  }, 8, 4000);
//
//  PROFILE_SAVE("test_profile.prof")
//}
62 63 64

int main() {
  PROFILE_ENABLE
65 66 67 68 69 70 71 72 73 74 75
  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;
    }
  });
76 77 78

  PROFILE_SAVE("test_profile.prof")
}