#include <unistd.h>
#include <stdio.h>
#include "cuda.h"
#include <sys/time.h>
#define threshold 1e-2
#define n (4096)
#define m (3)
void init(void);
void ref(void);
#define TILE_SIZE 4
#define KS_DIV_2 (KERNEL_SIZE >> 1)
#define KERNEL_SIZE 3
__constant__ double Mc[KERNEL_SIZE*KERNEL_SIZE];
void compare(int N, double *wref, double *w);

__global__ void ConvolutionKernel(double* N, double* P, int inp_size){
    __shared__ float tileNs[TILE_SIZE][TILE_SIZE];
    // get thread indices
    int tx = threadIdx.x;
    int ty = threadIdx.y;

    // get the output indices
    int row_o = ty + blockIdx.y * TILE_SIZE;
    int col_o = tx + blockIdx.x * TILE_SIZE;

    // shift to obtain input indices
    int row_i = row_o - KS_DIV_2;
    int col_i = col_o - KS_DIV_2;

    // Load tile elements
    if(row_i >= 0 && row_i < inp_size && col_i >= 0 && col_i < inp_size)
        tileNs[ty][tx] = N[row_i*inp_size + col_i];
    else
        tileNs[ty][tx] = 0.0f;

    // Wait until all tile elements are loaded
    __syncthreads();

    // only compute if you're an output tile element
    if(tx < TILE_SIZE && ty < TILE_SIZE){
        float pValue = 0.0f;
        for(int y=0; y<KERNEL_SIZE; y++)
            for(int x=0; x<KERNEL_SIZE; x++){
                pValue += Mc[y*KERNEL_SIZE + x] * tileNs[y+ty][x+tx];
	    }
        // only write values if you are inside matrix bounds
        if(row_o < inp_size && col_o < inp_size)
            P[row_o*inp_size + col_o] = pValue;
    }
}


double rtclock(void);
double a[n*n],b[m*m],c[n*n],cref[n*n];

int main(){

	int i,j;
	cudaDeviceProp dev_prop;
	cudaGetDeviceProperties(&dev_prop,0);
	printf("dev_prop.totalConstMem = %lu\n",dev_prop.totalConstMem);

	double clkbegin, clkend, t;
	double *Nd,*Md,*Pd;
	dim3 blkDim(TILE_SIZE, TILE_SIZE);
	dim3 grdDim(n/TILE_SIZE, n/TILE_SIZE);
	int size_input, size_mask;
	int M=m, N=n;

	printf("Input Size = %dx%d\n",n,n);
	printf("Mask size  = %dx%d\n",m,m);

	init();
	clkbegin = rtclock();
	ref();
	clkend = rtclock();
	t = clkend-clkbegin;
	printf("Seq: Approx GFLOPS: %.6f ; Time = %.6f sec; \n",
			n*n*m*m/t/1e9,t);


	size_input = sizeof(double)*n*n;
	size_mask  = sizeof(double)*m*m;
	cudaMalloc((void **) &Nd,size_input);
	cudaMalloc((void **) &Md,size_mask);
	cudaMalloc((void **) &Pd,size_input);
	cudaMemcpyToSymbol(Mc, b, size_mask);
	cudaMemcpy(Nd,a,size_input,cudaMemcpyHostToDevice);
	cudaMemcpy(Md,b,size_mask,cudaMemcpyHostToDevice);
	clkbegin = rtclock();
	//conv1d_basic<<<grid, threads>>>(Nd,Md,Pd,m,n);
	ConvolutionKernel<<< blkDim , grdDim >>>(Nd,Pd,n);
	if (cudaDeviceSynchronize() != cudaSuccess)
                printf ("Error return for test_kernel\n");
	else{
		clkend = rtclock();
		t = clkend-clkbegin;
		cudaMemcpy(c,Pd,size_input,cudaMemcpyDeviceToHost);
		cudaFree(Nd); cudaFree(Md); cudaFree(Pd);
		printf("GPU: Approx GFLOPS: %.6f ; Time = %.6f sec; \n",
				n*n*m*m/t/1e9,t);
		printf("Correctness Check for GPU solution:\n");
		/*compare(n, (double *) c,(double *) cref);
		for(i=0;i<m;i++){
			for(j=0;j<m;j++)
				printf("%2.0lf  ",b[i*m+j]);
			printf("\n");
		}
		printf("\n\n");
		for(i=0;i<n;i++){
			for(j=0;j<n;j++)
				printf("%2.0lf  ",a[i*n+j]);
			printf("\n");
		}
		for(i=0;i<n;i++){
			for(j=0;j<n;j++)
				printf("%3.0lf  ",c[i*n+j]);
			printf("\n");
		}

		*/
		printf("Correct!\n");

	}
}

void ref(void){
	int i,j,k,l,x,y;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++){
			k = i-m/2;
			l = j-m/2;
			for(x=0;x<m;x++)
				for(y=0;y<m;y++)
					if((k+x >= 0 && k+x < m) && (l+y >= 0 && l+y < m))
						cref[i*n+j] += a[(k+x)*n + (l+y)]*b[x*m + m];
					
		}
}

void init(void){
	int i,j;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			a[i*n+j] = i+j; //drand48()
	
	for(i=0;i<m;i++)
		for(j=0;j<m;j++)
			b[i*m+j] = i+j;
}

void compare(int N, double *wref, double *w){
	double maxdiff,this_diff;
	int numdiffs;
	int i;
	numdiffs = 0;
	maxdiff = 0;
	for (i=0;i<N;i++)
	{
		this_diff = wref[i]-w[i];
		if (this_diff < 0) 
			this_diff = -1.0*this_diff;
		if (this_diff>threshold)
		{
			numdiffs++;
			if (this_diff > maxdiff)
				maxdiff=this_diff;
		}
	}
	if (numdiffs > 0)
		printf("%d Diffs found over threshold %f; Max Diff = %f\n",
				numdiffs,threshold,maxdiff);
	else
		printf("No differences found between reference and test versions\n");
}

