#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>

#define N (1<<22)
#define BLOCK_SIZE 128

static void HandleError( cudaError_t err,
                         const char *file,
                         int line ) {
    if (err != cudaSuccess) {
        printf( "%s in %s at line %d\n", cudaGetErrorString( err ),
                file, line );
        exit( EXIT_FAILURE );
    }
}
#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))


#define HANDLE_NULL( a ) {if (a == NULL) { \
                            printf( "Host memory failed in %s at line %d\n", \
                                    __FILE__, __LINE__ ); \
                            exit( EXIT_FAILURE );}}

__global__ void reduxKernel(int* v, int n, int* res){
    int tx = threadIdx.x;
    int idx = blockIdx.x * (BLOCK_SIZE*2) + tx;
    __shared__ int vs[BLOCK_SIZE*2]; 
    
    // all threads execute the following, as the size of the shared memory is 
    // 2x the number of threads
    int reg = idx < n ? v[idx] : 0;
    if( idx + BLOCK_SIZE < n ){
        reg += v[idx + BLOCK_SIZE];
    }

    //vs[tx] = v[idx] + v[idx + BLOCK_SIZE];
    vs[tx] = reg;
    __syncthreads();

    if( BLOCK_SIZE >= 512 ){
        if( tx < 256 ){ vs[tx] = reg = reg + vs[tx + 256]; __syncthreads(); }
    } 
    if( BLOCK_SIZE >= 256 ){
        if( tx < 128 ){ vs[tx] = reg = reg + vs[tx + 128]; __syncthreads(); }
    }
    if( BLOCK_SIZE >= 128 ){
        if( tx < 64 ){ vs[tx] = reg = reg + vs[tx + 64]; __syncthreads(); }
    }
//    for( int bs = BLOCK_SIZE/2 ; bs > 32; bs >>= 1 ){
//        if( tx < bs ){
//            //accumulating on the register var "reg" saves a read
//            //from shared memory. Instead of doing vs[tx] += vs[tx + bs], 
//            //we just accum on the reg and write it back to shared memory
//            vs[tx] = reg = reg + vs[tx + bs];
//        }
//        __syncthreads();
//    }

    if( tx < 32 ){ //for the last wrap. Threads within a wrap don't need synchronize
        volatile int* synchdShared = vs; //so that the compiler doesn't reorder

        // in case the block size were less than 32 to begin with
        if( BLOCK_SIZE  >= 64 ){
            // means we can "reach out" fully 32 positions to the right of the thread
            // look at the general loop to work out why
            synchdShared[tx] = reg = reg + synchdShared[tx + 32];
        }
        //and of course we are gonna fall-through
        if( BLOCK_SIZE >= 32 ){ 
            synchdShared[tx] = reg = reg + synchdShared[tx + 16];
        }
        if( BLOCK_SIZE >= 16 ){
            synchdShared[tx] = reg = reg + synchdShared[tx + 8];
        }
        if( BLOCK_SIZE >= 8 ){
            synchdShared[tx] = reg = reg + synchdShared[tx + 4];
        }
        if( BLOCK_SIZE >= 4 ){
            synchdShared[tx] = reg = reg + synchdShared[tx + 2];
        }
        if( BLOCK_SIZE >= 2 ){
            synchdShared[tx] = reg = reg + synchdShared[tx + 1];
        }

    }

    //interesting: without this if, the kernel execution takes 5x the time! Probably
    //due the memory conflicts created by _ALL_ the threads trying to write to the same
    //memory location "at once"
    if(tx == 0){ 
       res[blockIdx.x] = vs[0];
    }

}

int hostRedux(int* v, int n){
    int res = 0;
    for(int i=0; i < n; i++){
        res += v[i];
    }
    return res;
}

void deviceRedux(int* v, int n, int* res){
    // reserve memory on device
    int *vd; 
    int *resd;
 
    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    float time; 

    const int numBlocks = (N/BLOCK_SIZE)/2;

    HANDLE_ERROR( cudaMalloc((void**)&vd, n*sizeof(int)) );
    HANDLE_ERROR( cudaMalloc((void**)&resd, numBlocks*sizeof(int)) );

    cudaEventRecord( start, 0 ); 
    // transfer v to vd
    HANDLE_ERROR( cudaMemcpy(vd, v, n*sizeof(int), cudaMemcpyHostToDevice) );
    cudaEventRecord( stop, 0 );
    
    cudaEventSynchronize( stop );
    cudaEventElapsedTime( &time, start, stop );
    printf("memcpy in time: %f ms\n", time);

 
    cudaEventRecord( start, 0 );
    // invoke kernel
    reduxKernel<<<numBlocks,BLOCK_SIZE>>>(vd,n,resd);
    cudaEventRecord( stop, 0 );
    
    cudaEventSynchronize( stop );

    cudaEventElapsedTime( &time, start, stop );

    printf("kernel time: %f ms. ", time);
    printf("Bandwidth: %f GB/s\n", (N*sizeof(int)/1e6)/time);

    cudaEventRecord( start, 0 );  
    // copy results back to host
    HANDLE_ERROR( cudaMemcpy(res, resd, numBlocks*sizeof(int), cudaMemcpyDeviceToHost) );
    cudaEventRecord( stop, 0 );
    
    cudaEventSynchronize( stop );
    cudaEventElapsedTime( &time, start, stop );
    printf("memcpy out time: %f ms\n", time);
 
 
    cudaEventDestroy(start);
    cudaEventDestroy(stop);
    //free vd
    cudaFree(vd);
    cudaFree(resd);
}

     /* Subtract the `struct timeval' values X and Y,
        storing the result in RESULT.
        Return 1 if the difference is negative, otherwise 0.  */
     
