#include <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <cuda_runtime.h>

int N = 1024;		// length of vector A
float* d_A = NULL;	// Pointer to vector A in device memory

double time_memcpy = 0;
double time_compute = 0;	// Timers

/////////////////////////////////////////////////////////////////////////
// Matrix-matrix Multiplication on CPU: Policy 1
/////////////////////////////////////////////////////////////////////////
void inc_cpu(float* A, int n, int inc_count) {
	for (int i=0; i<n; i++) {
		for (int j=0; j<inc_count; j++) {
			A[i] += 1;
		}
	}	
}

/////////////////////////////////////////////////////////////////////////
// Matrix-matrix Multiplication on GPU: Policy 2
/////////////////////////////////////////////////////////////////////////
__global__ void inc_gpu(float* A, int n, int inc_count)
{
	int i = blockIdx.x * blockDim.x + threadIdx.x;
	for (int j=0; j<inc_count; j++) {
		A[i] += 1;
	}
}

/////////////////////////////////////////////////////////////////////////
// Macro to check that CUDA API calls are successful
/////////////////////////////////////////////////////////////////////////
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
   if (code != cudaSuccess) 
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

/////////////////////////////////////////////////////////////////////////
// Allocate device memory and copy from host to device memory
/////////////////////////////////////////////////////////////////////////
void copy_host_to_device(float* A, int n)
{
	double time_start, time_end;
	struct timeval tv;
	struct timezone tz;

	gettimeofday (&tv ,   &tz);
	time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

	cudaMalloc((void**) &d_A, sizeof(float) * n * n);
	assert(d_A);
	cudaMemcpy(d_A, A, sizeof(float) * n, cudaMemcpyHostToDevice);

	gettimeofday (&tv ,   &tz);
	time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

	time_memcpy += time_end - time_start;
}

/////////////////////////////////////////////////////////////////////////
// Copy result from device to host memory and free device memory
/////////////////////////////////////////////////////////////////////////
void copy_device_to_host(float* A, int n)
{
	double time_start, time_end;
	struct timeval tv;
	struct timezone tz;

	gettimeofday (&tv ,   &tz);
	time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

	// TODO:
	// 1. Copy result array d_C to C using cudaMemcpy.
	// 2. Free memory allocated for d_A, d_B, d_C using cudaFree.

	cudaMemcpy(A, d_A, sizeof(float) * n, cudaMemcpyDeviceToHost);
	cudaFree(d_A);

	gettimeofday (&tv ,   &tz);
	time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

	time_memcpy += time_end - time_start;
}

/////////////////////////////////////////////////////////////////////////
// Program main
/////////////////////////////////////////////////////////////////////////

int main(int argc, char** argv)
{
	double time_start, time_end;
	struct timeval tv;
	struct timezone tz;

	if(argc != 5) {printf("Usage: mat_mul_gpu <array size> <block size> <policy (1|2|3)> <(debug|nodebug)>\n") ; exit(2) ;}
	int inc_count = atoi(argv[1]) ;		// number of times to increment each element
	int block_size = atoi(argv[2]) ;	// size of thread block
	int policy = atoi(argv[3]) ;		// 1 or 2
	bool debug = (!strcmp(argv[4], "debug")) ? true : false;

	// allocate host memory for vector A
	float* h_A = (float*)malloc(sizeof(float) * N);

	// initialize host memory with zeros
	for (int i = 0; i < N; ++i){
		h_A[i] = 0;
	}

	//call the relevant policy 
	if(policy==1){ // CPU baseline
		gettimeofday (&tv ,   &tz);
		time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

		inc_cpu(h_A, N, inc_count);

		gettimeofday (&tv ,   &tz);
		time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
		time_compute = time_end - time_start;
	}
	else if(policy==2){ // GPU
		copy_host_to_device(h_A, N);

		gettimeofday (&tv ,   &tz);
		time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

                dim3 dimBlock(block_size);
                dim3 dimGrid(N / dimBlock.x);
		inc_gpu <<<dimGrid, dimBlock>>> (d_A, N, inc_count);

		gpuErrchk( cudaPeekAtLastError() );
		gpuErrchk( cudaDeviceSynchronize() );

		gettimeofday (&tv ,   &tz);
		time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
		time_compute = time_end - time_start;

		copy_device_to_host(h_A, N);
	}

	if (debug) {
		// print selected results (for checking correctness)
		for (int i=0; i<N; i+=N/10) { printf(" %10.6f",h_A[i]); }
		printf("\n");
	}
	if(!debug) {
		printf("Memcpy Time: %f seconds\n", time_memcpy);
		printf("Compute Time: %f seconds\n", time_compute);
	}

	// clean up memory allocated  
	free(h_A);

	cudaDeviceReset();
}

	.file	"tmpxft_0032a0dc_00000000-6_vec_inc_gpu.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3896:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3896:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z7inc_cpuPfii
	.type	_Z7inc_cpuPfii, @function
