#include <cassert>
#include <chrono>           // timing
#include <cmath>            // sqrt()
#include <cstdlib>          // atoi()
#include <cstring>          // strncmp()
#include <ctime>
#include <iostream>
#include <sstream>
#include <vector>
#include <lapacke.h>
#include "timing.h"
#include "benchmark.h"
using namespace std;
using namespace std::chrono;  // timing

int main()
{   
    unsigned int n= 32;

    vector<double> M(n*n,4.0);
    
    for(unsigned int i=0; i<n; i++)
    {
        for(unsigned int j=0; j<n; j++)
        {
            if(i!=j)
            {
                double diff = i-j;
                M[i*n+j] = 1.0/(diff*diff);
            }
        }

        
    }
    vector<double> M2 = M;
    


    vector<int> ipiv(n); //pivots
    LAPACKE_dgetrf(LAPACK_ROW_MAJOR,n,n, M.data(),n,ipiv.data());  //M=PLU 

    


    double time;
    unsigned int nhrsmax = 1000000;
    for(unsigned int i=nhrsmax/10; i < nhrsmax;i+=nhrsmax/10)
    {
        
        unsigned int nhrs = i;

        //FOR CHECKING
        vector<double> X(n*nhrs,1.0);

        vector<double> b = benchmark_C(M2,X,n);
        
        tic();
        LAPACKE_dgetrs(LAPACK_ROW_MAJOR,'N',n,nhrs,M.data(),n,ipiv.data(),b.data(),nhrs);
        time = toc();
        cout << "Time for nhrs=" << nhrs << ": " << time << endl;


        
        double max_err = 0.0;
        for (unsigned int j = 0; j < n * nhrs; j++)
        {
            double err = b[j] - X[j];
            err *= err;
            if (err > max_err) max_err = err;
        }
        cout <<"max err^2:" << max_err <<endl;
        cout <<endl;

    }
        /*
    
        Time for nhrs=100000: 0.0605495
        max err^2:4.93038e-32

        Time for nhrs=200000: 0.127608
        max err^2:4.93038e-32

        Time for nhrs=300000: 0.182197
        max err^2:4.93038e-32

        Time for nhrs=400000: 0.202608
        max err^2:4.93038e-32

        Time for nhrs=500000: 0.24484
        max err^2:4.93038e-32

        Time for nhrs=600000: 0.298055
        max err^2:4.93038e-32

        Time for nhrs=700000: 0.362414
        max err^2:4.93038e-32

        Time for nhrs=800000: 0.410004
        max err^2:4.93038e-32

        Time for nhrs=900000: 0.492339
        max err^2:4.93038e-32

        Time grows slow (linearly)
    */
    
    




    return 0;

    
}  
