#define DIM 8192
#include<stdio.h>
#include<stdlib.h>
#include <chrono>

//FUNZIONE PER STAMPARE UNA MATRICE
void printMatrix(double *M) {
	int i, j;
	for (i = 0; i < DIM; i++) {
		for (j = 0; j < DIM; j++) {
			printf("%f	", M[i * DIM + j]);
		}
		printf("\n");
	}
}

//FUNZIONE CHE RIEMPIE LA MATRICE DI NUMERI double CASUALI
void populateMatrix(double *M) {
	int i, j;
	for (i = 0; i < DIM; i++) {
		for (j = 0; j < DIM; j++) {
			M[i * DIM + j] = (double)(i + j);
		}
	}
}

void MatrixMulHost(double *A, double *B, double *C) {
	int c, d, k;
	for (c = 0; c < DIM; c++) {
		for (d = 0; d < DIM; d++) {
			int Pvalue = 0;
			for (k = 0; k < DIM; k++) {
				Pvalue += A[c * DIM + k] * B[k * DIM + d];
			}
			C[c *DIM + d] = Pvalue;
		}
	}
}


int main(){
    double *A = (double *)malloc(DIM * DIM * sizeof(double));
	double *B = (double *)malloc(DIM * DIM * sizeof(double));
	double *C = (double *)malloc(DIM * DIM * sizeof(double));

    populateMatrix(A);
    populateMatrix(B);
	std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();

	MatrixMulHost(A, B, C);
		
	std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
	double tempo = std::chrono::duration_cast<std::chrono::duration<double> >(end - start).count();

    printf("%f\n", tempo);
   // printMatrix(C);

}

	.file	"tmpxft_002a1ae7_00000000-6_MatrixMul.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2134:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2134:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%f\t"
.LC1:
	.string	"\n"
	.text
	.globl	_Z11printMatrixPd
	.type	_Z11printMatrixPd, @function
