/*
COPYRIGHT (2011-2012) by:
Kevin Marco Erler (author), http://www.kevinerler.de
AIU-FSU Jena (co-owner), http://www.astro.uni-jena.de
SBSZ Jena-Göschwitz (co-owner), http://www.sbsz-jena.de
BSZ-Hermsdorf (co-owner), http://www.bszh.de
Advanced Licensing (dual license: COPYRIGHT and following licenses):
License (international): CC-BY v3.0-unported or later - link: http://creativecommons.org/licenses/by/3.0/deed.en
License (Germany):       CC-BY v3.0-DE       or later - link: http://creativecommons.org/licenses/by/3.0/de/
------------------
Compilation requirements:
Packages (x86-64):
  GCC >v4.2, compat. libstdc++ and GOMP v3.0
Normal-Compile with g++-Compiler (Red Hat GCC 4.4.5-6 x86-64 tested) + OpenMP v3.0 ([lib]GOMP v3.0 x86-64 tested)
  g++ -std=c++0x -m64 -fopenmp -Wall -Wextra -pedantic -pedantic-errors -lgomp -lm -s <source.cpp> -o <dest>
Release-Compile with g++-Compiler (Red Hat GCC 4.4.5-6 x86-64 tested) + OpenMP v3.0 ([lib]GOMP v3.0 x86-64 tested)
  g++ -std=c++0x -m64 -fopenmp -Wall -Wextra -pedantic -pedantic-errors -lgomp -lm -O3 -s <source.cpp> -o <dest>
Debug-Compile with g++-Compiler (Red Hat GCC 4.4.5-6 x86-64 tested) + OpenMP v3.0 ([lib]GOMP v3.0 x86-64 tested)
  g++ -std=c++0x -m64 -fopenmp -Wall -Wextra -pedantic -pedantic-errors -lgomp -lm -g -ggdb3 <source.cpp> -o <dest>
*/

// Includes of C/C++-Librarys for INTs, REAL/FLOATs, STRINGS, Math-Calc and I/O
#include <climits>
#include <cstdint>
#include <cinttypes>
#include <cfloat>
#include <cwchar>
#include <string>  //std:string
#include <string.h>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>

// Conditional compilation (conditional include) of the OpenMP-Mainlib for OpenMP-Support
#ifdef _OPENMP
#include <omp.h>
#endif

using namespace std;

#define free(x) free(x); *x=NULL
#define PRId128 "s"
#define PRIi128 "s"
#define PRIu128 "s"

const uint64_t UINT64_MIN     = 0;
const __int128_t INT128_MIN   = (__int128_t)((-170141183460469231731.687303715884105728) * pow(10,18));
const __int128_t INT128_MAX   = (__int128_t)(( 170141183460469231731.687303715884105727) * pow(10,18));
const __uint128_t UINT128_MAX = (__uint128_t)((340282366920938463463.374607431768211455) * pow(10,18));
const __uint128_t UINT128_MIN = 0/* * pow(10,18)*/;

std::ostream &operator<<(std::ostream &out, __uint128_t x)
{
  if(x >= 10)
  {
    out << x / 10;
  }
  return out << static_cast<unsigned>(x % 10);
}

std::ostream &operator<<(std::ostream &out, __int128_t x)
{
  if(x < 0)
  {
    out << '-';
    x = -x;
  }
  return out << static_cast<__uint128_t>(x);
}

string INT128ToSTR(__int128_t x)
{
  std::stringstream sstr;
  sstr<<x;
  return sstr.str();
}
#define INT128ToCSTR(x) (INT128ToSTR(x)).c_str()

string UINT128ToSTR(__uint128_t x)
{
  std::stringstream sstr;
  sstr<<x;
  return sstr.str();
}
#define UINT128ToCSTR(x) (UINT128ToSTR(x)).c_str()

struct V2D //my own 2D-Vector data type
{
  private:
  protected:
  public:
    uint64_t x, y;
    V2D(uint64_t X = 0ULL, uint64_t Y = 0ULL):x(X),y(Y){}
    ~V2D(){};
};

const uint64_t Num2DVecs = 400000000ULL; //Num 2D-Vec´s

