#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <cuda_runtime.h>
#include <sys/time.h>
#include <cuda.h>



/* Problem size */
#define NI 4096
#define NJ 4096

void Convolution(double* A, double* B)
{
	int i, j;
	double c11, c12, c13, c21, c22, c23, c31, c32, c33;

	c11 = +0.2;  c21 = +0.5;  c31 = -0.8;
	c12 = -0.3;  c22 = +0.6;  c32 = -0.9;
	c13 = +0.4;  c23 = +0.7;  c33 = +0.10;


	for (i = 1; i < NI - 1; ++i) {
		for (j = 1; j < NJ - 1; ++j) {
			B[i*NJ + j] = c11 * A[(i - 1)*NJ + (j - 1)]  +  c12 * A[(i + 0)*NJ + (j - 1)]  +  c13 * A[(i + 1)*NJ + (j - 1)]
				    + c21 * A[(i - 1)*NJ + (j + 0)]  +  c22 * A[(i + 0)*NJ + (j + 0)]  +  c23 * A[(i + 1)*NJ + (j + 0)]
				    + c31 * A[(i - 1)*NJ + (j + 1)]  +  c32 * A[(i + 0)*NJ + (j + 1)]  +  c33 * A[(i + 1)*NJ + (j + 1)];
		}
	}
}

__global__ void convolutionOnGPU(int nx, int ny, double *MatA, double *MatB)
{
unsigned	int j = blockIdx.x * blockDim.x + threadIdx.x;
unsigned	int i = blockIdx.y * blockDim.y + threadIdx.y;

	double c11, c12, c13, c21, c22, c23, c31, c32, c33;

	c11 = +0.2;  c21 = +0.5;  c31 = -0.8;
	c12 = -0.3;  c22 = +0.6;  c32 = -0.9;
	c13 = +0.4;  c23 = +0.7;  c33 = +0.10;

	if ((i < NI-1) && (j < NJ-1) && (i > 0) && (j > 0))
	{
		MatB [i * NJ + j] =  c11 * MatA[(i - 1) * NJ + (j - 1)]  + c21 * MatA[(i - 1) * NJ + (j + 0)] + c31 * MatA[(i - 1) * NJ + (j + 1)]
			+ c12 * MatA[(i + 0) * NJ + (j - 1)]  + c22 * MatA[(i + 0) * NJ + (j + 0)] +  c32 * MatA[(i + 0) * NJ + (j + 1)]
			+ c13 * MatA[(i + 1) * NJ + (j - 1)]  + c23 * MatA[(i + 1) * NJ + (j + 0)] +  c33 * MatA[(i + 1) * NJ + (j + 1)];
	}
}


void checkResult(double *hostRef, double *gpuRef, const int N)
{
    bool match = 1;

    for (int i = 0; i < N; i++)
    {
        if (hostRef[i] != gpuRef[i])
        {
            match = 0;
            break;
        }
    }

    if (match)
        printf("Arrays match.\n\n");
    else
        printf("Arrays do not match.\n\n");
}

void init(double* A)
{
	int i, j;

	for (i = 0; i < NI; ++i) {
		for (j = 0; j < NJ; ++j) {
			A[i*NJ + j] = (double)rand()/RAND_MAX;
        	}
    	}
}

int main(int argc, char *argv[])
{

 printf("2D convolution starting...\n");

 // set up device
 int dev = 0;
 cudaDeviceProp deviceProp;
 cudaGetDeviceProperties(&deviceProp, dev);
 printf("Device properties %d: %s\n", dev, deviceProp.name);
 cudaSetDevice(dev);

 struct timeval	cpu_start, cpu_end;

	// set up data size of matrix
 int nx = NI;
 int ny = NJ;
 int nxy = nx * ny;
 size_t nBytes = nxy * sizeof(double);
 printf("Matrix size: nx %d ny %d\n", nx, ny);

 // malloc host memory
 double		*A,*B,*gpuRef;

	A = (double*)malloc(nBytes);
	B = (double*)malloc(nBytes);
	gpuRef = (double*)malloc(nBytes);



	//initialize the arrays
	init(A);

	memset(gpuRef, 0, nBytes);


	gettimeofday(&cpu_start, NULL);
	Convolution(A, B);
	gettimeofday(&cpu_end, NULL);
	fprintf(stdout, "Convolution time on host: %0.6lf sec\n", ((cpu_end.tv_sec - cpu_start.tv_sec) * 1000000.0 + (cpu_end.tv_usec - cpu_start.tv_usec)) / 1000000.0);



	double *d_MatA,*d_MatB;
	// malloc device global memory
	cudaMalloc((void **)&d_MatA,nBytes);
	cudaMalloc((void **)&d_MatB,nBytes);


	// transfer data from host to device
	cudaMemcpy(d_MatA, A ,nBytes,cudaMemcpyHostToDevice);


	// invoke kernel at host side
	int dimx = 1024;
	int dimy = 2;
	dim3 block(dimx, dimy);
	dim3 grid((nx + block.x - 1) / block.x, (ny + block.y - 1) / block.y);
	printf("<<< grid (%d,%d) block (%d,%d)>>>\n", grid.x, grid.y, block.x,
				 block.y);

	gettimeofday(&cpu_start, NULL);
	convolutionOnGPU <<< grid,block >>> (nx, ny, d_MatA,d_MatB);
	gettimeofday(&cpu_end, NULL);

  cudaDeviceSynchronize();

	printf("Convolution time on gpu: %0.6lf sec\n",((cpu_end.tv_sec - cpu_start.tv_sec) * 1000000.0 + (cpu_end.tv_usec - cpu_start.tv_usec)) / 1000000.0);


	// check kernel error
  cudaGetLastError();

	// copy kernel result back to host side
	cudaMemcpy(gpuRef, d_MatB, nBytes, cudaMemcpyDeviceToHost);

	// free device global memory
	cudaFree(d_MatA);
  cudaFree(d_MatB);

	// check device results
	checkResult(B, gpuRef, nxy);

	// free host memory
	free(A);
	free(B);
	free(gpuRef);

	// reset device
	cudaDeviceReset();


	return (0);
}

	.file	"tmpxft_0039c3db_00000000-6_2D-Convolution.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2046:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2046:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z11ConvolutionPdS_
	.type	_Z11ConvolutionPdS_, @function
