#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctime>
#include <algorithm>

// Thread block size
#define BLOCK_SIZE 1024

//  Size of Array
// #define SOA 67107840
// #define SOA 2147483647
#define SOA 1147483647
// #define SOA 8193

// Allocates an array with random integer entries.
void randomInit(int* data, unsigned long long int size)
{
	srand( time(0) );
	for (unsigned long long int i = 0; i < size; ++i)
	{
		data[i] = rand() & INT_MAX;
		//std::cout << data[i] << "\n";
	}
}

__global__ void ReductionMax2(int *input, int *results, unsigned long long int n)    //take thread divergence into account
{
	extern __shared__ int sdata[];
	unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
	unsigned int tx = threadIdx.x;
	 //load input into __shared__ memory
	if(i < n)
	{
		sdata[tx] = input[i];
	}
	else
	{
		sdata[tx] = INT_MIN;
	}

	__syncthreads();

	// block-wide reduction
	for(unsigned int offset = 1; offset < blockDim.x; offset <<= 1)
	{
		int index = 2 * offset * tx;
		if(index < blockDim.x)
	        {
			// sdata[index] += sdata[index + offset];
			if(sdata[index] < sdata[index + offset]) sdata[index] = sdata[index + offset];
		}
		__syncthreads();
	}

	// finally, thread 0 writes the result
	if(threadIdx.x == 0)
	{
		// the result is per-block
		results[blockIdx.x] = sdata[0];
	}
}

// Reduce function wrapper
	void reduce(int* d_a, int* d_b)
	{
		unsigned long long int arraySize = SOA;
		unsigned long long int numBlocks = 1 + ((SOA - 1) / BLOCK_SIZE);

		int* device_intermediate;
		cudaMalloc(&device_intermediate, sizeof(int)*numBlocks);
  		cudaMemset(device_intermediate, 0, sizeof(int)*numBlocks);

		int i=1;

		do
		{
			std::cout << "GPU Iteration " << i << std::endl;
			i++;

			//setup execution parameters
                	dim3 block(BLOCK_SIZE);
	                dim3 grid(numBlocks);

        	        //execute the kernel
	                ReductionMax2<<<grid, block, BLOCK_SIZE*sizeof(int)>>>(d_a,device_intermediate,arraySize);
			arraySize = 1 + ((arraySize - 1) / BLOCK_SIZE);

			// device_in to device_intermediate
			cudaMemcpy(d_a, device_intermediate, sizeof(int)*numBlocks, cudaMemcpyDeviceToDevice);

			// Update required number of blocks
			numBlocks = 1 + ((numBlocks - 1) / BLOCK_SIZE);

			cudaFree(device_intermediate);
			cudaMalloc(&device_intermediate, sizeof(int)*numBlocks);
		}
		while(arraySize > BLOCK_SIZE);

		// Now compute the rest
		ReductionMax2<<<1, BLOCK_SIZE, BLOCK_SIZE*sizeof(int)>>>(d_a,d_b,arraySize);
	}


// get global max element via per-block reductions
	int main(int argc, char **argv)
	{
		// Introduce program
		std::cout << "Finding max element of an array on a GPU" << std::endl;

		// show memory usage of GPU
        	size_t free_byte ;
	        size_t total_byte ;
		cudaError_t cuda_status = cudaMemGetInfo( &free_byte, &total_byte ) ;

        	if ( cudaSuccess != cuda_status )
		{
            		printf("Error: cudaMemGetInfo fails, %s \n", cudaGetErrorString(cuda_status) );
		        return 1;
		}

	        double free_db = (double)free_byte ;
        	double total_db = (double)total_byte ;
	        double used_db = total_db - free_db ;

        	std::cout << "GPU memory usage: used = " << used_db/1024.0/1024.0 << "MB, free = " <<
			free_db/1024.0/1024.0 << "MB, total = " << total_db/1024.0/1024.0 << " MB" << std::endl;



		// initial num of blocks
		unsigned long long int num_blocks = 1 + ((SOA - 1) / BLOCK_SIZE);
		std::cout << num_blocks << " blocks initially" << std::endl;

		//allocate host memory for array a
		unsigned long long int mem_size_a = sizeof(int) * SOA;
		if(mem_size_a > free_db)
		{
			std::cout << "Error: Not enough available GPU memory!" << std::endl;
			return 1;
		}

		std::cout << mem_size_a/1024.0/1024.0 << "MB requested" << std::endl;
		int* h_a = (int*)malloc(mem_size_a);

		//allocate device memory
		int* d_a;
		cudaMalloc((void**) &d_a, mem_size_a);

		randomInit(h_a,SOA);

		//copy host memory to device
		cudaMemcpy(d_a, h_a, mem_size_a, cudaMemcpyHostToDevice);

		//allocate device memory for temporary results
		int mem_size_b = sizeof(int) * 1;
		int* d_b;
		cudaMalloc((void**) &d_b, mem_size_b);
		int h_b;

		// Run our kernel wrapper
		reduce(d_a, d_b);

		//copy final result from device to host
		cudaMemcpy(&h_b, d_b, sizeof(int), cudaMemcpyDeviceToHost);

                std::cout << "GPU max: " << h_b << "\n";

		int max = *std::max_element(h_a, h_a + SOA);

		std::cout << "Old-fashioned way: " << max << "\n";

		//clean up memory
		free(h_a);
		cudaFree(d_a);
		cudaFree(d_b);

		cudaThreadExit();

}

	.file	"tmpxft_0024b4b4_00000000-6_max_array.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3952:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3952:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.align 2
	.type	_ZNSolsEPFRSoS_E.isra.0, @function