int main(int argc, char *argv[])
{
  // Runtime manipulation of OpenMP-state variables
  //omp_set_num_threads(4);
  omp_set_dynamic(0);

  // data declarations and implementations
  double starttime = 0.00, sdelay = 0.00, pdelay = 0.00;
  struct V2D A_s[Num2DVecs], \
             B_s[Num2DVecs], \
             C_s[Num2DVecs], \
             A_p[Num2DVecs], \
             B_p[Num2DVecs], \
             C_p[Num2DVecs];
  bool ResultsAreCorrect = false;

  std::cout << "Vektoraddition (2D)                                        (64-Bit)\n"
            << "===================================================================\n"
            << "Initialisierung:";

  //--------------------------Begin: Initialization of data------------------------------------------

  for(uint64_t i=0ULL;i<Num2DVecs;++i)
  {
    A_s[i].x = A_s[i].y = B_s[i].x = B_s[i].y =
    A_p[i].x = A_p[i].y = B_p[i].x = B_p[i].y= (i+1ULL);
  }

  //--------------------------End: Initialization of data--------------------------------------------

  std::cout << "                                               done\n"
            << "SERIELLE AUSFÜHRUNG:";

  //--------------------------Begin: CPU-serial execution of algorithm-------------------------------

  starttime = omp_get_wtime();
  //CPU-serial algorithm:
  for(uint64_t j=0ULL;j<Num2DVecs;++j)
  {
    C_s[j].x = A_s[j].x + B_s[j].x;
    C_s[j].y = A_s[j].y + B_s[j].y;
  }
  sdelay = omp_get_wtime()-starttime;
  std::cout << "                                           done\n"; //serial

  //--------------------------End: CPU-serial execution of algorithm---------------------------------

  //--------------------------Begin: CPU-parallel OpenMP-execution of algorithm----------------------

  std::cout << "PARALLELE AUSFÜHRUNG mit ";

  // create parallel region:
  #pragma omp parallel default(none) shared(std::cout, starttime, pdelay, A_p, B_p, C_p)
  {
    #pragma omp master
    {
      std::cout << omp_get_num_threads() << " Threads:";
      starttime = omp_get_wtime();
    }

    //OpenMP-CPU-parallel algorithm
    #pragma omp flush
    #pragma omp for schedule(static)
    for(uint64_t k=0ULL;k<Num2DVecs;++k)
    {
      C_p[k].x = A_p[k].x + B_p[k].x;
      C_p[k].y = A_p[k].y + B_p[k].y;
    }

    #pragma omp master
    {
      pdelay = omp_get_wtime()-starttime;
      if(omp_get_num_threads() >= 10)
      {
        std::cout << "                           done\n";  //parallel
      }
      else
      {
        std::cout << "                            done\n"; //parallel
      }
    }
  }

  //--------------------------End: CPU-parallel OpenMP-execution of algorithm------------------------

  //--------------------------Analysis of results----------------------------------------------------

  std::cout << "Überprüfe Ergebnisse:";
  for(uint64_t l=0ULL;l<Num2DVecs;++l)
  {
    if((C_p[l].x==C_s[l].x)&&(C_p[l].y==C_s[l].y))
    {
      ResultsAreCorrect = true;
    }
    else
    {
      ResultsAreCorrect = false;
      break;
    }
  }
  std::cout << "                                          done\n";

  std::cout << "\nAuswertung:\n"
            << "*******************************************************************\n"
            << "Anzahl 2D-Eingangsvektoren  A: " << Num2DVecs << '\n'
            << "Anzahl 2D-Eingangsvektoren  B: " << Num2DVecs << '\n'
            << "Anzahl 2D-Ergebnis-Vektoren C: " << Num2DVecs << '\n'
            << "Seriell & parallel richtig gerechnet?:                          " << ((ResultsAreCorrect==true)?"yes\n":" no\n")
            << "Dauer - SERIELL:     " << sdelay << " sec\n"
            << "Dauer - PARALLEL:    " << pdelay << " sec\n"
            << "__________________\n"
            << "Beispiele:\n"
            << "==> 1.2D-Vektoraddition:\n"
            << "a1(" << A_p[0].x << ';' << A_p[0].y << ") + b1(" << B_p[0].x << ';' << B_p[0].y << ") = \n"
            << "c1(" << C_p[0].x << ';' << C_p[0].y << ")\n"
            << "==> " << Num2DVecs << ".2D-Vektoraddition:\n"
            << 'a' << Num2DVecs << '(' << A_p[Num2DVecs-1ULL].x << ';' << A_p[Num2DVecs-1ULL].y << ") + b"
                   << Num2DVecs << '(' << B_p[Num2DVecs-1ULL].x << ';' << B_p[Num2DVecs-1ULL].y << ") = \n"
            << 'c' << Num2DVecs << '(' << C_p[Num2DVecs-1ULL].x << ';' << C_p[Num2DVecs-1ULL].y << ")\n"
            << "__________________"
            << "\n64-Bit-Werte:\n"
            << "INT64_MIN:                                    "  << INT64_MIN << '\n'
            << "INT64_MAX:                                     " << INT64_MAX << '\n'
            << "UINT64_MIN:                                    " << UINT64_MIN << '\n'
            << "UINT64_MAX:                                    " << UINT64_MAX << '\n';
  /*
  // Detailed output
  std::cout << "__________________\n"
            << "Ergebnisliste:\n";
  for(uint64_t m=0ULL;m<Num2DVecs;++m)
  {
    std::cout << "Seriell:  " 
              << "a" << (m+1ULL) << '(' << A_s[m].x << ';' << A_s[m].y << ") + "
              << "b" << (m+1ULL) << '(' << B_s[m].x << ';' << B_s[m].y << ") = "
              << "c" << (m+1ULL) << '(' << C_s[m].x << ';' << C_s[m].y << ")\n"
              << "Parallel: " 
              << "a" << (m+1ULL) << '(' << A_p[m].x << ';' << A_p[m].y << ") + "
              << "b" << (m+1ULL) << '(' << B_p[m].x << ';' << B_p[m].y << ") = "
              << "c" << (m+1ULL) << '(' << C_p[m].x << ';' << C_p[m].y << ")\n";
  }*/

  getchar();
  return 0;
}