#include <cuda.h>
#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include <chrono>

#define CUDA_CHECK(status) (assert(status == cudaSuccess))
#define threads_per_block 1024


// sum the shared data reductions into a single one
// loop unrolled version for increased performance
// Note: do not remove volatile!
// Note: some redudant calculation is done due to bank conflicts / sleeping threads
__device__ void columnWarpReduce(volatile float* sdata, int tid) {
    sdata[tid] += sdata[tid + 16];
    sdata[tid] += sdata[tid + 8];
    sdata[tid] += sdata[tid + 4];
    sdata[tid] += sdata[tid + 2];
    sdata[tid] += sdata[tid + 1];
}

__global__ void column_reduce(float * matrix, float * result, int m /* lines */, int n /* columns*/) {
    extern __shared__ float sdata[];
    
    // normal tid
    int tid = threadIdx.x + threadIdx.y * blockDim.x;
    
    // transposed tid for shared memory
    int new_tid = threadIdx.y + threadIdx.x * blockDim.y;

    // true x value in the matrix
    int real_x = threadIdx.x + blockDim.x * blockIdx.x;
    
    int i = real_x + n * threadIdx.y;
    const int it = n*blockDim.y;
    int offset = it;
    float accumulator = 0;

    if (threadIdx.y < m && real_x < n) {
        // store all the values from this column in a warped way
        accumulator = matrix[i];
        while (i + offset < n*m) {
            accumulator += matrix[i + offset];
            offset += it;
        }
    }

    // save column reduction data in a transposed way
    sdata[new_tid] = accumulator;
    __syncthreads();

    // avoid last warp to run causing memory errors
    if (tid < 32 * 32 - 16) { 
        columnWarpReduce(sdata, tid);
    }
    __syncthreads();
    
    if (threadIdx.y == 0 && real_x < n) 
        result[real_x] = sdata[new_tid];
    
}

int main(int argc, char * argv[])  {
    if (argc < 3) {
        printf("Usage: %s <m> <n>\n", argv[0]);
        return 0;
    }

    int m = atoi(argv[1]), n = atoi(argv[2]);
    
    unsigned long seed = 1620144156; //time(NULL);
    srand(seed); // seed 
    printf("Running with seed %ld\n", seed);

    // create row-major matrix m x n
    float * matrix = (float *) malloc(sizeof(float) * m * n); // m x n

    // create array to store result
    float * result_gpu = (float *) malloc(sizeof(float) * n); // tot_num_blocks x 1
    float * result_cpu = (float *) malloc(sizeof(float) * n); // validation
    memset(result_cpu, 0, sizeof(float) * n);

    printf("Populating array \n");
    // populate the array 
    for (int i = 0; i < m * n; i++)
        matrix[i] = 1.0 / ((rand() % 977) + 1);
    

    printf("Calculating final result\n");
    auto cpu_start = std::chrono::high_resolution_clock::now();
    
    // calculate cpu result
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) 
            result_cpu[j] += matrix[i * n + j];
    auto cpu_end = std::chrono::high_resolution_clock::now();

    printf("CPU took %f ms.\n", std::chrono::duration_cast<std::chrono::microseconds>(cpu_end - cpu_start).count() / 1000.0);
    

    printf("Allocating GPU memory, m=%d, n=%d\n", m, n);
    // allocate gpu memory
    float * matrix_gpu, * device_result, * helper_result = NULL;
    
    CUDA_CHECK(cudaMalloc(&matrix_gpu, sizeof(float) * m * n));
    CUDA_CHECK(cudaMalloc(&device_result, sizeof(float) * n));
    CUDA_CHECK(cudaMemset(device_result, 0, sizeof(float) * n));
    printf("Finished allocating. Copying matrix...\n");
    // move matrix into gpu
    CUDA_CHECK(cudaMemcpy(matrix_gpu, matrix, m * n * sizeof(float), cudaMemcpyHostToDevice));

    
    cudaEvent_t start, stop;
    float elapsed_time;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    // call kernel
    dim3 block_threads(32, 32);
    dim3 grid_threads(n / 32 + (n % 32 ? 1 : 0), 1);

    printf("Calling kernel with m=%d n=%d, gridsize=(%d,%d)\n", m, n, grid_threads.x, grid_threads.y);

    CUDA_CHECK(cudaEventRecord(start));
    column_reduce<<<grid_threads, block_threads, sizeof(float)*32*32>>>(matrix_gpu, device_result, m, n);
    CUDA_CHECK(cudaEventRecord(stop));

    // Wait for final kernel to finish
    CUDA_CHECK(cudaDeviceSynchronize());
    // end = std::chrono::high_resolution_clock::now();
    
    cudaEventElapsedTime(&elapsed_time, start, stop);
    printf("Kernel finished. Took %f ms. Copying back results.\n", elapsed_time);
    // copy back results
    CUDA_CHECK(cudaMemcpy(result_gpu, device_result, n * sizeof(float), cudaMemcpyDeviceToHost));
    
    // free gpu memory
    CUDA_CHECK(cudaFree(matrix_gpu));
    CUDA_CHECK(cudaFree(device_result));
    if (helper_result) CUDA_CHECK(cudaFree(helper_result));
    
    printf("Released GPU memory. Validating results...\n");
    // compare results
    for (int i = 0; i < n; i++) {
        if (abs(result_cpu[i] - result_gpu[i]) > 1e-3) 
            printf("INCORRECT RESULT: cpu=%.10f gpu=%.10f @ index=%d, diff=%.10f\n", result_cpu[i], result_gpu[i], i, result_cpu[i] - result_gpu[i]);
        // else printf("Correct result! cpu=%.10f, gpu=%.10f, diff=%.10f\n", result_cpu[i], result_gpu[i], result_cpu[i] - result_gpu[i]);
    }
    
    free(result_gpu);
    free(result_cpu);
    free(matrix);
    return 0;
}
	.file	"tmpxft_002e7c94_00000000-6_main.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3740:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3740:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z16columnWarpReducePVfi
	.type	_Z16columnWarpReducePVfi, @function
