#include <chrono>
#include <stdio.h>
#include <stdlib.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
using namespace std;
using namespace chrono;

#define GRIDSIZE 1
#define BLOCKSIZE 1024
#define TOTALSIZE (GRIDSIZE*BLOCKSIZE)

void genData(unsigned* ptr, unsigned int size) {
	while (size--) {
		*ptr++ = (float)(rand() % 1000) / 1000.0F;
	}
}

void kernel(unsigned* pData, unsigned* pAnswer, unsigned size) {
	while (size--) {
		unsigned value = *pData++;
		*pAnswer = *pAnswer + value;
	}
}
int main(void) {
	unsigned* pData = NULL;
	unsigned answer = 0;

	//malloc memories on the host-side
	pData = (unsigned*)malloc(2 * BLOCKSIZE * sizeof(unsigned));

	//generate source data
	genData(pData, 2 * BLOCKSIZE);

	//HOST: start the timer
	system_clock::time_point h_start = system_clock::now();
	kernel(pData, &answer, 2 * BLOCKSIZE);

	//end the timer
	system_clock::time_point h_end = system_clock::now();
	nanoseconds h_du = h_end - h_start;
	printf("%lld nano-secodns\n", h_du);
	printf("answer= %lld \n", answer);
}
	.file	"tmpxft_00225951_00000000-6_File.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
	.globl	_Z7genDataPjj
	.type	_Z7genDataPjj, @function
_Z7genDataPjj:
.LFB2126:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%esi, %esi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	leaq	(%rdi,%rsi,4), %rbp
	movq	%rdi, %rbx
	pushq	%rdx
	.cfi_def_cfa_offset 32
.L3:
	cmpq	%rbp, %rbx
	je	.L7
	call	rand@PLT
	movl	$1000, %ecx
	addq	$4, %rbx
	cltd
	idivl	%ecx
	cvtsi2ssl	%edx, %xmm0
	divss	.LC0(%rip), %xmm0
	cvttss2siq	%xmm0, %rax
	movl	%eax, -4(%rbx)
	jmp	.L3
.L7:
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2126:
	.size	_Z7genDataPjj, .-_Z7genDataPjj
	.globl	_Z6kernelPjS_j
	.type	_Z6kernelPjS_j, @function
_Z6kernelPjS_j:
.LFB2127:
	.cfi_startproc
	endbr64
	movl	%edx, %edx
	xorl	%eax, %eax
.L9:
	cmpq	%rdx, %rax
	je	.L11
	movl	(%rdi,%rax,4), %ecx
	incq	%rax
	addl	%ecx, (%rsi)
	jmp	.L9
.L11:
	ret
	.cfi_endproc
.LFE2127:
	.size	_Z6kernelPjS_j, .-_Z6kernelPjS_j
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"%lld nano-secodns\n"
.LC2:
	.string	"answer= %lld \n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2128:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	$8192, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	xorl	%eax, %eax
	movl	%eax, 4(%rsp)
	call	malloc@PLT
	movl	$2048, %esi
	movq	%rax, %rbx
	movq	%rax, %rdi
	call	_Z7genDataPjj
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	leaq	4(%rsp), %rsi
	movl	$2048, %edx
	movq	%rbx, %rdi
	movq	%rax, %rbp
	call	_Z6kernelPjS_j
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	subq	%rbp, %rax
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	4(%rsp), %edx
	xorl	%eax, %eax
	movl	$2, %edi
	leaq	.LC2(%rip), %rsi
	call	__printf_chk@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2128:
	.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.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	1148846080
	.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 <chrono>
#include <stdio.h>
#include <stdlib.h>
#include "hip/hip_runtime.h"

using namespace std;
using namespace chrono;

#define GRIDSIZE 1
#define BLOCKSIZE 1024
#define TOTALSIZE (GRIDSIZE*BLOCKSIZE)

