
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>
#include <vector>
#include <iostream>
#include <chrono>

using namespace std;

// adds elements of array in place like this for a 11 element array:
//   [1][1][1][1][1][1][1][1][1][1][1][0][0][0][0][0]
//    ^+=^  ^+=^  ^+=^  ^+=^  ^+=^  ^+=^  ^+=^  ^+=^ 
//   [2][1][2][1][2][1][2][1][2][1][1][0][0][0][0][0]
//    ^ +=  ^     ^ +=  ^     ^ +=  ^     ^ +=  ^       
//   [4][1][2][1][4][1][2][1][3][1][1][0][0][0][0][0]
//    ^    +=     ^           ^    +=     ^                   
//   [8][1][2][1][4][1][2][1][3][1][1][0][0][0][0][0]
//    ^          +=           ^                                           
//   [11][1][2][1][4][1][2][1][3][1][1][0][0][0][0][0]
//    ^ this is the final total
__global__ void addKernel(unsigned int *a, unsigned int interval, unsigned int xDim) {
    unsigned int xInd = blockIdx.x * blockDim.x + threadIdx.x;
    unsigned int yInd = blockIdx.y * blockDim.y + threadIdx.y;
    unsigned int i = interval * (xInd + xDim * yInd);
    a[i] = a[i] + a[i + interval / 2];
}

// Helper function for using CUDA to add vectors in parallel.
unsigned int addWithCuda(unsigned int* aHost, unsigned int size) {
    unsigned int* aDevice;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaSetDevice(0);

    // Allocate GPU buffers for the vector, ensuring that this buffer is a multiple of 2 equal to or larger than the size of the input array.  set any additional elements to 0
    unsigned int multTwoSize = 2;
    while (multTwoSize < size) {
        multTwoSize = multTwoSize * 2;
    }
    cudaMalloc(&aDevice, multTwoSize * sizeof(unsigned int));
    if (multTwoSize > size) {
        cudaMemset(&(aDevice[size]), 0, (multTwoSize - size) * sizeof(unsigned int));
    }

    // Copy input vector from host memory to GPU buffer
    cudaMemcpy(aDevice, aHost, size * sizeof(unsigned int), cudaMemcpyHostToDevice);

    auto start = chrono::high_resolution_clock::now();
    // Launch a kernel on the GPU with one thread first for every other element then every fourth and so on, synchronizing threads after each iteration
    unsigned int interval = 2;
    while (interval <= multTwoSize) {
        unsigned int numThreads = multTwoSize / interval;
        dim3 gridDim(1, 1);
        dim3 blockDim(1, 1);
        // max block dimension is 32x32 threads since the max threads per block is 1024
        // max grid dimension is 2048x2048 blocks assuming each block is 32x32 threads.  This stems from the max x and y dimensions of 65536x65536
        if (numThreads > 32) {
            blockDim.x = 32;
            if ((numThreads / 32) > 32) {
                blockDim.y = 32;
                if ((numThreads / (32 * 32)) > 2048) {
                    gridDim.x = 2048;
                    if ((numThreads / (32 * 32 * 2048)) > 2048) {
                        cout << "Array is too large" << endl;
                        return 0;
                    }
                    else {
                        gridDim.y = numThreads / (32 * 32 * 2048);
                    }
                }
                else {
                    gridDim.x = numThreads / (32 * 32);
                }
            }
            else {
                blockDim.y = numThreads / 32;
            }
        }
        else {
            blockDim.x = numThreads;
        }
        unsigned int xDim = gridDim.x * blockDim.x;
        addKernel <<< gridDim, blockDim >>> (aDevice, interval, xDim);
        cudaDeviceSynchronize();
        interval = interval * 2;
    }
    auto end = chrono::high_resolution_clock::now();
    auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);
    cout << "Time:  " << duration.count() << " ms" << endl;

    // check for errors during kernel creation
    cudaError status;
    status = cudaGetLastError();
    if (status != cudaSuccess) {
        cout << cudaGetErrorString(status) << endl;
    }

    // Copy output vector from GPU buffer to host memory.
    cudaMemcpy(aHost, aDevice, sizeof(unsigned int), cudaMemcpyDeviceToHost);

    // free the memory
    cudaFree(aDevice);

    // return the total
    unsigned int total = aHost[0];
    return total;
}

int main() {
    vector<unsigned int> a;
    // breaks at 536870913 since this is 2^29 + 1 so multTwo array length will be rounded up to 2^30 = 1073741824 integers times 4 bytes per integer is 4GB which is all of the available GPU memory
    for (unsigned int i = 0; i < 536870912; i++) {
        a.push_back(1);
    }

    // Add elements of the vector in parallel.
    unsigned int total = addWithCuda(&(a[0]), a.size());

    cout << total << endl;

    // cudaDeviceReset must be called before exiting in order for profiling and tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaDeviceReset();

    return 0;
}

	.file	"tmpxft_002aa19e_00000000-6_kernel.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB4114:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE4114:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z30__device_stub__Z9addKernelPjjjPjjj
	.type	_Z30__device_stub__Z9addKernelPjjjPjjj, @function
