#define LIMIT -999
#define BLOCK_SIZE 16
#define MAX_SEQ_LEN 2100
#define MAX_SEQ_NUM 1024
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <cuda.h>
#include <sys/time.h>

inline void cudaCheckError(int line, cudaError_t ce)
{
    if (ce != cudaSuccess) {
        printf("Error: line %d %s\n", line, cudaGetErrorString(ce));
        exit(1);
    }
}

// HACK Huan's hack
// this is not the updated validation code
int validation(int *score_matrix_cpu, int *score_matrix, unsigned int length)
{
    unsigned int i = 0;
    while (i!=length) {
        if ( (score_matrix_cpu[i]) == (score_matrix[i] >> 2) ) {
            ++i;
            continue;
        }
        else {
            printf("i = %d, expected %d, got %d.\n",i, score_matrix_cpu[i], score_matrix[i] >> 2);
            return 0;
        }
    }
    return 1;
}

////////////////////////////////////////////////////////////////////////////////
// declaration, forward
void runTest( int argc, char** argv);

double gettime() {
    struct timeval t;
    gettimeofday(&t,NULL);
    return t.tv_sec+t.tv_usec*1e-6;
}

__global__ void dummy_function(int * array, unsigned int howlarge)
{
		int tid = blockIdx.x*blockDim.x + threadIdx.x;
		for (int i = 0; i < 9; i++) {
			for (int delta=0; delta<howlarge; delta=delta+blockDim.x*gridDim.x) {
				if (tid+delta < howlarge)
		        		array[tid+delta] = array[tid+delta] + tid;	
			}
		}
}


void runTest()
{
    double start, end, now;
    unsigned int nints = 100 * 1024 * 1024;
    unsigned int sz = nints * sizeof(int);

		unsigned int nints_small = 1 * 1024 * 1024;
		unsigned int sz_small = nints_small * sizeof(int);

		#ifdef _LP64
		printf ("Running on a 64-bit platform!\n", 0);
		#else
		#endif


		printf("Big Chunk of memory allocated on host & device = %d\n", sz / 1024 /1024);
		printf("Small Chunk of memory allocated on host & device = %d\n", sz_small / 1024 /1024);
		
    int * dummy_cpu, * dummy_cpu2, * dummy_small_cpu, * dummy_small_cpu2;
    cudaMallocHost( (void**) &dummy_cpu, sz );
    cudaMallocHost( (void**) &dummy_cpu2, sz );
	cudaMallocHost ( (void**) &dummy_small_cpu, sz_small);
	cudaMallocHost ( (void**) &dummy_small_cpu2, sz_small);


		int * dummy_gpu, * dummy_gpu2, * dummy_small_gpu, * dummy_small_gpu2;
		cudaMalloc( (void**) &dummy_gpu, sz );
		cudaMalloc( (void**) &dummy_gpu2, sz );
		cudaMalloc( (void**) &dummy_small_gpu, sz_small );
		cudaMalloc( (void**) &dummy_small_gpu2, sz_small );

		double kernelt = 0, memcpyt = 0, st = 0, ast = 0;


#define TIMES 5	
    start = gettime();
    dummy_function<<<100,512>>>(dummy_gpu, nints);
    cudaDeviceSynchronize();
    end = gettime();
    printf("time for kernel call = %f\n", end-start);
	
    start = gettime();
    cudaMemcpy(dummy_cpu, dummy_gpu, sz, cudaMemcpyDeviceToHost );
    cudaDeviceSynchronize();
    end = gettime();
    printf("time for memcopy D-H = %f\n", end-start);

    start = gettime();
    cudaMemcpy(dummy_small_gpu, dummy_small_cpu, sz_small, cudaMemcpyHostToDevice);
    dummy_function<<<100,512>>>(dummy_gpu, nints);
    cudaMemcpy(dummy_cpu, dummy_gpu, sz, cudaMemcpyDeviceToHost );
    cudaDeviceSynchronize();
    end = gettime();
    printf("time for one iteration = %f\n", end-start);

    cudaStream_t stream1;
    cudaStreamCreate(&stream1);
    cudaStream_t stream2;
    cudaStreamCreate(&stream2);

#define DEBUG 1
	
    for (int sync=0; sync<2; sync++){

        start = gettime();
	for (int i = 0; i< TIMES; i++) {
	        // small sync copy H->D	
		cudaMemcpyAsync(dummy_small_gpu, dummy_small_cpu, sz_small, cudaMemcpyHostToDevice, stream1);
                //kernel function
	        dummy_function<<<100,512, 0, stream1>>>(dummy_gpu, nints);
    	        cudaDeviceSynchronize();
                //large copy D->H can be sync or async
#ifdef DEBUG		
                now = gettime();
#endif
		if (sync){
		    cudaMemcpy(dummy_cpu, dummy_gpu, sz, cudaMemcpyDeviceToHost);
                }else{
		    cudaMemcpyAsync(dummy_cpu, dummy_gpu, sz, cudaMemcpyDeviceToHost, stream1 );
                }
#ifdef DEBUG		
		printf("(A)sync call took %f\n", gettime() - now);
#endif
		// small sync copy H->D 
                cudaMemcpyAsync(dummy_small_gpu2, dummy_small_cpu2, sz_small, cudaMemcpyHostToDevice, stream2);
                //kernel function
                dummy_function<<<100,512, 0, stream2>>>(dummy_gpu2, nints);
                cudaDeviceSynchronize();
                //large copy D->H can be sync or async
                if (sync){
                    cudaMemcpy(dummy_cpu2, dummy_gpu2, sz, cudaMemcpyDeviceToHost);
                }else{
                    cudaMemcpyAsync(dummy_cpu2, dummy_gpu2, sz, cudaMemcpyDeviceToHost , stream2);
                }

	}
    	cudaDeviceSynchronize();
        end = gettime();
	if (!sync)
		printf("%d iterations: time for ASYNC calls = %f\n",TIMES,end-start);
	else
		printf("%d iterations: time for SYNC calls = %f\n",TIMES,end-start);

   }
}