_Z11ConvolutionPdS_:
.LFB2040:
	.cfi_startproc
	endbr64
	movsd	.LC0(%rip), %xmm1
	movsd	.LC1(%rip), %xmm2
	xorl	%r8d, %r8d
	movl	$32768, %ecx
	movsd	.LC2(%rip), %xmm3
	movsd	.LC3(%rip), %xmm4
	movsd	.LC4(%rip), %xmm5
	movsd	.LC5(%rip), %xmm6
	movsd	.LC6(%rip), %xmm7
	movsd	.LC7(%rip), %xmm8
	movsd	.LC8(%rip), %xmm9
.L3:
	addl	$4096, %r8d
	movq	%rcx, %rax
	movl	$1, %edx
.L4:
	movsd	-32768(%rdi,%rax), %xmm0
	movsd	(%rdi,%rax), %xmm10
	incl	%edx
	mulsd	%xmm2, %xmm10
	mulsd	%xmm1, %xmm0
	addsd	%xmm10, %xmm0
	movsd	32768(%rdi,%rax), %xmm10
	mulsd	%xmm3, %xmm10
	addsd	%xmm10, %xmm0
	movsd	-32760(%rdi,%rax), %xmm10
	mulsd	%xmm4, %xmm10
	addsd	%xmm10, %xmm0
	movsd	8(%rdi,%rax), %xmm10
	mulsd	%xmm5, %xmm10
	addsd	%xmm10, %xmm0
	movsd	32776(%rdi,%rax), %xmm10
	mulsd	%xmm6, %xmm10
	addsd	%xmm10, %xmm0
	movsd	-32752(%rdi,%rax), %xmm10
	mulsd	%xmm7, %xmm10
	addsd	%xmm10, %xmm0
	movsd	16(%rdi,%rax), %xmm10
	mulsd	%xmm8, %xmm10
	addsd	%xmm10, %xmm0
	movsd	32784(%rdi,%rax), %xmm10
	mulsd	%xmm9, %xmm10
	addsd	%xmm10, %xmm0
	movsd	%xmm0, 8(%rsi,%rax)
	addq	$8, %rax
	cmpl	$4095, %edx
	jne	.L4
	addq	$32768, %rcx
	cmpl	$16769024, %r8d
	jne	.L3
	ret
	.cfi_endproc
.LFE2040:
	.size	_Z11ConvolutionPdS_, .-_Z11ConvolutionPdS_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC9:
	.string	"Arrays do not match.\n\n"
.LC10:
	.string	"Arrays match.\n\n"
	.text
	.globl	_Z11checkResultPdS_i
	.type	_Z11checkResultPdS_i, @function
_Z11checkResultPdS_i:
.LFB2041:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L9:
	cmpl	%eax, %edx
	jle	.L16
	movsd	(%rdi,%rax,8), %xmm0
	incq	%rax
	ucomisd	-8(%rsi,%rax,8), %xmm0
	jp	.L10
	je	.L9
.L10:
	leaq	.LC9(%rip), %rsi
	jmp	.L15
.L16:
	leaq	.LC10(%rip), %rsi
.L15:
	movl	$2, %edi
	xorl	%eax, %eax
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2041:
	.size	_Z11checkResultPdS_i, .-_Z11checkResultPdS_i
	.globl	_Z4initPd
	.type	_Z4initPd, @function
_Z4initPd:
.LFB2042:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	pushq	%rdx
	.cfi_def_cfa_offset 48
.L18:
	movq	%rbp, %r12
	xorl	%ebx, %ebx
	salq	$15, %r12
	addq	%r13, %r12
.L19:
	call	rand@PLT
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC11(%rip), %xmm0
	movsd	%xmm0, (%r12,%rbx,8)
	incq	%rbx
	cmpq	$4096, %rbx
	jne	.L19
	incq	%rbp
	cmpq	$4096, %rbp
	jne	.L18
	popq	%rax
	.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
.LFE2042:
	.size	_Z4initPd, .-_Z4initPd
	.globl	_Z40__device_stub__Z16convolutionOnGPUiiPdS_iiPdS_
	.type	_Z40__device_stub__Z16convolutionOnGPUiiPdS_iiPdS_, @function
