#define VS
//#define MGPU

#ifdef VS
	#include "cuda_runtime.h"
	#include "device_launch_parameters.h"

#endif

#ifdef MGPU
	#include<cuda.h>
#endif

#include <stdio.h>
#include<stdlib.h>

//#define DEBUG_MATRIX
#define DEBUG


//Dal profiler nvida per il kernel vengono usati 13 registri (si deve passare sulla multiGPU)

void initializeMatrix(int *array, int matrixSize);
void printMatrix(int *matrix, int rows, int cols);
__global__ void sumMatrixGPU(int *a, int*b, int *c, int rows, int cols);
void sumMatrixCPU(int *a, int*b, int *c, int rows, int cols);
void matrixEqualCheck(int *mHost, int *mDevice, int cols, int rows);
int main()
{
	int *aHost, *bHost, *cHost, *rHost;
	int *aDevice, *bDevice, *cDevice;
	int size, N;
	dim3 gridDim, blockDim;
	int matrixSize;
	cudaEvent_t start, stop;
	float elapsed;


	printf("Inserisci la size della matrice quadrata(N):");
	fflush(stdout);
	scanf("%d",&N);
	
	printf("Inserisci la size dei blocchi di thread Nt (NtxNt):");
	fflush(stdout);
	scanf("%d", &blockDim.x);

	blockDim.y = blockDim.x;
	
	matrixSize = N * N;

	//Determino il numero esatto di blocchi 
	gridDim.x = N / blockDim.x + ((N % blockDim.x) == 0 ? 0 : 1);
	gridDim.y = N / blockDim.y + ((N % blockDim.y) == 0 ? 0 : 1);

	size = matrixSize * sizeof(int);

#ifdef DEBUG
	printf("Size della matrice: %d\n", matrixSize);
	printf("Numero totale di blocchi: %d (%d,%d)\n",gridDim.x*gridDim.y, gridDim.x, gridDim.y);
	printf("Numero totale dei Thread per blocco: %d (%d,%d)\n",blockDim.x*blockDim.y, blockDim.x, blockDim.y);
#endif 

	//Alloco la memoria sull' host
	aHost = (int*)malloc(size);
	bHost = (int*)malloc(size);
	rHost = (int*)malloc(size);
	cHost = (int*)calloc(matrixSize, sizeof(int)); //Azzeriamo gli array che raccolgono il risultato(lo inizializzo a zero)

	//Alloco memoria sul Device
	cudaMalloc((void **)&aDevice, size);
	cudaMalloc((void **)&bDevice, size);
	cudaMalloc((void **)&cDevice, size);

	//Azzeriamo gli array che raccolgono il risultato
	cudaMemset(cDevice, 0, size);

	//inizializzo le matrici
	initializeMatrix(aHost, matrixSize);
	initializeMatrix(bHost, matrixSize);

	//copio i dati dall'host al device
	cudaMemcpy(aDevice, aHost, size, cudaMemcpyHostToDevice);
	cudaMemcpy(bDevice, bHost, size, cudaMemcpyHostToDevice);

	//Somma parallela
	cudaEventCreate(&start);
	cudaEventCreate(&stop);
	cudaEventRecord(start, 0);

	sumMatrixGPU <<<gridDim,blockDim>>> (aDevice, bDevice, cDevice, N, N);
	
	cudaEventRecord(stop, 0);
	cudaEventSynchronize(stop);

	cudaEventElapsedTime(&elapsed, start, stop);
	//De-allocazione eventi
	cudaEventDestroy(start); 
	cudaEventDestroy(stop);
	
	printf("Tempo per la somma di matrici GPU:%f ms\n", elapsed);

	//Somma Seriale
	cudaEventCreate(&start);
	cudaEventCreate(&stop);
	cudaEventRecord(start, 0);

	sumMatrixCPU(aHost, bHost, cHost, N, N);
	
	cudaEventRecord(stop, 0);
	cudaEventSynchronize(stop);

	elapsed = 0;
	cudaEventElapsedTime(&elapsed, start, stop);
	cudaEventDestroy(start);
	cudaEventDestroy(stop);

	printf("Tempo per la somma di matrici CPU:%f ms\n", elapsed);
	//fare il metodo di controllo controllo 
	cudaMemcpy(rHost, cDevice, size, cudaMemcpyDeviceToHost);
#ifdef DEBUG_MATRIX
	printf("Host\n");
	printMatrix(cHost, N, N);
	printf("Device\n");
	printMatrix(rHost, N, N);
#endif 
	
	matrixEqualCheck(cHost,rHost,N,N);

	//De-allocazione eventi
	cudaEventDestroy(start); cudaEventDestroy(stop);
	//De-allocazione Host
	free(aHost); free(bHost); free(cHost); free(rHost);
	//De-allocazione Device
	cudaFree(aDevice); cudaFree(bDevice); cudaFree(cDevice);

}


