#include "pls/pls.h"

using namespace pls;

#include "benchmark_runner.h"
#include "benchmark_base/matrix.h"
#include <iostream>

using namespace comparison_benchmarks::base;

template<typename T>
class pls_matrix : public matrix::matrix<T> {
 public:
  explicit pls_matrix(size_t size) : matrix::matrix<T>(size) {}

  void multiply(const matrix::matrix<T> &a, const matrix::matrix<T> &b) override {
    pls::algorithm::for_each_range(0, this->size_, [&](int i) {
      this->multiply_column(i, a, b);
    });
  }
};

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

int main(int argc, char **argv) {
  auto settings = benchmark_runner::parse_parameters(argc, argv);

  pls_matrix<double> a{settings.size_};
  pls_matrix<double> b{settings.size_};
  pls_matrix<double> result{settings.size_};

  string test_name = to_string(settings.num_threads_) + ".csv";
  string full_directory = settings.output_directory_ + "/PLS_v3/";
  benchmark_runner runner{full_directory, test_name};

  pls::scheduler scheduler{(unsigned) settings.num_threads_, MAX_NUM_TASKS, MAX_STACK_SIZE};

  if (settings.type_ == benchmark_runner::benchmark_settings::ISOLATED) {
    printf("Running isolated measurement...\n");
    runner.enable_memory_stats();
    runner.pre_allocate_stats();

    runner.run_iterations(settings.iterations_, [&]() {
      scheduler.perform_work([&]() {
        result.multiply(a, b);
      });
    });
    runner.commit_results(true);
  } else {
    printf("Running periodic measurement...\n");
    runner.enable_wall_time_stats();
    runner.pre_allocate_stats();

    runner.run_periodic(settings.iterations_, settings.interval_period_, settings.interval_deadline_, [&]() {
      scheduler.perform_work([&]() {
        result.multiply(a, b);
      });
    });
    runner.commit_results(true);
  }

}