_Z40__device_stub__Z16convolutionOnGPUiiPdS_iiPdS_:
.LFB2068:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movl	%edi, 28(%rsp)
	leaq	56(%rsp), %rdi
	movl	%esi, 24(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 16(%rsp)
	leaq	40(%rsp), %rdx
	movq	%rcx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	28(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L23
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z16convolutionOnGPUiiPdS_(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L23:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2068:
	.size	_Z40__device_stub__Z16convolutionOnGPUiiPdS_iiPdS_, .-_Z40__device_stub__Z16convolutionOnGPUiiPdS_iiPdS_
	.globl	_Z16convolutionOnGPUiiPdS_
	.type	_Z16convolutionOnGPUiiPdS_, @function
_Z16convolutionOnGPUiiPdS_:
.LFB2069:
	.cfi_startproc
	endbr64
	jmp	_Z40__device_stub__Z16convolutionOnGPUiiPdS_iiPdS_
	.cfi_endproc
.LFE2069:
	.size	_Z16convolutionOnGPUiiPdS_, .-_Z16convolutionOnGPUiiPdS_
	.section	.rodata.str1.1
.LC12:
	.string	"2D convolution starting...\n"
.LC13:
	.string	"Device properties %d: %s\n"
.LC14:
	.string	"Matrix size: nx %d ny %d\n"
.LC16:
	.string	"Convolution time on host: %0.6lf sec\n"
.LC17:
	.string	"<<< grid (%d,%d) block (%d,%d)>>>\n"
.LC18:
	.string	"Convolution time on gpu: %0.6lf sec\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2043:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$1120, %rsp
	.cfi_def_cfa_offset 1168
	movq	%fs:40, %rax
	movq	%rax, 1112(%rsp)
	xorl	%eax, %eax
	leaq	80(%rsp), %rbx
	leaq	48(%rsp), %r14
	leaq	64(%rsp), %r13
	call	__printf_chk@PLT
	xorl	%esi, %esi
	movq	%rbx, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movq	%rbx, %rcx
	xorl	%edx, %edx
	movl	$2, %edi
	leaq	.LC13(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	cudaSetDevice@PLT
	movl	$4096, %edx
	movl	$4096, %ecx
	xorl	%eax, %eax
	leaq	.LC14(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	$134217728, %edi
	call	malloc@PLT
	movl	$134217728, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movl	$134217728, %edi
	movq	%rax, %r12
	call	malloc@PLT
	movq	%rbp, %rdi
	movq	%rax, %rbx
	call	_Z4initPd
	xorl	%eax, %eax
	movl	$33554432, %ecx
	movq	%rbx, %rdi
	rep stosl
	xorl	%esi, %esi
	movq	%r14, %rdi
	call	gettimeofday@PLT
	movq	%r12, %rsi
	movq	%rbp, %rdi
	call	_Z11ConvolutionPdS_
	xorl	%esi, %esi
	movq	%r13, %rdi
	call	gettimeofday@PLT
	movq	64(%rsp), %rax
	subq	48(%rsp), %rax
	leaq	.LC16(%rip), %rdx
	cvtsi2sdq	%rax, %xmm0
	movq	72(%rsp), %rax
	subq	56(%rsp), %rax
	movl	$2, %esi
	mulsd	.LC15(%rip), %xmm0
	cvtsi2sdq	%rax, %xmm1
	movb	$1, %al
	movq	stdout(%rip), %rdi
	addsd	%xmm1, %xmm0
	divsd	.LC15(%rip), %xmm0
	call	__fprintf_chk@PLT
	leaq	8(%rsp), %rdi
	movl	$134217728, %esi
	call	cudaMalloc@PLT
	leaq	16(%rsp), %rdi
	movl	$134217728, %esi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	movl	$134217728, %edx
	call	cudaMemcpy@PLT
	movl	$2, %r9d
	movl	$1024, %r8d
	movl	$2048, %ecx
	movl	$4, %edx
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%esi, %esi
	movq	%r14, %rdi
	call	gettimeofday@PLT
	movl	$8388609, %edx
	xorl	%r9d, %r9d
	movl	$1, %ecx
	xorl	%r8d, %r8d
	salq	$10, %rdx
	movl	$1, %esi
	movabsq	$8796093022212, %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L29
	movq	16(%rsp), %rcx
	movq	8(%rsp), %rdx
	movl	$4096, %esi
	movl	$4096, %edi
	call	_Z40__device_stub__Z16convolutionOnGPUiiPdS_iiPdS_
.L29:
	xorl	%esi, %esi
	movq	%r13, %rdi
	call	gettimeofday@PLT
	call	cudaDeviceSynchronize@PLT
	movq	64(%rsp), %rax
	subq	48(%rsp), %rax
	leaq	.LC18(%rip), %rsi
	cvtsi2sdq	%rax, %xmm0
	movq	72(%rsp), %rax
	subq	56(%rsp), %rax
	movl	$2, %edi
	mulsd	.LC15(%rip), %xmm0
	cvtsi2sdq	%rax, %xmm1
	movb	$1, %al
	addsd	%xmm1, %xmm0
	divsd	.LC15(%rip), %xmm0
	call	__printf_chk@PLT
	call	cudaGetLastError@PLT
	movq	16(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdi
	movl	$134217728, %edx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movl	$16777216, %edx
	movq	%rbx, %rsi
	movq	%r12, %rdi
	call	_Z11checkResultPdS_i
	movq	%rbp, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	call	cudaDeviceReset@PLT
	movq	1112(%rsp), %rax
	subq	%fs:40, %rax
	je	.L30
	call	__stack_chk_fail@PLT
.L30:
	addq	$1120, %rsp
	.cfi_def_cfa_offset 48
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2043:
	.size	main, .-main
	.section	.rodata.str1.1
.LC19:
	.string	"_Z16convolutionOnGPUiiPdS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2071:
	.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	.LC19(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z16convolutionOnGPUiiPdS_(%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
.LFE2071:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	-1717986918
	.long	1070176665
	.align 8
.LC1:
	.long	858993459
	.long	-1076677837
	.align 8
.LC2:
	.long	-1717986918
	.long	1071225241
	.align 8
.LC3:
	.long	0
	.long	1071644672
	.align 8
.LC4:
	.long	858993459
	.long	1071854387
	.align 8
.LC5:
	.long	1717986918
	.long	1072064102
	.align 8
.LC6:
	.long	-1717986918
	.long	-1075209831
	.align 8
.LC7:
	.long	-858993459
	.long	-1075000116
	.align 8
.LC8:
	.long	-1717986918
	.long	1069128089
	.align 8
.LC11:
	.long	-4194304
	.long	1105199103
	.align 8
.LC15:
	.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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <hip/hip_runtime.h>
#include <sys/time.h>




/* Problem size */
#define NI 4096
#define NJ 4096

void Convolution(double* A, double* B)
{
	int i, j;
	double c11, c12, c13, c21, c22, c23, c31, c32, c33;

	c11 = +0.2;  c21 = +0.5;  c31 = -0.8;
	c12 = -0.3;  c22 = +0.6;  c32 = -0.9;
	c13 = +0.4;  c23 = +0.7;  c33 = +0.10;


	for (i = 1; i < NI - 1; ++i) {
		for (j = 1; j < NJ - 1; ++j) {
			B[i*NJ + j] = c11 * A[(i - 1)*NJ + (j - 1)]  +  c12 * A[(i + 0)*NJ + (j - 1)]  +  c13 * A[(i + 1)*NJ + (j - 1)]
				    + c21 * A[(i - 1)*NJ + (j + 0)]  +  c22 * A[(i + 0)*NJ + (j + 0)]  +  c23 * A[(i + 1)*NJ + (j + 0)]
				    + c31 * A[(i - 1)*NJ + (j + 1)]  +  c32 * A[(i + 0)*NJ + (j + 1)]  +  c33 * A[(i + 1)*NJ + (j + 1)];
		}
	}
}

__global__ void convolutionOnGPU(int nx, int ny, double *MatA, double *MatB)
{
unsigned	int j = blockIdx.x * blockDim.x + threadIdx.x;
unsigned	int i = blockIdx.y * blockDim.y + threadIdx.y;

	double c11, c12, c13, c21, c22, c23, c31, c32, c33;

	c11 = +0.2;  c21 = +0.5;  c31 = -0.8;
	c12 = -0.3;  c22 = +0.6;  c32 = -0.9;
	c13 = +0.4;  c23 = +0.7;  c33 = +0.10;

	if ((i < NI-1) && (j < NJ-1) && (i > 0) && (j > 0))
	{
		MatB [i * NJ + j] =  c11 * MatA[(i - 1) * NJ + (j - 1)]  + c21 * MatA[(i - 1) * NJ + (j + 0)] + c31 * MatA[(i - 1) * NJ + (j + 1)]
			+ c12 * MatA[(i + 0) * NJ + (j - 1)]  + c22 * MatA[(i + 0) * NJ + (j + 0)] +  c32 * MatA[(i + 0) * NJ + (j + 1)]
			+ c13 * MatA[(i + 1) * NJ + (j - 1)]  + c23 * MatA[(i + 1) * NJ + (j + 0)] +  c33 * MatA[(i + 1) * NJ + (j + 1)];
	}
}


void checkResult(double *hostRef, double *gpuRef, const int N)
{
    bool match = 1;

    for (int i = 0; i < N; i++)
    {
        if (hostRef[i] != gpuRef[i])
        {
            match = 0;
            break;
        }
    }

    if (match)
        printf("Arrays match.\n\n");
    else
        printf("Arrays do not match.\n\n");
}

void init(double* A)
{
	int i, j;

	for (i = 0; i < NI; ++i) {
		for (j = 0; j < NJ; ++j) {
			A[i*NJ + j] = (double)rand()/RAND_MAX;
        	}
    	}
}

int main(int argc, char *argv[])
{

 printf("2D convolution starting...\n");

 // set up device
 int dev = 0;
 hipDeviceProp_t deviceProp;
 hipGetDeviceProperties(&deviceProp, dev);
 printf("Device properties %d: %s\n", dev, deviceProp.name);
 hipSetDevice(dev);

 struct timeval	cpu_start, cpu_end;

	// set up data size of matrix
 int nx = NI;
 int ny = NJ;
 int nxy = nx * ny;
 size_t nBytes = nxy * sizeof(double);
 printf("Matrix size: nx %d ny %d\n", nx, ny);

 // malloc host memory
 double		*A,*B,*gpuRef;

	A = (double*)malloc(nBytes);
	B = (double*)malloc(nBytes);
	gpuRef = (double*)malloc(nBytes);



	//initialize the arrays
	init(A);

	memset(gpuRef, 0, nBytes);


	gettimeofday(&cpu_start, NULL);
	Convolution(A, B);
	gettimeofday(&cpu_end, NULL);
	fprintf(stdout, "Convolution time on host: %0.6lf sec\n", ((cpu_end.tv_sec - cpu_start.tv_sec) * 1000000.0 + (cpu_end.tv_usec - cpu_start.tv_usec)) / 1000000.0);



	double *d_MatA,*d_MatB;
	// malloc device global memory
	hipMalloc((void **)&d_MatA,nBytes);
	hipMalloc((void **)&d_MatB,nBytes);


	// transfer data from host to device
	hipMemcpy(d_MatA, A ,nBytes,hipMemcpyHostToDevice);


	// invoke kernel at host side
	int dimx = 1024;
	int dimy = 2;
	dim3 block(dimx, dimy);
	dim3 grid((nx + block.x - 1) / block.x, (ny + block.y - 1) / block.y);
	printf("<<< grid (%d,%d) block (%d,%d)>>>\n", grid.x, grid.y, block.x,
				 block.y);

	gettimeofday(&cpu_start, NULL);
	convolutionOnGPU <<< grid,block >>> (nx, ny, d_MatA,d_MatB);
	gettimeofday(&cpu_end, NULL);

  hipDeviceSynchronize();

	printf("Convolution time on gpu: %0.6lf sec\n",((cpu_end.tv_sec - cpu_start.tv_sec) * 1000000.0 + (cpu_end.tv_usec - cpu_start.tv_usec)) / 1000000.0);


	// check kernel error
  hipGetLastError();

	// copy kernel result back to host side
	hipMemcpy(gpuRef, d_MatB, nBytes, hipMemcpyDeviceToHost);

	// free device global memory
	hipFree(d_MatA);
  hipFree(d_MatB);

	// check device results
	checkResult(B, gpuRef, nxy);

	// free host memory
	free(A);
	free(B);
	free(gpuRef);

	// reset device
	hipDeviceReset();


	return (0);
}

	.text
	.file	"2D-Convolution.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z11ConvolutionPdS_
.LCPI0_0:
	.quad	0x3fc999999999999a              # double 0.20000000000000001
.LCPI0_1:
	.quad	0xbfd3333333333333              # double -0.29999999999999999
.LCPI0_2:
	.quad	0x3fd999999999999a              # double 0.40000000000000002
.LCPI0_3:
	.quad	0x3fe0000000000000              # double 0.5
.LCPI0_4:
	.quad	0x3fe3333333333333              # double 0.59999999999999998
.LCPI0_5:
	.quad	0x3fe6666666666666              # double 0.69999999999999996
.LCPI0_6:
	.quad	0xbfe999999999999a              # double -0.80000000000000004
.LCPI0_7:
	.quad	0xbfeccccccccccccd              # double -0.90000000000000002
.LCPI0_8:
	.quad	0x3fb999999999999a              # double 0.10000000000000001
	.text
	.globl	_Z11ConvolutionPdS_
	.type	_Z11ConvolutionPdS_,@function
_Z11ConvolutionPdS_:                    # @_Z11ConvolutionPdS_
	.cfi_startproc
# %bb.0:
	addq	$65552, %rdi                    # imm = 0x10010
	addq	$32776, %rsi                    # imm = 0x8008
	movl	$1, %eax
	movsd	.LCPI0_0(%rip), %xmm0           # xmm0 = mem[0],zero
	movsd	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero
	movsd	.LCPI0_2(%rip), %xmm2           # xmm2 = mem[0],zero
	movsd	.LCPI0_3(%rip), %xmm3           # xmm3 = mem[0],zero
	movsd	.LCPI0_4(%rip), %xmm4           # xmm4 = mem[0],zero
	movsd	.LCPI0_5(%rip), %xmm5           # xmm5 = mem[0],zero
	movsd	.LCPI0_6(%rip), %xmm6           # xmm6 = mem[0],zero
	movsd	.LCPI0_7(%rip), %xmm7           # xmm7 = mem[0],zero
	movsd	.LCPI0_8(%rip), %xmm8           # xmm8 = mem[0],zero
	movl	$32768, %ecx                    # imm = 0x8000
.LBB0_1:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_2 Depth 2
	xorl	%edx, %edx
.LBB0_2:                                #   Parent Loop BB0_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movsd	-65552(%rdi,%rdx,8), %xmm9      # xmm9 = mem[0],zero
	mulsd	%xmm0, %xmm9
	movsd	-32784(%rdi,%rdx,8), %xmm10     # xmm10 = mem[0],zero
	mulsd	%xmm1, %xmm10
	addsd	%xmm9, %xmm10
	movsd	-16(%rdi,%rdx,8), %xmm9         # xmm9 = mem[0],zero
	mulsd	%xmm2, %xmm9
	addsd	%xmm10, %xmm9
	movsd	-65544(%rdi,%rdx,8), %xmm10     # xmm10 = mem[0],zero
	mulsd	%xmm3, %xmm10
	addsd	%xmm9, %xmm10
	movsd	-32776(%rdi,%rdx,8), %xmm9      # xmm9 = mem[0],zero
	mulsd	%xmm4, %xmm9
	addsd	%xmm10, %xmm9
	movsd	-8(%rdi,%rdx,8), %xmm10         # xmm10 = mem[0],zero
	mulsd	%xmm5, %xmm10
	addsd	%xmm9, %xmm10
	movsd	-65536(%rdi,%rdx,8), %xmm9      # xmm9 = mem[0],zero
	mulsd	%xmm6, %xmm9
	addsd	%xmm10, %xmm9
	movsd	-32768(%rdi,%rdx,8), %xmm10     # xmm10 = mem[0],zero
	mulsd	%xmm7, %xmm10
	addsd	%xmm9, %xmm10
	movsd	(%rdi,%rdx,8), %xmm9            # xmm9 = mem[0],zero
	mulsd	%xmm8, %xmm9
	addsd	%xmm10, %xmm9
	movsd	%xmm9, (%rsi,%rdx,8)
	incq	%rdx
	cmpq	$4094, %rdx                     # imm = 0xFFE
	jne	.LBB0_2
# %bb.3:                                #   in Loop: Header=BB0_1 Depth=1
	incq	%rax
	addq	%rcx, %rdi
	addq	%rcx, %rsi
	cmpq	$4095, %rax                     # imm = 0xFFF
	jne	.LBB0_1
# %bb.4:
	retq
.Lfunc_end0:
	.size	_Z11ConvolutionPdS_, .Lfunc_end0-_Z11ConvolutionPdS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z31__device_stub__convolutionOnGPUiiPdS_ # -- Begin function _Z31__device_stub__convolutionOnGPUiiPdS_
	.type	_Z31__device_stub__convolutionOnGPUiiPdS_,@function
_Z31__device_stub__convolutionOnGPUiiPdS_: # @_Z31__device_stub__convolutionOnGPUiiPdS_
	.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	12(%rsp), %rax
	movl	%edi, (%rax)
	leaq	8(%rsp), %rdi
	movl	%esi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%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	$_Z16convolutionOnGPUiiPdS_, %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_end1:
	.size	_Z31__device_stub__convolutionOnGPUiiPdS_, .Lfunc_end1-_Z31__device_stub__convolutionOnGPUiiPdS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z11checkResultPdS_i            # -- Begin function _Z11checkResultPdS_i
	.type	_Z11checkResultPdS_i,@function
_Z11checkResultPdS_i:                   # @_Z11checkResultPdS_i
	.cfi_startproc
# %bb.0:
	movq	%rdi, %rax
	movl	$.Lstr.1, %edi
	testl	%edx, %edx
	jle	puts@PLT                        # TAILCALL
# %bb.1:                                # %.lr.ph.preheader
	movl	%edx, %ecx
	xorl	%edx, %edx
	jmp	.LBB2_3
.LBB2_2:                                #   in Loop: Header=BB2_3 Depth=1
	incq	%rdx
	cmpq	%rdx, %rcx
	je	puts@PLT                        # TAILCALL
.LBB2_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movsd	(%rax,%rdx,8), %xmm0            # xmm0 = mem[0],zero
	ucomisd	(%rsi,%rdx,8), %xmm0
	jne	.LBB2_4
	jnp	.LBB2_2
.LBB2_4:
	movl	$.Lstr, %edi
	jmp	puts@PLT                        # TAILCALL
.Lfunc_end2:
	.size	_Z11checkResultPdS_i, .Lfunc_end2-_Z11checkResultPdS_i
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z4initPd
.LCPI3_0:
	.quad	0x41dfffffffc00000              # double 2147483647
	.text
	.globl	_Z4initPd
	.type	_Z4initPd,@function
_Z4initPd:                              # @_Z4initPd
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	xorl	%r14d, %r14d
.LBB3_1:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_2 Depth 2
	xorl	%r15d, %r15d
.LBB3_2:                                #   Parent Loop BB3_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	movsd	.LCPI3_0(%rip), %xmm1           # xmm1 = mem[0],zero
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	%xmm1, %xmm0
	movsd	%xmm0, (%rbx,%r15,8)
	incq	%r15
	cmpq	$4096, %r15                     # imm = 0x1000
	jne	.LBB3_2
# %bb.3:                                #   in Loop: Header=BB3_1 Depth=1
	incq	%r14
	addq	$32768, %rbx                    # imm = 0x8000
	cmpq	$4096, %r14                     # imm = 0x1000
	jne	.LBB3_1
# %bb.4:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z4initPd, .Lfunc_end3-_Z4initPd
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI4_0:
	.quad	0x41dfffffffc00000              # double 2147483647
.LCPI4_1:
	.quad	0x412e848000000000              # double 1.0E+6
	.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	$1528, %rsp                     # imm = 0x5F8
	.cfi_def_cfa_offset 1584
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	leaq	56(%rsp), %rbx
	xorl	%r12d, %r12d
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	movl	$.L.str.3, %edi
	xorl	%esi, %esi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	printf
	xorl	%edi, %edi
	callq	hipSetDevice
	movl	$.L.str.4, %edi
	movl	$4096, %esi                     # imm = 0x1000
	movl	$4096, %edx                     # imm = 0x1000
	xorl	%eax, %eax
	callq	printf
	movl	$134217728, %edi                # imm = 0x8000000
	callq	malloc
	movq	%rax, %rbx
	movl	$134217728, %edi                # imm = 0x8000000
	callq	malloc
	movq	%rax, %r14
	movl	$134217728, %edi                # imm = 0x8000000
	callq	malloc
	movq	%rax, %r15
	movq	%rbx, %r13
.LBB4_1:                                # %.preheader.i
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_2 Depth 2
	xorl	%ebp, %ebp
.LBB4_2:                                #   Parent Loop BB4_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	movsd	.LCPI4_0(%rip), %xmm1           # xmm1 = mem[0],zero
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	%xmm1, %xmm0
	movsd	%xmm0, (%r13,%rbp,8)
	incq	%rbp
	cmpq	$4096, %rbp                     # imm = 0x1000
	jne	.LBB4_2
# %bb.3:                                #   in Loop: Header=BB4_1 Depth=1
	incq	%r12
	addq	$32768, %r13                    # imm = 0x8000
	cmpq	$4096, %r12                     # imm = 0x1000
	jne	.LBB4_1
# %bb.4:                                # %_Z4initPd.exit
	movl	$134217728, %edx                # imm = 0x8000000
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
	leaq	24(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	_Z11ConvolutionPdS_
	leaq	40(%rsp), %r13
	movq	%r13, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	stdout(%rip), %rdi
	movq	(%r13), %rax
	movq	8(%r13), %rcx
	subq	(%r12), %rax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	movsd	.LCPI4_1(%rip), %xmm2           # xmm2 = mem[0],zero
	mulsd	%xmm2, %xmm1
	subq	8(%r12), %rcx
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rcx, %xmm0
	addsd	%xmm1, %xmm0
	divsd	%xmm2, %xmm0
	movl	$.L.str.5, %esi
	movb	$1, %al
	callq	fprintf
	leaq	16(%rsp), %r13
	movl	$134217728, %esi                # imm = 0x8000000
	movq	%r13, %rdi
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movl	$134217728, %esi                # imm = 0x8000000
	callq	hipMalloc
	movq	(%r13), %rdi
	movl	$134217728, %edx                # imm = 0x8000000
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$.L.str.6, %edi
	movl	$4, %esi
	movl	$2048, %edx                     # imm = 0x800
	movl	$1024, %ecx                     # imm = 0x400
	movl	$2, %r8d
	xorl	%eax, %eax
	callq	printf
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movabsq	$8796093022212, %rdi            # imm = 0x80000000004
	movabsq	$8589935616, %rdx               # imm = 0x200000400
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_6
# %bb.5:
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rcx
	movl	$4096, %edi                     # imm = 0x1000
	movl	$4096, %esi                     # imm = 0x1000
	callq	_Z31__device_stub__convolutionOnGPUiiPdS_
.LBB4_6:
	xorl	%r13d, %r13d
	leaq	40(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	callq	hipDeviceSynchronize
	movq	(%r12), %rax
	subq	24(%rsp), %rax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	movq	8(%r12), %rax
	movsd	.LCPI4_1(%rip), %xmm2           # xmm2 = mem[0],zero
	mulsd	%xmm2, %xmm1
	subq	32(%rsp), %rax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	addsd	%xmm1, %xmm0
	divsd	%xmm2, %xmm0
	movl	$.L.str.7, %edi
	movb	$1, %al
	callq	printf
	callq	hipGetLastError
	movq	8(%rsp), %rsi
	movl	$134217728, %edx                # imm = 0x8000000
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
.LBB4_7:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movsd	(%r14,%r13,8), %xmm0            # xmm0 = mem[0],zero
	ucomisd	(%r15,%r13,8), %xmm0
	jne	.LBB4_10
	jp	.LBB4_10
# %bb.8:                                #   in Loop: Header=BB4_7 Depth=1
	incq	%r13
	cmpq	$16777216, %r13                 # imm = 0x1000000
	jne	.LBB4_7
# %bb.9:
	movl	$.Lstr.1, %edi
	jmp	.LBB4_11
.LBB4_10:
	movl	$.Lstr, %edi
.LBB4_11:                               # %_Z11checkResultPdS_i.exit
	callq	puts@PLT
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	callq	hipDeviceReset
	xorl	%eax, %eax
	addq	$1528, %rsp                     # imm = 0x5F8
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_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	$_Z16convolutionOnGPUiiPdS_, %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	_Z16convolutionOnGPUiiPdS_,@object # @_Z16convolutionOnGPUiiPdS_
	.section	.rodata,"a",@progbits
	.globl	_Z16convolutionOnGPUiiPdS_
	.p2align	3, 0x0
_Z16convolutionOnGPUiiPdS_:
	.quad	_Z31__device_stub__convolutionOnGPUiiPdS_
	.size	_Z16convolutionOnGPUiiPdS_, 8

	.type	.L.str.3,@object                # @.str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.3:
	.asciz	"Device properties %d: %s\n"
	.size	.L.str.3, 26

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Matrix size: nx %d ny %d\n"
	.size	.L.str.4, 26

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Convolution time on host: %0.6lf sec\n"
	.size	.L.str.5, 38

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"<<< grid (%d,%d) block (%d,%d)>>>\n"
	.size	.L.str.6, 35

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Convolution time on gpu: %0.6lf sec\n"
	.size	.L.str.7, 37

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

	.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	"Arrays do not match.\n"
	.size	.Lstr, 22

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Arrays match.\n"
	.size	.Lstr.1, 15

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"2D convolution starting..."
	.size	.Lstr.2, 27

	.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 _Z31__device_stub__convolutionOnGPUiiPdS_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z16convolutionOnGPUiiPdS_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z16convolutionOnGPUiiPdS_ ; -- Begin function _Z16convolutionOnGPUiiPdS_
	.globl	_Z16convolutionOnGPUiiPdS_
	.p2align	8
	.type	_Z16convolutionOnGPUiiPdS_,@function
_Z16convolutionOnGPUiiPdS_:             ; @_Z16convolutionOnGPUiiPdS_
; %bb.0:
	s_load_b32 s2, s[0:1], 0x24
	v_bfe_u32 v1, v0, 10, 10
	v_and_b32_e32 v4, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s3, s2, 16
	s_and_b32 s2, s2, 0xffff
	v_mad_u64_u32 v[2:3], null, s15, s3, v[1:2]
	v_mad_u64_u32 v[0:1], null, s14, s2, v[4:5]
	s_mov_b32 s2, exec_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v3, -1, v2
	v_add_nc_u32_e32 v1, -1, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_max_u32_e32 v3, v1, v3
	v_cmpx_gt_u32_e32 0xffe, v3
	s_cbranch_execz .LBB0_2
; %bb.1:
	v_dual_mov_b32 v3, 0 :: v_dual_lshlrev_b32 v18, 12, v2
	s_load_b128 s[0:3], s[0:1], 0x8
	s_mov_b32 s5, 0xbfd33333
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v8, 0xfffff000, v18
	v_add_nc_u32_e32 v20, 0x1000, v18
	v_add_nc_u32_e32 v2, v8, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[4:5], 3, v[2:3]
	v_add_nc_u32_e32 v2, v8, v0
	v_add_co_u32 v8, s4, v8, v0
	v_add_co_ci_u32_e64 v9, null, 0, 0, s4
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_2) | instid1(VALU_DEP_4)
	v_lshlrev_b64 v[6:7], 3, v[2:3]
	v_add_nc_u32_e32 v2, v18, v1
	v_add_co_u32 v16, s4, v18, v0
	v_lshlrev_b64 v[8:9], 3, v[8:9]
	v_add_co_ci_u32_e64 v17, null, 0, 0, s4
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v6, vcc_lo, s0, v6
	v_add_co_ci_u32_e32 v7, vcc_lo, s1, v7, vcc_lo
	v_add_co_u32 v4, vcc_lo, s0, v4
	v_add_co_ci_u32_e32 v5, vcc_lo, s1, v5, vcc_lo
	global_load_b64 v[6:7], v[6:7], off
	v_add_co_u32 v8, vcc_lo, s0, v8
	global_load_b64 v[4:5], v[4:5], off
	v_add_co_ci_u32_e32 v9, vcc_lo, s1, v9, vcc_lo
	v_lshlrev_b64 v[10:11], 3, v[2:3]
	v_add_nc_u32_e32 v2, v18, v0
	v_lshlrev_b64 v[16:17], 3, v[16:17]
	global_load_b64 v[8:9], v[8:9], off offset:8
	s_mov_b32 s4, 0x33333333
	v_add_co_u32 v10, vcc_lo, s0, v10
	v_add_co_ci_u32_e32 v11, vcc_lo, s1, v11, vcc_lo
	v_lshlrev_b64 v[12:13], 3, v[2:3]
	v_add_nc_u32_e32 v2, v20, v1
	global_load_b64 v[10:11], v[10:11], off
	v_add_co_u32 v14, vcc_lo, s0, v12
	v_add_co_ci_u32_e32 v15, vcc_lo, s1, v13, vcc_lo
	v_add_co_u32 v16, vcc_lo, s0, v16
	v_add_co_ci_u32_e32 v17, vcc_lo, s1, v17, vcc_lo
	global_load_b64 v[14:15], v[14:15], off
	v_lshlrev_b64 v[1:2], 3, v[2:3]
	global_load_b64 v[16:17], v[16:17], off offset:8
	v_add_co_u32 v1, vcc_lo, s0, v1
	v_add_co_ci_u32_e32 v2, vcc_lo, s1, v2, vcc_lo
	global_load_b64 v[18:19], v[1:2], off
	v_add_nc_u32_e32 v2, v20, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[0:1], 3, v[2:3]
	v_add_nc_u32_e32 v2, 1, v2
	v_add_co_u32 v0, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	v_lshlrev_b64 v[2:3], 3, v[2:3]
	global_load_b64 v[0:1], v[0:1], off
	v_add_co_u32 v2, vcc_lo, s0, v2
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v3, vcc_lo
	s_mov_b32 s1, 0x3fc99999
	s_mov_b32 s0, 0x9999999a
	global_load_b64 v[2:3], v[2:3], off
	s_waitcnt vmcnt(8)
	v_mul_f64 v[6:7], v[6:7], 0.5
	s_waitcnt vmcnt(7)
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_fma_f64 v[4:5], v[4:5], s[0:1], v[6:7]
	s_mov_b32 s1, 0xbfe99999
	s_waitcnt vmcnt(6)
	v_fma_f64 v[4:5], v[8:9], s[0:1], v[4:5]
	s_mov_b32 s1, 0x3fd99999
	s_waitcnt vmcnt(5)
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_fma_f64 v[4:5], v[10:11], s[4:5], v[4:5]
	s_mov_b32 s5, 0x3fe33333
	s_waitcnt vmcnt(4)
	v_fma_f64 v[4:5], v[14:15], s[4:5], v[4:5]
	s_mov_b32 s4, 0xcccccccd
	s_mov_b32 s5, 0xbfeccccc
	s_waitcnt vmcnt(3)
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_fma_f64 v[4:5], v[16:17], s[4:5], v[4:5]
	s_mov_b32 s4, 0x66666666
	s_mov_b32 s5, 0x3fe66666
	s_waitcnt vmcnt(2)
	v_fma_f64 v[4:5], v[18:19], s[0:1], v[4:5]
	s_mov_b32 s1, 0x3fb99999
	s_waitcnt vmcnt(1)
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_fma_f64 v[0:1], v[0:1], s[4:5], v[4:5]
	s_waitcnt vmcnt(0)
	v_fma_f64 v[0:1], v[2:3], s[0:1], v[0:1]
	v_add_co_u32 v2, vcc_lo, s2, v12
	v_add_co_ci_u32_e32 v3, vcc_lo, s3, v13, vcc_lo
	global_store_b64 v[2:3], v[0:1], off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z16convolutionOnGPUiiPdS_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.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 21
		.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	_Z16convolutionOnGPUiiPdS_, .Lfunc_end0-_Z16convolutionOnGPUiiPdS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 712
; NumSgprs: 18
; NumVgprs: 21
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 21
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .offset:         0
        .size:           4
        .value_kind:     by_value
      - .offset:         4
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z16convolutionOnGPUiiPdS_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z16convolutionOnGPUiiPdS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     21
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
