#include <bits/stdc++.h>
using namespace std;

void add(int N, float *X, float *Y)
{
	for (int i=0; i<N; i++)
	{  
		Y[i] = X[i] + Y[i];
	}
}

int main(void)
{
	int N = 1<<27;//1.34217728 *10^8 elements. 512 MB
	
	//Allocate Memory (512*2=1GB). 
	float *X = new float[N];
    float *Y = new float[N];
	// initialize x and y arrays on the host
	for(int i = 0; i < N; i++) 
	{
		X[i] = 1.0f;
		Y[i] = 2.0f;
	}
	
	double avg=0;
	clock_t t;
	
	// Runs add 10 times on CPU
	for(int i=0;i<10;i++)
	{
		t=clock();//start time
		add(N, X, Y);
		t = clock() - t;//total time = end time - start time
		printf ("CPU RUN-%d time = %f ms.\n",i,(((float)t)/CLOCKS_PER_SEC)*1000);
		avg+=((((float)t)/CLOCKS_PER_SEC)*1000);//time is calculated in terms of clockcycle. Converted in millisecond
	}
	
	avg=avg/10;// average of 10 elements
	printf ("CPU Avg time = %lf ms.\n",avg);

	delete [] X;
	delete [] Y; 
	return 0;
}

	.file	"tmpxft_0028b462_00000000-6_1_tool_setup.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB10829:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE10829:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z3addiPfS_
	.type	_Z3addiPfS_, @function
_Z3addiPfS_:
.LFB10825:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L3:
	cmpl	%eax, %edi
	jle	.L6
	movss	(%rdx,%rax,4), %xmm0
	addss	(%rsi,%rax,4), %xmm0
	movss	%xmm0, (%rdx,%rax,4)
	incq	%rax
	jmp	.L3
.L6:
	ret
	.cfi_endproc
.LFE10825:
	.size	_Z3addiPfS_, .-_Z3addiPfS_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC5:
	.string	"CPU RUN-%d time = %f ms.\n"
.LC7:
	.string	"CPU Avg time = %lf ms.\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB10826:
	.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
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	call	_Znam@PLT
	movl	$536870912, %edi
	movq	%rax, %rbp
	call	_Znam@PLT
	movss	.LC1(%rip), %xmm1
	movss	.LC2(%rip), %xmm0
	movq	%rax, %rbx
	xorl	%eax, %eax
.L8:
	movss	%xmm1, 0(%rbp,%rax)
	movss	%xmm0, (%rbx,%rax)
	addq	$4, %rax
	cmpq	$536870912, %rax
	jne	.L8
	xorl	%r12d, %r12d
	xorps	%xmm1, %xmm1
