#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <iostream>


struct GpuTimer
{
	cudaEvent_t start;
	cudaEvent_t stop;

	GpuTimer()
	{
		cudaEventCreate(&start);
		cudaEventCreate(&stop);
	}

	~GpuTimer()
	{
		cudaEventDestroy(start);
		cudaEventDestroy(stop);
	}

	void Start()
	{
		cudaEventRecord(start, 0);
	}

	void Stop()
	{
		cudaEventRecord(stop, 0);
	}

	float Elapsed()
	{
		float elapsed;
		cudaEventSynchronize(stop);
		cudaEventElapsedTime(&elapsed, start, stop);
		return elapsed;
	}

};


__global__ void global_reduce_kernel (float* d_out, float* d_in)
{
	int index = threadIdx.x + blockDim.x * blockIdx.x;
	int tid = threadIdx.x;

	for (unsigned int stride = blockDim.x/2; stride > 0; stride >>= 1 ) {
		// intra-block sum
		if (tid < stride) {
			d_in[index] += d_in[index + stride];
		}

		__syncthreads();	// make sure all adds at one stage are done!
	}

	// only thread 0 writes result for this block back to global mem
	if (tid == 0)
	{
		d_out[blockIdx.x] = d_in[index];
	}
}


void reduce (float* d_out, float* d_intermediate, float* d_in,
			int size)
{
	// assumes that size is not greater than maxThreadsPerBlock^2
	// and that size is a multiple of maxThreadsPerBlock
	const int maxThreadsPerBlock = 1024;
	int threads = maxThreadsPerBlock;
	int blocks = size / maxThreadsPerBlock;

	global_reduce_kernel<<<blocks, threads>>>
		(d_intermediate, d_in);

	// now we're down to one block left, so reduce it
	threads = blocks; // launch one thread for each block in prev step
	blocks = 1;

	global_reduce_kernel<<<blocks, threads>>>
		(d_out, d_intermediate);
}

int main (int argc, char **argv)
{
	// print GPU info
	int deviceCount;
	cudaGetDeviceCount(&deviceCount);
	if (deviceCount == 0) {
		fprintf(stderr, "error: no devices supporting CUDA.\n");
		exit(EXIT_FAILURE);
	}
	int dev = 0;
	cudaSetDevice(dev);

	cudaDeviceProp devProps;
	if (cudaGetDeviceProperties(&devProps, dev) == 0) 
	{
		printf("Using device %d:\n", dev);
		printf("%s; global mem: %zdB; compute v%d.%d; clock: %d kHz\n",
			devProps.name, (int)devProps.totalGlobalMem,
			(int)devProps.major, (int)devProps.minor,
			(int)devProps.clockRate);
	}
	
	GpuTimer timer;
	const int ARRAY_SIZE = 1 << 20;
	const int ARRAY_BYTES = ARRAY_SIZE * sizeof(float);

	// generate the input array on the host
	float h_in[ARRAY_SIZE];
	float sum = 0.0f;
	for (int i = 0; i < ARRAY_SIZE; i++) {
		// generate random float in [-1.0f, 1.0f]
		h_in[i] = -1.0f + (float)random() / ((float)RAND_MAX/2.0f);
		sum += h_in[i];
	}

	// declare GPU memory pointers
	float *d_in, *d_intermediate, *d_out;

	// allocate GPU memory
	cudaMalloc(&d_in, ARRAY_BYTES);
	if (d_in == NULL) {
		std::cout << "Failed to alloc GPU mem\n";
		return -1;
	}

	cudaMalloc(&d_intermediate, ARRAY_BYTES); // overallocated
	if (d_intermediate == NULL) {
		std::cout << "Failed to alloc GPU mem\n";
		cudaFree(d_in);
		return -1;
	}

	cudaMalloc(&d_out, sizeof(float));
	if (d_out == NULL) {
		std::cout << "Failed to alloc GPU mem\n";
		cudaFree(d_in);
		cudaFree(d_intermediate);
		return -1;
	}

	// transfer the input array to the GPU
	cudaMemcpy(d_in, h_in, ARRAY_BYTES, cudaMemcpyHostToDevice);

        printf("Running global reduce\n");
	
	timer.Start();
	
	for (int i=0; i < 100; i++) {

		reduce(d_out, d_intermediate, d_in, ARRAY_SIZE);
	}
	timer.Stop();

	float elapsedTime;
	elapsedTime = timer.Elapsed();
	elapsedTime /= 100.0f;		// 100 trials


	float h_out;
	cudaMemcpy(&h_out, d_out, sizeof(float), cudaMemcpyDeviceToHost);

	printf("average time elapsed: %f\n", elapsedTime);

    	// free GPU memory allocation
    	cudaFree(d_in);
    	cudaFree(d_intermediate);
    	cudaFree(d_out);
        
    	return 0;
}

	.file	"tmpxft_002b4674_00000000-6_global_reduce.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3648:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3648:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z42__device_stub__Z20global_reduce_kernelPfS_PfS_
	.type	_Z42__device_stub__Z20global_reduce_kernelPfS_PfS_, @function