////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main( int argc, char** argv)
{
    runTest();
    return EXIT_SUCCESS;
}


	.file	"tmpxft_002990b2_00000000-6_dual_buffering.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	"i = %d, expected %d, got %d.\n"
	.text
	.globl	_Z10validationPiS_j
	.type	_Z10validationPiS_j, @function
_Z10validationPiS_j:
.LFB2028:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L3:
	movl	%eax, %r9d
	cmpl	%eax, %edx
	je	.L12
	movl	(%rsi,%rax,4), %r8d
	movl	(%rdi,%rax,4), %ecx
	incq	%rax
	sarl	$2, %r8d
	cmpl	%r8d, %ecx
	je	.L3
	pushq	%rax
	.cfi_def_cfa_offset 16
	movl	%r9d, %edx
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
.L12:
	movl	$1, %eax
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z10validationPiS_j, .-_Z10validationPiS_j
	.globl	_Z7gettimev
	.type	_Z7gettimev, @function
_Z7gettimev:
.LFB2029:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	xorl	%esi, %esi
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	16(%rsp), %xmm0
	mulsd	.LC1(%rip), %xmm0
	cvtsi2sdq	8(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L14
	call	__stack_chk_fail@PLT
.L14:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z7gettimev, .-_Z7gettimev
	.globl	_Z35__device_stub__Z14dummy_functionPijPij
	.type	_Z35__device_stub__Z14dummy_functionPijPij, @function
_Z35__device_stub__Z14dummy_functionPijPij:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movl	%esi, 4(%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L16
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z14dummy_functionPij(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L16:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L18
	call	__stack_chk_fail@PLT
.L18:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z35__device_stub__Z14dummy_functionPijPij, .-_Z35__device_stub__Z14dummy_functionPijPij
	.globl	_Z14dummy_functionPij
	.type	_Z14dummy_functionPij, @function
_Z14dummy_functionPij:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z35__device_stub__Z14dummy_functionPijPij
	.cfi_endproc
.LFE2057:
	.size	_Z14dummy_functionPij, .-_Z14dummy_functionPij
	.section	.rodata.str1.1
.LC2:
	.string	"Running on a 64-bit platform!\n"
.LC3:
	.string	"Big Chunk of memory allocated on host & device = %d\n"
.LC4:
	.string	"Small Chunk of memory allocated on host & device = %d\n"
.LC5:
	.string	"time for kernel call = %f\n"
.LC6:
	.string	"time for memcopy D-H = %f\n"
.LC7:
	.string	"time for one iteration = %f\n"
.LC8:
	.string	"(A)sync call took %f\n"
.LC9:
	.string	"%d iterations: time for ASYNC calls = %f\n"
.LC10:
	.string	"%d iterations: time for SYNC calls = %f\n"
	.text
	.globl	_Z7runTestv
	.type	_Z7runTestv, @function
_Z7runTestv:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	xorl	%edx, %edx
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	addq	$-128, %rsp
	.cfi_def_cfa_offset 176
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$400, %edx
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	$4, %edx
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	16(%rsp), %rdi
	movl	$419430400, %esi
	call	cudaMallocHost@PLT
	leaq	24(%rsp), %rdi
	movl	$419430400, %esi
	call	cudaMallocHost@PLT
	leaq	32(%rsp), %rdi
	movl	$4194304, %esi
	call	cudaMallocHost@PLT
	leaq	40(%rsp), %rdi
	movl	$4194304, %esi
	call	cudaMallocHost@PLT
	leaq	48(%rsp), %rdi
	movl	$419430400, %esi
	call	cudaMalloc@PLT
	leaq	56(%rsp), %rdi
	movl	$419430400, %esi
	call	cudaMalloc@PLT
	leaq	64(%rsp), %rdi
	movl	$4194304, %esi
	call	cudaMalloc@PLT
	leaq	72(%rsp), %rdi
	movl	$4194304, %esi
	call	cudaMalloc@PLT
	call	_Z7gettimev
	movl	$8388609, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1073741849, %edi
	salq	$9, %rdx
	movl	$1, %ecx
	movl	$1, %esi
	salq	$2, %rdi
	movq	%rdx, 108(%rsp)
	movl	$1, 116(%rsp)
	movq	%rdi, 96(%rsp)
	movl	$1, 104(%rsp)
	movsd	%xmm0, (%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L22
	movq	48(%rsp), %rdi
	movl	$104857600, %esi
	call	_Z35__device_stub__Z14dummy_functionPijPij
.L22:
	call	cudaDeviceSynchronize@PLT
	call	_Z7gettimev
	subsd	(%rsp), %xmm0
	leaq	.LC5(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	call	_Z7gettimev
	movq	48(%rsp), %rsi
	movq	16(%rsp), %rdi
	movl	$2, %ecx
	movl	$419430400, %edx
	movsd	%xmm0, (%rsp)
	call	cudaMemcpy@PLT
	call	cudaDeviceSynchronize@PLT
	call	_Z7gettimev
	subsd	(%rsp), %xmm0
	leaq	.LC6(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	call	_Z7gettimev
	movq	32(%rsp), %rsi
	movq	64(%rsp), %rdi
	movl	$1, %ecx
	movl	$4194304, %edx
	movsd	%xmm0, (%rsp)
	call	cudaMemcpy@PLT
	movl	$8388609, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1073741849, %edi
	salq	$9, %rdx
	movl	$1, %ecx
	movl	$1, %esi
	salq	$2, %rdi
	movq	%rdx, 108(%rsp)
	movl	$1, 116(%rsp)
	movq	%rdi, 96(%rsp)
	movl	$1, 104(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L23
	movq	48(%rsp), %rdi
	movl	$104857600, %esi
	call	_Z35__device_stub__Z14dummy_functionPijPij
.L23:
	movq	48(%rsp), %rsi
	movq	16(%rsp), %rdi
	movl	$2, %ecx
	xorl	%ebx, %ebx
	movl	$419430400, %edx
	movl	$8388609, %ebp
	call	cudaMemcpy@PLT
	salq	$9, %rbp
	call	cudaDeviceSynchronize@PLT
	call	_Z7gettimev
	subsd	(%rsp), %xmm0
	leaq	.LC7(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	80(%rsp), %rdi
	call	cudaStreamCreate@PLT
	leaq	88(%rsp), %rdi
	call	cudaStreamCreate@PLT
.L32:
	call	_Z7gettimev
	movl	$5, %r13d
	movabsq	$4294967396, %r12
	leaq	.LC8(%rip), %r14
	movsd	%xmm0, (%rsp)
.L30:
	movq	80(%rsp), %r8
	movq	32(%rsp), %rsi
	movl	$1, %ecx
	movl	$4194304, %edx
	movq	64(%rsp), %rdi
	call	cudaMemcpyAsync@PLT
	movq	80(%rsp), %r9
	xorl	%r8d, %r8d
	movq	%rbp, %rdx
	movl	$1, %ecx
	movq	%r12, %rdi
	movl	$1, %esi
	movq	%rbp, 108(%rsp)
	movl	$1, 116(%rsp)
	movq	%r12, 96(%rsp)
	movl	$1, 104(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	movq	48(%rsp), %rdi
	movl	$104857600, %esi
	call	_Z35__device_stub__Z14dummy_functionPijPij
.L24:
	call	cudaDeviceSynchronize@PLT
	call	_Z7gettimev
	testl	%ebx, %ebx
	movq	48(%rsp), %rsi
	movq	16(%rsp), %rdi
	movsd	%xmm0, 8(%rsp)
	je	.L25
	movl	$2, %ecx
	movl	$419430400, %edx
	call	cudaMemcpy@PLT
	jmp	.L26
.L25:
	movq	80(%rsp), %r8
	movl	$2, %ecx
	movl	$419430400, %edx
	call	cudaMemcpyAsync@PLT
.L26:
	call	_Z7gettimev
	subsd	8(%rsp), %xmm0
	movq	%r14, %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	88(%rsp), %r8
	movq	40(%rsp), %rsi
	movl	$1, %ecx
	movq	72(%rsp), %rdi
	movl	$4194304, %edx
	call	cudaMemcpyAsync@PLT
	movq	88(%rsp), %r9
	xorl	%r8d, %r8d
	movq	%rbp, %rdx
	movl	$1, %ecx
	movq	%r12, %rdi
	movl	$1, %esi
	movq	%rbp, 108(%rsp)
	movl	$1, 116(%rsp)
	movq	%r12, 96(%rsp)
	movl	$1, 104(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L27
	movq	56(%rsp), %rdi
	movl	$104857600, %esi
	call	_Z35__device_stub__Z14dummy_functionPijPij
.L27:
	call	cudaDeviceSynchronize@PLT
	testl	%ebx, %ebx
	movq	56(%rsp), %rsi
	movq	24(%rsp), %rdi
	je	.L28
	movl	$2, %ecx
	movl	$419430400, %edx
	call	cudaMemcpy@PLT
	jmp	.L29
.L28:
	movq	88(%rsp), %r8
	movl	$2, %ecx
	movl	$419430400, %edx
	call	cudaMemcpyAsync@PLT
.L29:
	decl	%r13d
	jne	.L30
	call	cudaDeviceSynchronize@PLT
	call	_Z7gettimev
	testl	%ebx, %ebx
	subsd	(%rsp), %xmm0
	movl	$5, %edx
	jne	.L31
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movl	$1, %ebx
	call	__printf_chk@PLT
	jmp	.L32
.L31:
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L33
	call	__stack_chk_fail@PLT
.L33:
	subq	$-128, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z7runTestv, .-_Z7runTestv
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	_Z7runTestv
	xorl	%eax, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	main, .-main
	.section	.rodata.str1.1
.LC11:
	.string	"_Z14dummy_functionPij"
	.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	.LC11(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z14dummy_functionPij(%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
.LC1:
	.long	-1598689907
	.long	1051772663
	.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:

#define LIMIT -999
#define BLOCK_SIZE 16
#define MAX_SEQ_LEN 2100
#define MAX_SEQ_NUM 1024
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <hip/hip_runtime.h>
#include <sys/time.h>

inline void cudaCheckError(int line, hipError_t ce)
{
    if (ce != hipSuccess) {
        printf("Error: line %d %s\n", line, hipGetErrorString(ce));
        exit(1);
    }
}

// HACK Huan's hack
// this is not the updated validation code
int validation(int *score_matrix_cpu, int *score_matrix, unsigned int length)
{
    unsigned int i = 0;
    while (i!=length) {
        if ( (score_matrix_cpu[i]) == (score_matrix[i] >> 2) ) {
            ++i;
            continue;
        }
        else {
            printf("i = %d, expected %d, got %d.\n",i, score_matrix_cpu[i], score_matrix[i] >> 2);
            return 0;
        }
    }
    return 1;
}

////////////////////////////////////////////////////////////////////////////////
// declaration, forward
void runTest( int argc, char** argv);

double gettime() {
    struct timeval t;
    gettimeofday(&t,NULL);
    return t.tv_sec+t.tv_usec*1e-6;
}

__global__ void dummy_function(int * array, unsigned int howlarge)
{
		int tid = blockIdx.x*blockDim.x + threadIdx.x;
		for (int i = 0; i < 9; i++) {
			for (int delta=0; delta<howlarge; delta=delta+blockDim.x*gridDim.x) {
				if (tid+delta < howlarge)
		        		array[tid+delta] = array[tid+delta] + tid;	
			}
		}
}


void runTest()
{
    double start, end, now;
    unsigned int nints = 100 * 1024 * 1024;
    unsigned int sz = nints * sizeof(int);

		unsigned int nints_small = 1 * 1024 * 1024;
		unsigned int sz_small = nints_small * sizeof(int);

		#ifdef _LP64
		printf ("Running on a 64-bit platform!\n", 0);
		#else
		#endif


		printf("Big Chunk of memory allocated on host & device = %d\n", sz / 1024 /1024);
		printf("Small Chunk of memory allocated on host & device = %d\n", sz_small / 1024 /1024);
		
    int * dummy_cpu, * dummy_cpu2, * dummy_small_cpu, * dummy_small_cpu2;
    hipHostMalloc( (void**) &dummy_cpu, sz , hipHostMallocDefault);
    hipHostMalloc( (void**) &dummy_cpu2, sz , hipHostMallocDefault);
	hipHostMalloc ( (void**) &dummy_small_cpu, sz_small, hipHostMallocDefault);
	hipHostMalloc ( (void**) &dummy_small_cpu2, sz_small, hipHostMallocDefault);


		int * dummy_gpu, * dummy_gpu2, * dummy_small_gpu, * dummy_small_gpu2;
		hipMalloc( (void**) &dummy_gpu, sz );
		hipMalloc( (void**) &dummy_gpu2, sz );
		hipMalloc( (void**) &dummy_small_gpu, sz_small );
		hipMalloc( (void**) &dummy_small_gpu2, sz_small );

		double kernelt = 0, memcpyt = 0, st = 0, ast = 0;


#define TIMES 5	
    start = gettime();
    dummy_function<<<100,512>>>(dummy_gpu, nints);
    hipDeviceSynchronize();
    end = gettime();
    printf("time for kernel call = %f\n", end-start);
	
    start = gettime();
    hipMemcpy(dummy_cpu, dummy_gpu, sz, hipMemcpyDeviceToHost );
    hipDeviceSynchronize();
    end = gettime();
    printf("time for memcopy D-H = %f\n", end-start);

    start = gettime();
    hipMemcpy(dummy_small_gpu, dummy_small_cpu, sz_small, hipMemcpyHostToDevice);
    dummy_function<<<100,512>>>(dummy_gpu, nints);
    hipMemcpy(dummy_cpu, dummy_gpu, sz, hipMemcpyDeviceToHost );
    hipDeviceSynchronize();
    end = gettime();
    printf("time for one iteration = %f\n", end-start);

    hipStream_t stream1;
    hipStreamCreate(&stream1);
    hipStream_t stream2;
    hipStreamCreate(&stream2);

#define DEBUG 1
	
    for (int sync=0; sync<2; sync++){

        start = gettime();
	for (int i = 0; i< TIMES; i++) {
	        // small sync copy H->D	
		hipMemcpyAsync(dummy_small_gpu, dummy_small_cpu, sz_small, hipMemcpyHostToDevice, stream1);
                //kernel function
	        dummy_function<<<100,512, 0, stream1>>>(dummy_gpu, nints);
    	        hipDeviceSynchronize();
                //large copy D->H can be sync or async
#ifdef DEBUG		
                now = gettime();
#endif
		if (sync){
		    hipMemcpy(dummy_cpu, dummy_gpu, sz, hipMemcpyDeviceToHost);
                }else{
		    hipMemcpyAsync(dummy_cpu, dummy_gpu, sz, hipMemcpyDeviceToHost, stream1 );
                }
#ifdef DEBUG		
		printf("(A)sync call took %f\n", gettime() - now);
#endif
		// small sync copy H->D 
                hipMemcpyAsync(dummy_small_gpu2, dummy_small_cpu2, sz_small, hipMemcpyHostToDevice, stream2);
                //kernel function
                dummy_function<<<100,512, 0, stream2>>>(dummy_gpu2, nints);
                hipDeviceSynchronize();
                //large copy D->H can be sync or async
                if (sync){
                    hipMemcpy(dummy_cpu2, dummy_gpu2, sz, hipMemcpyDeviceToHost);
                }else{
                    hipMemcpyAsync(dummy_cpu2, dummy_gpu2, sz, hipMemcpyDeviceToHost , stream2);
                }

	}
    	hipDeviceSynchronize();
        end = gettime();
	if (!sync)
		printf("%d iterations: time for ASYNC calls = %f\n",TIMES,end-start);
	else
		printf("%d iterations: time for SYNC calls = %f\n",TIMES,end-start);

   }
}

////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main( int argc, char** argv)
{
    runTest();
    return EXIT_SUCCESS;
}


	.text
	.file	"dual_buffering.hip"
	.globl	_Z10validationPiS_j             # -- Begin function _Z10validationPiS_j
	.type	_Z10validationPiS_j,@function
_Z10validationPiS_j:                    # @_Z10validationPiS_j
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	$1, %ebx
	testl	%edx, %edx
	je	.LBB0_5
# %bb.1:                                # %.lr.ph.preheader
	movl	%edx, %r8d
	xorl	%eax, %eax
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rdi,%rax,4), %edx
	movl	(%rsi,%rax,4), %ecx
	sarl	$2, %ecx
	cmpl	%ecx, %edx
	jne	.LBB0_4
# %bb.3:                                #   in Loop: Header=BB0_2 Depth=1
	incq	%rax
	cmpl	%eax, %r8d
	jne	.LBB0_2
	jmp	.LBB0_5
.LBB0_4:
	xorl	%ebx, %ebx
	movl	$.L.str, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
.LBB0_5:                                # %.loopexit
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z10validationPiS_j, .Lfunc_end0-_Z10validationPiS_j
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z7gettimev
.LCPI1_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	_Z7gettimev
	.type	_Z7gettimev,@function
_Z7gettimev:                            # @_Z7gettimev
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	movq	%rsp, %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	cvtsi2sdq	(%rbx), %xmm1
	cvtsi2sdq	8(%rbx), %xmm0
	mulsd	.LCPI1_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z7gettimev, .Lfunc_end1-_Z7gettimev
	.cfi_endproc
                                        # -- End function
	.globl	_Z29__device_stub__dummy_functionPij # -- Begin function _Z29__device_stub__dummy_functionPij
	.type	_Z29__device_stub__dummy_functionPij,@function
_Z29__device_stub__dummy_functionPij:   # @_Z29__device_stub__dummy_functionPij
	.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	$80, %rsp
	.cfi_def_cfa_offset 128
	.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)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%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	$_Z14dummy_functionPij, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	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_end2:
	.size	_Z29__device_stub__dummy_functionPij, .Lfunc_end2-_Z29__device_stub__dummy_functionPij
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z7runTestv
.LCPI3_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	_Z7runTestv
	.type	_Z7runTestv,@function
_Z7runTestv:                            # @_Z7runTestv
	.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	$120, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movabsq	$4294967396, %rbx               # imm = 0x100000064
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$.L.str.2, %edi
	movl	$400, %esi                      # imm = 0x190
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.3, %edi
	movl	$4, %esi
	xorl	%eax, %eax
	callq	printf
	leaq	56(%rsp), %rdi
	movl	$419430400, %esi                # imm = 0x19000000
	xorl	%edx, %edx
	callq	hipHostMalloc
	leaq	112(%rsp), %rdi
	movl	$419430400, %esi                # imm = 0x19000000
	xorl	%edx, %edx
	callq	hipHostMalloc
	leaq	80(%rsp), %rdi
	movl	$4194304, %esi                  # imm = 0x400000
	xorl	%edx, %edx
	callq	hipHostMalloc
	leaq	104(%rsp), %rdi
	movl	$4194304, %esi                  # imm = 0x400000
	xorl	%edx, %edx
	callq	hipHostMalloc
	leaq	8(%rsp), %rdi
	movl	$419430400, %esi                # imm = 0x19000000
	callq	hipMalloc
	leaq	72(%rsp), %rdi
	movl	$419430400, %esi                # imm = 0x19000000
	callq	hipMalloc
	leaq	64(%rsp), %rdi
	movl	$4194304, %esi                  # imm = 0x400000
	callq	hipMalloc
	leaq	96(%rsp), %rdi
	movl	$4194304, %esi                  # imm = 0x400000
	callq	hipMalloc
	leaq	16(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	cvtsi2sdq	(%r14), %xmm0
	cvtsi2sdq	8(%r14), %xmm1
	mulsd	.LCPI3_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, (%rsp)                   # 8-byte Spill
	leaq	412(%rbx), %r14
	movq	%rbx, %rdi
	movl	$1, %esi
	movq	%r14, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_2
# %bb.1:
	movq	8(%rsp), %rdi
	movl	$104857600, %esi                # imm = 0x6400000
	callq	_Z29__device_stub__dummy_functionPij
.LBB3_2:
	callq	hipDeviceSynchronize
	leaq	16(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm1, %xmm1
	cvtsi2sdq	(%r15), %xmm1
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%r15), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	(%rsp), %xmm0                   # 8-byte Folded Reload
	movb	$1, %bpl
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	leaq	16(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r15), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%r15), %xmm1
	mulsd	.LCPI3_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, (%rsp)                   # 8-byte Spill
	movq	56(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	$419430400, %edx                # imm = 0x19000000
	movl	$2, %ecx
	callq	hipMemcpy
	callq	hipDeviceSynchronize
	leaq	16(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm1, %xmm1
	cvtsi2sdq	(%r15), %xmm1
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%r15), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	(%rsp), %xmm0                   # 8-byte Folded Reload
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	leaq	16(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r15), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%r15), %xmm1
	mulsd	.LCPI3_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, (%rsp)                   # 8-byte Spill
	movq	64(%rsp), %rdi
	movq	80(%rsp), %rsi
	movl	$4194304, %edx                  # imm = 0x400000
	movl	$1, %ecx
	callq	hipMemcpy
	movq	%rbx, %rdi
	movl	$1, %esi
	movq	%r14, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_4
# %bb.3:
	movq	8(%rsp), %rdi
	movl	$104857600, %esi                # imm = 0x6400000
	callq	_Z29__device_stub__dummy_functionPij
.LBB3_4:
	movq	56(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	$419430400, %edx                # imm = 0x19000000
	movl	$2, %ecx
	callq	hipMemcpy
	callq	hipDeviceSynchronize
	leaq	16(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm1, %xmm1
	cvtsi2sdq	(%r15), %xmm1
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%r15), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	(%rsp), %xmm0                   # 8-byte Folded Reload
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	leaq	48(%rsp), %rdi
	callq	hipStreamCreate
	leaq	40(%rsp), %rdi
	callq	hipStreamCreate
.LBB3_5:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_6 Depth 2
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	16(%rsp), %r12
	xorps	%xmm0, %xmm0
	cvtsi2sdq	24(%rsp), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	movsd	%xmm0, 32(%rsp)                 # 8-byte Spill
	movl	$5, %r13d
.LBB3_6:                                #   Parent Loop BB3_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	64(%rsp), %rdi
	movq	80(%rsp), %rsi
	movq	48(%rsp), %r8
	movl	$4194304, %edx                  # imm = 0x400000
	movl	$1, %ecx
	callq	hipMemcpyAsync
	movq	48(%rsp), %r9
	movq	%rbx, %rdi
	movl	$1, %esi
	movq	%r14, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_8
# %bb.7:                                #   in Loop: Header=BB3_6 Depth=2
	movq	8(%rsp), %rdi
	movl	$104857600, %esi                # imm = 0x6400000
	callq	_Z29__device_stub__dummy_functionPij
.LBB3_8:                                #   in Loop: Header=BB3_6 Depth=2
	callq	hipDeviceSynchronize
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	16(%rsp), %xmm0
	movsd	%xmm0, 88(%rsp)                 # 8-byte Spill
	xorps	%xmm0, %xmm0
	cvtsi2sdq	24(%rsp), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	movq	56(%rsp), %rdi
	movq	8(%rsp), %rsi
	testb	$1, %bpl
	je	.LBB3_9
# %bb.12:                               #   in Loop: Header=BB3_6 Depth=2
	movq	48(%rsp), %r8
	movl	$419430400, %edx                # imm = 0x19000000
	movl	$2, %ecx
	callq	hipMemcpyAsync
	jmp	.LBB3_13
.LBB3_9:                                #   in Loop: Header=BB3_6 Depth=2
	movl	$419430400, %edx                # imm = 0x19000000
	movl	$2, %ecx
	callq	hipMemcpy
.LBB3_13:                               #   in Loop: Header=BB3_6 Depth=2
	movsd	(%rsp), %xmm0                   # 8-byte Reload
                                        # xmm0 = mem[0],zero
	addsd	88(%rsp), %xmm0                 # 8-byte Folded Reload
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm1, %xmm1
	cvtsi2sdq	16(%rsp), %xmm1
	xorps	%xmm0, %xmm0
	cvtsi2sdq	24(%rsp), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	(%rsp), %xmm0                   # 8-byte Folded Reload
	movl	$.L.str.7, %edi
	movb	$1, %al
	callq	printf
	movq	96(%rsp), %rdi
	movq	104(%rsp), %rsi
	movq	40(%rsp), %r8
	movl	$4194304, %edx                  # imm = 0x400000
	movl	$1, %ecx
	callq	hipMemcpyAsync
	movq	40(%rsp), %r9
	movq	%rbx, %rdi
	movl	$1, %esi
	movq	%r14, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_15
# %bb.14:                               #   in Loop: Header=BB3_6 Depth=2
	movq	72(%rsp), %rdi
	movl	$104857600, %esi                # imm = 0x6400000
	callq	_Z29__device_stub__dummy_functionPij
.LBB3_15:                               #   in Loop: Header=BB3_6 Depth=2
	callq	hipDeviceSynchronize
	movq	112(%rsp), %rdi
	movq	72(%rsp), %rsi
	testb	$1, %bpl
	je	.LBB3_16
# %bb.17:                               #   in Loop: Header=BB3_6 Depth=2
	movq	40(%rsp), %r8
	movl	$419430400, %edx                # imm = 0x19000000
	movl	$2, %ecx
	callq	hipMemcpyAsync
	jmp	.LBB3_18
.LBB3_16:                               #   in Loop: Header=BB3_6 Depth=2
	movl	$419430400, %edx                # imm = 0x19000000
	movl	$2, %ecx
	callq	hipMemcpy
.LBB3_18:                               #   in Loop: Header=BB3_6 Depth=2
	decl	%r13d
	jne	.LBB3_6
# %bb.10:                               #   in Loop: Header=BB3_5 Depth=1
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r12, %xmm0
	movsd	32(%rsp), %xmm1                 # 8-byte Reload
                                        # xmm1 = mem[0],zero
	addsd	%xmm0, %xmm1
	movsd	%xmm1, 32(%rsp)                 # 8-byte Spill
	callq	hipDeviceSynchronize
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm1, %xmm1
	cvtsi2sdq	16(%rsp), %xmm1
	xorps	%xmm0, %xmm0
	cvtsi2sdq	24(%rsp), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	32(%rsp), %xmm0                 # 8-byte Folded Reload
	testb	$1, %bpl
	je	.LBB3_11
# %bb.19:                               #   in Loop: Header=BB3_5 Depth=1
	movl	$.L.str.8, %edi
	movl	$5, %esi
	movb	$1, %al
	callq	printf
	xorl	%ebp, %ebp
	jmp	.LBB3_5
.LBB3_11:
	movl	$.L.str.9, %edi
	movl	$5, %esi
	movb	$1, %al
	callq	printf
	addq	$120, %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	_Z7runTestv, .Lfunc_end3-_Z7runTestv
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	_Z7runTestv
	xorl	%eax, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	main, .Lfunc_end4-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	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z14dummy_functionPij, %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_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__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	.LBB6_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
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"i = %d, expected %d, got %d.\n"
	.size	.L.str, 30

	.type	_Z14dummy_functionPij,@object   # @_Z14dummy_functionPij
	.section	.rodata,"a",@progbits
	.globl	_Z14dummy_functionPij
	.p2align	3, 0x0
_Z14dummy_functionPij:
	.quad	_Z29__device_stub__dummy_functionPij
	.size	_Z14dummy_functionPij, 8

	.type	.L.str.2,@object                # @.str.2
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.2:
	.asciz	"Big Chunk of memory allocated on host & device = %d\n"
	.size	.L.str.2, 53

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Small Chunk of memory allocated on host & device = %d\n"
	.size	.L.str.3, 55

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"time for kernel call = %f\n"
	.size	.L.str.4, 27

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"time for memcopy D-H = %f\n"
	.size	.L.str.5, 27

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"time for one iteration = %f\n"
	.size	.L.str.6, 29

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"(A)sync call took %f\n"
	.size	.L.str.7, 22

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"%d iterations: time for ASYNC calls = %f\n"
	.size	.L.str.8, 42

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"%d iterations: time for SYNC calls = %f\n"
	.size	.L.str.9, 41

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z14dummy_functionPij"
	.size	.L__unnamed_1, 22

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Running on a 64-bit platform!"
	.size	.Lstr, 30

	.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 _Z29__device_stub__dummy_functionPij
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z14dummy_functionPij
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z14dummy_functionPij   ; -- Begin function _Z14dummy_functionPij
	.globl	_Z14dummy_functionPij
	.p2align	8
	.type	_Z14dummy_functionPij,@function
_Z14dummy_functionPij:                  ; @_Z14dummy_functionPij
; %bb.0:
	s_clause 0x3
	s_load_b32 s3, s[0:1], 0x1c
	s_load_b32 s2, s[0:1], 0x8
	s_load_b32 s5, s[0:1], 0x10
	s_load_b64 s[0:1], s[0:1], 0x0
	s_mov_b32 s4, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s6, s3, 0xffff
	s_cmp_lg_u32 s2, 0
	v_mad_u64_u32 v[1:2], null, s15, s6, v[0:1]
	s_cselect_b32 s3, -1, 0
	s_mul_i32 s5, s5, s6
.LBB0_1:                                ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_3 Depth 2
	s_and_not1_b32 vcc_lo, exec_lo, s3
	s_cbranch_vccnz .LBB0_6
; %bb.2:                                ; %.lr.ph.preheader
                                        ;   in Loop: Header=BB0_1 Depth=1
	s_mov_b32 s6, 0
.LBB0_3:                                ; %.lr.ph
                                        ;   Parent Loop BB0_1 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_add_nc_u32_e32 v2, s6, v1
	s_mov_b32 s7, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_u32_e64 s2, v2
	s_cbranch_execz .LBB0_5
; %bb.4:                                ;   in Loop: Header=BB0_3 Depth=2
	v_ashrrev_i32_e32 v3, 31, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[2:3]
	v_add_co_u32 v2, vcc_lo, s0, v2
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v3, vcc_lo
	global_load_b32 v0, v[2:3], off
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v0, v0, v1
	global_store_b32 v[2:3], v0, off
.LBB0_5:                                ;   in Loop: Header=BB0_3 Depth=2
	s_or_b32 exec_lo, exec_lo, s7
	s_add_i32 s6, s6, s5
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_ge_u32 s6, s2
	s_cbranch_scc0 .LBB0_3
.LBB0_6:                                ; %._crit_edge
                                        ;   in Loop: Header=BB0_1 Depth=1
	s_add_i32 s4, s4, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s4, 9
	s_cbranch_scc0 .LBB0_1
; %bb.7:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z14dummy_functionPij
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 4
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z14dummy_functionPij, .Lfunc_end0-_Z14dummy_functionPij
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 216
; NumSgprs: 18
; NumVgprs: 4
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 4
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .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:           _Z14dummy_functionPij
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z14dummy_functionPij.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     4
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
