#include "user_device.cuh"

__device__ float global_cache[GLOBAL_CACHE_SIZE];


/**
 * function name: atomicMax_float
 * Return Type: float
 * Description:
 *      "atomicMax" for float.
 *      Compare old value (*maxVal) and new value (value).
 *      If new value is larger than old value, than new value will overwrite old value.
 *      This function will return old value.
 */
__device__
float atomicMax_float(float *maxVal, float value) {
    float f_old = *maxVal;
    return atomicCAS((int *)maxVal, __float_as_int(f_old), __float_as_int(f_old < value ? value : f_old));
}


/**
 * function name: device_maxValueVector
 * Return Type: void
 * Description:
 *      This kernel function find max value from large vector.
 *      Stage 1: Find max value from partial vector which allocated to each thread.
 *      Stage 2: Find max value from thread block.
 *      Stage 3: Find max value from grid.
 */
__global__
void device_maxValueVector(float *vec, float *p_maxVal, int vector_size, int *p_block_cnt, int numOps) {
    extern __shared__ float cache[];
    int index = blockDim.x * blockIdx.x + threadIdx.x;
    int offset = gridDim.x * blockDim.x;
    float tmpMax = -INFINITY, tmpCmp;
    
    // Stage 1: Thread. Get max value of thread's partial vector.
    for (int i = 0; i < numOps; i++) {
        if (index < vector_size) {
            tmpCmp = vec[index];
        } else {    // index is out of range.
            tmpCmp = -INFINITY;
        }
        tmpMax = (tmpMax < tmpCmp) ? tmpCmp : tmpMax;
        index += offset;
    }
    cache[threadIdx.x] = tmpMax;
    __syncthreads();

    // Stage 2: Thread Block. Get max value from 'global_cache[]'
    if (threadIdx.x == 0) {
        for (int i = 0; i < blockDim.x; i++) {
            tmpCmp = cache[i];
            tmpMax = (tmpMax < tmpCmp) ? tmpCmp : tmpMax;
        }
        global_cache[blockIdx.x] = tmpMax;
        atomicAdd(p_block_cnt, 1);  // Counter for synchronization of thread blocks.
    } else {
        return;
    }

    // Stage 3: Grid, Get max value of from thread blocks' results.
    if ((*p_block_cnt) == gridDim.x) {
        for (int i = 0; i < gridDim.x; i++) {
            tmpCmp = global_cache[i];
            tmpMax = (tmpMax < tmpCmp) ? tmpCmp : tmpMax;
        }
        (*p_maxVal) = tmpMax;
    } else {
        return;
    }
}

	.file	"tmpxft_002977f5_00000000-6_user_device.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB4853:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE4853:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z15atomicMax_floatPff
	.type	_Z15atomicMax_floatPff, @function
_Z15atomicMax_floatPff:
.LFB4850:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE4850:
	.size	_Z15atomicMax_floatPff, .-_Z15atomicMax_floatPff
	.globl	_Z47__device_stub__Z21device_maxValueVectorPfS_iPiiPfS_iPii
	.type	_Z47__device_stub__Z21device_maxValueVectorPfS_iPiiPfS_iPii, @function
_Z47__device_stub__Z21device_maxValueVectorPfS_iPiiPfS_iPii:
.LFB4875:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rsp)
	leaq	40(%rsp), %rcx
	movl	%r8d, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%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	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	movq	%rsp, %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L4
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z21device_maxValueVectorPfS_iPii(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	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 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L4:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L6
	call	__stack_chk_fail@PLT
.L6:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4875:
	.size	_Z47__device_stub__Z21device_maxValueVectorPfS_iPiiPfS_iPii, .-_Z47__device_stub__Z21device_maxValueVectorPfS_iPiiPfS_iPii
	.globl	_Z21device_maxValueVectorPfS_iPii
	.type	_Z21device_maxValueVectorPfS_iPii, @function
_Z21device_maxValueVectorPfS_iPii:
.LFB4876:
	.cfi_startproc
	endbr64
	jmp	_Z47__device_stub__Z21device_maxValueVectorPfS_iPiiPfS_iPii
	.cfi_endproc
.LFE4876:
	.size	_Z21device_maxValueVectorPfS_iPii, .-_Z21device_maxValueVectorPfS_iPii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z21device_maxValueVectorPfS_iPii"
.LC1:
	.string	"global_cache"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB4878:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -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	.LC0(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z21device_maxValueVectorPfS_iPii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC1(%rip), %rdx
	movl	$2048, %r9d
	leaq	_ZL12global_cache(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	call	__cudaRegisterVar@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE4878:
	.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
	.local	_ZL12global_cache
	.comm	_ZL12global_cache,2048,32
	.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:

#ifndef USER_DEVICE_H
#define USER_DEVICE_H

#include "user.cuh"

#define GLOBAL_CACHE_SIZE 512

#define NUM_THREADS_BASE        32
#define NUM_THREADS_MAX         1024
#define NUM_THREAD_BLKS_FROM    1
#define NUM_THREAD_BLKS_TO      512
#define ITERATION               10

__global__
void device_maxValueVector(float *vec, float *p_maxVal, int vector_size, int *p_block_cnt, int numOps);

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

                                        # End of file scope inline assembly
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
