#include "cuda_runtime.h"
//#include "device_launch_parameters.h"

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


// sum array of integers sequentially (using cache)
void arraySum(int *arr, int arraySize, int *sumValue) {
	int tempSum = 0;
	for (int i = 0; i < arraySize; i++) {
		tempSum += arr[i];
	}
	*sumValue = tempSum;
}

int main()
{
	int arraySize = 1 << 21; // 2M integers
	int numCycles = 4000; // # of repetitions
	int *arr;
	cudaMallocManaged(&arr, arraySize * sizeof(int)); // allocate arraySize * 4 bytes
	printf("Sum array of integers on CPU, using cache.\nArray size=  %d integers\n", arraySize);

	// initialize array
	for (int i = 0; i < arraySize; i++) {
		arr[i] = 1;
	}
	int sumValue = 0;
	
	// Time measurement
	int elapsedClocks = 0, startClock = 0, endClock = 0;
	double elapsedTime, avgElapsedTime;

	startClock = clock();
	for (int j = 0; j < numCycles; j++) {
		arraySum(arr, arraySize, &sumValue);

	}
	endClock = clock();

	// Print results
	printf("startClock: %d\n", startClock);
	printf("endClock: %d\n", endClock);
	elapsedClocks = endClock - startClock;
	printf("elapsedClock: %d\n", elapsedClocks);
	elapsedTime = ((double)(elapsedClocks)) / (CLOCKS_PER_SEC);
	avgElapsedTime = elapsedTime / numCycles;
	printf("Sum= %d. Number of repetitions= %d.\nElapsed time= %fs. Average elapsed time= %fs.\n", sumValue, numCycles, elapsedTime, avgElapsedTime);

	cudaFree(arr);

	return 0;
}

	.file	"tmpxft_003b5da5_00000000-6_CPU_cache.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3639:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3639:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z8arraySumPiiS_
	.type	_Z8arraySumPiiS_, @function
_Z8arraySumPiiS_:
.LFB3635:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	xorl	%ecx, %ecx
.L3:
	cmpl	%eax, %esi
	jle	.L6
	addl	(%rdi,%rax,4), %ecx
	incq	%rax
	jmp	.L3
.L6:
	movl	%ecx, (%rdx)
	ret
	.cfi_endproc
.LFE3635:
	.size	_Z8arraySumPiiS_, .-_Z8arraySumPiiS_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Sum array of integers on CPU, using cache.\nArray size=  %d integers\n"
.LC1:
	.string	"startClock: %d\n"
.LC2:
	.string	"endClock: %d\n"
.LC3:
	.string	"elapsedClock: %d\n"