_Z42__device_stub__Z20global_reduce_kernelPfS_PfS_:
.LFB3670:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movq	%rsi, (%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z20global_reduce_kernelPfS_(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L2:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3670:
	.size	_Z42__device_stub__Z20global_reduce_kernelPfS_PfS_, .-_Z42__device_stub__Z20global_reduce_kernelPfS_PfS_
	.globl	_Z20global_reduce_kernelPfS_
	.type	_Z20global_reduce_kernelPfS_, @function
_Z20global_reduce_kernelPfS_:
.LFB3671:
	.cfi_startproc
	endbr64
	jmp	_Z42__device_stub__Z20global_reduce_kernelPfS_PfS_
	.cfi_endproc
.LFE3671:
	.size	_Z20global_reduce_kernelPfS_, .-_Z20global_reduce_kernelPfS_
	.globl	_Z6reducePfS_S_i
	.type	_Z6reducePfS_S_i, @function
_Z6reducePfS_S_i:
.LFB3644:
	.cfi_startproc
	endbr64
	movl	%ecx, %eax
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movq	%rdx, %r13
	xorl	%r9d, %r9d
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	cltd
	movq	%rdi, %r12
	movl	$1024, %edi
	idivl	%edi
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rsi, %rbp
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	movl	$4194305, %esi
	xorl	%r8d, %r8d
	salq	$10, %rsi
	movl	$1, %ecx
	movl	$1, 28(%rsp)
	movq	%rsi, 20(%rsp)
	movq	%rsi, %rdx
	movl	%eax, 8(%rsp)
	movl	%eax, %ebx
	movabsq	$4294967297, %rax
	movq	%rax, 12(%rsp)
	movq	8(%rsp), %rdi
	movl	16(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L9
	movq	%r13, %rsi
	movq	%rbp, %rdi
	call	_Z42__device_stub__Z20global_reduce_kernelPfS_PfS_
.L9:
	movl	%ebx, 20(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1, %esi
	movabsq	$4294967297, %rdi
	movl	$1, 16(%rsp)
	movq	%rdi, 24(%rsp)
	movl	28(%rsp), %ecx
	movq	20(%rsp), %rdx
	movq	%rdi, 8(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L8
	addq	$40, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 40
	movq	%rbp, %rsi
	movq	%r12, %rdi
	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
	jmp	_Z42__device_stub__Z20global_reduce_kernelPfS_PfS_
.L8:
	.cfi_restore_state
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	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
.LFE3644:
	.size	_Z6reducePfS_S_i, .-_Z6reducePfS_S_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"error: no devices supporting CUDA.\n"
.LC1:
	.string	"Using device %d:\n"
.LC2:
	.string	"%s; global mem: %zdB; compute v%d.%d; clock: %d kHz\n"
.LC5:
	.string	"Failed to alloc GPU mem\n"
.LC6:
	.string	"Running global reduce\n"
.LC8:
	.string	"average time elapsed: %f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3645:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA3645
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	leaq	-4194304(%rsp), %r11
	.cfi_def_cfa 11, 4194328
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	.cfi_def_cfa_register 7
	subq	$1112, %rsp
	.cfi_def_cfa_offset 4195440
	movq	%fs:40, %rax
	movq	%rax, 4195400(%rsp)
	xorl	%eax, %eax
	leaq	12(%rsp), %rdi
.LEHB0:
	call	cudaGetDeviceCount@PLT
	cmpl	$0, 12(%rsp)
	jne	.L13
	movq	stderr(%rip), %rdi
	leaq	.LC0(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L13:
	xorl	%edi, %edi
	leaq	64(%rsp), %rbx
	call	cudaSetDevice@PLT
	xorl	%esi, %esi
	movq	%rbx, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	testl	%eax, %eax
	jne	.L14
	xorl	%edx, %edx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	%rbx, %rdx
	movl	$2, %edi
	leaq	.LC2(%rip), %rsi
	pushq	%rax
	.cfi_def_cfa_offset 4195448
	movl	420(%rsp), %eax
	pushq	%rax
	.cfi_def_cfa_offset 4195456
	movl	444(%rsp), %r9d
	xorl	%eax, %eax
	movl	440(%rsp), %r8d
	movl	368(%rsp), %ecx
	call	__printf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 4195448
	popq	%rcx
	.cfi_def_cfa_offset 4195440
.L14:
	leaq	48(%rsp), %rdi
	xorl	%ebx, %ebx
	call	cudaEventCreate@PLT
	leaq	56(%rsp), %rdi
	call	cudaEventCreate@PLT
.LEHE0:
.L15:
	call	random@PLT
	leaq	1096(%rsp), %rbp
	cvtsi2ssq	%rax, %xmm0
	mulss	.LC3(%rip), %xmm0
	subss	.LC4(%rip), %xmm0
	movss	%xmm0, 0(%rbp,%rbx,4)
	incq	%rbx
	cmpq	$1048576, %rbx
	jne	.L15
	leaq	24(%rsp), %rdi
	movl	$4194304, %esi
.LEHB1:
	call	cudaMalloc@PLT
	cmpq	$0, 24(%rsp)
	jne	.L16
	movl	$24, %edx
	leaq	.LC5(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
.L19:
	orl	$-1, %ebx
	jmp	.L17
.L16:
	leaq	32(%rsp), %rdi
	movl	$4194304, %esi
	call	cudaMalloc@PLT
	cmpq	$0, 32(%rsp)
	jne	.L18
	movl	$24, %edx
	leaq	.LC5(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
	movq	24(%rsp), %rdi
	jmp	.L31
.L18:
	leaq	40(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
	cmpq	$0, 40(%rsp)
	jne	.L20
	movl	$24, %edx
	leaq	.LC5(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
.L31:
	call	cudaFree@PLT
	jmp	.L19
.L20:
	movq	24(%rsp), %rdi
	movl	$1, %ecx
	movl	$4194304, %edx
	movq	%rbp, %rsi
	call	cudaMemcpy@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	$100, %ebx
.L21:
	movq	24(%rsp), %rdx
	movq	32(%rsp), %rsi
	movl	$1048576, %ecx
	movq	40(%rsp), %rdi
	call	_Z6reducePfS_S_i
	decl	%ebx
	jne	.L21
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	56(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	leaq	20(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	40(%rsp), %rsi
	leaq	16(%rsp), %rdi
	movl	$2, %ecx
	movl	$4, %edx
	movss	20(%rsp), %xmm0
	divss	.LC7(%rip), %xmm0
	movd	%xmm0, %ebx
	call	cudaMemcpy@PLT
	movd	%ebx, %xmm1
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	%xmm1, %xmm0
	call	__printf_chk@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
.LEHE1:
	xorl	%ebx, %ebx
.L17:
	movq	48(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	56(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	4195400(%rsp), %rax
	subq	%fs:40, %rax
	je	.L24
	jmp	.L28
.L25:
	endbr64
	movq	%rax, %rbx
.L22:
	movq	48(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	56(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	4195400(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L28
	movq	%rbx, %rdi
.LEHB2:
	call	_Unwind_Resume@PLT
.LEHE2:
.L28:
	call	__stack_chk_fail@PLT
.L24:
	addq	$4195416, %rsp
	.cfi_def_cfa_offset 24
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3645:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA3645:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE3645-.LLSDACSB3645
.LLSDACSB3645:
	.uleb128 .LEHB0-.LFB3645
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB3645
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L25-.LFB3645
	.uleb128 0
	.uleb128 .LEHB2-.LFB3645
	.uleb128 .LEHE2-.LEHB2
	.uleb128 0
	.uleb128 0
.LLSDACSE3645:
	.section	.text.startup
	.size	main, .-main
	.section	.rodata.str1.1
.LC9:
	.string	"_Z20global_reduce_kernelPfS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3673:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC9(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z20global_reduce_kernelPfS_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3673:
	.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
.LC3:
	.long	813694976
	.align 4
.LC4:
	.long	1065353216
	.align 4
.LC7:
	.long	1120403456
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.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 <stdio.h>
#include <stdlib.h>
#include <hip/hip_runtime.h>

#include <iostream>


struct GpuTimer
{
	hipEvent_t start;
	hipEvent_t stop;

	GpuTimer()
	{
		hipEventCreate(&start);
		hipEventCreate(&stop);
	}

	~GpuTimer()
	{
		hipEventDestroy(start);
		hipEventDestroy(stop);
	}

	void Start()
	{
		hipEventRecord(start, 0);
	}

	void Stop()
	{
		hipEventRecord(stop, 0);
	}

	float Elapsed()
	{
		float elapsed;
		hipEventSynchronize(stop);
		hipEventElapsedTime(&elapsed, start, stop);
		return elapsed;
	}

};


__global__ void global_reduce_kernel (float* d_out, float* d_in)
{
	int index = threadIdx.x + blockDim.x * blockIdx.x;
	int tid = threadIdx.x;

	for (unsigned int stride = blockDim.x/2; stride > 0; stride >>= 1 ) {
		// intra-block sum
		if (tid < stride) {
			d_in[index] += d_in[index + stride];
		}

		__syncthreads();	// make sure all adds at one stage are done!
	}

	// only thread 0 writes result for this block back to global mem
	if (tid == 0)
	{
		d_out[blockIdx.x] = d_in[index];
	}
}


void reduce (float* d_out, float* d_intermediate, float* d_in,
			int size)
{
	// assumes that size is not greater than maxThreadsPerBlock^2
	// and that size is a multiple of maxThreadsPerBlock
	const int maxThreadsPerBlock = 1024;
	int threads = maxThreadsPerBlock;
	int blocks = size / maxThreadsPerBlock;

	global_reduce_kernel<<<blocks, threads>>>
		(d_intermediate, d_in);

	// now we're down to one block left, so reduce it
	threads = blocks; // launch one thread for each block in prev step
	blocks = 1;

	global_reduce_kernel<<<blocks, threads>>>
		(d_out, d_intermediate);
}

int main (int argc, char **argv)
{
	// print GPU info
	int deviceCount;
	hipGetDeviceCount(&deviceCount);
	if (deviceCount == 0) {
		fprintf(stderr, "error: no devices supporting CUDA.\n");
		exit(EXIT_FAILURE);
	}
	int dev = 0;
	hipSetDevice(dev);

	hipDeviceProp_t devProps;
	if (hipGetDeviceProperties(&devProps, dev) == 0) 
	{
		printf("Using device %d:\n", dev);
		printf("%s; global mem: %zdB; compute v%d.%d; clock: %d kHz\n",
			devProps.name, (int)devProps.totalGlobalMem,
			(int)devProps.major, (int)devProps.minor,
			(int)devProps.clockRate);
	}
	
	GpuTimer timer;
	const int ARRAY_SIZE = 1 << 20;
	const int ARRAY_BYTES = ARRAY_SIZE * sizeof(float);

	// generate the input array on the host
	float h_in[ARRAY_SIZE];
	float sum = 0.0f;
	for (int i = 0; i < ARRAY_SIZE; i++) {
		// generate random float in [-1.0f, 1.0f]
		h_in[i] = -1.0f + (float)random() / ((float)RAND_MAX/2.0f);
		sum += h_in[i];
	}

	// declare GPU memory pointers
	float *d_in, *d_intermediate, *d_out;

	// allocate GPU memory
	hipMalloc(&d_in, ARRAY_BYTES);
	if (d_in == NULL) {
		std::cout << "Failed to alloc GPU mem\n";
		return -1;
	}

	hipMalloc(&d_intermediate, ARRAY_BYTES); // overallocated
	if (d_intermediate == NULL) {
		std::cout << "Failed to alloc GPU mem\n";
		hipFree(d_in);
		return -1;
	}

	hipMalloc(&d_out, sizeof(float));
	if (d_out == NULL) {
		std::cout << "Failed to alloc GPU mem\n";
		hipFree(d_in);
		hipFree(d_intermediate);
		return -1;
	}

	// transfer the input array to the GPU
	hipMemcpy(d_in, h_in, ARRAY_BYTES, hipMemcpyHostToDevice);

        printf("Running global reduce\n");
	
	timer.Start();
	
	for (int i=0; i < 100; i++) {

		reduce(d_out, d_intermediate, d_in, ARRAY_SIZE);
	}
	timer.Stop();

	float elapsedTime;
	elapsedTime = timer.Elapsed();
	elapsedTime /= 100.0f;		// 100 trials


	float h_out;
	hipMemcpy(&h_out, d_out, sizeof(float), hipMemcpyDeviceToHost);

	printf("average time elapsed: %f\n", elapsedTime);

    	// free GPU memory allocation
    	hipFree(d_in);
    	hipFree(d_intermediate);
    	hipFree(d_out);
        
    	return 0;
}

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

                                        # End of file scope inline assembly
	.globl	_Z35__device_stub__global_reduce_kernelPfS_ # -- Begin function _Z35__device_stub__global_reduce_kernelPfS_
	.type	_Z35__device_stub__global_reduce_kernelPfS_,@function
_Z35__device_stub__global_reduce_kernelPfS_: # @_Z35__device_stub__global_reduce_kernelPfS_
	.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	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	16(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%rsp, %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z20global_reduce_kernelPfS_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	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
.Lfunc_end0:
	.size	_Z35__device_stub__global_reduce_kernelPfS_, .Lfunc_end0-_Z35__device_stub__global_reduce_kernelPfS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z6reducePfS_S_i                # -- Begin function _Z6reducePfS_S_i
	.type	_Z6reducePfS_S_i,@function
_Z6reducePfS_S_i:                       # @_Z6reducePfS_S_i
	.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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
                                        # kill: def $ecx killed $ecx def $rcx
	movq	%rdx, %r13
	movq	%rsi, %rbx
	movq	%rdi, %r14
	leal	1023(%rcx), %r15d
	testl	%ecx, %ecx
	cmovnsl	%ecx, %r15d
	movabsq	$4294967296, %r12               # imm = 0x100000000
	sarl	$10, %r15d
	btsq	$32, %r15
	leaq	1024(%r12), %rdx
	movq	%r15, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_2
# %bb.1:
	movq	%rbx, %rdi
	movq	%r13, %rsi
	callq	_Z35__device_stub__global_reduce_kernelPfS_
.LBB1_2:
	incq	%r12
	movq	%r12, %rdi
	movl	$1, %esi
	movq	%r15, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	je	.LBB1_4
# %bb.3:
	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
.LBB1_4:
	.cfi_def_cfa_offset 48
	movq	%r14, %rdi
	movq	%rbx, %rsi
	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
	jmp	_Z35__device_stub__global_reduce_kernelPfS_ # TAILCALL
.Lfunc_end1:
	.size	_Z6reducePfS_S_i, .Lfunc_end1-_Z6reducePfS_S_i
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI2_0:
	.long	0x30800000                      # float 9.31322574E-10
.LCPI2_1:
	.long	0xbf800000                      # float -1
.LCPI2_2:
	.long	0x42c80000                      # float 100
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$4195848, %rsp                  # imm = 0x400608
	.cfi_def_cfa_offset 4195872
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	leaq	60(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipGetDeviceCount
	cmpl	$0, (%rbx)
	je	.LBB2_34
# %bb.1:
	xorl	%edi, %edi
	callq	hipSetDevice
	leaq	64(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	testl	%eax, %eax
	jne	.LBB2_3
# %bb.2:
	movl	$.L.str.1, %edi
	xorl	%esi, %esi
	xorl	%eax, %eax
	callq	printf
	leaq	64(%rsp), %rsi
	movl	360(%rsi), %ecx
	movl	364(%rsi), %r8d
	movl	288(%rsi), %edx
	movl	348(%rsi), %r9d
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
.LBB2_3:
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	leaq	32(%rsp), %rdi
	callq	hipEventCreate
	xorl	%ebx, %ebx
.LBB2_4:                                # =>This Inner Loop Header: Depth=1
	callq	random
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
	mulss	.LCPI2_0(%rip), %xmm0
	addss	.LCPI2_1(%rip), %xmm0
	movss	%xmm0, 1536(%rsp,%rbx,4)
	incq	%rbx
	cmpq	$1048576, %rbx                  # imm = 0x100000
	jne	.LBB2_4
# %bb.5:
.Ltmp0:
	leaq	8(%rsp), %r14
	movl	$4194304, %esi                  # imm = 0x400000
	movq	%r14, %rdi
	callq	hipMalloc
.Ltmp1:
# %bb.6:                                # %_ZL9hipMallocIfE10hipError_tPPT_m.exit
	cmpq	$0, 8(%rsp)
	je	.LBB2_7
# %bb.8:
.Ltmp2:
	leaq	40(%rsp), %rbx
	movl	$4194304, %esi                  # imm = 0x400000
	movq	%rbx, %rdi
	callq	hipMalloc
.Ltmp3:
# %bb.9:                                # %_ZL9hipMallocIfE10hipError_tPPT_m.exit23
	cmpq	$0, 40(%rsp)
	je	.LBB2_10
# %bb.11:
.Ltmp4:
	leaq	16(%rsp), %rdi
	movl	$4, %esi
	callq	hipMalloc
.Ltmp5:
# %bb.12:                               # %_ZL9hipMallocIfE10hipError_tPPT_m.exit25
	cmpq	$0, 16(%rsp)
	je	.LBB2_13
# %bb.16:
	movq	8(%rsp), %rdi
.Ltmp6:
	leaq	1536(%rsp), %rsi
	movl	$4194304, %edx                  # imm = 0x400000
	movl	$1, %ecx
	callq	hipMemcpy
.Ltmp7:
# %bb.17:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movq	24(%rsp), %rdi
.Ltmp8:
	xorl	%esi, %esi
	callq	hipEventRecord
.Ltmp9:
# %bb.18:                               # %_ZN8GpuTimer5StartEv.exit.preheader.preheader
	movl	$100, %ebx
.LBB2_19:                               # %_ZN8GpuTimer5StartEv.exit.preheader
                                        # =>This Inner Loop Header: Depth=1
	movq	16(%rsp), %rdi
	movq	40(%rsp), %rsi
	movq	8(%rsp), %rdx
.Ltmp10:
	movl	$1048576, %ecx                  # imm = 0x100000
	callq	_Z6reducePfS_S_i
.Ltmp11:
# %bb.20:                               # %_ZN8GpuTimer5StartEv.exit
                                        #   in Loop: Header=BB2_19 Depth=1
	decl	%ebx
	jne	.LBB2_19
# %bb.21:
	movq	32(%rsp), %rdi
.Ltmp13:
	xorl	%esi, %esi
	callq	hipEventRecord
.Ltmp14:
# %bb.22:                               # %_ZN8GpuTimer4StopEv.exit
	movq	32(%rsp), %rdi
.Ltmp15:
	callq	hipEventSynchronize
.Ltmp16:
# %bb.23:                               # %.noexc
	movq	24(%rsp), %rsi
	movq	32(%rsp), %rdx
.Ltmp17:
	leaq	56(%rsp), %rdi
	callq	hipEventElapsedTime
.Ltmp18:
# %bb.24:
	leaq	56(%rsp), %rdi
	movss	(%rdi), %xmm0                   # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, 52(%rsp)                 # 4-byte Spill
	movq	16(%rsp), %rsi
.Ltmp20:
	movl	$4, %edx
	movl	$2, %ecx
	callq	hipMemcpy
.Ltmp21:
# %bb.25:
	movss	52(%rsp), %xmm0                 # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	divss	.LCPI2_2(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	movq	8(%rsp), %rdi
.Ltmp22:
	callq	hipFree
.Ltmp23:
# %bb.26:
	movq	40(%rsp), %rdi
.Ltmp24:
	callq	hipFree
.Ltmp25:
# %bb.27:
	movq	16(%rsp), %rdi
.Ltmp26:
	callq	hipFree
.Ltmp27:
# %bb.28:
	xorl	%ebx, %ebx
	jmp	.LBB2_29
.LBB2_7:
	movl	$-1, %ebx
.Ltmp37:
	movl	$_ZSt4cout, %edi
	movl	$.L.str.3, %esi
	movl	$24, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp38:
	jmp	.LBB2_29
.LBB2_10:
.Ltmp33:
	movl	$_ZSt4cout, %edi
	movl	$.L.str.3, %esi
	movl	$24, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp34:
	jmp	.LBB2_15
.LBB2_13:
.Ltmp29:
	movl	$_ZSt4cout, %edi
	movl	$.L.str.3, %esi
	movl	$24, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp30:
# %bb.14:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit26
	movq	8(%rsp), %rdi
.Ltmp31:
	callq	hipFree
.Ltmp32:
	movq	%rbx, %r14
.LBB2_15:                               # %.invoke
	movq	(%r14), %rdi
	movl	$-1, %ebx
.Ltmp35:
	callq	hipFree
.Ltmp36:
.LBB2_29:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit
	leaq	24(%rsp), %rdi
	callq	_ZN8GpuTimerD2Ev
	movl	%ebx, %eax
	addq	$4195848, %rsp                  # imm = 0x400608
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.LBB2_34:
	.cfi_def_cfa_offset 4195872
	movq	stderr(%rip), %rcx
	movl	$.L.str, %edi
	movl	$35, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$1, %edi
	callq	exit
.LBB2_30:
.Ltmp19:
	jmp	.LBB2_33
.LBB2_31:
.Ltmp28:
	jmp	.LBB2_33
.LBB2_32:
.Ltmp39:
	jmp	.LBB2_33
.LBB2_35:
.Ltmp12:
.LBB2_33:
	movq	%rax, %rbx
	leaq	24(%rsp), %rdi
	callq	_ZN8GpuTimerD2Ev
	movq	%rbx, %rdi
	callq	_Unwind_Resume@PLT
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table2:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	255                             # @TType Encoding = omit
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Lfunc_begin0-.Lfunc_begin0    # >> Call Site 1 <<
	.uleb128 .Ltmp0-.Lfunc_begin0           #   Call between .Lfunc_begin0 and .Ltmp0
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 2 <<
	.uleb128 .Ltmp9-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp9
	.uleb128 .Ltmp39-.Lfunc_begin0          #     jumps to .Ltmp39
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp10-.Lfunc_begin0          # >> Call Site 3 <<
	.uleb128 .Ltmp11-.Ltmp10                #   Call between .Ltmp10 and .Ltmp11
	.uleb128 .Ltmp12-.Lfunc_begin0          #     jumps to .Ltmp12
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp13-.Lfunc_begin0          # >> Call Site 4 <<
	.uleb128 .Ltmp14-.Ltmp13                #   Call between .Ltmp13 and .Ltmp14
	.uleb128 .Ltmp39-.Lfunc_begin0          #     jumps to .Ltmp39
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp15-.Lfunc_begin0          # >> Call Site 5 <<
	.uleb128 .Ltmp18-.Ltmp15                #   Call between .Ltmp15 and .Ltmp18
	.uleb128 .Ltmp19-.Lfunc_begin0          #     jumps to .Ltmp19
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp20-.Lfunc_begin0          # >> Call Site 6 <<
	.uleb128 .Ltmp27-.Ltmp20                #   Call between .Ltmp20 and .Ltmp27
	.uleb128 .Ltmp28-.Lfunc_begin0          #     jumps to .Ltmp28
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp37-.Lfunc_begin0          # >> Call Site 7 <<
	.uleb128 .Ltmp36-.Ltmp37                #   Call between .Ltmp37 and .Ltmp36
	.uleb128 .Ltmp39-.Lfunc_begin0          #     jumps to .Ltmp39
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp36-.Lfunc_begin0          # >> Call Site 8 <<
	.uleb128 .Lfunc_end2-.Ltmp36            #   Call between .Ltmp36 and .Lfunc_end2
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
.Lcst_end0:
	.p2align	2, 0x0
                                        # -- End function
	.section	.text._ZN8GpuTimerD2Ev,"axG",@progbits,_ZN8GpuTimerD2Ev,comdat
	.weak	_ZN8GpuTimerD2Ev                # -- Begin function _ZN8GpuTimerD2Ev
	.p2align	1, 0x90
	.type	_ZN8GpuTimerD2Ev,@function
_ZN8GpuTimerD2Ev:                       # @_ZN8GpuTimerD2Ev
.Lfunc_begin1:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception1
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
.Ltmp40:
	callq	hipEventDestroy
.Ltmp41:
# %bb.1:
	movq	8(%rbx), %rdi
.Ltmp42:
	callq	hipEventDestroy
.Ltmp43:
# %bb.2:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB3_3:
	.cfi_def_cfa_offset 16
.Ltmp44:
	movq	%rax, %rdi
	callq	__clang_call_terminate
.Lfunc_end3:
	.size	_ZN8GpuTimerD2Ev, .Lfunc_end3-_ZN8GpuTimerD2Ev
	.cfi_endproc
	.section	.gcc_except_table._ZN8GpuTimerD2Ev,"aG",@progbits,_ZN8GpuTimerD2Ev,comdat
	.p2align	2, 0x0
GCC_except_table3:
.Lexception1:
	.byte	255                             # @LPStart Encoding = omit
	.byte	3                               # @TType Encoding = udata4
	.uleb128 .Lttbase0-.Lttbaseref0
.Lttbaseref0:
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end1-.Lcst_begin1
.Lcst_begin1:
	.uleb128 .Ltmp40-.Lfunc_begin1          # >> Call Site 1 <<
	.uleb128 .Ltmp43-.Ltmp40                #   Call between .Ltmp40 and .Ltmp43
	.uleb128 .Ltmp44-.Lfunc_begin1          #     jumps to .Ltmp44
	.byte	1                               #   On action: 1
.Lcst_end1:
	.byte	1                               # >> Action Record 1 <<
                                        #   Catch TypeInfo 1
	.byte	0                               #   No further actions
	.p2align	2, 0x0
                                        # >> Catch TypeInfos <<
	.long	0                               # TypeInfo 1
.Lttbase0:
	.p2align	2, 0x0
                                        # -- End function
	.section	.text.__clang_call_terminate,"axG",@progbits,__clang_call_terminate,comdat
	.hidden	__clang_call_terminate          # -- Begin function __clang_call_terminate
	.weak	__clang_call_terminate
	.type	__clang_call_terminate,@function
__clang_call_terminate:                 # @__clang_call_terminate
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__cxa_begin_catch
	callq	_ZSt9terminatev
.Lfunc_end4:
	.size	__clang_call_terminate, .Lfunc_end4-__clang_call_terminate
	.cfi_endproc
                                        # -- End function
	.text
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z20global_reduce_kernelPfS_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB6_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z20global_reduce_kernelPfS_,@object # @_Z20global_reduce_kernelPfS_
	.section	.rodata,"a",@progbits
	.globl	_Z20global_reduce_kernelPfS_
	.p2align	3, 0x0
_Z20global_reduce_kernelPfS_:
	.quad	_Z35__device_stub__global_reduce_kernelPfS_
	.size	_Z20global_reduce_kernelPfS_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"error: no devices supporting CUDA.\n"
	.size	.L.str, 36

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Using device %d:\n"
	.size	.L.str.1, 18

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%s; global mem: %zdB; compute v%d.%d; clock: %d kHz\n"
	.size	.L.str.2, 53

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Failed to alloc GPU mem\n"
	.size	.L.str.3, 25

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"average time elapsed: %f\n"
	.size	.L.str.5, 26

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z20global_reduce_kernelPfS_"
	.size	.L__unnamed_1, 29

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Running global reduce"
	.size	.Lstr, 22

	.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 _Z35__device_stub__global_reduce_kernelPfS_
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Unwind_Resume
	.addrsig_sym _Z20global_reduce_kernelPfS_
	.addrsig_sym _ZSt4cout
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z20global_reduce_kernelPfS_ ; -- Begin function _Z20global_reduce_kernelPfS_
	.globl	_Z20global_reduce_kernelPfS_
	.p2align	8
	.type	_Z20global_reduce_kernelPfS_,@function
_Z20global_reduce_kernelPfS_:           ; @_Z20global_reduce_kernelPfS_
; %bb.0:
	s_clause 0x1
	s_load_b32 s5, s[0:1], 0x1c
	s_load_b128 s[0:3], s[0:1], 0x0
	s_mov_b32 s4, s15
	s_waitcnt lgkmcnt(0)
	s_and_b32 s5, s5, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s4, s5, v[0:1]
	s_cmp_lt_u32 s5, 2
	v_ashrrev_i32_e32 v2, 31, v1
	s_cbranch_scc1 .LBB0_5
; %bb.1:                                ; %.lr.ph
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[3:4], 2, v[1:2]
	v_mov_b32_e32 v6, 0
	v_add_co_u32 v3, vcc_lo, s2, v3
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v4, vcc_lo, s3, v4, vcc_lo
.LBB0_2:                                ; =>This Inner Loop Header: Depth=1
	s_lshr_b32 s6, s5, 1
	s_mov_b32 s7, exec_lo
	v_cmpx_gt_u32_e64 s6, v0
	s_cbranch_execz .LBB0_4
; %bb.3:                                ;   in Loop: Header=BB0_2 Depth=1
	v_add_nc_u32_e32 v5, s6, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[7:8], 2, v[5:6]
	v_add_co_u32 v7, vcc_lo, s2, v7
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v8, vcc_lo, s3, v8, vcc_lo
	s_clause 0x1
	global_load_b32 v5, v[7:8], off
	global_load_b32 v7, v[3:4], off
	s_waitcnt vmcnt(0)
	v_add_f32_e32 v5, v5, v7
	global_store_b32 v[3:4], v5, off
.LBB0_4:                                ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s7
	s_cmp_lt_u32 s5, 4
	s_mov_b32 s5, s6
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB0_2
.LBB0_5:                                ; %._crit_edge
	s_mov_b32 s5, 0
	s_mov_b32 s6, exec_lo
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB0_7
; %bb.6:
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v0, vcc_lo, s2, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	s_lshl_b64 s[2:3], s[4:5], 2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_u32 s0, s0, s2
	global_load_b32 v0, v[0:1], off
	v_mov_b32_e32 v1, 0
	s_addc_u32 s1, s1, s3
	s_waitcnt vmcnt(0)
	global_store_b32 v1, v0, s[0:1]
.LBB0_7:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z20global_reduce_kernelPfS_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 9
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z20global_reduce_kernelPfS_, .Lfunc_end0-_Z20global_reduce_kernelPfS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 304
; NumSgprs: 18
; NumVgprs: 9
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 9
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.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_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z20global_reduce_kernelPfS_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z20global_reduce_kernelPfS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     9
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
