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

int main (int argc, char* argv[]){
	//variables
	int matDim;

	// get inputs
	if (argc < 2){
		std::cout << "Not enough arguments. <<matrix dimension>>" << std::endl; 
		return 1;
	}
	else{
	       matDim = atoi (argv [1]);
	}

	//create arrays
	float *MatA = new float[(int)pow(matDim, 2)];
	float *MatB = new float[(int)pow(matDim, 2)]; 
	float *MatC = new float[(int)pow(matDim, 2)];
	
	//load
	for (int i=0; i < (int)pow(matDim, 2); i++) {
 		MatA[i] = (float) i;
 		MatB[i] = (float) i;
 	}


	// begin timing
 	cudaEvent_t start, end;
  	cudaEventCreate(&start);
  	cudaEventCreate(&end);

 	cudaEventRecord( start, 0 );

   	for (int i = 0; i < matDim; i++) {
      		for (int j = 0; j < matDim; j++) {
			float sum = 0.0;
        		for (int k = 0; k < matDim; k++) {
         			 sum = sum + MatA[i*matDim + k]*MatB[k*matDim+j];
       			}
 
       		 	MatC [i*matDim + j] = sum;
     	 	}

  	}

	//end time
	cudaEventRecord( end, 0 );
  	cudaEventSynchronize( end );


	//output results
	/*for (int i = 0; i < matDim; i++){
		for (int j = 0; j < matDim; j++){
			printf ("%.2f	", MatC[(i*matDim)+j]); 
			//std::cout << MatC[(i*matDim)+j] << " ";
		}
	std::cout << std::endl;
	}*/

 	float elapsedTime;
  	cudaEventElapsedTime( &elapsedTime, start, end );

        std::cout << "Time: " << elapsedTime << " ms." << std::endl;

	//dealloc memory
	delete MatA;
	MatA = NULL;
	delete MatB;
	MatB = NULL;
	delete MatC;
	MatC = NULL;

	return 0;

}
	.file	"tmpxft_00356eac_00000000-6_sequential.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3638:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3638:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"Not enough arguments. <<matrix dimension>>"
.LC2:
	.string	"Time: "