.LC6:
	.string	"Sum= %d. Number of repetitions= %d.\nElapsed time= %fs. Average elapsed time= %fs.\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	$1, %edx
	movl	$8388608, %esi
	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	$40, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	16(%rsp), %rdi
	call	cudaMallocManaged@PLT
	movl	$2097152, %edx
	leaq	.LC0(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	16(%rsp), %rdx
	xorl	%eax, %eax
.L8:
	movl	$1, (%rdx,%rax,4)
	incq	%rax
	cmpq	$2097152, %rax
	jne	.L8
	call	clock@PLT
	movl	$4000, %r8d
	leaq	12(%rsp), %rdx
	movq	%rax, %rbp
	movl	%eax, %r13d
.L9:
	movq	16(%rsp), %rdi
	movl	$2097152, %esi
	call	_Z8arraySumPiiS_
	decl	%r8d
	jne	.L9
	call	clock@PLT
	movl	%r13d, %edx
	movl	$2, %edi
	leaq	.LC1(%rip), %rsi
	movq	%rax, %rbx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebx, %edx
	leaq	.LC2(%rip), %rsi
	subl	%ebp, %ebx
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebx, %edx
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	12(%rsp), %edx
	movl	$2, %edi
	movb	$2, %al
	movl	$4000, %ecx
	cvtsi2sdl	%ebx, %xmm0
	leaq	.LC6(%rip), %rsi
	divsd	.LC4(%rip), %xmm0
	movaps	%xmm0, %xmm1
	divsd	.LC5(%rip), %xmm1
	call	__printf_chk@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$40, %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
.LFE3636:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3662:
	.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
.LFE3662:
	.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
.LC4:
	.long	0
	.long	1093567616
	.align 8
.LC5:
	.long	0
	.long	1085227008
	.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 "device_launch_parameters.h"

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


// sum array of integers sequentially (using cache)
void arraySum(int *arr, int arraySize, int *sumValue) {
	int tempSum = 0;
	for (int i = 0; i < arraySize; i++) {
		tempSum += arr[i];
	}
	*sumValue = tempSum;
}

int main()
{
	int arraySize = 1 << 21; // 2M integers
	int numCycles = 4000; // # of repetitions
	int *arr;
	hipMallocManaged(&arr, arraySize * sizeof(int)); // allocate arraySize * 4 bytes
	printf("Sum array of integers on CPU, using cache.\nArray size=  %d integers\n", arraySize);

	// initialize array
	for (int i = 0; i < arraySize; i++) {
		arr[i] = 1;
	}
	int sumValue = 0;
	
	// Time measurement
	int elapsedClocks = 0, startClock = 0, endClock = 0;
	double elapsedTime, avgElapsedTime;

	startClock = clock();
	for (int j = 0; j < numCycles; j++) {
		arraySum(arr, arraySize, &sumValue);

	}
	endClock = clock();

	// Print results
	printf("startClock: %d\n", startClock);
	printf("endClock: %d\n", endClock);
	elapsedClocks = endClock - startClock;
	printf("elapsedClock: %d\n", elapsedClocks);
	elapsedTime = ((double)(elapsedClocks)) / (CLOCKS_PER_SEC);
	avgElapsedTime = elapsedTime / numCycles;
	printf("Sum= %d. Number of repetitions= %d.\nElapsed time= %fs. Average elapsed time= %fs.\n", sumValue, numCycles, elapsedTime, avgElapsedTime);

	hipFree(arr);

	return 0;
}

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

                                        # End of file scope inline assembly
	.globl	_Z8arraySumPiiS_                # -- Begin function _Z8arraySumPiiS_
	.type	_Z8arraySumPiiS_,@function
_Z8arraySumPiiS_:                       # @_Z8arraySumPiiS_
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB0_1
# %bb.3:                                # %.lr.ph.preheader
	movl	%esi, %eax
	xorl	%esi, %esi
	xorl	%ecx, %ecx
.LBB0_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	addl	(%rdi,%rsi,4), %ecx
	incq	%rsi
	cmpq	%rsi, %rax
	jne	.LBB0_4
	jmp	.LBB0_2
.LBB0_1:
	xorl	%ecx, %ecx
.LBB0_2:                                # %._crit_edge
	movl	%ecx, (%rdx)
	retq
.Lfunc_end0:
	.size	_Z8arraySumPiiS_, .Lfunc_end0-_Z8arraySumPiiS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0x412e848000000000              # double 1.0E+6
.LCPI1_1:
	.quad	0x40af400000000000              # double 4000
	.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	%rbx
	.cfi_def_cfa_offset 32
	subq	$16, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	8(%rsp), %rbx
	movl	$8388608, %esi                  # imm = 0x800000
	movq	%rbx, %rdi
	movl	$1, %edx
	callq	hipMallocManaged
	xorl	%r14d, %r14d
	movl	$.L.str, %edi
	movl	$2097152, %esi                  # imm = 0x200000
	xorl	%eax, %eax
	callq	printf
	movq	(%rbx), %rax
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	movl	$1, (%rax,%r14,4)
	incq	%r14
	cmpq	$2097152, %r14                  # imm = 0x200000
	jne	.LBB1_1
# %bb.2:
	callq	clock
	movq	%rax, %r14
	xorl	%eax, %eax
	movq	8(%rsp), %rcx
.LBB1_3:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
	xorl	%edx, %edx
	xorl	%ebx, %ebx
.LBB1_4:                                # %.lr.ph.i
                                        #   Parent Loop BB1_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	addl	(%rcx,%rdx,4), %ebx
	incq	%rdx
	cmpq	$2097152, %rdx                  # imm = 0x200000
	jne	.LBB1_4
# %bb.5:                                # %_Z8arraySumPiiS_.exit
                                        #   in Loop: Header=BB1_3 Depth=1
	incl	%eax
	cmpl	$4000, %eax                     # imm = 0xFA0
	jne	.LBB1_3
# %bb.6:
	callq	clock
	movq	%rax, %r15
	movl	$.L.str.1, %edi
	movl	%r14d, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.2, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
	subl	%r14d, %r15d
	movl	$.L.str.3, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
	cvtsi2sd	%r15d, %xmm0
	divsd	.LCPI1_0(%rip), %xmm0
	movapd	%xmm0, %xmm1
	divsd	.LCPI1_1(%rip), %xmm1
	movl	$.L.str.4, %edi
	movl	%ebx, %esi
	movl	$4000, %edx                     # imm = 0xFA0
	movb	$2, %al
	callq	printf
	movq	8(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$16, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.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	"Sum array of integers on CPU, using cache.\nArray size=  %d integers\n"
	.size	.L.str, 69

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"startClock: %d\n"
	.size	.L.str.1, 16

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"endClock: %d\n"
	.size	.L.str.2, 14

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"elapsedClock: %d\n"
	.size	.L.str.3, 18

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Sum= %d. Number of repetitions= %d.\nElapsed time= %fs. Average elapsed time= %fs.\n"
	.size	.L.str.4, 83

	.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