_Z11printMatrixPd:
.LFB2126:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	leaq	.LC0(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
.L3:
	movq	%rbp, %r12
	xorl	%ebx, %ebx
	salq	$16, %r12
	addq	%r13, %r12
.L4:
	movsd	(%r12,%rbx,8), %xmm0
	movq	%r14, %rsi
	movl	$2, %edi
	movb	$1, %al
	incq	%rbx
	call	__printf_chk@PLT
	cmpq	$8192, %rbx
	jne	.L4
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbp
	call	__printf_chk@PLT
	cmpq	$8192, %rbp
	jne	.L3
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2126:
	.size	_Z11printMatrixPd, .-_Z11printMatrixPd
	.globl	_Z14populateMatrixPd
	.type	_Z14populateMatrixPd, @function
_Z14populateMatrixPd:
.LFB2127:
	.cfi_startproc
	endbr64
	xorl	%edx, %edx
.L10:
	xorl	%eax, %eax
.L11:
	leal	(%rdx,%rax), %ecx
	cvtsi2sdl	%ecx, %xmm0
	movsd	%xmm0, (%rdi,%rax,8)
	incq	%rax
	cmpq	$8192, %rax
	jne	.L11
	incl	%edx
	addq	$65536, %rdi
	cmpl	$8192, %edx
	jne	.L10
	ret
	.cfi_endproc
.LFE2127:
	.size	_Z14populateMatrixPd, .-_Z14populateMatrixPd
	.globl	_Z13MatrixMulHostPdS_S_
	.type	_Z13MatrixMulHostPdS_S_, @function
_Z13MatrixMulHostPdS_S_:
.LFB2128:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %r8
	xorl	%eax, %eax
.L15:
	xorl	%ecx, %ecx
	leaq	(%r8,%rax), %rbx
	leaq	(%rdx,%rax), %r11
.L19:
	leaq	(%rsi,%rcx,8), %r10
	xorl	%edi, %edi
	xorl	%r9d, %r9d
.L16:
	movsd	(%rbx,%rdi,8), %xmm0
	mulsd	(%r10), %xmm0
	cvtsi2sdl	%r9d, %xmm1
	incq	%rdi
	addq	$65536, %r10
	addsd	%xmm1, %xmm0
	cvttsd2sil	%xmm0, %r9d
	cmpq	$8192, %rdi
	jne	.L16
	cvtsi2sdl	%r9d, %xmm0
	movsd	%xmm0, (%r11,%rcx,8)
	incq	%rcx
	cmpq	$8192, %rcx
	jne	.L19
	addq	$65536, %rax
	cmpq	$536870912, %rax
	jne	.L15
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2128:
	.size	_Z13MatrixMulHostPdS_S_, .-_Z13MatrixMulHostPdS_S_
	.section	.rodata.str1.1
.LC3:
	.string	"%f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2129:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	$536870912, %edi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	pushq	%rax
	.cfi_def_cfa_offset 48
	call	malloc@PLT
	movl	$536870912, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	movl	$536870912, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r13
	call	_Z14populateMatrixPd
	movq	%rbp, %rdi
	call	_Z14populateMatrixPd
	call	_ZNSt6chrono3_V212steady_clock3nowEv@PLT
	movq	%r13, %rdx
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	movq	%rax, %r12
	call	_Z13MatrixMulHostPdS_S_
	call	_ZNSt6chrono3_V212steady_clock3nowEv@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	subq	%r12, %rax
	cvtsi2sdq	%rax, %xmm0
	movb	$1, %al
	divsd	.LC2(%rip), %xmm0
	call	__printf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2129:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2157:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2157:
	.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
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC2:
	.long	0
	.long	1104006501
	.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 DIM 8192

#include <hip/hip_runtime.h>
#include<stdio.h>
#include<stdlib.h>
#include <chrono>

//FUNZIONE PER STAMPARE UNA MATRICE
void printMatrix(double *M) {
	int i, j;
	for (i = 0; i < DIM; i++) {
		for (j = 0; j < DIM; j++) {
			printf("%f	", M[i * DIM + j]);
		}
		printf("\n");
	}
}

//FUNZIONE CHE RIEMPIE LA MATRICE DI NUMERI double CASUALI
void populateMatrix(double *M) {
	int i, j;
	for (i = 0; i < DIM; i++) {
		for (j = 0; j < DIM; j++) {
			M[i * DIM + j] = (double)(i + j);
		}
	}
}

void MatrixMulHost(double *A, double *B, double *C) {
	int c, d, k;
	for (c = 0; c < DIM; c++) {
		for (d = 0; d < DIM; d++) {
			int Pvalue = 0;
			for (k = 0; k < DIM; k++) {
				Pvalue += A[c * DIM + k] * B[k * DIM + d];
			}
			C[c *DIM + d] = Pvalue;
		}
	}
}


int main(){
    double *A = (double *)malloc(DIM * DIM * sizeof(double));
	double *B = (double *)malloc(DIM * DIM * sizeof(double));
	double *C = (double *)malloc(DIM * DIM * sizeof(double));

    populateMatrix(A);
    populateMatrix(B);
	std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();

	MatrixMulHost(A, B, C);
		
	std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
	double tempo = std::chrono::duration_cast<std::chrono::duration<double> >(end - start).count();

    printf("%f\n", tempo);
   // printMatrix(C);

}

	.text
	.file	"MatrixMul.hip"
	.globl	_Z11printMatrixPd               # -- Begin function _Z11printMatrixPd
	.type	_Z11printMatrixPd,@function
_Z11printMatrixPd:                      # @_Z11printMatrixPd
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	xorl	%r14d, %r14d
.LBB0_1:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_2 Depth 2
	xorl	%r15d, %r15d
.LBB0_2:                                #   Parent Loop BB0_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movsd	(%rbx,%r15,8), %xmm0            # xmm0 = mem[0],zero
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	incq	%r15
	cmpq	$8192, %r15                     # imm = 0x2000
	jne	.LBB0_2
# %bb.3:                                #   in Loop: Header=BB0_1 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r14
	addq	$65536, %rbx                    # imm = 0x10000
	cmpq	$8192, %r14                     # imm = 0x2000
	jne	.LBB0_1
# %bb.4:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z11printMatrixPd, .Lfunc_end0-_Z11printMatrixPd
	.cfi_endproc
                                        # -- End function
	.globl	_Z14populateMatrixPd            # -- Begin function _Z14populateMatrixPd
	.type	_Z14populateMatrixPd,@function
_Z14populateMatrixPd:                   # @_Z14populateMatrixPd
	.cfi_startproc
# %bb.0:
	xorl	%eax, %eax
.LBB1_1:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_2 Depth 2
	xorl	%ecx, %ecx
.LBB1_2:                                #   Parent Loop BB1_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leal	(%rax,%rcx), %edx
	xorps	%xmm0, %xmm0
	cvtsi2sd	%edx, %xmm0
	movsd	%xmm0, (%rdi,%rcx,8)
	incq	%rcx
	cmpq	$8192, %rcx                     # imm = 0x2000
	jne	.LBB1_2
# %bb.3:                                #   in Loop: Header=BB1_1 Depth=1
	incq	%rax
	addq	$65536, %rdi                    # imm = 0x10000
	cmpq	$8192, %rax                     # imm = 0x2000
	jne	.LBB1_1
# %bb.4:
	retq
.Lfunc_end1:
	.size	_Z14populateMatrixPd, .Lfunc_end1-_Z14populateMatrixPd
	.cfi_endproc
                                        # -- End function
	.globl	_Z13MatrixMulHostPdS_S_         # -- Begin function _Z13MatrixMulHostPdS_S_
	.type	_Z13MatrixMulHostPdS_S_,@function
_Z13MatrixMulHostPdS_S_:                # @_Z13MatrixMulHostPdS_S_
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	xorl	%eax, %eax
.LBB2_1:                                # %.preheader19
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_2 Depth 2
                                        #       Child Loop BB2_3 Depth 3
	movq	%rax, %rcx
	shlq	$16, %rcx
	addq	%rdx, %rcx
	movq	%rsi, %r8
	xorl	%r9d, %r9d
.LBB2_2:                                # %.preheader
                                        #   Parent Loop BB2_1 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB2_3 Depth 3
	xorl	%r10d, %r10d
	movq	%r8, %r11
	xorl	%ebx, %ebx
.LBB2_3:                                #   Parent Loop BB2_1 Depth=1
                                        #     Parent Loop BB2_2 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movsd	(%rdi,%r10,8), %xmm0            # xmm0 = mem[0],zero
	mulsd	(%r11), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sd	%ebx, %xmm1
	addsd	%xmm0, %xmm1
	cvttsd2si	%xmm1, %ebx
	incq	%r10
	addq	$65536, %r11                    # imm = 0x10000
	cmpq	$8192, %r10                     # imm = 0x2000
	jne	.LBB2_3
# %bb.4:                                #   in Loop: Header=BB2_2 Depth=2
	xorps	%xmm0, %xmm0
	cvtsi2sd	%ebx, %xmm0
	movsd	%xmm0, (%rcx,%r9,8)
	incq	%r9
	addq	$8, %r8
	cmpq	$8192, %r9                      # imm = 0x2000
	jne	.LBB2_2
# %bb.5:                                #   in Loop: Header=BB2_1 Depth=1
	incq	%rax
	addq	$65536, %rdi                    # imm = 0x10000
	cmpq	$8192, %rax                     # imm = 0x2000
	jne	.LBB2_1
# %bb.6:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z13MatrixMulHostPdS_S_, .Lfunc_end2-_Z13MatrixMulHostPdS_S_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI3_0:
	.quad	0x41cdcd6500000000              # double 1.0E+9
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$536870912, %edi                # imm = 0x20000000
	callq	malloc
	movq	%rax, %rbx
	movl	$536870912, %edi                # imm = 0x20000000
	callq	malloc
	movq	%rax, %r14
	movl	$536870912, %edi                # imm = 0x20000000
	callq	malloc
	movq	%rax, %r15
	xorl	%eax, %eax
	movq	%rbx, %rcx
.LBB3_1:                                # %.preheader.i
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_2 Depth 2
	xorl	%edx, %edx
.LBB3_2:                                #   Parent Loop BB3_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leal	(%rax,%rdx), %esi
	xorps	%xmm0, %xmm0
	cvtsi2sd	%esi, %xmm0
	movsd	%xmm0, (%rcx,%rdx,8)
	incq	%rdx
	cmpq	$8192, %rdx                     # imm = 0x2000
	jne	.LBB3_2
# %bb.3:                                #   in Loop: Header=BB3_1 Depth=1
	incq	%rax
	addq	$65536, %rcx                    # imm = 0x10000
	cmpq	$8192, %rax                     # imm = 0x2000
	jne	.LBB3_1
# %bb.4:                                # %.preheader.i6.preheader
	xorl	%eax, %eax
	movq	%r14, %rcx
.LBB3_5:                                # %.preheader.i6
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_6 Depth 2
	xorl	%edx, %edx
.LBB3_6:                                #   Parent Loop BB3_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leal	(%rax,%rdx), %esi
	xorps	%xmm0, %xmm0
	cvtsi2sd	%esi, %xmm0
	movsd	%xmm0, (%rcx,%rdx,8)
	incq	%rdx
	cmpq	$8192, %rdx                     # imm = 0x2000
	jne	.LBB3_6
# %bb.7:                                #   in Loop: Header=BB3_5 Depth=1
	incq	%rax
	addq	$65536, %rcx                    # imm = 0x10000
	cmpq	$8192, %rax                     # imm = 0x2000
	jne	.LBB3_5
# %bb.8:                                # %_Z14populateMatrixPd.exit15
	callq	_ZNSt6chrono3_V212steady_clock3nowEv
	movq	%rax, %r12
	movq	%rbx, %rdi
	movq	%r14, %rsi
	movq	%r15, %rdx
	callq	_Z13MatrixMulHostPdS_S_
	callq	_ZNSt6chrono3_V212steady_clock3nowEv
	subq	%r12, %rax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI3_0(%rip), %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%f\t"
	.size	.L.str, 4

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%f\n"
	.size	.L.str.2, 4

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.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_

	.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_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