void initializeMatrix(int *array, int matrixSize)
{
	int i;
	for ( i = 0; i < matrixSize; i++)
	{
		array[i] = i;
	}
	
}

__global__ void sumMatrixGPU(int *a, int*b, int *c, int rows, int cols)
{
	//definire id per il controllo
	int idx = (blockDim.x*blockIdx.x) + threadIdx.x;
	int idy = (blockDim.y*blockIdx.y) + threadIdx.y;
	if (idx < rows && idy < cols)
	{
		//Essendo che rows e cols sono uguli li possiamo usare tranquillamente in maniera intercambiabile
		c[(idx*cols) + idy] = a[(idx*cols) + idy] + b[(idx*cols) + idy];
	}
	
}

void printMatrix(int *matrix, int rows, int cols)
{
	int i, j;

	if (cols < 10 && rows < 10)
	{
		for (i = 0; i < rows; i++)
		{
			for (j = 0; j < cols; j++)
			{
				printf("%d ", matrix[(i*cols) + j]);
			}
			printf("\n");
		}
		printf("\n");
	}
	printf("\n");

}

void sumMatrixCPU(int *a, int*b, int *c, int rows, int cols)
{
	int i, size;
	size = rows * cols;
	for (i = 0; i < size; i++)
	{
		c[i] = a[i] + b[i];
	}
}

void matrixEqualCheck(int *mHost, int *mDevice, int cols, int rows)
{
	int i, size, count = 0;
	size = cols * rows;

	for ( i = 0; i < size; i++)
	{
		if (mHost[i] == mDevice[i])
		{
			count++;
		}
	}

	if (count == size)
	{
		printf("Le matrici sono uguali\n");
	}
	else
	{
		printf("Le matrici sono diverse\n");
	}
}

	.file	"tmpxft_0028a26a_00000000-6_Esercitazione5MNIPunto2.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z16initializeMatrixPii
	.type	_Z16initializeMatrixPii, @function
_Z16initializeMatrixPii:
.LFB2028:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L3:
	cmpl	%eax, %esi
	jle	.L6
	movl	%eax, (%rdi,%rax,4)
	incq	%rax
	jmp	.L3
.L6:
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z16initializeMatrixPii, .-_Z16initializeMatrixPii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d "
.LC1:
	.string	"\n"
	.text
	.globl	_Z11printMatrixPiii
	.type	_Z11printMatrixPiii, @function
_Z11printMatrixPiii:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	cmpl	$9, %edx
	jg	.L8
	movl	%esi, %ebp
	cmpl	$9, %esi
	jg	.L8
	movq	%rdi, %r12
	movl	%edx, %ebx
	xorl	%r13d, %r13d
	xorl	%r14d, %r14d
.L9:
	cmpl	%ebp, %r14d
	jge	.L11
	movslq	%r13d, %rax
	xorl	%r15d, %r15d
	leaq	(%r12,%rax,4), %r8
.L12:
	cmpl	%r15d, %ebx
	jle	.L20
	movl	(%r8,%r15,4), %edx
	leaq	.LC0(%rip), %rsi
	xorl	%eax, %eax
	incq	%r15
	movl	$2, %edi
	movq	%r8, 8(%rsp)
	call	__printf_chk@PLT
	movq	8(%rsp), %r8
	jmp	.L12
.L20:
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	incl	%r14d
	addl	%ebx, %r13d
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L9
.L11:
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L8:
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z11printMatrixPiii, .-_Z11printMatrixPiii
	.globl	_Z12sumMatrixCPUPiS_S_ii
	.type	_Z12sumMatrixCPUPiS_S_ii, @function
_Z12sumMatrixCPUPiS_S_ii:
.LFB2030:
	.cfi_startproc
	endbr64
	imull	%r8d, %ecx
	xorl	%eax, %eax
.L22:
	cmpl	%eax, %ecx
	jle	.L24
	movl	(%rsi,%rax,4), %r8d
	addl	(%rdi,%rax,4), %r8d
	movl	%r8d, (%rdx,%rax,4)
	incq	%rax
	jmp	.L22
.L24:
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z12sumMatrixCPUPiS_S_ii, .-_Z12sumMatrixCPUPiS_S_ii
	.section	.rodata.str1.1
.LC2:
	.string	"Le matrici sono uguali\n"
.LC3:
	.string	"Le matrici sono diverse\n"
	.text
	.globl	_Z16matrixEqualCheckPiS_ii
	.type	_Z16matrixEqualCheckPiS_ii, @function
_Z16matrixEqualCheckPiS_ii:
.LFB2031:
	.cfi_startproc
	endbr64
	imull	%ecx, %edx
	xorl	%eax, %eax
	xorl	%ecx, %ecx
.L26:
	cmpl	%eax, %edx
	jle	.L31
	movl	(%rsi,%rax,4), %r8d
	cmpl	%r8d, (%rdi,%rax,4)
	jne	.L27
	incl	%ecx