int
timeval_subtract (struct timeval* result, struct timeval*x, struct timeval*y)
{
  /* Perform the carry for the later subtraction by updating y. */
  if (x->tv_usec < y->tv_usec) {
    int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
    y->tv_usec -= 1000000 * nsec;
    y->tv_sec += nsec;
  }
  if (x->tv_usec - y->tv_usec > 1000000) {
    int nsec = (x->tv_usec - y->tv_usec) / 1000000;
    y->tv_usec += 1000000 * nsec;
    y->tv_sec -= nsec;
  }

  /* Compute the time remaining to wait.
     tv_usec is certainly positive. */
  result->tv_sec = x->tv_sec - y->tv_sec;
  result->tv_usec = x->tv_usec - y->tv_usec;

  /* Return 1 if result is negative. */
  return x->tv_sec < y->tv_sec;
}


int main(){
    int* v = (int*)malloc( sizeof(int) * N);
    for(int i=0; i < N; i++){
        v[i] = rand() % 10;
        //v[i] =  i;
    }

    int h;
    struct timeval tv_start;
    struct timeval tv_stop;
    struct timeval tv_diff;
    gettimeofday(&tv_start, 0);
    h=hostRedux(v,N);
    gettimeofday(&tv_stop, 0);
    timeval_subtract(&tv_diff, &tv_stop, &tv_start);
    printf("host: %d\n", h);
    float msdiff = tv_diff.tv_sec*1000 + tv_diff.tv_usec/1000.0;
    printf("CPU time: %.3f ms\n", msdiff);

    const int numBlocks = (N/BLOCK_SIZE)/2;
    int *res = (int*)malloc( sizeof(int) * numBlocks);
    int d;

    cudaEvent_t start, stop;
    float time; 
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    cudaEventRecord( start, 0 );

    deviceRedux(v,N, res);
    d=0;
    for(int i=0; i < numBlocks; i++){
       // printf("%d\n", res[i]);
        d+=res[i];
    }
    cudaEventRecord( stop, 0 );
    cudaEventSynchronize( stop );

    cudaEventElapsedTime( &time, start, stop );
    cudaEventDestroy(start);
    cudaEventDestroy(stop);

    printf("CUDA time: %f ms\n", time);


    printf("device: %d\n", d);

    free(v);
    free(res);
    return !(h==d);

}

	.file	"tmpxft_0029ae36_00000000-6_6.1.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/dgquintas/my-code-samples/master/cuda/ProgrammingMassivelyParallelProcessors/ch6/6.1.cu"
.LC1:
	.string	"%s in %s at line %d\n"
	.text
	.type	_ZL11HandleError9cudaErrorPKci.constprop.0, @function
