#include "pls/pls.h"

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

using namespace comparison_benchmarks::base;

void pls_conquer(fft::complex_vector::iterator data, fft::complex_vector::iterator swap_array, int n) {
  if (n < 2) {
    return;
  }

  fft::divide(data, swap_array, n);
  if (n <= fft::RECURSIVE_CUTOFF) {
    fft::conquer(data, swap_array, n / 2);
    fft::conquer(data + n / 2, swap_array + n / 2, n / 2);
  } else {
    pls::spawn([data, n, swap_array]() {
      pls_conquer(data, swap_array, n / 2);
    });
    pls::spawn([data, n, swap_array]() {
      pls_conquer(data + n / 2, swap_array + n / 2, n / 2);
    });
    pls::sync();
  }

  fft::combine(data, n);
}

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

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};
  runner.enable_memory_stats();
  runner.pre_allocate_stats();

  fft::complex_vector data(fft::SIZE);
  fft::complex_vector swap_array(fft::SIZE);
  fft::fill_input(data);

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

//  scheduler.get_profiler().disable_memory_measure();
  runner.run_iterations(fft::NUM_ITERATIONS, [&]() {
    scheduler.perform_work([&]() {
      pls_conquer(data.begin(), swap_array.begin(), fft::SIZE);;
    });
//    scheduler.get_profiler().current_run().print_stats();
  }, fft::NUM_WARMUP_ITERATIONS, [&]() {
    fft::fill_input(data); // Reset data before each run
  });
//  scheduler.get_profiler().current_run().print_dag(std::cout);
//  scheduler.get_profiler().current_run().print_stats();

  runner.commit_results(true);

  return 0;
}
