#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <numeric>
#include <iostream>


#include "wallclock_cpu.h"


#define TIME(x) std::cerr << "\t\t[" << timer.elapsed_time() << " : " << gtimer.elapsed_time() << "] \t" << #x << std::endl; timer.restart(); 


double my_random(unsigned int * seed)
{
   return static_cast<double>(rand_r(seed)) / RAND_MAX ;
}


int main(int argc, char* argv[])
{
  timer<CPU> gtimer; 
  timer<CPU> timer; 
  // tablica o 1M elementów
  const int N = (argc == 1) ? 1<<19 : atoi(argv[1]);

  // dane wejściowe na CPU z generatora liczb pseudolosowych
  std::vector<double> h_input(N);
  TIME("Zaalokowano tablicę na CPU");

//  srand(time(0));
  time_t t0 = time(0); 

#pragma omp parallel default (none) shared(t0, h_input)
{
  unsigned int my_seed = omp_get_thread_num() + t0; 
# pragma omp for  
  for(int i = 0; i < h_input.size(); ++i)
  {
    h_input[i] = my_random(&my_seed);
  }
}
  TIME("Wygenerowano dane na CPU");

  const double  host_result = std::accumulate(h_input.begin(), h_input.end(), 0.0);

  TIME("Wyznaczono sumę na CPU");

  std::cout.precision(16);
  std::cout << "Suma na CPU: " << host_result << std::endl;

  return 0;
}