.LC3:
	.string	" ms."
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$32, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	decl	%edi
	jg	.L3
	leaq	.LC1(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movl	$1, %eax
	jmp	.L2
.L3:
	movq	8(%rsi), %rdi
	call	atoi@PLT
	cvtsi2sdl	%eax, %xmm0
	movl	%eax, %ebx
	movabsq	$2305843009213693950, %rax
	mulsd	%xmm0, %xmm0
	cvttsd2sil	%xmm0, %r14d
	movslq	%r14d, %rbp
	cmpq	%rbp, %rax
	jb	.L5
	salq	$2, %rbp
	movq	%rbp, %rdi
	call	_Znam@PLT
	movq	%rbp, %rdi
	movq	%rax, %r13
	call	_Znam@PLT
	movq	%rbp, %rdi
	movq	%rax, %r12
	call	_Znam@PLT
	movq	%rax, %rbp
	xorl	%eax, %eax
	jmp	.L6
.L5:
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L17
	call	__cxa_throw_bad_array_new_length@PLT
.L6:
	cmpl	%eax, %r14d
	jle	.L19
	cvtsi2ssl	%eax, %xmm0
	movss	%xmm0, 0(%r13,%rax,4)
	movss	%xmm0, (%r12,%rax,4)
	incq	%rax
	jmp	.L6
.L19:
	leaq	8(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	16(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movslq	%ebx, %rcx
	xorl	%edx, %edx
	xorl	%edi, %edi
	salq	$2, %rcx
.L9:
	cmpl	%ebx, %edi
	jge	.L20
	xorl	%eax, %eax
	leaq	0(%r13,%rdx), %r9
	leaq	0(%rbp,%rdx), %r10
.L12:
	leaq	(%r12,%rax,4), %r8
	xorl	%esi, %esi
	xorps	%xmm0, %xmm0
.L10:
	movss	(%r9,%rsi,4), %xmm1
	mulss	(%r8), %xmm1
	incq	%rsi
	addq	%rcx, %r8
	addss	%xmm1, %xmm0
	cmpl	%esi, %ebx
	jg	.L10
	movss	%xmm0, (%r10,%rax,4)
	incq	%rax
	cmpl	%eax, %ebx
	jg	.L12
	incl	%edi
	addq	%rcx, %rdx
	jmp	.L9
.L20:
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	16(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rsi
	leaq	4(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC2(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	cvtss2sd	4(%rsp), %xmm0
	movq	%rax, %rdi
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	leaq	.LC3(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movl	$4, %esi
	movq	%r13, %rdi
	call	_ZdlPvm@PLT
	movl	$4, %esi
	movq	%r12, %rdi
	call	_ZdlPvm@PLT
	movl	$4, %esi
	movq	%rbp, %rdi
	call	_ZdlPvm@PLT
	xorl	%eax, %eax
.L2:
	movq	24(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L13
.L17:
	call	__stack_chk_fail@PLT
.L13:
	addq	$32, %rsp
	.cfi_def_cfa_offset 48
	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
.LFE3635:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3661:
	.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
.LFE3661:
	.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:


#include <hip/hip_runtime.h>
#include <iostream>
#include <stdlib.h> 
#include <cmath>
#include <stdio.h>

int main (int argc, char* argv[]){
	//variables
	int matDim;

	// get inputs
	if (argc < 2){
		std::cout << "Not enough arguments. <<matrix dimension>>" << std::endl; 
		return 1;
	}
	else{
	       matDim = atoi (argv [1]);
	}

	//create arrays
	float *MatA = new float[(int)pow(matDim, 2)];
	float *MatB = new float[(int)pow(matDim, 2)]; 
	float *MatC = new float[(int)pow(matDim, 2)];
	
	//load
	for (int i=0; i < (int)pow(matDim, 2); i++) {
 		MatA[i] = (float) i;
 		MatB[i] = (float) i;
 	}


	// begin timing
 	hipEvent_t start, end;
  	hipEventCreate(&start);
  	hipEventCreate(&end);

 	hipEventRecord( start, 0 );

   	for (int i = 0; i < matDim; i++) {
      		for (int j = 0; j < matDim; j++) {
			float sum = 0.0;
        		for (int k = 0; k < matDim; k++) {
         			 sum = sum + MatA[i*matDim + k]*MatB[k*matDim+j];
       			}
 
       		 	MatC [i*matDim + j] = sum;
     	 	}

  	}

	//end time
	hipEventRecord( end, 0 );
  	hipEventSynchronize( end );


	//output results
	/*for (int i = 0; i < matDim; i++){
		for (int j = 0; j < matDim; j++){
			printf ("%.2f	", MatC[(i*matDim)+j]); 
			//std::cout << MatC[(i*matDim)+j] << " ";
		}
	std::cout << std::endl;
	}*/

 	float elapsedTime;
  	hipEventElapsedTime( &elapsedTime, start, end );

        std::cout << "Time: " << elapsedTime << " ms." << std::endl;

	//dealloc memory
	delete MatA;
	MatA = NULL;
	delete MatB;
	MatB = NULL;
	delete MatC;
	MatC = NULL;

	return 0;

}
	.text
	.file	"sequential.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.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	$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
	cmpl	$1, %edi
	jg	.LBB0_2
# %bb.1:
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str, %esi
	movl	$42, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$1, %r12d
	jmp	.LBB0_13
.LBB0_2:
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %ebp
	cvtsi2sd	%eax, %xmm0
	mulsd	%xmm0, %xmm0
	cvttsd2si	%xmm0, %r12d
	movslq	%r12d, %r13
	leaq	(,%r13,4), %rax
	testl	%r13d, %r13d
	movq	$-1, %r15
	cmovnsq	%rax, %r15
	movq	%r15, %rdi
	callq	_Znam
	movq	%rax, %rbx
	movq	%r15, %rdi
	callq	_Znam
	movq	%rax, %r14
	movq	%r15, %rdi
	callq	_Znam
	movq	%rax, %r15
	testl	%r13d, %r13d
	jle	.LBB0_5
# %bb.3:                                # %.lr.ph.preheader
	movl	%r12d, %eax
	xorl	%ecx, %ecx
.LBB0_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtsi2ss	%ecx, %xmm0
	movss	%xmm0, (%rbx,%rcx,4)
	movss	%xmm0, (%r14,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB0_4
.LBB0_5:                                # %._crit_edge
	leaq	8(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventCreate
	movq	%rsp, %rdi
	callq	hipEventCreate
	movq	(%r12), %rdi
	xorl	%r12d, %r12d
	xorl	%esi, %esi
	callq	hipEventRecord
	testl	%ebp, %ebp
	jle	.LBB0_12
# %bb.6:                                # %.preheader49.lr.ph
	movl	%ebp, %eax
	leaq	(,%rax,4), %rcx
	xorl	%edx, %edx
	xorl	%esi, %esi
.LBB0_7:                                # %.preheader.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_8 Depth 2
                                        #       Child Loop BB0_9 Depth 3
	movl	%edx, %edi
	leaq	(%rbx,%rdi,4), %rdi
	movq	%rsi, %r8
	imulq	%rax, %r8
	leaq	(%r15,%r8,4), %r8
	movq	%r14, %r9
	xorl	%r10d, %r10d
.LBB0_8:                                # %.preheader
                                        #   Parent Loop BB0_7 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB0_9 Depth 3
	xorpd	%xmm0, %xmm0
	movq	%r9, %r11
	xorl	%r13d, %r13d
.LBB0_9:                                #   Parent Loop BB0_7 Depth=1
                                        #     Parent Loop BB0_8 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%rdi,%r13,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	mulss	(%r11), %xmm1
	addss	%xmm1, %xmm0
	incq	%r13
	addq	%rcx, %r11
	cmpq	%r13, %rax
	jne	.LBB0_9
# %bb.10:                               # %._crit_edge54
                                        #   in Loop: Header=BB0_8 Depth=2
	movss	%xmm0, (%r8,%r10,4)
	incq	%r10
	addq	$4, %r9
	cmpq	%rax, %r10
	jne	.LBB0_8
# %bb.11:                               # %._crit_edge56
                                        #   in Loop: Header=BB0_7 Depth=1
	incq	%rsi
	addl	%ebp, %edx
	cmpq	%rax, %rsi
	jne	.LBB0_7
.LBB0_12:                               # %._crit_edge58
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rsp), %rdi
	callq	hipEventSynchronize
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdx
	leaq	20(%rsp), %r13
	movq	%r13, %rdi
	callq	hipEventElapsedTime
	movl	$_ZSt4cout, %edi
	movl	$.L.str.1, %esi
	movl	$6, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r13), %xmm0
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	%rax, %r13
	movl	$.L.str.2, %esi
	movl	$4, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	(%r13), %rax
	movq	-24(%rax), %rdi
	addq	%r13, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r13, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movq	%rbx, %rdi
	callq	_ZdlPv
	movq	%r14, %rdi
	callq	_ZdlPv
	movq	%r15, %rdi
	callq	_ZdlPv
.LBB0_13:
	movl	%r12d, %eax
	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
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Not enough arguments. <<matrix dimension>>"
	.size	.L.str, 43

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Time: "
	.size	.L.str.1, 7

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	" ms."
	.size	.L.str.2, 5

	.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 _ZSt4cout
	.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