.L9:
	movsd	%xmm1, 8(%rsp)
	call	clock@PLT
	movq	%rbx, %rdx
	movq	%rbp, %rsi
	movl	$134217728, %edi
	movq	%rax, %r13
	call	_Z3addiPfS_
	call	clock@PLT
	movl	%r12d, %edx
	movl	$2, %edi
	incl	%r12d
	subq	%r13, %rax
	leaq	.LC5(%rip), %rsi
	cvtsi2ssq	%rax, %xmm0
	divss	.LC3(%rip), %xmm0
	movb	$1, %al
	mulss	.LC4(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm2
	movaps	%xmm2, %xmm0
	movsd	%xmm2, (%rsp)
	call	__printf_chk@PLT
	movsd	8(%rsp), %xmm1
	addsd	(%rsp), %xmm1
	cmpl	$10, %r12d
	jne	.L9
	divsd	.LC6(%rip), %xmm1
	leaq	.LC7(%rip), %rsi
	movaps	%xmm1, %xmm0
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	movq	%rbp, %rdi
	call	_ZdaPv@PLT
	movq	%rbx, %rdi
	call	_ZdaPv@PLT
	addq	$24, %rsp
	.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
.LFE10826:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB10852:
	.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
.LFE10852:
	.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.cst4,"aM",@progbits,4
	.align 4
.LC1:
	.long	1065353216
	.align 4
.LC2:
	.long	1073741824
	.align 4
.LC3:
	.long	1232348160
	.align 4
.LC4:
	.long	1148846080
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC6:
	.long	0
	.long	1076101120
	.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 <bits/stdc++.h>
using namespace std;

void add(int N, float *X, float *Y)
{
	for (int i=0; i<N; i++)
	{  
		Y[i] = X[i] + Y[i];
	}
}

int main(void)
{
	int N = 1<<27;//1.34217728 *10^8 elements. 512 MB
	
	//Allocate Memory (512*2=1GB). 
	float *X = new float[N];
    float *Y = new float[N];
	// initialize x and y arrays on the host
	for(int i = 0; i < N; i++) 
	{
		X[i] = 1.0f;
		Y[i] = 2.0f;
	}
	
	double avg=0;
	clock_t t;
	
	// Runs add 10 times on CPU
	for(int i=0;i<10;i++)
	{
		t=clock();//start time
		add(N, X, Y);
		t = clock() - t;//total time = end time - start time
		printf ("CPU RUN-%d time = %f ms.\n",i,(((float)t)/CLOCKS_PER_SEC)*1000);
		avg+=((((float)t)/CLOCKS_PER_SEC)*1000);//time is calculated in terms of clockcycle. Converted in millisecond
	}
	
	avg=avg/10;// average of 10 elements
	printf ("CPU Avg time = %lf ms.\n",avg);

	delete [] X;
	delete [] Y; 
	return 0;
}

	.text
	.file	"1_tool_setup.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z3addiPfS_                     # -- Begin function _Z3addiPfS_
	.type	_Z3addiPfS_,@function
_Z3addiPfS_:                            # @_Z3addiPfS_
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jle	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%edi, %eax
	xorl	%ecx, %ecx
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movss	(%rsi,%rcx,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	addss	(%rdx,%rcx,4), %xmm0
	movss	%xmm0, (%rdx,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z3addiPfS_, .Lfunc_end0-_Z3addiPfS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x49742400                      # float 1.0E+6
.LCPI1_1:
	.long	0x447a0000                      # float 1000
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI1_2:
	.quad	0x4024000000000000              # double 10
	.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
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$536870912, %edi                # imm = 0x20000000
	callq	_Znam
	movq	%rax, %rbx
	movl	$536870912, %edi                # imm = 0x20000000
	callq	_Znam
	movq	%rax, %r14
	xorl	%eax, %eax
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	movl	$1065353216, (%rbx,%rax,4)      # imm = 0x3F800000
	movl	$1073741824, (%r14,%rax,4)      # imm = 0x40000000
	incq	%rax
	cmpq	$134217728, %rax                # imm = 0x8000000
	jne	.LBB1_1
# %bb.2:                                # %.preheader.preheader
	xorpd	%xmm0, %xmm0
	xorl	%r15d, %r15d
.LBB1_3:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	callq	clock
	movq	%rax, %r12
	xorl	%eax, %eax
.LBB1_4:                                # %.lr.ph.i
                                        #   Parent Loop BB1_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rbx,%rax,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	addss	(%r14,%rax,4), %xmm0
	movss	%xmm0, (%r14,%rax,4)
	incq	%rax
	cmpq	$134217728, %rax                # imm = 0x8000000
	jne	.LBB1_4
# %bb.5:                                # %_Z3addiPfS_.exit
                                        #   in Loop: Header=BB1_3 Depth=1
	callq	clock
	subq	%r12, %rax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
	divss	.LCPI1_0(%rip), %xmm0
	mulss	.LCPI1_1(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movl	$.L.str, %edi
	movl	%r15d, %esi
	movb	$1, %al
	callq	printf
	movsd	16(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	addsd	8(%rsp), %xmm0                  # 8-byte Folded Reload
	incl	%r15d
	cmpl	$10, %r15d
	jne	.LBB1_3
# %bb.6:
	divsd	.LCPI1_2(%rip), %xmm0
	movl	$.L.str.1, %edi
	movb	$1, %al
	callq	printf
	movq	%rbx, %rdi
	callq	_ZdaPv
	movq	%r14, %rdi
	callq	_ZdaPv
	xorl	%eax, %eax
	addq	$24, %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_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"CPU RUN-%d time = %f ms.\n"
	.size	.L.str, 26

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"CPU Avg time = %lf ms.\n"
	.size	.L.str.1, 24

	.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