_ZNSolsEPFRSoS_E.isra.0:
.LFB4613:
	.cfi_startproc
	jmp	*%rsi
	.cfi_endproc
.LFE4613:
	.size	_ZNSolsEPFRSoS_E.isra.0, .-_ZNSolsEPFRSoS_E.isra.0
	.globl	_Z10randomInitPiy
	.type	_Z10randomInitPiy, @function
_Z10randomInitPiy:
.LFB3947:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movq	%rdi, %r12
	xorl	%edi, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	xorl	%ebx, %ebx
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
.L4:
	cmpq	%rbp, %rbx
	je	.L8
	call	rand@PLT
	andl	$2147483647, %eax
	movl	%eax, (%r12,%rbx,4)
	incq	%rbx
	jmp	.L4
.L8:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3947:
	.size	_Z10randomInitPiy, .-_Z10randomInitPiy
	.globl	_Z36__device_stub__Z13ReductionMax2PiS_yPiS_y
	.type	_Z36__device_stub__Z13ReductionMax2PiS_yPiS_y, @function
_Z36__device_stub__Z13ReductionMax2PiS_yPiS_y:
.LFB3974:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L9
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z13ReductionMax2PiS_y(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L9:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3974:
	.size	_Z36__device_stub__Z13ReductionMax2PiS_yPiS_y, .-_Z36__device_stub__Z13ReductionMax2PiS_yPiS_y
	.globl	_Z13ReductionMax2PiS_y
	.type	_Z13ReductionMax2PiS_y, @function
_Z13ReductionMax2PiS_y:
.LFB3975:
	.cfi_startproc
	endbr64
	jmp	_Z36__device_stub__Z13ReductionMax2PiS_yPiS_y
	.cfi_endproc
.LFE3975:
	.size	_Z13ReductionMax2PiS_y, .-_Z13ReductionMax2PiS_y
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"GPU Iteration "
	.text
	.globl	_Z6reducePiS_
	.type	_Z6reducePiS_, @function
_Z6reducePiS_:
.LFB3948:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC0(%rip), %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rsi, %r13
	movl	$4482360, %esi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	$1, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	$1147483647, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	$1120590, %ebx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %r14
	movq	%r14, %rdi
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	cudaMalloc@PLT
	movq	24(%rsp), %rdi
	movl	$4482360, %edx
	xorl	%esi, %esi
	call	cudaMemset@PLT
.L16:
	movq	%r15, %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%r12d, %esi
	incl	%r12d
	movq	%rax, %rdi
	call	_ZNSolsEi@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	%ebx, 44(%rsp)
	movl	$4194305, %edx
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	44(%rsp), %rdi
	salq	$10, %rdx
	xorl	%r9d, %r9d
	movl	52(%rsp), %esi
	movl	$4096, %r8d
	movl	$1, %ecx
	movq	%rdx, 32(%rsp)
	movl	$1, 40(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L15
	movq	24(%rsp), %rsi
	movq	8(%rsp), %rdi
	movq	%rbp, %rdx
	call	_Z36__device_stub__Z13ReductionMax2PiS_yPiS_y
.L15:
	movq	24(%rsp), %rsi
	movq	8(%rsp), %rdi
	movl	$3, %ecx
	decq	%rbp
	leaq	0(,%rbx,4), %rdx
	decq	%rbx
	shrq	$10, %rbp
	call	cudaMemcpy@PLT
	shrq	$10, %rbx
	movq	24(%rsp), %rdi
	incq	%rbp
	incq	%rbx
	call	cudaFree@PLT
	leaq	0(,%rbx,4), %rsi
	movq	%r14, %rdi
	call	cudaMalloc@PLT
	cmpl	$4, %r12d
	jne	.L16
	movl	$4194305, %edx
	xorl	%r9d, %r9d
	movl	$1, %ecx
	movabsq	$4294967297, %rdi
	salq	$10, %rdx
	movl	$4096, %r8d
	movl	$1, %esi
	movl	$1, 52(%rsp)
	movq	%rdx, 44(%rsp)
	movq	%rdi, 32(%rsp)
	movl	$1, 40(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L14
	movq	8(%rsp), %rdi
	movq	%rbp, %rdx
	movq	%r13, %rsi
	call	_Z36__device_stub__Z13ReductionMax2PiS_yPiS_y
.L14:
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L18
	call	__stack_chk_fail@PLT
.L18:
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	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
.LFE3948:
	.size	_Z6reducePiS_, .-_Z6reducePiS_
	.section	.rodata.str1.1
.LC1:
	.string	"Finding max element of an array on a GPU"
.LC2:
	.string	"Error: cudaMemGetInfo fails, %s \n"
.LC3:
	.string	"GPU memory usage: used = "
.LC5:
	.string	"MB, free = "
.LC6:
	.string	"MB, total = "
.LC7:
	.string	" MB"
.LC8:
	.string	" blocks initially"
.LC10:
	.string	"Error: Not enough available GPU memory!"
.LC12:
	.string	"MB requested"
.LC13:
	.string	"GPU max: "
.LC14:
	.string	"\n"
.LC15:
	.string	"Old-fashioned way: "
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3949:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	leaq	.LC1(%rip), %rsi
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	leaq	_ZSt4cout(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movq	%rbp, %rdi
	subq	$80, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	leaq	48(%rsp), %rsi
	leaq	40(%rsp), %rdi
	call	cudaMemGetInfo@PLT
	testl	%eax, %eax
	je	.L22
	movl	%eax, %edi
	call	cudaGetErrorString@PLT
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L23
.L22:
	movq	40(%rsp), %rax
	testq	%rax, %rax
	js	.L24
	cvtsi2sdq	%rax, %xmm2
	jmp	.L25
.L24:
	movq	%rax, %rdx
	andl	$1, %eax
	shrq	%rdx
	orq	%rax, %rdx
	cvtsi2sdq	%rdx, %xmm2
	addsd	%xmm2, %xmm2
.L25:
	movq	48(%rsp), %rax
	testq	%rax, %rax
	js	.L26
	cvtsi2sdq	%rax, %xmm1
	jmp	.L27
.L26:
	movq	%rax, %rdx
	andl	$1, %eax
	shrq	%rdx
	orq	%rax, %rdx
	cvtsi2sdq	%rdx, %xmm1
	addsd	%xmm1, %xmm1
.L27:
	movaps	%xmm1, %xmm0
	leaq	.LC3(%rip), %rsi
	movq	%rbp, %rdi
	movsd	%xmm1, 24(%rsp)
	subsd	%xmm2, %xmm0
	movsd	%xmm2, 16(%rsp)
	movsd	%xmm0, 8(%rsp)
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movsd	8(%rsp), %xmm0
	mulsd	.LC4(%rip), %xmm0
	mulsd	.LC4(%rip), %xmm0
	movq	%rax, %rdi
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	leaq	.LC5(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movsd	16(%rsp), %xmm2
	movsd	.LC4(%rip), %xmm0
	movq	%rax, %rdi
	mulsd	%xmm2, %xmm0
	movsd	%xmm2, 8(%rsp)
	mulsd	.LC4(%rip), %xmm0
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	leaq	.LC6(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movsd	24(%rsp), %xmm1
	mulsd	.LC4(%rip), %xmm1
	movsd	.LC4(%rip), %xmm0
	movq	%rax, %rdi
	mulsd	%xmm1, %xmm0
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	leaq	.LC7(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$1120590, %esi
	movq	%rbp, %rdi
	call	_ZNSo9_M_insertIyEERSoT_@PLT
	leaq	.LC8(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movsd	.LC9(%rip), %xmm0
	movsd	8(%rsp), %xmm2
	comisd	%xmm2, %xmm0
	jbe	.L37
	leaq	.LC10(%rip), %rsi
	movq	%rbp, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
.L23:
	movl	$1, %eax
	jmp	.L21
.L37:
	movsd	.LC11(%rip), %xmm0
	movq	%rbp, %rdi
	movabsq	$4589934588, %r12
	leaq	.LC14(%rip), %r13
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	leaq	.LC12(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movq	%r12, %rdi
	call	malloc@PLT
	movq	%r12, %rsi
	leaq	56(%rsp), %rdi
	movq	%rax, %rbx
	call	cudaMalloc@PLT
	movl	$1147483647, %esi
	movq	%rbx, %rdi
	call	_Z10randomInitPiy
	movq	56(%rsp), %rdi
	movq	%r12, %rdx
	movq	%rbx, %rsi
	movl	$1, %ecx
	addq	%rbx, %r12
	call	cudaMemcpy@PLT
	leaq	64(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
	movq	64(%rsp), %rsi
	movq	56(%rsp), %rdi
	call	_Z6reducePiS_
	movq	64(%rsp), %rsi
	movl	$4, %edx
	movl	$2, %ecx
	leaq	36(%rsp), %rdi
	call	cudaMemcpy@PLT
	leaq	.LC13(%rip), %rsi
	movq	%rbp, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	36(%rsp), %esi
	movq	%rax, %rdi
	call	_ZNSolsEi@PLT
	movq	%r13, %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rbx, %rdx
	movq	%rbx, %rax
.L31:
	addq	$4, %rax
	movl	(%rdx), %r14d
	cmpq	%rax, %r12
	je	.L39
	cmpl	%r14d, (%rax)
	cmovg	%rax, %rdx
	jmp	.L31
.L39:
	leaq	.LC15(%rip), %rsi
	movq	%rbp, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%r14d, %esi
	movq	%rax, %rdi
	call	_ZNSolsEi@PLT
	movq	%r13, %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	call	cudaThreadExit@PLT
	xorl	%eax, %eax
.L21:
	movq	72(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L34
	call	__stack_chk_fail@PLT
.L34:
	addq	$80, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3949:
	.size	main, .-main
	.section	.rodata.str1.1
.LC16:
	.string	"_Z13ReductionMax2PiS_y"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3977:
	.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	.LC16(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z13ReductionMax2PiS_y(%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
.LFE3977:
	.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	1062207488
	.align 8
.LC9:
	.long	2143289344
	.long	1106319693
	.align 8
.LC11:
	.long	2143289344
	.long	1085348173
	.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 <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctime>
#include <algorithm>

// Thread block size
#define BLOCK_SIZE 1024

//  Size of Array
// #define SOA 67107840
// #define SOA 2147483647
#define SOA 1147483647
// #define SOA 8193

// Allocates an array with random integer entries.
void randomInit(int* data, unsigned long long int size)
{
	srand( time(0) );
	for (unsigned long long int i = 0; i < size; ++i)
	{
		data[i] = rand() & INT_MAX;
		//std::cout << data[i] << "\n";
	}
}

__global__ void ReductionMax2(int *input, int *results, unsigned long long int n)    //take thread divergence into account
{
	extern __shared__ int sdata[];
	unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
	unsigned int tx = threadIdx.x;
	 //load input into __shared__ memory
	if(i < n)
	{
		sdata[tx] = input[i];
	}
	else
	{
		sdata[tx] = INT_MIN;
	}

	__syncthreads();

	// block-wide reduction
	for(unsigned int offset = 1; offset < blockDim.x; offset <<= 1)
	{
		int index = 2 * offset * tx;
		if(index < blockDim.x)
	        {
			// sdata[index] += sdata[index + offset];
			if(sdata[index] < sdata[index + offset]) sdata[index] = sdata[index + offset];
		}
		__syncthreads();
	}

	// finally, thread 0 writes the result
	if(threadIdx.x == 0)
	{
		// the result is per-block
		results[blockIdx.x] = sdata[0];
	}
}

// Reduce function wrapper
	void reduce(int* d_a, int* d_b)
	{
		unsigned long long int arraySize = SOA;
		unsigned long long int numBlocks = 1 + ((SOA - 1) / BLOCK_SIZE);

		int* device_intermediate;
		hipMalloc(&device_intermediate, sizeof(int)*numBlocks);
  		hipMemset(device_intermediate, 0, sizeof(int)*numBlocks);

		int i=1;

		do
		{
			std::cout << "GPU Iteration " << i << std::endl;
			i++;

			//setup execution parameters
                	dim3 block(BLOCK_SIZE);
	                dim3 grid(numBlocks);

        	        //execute the kernel
	                ReductionMax2<<<grid, block, BLOCK_SIZE*sizeof(int)>>>(d_a,device_intermediate,arraySize);
			arraySize = 1 + ((arraySize - 1) / BLOCK_SIZE);

			// device_in to device_intermediate
			hipMemcpy(d_a, device_intermediate, sizeof(int)*numBlocks, hipMemcpyDeviceToDevice);

			// Update required number of blocks
			numBlocks = 1 + ((numBlocks - 1) / BLOCK_SIZE);

			hipFree(device_intermediate);
			hipMalloc(&device_intermediate, sizeof(int)*numBlocks);
		}
		while(arraySize > BLOCK_SIZE);

		// Now compute the rest
		ReductionMax2<<<1, BLOCK_SIZE, BLOCK_SIZE*sizeof(int)>>>(d_a,d_b,arraySize);
	}


// get global max element via per-block reductions
	int main(int argc, char **argv)
	{
		// Introduce program
		std::cout << "Finding max element of an array on a GPU" << std::endl;

		// show memory usage of GPU
        	size_t free_byte ;
	        size_t total_byte ;
		hipError_t cuda_status = hipMemGetInfo( &free_byte, &total_byte ) ;

        	if ( hipSuccess != cuda_status )
		{
            		printf("Error: hipMemGetInfo fails, %s \n", hipGetErrorString(cuda_status) );
		        return 1;
		}

	        double free_db = (double)free_byte ;
        	double total_db = (double)total_byte ;
	        double used_db = total_db - free_db ;

        	std::cout << "GPU memory usage: used = " << used_db/1024.0/1024.0 << "MB, free = " <<
			free_db/1024.0/1024.0 << "MB, total = " << total_db/1024.0/1024.0 << " MB" << std::endl;



		// initial num of blocks
		unsigned long long int num_blocks = 1 + ((SOA - 1) / BLOCK_SIZE);
		std::cout << num_blocks << " blocks initially" << std::endl;

		//allocate host memory for array a
		unsigned long long int mem_size_a = sizeof(int) * SOA;
		if(mem_size_a > free_db)
		{
			std::cout << "Error: Not enough available GPU memory!" << std::endl;
			return 1;
		}

		std::cout << mem_size_a/1024.0/1024.0 << "MB requested" << std::endl;
		int* h_a = (int*)malloc(mem_size_a);

		//allocate device memory
		int* d_a;
		hipMalloc((void**) &d_a, mem_size_a);

		randomInit(h_a,SOA);

		//copy host memory to device
		hipMemcpy(d_a, h_a, mem_size_a, hipMemcpyHostToDevice);

		//allocate device memory for temporary results
		int mem_size_b = sizeof(int) * 1;
		int* d_b;
		hipMalloc((void**) &d_b, mem_size_b);
		int h_b;

		// Run our kernel wrapper
		reduce(d_a, d_b);

		//copy final result from device to host
		hipMemcpy(&h_b, d_b, sizeof(int), hipMemcpyDeviceToHost);

                std::cout << "GPU max: " << h_b << "\n";

		int max = *std::max_element(h_a, h_a + SOA);

		std::cout << "Old-fashioned way: " << max << "\n";

		//clean up memory
		free(h_a);
		hipFree(d_a);
		hipFree(d_b);

		hipDeviceReset();

}

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

                                        # End of file scope inline assembly
	.globl	_Z10randomInitPiy               # -- Begin function _Z10randomInitPiy
	.type	_Z10randomInitPiy,@function
_Z10randomInitPiy:                      # @_Z10randomInitPiy
	.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
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rbx
	movq	%rdi, %r14
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	testq	%rbx, %rbx
	je	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	xorl	%r15d, %r15d
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	andl	$2147483647, %eax               # imm = 0x7FFFFFFF
	movl	%eax, (%r14,%r15,4)
	incq	%r15
	cmpq	%r15, %rbx
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z10randomInitPiy, .Lfunc_end0-_Z10randomInitPiy
	.cfi_endproc
                                        # -- End function
	.globl	_Z28__device_stub__ReductionMax2PiS_y # -- Begin function _Z28__device_stub__ReductionMax2PiS_y
	.type	_Z28__device_stub__ReductionMax2PiS_y,@function
_Z28__device_stub__ReductionMax2PiS_y:  # @_Z28__device_stub__ReductionMax2PiS_y
	.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	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%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	$_Z13ReductionMax2PiS_y, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	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_end1:
	.size	_Z28__device_stub__ReductionMax2PiS_y, .Lfunc_end1-_Z28__device_stub__ReductionMax2PiS_y
	.cfi_endproc
                                        # -- End function
	.globl	_Z6reducePiS_                   # -- Begin function _Z6reducePiS_
	.type	_Z6reducePiS_,@function
_Z6reducePiS_:                          # @_Z6reducePiS_
	.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	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, 16(%rsp)                  # 8-byte Spill
	movq	%rdi, %r14
	movabsq	$4294968320, %r15               # imm = 0x100000400
	leaq	8(%rsp), %rbx
	movl	$4482360, %esi                  # imm = 0x446538
	movq	%rbx, %rdi
	callq	hipMalloc
	movq	(%rbx), %rdi
	movl	$4482360, %edx                  # imm = 0x446538
	xorl	%esi, %esi
	callq	hipMemset
	movl	$1147483647, %r13d              # imm = 0x446535FF
	movl	$1120590, %r12d                 # imm = 0x11194E
	movl	$1, %ebp
.LBB2_1:                                # =>This Inner Loop Header: Depth=1
	movl	$_ZSt4cout, %edi
	movl	$.L.str, %esi
	movl	$14, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	$_ZSt4cout, %edi
	movl	%ebp, %esi
	callq	_ZNSolsEi
	movq	%rax, %rbx
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%rbx, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	%r12d, %eax
	leaq	(%r15,%rax), %rdi
	addq	$-1024, %rdi                    # imm = 0xFC00
	movl	$4096, %r8d                     # imm = 0x1000
	movl	$1, %esi
	movq	%r15, %rdx
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_3
# %bb.2:                                #   in Loop: Header=BB2_1 Depth=1
	movq	8(%rsp), %rsi
	movq	%r14, %rdi
	movq	%r13, %rdx
	callq	_Z28__device_stub__ReductionMax2PiS_y
.LBB2_3:                                #   in Loop: Header=BB2_1 Depth=1
	incl	%ebp
	decq	%r13
	shrq	$10, %r13
	incq	%r13
	movq	8(%rsp), %rsi
	leaq	(,%r12,4), %rdx
	movq	%r14, %rdi
	movl	$3, %ecx
	callq	hipMemcpy
	decq	%r12
	shrq	$10, %r12
	leaq	1(%r12), %rbx
	movq	8(%rsp), %rdi
	callq	hipFree
	leaq	4(,%r12,4), %rsi
	leaq	8(%rsp), %rdi
	callq	hipMalloc
	movq	%rbx, %r12
	cmpl	$4, %ebp
	jne	.LBB2_1
# %bb.4:
	leaq	-1023(%r15), %rdi
	movl	$4096, %r8d                     # imm = 0x1000
	movl	$1, %esi
	movq	%r15, %rdx
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_6
# %bb.5:
	movl	$2, %edx
	movq	%r14, %rdi
	movq	16(%rsp), %rsi                  # 8-byte Reload
	callq	_Z28__device_stub__ReductionMax2PiS_y
.LBB2_6:
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.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	_Z6reducePiS_, .Lfunc_end2-_Z6reducePiS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function main
.LCPI3_0:
	.long	1127219200                      # 0x43300000
	.long	1160773632                      # 0x45300000
	.long	0                               # 0x0
	.long	0                               # 0x0
.LCPI3_1:
	.quad	0x4330000000000000              # double 4503599627370496
	.quad	0x4530000000000000              # double 1.9342813113834067E+25
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI3_2:
	.quad	0x3f50000000000000              # double 9.765625E-4
.LCPI3_3:
	.quad	0x40b1194d7fc00000              # double 4377.3027305603027
	.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	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$96, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str.1, %esi
	movl	$40, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	leaq	32(%rsp), %rdi
	leaq	24(%rsp), %rsi
	callq	hipMemGetInfo
	testl	%eax, %eax
	je	.LBB3_3
# %bb.1:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.2, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB3_2
.LBB3_3:
	movq	32(%rsp), %r14
	movq	%r14, %xmm0
	movdqa	.LCPI3_0(%rip), %xmm1           # xmm1 = [1127219200,1160773632,0,0]
	punpckldq	%xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
	movapd	.LCPI3_1(%rip), %xmm2           # xmm2 = [4.503599627370496E+15,1.9342813113834067E+25]
	subpd	%xmm2, %xmm0
	movapd	%xmm0, %xmm3
	unpckhpd	%xmm0, %xmm3                    # xmm3 = xmm3[1],xmm0[1]
	addsd	%xmm0, %xmm3
	movapd	%xmm3, 64(%rsp)                 # 16-byte Spill
	movq	24(%rsp), %xmm0                 # xmm0 = mem[0],zero
	punpckldq	%xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
	subpd	%xmm2, %xmm0
	movapd	%xmm0, %xmm1
	unpckhpd	%xmm0, %xmm1                    # xmm1 = xmm1[1],xmm0[1]
	addsd	%xmm0, %xmm1
	movapd	%xmm1, 80(%rsp)                 # 16-byte Spill
	subsd	%xmm3, %xmm1
	movapd	%xmm1, 48(%rsp)                 # 16-byte Spill
	movl	$_ZSt4cout, %r15d
	movl	$_ZSt4cout, %edi
	movl	$.L.str.3, %esi
	movl	$25, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movsd	.LCPI3_2(%rip), %xmm1           # xmm1 = mem[0],zero
	movapd	48(%rsp), %xmm0                 # 16-byte Reload
	mulsd	%xmm1, %xmm0
	mulsd	%xmm1, %xmm0
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	%rax, %rbx
	movl	$.L.str.4, %esi
	movl	$11, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movapd	64(%rsp), %xmm0                 # 16-byte Reload
	movsd	.LCPI3_2(%rip), %xmm1           # xmm1 = mem[0],zero
	mulsd	%xmm1, %xmm0
	mulsd	%xmm1, %xmm0
	movq	%rbx, %rdi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	%rax, %rbx
	movl	$.L.str.5, %esi
	movl	$12, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movapd	80(%rsp), %xmm0                 # 16-byte Reload
	movsd	.LCPI3_2(%rip), %xmm1           # xmm1 = mem[0],zero
	mulsd	%xmm1, %xmm0
	mulsd	%xmm1, %xmm0
	movq	%rbx, %rdi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	%rax, %rbx
	movl	$.L.str.6, %esi
	movl	$3, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	(%rbx), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%rbx, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$_ZSt4cout, %edi
	movl	$1120590, %esi                  # imm = 0x11194E
	callq	_ZNSo9_M_insertIyEERSoT_
	movq	%rax, %rbx
	movl	$.L.str.7, %esi
	movl	$17, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	(%rbx), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%rbx, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	shrq	$2, %r14
	cmpq	$1147483646, %r14               # imm = 0x446535FE
	ja	.LBB3_5
# %bb.4:
	movl	$_ZSt4cout, %edi
	movl	$.L.str.8, %esi
	movl	$39, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r15, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.LBB3_2:
	movl	$1, %eax
.LBB3_8:
	addq	$96, %rsp
	.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
.LBB3_5:
	.cfi_def_cfa_offset 144
	movabsq	$4589934588, %rbx               # imm = 0x11194D7FC
	movsd	.LCPI3_3(%rip), %xmm0           # xmm0 = mem[0],zero
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	%rax, %r14
	movl	$.L.str.9, %esi
	movl	$12, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	(%r14), %rax
	movq	-24(%rax), %rdi
	addq	%r14, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r14, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r14
	leaq	16(%rsp), %r15
	movq	%r15, %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	movl	$1147483647, %esi               # imm = 0x446535FF
	movq	%r14, %rdi
	callq	_Z10randomInitPiy
	movq	(%r15), %rdi
	movq	%r14, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	8(%rsp), %r12
	movl	$4, %r13d
	movl	$4, %esi
	movq	%r12, %rdi
	callq	hipMalloc
	movq	(%r15), %rdi
	movq	(%r12), %rsi
	callq	_Z6reducePiS_
	movq	(%r12), %rsi
	leaq	44(%rsp), %r15
	movl	$4, %edx
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movl	$_ZSt4cout, %edi
	movl	$.L.str.10, %esi
	movl	$9, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	(%r15), %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSolsEi
	movl	$.L.str.11, %esi
	movl	$1, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	(%r14), %eax
	movq	%r14, %rcx
.LBB3_6:                                # %.lr.ph.i.i
                                        # =>This Inner Loop Header: Depth=1
	leaq	(%r14,%r13), %rdx
	movl	(%r14,%r13), %esi
	cmpl	%esi, %eax
	cmovlel	%esi, %eax
	cmovlq	%rdx, %rcx
	addq	$4, %r13
	cmpq	%rbx, %r13
	jne	.LBB3_6
# %bb.7:                                # %_ZSt11max_elementIPiET_S1_S1_.exit
	movl	(%rcx), %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str.12, %esi
	movl	$19, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	$_ZSt4cout, %edi
	movl	%ebx, %esi
	callq	_ZNSolsEi
	movl	$.L.str.11, %esi
	movl	$1, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	%r14, %rdi
	callq	free
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	callq	hipDeviceReset
	xorl	%eax, %eax
	jmp	.LBB3_8
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.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	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z13ReductionMax2PiS_y, %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_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__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	.LBB5_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
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z13ReductionMax2PiS_y,@object  # @_Z13ReductionMax2PiS_y
	.section	.rodata,"a",@progbits
	.globl	_Z13ReductionMax2PiS_y
	.p2align	3, 0x0
_Z13ReductionMax2PiS_y:
	.quad	_Z28__device_stub__ReductionMax2PiS_y
	.size	_Z13ReductionMax2PiS_y, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"GPU Iteration "
	.size	.L.str, 15

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Finding max element of an array on a GPU"
	.size	.L.str.1, 41

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Error: hipMemGetInfo fails, %s \n"
	.size	.L.str.2, 33

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"GPU memory usage: used = "
	.size	.L.str.3, 26

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"MB, free = "
	.size	.L.str.4, 12

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"MB, total = "
	.size	.L.str.5, 13

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	" MB"
	.size	.L.str.6, 4

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	" blocks initially"
	.size	.L.str.7, 18

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Error: Not enough available GPU memory!"
	.size	.L.str.8, 40

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"MB requested"
	.size	.L.str.9, 13

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"GPU max: "
	.size	.L.str.10, 10

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

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Old-fashioned way: "
	.size	.L.str.12, 20

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z13ReductionMax2PiS_y"
	.size	.L__unnamed_1, 23

	.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

	.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 _Z28__device_stub__ReductionMax2PiS_y
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z13ReductionMax2PiS_y
	.addrsig_sym _ZSt4cout
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z13ReductionMax2PiS_y  ; -- Begin function _Z13ReductionMax2PiS_y
	.globl	_Z13ReductionMax2PiS_y
	.p2align	8
	.type	_Z13ReductionMax2PiS_y,@function
_Z13ReductionMax2PiS_y:                 ; @_Z13ReductionMax2PiS_y
; %bb.0:
	s_clause 0x2
	s_load_b32 s5, s[0:1], 0x24
	s_load_b64 s[6:7], s[0:1], 0x10
	s_load_b128 s[0:3], s[0:1], 0x0
	s_mov_b32 s4, s15
	v_bfrev_b32_e32 v3, 1
	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]
	v_mov_b32_e32 v2, 0
	v_cmp_gt_u64_e32 vcc_lo, s[6:7], v[1:2]
	s_and_saveexec_b32 s6, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	v_lshlrev_b64 v[1:2], 2, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v1, vcc_lo, s0, v1
	v_add_co_ci_u32_e32 v2, vcc_lo, s1, v2, vcc_lo
	global_load_b32 v3, v[1:2], off
.LBB0_2:
	s_or_b32 exec_lo, exec_lo, s6
	v_lshl_add_u32 v1, v0, 2, 0
	s_cmp_lt_u32 s5, 2
	s_waitcnt vmcnt(0)
	ds_store_b32 v1, v3
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_8
; %bb.3:                                ; %.lr.ph.preheader
	s_mov_b32 s0, 1
.LBB0_4:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	s_mov_b32 s6, s0
	s_lshl_b32 s0, s0, 1
	s_mov_b32 s1, exec_lo
	v_mul_lo_u32 v1, s0, v0
	v_cmpx_gt_u32_e64 s5, v1
	s_cbranch_execz .LBB0_7
; %bb.5:                                ;   in Loop: Header=BB0_4 Depth=1
	v_lshl_add_u32 v1, v1, 2, 0
	s_delay_alu instid0(VALU_DEP_1)
	v_lshl_add_u32 v2, s6, 2, v1
	ds_load_b32 v3, v1
	ds_load_b32 v2, v2
	s_waitcnt lgkmcnt(0)
	v_cmp_lt_i32_e32 vcc_lo, v3, v2
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_7
; %bb.6:                                ;   in Loop: Header=BB0_4 Depth=1
	ds_store_b32 v1, v2
.LBB0_7:                                ;   in Loop: Header=BB0_4 Depth=1
	s_or_b32 exec_lo, exec_lo, s1
	s_cmp_ge_u32 s0, s5
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB0_4
.LBB0_8:                                ; %._crit_edge
	s_mov_b32 s0, exec_lo
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB0_10
; %bb.9:
	v_mov_b32_e32 v0, 0
	s_mov_b32 s5, 0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b64 s[0:1], s[4:5], 2
	s_add_u32 s0, s2, s0
	ds_load_b32 v1, v0
	s_addc_u32 s1, s3, s1
	s_waitcnt lgkmcnt(0)
	global_store_b32 v0, v1, s[0:1]
.LBB0_10:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z13ReductionMax2PiS_y
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.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 4
		.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	_Z13ReductionMax2PiS_y, .Lfunc_end0-_Z13ReductionMax2PiS_y
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 352
; NumSgprs: 18
; NumVgprs: 4
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 4
; 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:           8
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
      - .offset:         144
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z13ReductionMax2PiS_y
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z13ReductionMax2PiS_y.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     4
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
