/*
 * a simple test of the scan kernel.
 */

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

__global__ void cudaScan(float* out, float *in, int size);
void startClock(char*);
void stopClock(char*);
void printClock(char*);

int main(int argc, char** argv) {

	if (argc < 2) {
		printf("Usage: %s size-of-array\n",argv[0]);
		exit(1);
	}
	int size = atoi(argv[1]);
	cudaDeviceProp props;
	cudaGetDeviceProperties (&props,0);
	if (size > props.maxThreadsPerBlock) {
		fprintf(stderr,"At most %d elements for small version\n",
			props.maxThreadsPerBlock);
		exit(1);
	}
	printf("size = %d\n",size);

	/* host based data */

	float *h_in;
	float *h_out;

	/* device based data */

	float *d_in;
	float *d_out;

	h_in = (float*) malloc(size*sizeof(float));
	h_out =(float*) malloc(size*sizeof(float));

	cudaMalloc(&d_in,size*sizeof(float));
	cudaMalloc(&d_out,size*sizeof(float));

	for (int i = 0; i < size; i++) {
		h_in[i] = 1.0;
	}

	startClock("copy data to device");
	cudaMemcpy(d_in,h_in,size*sizeof(float),cudaMemcpyHostToDevice);
	stopClock("copy data to device");

	startClock("compute");

	cudaScan<<<1,size,2*size*sizeof(float)>>>(d_out,d_in,size);
	cudaThreadSynchronize();

	stopClock("compute");

	startClock("copy data from device");
	cudaMemcpy(h_out,d_out,size*sizeof(float),cudaMemcpyDeviceToHost);
	stopClock("copy data from device");

	float sum = 0.0f;
	for (int i = 0; i < size; i++) {
		sum += h_in[i];
		printf("%d %f -> %f (%f)\n",i,h_in[i],h_out[i],sum);
	}

	free(h_in);
	free(h_out);

	cudaFree(d_in);
	cudaFree(d_out);

	printClock("copy data to device");
	printClock("compute");
	printClock("copy data from device");
}


	.file	"tmpxft_003b91d3_00000000-6_scan-cuda-test-small.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"Usage: %s size-of-array\n"
.LC2:
	.string	"At most %d elements for small version\n"
.LC3:
	.string	"size = %d\n"
.LC5:
	.string	"copy data to device"
.LC6:
	.string	"compute"
.LC7:
	.string	"copy data from device"
.LC8:
	.string	"%d %f -> %f (%f)\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$1112, %rsp
	.cfi_def_cfa_offset 1168
	movq	%fs:40, %rax
	movq	%rax, 1096(%rsp)
	xorl	%eax, %eax
	decl	%edi
	jg	.L3
	movq	(%rsi), %rdx
	movl	$2, %edi
	leaq	.LC1(%rip), %rsi
	call	__printf_chk@PLT
	jmp	.L12