.L27:
	incq	%rax
	jmp	.L26
.L31:
	leaq	.LC2(%rip), %rsi
	cmpl	%edx, %ecx
	je	.L30
	leaq	.LC3(%rip), %rsi
.L30:
	movl	$2, %edi
	xorl	%eax, %eax
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2031:
	.size	_Z16matrixEqualCheckPiS_ii, .-_Z16matrixEqualCheckPiS_ii
	.globl	_Z38__device_stub__Z12sumMatrixGPUPiS_S_iiPiS_S_ii
	.type	_Z38__device_stub__Z12sumMatrixGPUPiS_S_iiPiS_S_ii, @function
_Z38__device_stub__Z12sumMatrixGPUPiS_S_iiPiS_S_ii:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 4(%rsp)
	leaq	40(%rsp), %rcx
	movl	%r8d, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 120(%rsp)
	movq	%rsp, %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L32
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z12sumMatrixGPUPiS_S_ii(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L32:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L34
	call	__stack_chk_fail@PLT
.L34:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z38__device_stub__Z12sumMatrixGPUPiS_S_iiPiS_S_ii, .-_Z38__device_stub__Z12sumMatrixGPUPiS_S_iiPiS_S_ii
	.globl	_Z12sumMatrixGPUPiS_S_ii
	.type	_Z12sumMatrixGPUPiS_S_ii, @function
_Z12sumMatrixGPUPiS_S_ii:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z38__device_stub__Z12sumMatrixGPUPiS_S_iiPiS_S_ii
	.cfi_endproc
.LFE2057:
	.size	_Z12sumMatrixGPUPiS_S_ii, .-_Z12sumMatrixGPUPiS_S_ii
	.section	.rodata.str1.1
.LC4:
	.string	"Inserisci la size della matrice quadrata(N):"
.LC5:
	.string	"%d"
.LC6:
	.string	"Inserisci la size dei blocchi di thread Nt (NtxNt):"
.LC7:
	.string	"Size della matrice: %d\n"
.LC8:
	.string	"Numero totale di blocchi: %d (%d,%d)\n"
.LC9:
	.string	"Numero totale dei Thread per blocco: %d (%d,%d)\n"
.LC10:
	.string	"Tempo per la somma di matrici GPU:%f ms\n"
.LC12:
	.string	"Tempo per la somma di matrici CPU:%f ms\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	leaq	.LC5(%rip), %rbx
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	movl	$1, 88(%rsp)
	leaq	64(%rsp), %r15
	movabsq	$4294967297, %rax
	movq	%rax, 92(%rsp)
	xorl	%eax, %eax
	leaq	72(%rsp), %r14
	movl	$1, 100(%rsp)
	call	__printf_chk@PLT
	movq	stdout(%rip), %rdi
	call	fflush@PLT
	movq	%rbx, %rdi
	leaq	32(%rsp), %rsi
	xorl	%eax, %eax
	call	__isoc23_scanf@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	stdout(%rip), %rdi
	call	fflush@PLT
	movq	%rbx, %rdi
	leaq	92(%rsp), %rsi
	xorl	%eax, %eax
	call	__isoc23_scanf@PLT
	movl	32(%rsp), %eax
	movl	92(%rsp), %ecx
	xorl	%edx, %edx
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	movl	%eax, %r13d
	movl	%ecx, 96(%rsp)
	imull	%eax, %r13d
	divl	%ecx
	leal	0(,%r13,4), %ebx
	movslq	%ebx, %rbx
	cmpl	$1, %edx
	movl	%r13d, %edx
	sbbl	$-1, %eax
	movl	%eax, %ebp
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebp, %edx
	movl	%ebp, %r8d
	movl	%ebp, %ecx
	imull	%ebp, %edx
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	96(%rsp), %r8d
	movl	92(%rsp), %ecx
	xorl	%eax, %eax
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	movl	%r8d, %edx
	imull	%ecx, %edx
	call	__printf_chk@PLT
	movq	%rbx, %rdi
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r12
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	movslq	%r13d, %rdi
	movl	$4, %esi
	movq	%rax, 16(%rsp)
	call	calloc@PLT
	leaq	40(%rsp), %rdi
	movq	%rbx, %rsi
	movq	%rax, 24(%rsp)
	call	cudaMalloc@PLT
	leaq	48(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	56(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	movq	%rbx, %rdx
	call	cudaMemset@PLT
	movl	%r13d, %esi
	movq	%r12, %rdi
	movl	$1, %ecx
	call	_Z16initializeMatrixPii
	movq	8(%rsp), %rdi
	movq	%rbx, %rdx
	call	_Z16initializeMatrixPii
	movq	40(%rsp), %rdi
	movq	%r12, %rsi
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rsi
	movq	48(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	%r15, %rdi
	call	cudaEventCreate@PLT
	movq	%r14, %rdi
	call	cudaEventCreate@PLT
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	%ebp, 80(%rsp)
	movl	100(%rsp), %ecx
	xorl	%r9d, %r9d
	movq	92(%rsp), %rdx
	movl	88(%rsp), %esi
	movl	%ebp, 84(%rsp)
	xorl	%r8d, %r8d
	movq	80(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L38
	movl	32(%rsp), %ecx
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	movq	40(%rsp), %rdi
	movl	%ecx, %r8d
	call	_Z38__device_stub__Z12sumMatrixGPUPiS_S_iiPiS_S_ii
.L38:
	movq	72(%rsp), %rdi
	xorl	%esi, %esi
	leaq	36(%rsp), %r13
	call	cudaEventRecord@PLT
	movq	72(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	72(%rsp), %rdx
	movq	64(%rsp), %rsi
	movq	%r13, %rdi
	call	cudaEventElapsedTime@PLT
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	72(%rsp), %rdi
	call	cudaEventDestroy@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	36(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	%r15, %rdi
	call	cudaEventCreate@PLT
	movq	%r14, %rdi
	call	cudaEventCreate@PLT
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	32(%rsp), %ecx
	movq	24(%rsp), %rdx
	movq	%r12, %rdi
	movq	8(%rsp), %rsi
	movl	%ecx, %r8d
	call	_Z12sumMatrixCPUPiS_S_ii
	movq	72(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	72(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	72(%rsp), %rdx
	movq	%r13, %rdi
	movq	64(%rsp), %rsi
	movl	$0x00000000, 36(%rsp)
	call	cudaEventElapsedTime@PLT
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	72(%rsp), %rdi
	call	cudaEventDestroy@PLT
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	36(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	56(%rsp), %rsi
	movq	16(%rsp), %rdi
	movl	$2, %ecx
	movq	%rbx, %rdx
	call	cudaMemcpy@PLT
	movl	32(%rsp), %edx
	movq	16(%rsp), %rsi
	movq	24(%rsp), %rdi
	movl	%edx, %ecx
	call	_Z16matrixEqualCheckPiS_ii
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	72(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	8(%rsp), %rdi
	call	free@PLT
	movq	24(%rsp), %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	free@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L39
	call	__stack_chk_fail@PLT
.L39:
	addq	$120, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC13:
	.string	"_Z12sumMatrixGPUPiS_S_ii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC13(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z12sumMatrixGPUPiS_S_ii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2059:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#define VS
//#define MGPU

#ifdef VS
	#include "hip/hip_runtime.h"
	

#endif

#ifdef MGPU
	
#endif

#include <stdio.h>
#include<stdlib.h>

//#define DEBUG_MATRIX
#define DEBUG


//Dal profiler nvida per il kernel vengono usati 13 registri (si deve passare sulla multiGPU)

void initializeMatrix(int *array, int matrixSize);
void printMatrix(int *matrix, int rows, int cols);
__global__ void sumMatrixGPU(int *a, int*b, int *c, int rows, int cols);
void sumMatrixCPU(int *a, int*b, int *c, int rows, int cols);
void matrixEqualCheck(int *mHost, int *mDevice, int cols, int rows);
int main()
{
	int *aHost, *bHost, *cHost, *rHost;
	int *aDevice, *bDevice, *cDevice;
	int size, N;
	dim3 gridDim, blockDim;
	int matrixSize;
	hipEvent_t start, stop;
	float elapsed;


	printf("Inserisci la size della matrice quadrata(N):");
	fflush(stdout);
	scanf("%d",&N);
	
	printf("Inserisci la size dei blocchi di thread Nt (NtxNt):");
	fflush(stdout);
	scanf("%d", &blockDim.x);

	blockDim.y = blockDim.x;
	
	matrixSize = N * N;

	//Determino il numero esatto di blocchi 
	gridDim.x = N / blockDim.x + ((N % blockDim.x) == 0 ? 0 : 1);
	gridDim.y = N / blockDim.y + ((N % blockDim.y) == 0 ? 0 : 1);

	size = matrixSize * sizeof(int);

#ifdef DEBUG
	printf("Size della matrice: %d\n", matrixSize);
	printf("Numero totale di blocchi: %d (%d,%d)\n",gridDim.x*gridDim.y, gridDim.x, gridDim.y);
	printf("Numero totale dei Thread per blocco: %d (%d,%d)\n",blockDim.x*blockDim.y, blockDim.x, blockDim.y);
#endif 

	//Alloco la memoria sull' host
	aHost = (int*)malloc(size);
	bHost = (int*)malloc(size);
	rHost = (int*)malloc(size);
	cHost = (int*)calloc(matrixSize, sizeof(int)); //Azzeriamo gli array che raccolgono il risultato(lo inizializzo a zero)

	//Alloco memoria sul Device
	hipMalloc((void **)&aDevice, size);
	hipMalloc((void **)&bDevice, size);
	hipMalloc((void **)&cDevice, size);

	//Azzeriamo gli array che raccolgono il risultato
	hipMemset(cDevice, 0, size);

	//inizializzo le matrici
	initializeMatrix(aHost, matrixSize);
	initializeMatrix(bHost, matrixSize);

	//copio i dati dall'host al device
	hipMemcpy(aDevice, aHost, size, hipMemcpyHostToDevice);
	hipMemcpy(bDevice, bHost, size, hipMemcpyHostToDevice);

	//Somma parallela
	hipEventCreate(&start);
	hipEventCreate(&stop);
	hipEventRecord(start, 0);

	sumMatrixGPU <<<gridDim,blockDim>>> (aDevice, bDevice, cDevice, N, N);
	
	hipEventRecord(stop, 0);
	hipEventSynchronize(stop);

	hipEventElapsedTime(&elapsed, start, stop);
	//De-allocazione eventi
	hipEventDestroy(start); 
	hipEventDestroy(stop);
	
	printf("Tempo per la somma di matrici GPU:%f ms\n", elapsed);

	//Somma Seriale
	hipEventCreate(&start);
	hipEventCreate(&stop);
	hipEventRecord(start, 0);

	sumMatrixCPU(aHost, bHost, cHost, N, N);
	
	hipEventRecord(stop, 0);
	hipEventSynchronize(stop);

	elapsed = 0;
	hipEventElapsedTime(&elapsed, start, stop);
	hipEventDestroy(start);
	hipEventDestroy(stop);

	printf("Tempo per la somma di matrici CPU:%f ms\n", elapsed);
	//fare il metodo di controllo controllo 
	hipMemcpy(rHost, cDevice, size, hipMemcpyDeviceToHost);
#ifdef DEBUG_MATRIX
	printf("Host\n");
	printMatrix(cHost, N, N);
	printf("Device\n");
	printMatrix(rHost, N, N);
#endif 
	
	matrixEqualCheck(cHost,rHost,N,N);

	//De-allocazione eventi
	hipEventDestroy(start); hipEventDestroy(stop);
	//De-allocazione Host
	free(aHost); free(bHost); free(cHost); free(rHost);
	//De-allocazione Device
	hipFree(aDevice); hipFree(bDevice); hipFree(cDevice);

}


void initializeMatrix(int *array, int matrixSize)
{
	int i;
	for ( i = 0; i < matrixSize; i++)
	{
		array[i] = i;
	}
	
}

__global__ void sumMatrixGPU(int *a, int*b, int *c, int rows, int cols)
{
	//definire id per il controllo
	int idx = (blockDim.x*blockIdx.x) + threadIdx.x;
	int idy = (blockDim.y*blockIdx.y) + threadIdx.y;
	if (idx < rows && idy < cols)
	{
		//Essendo che rows e cols sono uguli li possiamo usare tranquillamente in maniera intercambiabile
		c[(idx*cols) + idy] = a[(idx*cols) + idy] + b[(idx*cols) + idy];
	}
	
}

void printMatrix(int *matrix, int rows, int cols)
{
	int i, j;

	if (cols < 10 && rows < 10)
	{
		for (i = 0; i < rows; i++)
		{
			for (j = 0; j < cols; j++)
			{
				printf("%d ", matrix[(i*cols) + j]);
			}
			printf("\n");
		}
		printf("\n");
	}
	printf("\n");

}

void sumMatrixCPU(int *a, int*b, int *c, int rows, int cols)
{
	int i, size;
	size = rows * cols;
	for (i = 0; i < size; i++)
	{
		c[i] = a[i] + b[i];
	}
}

void matrixEqualCheck(int *mHost, int *mDevice, int cols, int rows)
{
	int i, size, count = 0;
	size = cols * rows;

	for ( i = 0; i < size; i++)
	{
		if (mHost[i] == mDevice[i])
		{
			count++;
		}
	}

	if (count == size)
	{
		printf("Le matrici sono uguali\n");
	}
	else
	{
		printf("Le matrici sono diverse\n");
	}
}

	.text
	.file	"Esercitazione5MNIPunto2.hip"
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$104, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movabsq	$4294967297, %rax               # imm = 0x100000001
	leaq	88(%rsp), %rbx
	movq	%rax, (%rbx)
	movl	$1, 8(%rbx)
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	movq	stdout(%rip), %rdi
	callq	fflush
	leaq	12(%rsp), %r14
	movl	$.L.str.1, %edi
	movq	%r14, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	movq	stdout(%rip), %rdi
	callq	fflush
	movl	$.L.str.1, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	(%rbx), %ecx
	movl	%ecx, 4(%rbx)
	movl	(%r14), %esi
	movl	%esi, 68(%rsp)                  # 4-byte Spill
	movl	%esi, %eax
	xorl	%edx, %edx
	divl	%ecx
	movl	%eax, %r15d
	movl	%esi, %r14d
	imull	%r14d, %r14d
	cmpl	$1, %edx
	sbbl	$-1, %r15d
	movq	%r15, %rax
	shlq	$32, %rax
	orq	%r15, %rax
	movq	%rax, 72(%rsp)                  # 8-byte Spill
	leal	(,%r14,4), %ebp
	movl	$.L.str.3, %edi
	movl	%r14d, %esi
	xorl	%eax, %eax
	callq	printf
	movl	%r15d, %esi
	imull	%r15d, %esi
	movl	$.L.str.4, %edi
	movl	%r15d, %edx
	movl	%r15d, %ecx
	xorl	%eax, %eax
	callq	printf
	movl	(%rbx), %edx
	movl	4(%rbx), %ecx
	movl	%ecx, %esi
	imull	%edx, %esi
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	movslq	%ebp, %r13
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, %rbx
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, %r15
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	movl	$4, %esi
	movq	%r14, %rdi
	callq	calloc
	movq	%rax, %r12
	leaq	56(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	leaq	48(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	leaq	40(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	movq	(%rbp), %rdi
	xorl	%esi, %esi
	movq	%r13, %rdx
	callq	hipMemset
	cmpl	$0, 68(%rsp)                    # 4-byte Folded Reload
	je	.LBB0_5
# %bb.1:                                # %.lr.ph.i.preheader
	xorl	%eax, %eax
.LBB0_2:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movl	%eax, (%rbx,%rax,4)
	incq	%rax
	cmpq	%rax, %r14
	jne	.LBB0_2
# %bb.3:                                # %.lr.ph.i37.preheader
	xorl	%eax, %eax
.LBB0_4:                                # %.lr.ph.i37
                                        # =>This Inner Loop Header: Depth=1
	movl	%eax, (%r15,%rax,4)
	incq	%rax
	cmpq	%rax, %r14
	jne	.LBB0_4
.LBB0_5:                                # %_Z16initializeMatrixPii.exit41
	movq	56(%rsp), %rdi
	movq	%rbx, %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	48(%rsp), %rdi
	movq	%r15, %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	32(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventCreate
	leaq	16(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r14), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	88(%rsp), %rdx
	movl	96(%rsp), %ecx
	movq	72(%rsp), %rdi                  # 8-byte Reload
	movl	$1, %esi
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB0_7
# %bb.6:
	movq	56(%rsp), %rdi
	movq	48(%rsp), %rsi
	movq	40(%rsp), %rdx
	movl	12(%rsp), %ecx
	movl	%ecx, %r8d
	callq	_Z27__device_stub__sumMatrixGPUPiS_S_ii
.LBB0_7:
	leaq	16(%rsp), %r14
	movq	(%r14), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%r14), %rdi
	callq	hipEventSynchronize
	leaq	32(%rsp), %rbp
	movq	(%rbp), %rsi
	movq	(%r14), %rdx
	leaq	28(%rsp), %rdi
	callq	hipEventElapsedTime
	movq	(%rbp), %rdi
	callq	hipEventDestroy
	movq	(%r14), %rdi
	callq	hipEventDestroy
	leaq	28(%rsp), %rax
	cvtss2sd	(%rax), %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	movq	%rbp, %rdi
	callq	hipEventCreate
	movq	%r14, %rdi
	callq	hipEventCreate
	movq	(%rbp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	12(%rsp), %eax
	testl	%eax, %eax
	je	.LBB0_10
# %bb.8:                                # %.lr.ph.preheader.i42
	imull	%eax, %eax
	xorl	%ecx, %ecx
.LBB0_9:                                # %.lr.ph.i44
                                        # =>This Inner Loop Header: Depth=1
	movl	(%r15,%rcx,4), %edx
	addl	(%rbx,%rcx,4), %edx
	movl	%edx, (%r12,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB0_9
.LBB0_10:                               # %_Z12sumMatrixCPUPiS_S_ii.exit
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	16(%rsp), %rdi
	callq	hipEventSynchronize
	leaq	28(%rsp), %r14
	movl	$0, (%r14)
	movq	32(%rsp), %rsi
	movq	16(%rsp), %rdx
	movq	%r14, %rdi
	callq	hipEventElapsedTime
	movq	32(%rsp), %rdi
	callq	hipEventDestroy
	movq	16(%rsp), %rdi
	callq	hipEventDestroy
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r14), %xmm0
	movl	$.L.str.7, %edi
	movb	$1, %al
	callq	printf
	movq	40(%rsp), %rsi
	movq	80(%rsp), %r14                  # 8-byte Reload
	movq	%r14, %rdi
	movq	%r13, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	12(%rsp), %ecx
	movq	%r12, %rdi
	movq	%r14, %rsi
	movl	%ecx, %edx
	callq	_Z16matrixEqualCheckPiS_ii
	movq	32(%rsp), %rdi
	callq	hipEventDestroy
	movq	16(%rsp), %rdi
	callq	hipEventDestroy
	movq	%rbx, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	%r12, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	56(%rsp), %rdi
	callq	hipFree
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$104, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z16initializeMatrixPii         # -- Begin function _Z16initializeMatrixPii
	.type	_Z16initializeMatrixPii,@function
_Z16initializeMatrixPii:                # @_Z16initializeMatrixPii
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB1_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %eax
	xorl	%ecx, %ecx
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%ecx, (%rdi,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB1_2
.LBB1_3:                                # %._crit_edge
	retq
.Lfunc_end1:
	.size	_Z16initializeMatrixPii, .Lfunc_end1-_Z16initializeMatrixPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z27__device_stub__sumMatrixGPUPiS_S_ii # -- Begin function _Z27__device_stub__sumMatrixGPUPiS_S_ii
	.type	_Z27__device_stub__sumMatrixGPUPiS_S_ii,@function
_Z27__device_stub__sumMatrixGPUPiS_S_ii: # @_Z27__device_stub__sumMatrixGPUPiS_S_ii
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$128, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	movq	%rsp, %rcx
	movl	%r8d, (%rcx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z12sumMatrixGPUPiS_S_ii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z27__device_stub__sumMatrixGPUPiS_S_ii, .Lfunc_end2-_Z27__device_stub__sumMatrixGPUPiS_S_ii
	.cfi_endproc
                                        # -- End function
	.globl	_Z12sumMatrixCPUPiS_S_ii        # -- Begin function _Z12sumMatrixCPUPiS_S_ii
	.type	_Z12sumMatrixCPUPiS_S_ii,@function
_Z12sumMatrixCPUPiS_S_ii:               # @_Z12sumMatrixCPUPiS_S_ii
	.cfi_startproc
# %bb.0:
	imull	%r8d, %ecx
	testl	%ecx, %ecx
	jle	.LBB3_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%ecx, %eax
	xorl	%ecx, %ecx
.LBB3_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rsi,%rcx,4), %r8d
	addl	(%rdi,%rcx,4), %r8d
	movl	%r8d, (%rdx,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB3_2
.LBB3_3:                                # %._crit_edge
	retq
.Lfunc_end3:
	.size	_Z12sumMatrixCPUPiS_S_ii, .Lfunc_end3-_Z12sumMatrixCPUPiS_S_ii
	.cfi_endproc
                                        # -- End function
	.globl	_Z16matrixEqualCheckPiS_ii      # -- Begin function _Z16matrixEqualCheckPiS_ii
	.type	_Z16matrixEqualCheckPiS_ii,@function
_Z16matrixEqualCheckPiS_ii:             # @_Z16matrixEqualCheckPiS_ii
	.cfi_startproc
# %bb.0:
	imull	%ecx, %edx
	testl	%edx, %edx
	jle	.LBB4_1
# %bb.2:                                # %.lr.ph.preheader
	movl	%edx, %eax
	xorl	%r8d, %r8d
	xorl	%ecx, %ecx
.LBB4_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rdi,%r8,4), %r9d
	xorl	%r10d, %r10d
	cmpl	(%rsi,%r8,4), %r9d
	sete	%r10b
	addl	%r10d, %ecx
	incq	%r8
	cmpq	%r8, %rax
	jne	.LBB4_3
	jmp	.LBB4_4
.LBB4_1:
	xorl	%ecx, %ecx
.LBB4_4:                                # %._crit_edge
	cmpl	%edx, %ecx
	movl	$.Lstr.1, %eax
	movl	$.Lstr, %edi
	cmoveq	%rax, %rdi
	jmp	puts@PLT                        # TAILCALL
.Lfunc_end4:
	.size	_Z16matrixEqualCheckPiS_ii, .Lfunc_end4-_Z16matrixEqualCheckPiS_ii
	.cfi_endproc
                                        # -- End function
	.globl	_Z11printMatrixPiii             # -- Begin function _Z11printMatrixPiii
	.type	_Z11printMatrixPiii,@function
_Z11printMatrixPiii:                    # @_Z11printMatrixPiii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdi, 8(%rsp)                   # 8-byte Spill
	cmpl	$9, %esi
	jg	.LBB5_9
# %bb.1:
	movl	%edx, %ebx
	cmpl	$9, %edx
	jg	.LBB5_9
# %bb.2:                                # %.preheader17
	testl	%esi, %esi
	jle	.LBB5_8
# %bb.3:                                # %.preheader.lr.ph
	movl	%esi, %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	%ebx, %r12d
	xorl	%ebp, %ebp
	xorl	%r13d, %r13d
.LBB5_4:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB5_6 Depth 2
	testl	%ebx, %ebx
	jle	.LBB5_7
# %bb.5:                                # %.lr.ph
                                        #   in Loop: Header=BB5_4 Depth=1
	movl	%ebp, %eax
	movq	8(%rsp), %rcx                   # 8-byte Reload
	leaq	(%rcx,%rax,4), %r14
	xorl	%r15d, %r15d
.LBB5_6:                                #   Parent Loop BB5_4 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%r14,%r15,4), %esi
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r15
	cmpq	%r15, %r12
	jne	.LBB5_6
.LBB5_7:                                # %._crit_edge
                                        #   in Loop: Header=BB5_4 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r13
	addl	%ebx, %ebp
	cmpq	16(%rsp), %r13                  # 8-byte Folded Reload
	jne	.LBB5_4
.LBB5_8:                                # %._crit_edge20
	movl	$10, %edi
	callq	putchar@PLT
.LBB5_9:
	movl	$10, %edi
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	putchar@PLT                     # TAILCALL
.Lfunc_end5:
	.size	_Z11printMatrixPiii, .Lfunc_end5-_Z11printMatrixPiii
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z12sumMatrixGPUPiS_S_ii, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB7_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Inserisci la size della matrice quadrata(N):"
	.size	.L.str, 45

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"%d"
	.size	.L.str.1, 3

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Inserisci la size dei blocchi di thread Nt (NtxNt):"
	.size	.L.str.2, 52

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Size della matrice: %d\n"
	.size	.L.str.3, 24

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Numero totale di blocchi: %d (%d,%d)\n"
	.size	.L.str.4, 38

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Numero totale dei Thread per blocco: %d (%d,%d)\n"
	.size	.L.str.5, 49

	.type	_Z12sumMatrixGPUPiS_S_ii,@object # @_Z12sumMatrixGPUPiS_S_ii
	.section	.rodata,"a",@progbits
	.globl	_Z12sumMatrixGPUPiS_S_ii
	.p2align	3, 0x0
_Z12sumMatrixGPUPiS_S_ii:
	.quad	_Z27__device_stub__sumMatrixGPUPiS_S_ii
	.size	_Z12sumMatrixGPUPiS_S_ii, 8

	.type	.L.str.6,@object                # @.str.6
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.6:
	.asciz	"Tempo per la somma di matrici GPU:%f ms\n"
	.size	.L.str.6, 41

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Tempo per la somma di matrici CPU:%f ms\n"
	.size	.L.str.7, 41

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"%d "
	.size	.L.str.8, 4

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z12sumMatrixGPUPiS_S_ii"
	.size	.L__unnamed_1, 25

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Le matrici sono diverse"
	.size	.Lstr, 24

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Le matrici sono uguali"
	.size	.Lstr.1, 23

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z27__device_stub__sumMatrixGPUPiS_S_ii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z12sumMatrixGPUPiS_S_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z12sumMatrixGPUPiS_S_ii ; -- Begin function _Z12sumMatrixGPUPiS_S_ii
	.globl	_Z12sumMatrixGPUPiS_S_ii
	.p2align	8
	.type	_Z12sumMatrixGPUPiS_S_ii,@function
_Z12sumMatrixGPUPiS_S_ii:               ; @_Z12sumMatrixGPUPiS_S_ii
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x2c
	s_load_b64 s[2:3], s[0:1], 0x18
	v_and_b32_e32 v2, 0x3ff, v0
	v_bfe_u32 v3, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s5, s4, 16
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[0:1], null, s14, s4, v[2:3]
	v_mad_u64_u32 v[1:2], null, s15, s5, v[3:4]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_gt_i32_e32 vcc_lo, s2, v0
	v_cmp_gt_i32_e64 s2, s3, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, vcc_lo, s2
	s_and_saveexec_b32 s4, s2
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b128 s[4:7], s[0:1], 0x0
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[2:3], null, v0, s3, v[1:2]
	s_load_b64 s[0:1], s[0:1], 0x10
	v_ashrrev_i32_e32 v3, 31, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[2:3]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v2, vcc_lo, s4, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s5, v1, vcc_lo
	v_add_co_u32 v4, vcc_lo, s6, v0
	v_add_co_ci_u32_e32 v5, vcc_lo, s7, v1, vcc_lo
	v_add_co_u32 v0, vcc_lo, s0, v0
	global_load_b32 v2, v[2:3], off
	global_load_b32 v3, v[4:5], off
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v2, v3, v2
	global_store_b32 v[0:1], v2, off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z12sumMatrixGPUPiS_S_ii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.amdhsa_user_sgpr_count 14
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 6
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z12sumMatrixGPUPiS_S_ii, .Lfunc_end0-_Z12sumMatrixGPUPiS_S_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 236
; NumSgprs: 18
; NumVgprs: 6
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 6
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         28
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z12sumMatrixGPUPiS_S_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z12sumMatrixGPUPiS_S_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     6
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