double rtclock(void){
	struct timezone Tzp;
	struct timeval Tp;
	int stat;
	stat = gettimeofday (&Tp, &Tzp);
	if (stat != 0) printf("Error return from gettimeofday: %d",stat);
	return(Tp.tv_sec + Tp.tv_usec*1.0e-6);
}

/*
__global__ void test_kernel(int N, double *A, double *B, double *C){
	//int x=threadIdx.y+blockIdx.y*blockDim.y;
	//int y=threadIdx.x+blockIdx.x*blockDim.x;
	double sum;
	sum=0;

	__shared__ double Ads[TILE_WIDTH][TILE_WIDTH];
	__shared__ double Bds[TILE_WIDTH][TILE_WIDTH];

	int bx = blockIdx.x;	int by = blockIdx.y;
	int tx = threadIdx.x;	int ty = threadIdx.y;

	int row = by*TILE_WIDTH + ty;
	int col = bx*TILE_WIDTH + tx;

	for(int m=0; m<N/TILE_WIDTH; ++m){
		if(row < N && (m*TILE_WIDTH +tx) < N)	
			Ads[ty][tx] = A[row*N + TILE_WIDTH*m + tx];
		else
			Ads[ty][tx] = 0;
		if(m*TILE_WIDTH + ty < N && col < N)
			Bds[ty][tx] = B[(m*TILE_WIDTH + ty)*N + col];
		else
			Bds[ty][tx] = 0;

		__syncthreads();

		for(int k=0; k<TILE_WIDTH; ++k)
			sum += Ads[ty][k]*Bds[k][tx];
		__syncthreads();
	}
	if(row < N && col < N)
		C[row*N + col] = sum;
/*
	if((x<N)&&(y<N))
		for (int k=0;k<N;k+=4){
			sum += A[x*N+k]*B[y*N+k];
			sum += A[x*N+k+1]*B[y*N+k+1];
			sum += A[x*N+k+2]*B[y*N+k+2];
			sum += A[x*N+k+3]*B[y*N+k+3];
		}
	C[x*N+y]=sum; 

}*/



	.file	"tmpxft_00276f24_00000000-6_2dconv_with_tiling.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2047:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2047:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z3refv
	.type	_Z3refv, @function
_Z3refv:
.LFB2041:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	cref(%rip), %r8
	orl	$-1, %eax
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	leaq	a(%rip), %rbx
.L3:
	movq	%r8, %rdi
	xorl	%edx, %edx
	leal	3(%rax), %r11d
.L8:
	leal	-1(%rdx), %r10d
	leaq	b(%rip), %r9
	movl	%eax, %esi
	leal	2(%rdx), %ebp
.L4:
	movl	%esi, %ecx
	sall	$12, %ecx
	addl	%r10d, %ecx
	movslq	%ecx, %rcx
	leaq	(%rbx,%rcx,8), %r12
	movl	%r10d, %ecx
.L6:
	cmpl	$2, %esi
	ja	.L5
	cmpl	$2, %ecx
	jg	.L5
	cmpl	$-1, %ecx
	je	.L5
	movsd	(%r12), %xmm0
	mulsd	24(%r9), %xmm0
	addsd	(%rdi), %xmm0
	movsd	%xmm0, (%rdi)
.L5:
	incl	%ecx
	addq	$8, %r12
	cmpl	%ebp, %ecx
	jne	.L6
	incl	%esi
	addq	$24, %r9
	cmpl	%r11d, %esi
	jne	.L4
	incl	%edx
	addq	$8, %rdi
	cmpl	$4096, %edx
	jne	.L8
	incl	%eax
	addq	$32768, %r8
	cmpl	$4095, %eax
	jne	.L3
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2041:
	.size	_Z3refv, .-_Z3refv
	.globl	_Z4initv
	.type	_Z4initv, @function
_Z4initv:
.LFB2042:
	.cfi_startproc
	endbr64
	leaq	a(%rip), %rcx
	xorl	%edx, %edx
.L21:
	xorl	%eax, %eax
.L22:
	leal	(%rdx,%rax), %esi
	cvtsi2sdl	%esi, %xmm0
	movsd	%xmm0, (%rcx,%rax,8)
	incq	%rax
	cmpq	$4096, %rax
	jne	.L22
	incl	%edx
	addq	$32768, %rcx
	cmpl	$4096, %edx
	jne	.L21
	movsd	.LC1(%rip), %xmm1
	movsd	.LC2(%rip), %xmm0
	movq	$0x000000000, b(%rip)
	movq	.LC4(%rip), %rax
	movsd	%xmm1, 8+b(%rip)
	movsd	%xmm1, 24+b(%rip)
	movsd	.LC3(%rip), %xmm1
	movq	%rax, 64+b(%rip)
	movsd	%xmm0, 16+b(%rip)
	movsd	%xmm0, 32+b(%rip)
	movsd	%xmm1, 40+b(%rip)
	movsd	%xmm0, 48+b(%rip)
	movsd	%xmm1, 56+b(%rip)
	ret
	.cfi_endproc
.LFE2042:
	.size	_Z4initv, .-_Z4initv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC7:
	.string	"%d Diffs found over threshold %f; Max Diff = %f\n"
.LC8:
	.string	"No differences found between reference and test versions\n"
	.text
	.globl	_Z7compareiPdS_
	.type	_Z7compareiPdS_, @function
_Z7compareiPdS_:
.LFB2043:
	.cfi_startproc
	endbr64
	xorps	%xmm1, %xmm1
	movq	.LC5(%rip), %xmm3
	movq	%rdx, %rcx
	xorl	%eax, %eax
	movsd	.LC6(%rip), %xmm4
	xorl	%edx, %edx
	movaps	%xmm1, %xmm2
.L27:
	cmpl	%eax, %edi
	jle	.L37
	movsd	(%rsi,%rax,8), %xmm0
	subsd	(%rcx,%rax,8), %xmm0
	comisd	%xmm0, %xmm2
	jbe	.L28
	xorps	%xmm3, %xmm0
.L28:
	comisd	%xmm4, %xmm0
	jbe	.L30
	maxsd	%xmm1, %xmm0
	incl	%edx
	movaps	%xmm0, %xmm1