_Z16columnWarpReducePVfi:
.LFB3734:
	.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
.LFE3734:
	.size	_Z16columnWarpReducePVfi, .-_Z16columnWarpReducePVfi
	.globl	_Z37__device_stub__Z13column_reducePfS_iiPfS_ii
	.type	_Z37__device_stub__Z13column_reducePfS_iiPfS_ii, @function
_Z37__device_stub__Z13column_reducePfS_iiPfS_ii:
.LFB3762:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L4
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z13column_reducePfS_ii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%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
.LFE3762:
	.size	_Z37__device_stub__Z13column_reducePfS_iiPfS_ii, .-_Z37__device_stub__Z13column_reducePfS_iiPfS_ii
	.globl	_Z13column_reducePfS_ii
	.type	_Z13column_reducePfS_ii, @function
_Z13column_reducePfS_ii:
.LFB3763:
	.cfi_startproc
	endbr64
	jmp	_Z37__device_stub__Z13column_reducePfS_iiPfS_ii
	.cfi_endproc
.LFE3763:
	.size	_Z13column_reducePfS_ii, .-_Z13column_reducePfS_ii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Usage: %s <m> <n>\n"
.LC1:
	.string	"Running with seed %ld\n"
.LC2:
	.string	"Populating array \n"
.LC4:
	.string	"Calculating final result\n"
.LC6:
	.string	"CPU took %f ms.\n"
.LC7:
	.string	"Allocating GPU memory, m=%d, n=%d\n"
.LC8:
	.string	"Finished allocating. Copying matrix...\n"
.LC9:
	.string	"Calling kernel with m=%d n=%d, gridsize=(%d,%d)\n"
.LC10:
	.string	"Kernel finished. Took %f ms. Copying back results.\n"
.LC11:
	.string	"Released GPU memory. Validating results...\n"
.LC14:
	.string	"INCORRECT RESULT: cpu=%.10f gpu=%.10f @ index=%d, diff=%.10f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3735:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	cmpl	$2, %edi
	jg	.L11
	movq	(%rsi), %rdx
	movl	$2, %edi
	leaq	.LC0(%rip), %rsi
	call	__printf_chk@PLT
	jmp	.L12
.L11:
	movq	8(%rsi), %rdi
	call	atoi@PLT
	movq	16(%rbx), %rdi
	movl	%eax, %ebp
	call	atoi@PLT
	movl	$1620144156, %edi
	movslq	%eax, %r13
	call	srand@PLT
	movl	$1620144156, %edx
	xorl	%eax, %eax
	movq	%r13, %rbx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movslq	%ebp, %rdi
	imulq	%r13, %rdi
	salq	$2, %r13
	salq	$2, %rdi
	call	malloc@PLT
	movq	%r13, %rdi
	movq	%rax, %r14
	call	malloc@PLT
	movq	%r13, %rdi
	movq	%rax, %r15
	call	malloc@PLT
	movq	%r13, %rcx
	movq	%r13, %rdx
	xorl	%esi, %esi
	movq	%rax, %rdi
	movq	%rax, %r12
	xorl	%r13d, %r13d
	call	__memset_chk@PLT
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebp, %eax
	imull	%ebx, %eax
	movl	%eax, 12(%rsp)