void genData(unsigned* ptr, unsigned int size) {
	while (size--) {
		*ptr++ = (float)(rand() % 1000) / 1000.0F;
	}
}

void kernel(unsigned* pData, unsigned* pAnswer, unsigned size) {
	while (size--) {
		unsigned value = *pData++;
		*pAnswer = *pAnswer + value;
	}
}
int main(void) {
	unsigned* pData = NULL;
	unsigned answer = 0;

	//malloc memories on the host-side
	pData = (unsigned*)malloc(2 * BLOCKSIZE * sizeof(unsigned));

	//generate source data
	genData(pData, 2 * BLOCKSIZE);

	//HOST: start the timer
	system_clock::time_point h_start = system_clock::now();
	kernel(pData, &answer, 2 * BLOCKSIZE);

	//end the timer
	system_clock::time_point h_end = system_clock::now();
	nanoseconds h_du = h_end - h_start;
	printf("%lld nano-secodns\n", h_du);
	printf("answer= %lld \n", answer);
}
	.text
	.file	"File.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z7genDataPjj
.LCPI0_0:
	.long	0x447a0000                      # float 1000
	.text
	.globl	_Z7genDataPjj
	.type	_Z7genDataPjj,@function
_Z7genDataPjj:                          # @_Z7genDataPjj
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	je	.LBB0_4
# %bb.1:                                # %.lr.ph.preheader
	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
	movl	%esi, %r14d
	xorl	%r15d, %r15d
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	divss	.LCPI0_0(%rip), %xmm0
	cvttss2si	%xmm0, %rax
	movl	%eax, (%rbx,%r15,4)
	incq	%r15
	cmpl	%r15d, %r14d
	jne	.LBB0_2
# %bb.3:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
.LBB0_4:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z7genDataPjj, .Lfunc_end0-_Z7genDataPjj
	.cfi_endproc
                                        # -- End function
	.globl	_Z6kernelPjS_j                  # -- Begin function _Z6kernelPjS_j
	.type	_Z6kernelPjS_j,@function
_Z6kernelPjS_j:                         # @_Z6kernelPjS_j
	.cfi_startproc
# %bb.0:
	testl	%edx, %edx
	je	.LBB1_3
# %bb.1:                                # %.lr.ph
	movl	(%rsi), %eax
	movl	%edx, %ecx
	xorl	%edx, %edx
.LBB1_2:                                # =>This Inner Loop Header: Depth=1
	addl	(%rdi,%rdx,4), %eax
	movl	%eax, (%rsi)
	incq	%rdx
	cmpl	%edx, %ecx
	jne	.LBB1_2
.LBB1_3:                                # %._crit_edge
	retq
.Lfunc_end1:
	.size	_Z6kernelPjS_j, .Lfunc_end1-_Z6kernelPjS_j
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI2_0:
	.long	0x447a0000                      # float 1000
	.text
	.globl	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	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$8192, %edi                     # imm = 0x2000
	callq	malloc
	movq	%rax, %rbx
	xorl	%r14d, %r14d
.LBB2_1:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	divss	.LCPI2_0(%rip), %xmm0
	cvttss2si	%xmm0, %rax
	movl	%eax, (%rbx,%r14,4)
	incq	%r14
	cmpl	$2048, %r14d                    # imm = 0x800
	jne	.LBB2_1
# %bb.2:                                # %_Z7genDataPjj.exit
	xorl	%r15d, %r15d
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, %r14
	xorl	%ebp, %ebp
.LBB2_3:                                # =>This Inner Loop Header: Depth=1
	addl	(%rbx,%r15,4), %ebp
	incq	%r15
	cmpl	$2048, %r15d                    # imm = 0x800
	jne	.LBB2_3
# %bb.4:                                # %_Z6kernelPjS_j.exit
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	subq	%r14, %rax
	movl	$.L.str, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.1, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.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_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%lld nano-secodns\n"
	.size	.L.str, 19

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"answer= %lld \n"
	.size	.L.str.1, 15

	.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