_ZL11HandleError9cudaErrorPKci.constprop.0:
.LFB2176:
	.cfi_startproc
	testl	%edi, %edi
	je	.L2
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	%esi, 12(%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r8d
	movl	$2, %edi
	leaq	.LC0(%rip), %rcx
	movq	%rax, %rdx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L2:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2176:
	.size	_ZL11HandleError9cudaErrorPKci.constprop.0, .-_ZL11HandleError9cudaErrorPKci.constprop.0
	.globl	_Z9hostReduxPii
	.type	_Z9hostReduxPii, @function
_Z9hostReduxPii:
.LFB2028:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	xorl	%edx, %edx
.L9:
	cmpl	%eax, %esi
	jle	.L11
	addl	(%rdi,%rax,4), %edx
	incq	%rax
	jmp	.L9
.L11:
	movl	%edx, %eax
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z9hostReduxPii, .-_Z9hostReduxPii
	.globl	_Z16timeval_subtractP7timevalS0_S0_
	.type	_Z16timeval_subtractP7timevalS0_S0_, @function
_Z16timeval_subtractP7timevalS0_S0_:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	%rdx, %rcx
	movq	8(%rsi), %rdx
	movq	8(%rcx), %r8
	cmpq	%r8, %rdx
	jge	.L13
	movq	%r8, %rax
	movl	$1000000, %r9d
	subq	%rdx, %rax
	cqto
	idivq	%r9
	incl	%eax
	imull	$1000000, %eax, %edx
	cltq
	addq	%rax, (%rcx)
	movslq	%edx, %rdx
	subq	%rdx, %r8
	movq	%r8, 8(%rcx)
.L13:
	movq	8(%rcx), %r8
	movq	8(%rsi), %rax
	subq	%r8, %rax
	cmpq	$1000000, %rax
	jle	.L14
	movl	$1000000, %r9d
	cqto
	idivq	%r9
	imull	$1000000, %eax, %edx
	cltq
	subq	%rax, (%rcx)
	movslq	%edx, %rdx
	addq	%r8, %rdx
	movq	%rdx, 8(%rcx)
.L14:
	movq	(%rsi), %rax
	subq	(%rcx), %rax
	movq	%rax, (%rdi)
	movq	8(%rsi), %rax
	subq	8(%rcx), %rax
	movq	%rax, 8(%rdi)
	movq	(%rcx), %rax
	cmpq	%rax, (%rsi)
	setl	%al
	movzbl	%al, %eax
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z16timeval_subtractP7timevalS0_S0_, .-_Z16timeval_subtractP7timevalS0_S0_
	.globl	_Z34__device_stub__Z11reduxKernelPiiS_PiiS_
	.type	_Z34__device_stub__Z11reduxKernelPiiS_PiiS_, @function
_Z34__device_stub__Z11reduxKernelPiiS_PiiS_:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movl	%esi, 20(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L15
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z11reduxKernelPiiS_(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L15:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L17
	call	__stack_chk_fail@PLT
.L17:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z34__device_stub__Z11reduxKernelPiiS_PiiS_, .-_Z34__device_stub__Z11reduxKernelPiiS_PiiS_
	.globl	_Z11reduxKernelPiiS_
	.type	_Z11reduxKernelPiiS_, @function
_Z11reduxKernelPiiS_:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z34__device_stub__Z11reduxKernelPiiS_PiiS_
	.cfi_endproc
.LFE2057:
	.size	_Z11reduxKernelPiiS_, .-_Z11reduxKernelPiiS_
	.section	.rodata.str1.1
.LC2:
	.string	"memcpy in time: %f ms\n"
.LC3:
	.string	"kernel time: %f ms. "
.LC5:
	.string	"Bandwidth: %f GB/s\n"
.LC6:
	.string	"memcpy out time: %f ms\n"
	.text
	.globl	_Z11deviceReduxPiiS_
	.type	_Z11deviceReduxPiiS_, @function
_Z11deviceReduxPiiS_:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movslq	%esi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rbp, %rbx
	salq	$2, %rbp
	subq	$88, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	leaq	32(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	40(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	%rbp, %rsi
	leaq	16(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	$117, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	leaq	24(%rsp), %rdi
	movl	$65536, %esi
	call	cudaMalloc@PLT
	movl	$118, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	16(%rsp), %rdi
	movq	%rbp, %rdx
	movq	%r13, %rsi
	movl	$1, %ecx
	leaq	12(%rsp), %rbp
	call	cudaMemcpy@PLT
	movl	$122, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	40(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	movq	%rbp, %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	12(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	$33554433, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$262145, %edi
	salq	$7, %rdx
	movl	$1, %ecx
	movl	$1, %esi
	salq	$14, %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L21
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rdi
	movl	%ebx, %esi
	call	_Z34__device_stub__Z11reduxKernelPiiS_PiiS_
.L21:
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	40(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	movq	%rbp, %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	12(%rsp), %xmm0
	call	__printf_chk@PLT
	movl	$2, %edi
	movb	$1, %al
	movsd	.LC4(%rip), %xmm0
	cvtss2sd	12(%rsp), %xmm1
	leaq	.LC5(%rip), %rsi
	divsd	%xmm1, %xmm0
	call	__printf_chk@PLT
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movq	%r12, %rdi
	movl	$65536, %edx
	call	cudaMemcpy@PLT
	movl	$144, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	40(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	movq	%rbp, %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	12(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	32(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	40(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	addq	$88, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z11deviceReduxPiiS_, .-_Z11deviceReduxPiiS_
	.section	.rodata.str1.1
.LC7:
	.string	"host: %d\n"
.LC9:
	.string	"CPU time: %.3f ms\n"
.LC10:
	.string	"CUDA time: %f ms\n"
.LC11:
	.string	"device: %d\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	$16777216, %edi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movl	$10, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$88, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movq	%rax, %rbx
.L25:
	call	rand@PLT
	cltd
	idivl	%r12d
	movl	%edx, (%rbx,%rbp,4)
	incq	%rbp
	cmpq	$4194304, %rbp
	jne	.L25
	leaq	24(%rsp), %r13
	xorl	%esi, %esi
	leaq	40(%rsp), %rbp
	movq	%r13, %rdi
	call	gettimeofday@PLT
	movl	$4194304, %esi
	movq	%rbx, %rdi
	call	_Z9hostReduxPii
	movq	%rbp, %rdi
	xorl	%esi, %esi
	movl	%eax, %r12d
	call	gettimeofday@PLT
	movq	%r13, %rdx
	movq	%rbp, %rsi
	leaq	56(%rsp), %rdi
	call	_Z16timeval_subtractP7timevalS0_S0_
	movl	%r12d, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC7(%rip), %rsi
	xorl	%r13d, %r13d
	call	__printf_chk@PLT
	cvtsi2sdq	64(%rsp), %xmm1
	movl	$2, %edi
	divsd	.LC8(%rip), %xmm1
	imulq	$1000, 56(%rsp), %rax
	leaq	.LC9(%rip), %rsi
	cvtsi2sdq	%rax, %xmm0
	movb	$1, %al
	addsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movl	$65536, %edi
	call	malloc@PLT
	leaq	8(%rsp), %rdi
	movq	%rax, %rbp
	call	cudaEventCreate@PLT
	leaq	16(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	%rbp, %rdx
	movl	$4194304, %esi
	movq	%rbx, %rdi
	call	_Z11deviceReduxPiiS_
	xorl	%eax, %eax
.L26:
	addl	0(%rbp,%rax,4), %r13d
	incq	%rax
	cmpq	$16384, %rax
	jne	.L26
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	16(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rsi
	leaq	4(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	8(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	16(%rsp), %rdi
	call	cudaEventDestroy@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	4(%rsp), %xmm0
	call	__printf_chk@PLT
	movl	%r13d, %edx
	xorl	%eax, %eax
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	xorl	%eax, %eax
	cmpl	%r13d, %r12d
	setne	%al
	movq	72(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	addq	$88, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	main, .-main
	.section	.rodata.str1.1
.LC12:
	.string	"_Z11reduxKernelPiiS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.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	.LC12(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z11reduxKernelPiiS_(%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
.LFE2059:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC4:
	.long	-1598689907
	.long	1076938487
	.align 8
.LC8:
	.long	0
	.long	1083129856
	.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 <sys/time.h>
#include <stdlib.h>

#define N (1<<22)
#define BLOCK_SIZE 128

static void HandleError( hipError_t err,
                         const char *file,
                         int line ) {
    if (err != hipSuccess) {
        printf( "%s in %s at line %d\n", hipGetErrorString( err ),
                file, line );
        exit( EXIT_FAILURE );
    }
}
#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))


#define HANDLE_NULL( a ) {if (a == NULL) { \
                            printf( "Host memory failed in %s at line %d\n", \
                                    __FILE__, __LINE__ ); \
                            exit( EXIT_FAILURE );}}

__global__ void reduxKernel(int* v, int n, int* res){
    int tx = threadIdx.x;
    int idx = blockIdx.x * (BLOCK_SIZE*2) + tx;
    __shared__ int vs[BLOCK_SIZE*2]; 
    
    // all threads execute the following, as the size of the shared memory is 
    // 2x the number of threads
    int reg = idx < n ? v[idx] : 0;
    if( idx + BLOCK_SIZE < n ){
        reg += v[idx + BLOCK_SIZE];
    }

    //vs[tx] = v[idx] + v[idx + BLOCK_SIZE];
    vs[tx] = reg;
    __syncthreads();

    if( BLOCK_SIZE >= 512 ){
        if( tx < 256 ){ vs[tx] = reg = reg + vs[tx + 256]; __syncthreads(); }
    } 
    if( BLOCK_SIZE >= 256 ){
        if( tx < 128 ){ vs[tx] = reg = reg + vs[tx + 128]; __syncthreads(); }
    }
    if( BLOCK_SIZE >= 128 ){
        if( tx < 64 ){ vs[tx] = reg = reg + vs[tx + 64]; __syncthreads(); }
    }
//    for( int bs = BLOCK_SIZE/2 ; bs > 32; bs >>= 1 ){
//        if( tx < bs ){
//            //accumulating on the register var "reg" saves a read
//            //from shared memory. Instead of doing vs[tx] += vs[tx + bs], 
//            //we just accum on the reg and write it back to shared memory
//            vs[tx] = reg = reg + vs[tx + bs];
//        }
//        __syncthreads();
//    }

    if( tx < 32 ){ //for the last wrap. Threads within a wrap don't need synchronize
        volatile int* synchdShared = vs; //so that the compiler doesn't reorder

        // in case the block size were less than 32 to begin with
        if( BLOCK_SIZE  >= 64 ){
            // means we can "reach out" fully 32 positions to the right of the thread
            // look at the general loop to work out why
            synchdShared[tx] = reg = reg + synchdShared[tx + 32];
        }
        //and of course we are gonna fall-through
        if( BLOCK_SIZE >= 32 ){ 
            synchdShared[tx] = reg = reg + synchdShared[tx + 16];
        }
        if( BLOCK_SIZE >= 16 ){
            synchdShared[tx] = reg = reg + synchdShared[tx + 8];
        }
        if( BLOCK_SIZE >= 8 ){
            synchdShared[tx] = reg = reg + synchdShared[tx + 4];
        }
        if( BLOCK_SIZE >= 4 ){
            synchdShared[tx] = reg = reg + synchdShared[tx + 2];
        }
        if( BLOCK_SIZE >= 2 ){
            synchdShared[tx] = reg = reg + synchdShared[tx + 1];
        }

    }

    //interesting: without this if, the kernel execution takes 5x the time! Probably
    //due the memory conflicts created by _ALL_ the threads trying to write to the same
    //memory location "at once"
    if(tx == 0){ 
       res[blockIdx.x] = vs[0];
    }

}

int hostRedux(int* v, int n){
    int res = 0;
    for(int i=0; i < n; i++){
        res += v[i];
    }
    return res;
}

void deviceRedux(int* v, int n, int* res){
    // reserve memory on device
    int *vd; 
    int *resd;
 
    hipEvent_t start, stop;
    hipEventCreate(&start);
    hipEventCreate(&stop);
    float time; 

    const int numBlocks = (N/BLOCK_SIZE)/2;

    HANDLE_ERROR( hipMalloc((void**)&vd, n*sizeof(int)) );
    HANDLE_ERROR( hipMalloc((void**)&resd, numBlocks*sizeof(int)) );

    hipEventRecord( start, 0 ); 
    // transfer v to vd
    HANDLE_ERROR( hipMemcpy(vd, v, n*sizeof(int), hipMemcpyHostToDevice) );
    hipEventRecord( stop, 0 );
    
    hipEventSynchronize( stop );
    hipEventElapsedTime( &time, start, stop );
    printf("memcpy in time: %f ms\n", time);

 
    hipEventRecord( start, 0 );
    // invoke kernel
    reduxKernel<<<numBlocks,BLOCK_SIZE>>>(vd,n,resd);
    hipEventRecord( stop, 0 );
    
    hipEventSynchronize( stop );

    hipEventElapsedTime( &time, start, stop );

    printf("kernel time: %f ms. ", time);
    printf("Bandwidth: %f GB/s\n", (N*sizeof(int)/1e6)/time);

    hipEventRecord( start, 0 );  
    // copy results back to host
    HANDLE_ERROR( hipMemcpy(res, resd, numBlocks*sizeof(int), hipMemcpyDeviceToHost) );
    hipEventRecord( stop, 0 );
    
    hipEventSynchronize( stop );
    hipEventElapsedTime( &time, start, stop );
    printf("memcpy out time: %f ms\n", time);
 
 
    hipEventDestroy(start);
    hipEventDestroy(stop);
    //free vd
    hipFree(vd);
    hipFree(resd);
}

     /* Subtract the `struct timeval' values X and Y,
        storing the result in RESULT.
        Return 1 if the difference is negative, otherwise 0.  */
     
int
timeval_subtract (struct timeval* result, struct timeval*x, struct timeval*y)
{
  /* Perform the carry for the later subtraction by updating y. */
  if (x->tv_usec < y->tv_usec) {
    int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
    y->tv_usec -= 1000000 * nsec;
    y->tv_sec += nsec;
  }
  if (x->tv_usec - y->tv_usec > 1000000) {
    int nsec = (x->tv_usec - y->tv_usec) / 1000000;
    y->tv_usec += 1000000 * nsec;
    y->tv_sec -= nsec;
  }

  /* Compute the time remaining to wait.
     tv_usec is certainly positive. */
  result->tv_sec = x->tv_sec - y->tv_sec;
  result->tv_usec = x->tv_usec - y->tv_usec;

  /* Return 1 if result is negative. */
  return x->tv_sec < y->tv_sec;
}


int main(){
    int* v = (int*)malloc( sizeof(int) * N);
    for(int i=0; i < N; i++){
        v[i] = rand() % 10;
        //v[i] =  i;
    }

    int h;
    struct timeval tv_start;
    struct timeval tv_stop;
    struct timeval tv_diff;
    gettimeofday(&tv_start, 0);
    h=hostRedux(v,N);
    gettimeofday(&tv_stop, 0);
    timeval_subtract(&tv_diff, &tv_stop, &tv_start);
    printf("host: %d\n", h);
    float msdiff = tv_diff.tv_sec*1000 + tv_diff.tv_usec/1000.0;
    printf("CPU time: %.3f ms\n", msdiff);

    const int numBlocks = (N/BLOCK_SIZE)/2;
    int *res = (int*)malloc( sizeof(int) * numBlocks);
    int d;

    hipEvent_t start, stop;
    float time; 
    hipEventCreate(&start);
    hipEventCreate(&stop);
    hipEventRecord( start, 0 );

    deviceRedux(v,N, res);
    d=0;
    for(int i=0; i < numBlocks; i++){
       // printf("%d\n", res[i]);
        d+=res[i];
    }
    hipEventRecord( stop, 0 );
    hipEventSynchronize( stop );

    hipEventElapsedTime( &time, start, stop );
    hipEventDestroy(start);
    hipEventDestroy(stop);

    printf("CUDA time: %f ms\n", time);


    printf("device: %d\n", d);

    free(v);
    free(res);
    return !(h==d);

}

	.text
	.file	"6.1.hip"
	.globl	_Z26__device_stub__reduxKernelPiiS_ # -- Begin function _Z26__device_stub__reduxKernelPiiS_
	.type	_Z26__device_stub__reduxKernelPiiS_,@function
_Z26__device_stub__reduxKernelPiiS_:    # @_Z26__device_stub__reduxKernelPiiS_
	.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	12(%rsp), %rcx
	movl	%esi, (%rcx)
	leaq	32(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	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	$_Z11reduxKernelPiiS_, %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	_Z26__device_stub__reduxKernelPiiS_, .Lfunc_end0-_Z26__device_stub__reduxKernelPiiS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z9hostReduxPii                 # -- Begin function _Z9hostReduxPii
	.type	_Z9hostReduxPii,@function
_Z9hostReduxPii:                        # @_Z9hostReduxPii
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB1_1
# %bb.3:                                # %.lr.ph.preheader
	movl	%esi, %ecx
	xorl	%edx, %edx
	xorl	%eax, %eax
.LBB1_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	addl	(%rdi,%rdx,4), %eax
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB1_4
# %bb.2:                                # %._crit_edge
	retq
.LBB1_1:
	xorl	%eax, %eax
	retq
.Lfunc_end1:
	.size	_Z9hostReduxPii, .Lfunc_end1-_Z9hostReduxPii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z11deviceReduxPiiS_
.LCPI2_0:
	.quad	0x4030c6f7a0b5ed8d              # double 16.777215999999999
	.text
	.globl	_Z11deviceReduxPiiS_
	.type	_Z11deviceReduxPiiS_,@function
_Z11deviceReduxPiiS_:                   # @_Z11deviceReduxPiiS_
	.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	$56, %rsp
	.cfi_def_cfa_offset 112
	.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, 48(%rsp)                  # 8-byte Spill
	movl	%esi, %ebp
	movq	%rdi, %r12
	leaq	16(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventCreate
	movslq	%ebp, %r13
	shlq	$2, %r13
	leaq	40(%rsp), %rbx
	movq	%rbx, %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$119, %esi
	callq	_ZL11HandleError10hipError_tPKci
	leaq	24(%rsp), %rdi
	movl	$65536, %esi                    # imm = 0x10000
	callq	hipMalloc
	movl	%eax, %edi
	movl	$120, %esi
	callq	_ZL11HandleError10hipError_tPKci
	movq	(%r14), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rbx), %rdi
	movq	%r12, %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$124, %esi
	callq	_ZL11HandleError10hipError_tPKci
	movq	(%r15), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%r15), %rdi
	callq	hipEventSynchronize
	movq	(%r14), %rsi
	movq	(%r15), %rdx
	leaq	36(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	cvtss2sd	(%rbx), %xmm0
	movl	$.L.str.1, %edi
	movb	$1, %al
	callq	printf
	movq	(%r14), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movabsq	$4294967424, %rdx               # imm = 0x100000080
	leaq	16256(%rdx), %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_2
# %bb.1:
	movq	40(%rsp), %rdi
	movq	24(%rsp), %rdx
	movl	%ebp, %esi
	callq	_Z26__device_stub__reduxKernelPiiS_
.LBB2_2:
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	36(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventElapsedTime
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r14), %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	cvtss2sd	(%r14), %xmm1
	movsd	.LCPI2_0(%rip), %xmm0           # xmm0 = mem[0],zero
	divsd	%xmm1, %xmm0
	movl	$.L.str.3, %edi
	movb	$1, %al
	callq	printf
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	24(%rsp), %rsi
	movl	$65536, %edx                    # imm = 0x10000
	movq	48(%rsp), %rdi                  # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$146, %esi
	callq	_ZL11HandleError10hipError_tPKci
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdx
	movq	%r14, %rdi
	callq	hipEventElapsedTime
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r14), %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movq	16(%rsp), %rdi
	callq	hipEventDestroy
	movq	8(%rsp), %rdi
	callq	hipEventDestroy
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z11deviceReduxPiiS_, .Lfunc_end2-_Z11deviceReduxPiiS_
	.cfi_endproc
                                        # -- End function
	.type	_ZL11HandleError10hipError_tPKci,@function # -- Begin function _ZL11HandleError10hipError_tPKci
_ZL11HandleError10hipError_tPKci:       # @_ZL11HandleError10hipError_tPKci
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jne	.LBB3_2
# %bb.1:
	retq
.LBB3_2:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%esi, %ebx
	callq	hipGetErrorString
	movl	$.L.str.9, %edi
	movl	$.L.str, %edx
	movq	%rax, %rsi
	movl	%ebx, %ecx
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end3:
	.size	_ZL11HandleError10hipError_tPKci, .Lfunc_end3-_ZL11HandleError10hipError_tPKci
	.cfi_endproc
                                        # -- End function
	.globl	_Z16timeval_subtractP7timevalS0_S0_ # -- Begin function _Z16timeval_subtractP7timevalS0_S0_
	.type	_Z16timeval_subtractP7timevalS0_S0_,@function
_Z16timeval_subtractP7timevalS0_S0_:    # @_Z16timeval_subtractP7timevalS0_S0_
	.cfi_startproc
# %bb.0:
	movq	%rdx, %rcx
	movq	8(%rsi), %rax
	movq	(%rdx), %r8
	movq	8(%rdx), %r9
	movq	%r9, %rdx
	movabsq	$4835703278458516699, %r10      # imm = 0x431BDE82D7B634DB
	subq	%rax, %rdx
	jle	.LBB4_2
# %bb.1:
	movq	%rdx, %rax
	imulq	%r10
	movq	%rdx, %rax
	shrq	$18, %rax
	shrq	$63, %rdx
	addl	%edx, %eax
	incl	%eax
	imull	$1000000, %eax, %edx            # imm = 0xF4240
	movslq	%edx, %rdx
	subq	%rdx, %r9
	movq	%r9, 8(%rcx)
	cltq
	addq	%rax, %r8
	movq	%r8, (%rcx)
	movq	8(%rsi), %rax
.LBB4_2:
	subq	%r9, %rax
	cmpq	$1000001, %rax                  # imm = 0xF4241
	jl	.LBB4_4
# %bb.3:
	mulq	%r10
	shrq	$18, %rdx
	movabsq	$4294967296000000, %r10         # imm = 0xF424000000000
	imulq	%rdx, %r10
	sarq	$32, %r10
	addq	%r9, %r10
	movq	%r10, 8(%rcx)
	movslq	%edx, %rax
	subq	%rax, %r8
	movq	%r8, (%rcx)
	movq	8(%rsi), %rax
	subq	%r10, %rax
.LBB4_4:
	movq	(%rsi), %rdx
	subq	%r8, %rdx
	movq	%rdx, (%rdi)
	movq	%rax, 8(%rdi)
	movq	(%rsi), %rdx
	xorl	%eax, %eax
	cmpq	(%rcx), %rdx
	setl	%al
	retq
.Lfunc_end4:
	.size	_Z16timeval_subtractP7timevalS0_S0_, .Lfunc_end4-_Z16timeval_subtractP7timevalS0_S0_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI5_0:
	.quad	0x408f400000000000              # double 1000
	.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	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$16777216, %edi                 # imm = 0x1000000
	callq	malloc
	movq	%rax, %rbx
	xorl	%r14d, %r14d
.LBB5_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$34, %rcx
	addl	%edx, %ecx
	addl	%ecx, %ecx
	leal	(%rcx,%rcx,4), %ecx
	subl	%ecx, %eax
	movl	%eax, (%rbx,%r14,4)
	incq	%r14
	cmpq	$4194304, %r14                  # imm = 0x400000
	jne	.LBB5_1
# %bb.2:
	xorl	%r14d, %r14d
	leaq	32(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorl	%ebp, %ebp
.LBB5_3:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	addl	(%rbx,%r14,4), %ebp
	incq	%r14
	cmpq	$4194304, %r14                  # imm = 0x400000
	jne	.LBB5_3
# %bb.4:                                # %_Z9hostReduxPii.exit
	xorl	%r12d, %r12d
	leaq	64(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leaq	48(%rsp), %r15
	leaq	32(%rsp), %rdx
	movq	%r15, %rdi
	movq	%r14, %rsi
	callq	_Z16timeval_subtractP7timevalS0_S0_
	movl	$.L.str.5, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	imulq	$1000, (%r15), %rax             # imm = 0x3E8
	cvtsi2sd	%rax, %xmm0
	cvtsi2sdq	8(%r15), %xmm1
	divsd	.LCPI5_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	cvtsd2ss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	movl	$65536, %edi                    # imm = 0x10000
	callq	malloc
	movq	%rax, %r14
	leaq	16(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r15), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	%rbx, %rdi
	movl	$4194304, %esi                  # imm = 0x400000
	movq	%r14, %rdx
	callq	_Z11deviceReduxPiiS_
	xorl	%r15d, %r15d
.LBB5_5:                                # =>This Inner Loop Header: Depth=1
	addl	(%r14,%r12,4), %r15d
	incq	%r12
	cmpq	$16384, %r12                    # imm = 0x4000
	jne	.LBB5_5
# %bb.6:
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	28(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventElapsedTime
	movq	16(%rsp), %rdi
	callq	hipEventDestroy
	movq	8(%rsp), %rdi
	callq	hipEventDestroy
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r12), %xmm0
	movl	$.L.str.7, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.8, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	xorl	%eax, %eax
	cmpl	%r15d, %ebp
	setne	%al
	addq	$80, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.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_end5:
	.size	main, .Lfunc_end5-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	.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	$_Z11reduxKernelPiiS_, %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	_Z11reduxKernelPiiS_,@object    # @_Z11reduxKernelPiiS_
	.section	.rodata,"a",@progbits
	.globl	_Z11reduxKernelPiiS_
	.p2align	3, 0x0
_Z11reduxKernelPiiS_:
	.quad	_Z26__device_stub__reduxKernelPiiS_
	.size	_Z11reduxKernelPiiS_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/dgquintas/my-code-samples/master/cuda/ProgrammingMassivelyParallelProcessors/ch6/6.1.hip"
	.size	.L.str, 151

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"memcpy in time: %f ms\n"
	.size	.L.str.1, 23

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"kernel time: %f ms. "
	.size	.L.str.2, 21

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Bandwidth: %f GB/s\n"
	.size	.L.str.3, 20

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"memcpy out time: %f ms\n"
	.size	.L.str.4, 24

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"host: %d\n"
	.size	.L.str.5, 10

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"CPU time: %.3f ms\n"
	.size	.L.str.6, 19

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"CUDA time: %f ms\n"
	.size	.L.str.7, 18

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"device: %d\n"
	.size	.L.str.8, 12

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"%s in %s at line %d\n"
	.size	.L.str.9, 21

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z11reduxKernelPiiS_"
	.size	.L__unnamed_1, 21

	.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 _Z26__device_stub__reduxKernelPiiS_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z11reduxKernelPiiS_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z11reduxKernelPiiS_    ; -- Begin function _Z11reduxKernelPiiS_
	.globl	_Z11reduxKernelPiiS_
	.p2align	8
	.type	_Z11reduxKernelPiiS_,@function
_Z11reduxKernelPiiS_:                   ; @_Z11reduxKernelPiiS_
; %bb.0:
	s_clause 0x1
	s_load_b32 s5, s[0:1], 0x8
	s_load_b64 s[2:3], s[0:1], 0x0
	s_mov_b32 s4, s15
	v_mov_b32_e32 v3, 0
	v_lshl_add_u32 v1, s4, 8, v0
	s_mov_b32 s6, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_ashrrev_i32_e32 v2, 31, v1
	s_waitcnt lgkmcnt(0)
	v_cmpx_gt_i32_e64 s5, v1
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[3:4], 2, v[1:2]
	v_add_co_u32 v3, vcc_lo, s2, v3
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v4, vcc_lo, s3, v4, vcc_lo
	global_load_b32 v3, v[3:4], off
.LBB0_2:
	s_or_b32 exec_lo, exec_lo, s6
	v_add_nc_u32_e32 v4, 0x80, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_gt_i32_e32 vcc_lo, s5, v4
	s_and_saveexec_b32 s5, vcc_lo
	s_cbranch_execz .LBB0_4
; %bb.3:
	v_lshlrev_b64 v[1:2], 2, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v1, vcc_lo, s2, v1
	v_add_co_ci_u32_e32 v2, vcc_lo, s3, v2, vcc_lo
	global_load_b32 v1, v[1:2], off offset:512
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v3, v1, v3
.LBB0_4:
	s_or_b32 exec_lo, exec_lo, s5
	v_lshlrev_b32_e32 v1, 2, v0
	s_mov_b32 s2, exec_lo
	s_waitcnt vmcnt(0)
	ds_store_b32 v1, v3
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_cmpx_gt_u32_e32 64, v0
	s_cbranch_execz .LBB0_6
; %bb.5:
	ds_load_b32 v2, v1 offset:256
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v3, v2, v3
	ds_store_b32 v1, v3
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
.LBB0_6:
	s_or_b32 exec_lo, exec_lo, s2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mov_b32 s3, exec_lo
	v_cmpx_gt_u32_e32 32, v0
	s_cbranch_execz .LBB0_8
; %bb.7:
	v_lshl_add_u32 v2, v0, 2, 0x80
	s_mov_b64 s[6:7], src_shared_base
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_ne_u32_e32 vcc_lo, -1, v2
	v_cndmask_b32_e32 v4, 0, v2, vcc_lo
	v_cndmask_b32_e64 v5, 0, s7, vcc_lo
	v_add_nc_u32_e32 v2, 64, v1
	v_cmp_ne_u32_e32 vcc_lo, -1, v1
	flat_load_b32 v6, v[4:5] glc dlc
	s_waitcnt vmcnt(0)
	v_cmp_ne_u32_e64 s2, -1, v2
	v_cndmask_b32_e64 v5, 0, s7, vcc_lo
	v_cndmask_b32_e32 v4, 0, v1, vcc_lo
	s_delay_alu instid0(VALU_DEP_3)
	v_cndmask_b32_e64 v2, 0, v2, s2
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v6, v6, v3
	v_cndmask_b32_e64 v3, 0, s7, s2
	flat_store_b32 v[4:5], v6 dlc
	s_waitcnt_vscnt null, 0x0
	flat_load_b32 v3, v[2:3] glc dlc
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v2, 32, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_ne_u32_e32 vcc_lo, -1, v2
	v_cndmask_b32_e32 v2, 0, v2, vcc_lo
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v6, v3, v6
	v_cndmask_b32_e64 v3, 0, s7, vcc_lo
	flat_store_b32 v[4:5], v6 dlc
	s_waitcnt_vscnt null, 0x0
	flat_load_b32 v3, v[2:3] glc dlc
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v2, 16, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_ne_u32_e32 vcc_lo, -1, v2
	v_cndmask_b32_e32 v2, 0, v2, vcc_lo
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v6, v3, v6
	v_cndmask_b32_e64 v3, 0, s7, vcc_lo
	flat_store_b32 v[4:5], v6 dlc
	s_waitcnt_vscnt null, 0x0
	flat_load_b32 v3, v[2:3] glc dlc
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v2, 8, v1
	v_add_nc_u32_e32 v1, 4, v1
	s_delay_alu instid0(VALU_DEP_2)
	v_cmp_ne_u32_e32 vcc_lo, -1, v2
	v_cndmask_b32_e32 v2, 0, v2, vcc_lo
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v6, v3, v6
	v_cndmask_b32_e64 v3, 0, s7, vcc_lo
	v_cmp_ne_u32_e32 vcc_lo, -1, v1
	flat_store_b32 v[4:5], v6 dlc
	s_waitcnt_vscnt null, 0x0
	flat_load_b32 v2, v[2:3] glc dlc
	s_waitcnt vmcnt(0)
	v_cndmask_b32_e32 v1, 0, v1, vcc_lo
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v3, v2, v6
	v_cndmask_b32_e64 v2, 0, s7, vcc_lo
	flat_store_b32 v[4:5], v3 dlc
	s_waitcnt_vscnt null, 0x0
	flat_load_b32 v1, v[1:2] glc dlc
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_add_nc_u32_e32 v1, v1, v3
	flat_store_b32 v[4:5], v1 dlc
	s_waitcnt_vscnt null, 0x0
.LBB0_8:
	s_or_b32 exec_lo, exec_lo, s3
	s_mov_b32 s5, 0
	s_mov_b32 s2, exec_lo
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB0_10
; %bb.9:
	v_mov_b32_e32 v0, 0
	s_load_b64 s[0:1], s[0:1], 0x10
	s_lshl_b64 s[2:3], s[4:5], 2
	ds_load_b32 v1, v0
	s_waitcnt lgkmcnt(0)
	s_add_u32 s0, s0, s2
	s_addc_u32 s1, s1, s3
	global_store_b32 v0, v1, s[0:1]
.LBB0_10:
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z11reduxKernelPiiS_
		.amdhsa_group_segment_fixed_size 1024
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 24
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 7
		.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	_Z11reduxKernelPiiS_, .Lfunc_end0-_Z11reduxKernelPiiS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 708
; NumSgprs: 18
; NumVgprs: 7
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 1024 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 7
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
    .group_segment_fixed_size: 1024
    .kernarg_segment_align: 8
    .kernarg_segment_size: 24
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z11reduxKernelPiiS_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z11reduxKernelPiiS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     7
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