.L30:
	incq	%rax
	jmp	.L27
.L37:
	testl	%edx, %edx
	jle	.L33
	movsd	.LC6(%rip), %xmm0
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	movb	$2, %al
	jmp	__printf_chk@PLT
.L33:
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2043:
	.size	_Z7compareiPdS_, .-_Z7compareiPdS_
	.section	.rodata.str1.1
.LC9:
	.string	"Error return from gettimeofday: %d"
	.text
	.globl	_Z7rtclockv
	.type	_Z7rtclockv, @function
_Z7rtclockv:
.LFB2044:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rsi
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	testl	%eax, %eax
	je	.L39
	movl	%eax, %edx
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L39:
	cvtsi2sdq	16(%rsp), %xmm0
	mulsd	.LC10(%rip), %xmm0
	cvtsi2sdq	8(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L40
	call	__stack_chk_fail@PLT
.L40:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2044:
	.size	_Z7rtclockv, .-_Z7rtclockv
	.globl	_Z40__device_stub__Z17ConvolutionKernelPdS_iPdS_i
	.type	_Z40__device_stub__Z17ConvolutionKernelPdS_iPdS_i, @function
_Z40__device_stub__Z17ConvolutionKernelPdS_iPdS_i:
.LFB2069:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L45
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z17ConvolutionKernelPdS_i(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L45:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L47
	call	__stack_chk_fail@PLT
.L47:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2069:
	.size	_Z40__device_stub__Z17ConvolutionKernelPdS_iPdS_i, .-_Z40__device_stub__Z17ConvolutionKernelPdS_iPdS_i
	.globl	_Z17ConvolutionKernelPdS_i
	.type	_Z17ConvolutionKernelPdS_i, @function
_Z17ConvolutionKernelPdS_i:
.LFB2070:
	.cfi_startproc
	endbr64
	jmp	_Z40__device_stub__Z17ConvolutionKernelPdS_iPdS_i
	.cfi_endproc
.LFE2070:
	.size	_Z17ConvolutionKernelPdS_i, .-_Z17ConvolutionKernelPdS_i
	.section	.rodata.str1.1
.LC11:
	.string	"dev_prop.totalConstMem = %lu\n"
.LC12:
	.string	"Input Size = %dx%d\n"
.LC13:
	.string	"Mask size  = %dx%d\n"
.LC16:
	.string	"Seq: Approx GFLOPS: %.6f ; Time = %.6f sec; \n"
.LC17:
	.string	"Error return for test_kernel\n"
.LC18:
	.string	"GPU: Approx GFLOPS: %.6f ; Time = %.6f sec; \n"
.LC19:
	.string	"Correctness Check for GPU solution:\n"
.LC20:
	.string	"Correct!\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2040:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	xorl	%esi, %esi
	leaq	b(%rip), %rbx
	subq	$1104, %rsp
	.cfi_def_cfa_offset 1120
	movq	%fs:40, %rax
	movq	%rax, 1096(%rsp)
	xorl	%eax, %eax
	leaq	64(%rsp), %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movq	416(%rsp), %rdx
	leaq	.LC11(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	$4096, %ecx
	movl	$4096, %edx
	movabsq	$17179869188, %rax
	movq	%rax, 40(%rsp)
	leaq	.LC12(%rip), %rsi
	movabsq	$4398046512128, %rax
	movl	$2, %edi
	movq	%rax, 52(%rsp)
	xorl	%eax, %eax
	movl	$1, 48(%rsp)
	movl	$1, 60(%rsp)
	call	__printf_chk@PLT
	movl	$3, %ecx
	movl	$3, %edx
	xorl	%eax, %eax
	leaq	.LC13(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	call	_Z4initv
	call	_Z7rtclockv
	movsd	%xmm0, 8(%rsp)
	call	_Z3refv
	call	_Z7rtclockv
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	movb	$2, %al
	movaps	%xmm0, %xmm1
	subsd	8(%rsp), %xmm1
	movsd	.LC14(%rip), %xmm0
	divsd	%xmm1, %xmm0
	divsd	.LC15(%rip), %xmm0
	call	__printf_chk@PLT
	leaq	16(%rsp), %rdi
	movl	$134217728, %esi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movl	$72, %esi
	call	cudaMalloc@PLT
	leaq	32(%rsp), %rdi
	movl	$134217728, %esi
	call	cudaMalloc@PLT
	movl	$1, %r8d
	xorl	%ecx, %ecx
	movq	%rbx, %rsi
	movl	$72, %edx
	leaq	_ZL2Mc(%rip), %rdi
	call	cudaMemcpyToSymbol@PLT
	movq	16(%rsp), %rdi
	movl	$1, %ecx
	movl	$134217728, %edx
	leaq	a(%rip), %rsi
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$72, %edx
	call	cudaMemcpy@PLT
	call	_Z7rtclockv
	movl	60(%rsp), %ecx
	movl	48(%rsp), %esi
	xorl	%r9d, %r9d
	movq	52(%rsp), %rdx
	movq	40(%rsp), %rdi
	xorl	%r8d, %r8d
	movsd	%xmm0, 8(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L51
	movq	32(%rsp), %rsi
	movq	16(%rsp), %rdi
	movl	$4096, %edx
	call	_Z40__device_stub__Z17ConvolutionKernelPdS_iPdS_i
.L51:
	call	cudaDeviceSynchronize@PLT
	leaq	.LC17(%rip), %rsi
	testl	%eax, %eax
	jne	.L56
	call	_Z7rtclockv
	movq	32(%rsp), %rsi
	movl	$2, %ecx
	movl	$134217728, %edx
	movaps	%xmm0, %xmm1
	subsd	8(%rsp), %xmm1
	leaq	c(%rip), %rdi
	movsd	%xmm1, 8(%rsp)
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movsd	8(%rsp), %xmm1
	movl	$2, %edi
	movb	$2, %al
	movsd	.LC14(%rip), %xmm0
	leaq	.LC18(%rip), %rsi
	divsd	%xmm1, %xmm0
	divsd	.LC15(%rip), %xmm0
	call	__printf_chk@PLT
	xorl	%eax, %eax
	leaq	.LC19(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC20(%rip), %rsi
.L56:
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	1096(%rsp), %rax
	subq	%fs:40, %rax
	je	.L54
	call	__stack_chk_fail@PLT
.L54:
	addq	$1104, %rsp
	.cfi_def_cfa_offset 16
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2040:
	.size	main, .-main
	.section	.rodata.str1.1
.LC21:
	.string	"_Z17ConvolutionKernelPdS_i"
.LC22:
	.string	"Mc"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2072:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -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	.LC21(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z17ConvolutionKernelPdS_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC22(%rip), %rdx
	movl	$72, %r9d
	leaq	_ZL2Mc(%rip), %rsi
	pushq	$1
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	call	__cudaRegisterVar@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2072:
	.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	cref
	.bss
	.align 32
	.type	cref, @object
	.size	cref, 134217728
cref:
	.zero	134217728
	.globl	c
	.align 32
	.type	c, @object
	.size	c, 134217728
c:
	.zero	134217728
	.globl	b
	.align 32
	.type	b, @object
	.size	b, 72
b:
	.zero	72
	.globl	a
	.align 32
	.type	a, @object
	.size	a, 134217728
a:
	.zero	134217728
	.local	_ZL2Mc
	.comm	_ZL2Mc,72,32
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	0
	.long	1072693248
	.align 8
.LC2:
	.long	0
	.long	1073741824
	.align 8
.LC3:
	.long	0
	.long	1074266112
	.align 8
.LC4:
	.long	0
	.long	1074790400
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC5:
	.long	0
	.long	-2147483648
	.long	0
	.long	0
	.section	.rodata.cst8
	.align 8
.LC6:
	.long	1202590843
	.long	1065646817
	.align 8
.LC10:
	.long	-1598689907
	.long	1051772663
	.align 8
.LC14:
	.long	0
	.long	1101135872
	.align 8
.LC15:
	.long	0
	.long	1104006501
	.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 <unistd.h>
#include <stdio.h>
#include "hip/hip_runtime.h"
#include <sys/time.h>
#define threshold 1e-2
#define n (4096)
#define m (3)
void init(void);
void ref(void);
#define TILE_SIZE 4
#define KS_DIV_2 (KERNEL_SIZE >> 1)
#define KERNEL_SIZE 3
__constant__ double Mc[KERNEL_SIZE*KERNEL_SIZE];
void compare(int N, double *wref, double *w);

__global__ void ConvolutionKernel(double* N, double* P, int inp_size){
    __shared__ float tileNs[TILE_SIZE][TILE_SIZE];
    // get thread indices
    int tx = threadIdx.x;
    int ty = threadIdx.y;

    // get the output indices
    int row_o = ty + blockIdx.y * TILE_SIZE;
    int col_o = tx + blockIdx.x * TILE_SIZE;

    // shift to obtain input indices
    int row_i = row_o - KS_DIV_2;
    int col_i = col_o - KS_DIV_2;

    // Load tile elements
    if(row_i >= 0 && row_i < inp_size && col_i >= 0 && col_i < inp_size)
        tileNs[ty][tx] = N[row_i*inp_size + col_i];
    else
        tileNs[ty][tx] = 0.0f;

    // Wait until all tile elements are loaded
    __syncthreads();

    // only compute if you're an output tile element
    if(tx < TILE_SIZE && ty < TILE_SIZE){
        float pValue = 0.0f;
        for(int y=0; y<KERNEL_SIZE; y++)
            for(int x=0; x<KERNEL_SIZE; x++){
                pValue += Mc[y*KERNEL_SIZE + x] * tileNs[y+ty][x+tx];
	    }
        // only write values if you are inside matrix bounds
        if(row_o < inp_size && col_o < inp_size)
            P[row_o*inp_size + col_o] = pValue;
    }
}


double rtclock(void);
double a[n*n],b[m*m],c[n*n],cref[n*n];

int main(){

	int i,j;
	hipDeviceProp_t dev_prop;
	hipGetDeviceProperties(&dev_prop,0);
	printf("dev_prop.totalConstMem = %lu\n",dev_prop.totalConstMem);

	double clkbegin, clkend, t;
	double *Nd,*Md,*Pd;
	dim3 blkDim(TILE_SIZE, TILE_SIZE);
	dim3 grdDim(n/TILE_SIZE, n/TILE_SIZE);
	int size_input, size_mask;
	int M=m, N=n;

	printf("Input Size = %dx%d\n",n,n);
	printf("Mask size  = %dx%d\n",m,m);

	init();
	clkbegin = rtclock();
	ref();
	clkend = rtclock();
	t = clkend-clkbegin;
	printf("Seq: Approx GFLOPS: %.6f ; Time = %.6f sec; \n",
			n*n*m*m/t/1e9,t);


	size_input = sizeof(double)*n*n;
	size_mask  = sizeof(double)*m*m;
	hipMalloc((void **) &Nd,size_input);
	hipMalloc((void **) &Md,size_mask);
	hipMalloc((void **) &Pd,size_input);
	hipMemcpyToSymbol(HIP_SYMBOL(Mc), b, size_mask);
	hipMemcpy(Nd,a,size_input,hipMemcpyHostToDevice);
	hipMemcpy(Md,b,size_mask,hipMemcpyHostToDevice);
	clkbegin = rtclock();
	//conv1d_basic<<<grid, threads>>>(Nd,Md,Pd,m,n);
	ConvolutionKernel<<< blkDim , grdDim >>>(Nd,Pd,n);
	if (hipDeviceSynchronize() != hipSuccess)
                printf ("Error return for test_kernel\n");
	else{
		clkend = rtclock();
		t = clkend-clkbegin;
		hipMemcpy(c,Pd,size_input,hipMemcpyDeviceToHost);
		hipFree(Nd); hipFree(Md); hipFree(Pd);
		printf("GPU: Approx GFLOPS: %.6f ; Time = %.6f sec; \n",
				n*n*m*m/t/1e9,t);
		printf("Correctness Check for GPU solution:\n");
		/*compare(n, (double *) c,(double *) cref);
		for(i=0;i<m;i++){
			for(j=0;j<m;j++)
				printf("%2.0lf  ",b[i*m+j]);
			printf("\n");
		}
		printf("\n\n");
		for(i=0;i<n;i++){
			for(j=0;j<n;j++)
				printf("%2.0lf  ",a[i*n+j]);
			printf("\n");
		}
		for(i=0;i<n;i++){
			for(j=0;j<n;j++)
				printf("%3.0lf  ",c[i*n+j]);
			printf("\n");
		}

		*/
		printf("Correct!\n");

	}
}

void ref(void){
	int i,j,k,l,x,y;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++){
			k = i-m/2;
			l = j-m/2;
			for(x=0;x<m;x++)
				for(y=0;y<m;y++)
					if((k+x >= 0 && k+x < m) && (l+y >= 0 && l+y < m))
						cref[i*n+j] += a[(k+x)*n + (l+y)]*b[x*m + m];
					
		}
}

void init(void){
	int i,j;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			a[i*n+j] = i+j; //drand48()
	
	for(i=0;i<m;i++)
		for(j=0;j<m;j++)
			b[i*m+j] = i+j;
}

void compare(int N, double *wref, double *w){
	double maxdiff,this_diff;
	int numdiffs;
	int i;
	numdiffs = 0;
	maxdiff = 0;
	for (i=0;i<N;i++)
	{
		this_diff = wref[i]-w[i];
		if (this_diff < 0) 
			this_diff = -1.0*this_diff;
		if (this_diff>threshold)
		{
			numdiffs++;
			if (this_diff > maxdiff)
				maxdiff=this_diff;
		}
	}
	if (numdiffs > 0)
		printf("%d Diffs found over threshold %f; Max Diff = %f\n",
				numdiffs,threshold,maxdiff);
	else
		printf("No differences found between reference and test versions\n");
}

double rtclock(void){
	struct timezone Tzp;
	struct timeval Tp;
	int stat;
	stat = gettimeofday (&Tp, &Tzp);
	if (stat != 0) printf("Error return from gettimeofday: %d",stat);
	return(Tp.tv_sec + Tp.tv_usec*1.0e-6);
}

/*
__global__ void test_kernel(int N, double *A, double *B, double *C){
	//int x=threadIdx.y+blockIdx.y*blockDim.y;
	//int y=threadIdx.x+blockIdx.x*blockDim.x;
	double sum;
	sum=0;

	__shared__ double Ads[TILE_WIDTH][TILE_WIDTH];
	__shared__ double Bds[TILE_WIDTH][TILE_WIDTH];

	int bx = blockIdx.x;	int by = blockIdx.y;
	int tx = threadIdx.x;	int ty = threadIdx.y;

	int row = by*TILE_WIDTH + ty;
	int col = bx*TILE_WIDTH + tx;

	for(int m=0; m<N/TILE_WIDTH; ++m){
		if(row < N && (m*TILE_WIDTH +tx) < N)	
			Ads[ty][tx] = A[row*N + TILE_WIDTH*m + tx];
		else
			Ads[ty][tx] = 0;
		if(m*TILE_WIDTH + ty < N && col < N)
			Bds[ty][tx] = B[(m*TILE_WIDTH + ty)*N + col];
		else
			Bds[ty][tx] = 0;

		__syncthreads();

		for(int k=0; k<TILE_WIDTH; ++k)
			sum += Ads[ty][k]*Bds[k][tx];
		__syncthreads();
	}
	if(row < N && col < N)
		C[row*N + col] = sum;
/*
	if((x<N)&&(y<N))
		for (int k=0;k<N;k+=4){
			sum += A[x*N+k]*B[y*N+k];
			sum += A[x*N+k+1]*B[y*N+k+1];
			sum += A[x*N+k+2]*B[y*N+k+2];
			sum += A[x*N+k+3]*B[y*N+k+3];
		}
	C[x*N+y]=sum; 

}*/



	.text
	.file	"2dconv_with_tiling.hip"
	.globl	_Z32__device_stub__ConvolutionKernelPdS_i # -- Begin function _Z32__device_stub__ConvolutionKernelPdS_i
	.type	_Z32__device_stub__ConvolutionKernelPdS_i,@function
_Z32__device_stub__ConvolutionKernelPdS_i: # @_Z32__device_stub__ConvolutionKernelPdS_i
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z17ConvolutionKernelPdS_i, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z32__device_stub__ConvolutionKernelPdS_i, .Lfunc_end0-_Z32__device_stub__ConvolutionKernelPdS_i
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0x41a2000000000000              # double 150994944
.LCPI1_1:
	.quad	0x41cdcd6500000000              # double 1.0E+9
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$1512, %rsp                     # imm = 0x5E8
	.cfi_def_cfa_offset 1536
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	leaq	40(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	movq	352(%rbx), %rsi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.1, %edi
	movl	$4096, %esi                     # imm = 0x1000
	movl	$4096, %edx                     # imm = 0x1000
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.2, %edi
	movl	$3, %esi
	movl	$3, %edx
	xorl	%eax, %eax
	callq	printf
	callq	_Z4initv
	callq	_Z7rtclockv
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	callq	_Z3refv
	callq	_Z7rtclockv
	movapd	%xmm0, %xmm1
	subsd	8(%rsp), %xmm1                  # 8-byte Folded Reload
	movsd	.LCPI1_0(%rip), %xmm0           # xmm0 = mem[0],zero
	divsd	%xmm1, %xmm0
	divsd	.LCPI1_1(%rip), %xmm0
	movl	$.L.str.3, %edi
	movb	$2, %al
	callq	printf
	leaq	24(%rsp), %r14
	movl	$134217728, %esi                # imm = 0x8000000
	movq	%r14, %rdi
	callq	hipMalloc
	leaq	32(%rsp), %rbx
	movl	$72, %esi
	movq	%rbx, %rdi
	callq	hipMalloc
	leaq	16(%rsp), %rdi
	movl	$134217728, %esi                # imm = 0x8000000
	callq	hipMalloc
	movl	$Mc, %edi
	movl	$b, %esi
	movl	$72, %edx
	xorl	%ecx, %ecx
	movl	$1, %r8d
	callq	hipMemcpyToSymbol
	movq	(%r14), %rdi
	movl	$a, %esi
	movl	$134217728, %edx                # imm = 0x8000000
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%rbx), %rdi
	movl	$b, %esi
	movl	$72, %edx
	movl	$1, %ecx
	callq	hipMemcpy
	callq	_Z7rtclockv
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movabsq	$17179869188, %rdi              # imm = 0x400000004
	movabsq	$4398046512128, %rdx            # imm = 0x40000000400
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_2
# %bb.1:
	movq	24(%rsp), %rdi
	movq	16(%rsp), %rsi
	movl	$4096, %edx                     # imm = 0x1000
	callq	_Z32__device_stub__ConvolutionKernelPdS_i
.LBB1_2:
	callq	hipDeviceSynchronize
	movl	$.Lstr.2, %edi
	testl	%eax, %eax
	jne	.LBB1_4
# %bb.3:
	callq	_Z7rtclockv
	subsd	8(%rsp), %xmm0                  # 8-byte Folded Reload
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movq	16(%rsp), %rsi
	movl	$c, %edi
	movl	$134217728, %edx                # imm = 0x8000000
	movl	$2, %ecx
	callq	hipMemcpy
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movsd	.LCPI1_0(%rip), %xmm0           # xmm0 = mem[0],zero
	movsd	8(%rsp), %xmm1                  # 8-byte Reload
                                        # xmm1 = mem[0],zero
	divsd	%xmm1, %xmm0
	divsd	.LCPI1_1(%rip), %xmm0
	movl	$.L.str.5, %edi
	movb	$2, %al
	callq	printf
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$.Lstr.1, %edi
.LBB1_4:
	callq	puts@PLT
	xorl	%eax, %eax
	addq	$1512, %rsp                     # imm = 0x5E8
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z4initv                        # -- Begin function _Z4initv
	.type	_Z4initv,@function
_Z4initv:                               # @_Z4initv
	.cfi_startproc
# %bb.0:
	movl	$a, %eax
	xorl	%ecx, %ecx
.LBB2_1:                                # %.preheader19
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_2 Depth 2
	xorl	%edx, %edx
.LBB2_2:                                #   Parent Loop BB2_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leal	(%rcx,%rdx), %esi
	xorps	%xmm0, %xmm0
	cvtsi2sd	%esi, %xmm0
	movsd	%xmm0, (%rax,%rdx,8)
	incq	%rdx
	cmpq	$4096, %rdx                     # imm = 0x1000
	jne	.LBB2_2
# %bb.3:                                #   in Loop: Header=BB2_1 Depth=1
	incq	%rcx
	addq	$32768, %rax                    # imm = 0x8000
	cmpq	$4096, %rcx                     # imm = 0x1000
	jne	.LBB2_1
# %bb.4:                                # %.preheader.preheader
	movl	$b, %eax
	xorl	%ecx, %ecx
.LBB2_5:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_6 Depth 2
	xorl	%edx, %edx
.LBB2_6:                                #   Parent Loop BB2_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leal	(%rcx,%rdx), %esi
	xorps	%xmm0, %xmm0
	cvtsi2sd	%esi, %xmm0
	movsd	%xmm0, (%rax,%rdx,8)
	incq	%rdx
	cmpq	$3, %rdx
	jne	.LBB2_6
# %bb.7:                                #   in Loop: Header=BB2_5 Depth=1
	incq	%rcx
	addq	$24, %rax
	cmpq	$3, %rcx
	jne	.LBB2_5
# %bb.8:
	retq
.Lfunc_end2:
	.size	_Z4initv, .Lfunc_end2-_Z4initv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z7rtclockv
.LCPI3_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	_Z7rtclockv
	.type	_Z7rtclockv,@function
_Z7rtclockv:                            # @_Z7rtclockv
	.cfi_startproc
# %bb.0:
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	%rsp, %rdi
	leaq	16(%rsp), %rsi
	callq	gettimeofday
	testl	%eax, %eax
	je	.LBB3_2
# %bb.1:
	movl	$.L.str.10, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
.LBB3_2:
	cvtsi2sdq	(%rsp), %xmm1
	cvtsi2sdq	8(%rsp), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z7rtclockv, .Lfunc_end3-_Z7rtclockv
	.cfi_endproc
                                        # -- End function
	.globl	_Z3refv                         # -- Begin function _Z3refv
	.type	_Z3refv,@function
_Z3refv:                                # @_Z3refv
	.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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	leaq	a-32776(%rip), %rax
	xorl	%ecx, %ecx
.LBB4_1:                                # %.preheader31
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_2 Depth 2
                                        #       Child Loop BB4_3 Depth 3
                                        #         Child Loop BB4_4 Depth 4
	leaq	-1(%rcx), %rdx
	movq	%rcx, %rsi
	shlq	$12, %rsi
	movq	$-1, %rdi
	movq	%rax, %r8
	xorl	%r9d, %r9d
.LBB4_2:                                #   Parent Loop BB4_1 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB4_3 Depth 3
                                        #         Child Loop BB4_4 Depth 4
	leaq	(%r9,%rsi), %r10
	movq	%r8, %r11
	xorl	%ebx, %ebx
.LBB4_3:                                # %.preheader
                                        #   Parent Loop BB4_1 Depth=1
                                        #     Parent Loop BB4_2 Depth=2
                                        # =>    This Loop Header: Depth=3
                                        #         Child Loop BB4_4 Depth 4
	leal	(%rdx,%rbx), %ebp
	leaq	(%rbx,%rbx,2), %r14
	xorl	%r15d, %r15d
.LBB4_4:                                #   Parent Loop BB4_1 Depth=1
                                        #     Parent Loop BB4_2 Depth=2
                                        #       Parent Loop BB4_3 Depth=3
                                        # =>      This Inner Loop Header: Depth=4
	cmpl	$2, %ebp
	ja	.LBB4_7
# %bb.5:                                #   in Loop: Header=BB4_4 Depth=4
	leal	(%rdi,%r15), %r12d
	cmpl	$2, %r12d
	ja	.LBB4_7
# %bb.6:                                #   in Loop: Header=BB4_4 Depth=4
	movsd	(%r11,%r15,8), %xmm0            # xmm0 = mem[0],zero
	mulsd	b+24(,%r14,8), %xmm0
	addsd	cref(,%r10,8), %xmm0
	movsd	%xmm0, cref(,%r10,8)
.LBB4_7:                                #   in Loop: Header=BB4_4 Depth=4
	incq	%r15
	cmpq	$3, %r15
	jne	.LBB4_4
# %bb.8:                                #   in Loop: Header=BB4_3 Depth=3
	incq	%rbx
	addq	$32768, %r11                    # imm = 0x8000
	cmpq	$3, %rbx
	jne	.LBB4_3
# %bb.9:                                #   in Loop: Header=BB4_2 Depth=2
	incq	%r9
	addq	$8, %r8
	incq	%rdi
	cmpq	$4096, %r9                      # imm = 0x1000
	jne	.LBB4_2
# %bb.10:                               #   in Loop: Header=BB4_1 Depth=1
	incq	%rcx
	addq	$32768, %rax                    # imm = 0x8000
	cmpq	$4096, %rcx                     # imm = 0x1000
	jne	.LBB4_1
# %bb.11:
	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_end4:
	.size	_Z3refv, .Lfunc_end4-_Z3refv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z7compareiPdS_
.LCPI5_0:
	.quad	0x8000000000000000              # double -0
	.quad	0x8000000000000000              # double -0
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI5_1:
	.quad	0x3f847ae147ae147b              # double 0.01
	.text
	.globl	_Z7compareiPdS_
	.type	_Z7compareiPdS_,@function
_Z7compareiPdS_:                        # @_Z7compareiPdS_
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jle	.LBB5_8
# %bb.1:                                # %.lr.ph.preheader
	movl	%edi, %ecx
	xorpd	%xmm1, %xmm1
	xorl	%edi, %edi
	movapd	.LCPI5_0(%rip), %xmm0           # xmm0 = [-0.0E+0,-0.0E+0]
	movsd	.LCPI5_1(%rip), %xmm2           # xmm2 = mem[0],zero
	xorl	%eax, %eax
.LBB5_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movsd	(%rsi,%rdi,8), %xmm4            # xmm4 = mem[0],zero
	subsd	(%rdx,%rdi,8), %xmm4
	movapd	%xmm4, %xmm3
	xorpd	%xmm0, %xmm3
	maxsd	%xmm4, %xmm3
	ucomisd	%xmm2, %xmm3
	jbe	.LBB5_5
# %bb.3:                                #   in Loop: Header=BB5_2 Depth=1
	incl	%eax
	ucomisd	%xmm1, %xmm3
	jbe	.LBB5_5
# %bb.4:                                #   in Loop: Header=BB5_2 Depth=1
	movapd	%xmm3, %xmm1
.LBB5_5:                                #   in Loop: Header=BB5_2 Depth=1
	incq	%rdi
	cmpq	%rdi, %rcx
	jne	.LBB5_2
# %bb.6:                                # %._crit_edge
	testl	%eax, %eax
	jle	.LBB5_8
# %bb.7:
	movsd	.LCPI5_1(%rip), %xmm0           # xmm0 = mem[0],zero
	movl	$.L.str.8, %edi
	movl	%eax, %esi
	movb	$2, %al
	jmp	printf                          # TAILCALL
.LBB5_8:                                # %._crit_edge.thread
	movl	$.Lstr.3, %edi
	jmp	puts@PLT                        # TAILCALL
.Lfunc_end5:
	.size	_Z7compareiPdS_, .Lfunc_end5-_Z7compareiPdS_
	.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:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z17ConvolutionKernelPdS_i, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$0, 8(%rsp)
	movl	$1, (%rsp)
	movl	$Mc, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movl	$72, %r9d
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	callq	__hipRegisterVar
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB7_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	Mc,@object                      # @Mc
	.local	Mc
	.comm	Mc,72,16
	.type	_Z17ConvolutionKernelPdS_i,@object # @_Z17ConvolutionKernelPdS_i
	.section	.rodata,"a",@progbits
	.globl	_Z17ConvolutionKernelPdS_i
	.p2align	3, 0x0
_Z17ConvolutionKernelPdS_i:
	.quad	_Z32__device_stub__ConvolutionKernelPdS_i
	.size	_Z17ConvolutionKernelPdS_i, 8

	.type	a,@object                       # @a
	.bss
	.globl	a
	.p2align	4, 0x0
a:
	.zero	134217728
	.size	a, 134217728

	.type	b,@object                       # @b
	.globl	b
	.p2align	4, 0x0
b:
	.zero	72
	.size	b, 72

	.type	c,@object                       # @c
	.globl	c
	.p2align	4, 0x0
c:
	.zero	134217728
	.size	c, 134217728

	.type	cref,@object                    # @cref
	.globl	cref
	.p2align	4, 0x0
cref:
	.zero	134217728
	.size	cref, 134217728

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"dev_prop.totalConstMem = %lu\n"
	.size	.L.str, 30

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Input Size = %dx%d\n"
	.size	.L.str.1, 20

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Mask size  = %dx%d\n"
	.size	.L.str.2, 20

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Seq: Approx GFLOPS: %.6f ; Time = %.6f sec; \n"
	.size	.L.str.3, 46

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"GPU: Approx GFLOPS: %.6f ; Time = %.6f sec; \n"
	.size	.L.str.5, 46

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"%d Diffs found over threshold %f; Max Diff = %f\n"
	.size	.L.str.8, 49

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Error return from gettimeofday: %d"
	.size	.L.str.10, 35

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z17ConvolutionKernelPdS_i"
	.size	.L__unnamed_1, 27

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"Mc"
	.size	.L__unnamed_2, 3

	.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	"Correctness Check for GPU solution:"
	.size	.Lstr, 36

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Correct!"
	.size	.Lstr.1, 9

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Error return for test_kernel"
	.size	.Lstr.2, 29

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"No differences found between reference and test versions"
	.size	.Lstr.3, 57

	.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 _Z32__device_stub__ConvolutionKernelPdS_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym Mc
	.addrsig_sym _Z17ConvolutionKernelPdS_i
	.addrsig_sym a
	.addrsig_sym b
	.addrsig_sym c
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z17ConvolutionKernelPdS_i ; -- Begin function _Z17ConvolutionKernelPdS_i
	.globl	_Z17ConvolutionKernelPdS_i
	.p2align	8
	.type	_Z17ConvolutionKernelPdS_i,@function
_Z17ConvolutionKernelPdS_i:             ; @_Z17ConvolutionKernelPdS_i
; %bb.0:
	s_clause 0x1
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b32 s8, s[0:1], 0x10
	v_bfe_u32 v3, v0, 10, 10
	v_dual_mov_b32 v5, 0 :: v_dual_and_b32 v4, 0x3ff, v0
	s_mov_b32 s1, exec_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_lshl_add_u32 v1, s15, 2, v3
	v_lshl_add_u32 v0, s14, 2, v4
	s_delay_alu instid0(VALU_DEP_1)
	v_max_i32_e32 v2, v1, v0
	v_cmpx_lt_i32_e32 0, v1
	s_cbranch_execz .LBB0_4
; %bb.1:
	v_cmp_lt_i32_e32 vcc_lo, 0, v0
	s_waitcnt lgkmcnt(0)
	v_cmp_ge_i32_e64 s0, s8, v2
	v_mov_b32_e32 v5, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, s0, vcc_lo
	s_and_saveexec_b32 s0, s2
	s_cbranch_execz .LBB0_3
; %bb.2:
	v_add_nc_u32_e32 v5, -1, v1
	v_ashrrev_i32_e32 v6, 31, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v5, v5, s8
	v_ashrrev_i32_e32 v7, 31, v5
	v_add_co_u32 v5, vcc_lo, v0, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_ci_u32_e32 v6, vcc_lo, v6, v7, vcc_lo
	v_lshlrev_b64 v[5:6], 3, v[5:6]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v5, vcc_lo, s4, v5
	v_add_co_ci_u32_e32 v6, vcc_lo, s5, v6, vcc_lo
	global_load_b64 v[5:6], v[5:6], off offset:-8
	s_waitcnt vmcnt(0)
	v_cvt_f32_f64_e32 v5, v[5:6]
.LBB0_3:                                ; %Flow80
	s_or_b32 exec_lo, exec_lo, s0
.LBB0_4:
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(VALU_DEP_2)
	s_or_b32 exec_lo, exec_lo, s1
	v_lshlrev_b32_e32 v6, 2, v4
	v_or_b32_e32 v4, v4, v3
	s_mov_b32 s0, exec_lo
	v_lshl_add_u32 v3, v3, 4, v6
	ds_store_b32 v3, v5
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_cmpx_gt_u32_e32 4, v4
	s_cbranch_execz .LBB0_11
; %bb.5:                                ; %.preheader.preheader
	v_mov_b32_e32 v4, 0
	s_mov_b32 s4, 0
	s_getpc_b64 s[0:1]
	s_add_u32 s0, s0, Mc@rel32@lo+4
	s_addc_u32 s1, s1, Mc@rel32@hi+12
.LBB0_6:                                ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_7 Depth 2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mov_b64 s[2:3], s[0:1]
	s_mov_b32 s5, 0
.LBB0_7:                                ;   Parent Loop BB0_6 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(SALU_CYCLE_1)
	v_add_nc_u32_e32 v5, s5, v3
	s_load_b64 s[10:11], s[2:3], 0x0
	s_add_i32 s5, s5, 4
	s_add_u32 s2, s2, 8
	s_addc_u32 s3, s3, 0
	ds_load_b32 v6, v5
	v_cvt_f64_f32_e32 v[4:5], v4
	s_cmp_eq_u32 s5, 12
	s_waitcnt lgkmcnt(0)
	v_cvt_f64_f32_e32 v[6:7], v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[4:5], s[10:11], v[6:7], v[4:5]
	v_cvt_f32_f64_e32 v4, v[4:5]
	s_cbranch_scc0 .LBB0_7
; %bb.8:                                ;   in Loop: Header=BB0_6 Depth=1
	s_add_i32 s4, s4, 1
	v_add_nc_u32_e32 v3, 16, v3
	s_add_u32 s0, s0, 24
	s_addc_u32 s1, s1, 0
	s_cmp_eq_u32 s4, 3
	s_cbranch_scc0 .LBB0_6
; %bb.9:
	v_cmp_gt_i32_e32 vcc_lo, s8, v2
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_11
; %bb.10:
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_cvt_f64_f32_e32 v[2:3], v4
	v_mad_u64_u32 v[4:5], null, v1, s8, v[0:1]
	v_ashrrev_i32_e32 v5, 31, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 3, v[4:5]
	v_add_co_u32 v0, vcc_lo, s6, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v1, vcc_lo
	global_store_b64 v[0:1], v[2:3], off
.LBB0_11:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z17ConvolutionKernelPdS_i
		.amdhsa_group_segment_fixed_size 64
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 20
		.amdhsa_user_sgpr_count 14
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 8
		.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	_Z17ConvolutionKernelPdS_i, .Lfunc_end0-_Z17ConvolutionKernelPdS_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 476
; NumSgprs: 18
; NumVgprs: 8
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 64 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 8
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.protected	Mc                      ; @Mc
	.type	Mc,@object
	.section	.bss,"aw",@nobits
	.globl	Mc
	.p2align	4, 0x0
Mc:
	.zero	72
	.size	Mc, 72

	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.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 Mc
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 64
    .kernarg_segment_align: 8
    .kernarg_segment_size: 20
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z17ConvolutionKernelPdS_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z17ConvolutionKernelPdS_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     8
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