_Z7inc_cpuPfii:
.LFB3889:
	.cfi_startproc
	endbr64
	movss	.LC0(%rip), %xmm0
	xorl	%eax, %eax
.L3:
	cmpl	%eax, %esi
	jle	.L8
	xorl	%ecx, %ecx
.L5:
	cmpl	%edx, %ecx
	jge	.L9
	movss	(%rdi,%rax,4), %xmm1
	incl	%ecx
	addss	%xmm0, %xmm1
	movss	%xmm1, (%rdi,%rax,4)
	jmp	.L5
.L9:
	incq	%rax
	jmp	.L3
.L8:
	ret
	.cfi_endproc
.LFE3889:
	.size	_Z7inc_cpuPfii, .-_Z7inc_cpuPfii
	.section	.rodata._Z9gpuAssert9cudaErrorPKcib.str1.1,"aMS",@progbits,1
.LC1:
	.string	"GPUassert: %s %s %d\n"
	.section	.text._Z9gpuAssert9cudaErrorPKcib,"axG",@progbits,_Z9gpuAssert9cudaErrorPKcib,comdat
	.weak	_Z9gpuAssert9cudaErrorPKcib
	.type	_Z9gpuAssert9cudaErrorPKcib, @function
_Z9gpuAssert9cudaErrorPKcib:
.LFB3890:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L16
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	%edi, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	movl	%edx, 12(%rsp)
	movq	%rsi, (%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r9d
	movq	(%rsp), %r8
	leaq	.LC1(%rip), %rdx
	movq	stderr(%rip), %rdi
	movq	%rax, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	testb	%bpl, %bpl
	je	.L10
	movl	%ebx, %edi
	call	exit@PLT
.L10:
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
.L16:
	.cfi_restore 3
	.cfi_restore 6
	ret
	.cfi_endproc
.LFE3890:
	.size	_Z9gpuAssert9cudaErrorPKcib, .-_Z9gpuAssert9cudaErrorPKcib
	.text
	.globl	_Z19copy_host_to_devicePfi
	.type	_Z19copy_host_to_devicePfi, @function
_Z19copy_host_to_devicePfi:
.LFB3891:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movslq	%esi, %rbx
	subq	$56, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	leaq	16(%rsp), %r13
	leaq	24(%rsp), %r12
	movq	%r13, %rsi
	movq	%r12, %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	24(%rsp), %xmm1
	movq	%rbx, %rsi
	leaq	d_A(%rip), %rdi
	imulq	%rbx, %rsi
	cvtsi2sdq	32(%rsp), %xmm0
	divsd	.LC2(%rip), %xmm0
	addsd	%xmm1, %xmm0
	salq	$2, %rsi
	movsd	%xmm0, 8(%rsp)
	call	cudaMalloc@PLT
	movq	d_A(%rip), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	leaq	0(,%rbx,4), %rdx
	call	cudaMemcpy@PLT
	movq	%r13, %rsi
	movq	%r12, %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	24(%rsp), %xmm1
	cvtsi2sdq	32(%rsp), %xmm0
	divsd	.LC2(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	8(%rsp), %xmm0
	addsd	time_memcpy(%rip), %xmm0
	movsd	%xmm0, time_memcpy(%rip)
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L20
	call	__stack_chk_fail@PLT
.L20:
	addq	$56, %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
.LFE3891:
	.size	_Z19copy_host_to_devicePfi, .-_Z19copy_host_to_devicePfi
	.globl	_Z19copy_device_to_hostPfi
	.type	_Z19copy_device_to_hostPfi, @function
_Z19copy_device_to_hostPfi:
.LFB3892:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movl	%esi, %ebx
	subq	$56, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	leaq	16(%rsp), %r13
	leaq	24(%rsp), %r12
	movq	%r13, %rsi
	movq	%r12, %rdi
	call	gettimeofday@PLT
	movslq	%ebx, %rdx
	movl	$2, %ecx
	movq	%rbp, %rdi
	cvtsi2sdq	24(%rsp), %xmm1
	movq	d_A(%rip), %rsi
	salq	$2, %rdx
	cvtsi2sdq	32(%rsp), %xmm0
	divsd	.LC2(%rip), %xmm0
	addsd	%xmm1, %xmm0
	movsd	%xmm0, 8(%rsp)
	call	cudaMemcpy@PLT
	movq	d_A(%rip), %rdi
	call	cudaFree@PLT
	movq	%r13, %rsi
	movq	%r12, %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	24(%rsp), %xmm1
	cvtsi2sdq	32(%rsp), %xmm0
	divsd	.LC2(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	8(%rsp), %xmm0
	addsd	time_memcpy(%rip), %xmm0
	movsd	%xmm0, time_memcpy(%rip)
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	addq	$56, %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
.LFE3892:
	.size	_Z19copy_device_to_hostPfi, .-_Z19copy_device_to_hostPfi
	.globl	_Z28__device_stub__Z7inc_gpuPfiiPfii
	.type	_Z28__device_stub__Z7inc_gpuPfiiPfii, @function
_Z28__device_stub__Z7inc_gpuPfiiPfii:
.LFB3918:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	32(%rsp), %rdi
	movl	%esi, 4(%rsp)
	leaq	44(%rsp), %rsi
	movl	%edx, (%rsp)
	leaq	16(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L25
	pushq	24(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z7inc_gpuPfii(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 144
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L25:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3918:
	.size	_Z28__device_stub__Z7inc_gpuPfiiPfii, .-_Z28__device_stub__Z7inc_gpuPfiiPfii
	.globl	_Z7inc_gpuPfii
	.type	_Z7inc_gpuPfii, @function
_Z7inc_gpuPfii:
.LFB3919:
	.cfi_startproc
	endbr64
	jmp	_Z28__device_stub__Z7inc_gpuPfiiPfii
	.cfi_endproc
.LFE3919:
	.size	_Z7inc_gpuPfii, .-_Z7inc_gpuPfii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"Usage: mat_mul_gpu <array size> <block size> <policy (1|2|3)> <(debug|nodebug)>\n"
.LC4:
	.string	"debug"
.LC6:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/Jchuk99/CS1541_Fall2020/master/study_guides/midterm_2_review/vec_inc_gpu.cu"
.LC7:
	.string	"Memcpy Time: %f seconds\n"
.LC8:
	.string	"Compute Time: %f seconds\n"
.LC9:
	.string	" %10.6f"
.LC10:
	.string	"\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3893:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	cmpl	$5, %edi
	je	.L31
	movl	$2, %edi
	leaq	.LC3(%rip), %rsi
	call	__printf_chk@PLT
	movl	$2, %edi
	call	exit@PLT
.L31:
	movq	8(%rsi), %rdi
	movq	%rsi, %rbx
	call	atoi@PLT
	movq	16(%rbx), %rdi
	movl	%eax, %r13d
	call	atoi@PLT
	movq	24(%rbx), %rdi
	movl	%eax, %r12d
	call	atoi@PLT
	movq	32(%rbx), %rdi
	leaq	.LC4(%rip), %rsi
	movl	%eax, %r14d
	call	strcmp@PLT
	movslq	N(%rip), %rdi
	movl	%eax, %ebp
	movq	%rdi, %r15
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rax, %rbx
	xorl	%eax, %eax
.L32:
	cmpl	%eax, %r15d
	jle	.L44
	movl	$0x00000000, (%rbx,%rax,4)
	incq	%rax
	jmp	.L32
.L44:
	cmpl	$1, %r14d
	jne	.L34
	leaq	24(%rsp), %r14
	leaq	56(%rsp), %r12
	movq	%r14, %rsi
	movq	%r12, %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	56(%rsp), %xmm1
	movl	%r13d, %edx
	movq	%rbx, %rdi
	cvtsi2sdq	64(%rsp), %xmm0
	divsd	.LC2(%rip), %xmm0
	movl	N(%rip), %esi
	addsd	%xmm1, %xmm0
	movsd	%xmm0, 8(%rsp)
	call	_Z7inc_cpuPfii
	movq	%r14, %rsi
	movq	%r12, %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	56(%rsp), %xmm1
	cvtsi2sdq	64(%rsp), %xmm0
	divsd	.LC2(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	8(%rsp), %xmm0
	movsd	%xmm0, time_compute(%rip)
	jmp	.L35
.L34:
	cmpl	$2, %r14d
	jne	.L35
	movl	%r15d, %esi
	leaq	56(%rsp), %r14
	leaq	24(%rsp), %r15
	movq	%rbx, %rdi
	call	_Z19copy_host_to_devicePfi
	movq	%r15, %rsi
	movq	%r14, %rdi
	call	gettimeofday@PLT
	movl	N(%rip), %eax
	xorl	%edx, %edx
	xorl	%r9d, %r9d
	cvtsi2sdq	56(%rsp), %xmm1
	movl	%r12d, 32(%rsp)
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rcx
	divl	%r12d
	movq	%rcx, 48(%rsp)
	movl	52(%rsp), %esi
	movq	%rcx, 36(%rsp)
	movl	40(%rsp), %ecx
	movq	32(%rsp), %rdx
	cvtsi2sdq	64(%rsp), %xmm0
	divsd	.LC2(%rip), %xmm0
	addsd	%xmm1, %xmm0
	movsd	%xmm0, 8(%rsp)
	movl	%eax, 44(%rsp)
	movq	44(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L36
	movl	N(%rip), %esi
	movq	d_A(%rip), %rdi
	movl	%r13d, %edx
	call	_Z28__device_stub__Z7inc_gpuPfiiPfii
.L36:
	call	cudaPeekAtLastError@PLT
	movl	$1, %ecx
	movl	$144, %edx
	leaq	.LC6(%rip), %r12
	movl	%eax, %edi
	movq	%r12, %rsi
	call	_Z9gpuAssert9cudaErrorPKcib
	call	cudaDeviceSynchronize@PLT
	movl	$1, %ecx
	movl	$145, %edx
	movq	%r12, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
	movq	%r15, %rsi
	movq	%r14, %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	56(%rsp), %xmm1
	movl	N(%rip), %esi
	movq	%rbx, %rdi
	cvtsi2sdq	64(%rsp), %xmm0
	divsd	.LC2(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	8(%rsp), %xmm0
	movsd	%xmm0, time_compute(%rip)
	call	_Z19copy_device_to_hostPfi
.L35:
	leaq	.LC9(%rip), %r12
	testl	%ebp, %ebp
	je	.L37
	movsd	time_memcpy(%rip), %xmm0
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	movsd	time_compute(%rip), %xmm0
	leaq	.LC8(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L38
.L37:
	cmpl	%ebp, N(%rip)
	jle	.L45
	movslq	%ebp, %rax
	movq	%r12, %rsi
	movl	$2, %edi
	cvtss2sd	(%rbx,%rax,4), %xmm0
	movb	$1, %al
	call	__printf_chk@PLT
	movl	N(%rip), %eax
	movl	$10, %ecx
	cltd
	idivl	%ecx
	addl	%eax, %ebp
	jmp	.L37
.L45:
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L38:
	movq	%rbx, %rdi
	call	free@PLT
	call	cudaDeviceReset@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L40
	call	__stack_chk_fail@PLT
.L40:
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3893:
	.size	main, .-main
	.section	.rodata.str1.1
.LC11:
	.string	"_Z7inc_gpuPfii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3921:
	.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	_Z7inc_gpuPfii(%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
.LFE3921:
	.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
	.globl	time_compute
	.bss
	.align 8
	.type	time_compute, @object
	.size	time_compute, 8
time_compute:
	.zero	8
	.globl	time_memcpy
	.align 8
	.type	time_memcpy, @object
	.size	time_memcpy, 8
time_memcpy:
	.zero	8
	.globl	d_A
	.align 8
	.type	d_A, @object
	.size	d_A, 8
d_A:
	.zero	8
	.globl	N
	.data
	.align 4
	.type	N, @object
	.size	N, 4
N:
	.long	1024
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	1065353216
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC2:
	.long	0
	.long	1093567616
	.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 <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <hip/hip_runtime.h>

int N = 1024;		// length of vector A
float* d_A = NULL;	// Pointer to vector A in device memory

double time_memcpy = 0;
double time_compute = 0;	// Timers

/////////////////////////////////////////////////////////////////////////
// Matrix-matrix Multiplication on CPU: Policy 1
/////////////////////////////////////////////////////////////////////////
void inc_cpu(float* A, int n, int inc_count) {
	for (int i=0; i<n; i++) {
		for (int j=0; j<inc_count; j++) {
			A[i] += 1;
		}
	}	
}

/////////////////////////////////////////////////////////////////////////
// Matrix-matrix Multiplication on GPU: Policy 2
/////////////////////////////////////////////////////////////////////////
__global__ void inc_gpu(float* A, int n, int inc_count)
{
	int i = blockIdx.x * blockDim.x + threadIdx.x;
	for (int j=0; j<inc_count; j++) {
		A[i] += 1;
	}
}

/////////////////////////////////////////////////////////////////////////
// Macro to check that CUDA API calls are successful
/////////////////////////////////////////////////////////////////////////
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(hipError_t code, const char *file, int line, bool abort=true)
{
   if (code != hipSuccess) 
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", hipGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

/////////////////////////////////////////////////////////////////////////
// Allocate device memory and copy from host to device memory
/////////////////////////////////////////////////////////////////////////
void copy_host_to_device(float* A, int n)
{
	double time_start, time_end;
	struct timeval tv;
	struct timezone tz;

	gettimeofday (&tv ,   &tz);
	time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

	hipMalloc((void**) &d_A, sizeof(float) * n * n);
	assert(d_A);
	hipMemcpy(d_A, A, sizeof(float) * n, hipMemcpyHostToDevice);

	gettimeofday (&tv ,   &tz);
	time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

	time_memcpy += time_end - time_start;
}

/////////////////////////////////////////////////////////////////////////
// Copy result from device to host memory and free device memory
/////////////////////////////////////////////////////////////////////////
void copy_device_to_host(float* A, int n)
{
	double time_start, time_end;
	struct timeval tv;
	struct timezone tz;

	gettimeofday (&tv ,   &tz);
	time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

	// TODO:
	// 1. Copy result array d_C to C using cudaMemcpy.
	// 2. Free memory allocated for d_A, d_B, d_C using cudaFree.

	hipMemcpy(A, d_A, sizeof(float) * n, hipMemcpyDeviceToHost);
	hipFree(d_A);

	gettimeofday (&tv ,   &tz);
	time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

	time_memcpy += time_end - time_start;
}

/////////////////////////////////////////////////////////////////////////
// Program main
/////////////////////////////////////////////////////////////////////////

int main(int argc, char** argv)
{
	double time_start, time_end;
	struct timeval tv;
	struct timezone tz;

	if(argc != 5) {printf("Usage: mat_mul_gpu <array size> <block size> <policy (1|2|3)> <(debug|nodebug)>\n") ; exit(2) ;}
	int inc_count = atoi(argv[1]) ;		// number of times to increment each element
	int block_size = atoi(argv[2]) ;	// size of thread block
	int policy = atoi(argv[3]) ;		// 1 or 2
	bool debug = (!strcmp(argv[4], "debug")) ? true : false;

	// allocate host memory for vector A
	float* h_A = (float*)malloc(sizeof(float) * N);

	// initialize host memory with zeros
	for (int i = 0; i < N; ++i){
		h_A[i] = 0;
	}

	//call the relevant policy 
	if(policy==1){ // CPU baseline
		gettimeofday (&tv ,   &tz);
		time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

		inc_cpu(h_A, N, inc_count);

		gettimeofday (&tv ,   &tz);
		time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
		time_compute = time_end - time_start;
	}
	else if(policy==2){ // GPU
		copy_host_to_device(h_A, N);

		gettimeofday (&tv ,   &tz);
		time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;

                dim3 dimBlock(block_size);
                dim3 dimGrid(N / dimBlock.x);
		inc_gpu <<<dimGrid, dimBlock>>> (d_A, N, inc_count);

		gpuErrchk( hipPeekAtLastError() );
		gpuErrchk( hipDeviceSynchronize() );

		gettimeofday (&tv ,   &tz);
		time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
		time_compute = time_end - time_start;

		copy_device_to_host(h_A, N);
	}

	if (debug) {
		// print selected results (for checking correctness)
		for (int i=0; i<N; i+=N/10) { printf(" %10.6f",h_A[i]); }
		printf("\n");
	}
	if(!debug) {
		printf("Memcpy Time: %f seconds\n", time_memcpy);
		printf("Compute Time: %f seconds\n", time_compute);
	}

	// clean up memory allocated  
	free(h_A);

	hipDeviceReset();
}

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

                                        # End of file scope inline assembly
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z7inc_cpuPfii
.LCPI0_0:
	.long	0x3f800000                      # float 1
	.text
	.globl	_Z7inc_cpuPfii
	.type	_Z7inc_cpuPfii,@function
_Z7inc_cpuPfii:                         # @_Z7inc_cpuPfii
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB0_7
# %bb.1:                                # %.preheader.lr.ph
	movl	%esi, %eax
	xorl	%ecx, %ecx
	movss	.LCPI0_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
.LBB0_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
	testl	%edx, %edx
	jle	.LBB0_6
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB0_2 Depth=1
	movss	(%rdi,%rcx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	movl	%edx, %esi
.LBB0_4:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	addss	%xmm0, %xmm1
	decl	%esi
	jne	.LBB0_4
# %bb.5:                                # %._crit_edge
                                        #   in Loop: Header=BB0_2 Depth=1
	movss	%xmm1, (%rdi,%rcx,4)
.LBB0_6:                                #   in Loop: Header=BB0_2 Depth=1
	incq	%rcx
	cmpq	%rax, %rcx
	jne	.LBB0_2
.LBB0_7:                                # %._crit_edge10
	retq
.Lfunc_end0:
	.size	_Z7inc_cpuPfii, .Lfunc_end0-_Z7inc_cpuPfii
	.cfi_endproc
                                        # -- End function
	.globl	_Z22__device_stub__inc_gpuPfii  # -- Begin function _Z22__device_stub__inc_gpuPfii
	.type	_Z22__device_stub__inc_gpuPfii,@function
_Z22__device_stub__inc_gpuPfii:         # @_Z22__device_stub__inc_gpuPfii
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$96, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	4(%rsp), %rcx
	movl	%esi, (%rcx)
	movq	%rsp, %rsi
	movl	%edx, (%rsi)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z7inc_gpuPfii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$112, %rsp
	.cfi_adjust_cfa_offset -112
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z22__device_stub__inc_gpuPfii, .Lfunc_end1-_Z22__device_stub__inc_gpuPfii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z19copy_host_to_devicePfi
.LCPI2_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	_Z19copy_host_to_devicePfi
	.type	_Z19copy_host_to_devicePfi,@function
_Z19copy_host_to_devicePfi:             # @_Z19copy_host_to_devicePfi
	.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	$32, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebp
	movq	%rdi, %r15
	leaq	16(%rsp), %rbx
	leaq	8(%rsp), %r14
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	gettimeofday
	cvtsi2sdq	(%rbx), %xmm0
	cvtsi2sdq	8(%rbx), %xmm1
	divsd	.LCPI2_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, (%rsp)                   # 8-byte Spill
	movslq	%ebp, %rsi
	leaq	(,%rsi,4), %r12
	imulq	%r12, %rsi
	movl	$d_A, %edi
	callq	hipMalloc
	movq	d_A(%rip), %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%rbx), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%rbx), %xmm1
	divsd	.LCPI2_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	subsd	(%rsp), %xmm1                   # 8-byte Folded Reload
	addsd	time_memcpy(%rip), %xmm1
	movsd	%xmm1, time_memcpy(%rip)
	addq	$32, %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_end2:
	.size	_Z19copy_host_to_devicePfi, .Lfunc_end2-_Z19copy_host_to_devicePfi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z19copy_device_to_hostPfi
.LCPI3_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	_Z19copy_device_to_hostPfi
	.type	_Z19copy_device_to_hostPfi,@function
_Z19copy_device_to_hostPfi:             # @_Z19copy_device_to_hostPfi
	.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	%rbx
	.cfi_def_cfa_offset 40
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebp
	movq	%rdi, %r14
	leaq	24(%rsp), %rbx
	leaq	16(%rsp), %r15
	movq	%rbx, %rdi
	movq	%r15, %rsi
	callq	gettimeofday
	cvtsi2sdq	(%rbx), %xmm0
	cvtsi2sdq	8(%rbx), %xmm1
	divsd	.LCPI3_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, 8(%rsp)                  # 8-byte Spill
	movq	d_A(%rip), %rsi
	movslq	%ebp, %rdx
	shlq	$2, %rdx
	movq	%r14, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	d_A(%rip), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	movq	%r15, %rsi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%rbx), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%rbx), %xmm1
	divsd	.LCPI3_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	subsd	8(%rsp), %xmm1                  # 8-byte Folded Reload
	addsd	time_memcpy(%rip), %xmm1
	movsd	%xmm1, time_memcpy(%rip)
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.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	_Z19copy_device_to_hostPfi, .Lfunc_end3-_Z19copy_device_to_hostPfi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI4_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI4_1:
	.long	0x3f800000                      # float 1
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$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
	cmpl	$5, %edi
	jne	.LBB4_28
# %bb.1:
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %r14d
	movq	16(%rbx), %rdi
	callq	atoi
	movl	%eax, %r15d
	movq	24(%rbx), %rdi
	callq	atoi
	movl	%eax, %r12d
	movq	32(%rbx), %rdi
	movl	$.L.str.1, %esi
	callq	strcmp
	movl	%eax, 20(%rsp)                  # 4-byte Spill
	movslq	N(%rip), %rbp
	leaq	(,%rbp,4), %rdi
	callq	malloc
	movq	%rax, %rbx
	movl	%ebp, %r13d
	testq	%rbp, %rbp
	jle	.LBB4_3
# %bb.2:                                # %.lr.ph.preheader
	leaq	(,%r13,4), %rdx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
.LBB4_3:                                # %._crit_edge
	cmpl	$2, %r12d
	je	.LBB4_13
# %bb.4:                                # %._crit_edge
	cmpl	$1, %r12d
	jne	.LBB4_21
# %bb.5:
	leaq	40(%rsp), %r15
	leaq	24(%rsp), %rsi
	movq	%r15, %rdi
	callq	gettimeofday
	cvtsi2sdq	(%r15), %xmm0
	cvtsi2sdq	8(%r15), %xmm1
	divsd	.LCPI4_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, 8(%rsp)                  # 8-byte Spill
	movl	N(%rip), %eax
	testl	%eax, %eax
	jle	.LBB4_12
# %bb.6:                                # %.preheader.lr.ph.i
	xorl	%ecx, %ecx
	movss	.LCPI4_1(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
.LBB4_7:                                # %.preheader.i
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_9 Depth 2
	testl	%r14d, %r14d
	jle	.LBB4_11
# %bb.8:                                # %.lr.ph.i
                                        #   in Loop: Header=BB4_7 Depth=1
	movss	(%rbx,%rcx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	movl	%r14d, %edx
.LBB4_9:                                #   Parent Loop BB4_7 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	addss	%xmm0, %xmm1
	decl	%edx
	jne	.LBB4_9
# %bb.10:                               # %._crit_edge.i
                                        #   in Loop: Header=BB4_7 Depth=1
	movss	%xmm1, (%rbx,%rcx,4)
.LBB4_11:                               #   in Loop: Header=BB4_7 Depth=1
	incq	%rcx
	cmpq	%rax, %rcx
	jne	.LBB4_7
.LBB4_12:                               # %_Z7inc_cpuPfii.exit
	leaq	40(%rsp), %r14
	leaq	24(%rsp), %rsi
	movq	%r14, %rdi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r14), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%r14), %xmm1
	divsd	.LCPI4_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	subsd	8(%rsp), %xmm1                  # 8-byte Folded Reload
	movsd	%xmm1, time_compute(%rip)
	jmp	.LBB4_21
.LBB4_13:
	movq	%rbx, %rdi
	movl	%r13d, %esi
	callq	_Z19copy_host_to_devicePfi
	leaq	40(%rsp), %r12
	leaq	24(%rsp), %rsi
	movq	%r12, %rdi
	callq	gettimeofday
	cvtsi2sdq	(%r12), %xmm0
	movsd	%xmm0, 32(%rsp)                 # 8-byte Spill
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%r12), %xmm0
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movl	%r15d, %ecx
	btsq	$32, %rcx
	movl	N(%rip), %eax
	xorl	%edx, %edx
	divl	%r15d
                                        # kill: def $eax killed $eax def $rax
	btsq	$32, %rax
	movq	%rax, %rdi
	movl	$1, %esi
	movq	%rcx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_15
# %bb.14:
	movq	d_A(%rip), %rdi
	movl	N(%rip), %esi
	movl	%r14d, %edx
	callq	_Z22__device_stub__inc_gpuPfii
.LBB4_15:
	callq	hipPeekAtLastError
	testl	%eax, %eax
	jne	.LBB4_16
# %bb.18:                               # %_Z9gpuAssert10hipError_tPKcib.exit
	callq	hipDeviceSynchronize
	testl	%eax, %eax
	jne	.LBB4_19
# %bb.20:                               # %_Z9gpuAssert10hipError_tPKcib.exit40
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	divsd	.LCPI4_0(%rip), %xmm0
	addsd	32(%rsp), %xmm0                 # 8-byte Folded Reload
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	leaq	40(%rsp), %r14
	leaq	24(%rsp), %rsi
	movq	%r14, %rdi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r14), %xmm0
	cvtsi2sdq	8(%r14), %xmm1
	divsd	.LCPI4_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	subsd	8(%rsp), %xmm1                  # 8-byte Folded Reload
	movsd	%xmm1, time_compute(%rip)
	movl	N(%rip), %esi
	movq	%rbx, %rdi
	callq	_Z19copy_device_to_hostPfi
.LBB4_21:
	cmpl	$0, 20(%rsp)                    # 4-byte Folded Reload
	je	.LBB4_22
# %bb.26:                               # %.critedge
	movsd	time_memcpy(%rip), %xmm0        # xmm0 = mem[0],zero
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	movsd	time_compute(%rip), %xmm0       # xmm0 = mem[0],zero
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	jmp	.LBB4_27
.LBB4_22:                               # %.preheader
	cmpl	$0, N(%rip)
	jle	.LBB4_25
# %bb.23:                               # %.lr.ph45.preheader
	xorl	%r14d, %r14d
.LBB4_24:                               # %.lr.ph45
                                        # =>This Inner Loop Header: Depth=1
	movslq	%r14d, %r14
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx,%r14,4), %xmm0
	movl	$.L.str.3, %edi
	movb	$1, %al
	callq	printf
	movslq	N(%rip), %rax
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$34, %rcx
	addl	%edx, %ecx
	addl	%ecx, %r14d
	cmpl	%eax, %r14d
	jl	.LBB4_24
.LBB4_25:                               # %._crit_edge46
	movl	$10, %edi
	callq	putchar@PLT
.LBB4_27:
	movq	%rbx, %rdi
	callq	free
	callq	hipDeviceReset
	xorl	%eax, %eax
	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
.LBB4_28:
	.cfi_def_cfa_offset 112
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$2, %edi
	callq	exit
.LBB4_16:
	movl	%eax, %r14d
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.7, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$144, %r8d
	jmp	.LBB4_17
.LBB4_19:
	movl	%eax, %r14d
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.7, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$145, %r8d
.LBB4_17:
	xorl	%eax, %eax
	callq	fprintf
	movl	%r14d, %edi
	callq	exit
.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	$_Z7inc_gpuPfii, %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	N,@object                       # @N
	.data
	.globl	N
	.p2align	2, 0x0
N:
	.long	1024                            # 0x400
	.size	N, 4

	.type	d_A,@object                     # @d_A
	.bss
	.globl	d_A
	.p2align	3, 0x0
d_A:
	.quad	0
	.size	d_A, 8

	.type	time_memcpy,@object             # @time_memcpy
	.globl	time_memcpy
	.p2align	3, 0x0
time_memcpy:
	.quad	0x0000000000000000              # double 0
	.size	time_memcpy, 8

	.type	time_compute,@object            # @time_compute
	.globl	time_compute
	.p2align	3, 0x0
time_compute:
	.quad	0x0000000000000000              # double 0
	.size	time_compute, 8

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

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

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/Jchuk99/CS1541_Fall2020/master/study_guides/midterm_2_review/vec_inc_gpu.hip"
	.size	.L.str.2, 139

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	" %10.6f"
	.size	.L.str.3, 8

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Memcpy Time: %f seconds\n"
	.size	.L.str.5, 25

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Compute Time: %f seconds\n"
	.size	.L.str.6, 26

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"GPUassert: %s %s %d\n"
	.size	.L.str.7, 21

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z7inc_gpuPfii"
	.size	.L__unnamed_1, 15

	.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	"Usage: mat_mul_gpu <array size> <block size> <policy (1|2|3)> <(debug|nodebug)>"
	.size	.Lstr, 80

	.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 _Z22__device_stub__inc_gpuPfii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym d_A
	.addrsig_sym _Z7inc_gpuPfii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z7inc_gpuPfii          ; -- Begin function _Z7inc_gpuPfii
	.globl	_Z7inc_gpuPfii
	.p2align	8
	.type	_Z7inc_gpuPfii,@function
_Z7inc_gpuPfii:                         ; @_Z7inc_gpuPfii
; %bb.0:
	s_load_b32 s2, s[0:1], 0xc
	s_waitcnt lgkmcnt(0)
	s_cmp_lt_i32 s2, 1
	s_cbranch_scc1 .LBB0_4
; %bb.1:                                ; %.lr.ph
	s_clause 0x1
	s_load_b32 s3, s[0:1], 0x1c
	s_load_b64 s[0:1], s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s3, s3, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s3, v[0:1]
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	v_add_co_u32 v0, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_load_b32 v2, v[0:1], off
.LBB0_2:                                ; =>This Inner Loop Header: Depth=1
	s_waitcnt vmcnt(0)
	v_add_f32_e32 v2, 1.0, v2
	s_add_i32 s2, s2, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s2, 0
	s_cbranch_scc0 .LBB0_2
; %bb.3:                                ; %._crit_edge
	global_store_b32 v[0:1], v2, off
.LBB0_4:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z7inc_gpuPfii
		.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 3
		.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	_Z7inc_gpuPfii, .Lfunc_end0-_Z7inc_gpuPfii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 148
; NumSgprs: 18
; NumVgprs: 3
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 3
; 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:         12
        .size:           4
        .value_kind:     by_value
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z7inc_gpuPfii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z7inc_gpuPfii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     3
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
