#include "pls/pls.h"

#include "benchmark_runner.h"
#include "benchmark_base/fib.h"

using namespace comparison_benchmarks::base;

constexpr int MAX_NUM_TASKS = 32;
constexpr int MAX_STACK_SIZE = 4096 * 1;

int pls_fib(int n, int d) {
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return 1;
  }

  int a, b;
  pls::spawn([n, d, &a]() {
    a = pls_fib(n - 1, d + 1);
  });
  pls::spawn([n, d, &b]() {
    b = pls_fib(n - 2, d + 1);
  });
  pls::sync();

  return a + b;
}

int main(int argc, char **argv) {
  int num_threads;
  string directory;
  benchmark_runner::read_args(argc, argv, num_threads, directory);

  string test_name = to_string(num_threads) + ".csv";
  string full_directory = directory + "/PLS_v3/";
  benchmark_runner runner{full_directory, test_name};

  pls::scheduler scheduler{(unsigned) num_threads, MAX_NUM_TASKS, MAX_STACK_SIZE};

  volatile int res;
//  scheduler.get_profiler().disable_memory_measure();
  runner.run_iterations(fib::NUM_ITERATIONS, [&]() {
    scheduler.perform_work([&]() {
      res = pls_fib(fib::INPUT_N, 0);
    });
  }, fib::NUM_WARMUP_ITERATIONS);
//  scheduler.get_profiler().current_run().print_dag(std::cout);
//  scheduler.get_profiler().current_run().print_stats();

  runner.commit_results(true);

  return 0;
}
