//
// This is an OpenMP test/demonstration program
// Written by Christoph Federrath, 2016
//
#include <iostream>
#include <stdlib.h>
#include <omp.h>
//#include <time.h>
#include <ctime>

using namespace std;

/** ----------------------------- Main -------------------------------
 **  OMP_TEST main procedure
 ** ------------------------------------------------------------------ */
int main(int argc, char *argv[])
{
	cout << "*------------------- OMP_TEST -----------------*" << endl;

	long double time_start, time_end;
	double time_elapsed;

	double n = 9e9;

	time_start = time(0);
	//cout << "time_start = " << time_start << endl;
	double sum = 0.0;
	for (long i = 0; i <= (long)n; i++)
	{
	  sum += i;
	}
	cout << "sum = " << sum << endl;
	time_end = time(0);
	//cout << "time_end = " << time_end << endl;
	time_elapsed = (double)(time_end - time_start);
	cout << "Time: " << time_elapsed << " seconds" << endl;

	time_start = time(0);
	//cout << "time_start = " << time_start << endl;
        sum = 0.0;
        #pragma omp parallel
	{
            double sum_loc = 0.0;
	    //long i = 0;
            #pragma omp for
	    for (long i = 0; i <= (long)n; i++)
	    {
	      sum_loc += i;
	    }
            #pragma omp critical
	    {
              cout << "["<<omp_get_thread_num()<<"] sum_loc = " << sum_loc << endl;
              sum += sum_loc;
            }
        }
        cout << "sum = " << sum << endl;
        time_end = time(0);
	//cout << "time_end = " << time_end << endl;
        time_elapsed = (double)(time_end - time_start);
        cout << "Time: " << time_elapsed << " seconds" << endl;

        #pragma omp parallel
        {
          #pragma omp critical
	  cout << "["<<omp_get_thread_num()<<"] Total number of threads: "<<omp_get_num_threads()<<endl;
        }

	exit(0);

} // end: main()