.L13:
	cmpl	%r13d, 12(%rsp)
	jle	.L27
	call	rand@PLT
	movl	$977, %esi
	movsd	.LC3(%rip), %xmm0
	cltd
	idivl	%esi
	incl	%edx
	cvtsi2sdl	%edx, %xmm1
	divsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%r14,%r13,4)
	incq	%r13
	jmp	.L13
.L27:
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	xorl	%ecx, %ecx
	movq	%rax, %r13
	xorl	%eax, %eax
.L15:
	cmpl	%ebp, %ecx
	jge	.L17
	movslq	%eax, %rdx
	leaq	(%r14,%rdx,4), %rsi
	xorl	%edx, %edx
.L18:
	cmpl	%edx, %ebx
	jle	.L28
	movss	(%r12,%rdx,4), %xmm0
	addss	(%rsi,%rdx,4), %xmm0
	movss	%xmm0, (%r12,%rdx,4)
	incq	%rdx
	jmp	.L18
.L28:
	incl	%ecx
	addl	%ebx, %eax
	jmp	.L15
.L17:
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	movl	$1000, %ecx
	movl	$2, %edi
	leaq	.LC6(%rip), %rsi
	subq	%r13, %rax
	xorl	%r13d, %r13d
	cqto
	idivq	%rcx
	cvtsi2sdq	%rax, %xmm0
	movb	$1, %al
	divsd	.LC5(%rip), %xmm0
	call	__printf_chk@PLT
	movl	%ebx, %ecx
	movl	%ebp, %edx
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	32(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	40(%rsp), %rdi
	call	cudaEventCreate@PLT
	testb	$31, %bl
	movabsq	$137438953504, %rax
	movl	$32, %ecx
	movq	%rax, 48(%rsp)
	movl	%ebx, %eax
	setne	%r13b
	movl	$1, %r9d
	cltd
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	movl	$1, 56(%rsp)
	idivl	%ecx
	movl	%ebx, %ecx
	movl	%ebp, %edx
	addl	%eax, %r13d
	xorl	%eax, %eax
	movl	%r13d, %r8d
	call	__printf_chk@PLT
	movl	%r13d, 60(%rsp)
	movl	56(%rsp), %ecx
	xorl	%r9d, %r9d
	movabsq	$4294967297, %rax
	movq	48(%rsp), %rdx
	movl	$4096, %r8d
	movq	%rax, 64(%rsp)
	movq	60(%rsp), %rdi
	movl	68(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L19
	movl	%ebx, %ecx
	movl	%ebp, %edx
	xorl	%esi, %esi
	xorl	%edi, %edi
	call	_Z37__device_stub__Z13column_reducePfS_iiPfS_ii
.L19:
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	leaq	28(%rsp), %rdi
	xorl	%ebp, %ebp
	call	cudaEventElapsedTime@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	28(%rsp), %xmm0
	call	__printf_chk@PLT
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC11(%rip), %rsi
	call	__printf_chk@PLT
.L20:
	cmpl	%ebp, %ebx
	jle	.L29
	movss	(%r12,%rbp,4), %xmm0
	movss	(%r15,%rbp,4), %xmm1
	movaps	%xmm0, %xmm2
	subss	%xmm1, %xmm2
	movaps	%xmm2, %xmm3
	andps	.LC12(%rip), %xmm3
	cvtss2sd	%xmm3, %xmm3
	comisd	.LC13(%rip), %xmm3
	jbe	.L21
	movl	%ebp, %edx
	leaq	.LC14(%rip), %rsi
	movl	$2, %edi
	movb	$3, %al
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm2, %xmm2
	cvtss2sd	%xmm1, %xmm1
	call	__printf_chk@PLT
.L21:
	incq	%rbp
	jmp	.L20
.L29:
	movq	%r15, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%r14, %rdi
	call	free@PLT
.L12:
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L24
	call	__stack_chk_fail@PLT
.L24:
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3735:
	.size	main, .-main
	.section	.rodata.str1.1
.LC15:
	.string	"_Z13column_reducePfS_ii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3765:
	.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	.LC15(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z13column_reducePfS_ii(%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
.LFE3765:
	.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
.LC3:
	.long	0
	.long	1072693248
	.align 8
.LC5:
	.long	0
	.long	1083129856
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC12:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst8
	.align 8
.LC13:
	.long	-755914244
	.long	1062232653
	.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 <stdlib.h>
#include <assert.h>
#include <chrono>

#define CUDA_CHECK(status) (assert(status == hipSuccess))
#define threads_per_block 1024


// sum the shared data reductions into a single one
// loop unrolled version for increased performance
// Note: do not remove volatile!
// Note: some redudant calculation is done due to bank conflicts / sleeping threads
__device__ void columnWarpReduce(volatile float* sdata, int tid) {
    sdata[tid] += sdata[tid + 16];
    sdata[tid] += sdata[tid + 8];
    sdata[tid] += sdata[tid + 4];
    sdata[tid] += sdata[tid + 2];
    sdata[tid] += sdata[tid + 1];
}

__global__ void column_reduce(float * matrix, float * result, int m /* lines */, int n /* columns*/) {
    extern __shared__ float sdata[];
    
    // normal tid
    int tid = threadIdx.x + threadIdx.y * blockDim.x;
    
    // transposed tid for shared memory
    int new_tid = threadIdx.y + threadIdx.x * blockDim.y;

    // true x value in the matrix
    int real_x = threadIdx.x + blockDim.x * blockIdx.x;
    
    int i = real_x + n * threadIdx.y;
    const int it = n*blockDim.y;
    int offset = it;
    float accumulator = 0;

    if (threadIdx.y < m && real_x < n) {
        // store all the values from this column in a warped way
        accumulator = matrix[i];
        while (i + offset < n*m) {
            accumulator += matrix[i + offset];
            offset += it;
        }
    }

    // save column reduction data in a transposed way
    sdata[new_tid] = accumulator;
    __syncthreads();

    // avoid last warp to run causing memory errors
    if (tid < 32 * 32 - 16) { 
        columnWarpReduce(sdata, tid);
    }
    __syncthreads();
    
    if (threadIdx.y == 0 && real_x < n) 
        result[real_x] = sdata[new_tid];
    
}

int main(int argc, char * argv[])  {
    if (argc < 3) {
        printf("Usage: %s <m> <n>\n", argv[0]);
        return 0;
    }

    int m = atoi(argv[1]), n = atoi(argv[2]);
    
    unsigned long seed = 1620144156; //time(NULL);
    srand(seed); // seed 
    printf("Running with seed %ld\n", seed);

    // create row-major matrix m x n
    float * matrix = (float *) malloc(sizeof(float) * m * n); // m x n

    // create array to store result
    float * result_gpu = (float *) malloc(sizeof(float) * n); // tot_num_blocks x 1
    float * result_cpu = (float *) malloc(sizeof(float) * n); // validation
    memset(result_cpu, 0, sizeof(float) * n);

    printf("Populating array \n");
    // populate the array 
    for (int i = 0; i < m * n; i++)
        matrix[i] = 1.0 / ((rand() % 977) + 1);
    

    printf("Calculating final result\n");
    auto cpu_start = std::chrono::high_resolution_clock::now();
    
    // calculate cpu result
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) 
            result_cpu[j] += matrix[i * n + j];
    auto cpu_end = std::chrono::high_resolution_clock::now();

    printf("CPU took %f ms.\n", std::chrono::duration_cast<std::chrono::microseconds>(cpu_end - cpu_start).count() / 1000.0);
    

    printf("Allocating GPU memory, m=%d, n=%d\n", m, n);
    // allocate gpu memory
    float * matrix_gpu, * device_result, * helper_result = NULL;
    
    CUDA_CHECK(hipMalloc(&matrix_gpu, sizeof(float) * m * n));
    CUDA_CHECK(hipMalloc(&device_result, sizeof(float) * n));
    CUDA_CHECK(hipMemset(device_result, 0, sizeof(float) * n));
    printf("Finished allocating. Copying matrix...\n");
    // move matrix into gpu
    CUDA_CHECK(hipMemcpy(matrix_gpu, matrix, m * n * sizeof(float), hipMemcpyHostToDevice));

    
    hipEvent_t start, stop;
    float elapsed_time;
    hipEventCreate(&start);
    hipEventCreate(&stop);

    // call kernel
    dim3 block_threads(32, 32);
    dim3 grid_threads(n / 32 + (n % 32 ? 1 : 0), 1);

    printf("Calling kernel with m=%d n=%d, gridsize=(%d,%d)\n", m, n, grid_threads.x, grid_threads.y);

    CUDA_CHECK(hipEventRecord(start));
    column_reduce<<<grid_threads, block_threads, sizeof(float)*32*32>>>(matrix_gpu, device_result, m, n);
    CUDA_CHECK(hipEventRecord(stop));

    // Wait for final kernel to finish
    CUDA_CHECK(hipDeviceSynchronize());
    // end = std::chrono::high_resolution_clock::now();
    
    hipEventElapsedTime(&elapsed_time, start, stop);
    printf("Kernel finished. Took %f ms. Copying back results.\n", elapsed_time);
    // copy back results
    CUDA_CHECK(hipMemcpy(result_gpu, device_result, n * sizeof(float), hipMemcpyDeviceToHost));
    
    // free gpu memory
    CUDA_CHECK(hipFree(matrix_gpu));
    CUDA_CHECK(hipFree(device_result));
    if (helper_result) CUDA_CHECK(hipFree(helper_result));
    
    printf("Released GPU memory. Validating results...\n");
    // compare results
    for (int i = 0; i < n; i++) {
        if (abs(result_cpu[i] - result_gpu[i]) > 1e-3) 
            printf("INCORRECT RESULT: cpu=%.10f gpu=%.10f @ index=%d, diff=%.10f\n", result_cpu[i], result_gpu[i], i, result_cpu[i] - result_gpu[i]);
        // else printf("Correct result! cpu=%.10f, gpu=%.10f, diff=%.10f\n", result_cpu[i], result_gpu[i], result_cpu[i] - result_gpu[i]);
    }
    
    free(result_gpu);
    free(result_cpu);
    free(matrix);
    return 0;
}
	.text
	.file	"main.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z28__device_stub__column_reducePfS_ii # -- Begin function _Z28__device_stub__column_reducePfS_ii
	.type	_Z28__device_stub__column_reducePfS_ii,@function
_Z28__device_stub__column_reducePfS_ii: # @_Z28__device_stub__column_reducePfS_ii
	.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), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%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	$_Z13column_reducePfS_ii, %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_end0:
	.size	_Z28__device_stub__column_reducePfS_ii, .Lfunc_end0-_Z28__device_stub__column_reducePfS_ii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0x3ff0000000000000              # double 1
.LCPI1_1:
	.quad	0x408f400000000000              # double 1000
.LCPI1_3:
	.quad	0x3f50624dd2f1a9fc              # double 0.001
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI1_2:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	.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, %rbx
	cmpl	$2, %edi
	jg	.LBB1_2
# %bb.1:
	movq	(%rbx), %rsi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB1_19
.LBB1_2:
	movq	8(%rbx), %rdi
	callq	atoi
	movl	%eax, %ebp
	movq	16(%rbx), %rdi
	callq	atoi
	movl	%eax, %r12d
	movl	$1620144156, %edi               # imm = 0x6091701C
	callq	srand
	movl	$.L.str.1, %edi
	movl	$1620144156, %esi               # imm = 0x6091701C
	xorl	%eax, %eax
	callq	printf
	movslq	%ebp, %rdi
	movslq	%r12d, %r15
	imulq	%r15, %rdi
	shlq	$2, %rdi
	callq	malloc
	movq	%rax, %rbx
	shlq	$2, %r15
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r14
	movl	$1, %edi
	movq	%r15, %rsi
	callq	calloc@PLT
	movq	%rax, %r15
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	%r12d, %eax
	movl	%ebp, 4(%rsp)                   # 4-byte Spill
	imull	%ebp, %eax
	testl	%eax, %eax
	jle	.LBB1_5
# %bb.3:                                # %.lr.ph.preheader
	movl	%eax, %r13d
	xorl	%ebp, %ebp
.LBB1_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$562697865, %rax, %rcx          # imm = 0x218A1689
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$39, %rcx
	addl	%edx, %ecx
	imull	$977, %ecx, %ecx                # imm = 0x3D1
	negl	%ecx
	addl	%ecx, %eax
	incl	%eax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	movsd	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	cvtsd2ss	%xmm1, %xmm0
	movss	%xmm0, (%rbx,%rbp,4)
	incq	%rbp
	cmpq	%rbp, %r13
	jne	.LBB1_4
.LBB1_5:                                # %._crit_edge
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	movl	%r12d, %ebp
	movl	4(%rsp), %r13d                  # 4-byte Reload
	testl	%r13d, %r13d
	jle	.LBB1_11
# %bb.6:                                # %.preheader.lr.ph
	movl	%r13d, %eax
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.LBB1_7:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_9 Depth 2
	testl	%r12d, %r12d
	jle	.LBB1_10
# %bb.8:                                # %.lr.ph84
                                        #   in Loop: Header=BB1_7 Depth=1
	movl	%ecx, %esi
	leaq	(%rbx,%rsi,4), %rsi
	xorl	%edi, %edi
.LBB1_9:                                #   Parent Loop BB1_7 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rsi,%rdi,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	addss	(%r15,%rdi,4), %xmm0
	movss	%xmm0, (%r15,%rdi,4)
	incq	%rdi
	cmpq	%rdi, %rbp
	jne	.LBB1_9
.LBB1_10:                               # %._crit_edge85
                                        #   in Loop: Header=BB1_7 Depth=1
	incq	%rdx
	addl	%r12d, %ecx
	cmpq	%rax, %rdx
	jne	.LBB1_7
.LBB1_11:                               # %._crit_edge87
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	subq	8(%rsp), %rax                   # 8-byte Folded Reload
	movabsq	$2361183241434822607, %rcx      # imm = 0x20C49BA5E353F7CF
	imulq	%rcx
	movq	%rdx, %rax
	shrq	$63, %rax
	sarq	$7, %rdx
	addq	%rax, %rdx
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rdx, %xmm0
	divsd	.LCPI1_1(%rip), %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.5, %edi
	movl	%r13d, %esi
	movl	%r12d, %edx
	xorl	%eax, %eax
	callq	printf
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	leaq	16(%rsp), %rdi
	callq	hipEventCreate
	leal	31(%r12), %ecx
	testl	%r12d, %r12d
	cmovnsl	%r12d, %ecx
	sarl	$5, %ecx
	movl	%r12d, %eax
	andl	$31, %eax
	cmpl	$1, %eax
	sbbl	$-1, %ecx
	movq	%rcx, %r13
	btsq	$32, %r13
	movl	$.L.str.7, %edi
	movl	4(%rsp), %esi                   # 4-byte Reload
	movl	%r12d, %edx
                                        # kill: def $ecx killed $ecx killed $rcx
	movl	$1, %r8d
	xorl	%eax, %eax
	callq	printf
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movl	$4096, %r8d                     # imm = 0x1000
	movq	%r13, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_13
# %bb.12:
	movl	4(%rsp), %edx                   # 4-byte Reload
	movl	%r12d, %ecx
	callq	_Z28__device_stub__column_reducePfS_ii
.LBB1_13:
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdx
	leaq	36(%rsp), %r13
	movq	%r13, %rdi
	callq	hipEventElapsedTime
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r13), %xmm0
	movl	$.L.str.8, %edi
	movb	$1, %al
	callq	printf
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	testl	%r12d, %r12d
	jle	.LBB1_18
# %bb.14:                               # %.lr.ph90.preheader
	xorl	%r12d, %r12d
	movaps	.LCPI1_2(%rip), %xmm4           # xmm4 = [NaN,NaN,NaN,NaN]
	movsd	.LCPI1_3(%rip), %xmm5           # xmm5 = mem[0],zero
.LBB1_15:                               # %.lr.ph90
                                        # =>This Inner Loop Header: Depth=1
	movss	(%r15,%r12,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	(%r14,%r12,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	movaps	%xmm0, %xmm2
	subss	%xmm1, %xmm2
	movaps	%xmm2, %xmm3
	andps	%xmm4, %xmm3
	cvtss2sd	%xmm3, %xmm3
	ucomisd	%xmm5, %xmm3
	jbe	.LBB1_17
# %bb.16:                               #   in Loop: Header=BB1_15 Depth=1
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	cvtss2sd	%xmm2, %xmm2
	movl	$.L.str.10, %edi
	movl	%r12d, %esi
	movb	$3, %al
	callq	printf
	movsd	.LCPI1_3(%rip), %xmm5           # xmm5 = mem[0],zero
	movaps	.LCPI1_2(%rip), %xmm4           # xmm4 = [NaN,NaN,NaN,NaN]
.LBB1_17:                               #   in Loop: Header=BB1_15 Depth=1
	incq	%r12
	cmpq	%r12, %rbp
	jne	.LBB1_15
.LBB1_18:                               # %._crit_edge91
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	%rbx, %rdi
	callq	free
.LBB1_19:
	xorl	%eax, %eax
	addq	$40, %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_end1:
	.size	main, .Lfunc_end1-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	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z13column_reducePfS_ii, %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_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z13column_reducePfS_ii,@object # @_Z13column_reducePfS_ii
	.section	.rodata,"a",@progbits
	.globl	_Z13column_reducePfS_ii
	.p2align	3, 0x0
_Z13column_reducePfS_ii:
	.quad	_Z28__device_stub__column_reducePfS_ii
	.size	_Z13column_reducePfS_ii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Usage: %s <m> <n>\n"
	.size	.L.str, 19

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Running with seed %ld\n"
	.size	.L.str.1, 23

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"CPU took %f ms.\n"
	.size	.L.str.4, 17

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Allocating GPU memory, m=%d, n=%d\n"
	.size	.L.str.5, 35

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Calling kernel with m=%d n=%d, gridsize=(%d,%d)\n"
	.size	.L.str.7, 49

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Kernel finished. Took %f ms. Copying back results.\n"
	.size	.L.str.8, 52

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"INCORRECT RESULT: cpu=%.10f gpu=%.10f @ index=%d, diff=%.10f\n"
	.size	.L.str.10, 62

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z13column_reducePfS_ii"
	.size	.L__unnamed_1, 24

	.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	"Populating array "
	.size	.Lstr, 18

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Calculating final result"
	.size	.Lstr.1, 25

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Finished allocating. Copying matrix..."
	.size	.Lstr.2, 39

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"Released GPU memory. Validating results..."
	.size	.Lstr.3, 43

	.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__column_reducePfS_ii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z13column_reducePfS_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z13column_reducePfS_ii ; -- Begin function _Z13column_reducePfS_ii
	.globl	_Z13column_reducePfS_ii
	.p2align	8
	.type	_Z13column_reducePfS_ii,@function
_Z13column_reducePfS_ii:                ; @_Z13column_reducePfS_ii
; %bb.0:
	s_clause 0x2
	s_load_b32 s8, s[0:1], 0x24
	s_load_b64 s[2:3], s[0:1], 0x10
	s_load_b128 s[4:7], s[0:1], 0x0
	v_dual_mov_b32 v6, 0 :: v_dual_and_b32 v3, 0x3ff, v0
	v_bfe_u32 v0, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_and_b32 s1, s8, 0xffff
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_cmp_gt_u32_e64 s0, s2, v0
	v_mad_u64_u32 v[1:2], null, s15, s1, v[3:4]
	s_lshr_b32 s8, s8, 16
	v_cmp_gt_i32_e32 vcc_lo, s3, v1
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s0, s0, vcc_lo
	s_and_saveexec_b32 s9, s0
	s_cbranch_execz .LBB0_6
; %bb.1:
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[4:5], null, v0, s3, v[1:2]
	s_mul_i32 s10, s8, s3
	s_mul_i32 s3, s3, s2
	s_mov_b32 s2, exec_lo
	v_ashrrev_i32_e32 v5, 31, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[5:6], 2, v[4:5]
	v_add_nc_u32_e32 v4, s10, v4
	v_add_co_u32 v5, s0, s4, v5
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v6, s0, s5, v6, s0
	global_load_b32 v6, v[5:6], off
	v_cmpx_gt_i32_e64 s3, v4
	s_cbranch_execz .LBB0_5
; %bb.2:                                ; %.lr.ph.preheader
	s_mov_b32 s11, 0
.LBB0_3:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	v_ashrrev_i32_e32 v5, 31, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[7:8], 2, v[4:5]
	v_add_nc_u32_e32 v4, s10, v4
	v_add_co_u32 v7, s0, s4, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e64 v8, s0, s5, v8, s0
	v_cmp_le_i32_e64 s0, s3, v4
	global_load_b32 v2, v[7:8], off
	s_or_b32 s11, s0, s11
	s_waitcnt vmcnt(0)
	v_add_f32_e32 v6, v6, v2
	s_and_not1_b32 exec_lo, exec_lo, s11
	s_cbranch_execnz .LBB0_3
; %bb.4:                                ; %Flow
	s_or_b32 exec_lo, exec_lo, s11
.LBB0_5:                                ; %Flow54
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 exec_lo, exec_lo, s2
.LBB0_6:                                ; %.loopexit
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(VALU_DEP_2)
	s_or_b32 exec_lo, exec_lo, s9
	v_mad_u32_u24 v2, v3, s8, v0
	v_mad_u32_u24 v3, v0, s1, v3
	s_mov_b32 s2, exec_lo
	v_lshl_add_u32 v2, v2, 2, 0
	s_waitcnt vmcnt(0)
	ds_store_b32 v2, v6
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_cmpx_gt_u32_e32 0x3f0, v3
	s_cbranch_execz .LBB0_8
; %bb.7:
	v_lshl_add_u32 v7, v3, 2, 0
	s_mov_b64 s[4:5], src_shared_base
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v4, 64, v7
	v_cmp_ne_u32_e64 s1, -1, v7
	v_cmp_ne_u32_e64 s0, -1, v4
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cndmask_b32_e64 v3, 0, v7, s1
	v_cndmask_b32_e64 v5, 0, v4, s0
	v_cndmask_b32_e64 v6, 0, s5, s0
	v_cndmask_b32_e64 v4, 0, s5, s1
	flat_load_b32 v6, v[5:6] glc dlc
	s_waitcnt vmcnt(0)
	flat_load_b32 v8, v[3:4] glc dlc
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_dual_add_f32 v8, v6, v8 :: v_dual_add_nc_u32 v5, 32, v7
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_ne_u32_e64 s0, -1, v5
	flat_store_b32 v[3:4], v8 dlc
	s_waitcnt_vscnt null, 0x0
	v_cndmask_b32_e64 v5, 0, v5, s0
	v_cndmask_b32_e64 v6, 0, s5, s0
	flat_load_b32 v6, v[5:6] glc dlc
	s_waitcnt vmcnt(0)
	flat_load_b32 v8, v[3:4] glc dlc
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_dual_add_f32 v8, v6, v8 :: v_dual_add_nc_u32 v5, 16, v7
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_ne_u32_e64 s0, -1, v5
	flat_store_b32 v[3:4], v8 dlc
	s_waitcnt_vscnt null, 0x0
	v_cndmask_b32_e64 v5, 0, v5, s0
	v_cndmask_b32_e64 v6, 0, s5, s0
	flat_load_b32 v6, v[5:6] glc dlc
	s_waitcnt vmcnt(0)
	flat_load_b32 v8, v[3:4] glc dlc
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_dual_add_f32 v8, v6, v8 :: v_dual_add_nc_u32 v5, 8, v7
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_ne_u32_e64 s0, -1, v5
	flat_store_b32 v[3:4], v8 dlc
	s_waitcnt_vscnt null, 0x0
	v_cndmask_b32_e64 v5, 0, v5, s0
	v_cndmask_b32_e64 v6, 0, s5, s0
	flat_load_b32 v6, v[5:6] glc dlc
	s_waitcnt vmcnt(0)
	flat_load_b32 v8, v[3:4] glc dlc
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v5, 4, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ne_u32_e64 s0, -1, v5
	v_cndmask_b32_e64 v5, 0, v5, s0
	s_waitcnt lgkmcnt(0)
	v_add_f32_e32 v7, v6, v8
	v_cndmask_b32_e64 v6, 0, s5, s0
	flat_store_b32 v[3:4], v7 dlc
	s_waitcnt_vscnt null, 0x0
	flat_load_b32 v5, v[5:6] glc dlc
	s_waitcnt vmcnt(0)
	flat_load_b32 v6, v[3:4] glc dlc
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_add_f32_e32 v5, v5, v6
	flat_store_b32 v[3:4], v5 dlc
	s_waitcnt_vscnt null, 0x0
.LBB0_8:
	s_or_b32 exec_lo, exec_lo, s2
	v_cmp_eq_u32_e64 s0, 0, v0
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_and_b32 s0, s0, vcc_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s1, s0
	s_cbranch_execz .LBB0_10
; %bb.9:
	ds_load_b32 v3, v2
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	v_add_co_u32 v0, vcc_lo, s6, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v1, vcc_lo
	s_waitcnt lgkmcnt(0)
	global_store_b32 v[0:1], v3, off
.LBB0_10:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z13column_reducePfS_ii
		.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 1
		.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	_Z13column_reducePfS_ii, .Lfunc_end0-_Z13column_reducePfS_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 872
; 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: 1
	.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 sdata
	.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:     by_value
      - .offset:         20
        .size:           4
        .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:           _Z13column_reducePfS_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z13column_reducePfS_ii.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
