﻿#include "stdafx.h"
#include <boost/thread.hpp>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include <omp.h>

#define Nu 1500 // Lato della matrice quadrata
#define Nth 4 // Numero di thread da lanciare
int main() {
	const int steps=Nu/Nth;
	boost::thread lam[Nth];

	omp_set_num_threads(Nth);
	//int i,j,k; Perchè è un ERRORE definirle QUI ?
	double *A, *B, *C, *D, *S, start_time, end_time, ok_time, bad_time;
    double diff;
	A = (double *)malloc(Nu*Nu*sizeof(double)); // Riserva la memoria per
	B = (double *)malloc(Nu*Nu*sizeof(double)); 
	C = (double *)malloc(Nu*Nu*sizeof(double));
	D = (double *)malloc(Nu*Nu*sizeof(double));
	S = (double *)malloc(Nu*Nu*sizeof(double));
	printf(" Matrix line lenght (bytes): %d \n",Nu*sizeof(double));

		for( int j=0;j<Nu;j++)
	{
		for( int i=0;i<Nu;i++)	
		{			
			*(A+(j*Nu+i))=i+j; 
			*(B+(j*Nu+i))=i-j;
		}
	}


		start_time=omp_get_wtime();
//********************* Versione SCALARE
for ( int k=0; k<Nu; k++)
	{
		for (int j=0; j<Nu; j++)
		{
			*(D+(k*Nu+j))= *(B+(j*Nu+k));   // Solo questo punto potrebbe dare dei problemi 
											// Ma il numero di accessi � di ordine 2*Nu^2 di cui solo Nu^2 non locale
		}
	}
 [&] () {
for ( int i=0; i<Nu; i++)
	{
		for (int j=0; j<Nu; j++)
		{ 
			*(S+(i*Nu+j))=0.0;
			for(int k=0;k<Nu ;k++)
			{
				*(S+(i*Nu+j))  += *(A+(i*Nu+k)) *  *(D+(j*Nu+k)); // C(i,j) = sum(over k) A(i,k) * B(k,j) Numero di accessi Nu^3
			}
		}
}}();
end_time=omp_get_wtime();
double scalar_time=end_time-start_time;
		printf("Tempo scalare : %f  \n",scalar_time);
	
// *********** Indirizzamento Corretto 
	
	
	

	start_time=omp_get_wtime(); // fa partire il timer
	/*	
// Prepara la matrice trasposta  D ( Trasposta di B )
 for (int th=0; th<Nth;th++)
 {
	 lam[th]=boost::thread([&]( int TID) {
		 int k,j;
	for ( k=steps*TID; k<steps*(TID+1); k++)
	{
		for (j=0; j<Nu; j++)
		{
			*(D+(k*Nu+j))= *(B+(j*Nu+k));   // Solo questo punto potrebbe dare dei problemi 
											// Ma il numero di accessi � di ordine 2*Nu^2 di cui solo Nu^2 non locale
		}
	}
	 } // fine funzione Lambda
	 ,th);
 }
 	for( int j=0;j<4;j++) lam[j].join(); // BARRIERA.....
*/
for ( int k=0; k<Nu; k++)
	{
		for (int j=0; j<Nu; j++)
		{
			*(D+(k*Nu+j))= *(B+(j*Nu+k));   // Solo questo punto potrebbe dare dei problemi 
											// Ma il numero di accessi � di ordine 2*Nu^2 di cui solo Nu^2 non locale
		}
	}

 for (int th=0; th<Nth;th++)
 {
	 lam[th]=boost::thread([&]( int TID) {
		 int i,k,j;
		 for ( i=steps*TID; i<steps*(TID+1); i++)
	{
		for (j=0; j<Nu; j++)
		{
			*(C+(i*Nu+j))=0.0;
			for(k=0;k<Nu ;k++)
			{
				*(C+(i*Nu+j))  += *(A+(i*Nu+k)) *  *(D+(j*Nu+k)); // C(i,j) = sum(over k) A(i,k) * B(k,j) Numero di accessi Nu^3
			}
		}
	}
	 },th);
	 }
 for( int j=0;j<4;j++) lam[j].join(); // BARRIERA..... 
	end_time=omp_get_wtime();
	
	
	auto diffcalc = [&] () {
		diff=0;
	for (int j=0; j<Nu; j++)
		{
			
			for(int k=0;k<Nu ;k++)
			{
				 diff += (*(S+(j*Nu+k))  - *(C+(j*Nu+k)))*(*(S+(j*Nu+k))  - *(C+(j*Nu+k))) ; 
			}
	}};
	diffcalc();
	ok_time=end_time-start_time;
	printf("Tempo ok : %f  %f \n",ok_time, diff);
	

	
	

start_time=omp_get_wtime();

	for (int th=0; th<Nth;th++)
 {
	 lam[th]=boost::thread([&]( int TID) {
		 int i, k,j;
		 for ( i=steps*TID; i<steps*(TID+1); i++)
	{
		for (j=0; j<Nu; j++)
		{
			*(C+(i*Nu+j))=0.0;
			for(k=0;k<Nu ;k++)
			{
				*(C+(i*Nu+j))  += *(A+(i*Nu+k)) *  *(B+(k*Nu+j)); /* C(i,j) = sum(over k) A(i,k) * B(k,j) */
			}
			
		}	
	}
	 },th);
	}
	for( int j=0;j<4;j++) lam[j].join(); // BARRIERA..... 
		end_time=omp_get_wtime();
		bad_time=end_time-start_time;
		diffcalc();
		printf("Tempo bad : %f  %f\n",bad_time,diff);
		
	


		start_time=omp_get_wtime();
#pragma omp parallel for 
for ( int k=0; k<Nu; k++)
	{
		for (int j=0; j<Nu; j++)
		{
			*(D+(k*Nu+j))= *(B+(j*Nu+k));   // Solo questo punto potrebbe dare dei problemi 
											// Ma il numero di accessi � di ordine 2*Nu^2 di cui solo Nu^2 non locale
		}
	}
[&](){
#pragma omp parallel for 
for ( int i=0; i<Nu; i++)
	{
		for (int j=0; j<Nu; j++)
		{
			*(C+(i*Nu+j))=0.0;
			for(int k=0;k<Nu ;k++)
			{
				*(C+(i*Nu+j))  += *(A+(i*Nu+k)) *  *(D+(j*Nu+k)); // C(i,j) = sum(over k) A(i,k) * B(k,j) Numero di accessi Nu^3
			}
		}
}}();
end_time=omp_get_wtime();
double omp_time=end_time-start_time;
		diffcalc();
		printf("Tempo OpenMP : %f  %f\n",omp_time,diff);
		printf("Rapporto bad/ok : %f , Per una matrice quadrata di lato %d\n",bad_time/ok_time, Nu );
		printf("Rapporto OMP/ok: %f , Per una matrice quadrata di lato %d\n",omp_time/ok_time, Nu );
		printf("Rapporto Scalar/ok: %f , Per una matrice quadrata di lato %d\n",scalar_time/ok_time, Nu );
		printf("Rapporto Scalar/OMP: %f , Fra tempo scalare e parallelismo OMP \n",scalar_time/omp_time );
	free(A);
	free(B);
	free(C);
	free(D);
	
	
}