_Z30__device_stub__Z9addKernelPjjjPjjj:
.LFB4136:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	32(%rsp), %rdi
	movl	%esi, 4(%rsp)
	leaq	44(%rsp), %rsi
	movl	%edx, (%rsp)
	leaq	16(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	24(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z9addKernelPjjj(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 144
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L2:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4136:
	.size	_Z30__device_stub__Z9addKernelPjjjPjjj, .-_Z30__device_stub__Z9addKernelPjjjPjjj
	.globl	_Z9addKernelPjjj
	.type	_Z9addKernelPjjj, @function
_Z9addKernelPjjj:
.LFB4137:
	.cfi_startproc
	endbr64
	jmp	_Z30__device_stub__Z9addKernelPjjjPjjj
	.cfi_endproc
.LFE4137:
	.size	_Z9addKernelPjjj, .-_Z9addKernelPjjj
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Time:  "
.LC1:
	.string	" ms"
	.text
	.globl	_Z11addWithCudaPjj
	.type	_Z11addWithCudaPjj, @function
_Z11addWithCudaPjj:
.LFB4097:
	.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
	movq	%rdi, %r13
	xorl	%edi, %edi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	$2, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%esi, %ebx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	cudaSetDevice@PLT
.L9:
	cmpl	%ebx, %ebp
	jnb	.L26
	addl	%ebp, %ebp
	jmp	.L9
.L26:
	movl	%ebp, %esi
	leaq	24(%rsp), %rdi
	movl	%ebx, %r12d
	salq	$2, %rsi
	salq	$2, %r12
	call	cudaMalloc@PLT
	cmpl	%ebp, %ebx
	jnb	.L11
	movq	24(%rsp), %rdi
	movl	%ebp, %edx
	xorl	%esi, %esi
	subl	%ebx, %edx
	salq	$2, %rdx
	addq	%r12, %rdi
	call	cudaMemset@PLT
.L11:
	movq	24(%rsp), %rdi
	movq	%r12, %rdx
	movl	$1, %ecx
	movq	%r13, %rsi
	movabsq	$4294967297, %r15
	movl	$2, %r12d
	call	cudaMemcpy@PLT
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	movq	%rax, %r14
.L12:
	cmpl	%r12d, %ebp
	jb	.L27
	movl	%ebp, %eax
	xorl	%edx, %edx
	movq	%r15, 36(%rsp)
	movl	$1, %r10d
	divl	%r12d
	movq	%r15, 48(%rsp)
	movl	%eax, %ebx
	cmpl	$32, %eax
	jbe	.L13
	cmpl	$1055, %eax
	jbe	.L14
	movl	$32, 48(%rsp)
	cmpl	$2098175, %eax
	jbe	.L15
	shrl	$21, %ebx
	movl	$2048, %r10d
	movl	%ebx, 36(%rsp)
	movl	$32, %ebx
	jmp	.L13
.L15:
	shrl	$10, %ebx
	movl	%ebx, %r10d
	jmp	.L25
.L14:
	shrl	$5, %ebx
	movl	%ebx, 48(%rsp)
.L25:
	movl	$32, %ebx
.L13:
	movl	%r10d, 32(%rsp)
	movl	52(%rsp), %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	32(%rsp), %rdi
	movl	40(%rsp), %esi
	movl	%ebx, 44(%rsp)
	movq	44(%rsp), %rdx
	movl	%r10d, 12(%rsp)
	call	__cudaPushCallConfiguration@PLT
	movl	12(%rsp), %r10d
	testl	%eax, %eax
	jne	.L16
	movl	%r10d, %edx
	movq	24(%rsp), %rdi
	movl	%r12d, %esi
	imull	%ebx, %edx
	call	_Z30__device_stub__Z9addKernelPjjjPjjj
.L16:
	call	cudaDeviceSynchronize@PLT
	addl	%r12d, %r12d
	jmp	.L12
.L27:
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	movl	$1000000, %ecx
	leaq	_ZSt4cout(%rip), %rbp
	leaq	.LC0(%rip), %rsi
	subq	%r14, %rax
	movq	%rbp, %rdi
	cqto
	idivq	%rcx
	movq	%rax, %rbx
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rbx, %rsi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertIlEERSoT_@PLT
	leaq	.LC1(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L18
	call	cudaGetErrorString@PLT
	movq	%rbp, %rdi
	movq	%rax, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
.L18:
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movl	$4, %edx
	movq	%r13, %rdi
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movl	0(%r13), %eax
	movq	56(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L19
	call	__stack_chk_fail@PLT
.L19:
	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
.LFE4097:
	.size	_Z11addWithCudaPjj, .-_Z11addWithCudaPjj
	.section	.rodata.str1.1
.LC2:
	.string	"_Z9addKernelPjjj"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB4139:
	.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	.LC2(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z9addKernelPjjj(%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
.LFE4139:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.text._ZNSt12_Vector_baseIjSaIjEED2Ev,"axG",@progbits,_ZNSt12_Vector_baseIjSaIjEED5Ev,comdat
	.align 2
	.weak	_ZNSt12_Vector_baseIjSaIjEED2Ev
	.type	_ZNSt12_Vector_baseIjSaIjEED2Ev, @function
_ZNSt12_Vector_baseIjSaIjEED2Ev:
.LFB4481:
	.cfi_startproc
	endbr64
	movq	(%rdi), %rax
	testq	%rax, %rax
	je	.L30
	movq	16(%rdi), %rsi
	movq	%rax, %rdi
	subq	%rax, %rsi
	jmp	_ZdlPvm@PLT
.L30:
	ret
	.cfi_endproc
.LFE4481:
	.size	_ZNSt12_Vector_baseIjSaIjEED2Ev, .-_ZNSt12_Vector_baseIjSaIjEED2Ev
	.weak	_ZNSt12_Vector_baseIjSaIjEED1Ev
	.set	_ZNSt12_Vector_baseIjSaIjEED1Ev,_ZNSt12_Vector_baseIjSaIjEED2Ev
	.section	.rodata._ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_.str1.1,"aMS",@progbits,1
.LC3:
	.string	"vector::_M_realloc_insert"
	.section	.text._ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_,"axG",@progbits,_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_,comdat
	.align 2
	.weak	_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_
	.type	_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_, @function
_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_:
.LFB4764:
	.cfi_startproc
	endbr64
	movabsq	$2305843009213693951, %rcx
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	8(%rdi), %rax
	movq	(%rdi), %r14
	movq	%rax, %rbx
	movq	%rax, (%rsp)
	subq	%r14, %rbx
	sarq	$2, %rbx
	cmpq	%rcx, %rbx
	jne	.L33
	leaq	.LC3(%rip), %rdi
	call	_ZSt20__throw_length_errorPKc@PLT
.L33:
	cmpq	%r14, (%rsp)
	movl	$1, %eax
	movq	%rsi, %r15
	movq	%rdi, %r13
	cmovne	%rbx, %rax
	movq	%rsi, %rbp
	addq	%rax, %rbx
	setc	%al
	subq	%r14, %r15
	movzbl	%al, %eax
	testq	%rax, %rax
	jne	.L43
	xorl	%r12d, %r12d
	testq	%rbx, %rbx
	je	.L38
	movabsq	$2305843009213693951, %rax
	cmpq	%rax, %rbx
	cmova	%rax, %rbx
	jmp	.L37
.L43:
	movq	%rcx, %rbx
.L37:
	leaq	0(,%rbx,4), %rdi
	movq	%rdx, 8(%rsp)
	call	_Znwm@PLT
	movq	8(%rsp), %rdx
	movq	%rax, %r12
.L38:
	movl	(%rdx), %eax
	movl	%eax, (%r12,%r15)
	testq	%r15, %r15
	jle	.L39
	movq	%r15, %rdx
	movq	%r14, %rsi
	movq	%r12, %rdi
	call	memmove@PLT
.L39:
	movq	(%rsp), %rax
	leaq	4(%r12,%r15), %rdx
	subq	%rbp, %rax
	testq	%rax, %rax
	jle	.L40
	movq	%rdx, %rdi
	movq	%rbp, %rsi
	movq	%rax, %rcx
	rep movsb
.L40:
	leaq	(%rdx,%rax), %rbp
	testq	%r14, %r14
	je	.L41
	movq	16(%r13), %rsi
	movq	%r14, %rdi
	subq	%r14, %rsi
	call	_ZdlPvm@PLT
.L41:
	leaq	(%r12,%rbx,4), %rax
	movq	%r12, 0(%r13)
	movq	%rbp, 8(%r13)
	movq	%rax, 16(%r13)
	addq	$24, %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
.LFE4764:
	.size	_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_, .-_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_
	.section	.text.startup
	.globl	main
	.type	main, @function
main:
.LFB4101:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA4101
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	$536870912, %ebx
	subq	$56, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	xorl	%eax, %eax
	leaq	12(%rsp), %rbp
	movq	%rax, 16(%rsp)
	movq	%rax, 24(%rsp)
	movq	%rax, 32(%rsp)
.L53:
	movl	$1, 12(%rsp)
	movq	24(%rsp), %rsi
	cmpq	32(%rsp), %rsi
	je	.L51
	movl	$1, (%rsi)
	addq	$4, %rsi
	movq	%rsi, 24(%rsp)
	jmp	.L52
.L51:
	leaq	16(%rsp), %rdi
	movq	%rbp, %rdx
.LEHB0:
	call	_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_
.L52:
	decl	%ebx
	jne	.L53
	movq	16(%rsp), %rdi
	movq	24(%rsp), %rsi
	subq	%rdi, %rsi
	sarq	$2, %rsi
	call	_Z11addWithCudaPjj
	movl	%eax, %esi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	call	cudaDeviceReset@PLT
.LEHE0:
	leaq	16(%rsp), %rdi
	call	_ZNSt12_Vector_baseIjSaIjEED2Ev
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L56
	jmp	.L59
.L57:
	endbr64
	movq	%rax, %rbx
.L54:
	leaq	16(%rsp), %rdi
	call	_ZNSt12_Vector_baseIjSaIjEED2Ev
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L59
	movq	%rbx, %rdi
.LEHB1:
	call	_Unwind_Resume@PLT
.LEHE1:
.L59:
	call	__stack_chk_fail@PLT
.L56:
	addq	$56, %rsp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4101:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA4101:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE4101-.LLSDACSB4101
.LLSDACSB4101:
	.uleb128 .LEHB0-.LFB4101
	.uleb128 .LEHE0-.LEHB0
	.uleb128 .L57-.LFB4101
	.uleb128 0
	.uleb128 .LEHB1-.LFB4101
	.uleb128 .LEHE1-.LEHB1
	.uleb128 0
	.uleb128 0
.LLSDACSE4101:
	.section	.text.startup
	.size	main, .-main
	.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
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include "hip/hip_runtime.h"


#include <stdio.h>
#include <vector>
#include <iostream>
#include <chrono>

using namespace std;

// adds elements of array in place like this for a 11 element array:
//   [1][1][1][1][1][1][1][1][1][1][1][0][0][0][0][0]
//    ^+=^  ^+=^  ^+=^  ^+=^  ^+=^  ^+=^  ^+=^  ^+=^ 
//   [2][1][2][1][2][1][2][1][2][1][1][0][0][0][0][0]
//    ^ +=  ^     ^ +=  ^     ^ +=  ^     ^ +=  ^       
//   [4][1][2][1][4][1][2][1][3][1][1][0][0][0][0][0]
//    ^    +=     ^           ^    +=     ^                   
//   [8][1][2][1][4][1][2][1][3][1][1][0][0][0][0][0]
//    ^          +=           ^                                           
//   [11][1][2][1][4][1][2][1][3][1][1][0][0][0][0][0]
//    ^ this is the final total
__global__ void addKernel(unsigned int *a, unsigned int interval, unsigned int xDim) {
    unsigned int xInd = blockIdx.x * blockDim.x + threadIdx.x;
    unsigned int yInd = blockIdx.y * blockDim.y + threadIdx.y;
    unsigned int i = interval * (xInd + xDim * yInd);
    a[i] = a[i] + a[i + interval / 2];
}

// Helper function for using CUDA to add vectors in parallel.
unsigned int addWithCuda(unsigned int* aHost, unsigned int size) {
    unsigned int* aDevice;

    // Choose which GPU to run on, change this on a multi-GPU system.
    hipSetDevice(0);

    // Allocate GPU buffers for the vector, ensuring that this buffer is a multiple of 2 equal to or larger than the size of the input array.  set any additional elements to 0
    unsigned int multTwoSize = 2;
    while (multTwoSize < size) {
        multTwoSize = multTwoSize * 2;
    }
    hipMalloc(&aDevice, multTwoSize * sizeof(unsigned int));
    if (multTwoSize > size) {
        hipMemset(&(aDevice[size]), 0, (multTwoSize - size) * sizeof(unsigned int));
    }

    // Copy input vector from host memory to GPU buffer
    hipMemcpy(aDevice, aHost, size * sizeof(unsigned int), hipMemcpyHostToDevice);

    auto start = chrono::high_resolution_clock::now();
    // Launch a kernel on the GPU with one thread first for every other element then every fourth and so on, synchronizing threads after each iteration
    unsigned int interval = 2;
    while (interval <= multTwoSize) {
        unsigned int numThreads = multTwoSize / interval;
        dim3 gridDim(1, 1);
        dim3 blockDim(1, 1);
        // max block dimension is 32x32 threads since the max threads per block is 1024
        // max grid dimension is 2048x2048 blocks assuming each block is 32x32 threads.  This stems from the max x and y dimensions of 65536x65536
        if (numThreads > 32) {
            blockDim.x = 32;
            if ((numThreads / 32) > 32) {
                blockDim.y = 32;
                if ((numThreads / (32 * 32)) > 2048) {
                    gridDim.x = 2048;
                    if ((numThreads / (32 * 32 * 2048)) > 2048) {
                        cout << "Array is too large" << endl;
                        return 0;
                    }
                    else {
                        gridDim.y = numThreads / (32 * 32 * 2048);
                    }
                }
                else {
                    gridDim.x = numThreads / (32 * 32);
                }
            }
            else {
                blockDim.y = numThreads / 32;
            }
        }
        else {
            blockDim.x = numThreads;
        }
        unsigned int xDim = gridDim.x * blockDim.x;
        addKernel <<< gridDim, blockDim >>> (aDevice, interval, xDim);
        hipDeviceSynchronize();
        interval = interval * 2;
    }
    auto end = chrono::high_resolution_clock::now();
    auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);
    cout << "Time:  " << duration.count() << " ms" << endl;

    // check for errors during kernel creation
    hipError_t status;
    status = hipGetLastError();
    if (status != hipSuccess) {
        cout << hipGetErrorString(status) << endl;
    }

    // Copy output vector from GPU buffer to host memory.
    hipMemcpy(aHost, aDevice, sizeof(unsigned int), hipMemcpyDeviceToHost);

    // free the memory
    hipFree(aDevice);

    // return the total
    unsigned int total = aHost[0];
    return total;
}

int main() {
    vector<unsigned int> a;
    // breaks at 536870913 since this is 2^29 + 1 so multTwo array length will be rounded up to 2^30 = 1073741824 integers times 4 bytes per integer is 4GB which is all of the available GPU memory
    for (unsigned int i = 0; i < 536870912; i++) {
        a.push_back(1);
    }

    // Add elements of the vector in parallel.
    unsigned int total = addWithCuda(&(a[0]), a.size());

    cout << total << endl;

    // cudaDeviceReset must be called before exiting in order for profiling and tracing tools such as Nsight and Visual Profiler to show complete traces.
    hipDeviceReset();

    return 0;
}

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

                                        # End of file scope inline assembly
	.globl	_Z24__device_stub__addKernelPjjj # -- Begin function _Z24__device_stub__addKernelPjjj
	.type	_Z24__device_stub__addKernelPjjj,@function
_Z24__device_stub__addKernelPjjj:       # @_Z24__device_stub__addKernelPjjj
	.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
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	4(%rsp), %rcx
	movl	%esi, (%rcx)
	movq	%rsp, %rsi
	movl	%edx, (%rsi)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%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	$_Z9addKernelPjjj, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$112, %rsp
	.cfi_adjust_cfa_offset -112
	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	_Z24__device_stub__addKernelPjjj, .Lfunc_end0-_Z24__device_stub__addKernelPjjj
	.cfi_endproc
                                        # -- End function
	.globl	_Z11addWithCudaPjj              # -- Begin function _Z11addWithCudaPjj
	.type	_Z11addWithCudaPjj,@function
_Z11addWithCudaPjj:                     # @_Z11addWithCudaPjj
	.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
	movl	%esi, %ebp
	movq	%rdi, %rbx
	xorl	%edi, %edi
	callq	hipSetDevice
	movl	$2, %eax
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	movl	%eax, %r14d
	leal	(%r14,%r14), %eax
	cmpl	%ebp, %r14d
	jb	.LBB1_1
# %bb.2:
	movl	%r14d, %esi
	shlq	$2, %rsi
	movq	%rsp, %rdi
	callq	hipMalloc
	movl	%r14d, %edx
	movl	%ebp, %r15d
	subl	%ebp, %edx
	jbe	.LBB1_4
# %bb.3:
	leaq	(,%r15,4), %rdi
	addq	(%rsp), %rdi
	shlq	$2, %rdx
	xorl	%esi, %esi
	callq	hipMemset
.LBB1_4:
	movq	(%rsp), %rdi
	shlq	$2, %r15
	movq	%rbx, 16(%rsp)                  # 8-byte Spill
	movq	%rbx, %rsi
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	testl	%r14d, %r14d
	je	.LBB1_18
# %bb.5:                                # %.lr.ph.preheader
	movl	$2, %ebp
	movabsq	$4294967296, %r15               # imm = 0x100000000
	leaq	1(%r15), %rbx
.LBB1_6:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%r14d, %eax
	xorl	%edx, %edx
	divl	%ebp
	movl	%eax, %r12d
	cmpl	$33, %eax
	jb	.LBB1_10
# %bb.7:                                #   in Loop: Header=BB1_6 Depth=1
	cmpl	$1056, %r12d                    # imm = 0x420
	jb	.LBB1_11
# %bb.8:                                #   in Loop: Header=BB1_6 Depth=1
	cmpl	$2098176, %r12d                 # imm = 0x200400
	jb	.LBB1_16
# %bb.9:                                #   in Loop: Header=BB1_6 Depth=1
	shrl	$21, %r12d
	shlq	$32, %r12
	orq	$2048, %r12                     # imm = 0x800
	jmp	.LBB1_17
.LBB1_10:                               #   in Loop: Header=BB1_6 Depth=1
	movl	%r12d, %r13d
	orq	%r15, %r13
	jmp	.LBB1_12
.LBB1_11:                               #   in Loop: Header=BB1_6 Depth=1
	shrl	$5, %r12d
	shlq	$32, %r12
	orq	$32, %r12
	movq	%r12, %r13
.LBB1_12:                               #   in Loop: Header=BB1_6 Depth=1
	movq	%rbx, %r12
	jmp	.LBB1_13
.LBB1_16:                               #   in Loop: Header=BB1_6 Depth=1
	shrl	$10, %r12d
	orq	%r15, %r12
.LBB1_17:                               #   in Loop: Header=BB1_6 Depth=1
	movabsq	$137438953504, %r13             # imm = 0x2000000020
.LBB1_13:                               #   in Loop: Header=BB1_6 Depth=1
	movq	%r12, %rdi
	movl	$1, %esi
	movq	%r13, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_15
# %bb.14:                               #   in Loop: Header=BB1_6 Depth=1
	imull	%r13d, %r12d
	movq	(%rsp), %rdi
	movl	%ebp, %esi
	movl	%r12d, %edx
	callq	_Z24__device_stub__addKernelPjjj
.LBB1_15:                               #   in Loop: Header=BB1_6 Depth=1
	callq	hipDeviceSynchronize
	addl	%ebp, %ebp
	cmpl	%r14d, %ebp
	jbe	.LBB1_6
.LBB1_18:                               # %._crit_edge
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	subq	8(%rsp), %rax                   # 8-byte Folded Reload
	movabsq	$4835703278458516699, %rcx      # imm = 0x431BDE82D7B634DB
	imulq	%rcx
	movq	%rdx, %r14
	shrq	$63, %r14
	sarq	$18, %rdx
	addq	%rdx, %r14
	movl	$_ZSt4cout, %edi
	movl	$.L.str.1, %esi
	movl	$7, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	$_ZSt4cout, %edi
	movq	%r14, %rsi
	callq	_ZNSo9_M_insertIlEERSoT_
	movq	%rax, %r14
	movl	$.L.str.2, %esi
	movl	$3, %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
	callq	hipGetLastError
	testl	%eax, %eax
	movq	16(%rsp), %rbx                  # 8-byte Reload
	je	.LBB1_23
# %bb.19:
	movl	%eax, %edi
	callq	hipGetErrorString
	testq	%rax, %rax
	je	.LBB1_21
# %bb.20:
	movq	%rax, %r14
	movq	%rax, %rdi
	callq	strlen
	movl	$_ZSt4cout, %edi
	movq	%r14, %rsi
	movq	%rax, %rdx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	jmp	.LBB1_22
.LBB1_21:
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rax
	leaq	_ZSt4cout(%rax), %rdi
	movl	32(%rdi), %esi
	orl	$1, %esi
	callq	_ZNSt9basic_iosIcSt11char_traitsIcEE5clearESt12_Ios_Iostate
.LBB1_22:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit
	movq	_ZSt4cout(%rip), %rax
	movl	$_ZSt4cout, %ecx
	movq	-24(%rax), %rdi
	addq	%rcx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.LBB1_23:
	movq	(%rsp), %rsi
	movl	$4, %edx
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	(%rsp), %rdi
	callq	hipFree
	movl	(%rbx), %eax
	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_end1:
	.size	_Z11addWithCudaPjj, .Lfunc_end1-_Z11addWithCudaPjj
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$48, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %rbp, -16
	xorps	%xmm0, %xmm0
	leaq	16(%rsp), %rbx
	movaps	%xmm0, (%rbx)
	movq	$0, 16(%rbx)
	movl	$536870912, %ebp                # imm = 0x20000000
	leaq	12(%rsp), %r14
.LBB2_1:                                # =>This Inner Loop Header: Depth=1
	movl	$1, 12(%rsp)
	movq	24(%rsp), %rsi
	cmpq	32(%rsp), %rsi
	je	.LBB2_3
# %bb.2:                                #   in Loop: Header=BB2_1 Depth=1
	movl	$1, (%rsi)
	addq	$4, %rsi
	movq	%rsi, 24(%rsp)
	jmp	.LBB2_4
.LBB2_3:                                #   in Loop: Header=BB2_1 Depth=1
.Ltmp0:
	movq	%rbx, %rdi
	movq	%r14, %rdx
	callq	_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_
.Ltmp1:
.LBB2_4:                                # %_ZNSt6vectorIjSaIjEE9push_backEOj.exit
                                        #   in Loop: Header=BB2_1 Depth=1
	decl	%ebp
	jne	.LBB2_1
# %bb.5:
	movq	16(%rsp), %rdi
	movq	24(%rsp), %rsi
	subq	%rdi, %rsi
	shrq	$2, %rsi
.Ltmp3:
                                        # kill: def $esi killed $esi killed $rsi
	callq	_Z11addWithCudaPjj
.Ltmp4:
# %bb.6:
	movl	%eax, %esi
.Ltmp5:
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertImEERSoT_
.Ltmp6:
# %bb.7:                                # %_ZNSolsEj.exit
	movq	%rax, %rbx
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
.Ltmp7:
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
.Ltmp8:
# %bb.8:                                # %.noexc12
.Ltmp9:
	movsbl	%al, %esi
	movq	%rbx, %rdi
	callq	_ZNSo3putEc
.Ltmp10:
# %bb.9:                                # %.noexc13
.Ltmp11:
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.Ltmp12:
# %bb.10:                               # %_ZNSolsEPFRSoS_E.exit
.Ltmp13:
	callq	hipDeviceReset
.Ltmp14:
# %bb.11:
	movq	16(%rsp), %rdi
	testq	%rdi, %rdi
	je	.LBB2_13
# %bb.12:
	callq	_ZdlPv
.LBB2_13:                               # %_ZNSt6vectorIjSaIjEED2Ev.exit
	xorl	%eax, %eax
	addq	$48, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB2_14:
	.cfi_def_cfa_offset 80
.Ltmp15:
	jmp	.LBB2_15
.LBB2_18:
.Ltmp2:
.LBB2_15:
	movq	%rax, %rbx
	movq	16(%rsp), %rdi
	testq	%rdi, %rdi
	je	.LBB2_17
# %bb.16:
	callq	_ZdlPv
.LBB2_17:                               # %_ZNSt6vectorIjSaIjEED2Ev.exit11
	movq	%rbx, %rdi
	callq	_Unwind_Resume@PLT
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table2:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	255                             # @TType Encoding = omit
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 1 <<
	.uleb128 .Ltmp1-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp1
	.uleb128 .Ltmp2-.Lfunc_begin0           #     jumps to .Ltmp2
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp3-.Lfunc_begin0           # >> Call Site 2 <<
	.uleb128 .Ltmp14-.Ltmp3                 #   Call between .Ltmp3 and .Ltmp14
	.uleb128 .Ltmp15-.Lfunc_begin0          #     jumps to .Ltmp15
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp14-.Lfunc_begin0          # >> Call Site 3 <<
	.uleb128 .Lfunc_end2-.Ltmp14            #   Call between .Ltmp14 and .Lfunc_end2
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
.Lcst_end0:
	.p2align	2, 0x0
                                        # -- End function
	.section	.text._ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_,"axG",@progbits,_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_,comdat
	.weak	_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_ # -- Begin function _ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_
	.p2align	1, 0x90
	.type	_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_,@function
_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_: # @_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_
	.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	%rdx, (%rsp)                    # 8-byte Spill
	movq	%rsi, %r12
	movq	%rdi, %rbx
	movl	$1, %esi
	movl	$.L.str.3, %edx
	callq	_ZNKSt6vectorIjSaIjEE12_M_check_lenEmPKc
	movq	(%rbx), %r13
	movq	8(%rbx), %r15
	movq	%r12, 8(%rsp)                   # 8-byte Spill
	subq	%r13, %r12
	movq	%r12, %r14
	sarq	$2, %r14
	testq	%rax, %rax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	je	.LBB3_1
# %bb.2:
	movq	%rbx, %rdi
	movq	%rax, %rsi
	xorl	%edx, %edx
	callq	_ZNSt15__new_allocatorIjE8allocateEmPKv
	movq	%rax, %rbp
	jmp	.LBB3_3
.LBB3_1:
	xorl	%ebp, %ebp
.LBB3_3:                                # %_ZNSt12_Vector_baseIjSaIjEE11_M_allocateEm.exit
	movq	(%rsp), %rax                    # 8-byte Reload
	movl	(%rax), %eax
	movl	%eax, (%rbp,%r14,4)
	testq	%r12, %r12
	jle	.LBB3_5
# %bb.4:
	movq	%rbp, %rdi
	movq	%r13, %rsi
	movq	%r12, %rdx
	callq	memmove@PLT
.LBB3_5:                                # %_ZNSt6vectorIjSaIjEE11_S_relocateEPjS2_S2_RS0_.exit
	addq	%rbp, %r12
	addq	$4, %r12
	movq	8(%rsp), %rsi                   # 8-byte Reload
	subq	%rsi, %r15
	testq	%r15, %r15
	jle	.LBB3_7
# %bb.6:
	movq	%r12, %rdi
	movq	%r15, %rdx
	callq	memmove@PLT
.LBB3_7:                                # %_ZNSt6vectorIjSaIjEE11_S_relocateEPjS2_S2_RS0_.exit16
	testq	%r13, %r13
	je	.LBB3_9
# %bb.8:
	movq	%r13, %rdi
	callq	_ZdlPv
.LBB3_9:                                # %_ZNSt12_Vector_baseIjSaIjEE13_M_deallocateEPjm.exit
	addq	%r15, %r12
	movq	%rbp, (%rbx)
	movq	%r12, 8(%rbx)
	movq	16(%rsp), %rax                  # 8-byte Reload
	leaq	(,%rax,4), %rax
	addq	%rbp, %rax
	movq	%rax, 16(%rbx)
	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_end3:
	.size	_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_, .Lfunc_end3-_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_
	.cfi_endproc
                                        # -- End function
	.section	.text._ZNKSt6vectorIjSaIjEE12_M_check_lenEmPKc,"axG",@progbits,_ZNKSt6vectorIjSaIjEE12_M_check_lenEmPKc,comdat
	.weak	_ZNKSt6vectorIjSaIjEE12_M_check_lenEmPKc # -- Begin function _ZNKSt6vectorIjSaIjEE12_M_check_lenEmPKc
	.p2align	1, 0x90
	.type	_ZNKSt6vectorIjSaIjEE12_M_check_lenEmPKc,@function
_ZNKSt6vectorIjSaIjEE12_M_check_lenEmPKc: # @_ZNKSt6vectorIjSaIjEE12_M_check_lenEmPKc
	.cfi_startproc
# %bb.0:
	movabsq	$2305843009213693951, %rcx      # imm = 0x1FFFFFFFFFFFFFFF
	movq	8(%rdi), %r8
	subq	(%rdi), %r8
	sarq	$2, %r8
	movq	%rcx, %rax
	subq	%r8, %rax
	cmpq	%rsi, %rax
	jb	.LBB4_2
# %bb.1:
	cmpq	%rsi, %r8
	cmovaq	%r8, %rsi
	leaq	(%rsi,%r8), %rax
	cmpq	%rcx, %rax
	cmovaeq	%rcx, %rax
	addq	%r8, %rsi
	cmovbq	%rcx, %rax
	retq
.LBB4_2:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdx, %rdi
	callq	_ZSt20__throw_length_errorPKc
.Lfunc_end4:
	.size	_ZNKSt6vectorIjSaIjEE12_M_check_lenEmPKc, .Lfunc_end4-_ZNKSt6vectorIjSaIjEE12_M_check_lenEmPKc
	.cfi_endproc
                                        # -- End function
	.section	.text._ZNSt15__new_allocatorIjE8allocateEmPKv,"axG",@progbits,_ZNSt15__new_allocatorIjE8allocateEmPKv,comdat
	.weak	_ZNSt15__new_allocatorIjE8allocateEmPKv # -- Begin function _ZNSt15__new_allocatorIjE8allocateEmPKv
	.p2align	1, 0x90
	.type	_ZNSt15__new_allocatorIjE8allocateEmPKv,@function
_ZNSt15__new_allocatorIjE8allocateEmPKv: # @_ZNSt15__new_allocatorIjE8allocateEmPKv
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rsi, %rax
	shrq	$61, %rax
	jne	.LBB5_1
# %bb.3:
	shlq	$2, %rsi
	movq	%rsi, %rdi
	popq	%rax
	.cfi_def_cfa_offset 8
	jmp	_Znwm                           # TAILCALL
.LBB5_1:
	.cfi_def_cfa_offset 16
	shrq	$62, %rsi
	je	.LBB5_2
# %bb.4:
	callq	_ZSt28__throw_bad_array_new_lengthv
.LBB5_2:
	callq	_ZSt17__throw_bad_allocv
.Lfunc_end5:
	.size	_ZNSt15__new_allocatorIjE8allocateEmPKv, .Lfunc_end5-_ZNSt15__new_allocatorIjE8allocateEmPKv
	.cfi_endproc
                                        # -- End function
	.text
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z9addKernelPjjj, %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_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__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	.LBB7_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
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z9addKernelPjjj,@object        # @_Z9addKernelPjjj
	.section	.rodata,"a",@progbits
	.globl	_Z9addKernelPjjj
	.p2align	3, 0x0
_Z9addKernelPjjj:
	.quad	_Z24__device_stub__addKernelPjjj
	.size	_Z9addKernelPjjj, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"Time:  "
	.size	.L.str.1, 8

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	" ms"
	.size	.L.str.2, 4

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"vector::_M_realloc_insert"
	.size	.L.str.3, 26

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z9addKernelPjjj"
	.size	.L__unnamed_1, 17

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

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z9addKernelPjjj        ; -- Begin function _Z9addKernelPjjj
	.globl	_Z9addKernelPjjj
	.p2align	8
	.type	_Z9addKernelPjjj,@function
_Z9addKernelPjjj:                       ; @_Z9addKernelPjjj
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x1c
	s_load_b128 s[0:3], s[0:1], 0x0
	v_bfe_u32 v1, v0, 10, 10
	v_and_b32_e32 v0, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s5, s4, 16
	s_delay_alu instid0(VALU_DEP_2) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[2:3], null, s15, s5, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_mul_lo_u32 v1, v2, s3
	s_and_b32 s3, s4, 0xffff
	s_mul_i32 s14, s14, s3
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_add3_u32 v0, s14, v0, v1
	v_mov_b32_e32 v1, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v0, v0, s2
	s_lshr_b32 s2, s2, 1
	v_lshlrev_b64 v[2:3], 2, v[0:1]
	v_add_nc_u32_e32 v0, s2, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	v_add_co_u32 v2, vcc_lo, s0, v2
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v3, vcc_lo
	v_add_co_u32 v0, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_4)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	s_clause 0x1
	global_load_b32 v4, v[2:3], off
	global_load_b32 v0, v[0:1], off
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v0, v0, v4
	global_store_b32 v[2:3], v0, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z9addKernelPjjj
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.amdhsa_user_sgpr_count 14
		.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 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 5
		.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	_Z9addKernelPjjj, .Lfunc_end0-_Z9addKernelPjjj
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 216
; NumSgprs: 18
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 5
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; 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 __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z9addKernelPjjj
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z9addKernelPjjj.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     5
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