.L3:
	movq	8(%rsi), %rdi
	call	atoi@PLT
	leaq	64(%rsp), %rdi
	xorl	%esi, %esi
	movl	%eax, %ebx
	call	cudaGetDeviceProperties_v2@PLT
	movl	384(%rsp), %ecx
	cmpl	%ebx, %ecx
	jge	.L4
	movq	stderr(%rip), %rdi
	leaq	.LC2(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L12:
	movl	$1, %edi
	call	exit@PLT
.L4:
	movl	%ebx, %edx
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movslq	%ebx, %rax
	leaq	0(,%rax,4), %r15
	movq	%r15, %rdi
	call	malloc@PLT
	movq	%r15, %rdi
	movq	%rax, %rbp
	call	malloc@PLT
	leaq	24(%rsp), %rdi
	movq	%r15, %rsi
	movq	%rax, %r12
	call	cudaMalloc@PLT
	leaq	32(%rsp), %rdi
	movq	%r15, %rsi
	call	cudaMalloc@PLT
	movss	.LC4(%rip), %xmm0
	xorl	%eax, %eax
.L5:
	cmpl	%eax, %ebx
	jle	.L14
	movss	%xmm0, 0(%rbp,%rax,4)
	incq	%rax
	jmp	.L5
.L14:
	leaq	.LC5(%rip), %r14
	movq	%r14, %rdi
	call	_Z10startClockPc@PLT
	movq	24(%rsp), %rdi
	movq	%r15, %rdx
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	%r14, %rdi
	call	_Z9stopClockPc@PLT
	leaq	.LC6(%rip), %rdi
	call	_Z10startClockPc@PLT
	leal	(%rbx,%rbx), %eax
	movl	%ebx, 52(%rsp)
	xorl	%r9d, %r9d
	movabsq	$4294967297, %rdi
	cltq
	movl	$1, %esi
	movq	%rdi, 56(%rsp)
	movl	60(%rsp), %ecx
	leaq	0(,%rax,4), %r8
	movq	52(%rsp), %rdx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L7
	movq	24(%rsp), %rsi
	movq	32(%rsp), %rdi
	movl	%ebx, %edx
	call	_Z8cudaScanPfS_i@PLT
.L7:
	call	cudaThreadSynchronize@PLT
	leaq	.LC6(%rip), %rdi
	xorl	%r13d, %r13d
	call	_Z9stopClockPc@PLT
	leaq	.LC7(%rip), %rdi
	call	_Z10startClockPc@PLT
	movq	32(%rsp), %rsi
	movq	%r15, %rdx
	movq	%r12, %rdi
	movl	$2, %ecx
	leaq	.LC8(%rip), %r15
	call	cudaMemcpy@PLT
	leaq	.LC7(%rip), %rdi
	call	_Z9stopClockPc@PLT
	xorps	%xmm3, %xmm3
.L8:
	movl	%r13d, %edx
	cmpl	%r13d, %ebx
	jle	.L15
	movq	%r15, %rsi
	movss	0(%rbp,%r13,4), %xmm0
	movl	$2, %edi
	movb	$3, %al
	cvtss2sd	(%r12,%r13,4), %xmm1
	incq	%r13
	addss	%xmm0, %xmm3
	cvtss2sd	%xmm0, %xmm0
	movss	%xmm3, 12(%rsp)
	cvtss2sd	%xmm3, %xmm2
	call	__printf_chk@PLT
	movss	12(%rsp), %xmm3
	jmp	.L8
.L15:
	movq	%rbp, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	%r14, %rdi
	call	_Z10printClockPc@PLT
	leaq	.LC6(%rip), %rdi
	call	_Z10printClockPc@PLT
	leaq	.LC7(%rip), %rdi
	call	_Z10printClockPc@PLT
	movq	1096(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$1112, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2053:
	.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
.LFE2053:
	.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
.LC4:
	.long	1065353216
	.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:

/*
 * a simple test of the scan kernel.
 */


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

__global__ void cudaScan(float* out, float *in, int size);
void startClock(char*);
void stopClock(char*);
void printClock(char*);

int main(int argc, char** argv) {

	if (argc < 2) {
		printf("Usage: %s size-of-array\n",argv[0]);
		exit(1);
	}
	int size = atoi(argv[1]);
	hipDeviceProp_t props;
	hipGetDeviceProperties (&props,0);
	if (size > props.maxThreadsPerBlock) {
		fprintf(stderr,"At most %d elements for small version\n",
			props.maxThreadsPerBlock);
		exit(1);
	}
	printf("size = %d\n",size);

	/* host based data */

	float *h_in;
	float *h_out;

	/* device based data */

	float *d_in;
	float *d_out;

	h_in = (float*) malloc(size*sizeof(float));
	h_out =(float*) malloc(size*sizeof(float));

	hipMalloc(&d_in,size*sizeof(float));
	hipMalloc(&d_out,size*sizeof(float));

	for (int i = 0; i < size; i++) {
		h_in[i] = 1.0;
	}

	startClock("copy data to device");
	hipMemcpy(d_in,h_in,size*sizeof(float),hipMemcpyHostToDevice);
	stopClock("copy data to device");

	startClock("compute");

	cudaScan<<<1,size,2*size*sizeof(float)>>>(d_out,d_in,size);
	hipDeviceSynchronize();

	stopClock("compute");

	startClock("copy data from device");
	hipMemcpy(h_out,d_out,size*sizeof(float),hipMemcpyDeviceToHost);
	stopClock("copy data from device");

	float sum = 0.0f;
	for (int i = 0; i < size; i++) {
		sum += h_in[i];
		printf("%d %f -> %f (%f)\n",i,h_in[i],h_out[i],sum);
	}

	free(h_in);
	free(h_out);

	hipFree(d_in);
	hipFree(d_out);

	printClock("copy data to device");
	printClock("compute");
	printClock("copy data from device");
}


	.text
	.file	"scan-cuda-test-small.hip"
	.globl	main                            # -- Begin function 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	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$1504, %rsp                     # imm = 0x5E0
	.cfi_def_cfa_offset 1552
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	cmpl	$1, %edi
	jle	.LBB0_11
# %bb.1:
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %r15d
	leaq	32(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	movl	320(%rbx), %edx
	cmpl	%edx, %r15d
	jg	.LBB0_13
# %bb.2:
	movl	$.L.str.2, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
	movslq	%r15d, %r12
	shlq	$2, %r12
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %rbx
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %r14
	leaq	24(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	leaq	16(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movl	%r15d, %r13d
	testl	%r15d, %r15d
	jle	.LBB0_5
# %bb.3:                                # %.lr.ph.preheader
	xorl	%eax, %eax
.LBB0_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$1065353216, (%rbx,%rax,4)      # imm = 0x3F800000
	incq	%rax
	cmpq	%rax, %r13
	jne	.LBB0_4
.LBB0_5:                                # %._crit_edge
	movl	$.L.str.3, %edi
	callq	_Z10startClockPc
	movq	24(%rsp), %rdi
	movq	%rbx, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$.L.str.3, %edi
	callq	_Z9stopClockPc
	movl	$.L.str.4, %edi
	callq	_Z10startClockPc
	leal	(%r15,%r15), %eax
	movslq	%eax, %r8
	shlq	$2, %r8
	movq	%r13, %rdx
	btsq	$32, %rdx
	movabsq	$4294967296, %rdi               # imm = 0x100000000
	orq	$1, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB0_7
# %bb.6:
	movq	16(%rsp), %rdi
	movq	24(%rsp), %rsi
	movl	%r15d, %edx
	callq	_Z23__device_stub__cudaScanPfS_i
.LBB0_7:
	callq	hipDeviceSynchronize
	movl	$.L.str.4, %edi
	callq	_Z9stopClockPc
	movl	$.L.str.5, %edi
	callq	_Z10startClockPc
	movq	16(%rsp), %rsi
	movq	%r14, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	$.L.str.5, %edi
	callq	_Z9stopClockPc
	testl	%r15d, %r15d
	jle	.LBB0_10
# %bb.8:                                # %.lr.ph48.preheader
	xorps	%xmm1, %xmm1
	xorl	%r15d, %r15d
.LBB0_9:                                # %.lr.ph48
                                        # =>This Inner Loop Header: Depth=1
	movss	(%rbx,%r15,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	addss	%xmm0, %xmm1
	movss	%xmm1, 12(%rsp)                 # 4-byte Spill
	cvtss2sd	%xmm0, %xmm0
	xorps	%xmm1, %xmm1
	cvtss2sd	(%r14,%r15,4), %xmm1
	xorps	%xmm2, %xmm2
	cvtss2sd	12(%rsp), %xmm2                 # 4-byte Folded Reload
	movl	$.L.str.6, %edi
	movl	%r15d, %esi
	movb	$3, %al
	callq	printf
	movss	12(%rsp), %xmm1                 # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	incq	%r15
	cmpq	%r15, %r13
	jne	.LBB0_9
.LBB0_10:                               # %._crit_edge49
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movl	$.L.str.3, %edi
	callq	_Z10printClockPc
	movl	$.L.str.4, %edi
	callq	_Z10printClockPc
	movl	$.L.str.5, %edi
	callq	_Z10printClockPc
	xorl	%eax, %eax
	addq	$1504, %rsp                     # imm = 0x5E0
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB0_11:
	.cfi_def_cfa_offset 1552
	movq	(%rsi), %rsi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB0_12
.LBB0_13:
	movq	stderr(%rip), %rdi
	movl	$.L.str.1, %esi
	xorl	%eax, %eax
	callq	fprintf
.LBB0_12:
	movl	$1, %edi
	callq	exit
.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	"Usage: %s size-of-array\n"
	.size	.L.str, 25

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"At most %d elements for small version\n"
	.size	.L.str.1, 39

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

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"copy data to device"
	.size	.L.str.3, 20

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"compute"
	.size	.L.str.4, 8

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"copy data from device"
	.size	.L.str.5, 22

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"%d %f -> %f (%f)\n"
	.size	.L.str.6, 18

	.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
