#include <cuda_runtime.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define TILE_WIDTH 16
#define MAX_MASK_WIDTH 128

int UI(int argc, char* argv[], int* jkl);
void initData(int* data, int x, int y);
void initMask(float* mask, int k);
void showData(int* data, int len);
void showMask(float* mask, int len);
void convCPU(int* data, float* mask, float* conv_CPU, int x, int y, int k);
void checkCUDAError(cudaError_t e);
__global__
void convGPU(int* d_data, float* d_conv, int x, int y, int k);
void resultCheck(float* result_CPU, float* result_GPU, int size);
__constant__ float M[MAX_MASK_WIDTH * MAX_MASK_WIDTH];

int main(int argc, char* argv[]){
    cudaDeviceProp dev_prop;
    cudaGetDeviceProperties(&dev_prop, 0);
    printf("Total constant memory: %zd \n", dev_prop.totalConstMem);
    
    // reset rand seed
    srand((unsigned)time(NULL));
    clock_t start, finish;
    int total_time;

    // Go through UI first.
    // In UI section, only command with valid param can go to the next step.
    int UIStatus;
    int param[3];
    UIStatus = UI(argc, argv, param);
    if (UIStatus != 0) {
        printf("\nApplication terminates.\n");
        return 0;
    }
    // UI section ends

    
    // Initialize data array with int type
    const int x = param[0];
    const int y = param[1];
    const int k = param[2];
    int* data = (int*)malloc(x * y * sizeof(int));
    float* mask = (float*)malloc(k * k * sizeof(float));
    initData(data, x, y);
    initMask(mask, k);
    showData(data, x * y);
    showMask(mask, k * k);
    printf("Done initializing data array.\n");
    // Initialzing ends

    // CPU code for calculating convolution
    // Use this result to varify the kernel result later
    float* conv_CPU = (float*)calloc(x * y, sizeof(float));
    float* conv_GPU = (float*)calloc(x * y, sizeof(float));
    start = clock();
    convCPU(data, mask, conv_CPU, x, y, k);
    finish = clock();
    total_time = (int)(finish - start);
    showMask(conv_CPU, x * y);
    printf("Done convolution with CPU in %d miliseconds.\n", total_time);
    // Convolution calculating with CPU ends

    
    // Allocate device memory, copy data from host to device
    int* d_data; 
    float *d_conv;
    checkCUDAError(cudaMalloc((int**)&d_data, x * y * sizeof(int)));
    checkCUDAError(cudaMalloc((float**)&d_conv, x * y * sizeof(float)));
    printf("Done allocating space in device.");
    checkCUDAError(cudaMemcpy(d_data, data, x * y * sizeof(int), cudaMemcpyHostToDevice));
    checkCUDAError(cudaMemcpyToSymbol(M, mask, k * k * sizeof(float)));
    printf("\nDone copying memory from host to device.");
    // Done allocating, transfering and initializing

    // Initialize thread block and kernel grid dimensions
    dim3 threads(TILE_WIDTH, TILE_WIDTH);
    dim3 grid((int)ceil(1.0 * x / threads.x), (int)ceil(1.0 * y / threads.y));
    printf("\nDone initializing block dimention and grid dimention.");
    // Done initializing thread block and kernel grid dimensions
    
    // launch CUDA device kernel
    start = clock();
    convGPU<<< grid, threads >>>(d_data, d_conv, x, y, k);
    checkCUDAError(cudaDeviceSynchronize());
    finish = clock();
    total_time = (int)(finish - start);
    printf("\nDone matrix multiplication with GPU in %d miliseconds.\n", total_time);
    // Done CUDA device kernel

    
    // Copy results from device to host and free device memory
    checkCUDAError(cudaMemcpy(conv_GPU, d_conv, x * y * sizeof(float), cudaMemcpyDeviceToHost));
    printf("conv_GPU:");
    showMask(conv_GPU, x * y);
    checkCUDAError(cudaFree(d_conv));
    checkCUDAError(cudaFree(d_data));
    // Done copying results and freeing device memory

    
    // Check the result of the Calculated Matrix
    resultCheck(conv_CPU, conv_GPU, x * y);
    // Done result checking.
    
    return 0;
}

// UI for main function
// return 0 means everything's fine, just continue;
// return 1 means there's invalid input or '--help', terminate running.
int UI(int argc, char* argv[], int* param) {
    if (argc == 2 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)) {
        printf("CUDA Programming Homework. Histogram Algorithm.\n");
        printf("\nUsage: hist [OPTION]...\n");
        printf("\nOptions:\n");
        printf("%5s, %-10s %-50s\n", "-h", "--help", "Show helping information.");
        printf("%5s, %-10s %-50s\n", "-i", "--input", "Followed by 3 integers as input parameters.");
        printf("\nExamples:\n");
        printf("hist -h\n");
        printf("  Shows the helping information.\n");
        printf("hist -i 1960 1080 9\n");
        printf("  1960 1080 represents the picture is 1960 * 1080, 9 means the 2D mask is 9 * 9\n");
        return 1;
    }

    if (argc == 5 && (strcmp(argv[1], "-i") == 0 || strcmp(argv[1], "--input") == 0)) {
        int x = atoi(argv[2]);
        int y = atoi(argv[3]);
        int k = atoi(argv[4]);
        if (x <= 0 || y <= 0 || k <= 0) {
            printf("Invalid array length. The input values should be an integer greater than 0.\n");
            return 1;
        }
        if (k % 2 == 0) {
            printf("Invalid k, k should be odd.\n");
            return 1;
        }
        if (k > MAX_MASK_WIDTH) {
            printf("Invalid k, k is too big. Can't store it in texture memory.\n");
            return 1;
        }
        else {
            printf("x: %d\n", x);
            printf("y: %d\n", y);
            printf("k: %d\n", k);
            param[0] = x;
            param[1] = y;
            param[2] = k;
            return 0;
        }
    }
    else {
        printf("Invalid command. Please check how to make valid command by '-h' or '--help'.\n");
        return 1;
    }
}

void initData(int* data, int x, int y) {
    for (int i = 0; i < x * y; ++i) 
        data[i] = rand() % 16;
    return;
}

void initMask(float* mask, int k) {
    float sum = 0.0f;
    for (int i = 0; i < k * k; ++i) {
        mask[i] = rand() % 100 / 100.0f;
        sum += mask[i];
    }
    for (int i = 0; i < k * k; ++i) {
        mask[i] /= sum;
    }
    return;
}

void showData(int* data, int len) {
    printf("data:\n[");
    for (int i = 0; i < len && i < 10; ++i) {
        if (i != 0) printf(",");
        printf("%4d", data[i]);
    }
    if (len > 10) printf("...");
    printf("]\n");
    return;
}

void showMask(float* mask, int len) {
    printf("data:\n[");
    for (int i = 0; i < len && i < 10; ++i) {
        if (i != 0) printf(",");
        printf("%4f", mask[i]);
    }
    if (len > 10) printf("...");
    printf("]\n");
    return;
}

void convCPU(int* data, float* mask, float* conv_CPU, int x, int y, int k) {
    for (int i = 0; i < y; ++i) {
        for (int j = 0; j < x; ++j) {
            float sum = 0.0f;
            for (int m = 0; m < k; ++m) {
                for (int n = 0; n < k; ++n) {
                    if (i + m - k / 2 >= 0 && i + m - k / 2 < y && j + n - k / 2 >= 0 && j + n - k / 2 < x) {
                        sum += data[(i + m - k / 2) * x + (j + n - k / 2)] * mask[m * k + n];
                    }
                }
            }
            conv_CPU[i * x + j] = sum;
        }
    }
    return;
}

void checkCUDAError(cudaError_t e) {
    if (e == 0) return;
    printf("\nError: %s\n", cudaGetErrorName(e));
    printf("%s\n", cudaGetErrorString(e));
    exit(0);
}

__global__
void convGPU(int* d_data, float* d_conv, int x, int y, int k) {
    unsigned int tx = blockIdx.x * blockDim.x + threadIdx.x;
    unsigned int ty = blockIdx.y * blockDim.y + threadIdx.y;
    __shared__ float data_ds[TILE_WIDTH][TILE_WIDTH];
    if (ty < y && tx < x) {
        data_ds[threadIdx.y][threadIdx.x] = d_data[ty * x + tx];
        __syncthreads();
        float Pvalue = 0;
        for (int m = 0; m < k; ++m) {
            for (int n = 0; n < k; ++n) {
                int cur_row = ty + m - k / 2;
                int cur_col = tx + n - k / 2;
                if (cur_col >= 0 && cur_col < x && cur_row >= 0 && cur_row < y) {
                    if (cur_col >= blockDim.x * blockIdx.x && cur_col < blockDim.x * (blockIdx.x + 1)
                        && cur_row >= blockDim.y * blockIdx.y && cur_row < blockDim.y * (blockIdx.y + 1))
                        Pvalue += data_ds[threadIdx.y + m - k / 2][threadIdx.x + n - k / 2] * M[m * k + n];
                    else
                        Pvalue += d_data[cur_row * x + cur_col] * M[m * k + n];
                }
            }
        }
        d_conv[ty * x + tx] = Pvalue;
    }
}

void resultCheck(float* result_CPU, float* result_GPU, int size) {
    for (int i = 0; i < size; ++i) {
        if (result_CPU[i] * 1.001 <= result_GPU[i] || result_CPU[i] * 0.999 >= result_GPU[i]) {
            printf("\nResult check: Error!!!! Didn't pass.");
            return;
        }
    }
    printf("\nResult check: ---PASS---.");
    return;
}

	.file	"tmpxft_003adca2_00000000-6_hw3.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2038:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2038:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"--help"
.LC1:
	.string	"-h"
.LC2:
	.string	"CUDA Programming Homework. Histogram Algorithm.\n"
.LC3:
	.string	"\nUsage: hist [OPTION]...\n"
.LC4:
	.string	"\nOptions:\n"
.LC5:
	.string	"Show helping information."
.LC6:
	.string	"%5s, %-10s %-50s\n"
.LC7:
	.string	"Followed by 3 integers as input parameters."
.LC8:
	.string	"--input"
.LC9:
	.string	"-i"
.LC10:
	.string	"\nExamples:\n"
.LC11:
	.string	"hist -h\n"
.LC12:
	.string	"  Shows the helping information.\n"
.LC13:
	.string	"hist -i 1960 1080 9\n"
.LC14:
	.string	"  1960 1080 represents the picture is 1960 * 1080, 9 means the 2D mask is 9 * 9\n"
.LC15:
	.string	"Invalid array length. The input values should be an integer greater than 0.\n"
.LC16:
	.string	"Invalid k, k should be odd.\n"
.LC17:
	.string	"Invalid k, k is too big. Can't store it in texture memory.\n"
.LC18:
	.string	"x: %d\n"
.LC19:
	.string	"y: %d\n"
.LC20:
	.string	"k: %d\n"
.LC21:
	.string	"Invalid command. Please check how to make valid command by '-h' or '--help'.\n"
	.text
	.globl	_Z2UIiPPcPi
	.type	_Z2UIiPPcPi, @function
_Z2UIiPPcPi:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rsi, %rbx
	pushq	%rcx
	.cfi_def_cfa_offset 48
	cmpl	$2, %edi
	jne	.L3
	movq	8(%rsi), %r12
	leaq	.LC0(%rip), %rbp
	leaq	.LC1(%rip), %rbx
	movq	%rbp, %rsi
	movq	%r12, %rdi
	call	strcmp@PLT
	testl	%eax, %eax
	je	.L4
	movq	%rbx, %rsi
	movq	%r12, %rdi
	call	strcmp@PLT
	testl	%eax, %eax
	jne	.L5
.L4:
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbx, %rdx
	movq	%rbp, %rcx
	movl	$2, %edi
	leaq	.LC6(%rip), %rbx
	leaq	.LC5(%rip), %r8
	xorl	%eax, %eax
	movq	%rbx, %rsi
	call	__printf_chk@PLT
	movq	%rbx, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC7(%rip), %r8
	leaq	.LC8(%rip), %rcx
	leaq	.LC9(%rip), %rdx
	call	__printf_chk@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC13(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC14(%rip), %rsi
	jmp	.L21
.L3:
	cmpl	$5, %edi
	jne	.L5
	movq	8(%rsi), %r12
	leaq	.LC9(%rip), %rsi
	movq	%rdx, %rbp
	movq	%r12, %rdi
	call	strcmp@PLT
	testl	%eax, %eax
	je	.L7
	leaq	.LC8(%rip), %rsi
	movq	%r12, %rdi
	call	strcmp@PLT
	testl	%eax, %eax
	jne	.L5
.L7:
	movq	16(%rbx), %rdi
	call	atoi@PLT
	movq	24(%rbx), %rdi
	movl	%eax, %r13d
	call	atoi@PLT
	movq	32(%rbx), %rdi
	movl	%eax, %r12d
	call	atoi@PLT
	testl	%r13d, %r13d
	movl	%eax, %ebx
	setle	%al
	testl	%r12d, %r12d
	setle	%dl
	orb	%dl, %al
	jne	.L13
	testl	%ebx, %ebx
	jg	.L8
.L13:
	leaq	.LC15(%rip), %rsi
.L21:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L2
.L8:
	leaq	.LC16(%rip), %rsi
	testb	$1, %bl
	je	.L21
	leaq	.LC17(%rip), %rsi
	cmpl	$128, %ebx
	jg	.L21
	movl	%r13d, %edx
	leaq	.LC18(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%r12d, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC19(%rip), %rsi
	call	__printf_chk@PLT
	movl	%ebx, %edx
	leaq	.LC20(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	%r13d, 0(%rbp)
	xorl	%eax, %eax
	movl	%r12d, 4(%rbp)
	movl	%ebx, 8(%rbp)
	jmp	.L2
.L5:
	leaq	.LC21(%rip), %rsi
	jmp	.L21
.L2:
	popq	%rdx
	.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
.LFE2028:
	.size	_Z2UIiPPcPi, .-_Z2UIiPPcPi
	.globl	_Z8initDataPiii
	.type	_Z8initDataPiii, @function
_Z8initDataPiii:
.LFB2029:
	.cfi_startproc
	endbr64
	imull	%edx, %esi
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	$16, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movl	%esi, %ebp
	xorl	%ebx, %ebx
	pushq	%rcx
	.cfi_def_cfa_offset 48
.L24:
	cmpl	%ebx, %ebp
	jle	.L27
	call	rand@PLT
	cltd
	idivl	%r13d
	movl	%edx, (%r12,%rbx,4)
	incq	%rbx
	jmp	.L24
.L27:
	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
.LFE2029:
	.size	_Z8initDataPiii, .-_Z8initDataPiii
	.globl	_Z8initMaskPfi
	.type	_Z8initMaskPfi, @function
_Z8initMaskPfi:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	xorps	%xmm1, %xmm1
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%esi, %ebp
	imull	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rdi, %rbx
	movslq	%ebp, %rax
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	leaq	(%rdi,%rax,4), %r13
.L29:
	cmpq	%r13, %r12
	je	.L34
	movss	%xmm1, 12(%rsp)
	addq	$4, %r12
	call	rand@PLT
	movl	$100, %ecx
	movss	12(%rsp), %xmm1
	cltd
	idivl	%ecx
	cvtsi2ssl	%edx, %xmm0
	divss	.LC23(%rip), %xmm0
	addss	%xmm0, %xmm1
	movss	%xmm0, -4(%r12)
	jmp	.L29
.L34:
	xorl	%eax, %eax
.L31:
	cmpl	%eax, %ebp
	jle	.L35
	movss	(%rbx,%rax,4), %xmm0
	divss	%xmm1, %xmm0
	movss	%xmm0, (%rbx,%rax,4)
	incq	%rax
	jmp	.L31
.L35:
	addq	$24, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z8initMaskPfi, .-_Z8initMaskPfi
	.section	.rodata.str1.1
.LC24:
	.string	"data:\n["
.LC25:
	.string	","
.LC26:
	.string	"%4d"
.LC27:
	.string	"..."
.LC28:
	.string	"]\n"
	.text
	.globl	_Z8showDataPii
	.type	_Z8showDataPii, @function
_Z8showDataPii:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	xorl	%eax, %eax
	leaq	.LC25(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	movl	$2, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%esi, %ebp
	leaq	.LC24(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rcx
	.cfi_def_cfa_offset 48
	call	__printf_chk@PLT
.L37:
	cmpl	%ebx, %ebp
	jle	.L42
	cmpl	$9, %ebx
	jg	.L42
	testq	%rbx, %rbx
	je	.L38
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L38:
	movl	(%r12,%rbx,4), %edx
	leaq	.LC26(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbx
	call	__printf_chk@PLT
	jmp	.L37
.L42:
	cmpl	$10, %ebp
	jle	.L41
	leaq	.LC27(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L41:
	popq	%rdx
	.cfi_def_cfa_offset 40
	leaq	.LC28(%rip), %rsi
	popq	%rbx
	.cfi_def_cfa_offset 32
	movl	$2, %edi
	popq	%rbp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2031:
	.size	_Z8showDataPii, .-_Z8showDataPii
	.section	.rodata.str1.1
.LC29:
	.string	"%4f"
	.text
	.globl	_Z8showMaskPfi
	.type	_Z8showMaskPfi, @function
_Z8showMaskPfi:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	xorl	%eax, %eax
	leaq	.LC25(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	movl	$2, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%esi, %ebp
	leaq	.LC24(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rcx
	.cfi_def_cfa_offset 48
	call	__printf_chk@PLT
.L51:
	cmpl	%ebx, %ebp
	jle	.L56
	cmpl	$9, %ebx
	jg	.L56
	testq	%rbx, %rbx
	je	.L52
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L52:
	leaq	.LC29(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	(%r12,%rbx,4), %xmm0
	call	__printf_chk@PLT
	incq	%rbx
	jmp	.L51
.L56:
	cmpl	$10, %ebp
	jle	.L55
	leaq	.LC27(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L55:
	popq	%rdx
	.cfi_def_cfa_offset 40
	leaq	.LC28(%rip), %rsi
	popq	%rbx
	.cfi_def_cfa_offset 32
	movl	$2, %edi
	popq	%rbp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2032:
	.size	_Z8showMaskPfi, .-_Z8showMaskPfi
	.globl	_Z7convCPUPiPfS0_iii
	.type	_Z7convCPUPiPfS0_iii, @function
_Z7convCPUPiPfS0_iii:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	%r9d, %eax
	xorl	%r10d, %r10d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%ecx, %ebx
	movq	%rsi, -16(%rsp)
	movl	$2, %esi
	movq	%rdx, -8(%rsp)
	cltd
	idivl	%esi
	movq	%rdi, -24(%rsp)
	xorl	%edi, %edi
	negl	%eax
	imull	%eax, %ebx
	movl	%ebx, -28(%rsp)
.L65:
	cmpl	%r8d, %r10d
	jge	.L64
	movl	-28(%rsp), %ebx
	leal	(%rdi,%rbx), %edx
	movq	-8(%rsp), %rbx
	movl	%edx, -44(%rsp)
	movslq	%edi, %rdx
	leaq	(%rbx,%rdx,4), %rdx
	movq	%rdx, -40(%rsp)
	xorl	%edx, %edx
.L72:
	movl	%edx, -32(%rsp)
	cmpl	%edx, %ecx
	jle	.L69
	movl	-44(%rsp), %r13d
	leal	(%r10,%rax), %r11d
	xorl	%r12d, %r12d
	xorl	%ebp, %ebp
	xorps	%xmm0, %xmm0
.L70:
	cmpl	%r9d, %ebp
	jge	.L68
	movl	%r11d, %esi
	cmpl	%r11d, %r8d
	movl	-32(%rsp), %ebx
	notl	%esi
	setg	%r14b
	shrl	$31, %esi
	andl	%esi, %r14d
	leal	(%rbx,%rax), %esi
	xorl	%ebx, %ebx
	movb	%r14b, -45(%rsp)
.L67:
	cmpb	$0, -45(%rsp)
	je	.L66
	cmpl	%esi, %ecx
	jle	.L66
	testl	%esi, %esi
	js	.L66
	movq	-24(%rsp), %r14
	leal	(%rsi,%r13), %r15d
	movslq	%r15d, %r15
	cvtsi2ssl	(%r14,%r15,4), %xmm1
	movq	-16(%rsp), %r14
	leal	(%rbx,%r12), %r15d
	movslq	%r15d, %r15
	mulss	(%r14,%r15,4), %xmm1
	addss	%xmm1, %xmm0
.L66:
	incl	%ebx
	incl	%esi
	cmpl	%ebx, %r9d
	jne	.L67
	incl	%ebp
	addl	%ecx, %r13d
	addl	%r9d, %r12d
	incl	%r11d
	jmp	.L70
.L68:
	movq	-40(%rsp), %rbx
	movss	%xmm0, (%rbx,%rdx,4)
	incq	%rdx
	jmp	.L72
.L69:
	incl	%r10d
	addl	%ecx, %edi
	jmp	.L65
.L64:
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2033:
	.size	_Z7convCPUPiPfS0_iii, .-_Z7convCPUPiPfS0_iii
	.section	.rodata.str1.1
.LC30:
	.string	"\nError: %s\n"
.LC31:
	.string	"%s\n"
	.text
	.globl	_Z14checkCUDAError9cudaError
	.type	_Z14checkCUDAError9cudaError, @function
_Z14checkCUDAError9cudaError:
.LFB2034:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L83
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	%edi, %ebx
	call	cudaGetErrorName@PLT
	leaq	.LC30(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebx, %edi
	call	cudaGetErrorString@PLT
	movl	$2, %edi
	leaq	.LC31(%rip), %rsi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L83:
	.cfi_def_cfa_offset 8
	.cfi_restore 3
	ret
	.cfi_endproc
.LFE2034:
	.size	_Z14checkCUDAError9cudaError, .-_Z14checkCUDAError9cudaError
	.section	.rodata.str1.1
.LC34:
	.string	"\nResult check: Error!!!! Didn't pass."
.LC35:
	.string	"\nResult check: ---PASS---."
	.text
	.globl	_Z11resultCheckPfS_i
	.type	_Z11resultCheckPfS_i, @function
_Z11resultCheckPfS_i:
.LFB2035:
	.cfi_startproc
	endbr64
	movsd	.LC32(%rip), %xmm2
	movsd	.LC33(%rip), %xmm3
	xorl	%eax, %eax
.L88:
	cmpl	%eax, %edx
	jle	.L93
	cvtss2sd	(%rdi,%rax,4), %xmm0
	movaps	%xmm0, %xmm4
	cvtss2sd	(%rsi,%rax,4), %xmm1
	mulsd	%xmm2, %xmm4
	comisd	%xmm4, %xmm1
	jnb	.L89
	mulsd	%xmm3, %xmm0
	incq	%rax
	comisd	%xmm1, %xmm0
	jb	.L88
.L89:
	leaq	.LC34(%rip), %rsi
	jmp	.L92
.L93:
	leaq	.LC35(%rip), %rsi
.L92:
	movl	$2, %edi
	xorl	%eax, %eax
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2035:
	.size	_Z11resultCheckPfS_i, .-_Z11resultCheckPfS_i
	.globl	_Z31__device_stub__Z7convGPUPiPfiiiPiPfiii
	.type	_Z31__device_stub__Z7convGPUPiPfiiiPiPfiii, @function
_Z31__device_stub__Z7convGPUPiPfiiiPiPfiii:
.LFB2060:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	40(%rsp), %rcx
	movl	%r8d, 4(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%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)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L94
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z7convGPUPiPfiii(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	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 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L94:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L96
	call	__stack_chk_fail@PLT
.L96:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2060:
	.size	_Z31__device_stub__Z7convGPUPiPfiiiPiPfiii, .-_Z31__device_stub__Z7convGPUPiPfiiiPiPfiii
	.globl	_Z7convGPUPiPfiii
	.type	_Z7convGPUPiPfiii, @function
_Z7convGPUPiPfiii:
.LFB2061:
	.cfi_startproc
	endbr64
	jmp	_Z31__device_stub__Z7convGPUPiPfiiiPiPfiii
	.cfi_endproc
.LFE2061:
	.size	_Z7convGPUPiPfiii, .-_Z7convGPUPiPfiii
	.section	.rodata.str1.1
.LC36:
	.string	"Total constant memory: %zd \n"
.LC37:
	.string	"\nApplication terminates.\n"
.LC38:
	.string	"Done initializing data array.\n"
.LC39:
	.string	"Done convolution with CPU in %d miliseconds.\n"
.LC40:
	.string	"Done allocating space in device."
.LC41:
	.string	"\nDone copying memory from host to device."
.LC43:
	.string	"\nDone initializing block dimention and grid dimention."
.LC44:
	.string	"\nDone matrix multiplication with GPU in %d miliseconds.\n"
.LC45:
	.string	"conv_GPU:"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rsi, %rbp
	xorl	%esi, %esi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edi, %ebx
	subq	$1160, %rsp
	.cfi_def_cfa_offset 1216
	movq	%fs:40, %rax
	movq	%rax, 1144(%rsp)
	xorl	%eax, %eax
	leaq	112(%rsp), %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movq	464(%rsp), %rdx
	leaq	.LC36(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
	leaq	100(%rsp), %rdx
	movq	%rbp, %rsi
	movl	%ebx, %edi
	call	_Z2UIiPPcPi
	testl	%eax, %eax
	je	.L100
	leaq	.LC37(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L101
.L100:
	movl	100(%rsp), %ebp
	movl	104(%rsp), %r12d
	movl	108(%rsp), %r13d
	movl	%ebp, %ebx
	imull	%r12d, %ebx
	movl	%r13d, %r15d
	imull	%r13d, %r15d
	movslq	%ebx, %r14
	leaq	0(,%r14,4), %rax
	movq	%rax, %rdi
	movq	%rax, (%rsp)
	call	malloc@PLT
	movq	%rax, 8(%rsp)
	movslq	%r15d, %rax
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, 40(%rsp)
	call	malloc@PLT
	movq	8(%rsp), %rdi
	movl	%r12d, %edx
	movl	%ebp, %esi
	movq	%rax, 16(%rsp)
	call	_Z8initDataPiii
	movq	16(%rsp), %rdi
	movl	%r13d, %esi
	call	_Z8initMaskPfi
	movq	8(%rsp), %rdi
	movl	%ebx, %esi
	call	_Z8showDataPii
	movq	16(%rsp), %rdi
	movl	%r15d, %esi
	call	_Z8showMaskPfi
	leaq	.LC38(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r14, %rdi
	movl	$4, %esi
	call	calloc@PLT
	movq	%r14, %rdi
	movl	$4, %esi
	movq	%rax, 24(%rsp)
	call	calloc@PLT
	movq	%rax, 32(%rsp)
	call	clock@PLT
	movq	24(%rsp), %rdx
	movl	%r13d, %r9d
	movl	%r12d, %r8d
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdi
	movl	%ebp, %ecx
	movq	%rax, %r15
	call	_Z7convCPUPiPfS0_iii
	call	clock@PLT
	movq	24(%rsp), %rdi
	movl	%ebx, %esi
	movq	%rax, %r14
	call	_Z8showMaskPfi
	movl	%r14d, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	subl	%r15d, %edx
	leaq	.LC39(%rip), %rsi
	call	__printf_chk@PLT
	movq	(%rsp), %rsi
	leaq	56(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z14checkCUDAError9cudaError
	movq	(%rsp), %rsi
	leaq	64(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z14checkCUDAError9cudaError
	leaq	.LC40(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	(%rsp), %rdx
	movq	8(%rsp), %rsi
	movl	$1, %ecx
	movq	56(%rsp), %rdi
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z14checkCUDAError9cudaError
	movq	40(%rsp), %rdx
	movq	16(%rsp), %rsi
	xorl	%ecx, %ecx
	movl	$1, %r8d
	leaq	_ZL1M(%rip), %rdi
	call	cudaMemcpyToSymbol@PLT
	movl	%eax, %edi
	call	_Z14checkCUDAError9cudaError
	leaq	.LC41(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	cvtsi2sdl	%r12d, %xmm0
	mulsd	.LC42(%rip), %xmm0
	call	ceil@PLT
	movq	%xmm0, %r14
	cvtsi2sdl	%ebp, %xmm0
	mulsd	.LC42(%rip), %xmm0
	call	ceil@PLT
	movq	%r14, %xmm1
	leaq	.LC43(%rip), %rsi
	movl	$2, %edi
	cvttsd2sil	%xmm0, %eax
	movl	$1, 96(%rsp)
	movl	%eax, 88(%rsp)
	cvttsd2sil	%xmm1, %eax
	movl	%eax, 92(%rsp)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	call	clock@PLT
	movq	88(%rsp), %rdi
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	96(%rsp), %esi
	movl	$1, %ecx
	movq	%rax, %r14
	movabsq	$68719476752, %rdx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L102
	movq	64(%rsp), %rsi
	movq	56(%rsp), %rdi
	movl	%r13d, %r8d
	movl	%r12d, %ecx
	movl	%ebp, %edx
	call	_Z31__device_stub__Z7convGPUPiPfiiiPiPfiii
.L102:
	call	cudaDeviceSynchronize@PLT
	movl	%eax, %edi
	call	_Z14checkCUDAError9cudaError
	call	clock@PLT
	leaq	.LC44(%rip), %rsi
	movl	$2, %edi
	movl	%eax, %edx
	xorl	%eax, %eax
	subl	%r14d, %edx
	call	__printf_chk@PLT
	movq	(%rsp), %rdx
	movq	64(%rsp), %rsi
	movl	$2, %ecx
	movq	32(%rsp), %rdi
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z14checkCUDAError9cudaError
	leaq	.LC45(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	32(%rsp), %rdi
	movl	%ebx, %esi
	call	_Z8showMaskPfi
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	movl	%eax, %edi
	call	_Z14checkCUDAError9cudaError
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movl	%eax, %edi
	call	_Z14checkCUDAError9cudaError
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdi
	movl	%ebx, %edx
	call	_Z11resultCheckPfS_i
.L101:
	movq	1144(%rsp), %rax
	subq	%fs:40, %rax
	je	.L103
	call	__stack_chk_fail@PLT
.L103:
	addq	$1160, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC46:
	.string	"_Z7convGPUPiPfiii"
.LC47:
	.string	"M"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2063:
	.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	.LC46(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z7convGPUPiPfiii(%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	.LC47(%rip), %rdx
	movl	$65536, %r9d
	leaq	_ZL1M(%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
.LFE2063:
	.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
	.local	_ZL1M
	.comm	_ZL1M,65536,32
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC23:
	.long	1120403456
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC32:
	.long	-1821066134
	.long	1072694296
	.align 8
.LC33:
	.long	-652835029
	.long	1072691150
	.align 8
.LC42:
	.long	0
	.long	1068498944
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <hip/hip_runtime.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define TILE_WIDTH 16
#define MAX_MASK_WIDTH 128

int UI(int argc, char* argv[], int* jkl);
void initData(int* data, int x, int y);
void initMask(float* mask, int k);
void showData(int* data, int len);
void showMask(float* mask, int len);
void convCPU(int* data, float* mask, float* conv_CPU, int x, int y, int k);
void checkCUDAError(hipError_t e);
__global__
void convGPU(int* d_data, float* d_conv, int x, int y, int k);
void resultCheck(float* result_CPU, float* result_GPU, int size);
__constant__ float M[MAX_MASK_WIDTH * MAX_MASK_WIDTH];

int main(int argc, char* argv[]){
    hipDeviceProp_t dev_prop;
    hipGetDeviceProperties(&dev_prop, 0);
    printf("Total constant memory: %zd \n", dev_prop.totalConstMem);
    
    // reset rand seed
    srand((unsigned)time(NULL));
    clock_t start, finish;
    int total_time;

    // Go through UI first.
    // In UI section, only command with valid param can go to the next step.
    int UIStatus;
    int param[3];
    UIStatus = UI(argc, argv, param);
    if (UIStatus != 0) {
        printf("\nApplication terminates.\n");
        return 0;
    }
    // UI section ends

    
    // Initialize data array with int type
    const int x = param[0];
    const int y = param[1];
    const int k = param[2];
    int* data = (int*)malloc(x * y * sizeof(int));
    float* mask = (float*)malloc(k * k * sizeof(float));
    initData(data, x, y);
    initMask(mask, k);
    showData(data, x * y);
    showMask(mask, k * k);
    printf("Done initializing data array.\n");
    // Initialzing ends

    // CPU code for calculating convolution
    // Use this result to varify the kernel result later
    float* conv_CPU = (float*)calloc(x * y, sizeof(float));
    float* conv_GPU = (float*)calloc(x * y, sizeof(float));
    start = clock();
    convCPU(data, mask, conv_CPU, x, y, k);
    finish = clock();
    total_time = (int)(finish - start);
    showMask(conv_CPU, x * y);
    printf("Done convolution with CPU in %d miliseconds.\n", total_time);
    // Convolution calculating with CPU ends

    
    // Allocate device memory, copy data from host to device
    int* d_data; 
    float *d_conv;
    checkCUDAError(hipMalloc((int**)&d_data, x * y * sizeof(int)));
    checkCUDAError(hipMalloc((float**)&d_conv, x * y * sizeof(float)));
    printf("Done allocating space in device.");
    checkCUDAError(hipMemcpy(d_data, data, x * y * sizeof(int), hipMemcpyHostToDevice));
    checkCUDAError(hipMemcpyToSymbol(HIP_SYMBOL(M), mask, k * k * sizeof(float)));
    printf("\nDone copying memory from host to device.");
    // Done allocating, transfering and initializing

    // Initialize thread block and kernel grid dimensions
    dim3 threads(TILE_WIDTH, TILE_WIDTH);
    dim3 grid((int)ceil(1.0 * x / threads.x), (int)ceil(1.0 * y / threads.y));
    printf("\nDone initializing block dimention and grid dimention.");
    // Done initializing thread block and kernel grid dimensions
    
    // launch CUDA device kernel
    start = clock();
    convGPU<<< grid, threads >>>(d_data, d_conv, x, y, k);
    checkCUDAError(hipDeviceSynchronize());
    finish = clock();
    total_time = (int)(finish - start);
    printf("\nDone matrix multiplication with GPU in %d miliseconds.\n", total_time);
    // Done CUDA device kernel

    
    // Copy results from device to host and free device memory
    checkCUDAError(hipMemcpy(conv_GPU, d_conv, x * y * sizeof(float), hipMemcpyDeviceToHost));
    printf("conv_GPU:");
    showMask(conv_GPU, x * y);
    checkCUDAError(hipFree(d_conv));
    checkCUDAError(hipFree(d_data));
    // Done copying results and freeing device memory

    
    // Check the result of the Calculated Matrix
    resultCheck(conv_CPU, conv_GPU, x * y);
    // Done result checking.
    
    return 0;
}

// UI for main function
// return 0 means everything's fine, just continue;
// return 1 means there's invalid input or '--help', terminate running.
int UI(int argc, char* argv[], int* param) {
    if (argc == 2 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)) {
        printf("CUDA Programming Homework. Histogram Algorithm.\n");
        printf("\nUsage: hist [OPTION]...\n");
        printf("\nOptions:\n");
        printf("%5s, %-10s %-50s\n", "-h", "--help", "Show helping information.");
        printf("%5s, %-10s %-50s\n", "-i", "--input", "Followed by 3 integers as input parameters.");
        printf("\nExamples:\n");
        printf("hist -h\n");
        printf("  Shows the helping information.\n");
        printf("hist -i 1960 1080 9\n");
        printf("  1960 1080 represents the picture is 1960 * 1080, 9 means the 2D mask is 9 * 9\n");
        return 1;
    }

    if (argc == 5 && (strcmp(argv[1], "-i") == 0 || strcmp(argv[1], "--input") == 0)) {
        int x = atoi(argv[2]);
        int y = atoi(argv[3]);
        int k = atoi(argv[4]);
        if (x <= 0 || y <= 0 || k <= 0) {
            printf("Invalid array length. The input values should be an integer greater than 0.\n");
            return 1;
        }
        if (k % 2 == 0) {
            printf("Invalid k, k should be odd.\n");
            return 1;
        }
        if (k > MAX_MASK_WIDTH) {
            printf("Invalid k, k is too big. Can't store it in texture memory.\n");
            return 1;
        }
        else {
            printf("x: %d\n", x);
            printf("y: %d\n", y);
            printf("k: %d\n", k);
            param[0] = x;
            param[1] = y;
            param[2] = k;
            return 0;
        }
    }
    else {
        printf("Invalid command. Please check how to make valid command by '-h' or '--help'.\n");
        return 1;
    }
}

void initData(int* data, int x, int y) {
    for (int i = 0; i < x * y; ++i) 
        data[i] = rand() % 16;
    return;
}

void initMask(float* mask, int k) {
    float sum = 0.0f;
    for (int i = 0; i < k * k; ++i) {
        mask[i] = rand() % 100 / 100.0f;
        sum += mask[i];
    }
    for (int i = 0; i < k * k; ++i) {
        mask[i] /= sum;
    }
    return;
}

void showData(int* data, int len) {
    printf("data:\n[");
    for (int i = 0; i < len && i < 10; ++i) {
        if (i != 0) printf(",");
        printf("%4d", data[i]);
    }
    if (len > 10) printf("...");
    printf("]\n");
    return;
}

void showMask(float* mask, int len) {
    printf("data:\n[");
    for (int i = 0; i < len && i < 10; ++i) {
        if (i != 0) printf(",");
        printf("%4f", mask[i]);
    }
    if (len > 10) printf("...");
    printf("]\n");
    return;
}

void convCPU(int* data, float* mask, float* conv_CPU, int x, int y, int k) {
    for (int i = 0; i < y; ++i) {
        for (int j = 0; j < x; ++j) {
            float sum = 0.0f;
            for (int m = 0; m < k; ++m) {
                for (int n = 0; n < k; ++n) {
                    if (i + m - k / 2 >= 0 && i + m - k / 2 < y && j + n - k / 2 >= 0 && j + n - k / 2 < x) {
                        sum += data[(i + m - k / 2) * x + (j + n - k / 2)] * mask[m * k + n];
                    }
                }
            }
            conv_CPU[i * x + j] = sum;
        }
    }
    return;
}

void checkCUDAError(hipError_t e) {
    if (e == 0) return;
    printf("\nError: %s\n", hipGetErrorName(e));
    printf("%s\n", hipGetErrorString(e));
    exit(0);
}

__global__
void convGPU(int* d_data, float* d_conv, int x, int y, int k) {
    unsigned int tx = blockIdx.x * blockDim.x + threadIdx.x;
    unsigned int ty = blockIdx.y * blockDim.y + threadIdx.y;
    __shared__ float data_ds[TILE_WIDTH][TILE_WIDTH];
    if (ty < y && tx < x) {
        data_ds[threadIdx.y][threadIdx.x] = d_data[ty * x + tx];
        __syncthreads();
        float Pvalue = 0;
        for (int m = 0; m < k; ++m) {
            for (int n = 0; n < k; ++n) {
                int cur_row = ty + m - k / 2;
                int cur_col = tx + n - k / 2;
                if (cur_col >= 0 && cur_col < x && cur_row >= 0 && cur_row < y) {
                    if (cur_col >= blockDim.x * blockIdx.x && cur_col < blockDim.x * (blockIdx.x + 1)
                        && cur_row >= blockDim.y * blockIdx.y && cur_row < blockDim.y * (blockIdx.y + 1))
                        Pvalue += data_ds[threadIdx.y + m - k / 2][threadIdx.x + n - k / 2] * M[m * k + n];
                    else
                        Pvalue += d_data[cur_row * x + cur_col] * M[m * k + n];
                }
            }
        }
        d_conv[ty * x + tx] = Pvalue;
    }
}

void resultCheck(float* result_CPU, float* result_GPU, int size) {
    for (int i = 0; i < size; ++i) {
        if (result_CPU[i] * 1.001 <= result_GPU[i] || result_CPU[i] * 0.999 >= result_GPU[i]) {
            printf("\nResult check: Error!!!! Didn't pass.");
            return;
        }
    }
    printf("\nResult check: ---PASS---.");
    return;
}

	.text
	.file	"hw3.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI0_0:
	.quad	0x3fb0000000000000              # double 0.0625
	.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	$1576, %rsp                     # imm = 0x628
	.cfi_def_cfa_offset 1632
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movl	%edi, %ebp
	leaq	104(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	movq	352(%r14), %rsi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	leaq	52(%rsp), %rdx
	movl	%ebp, %edi
	movq	%rbx, %rsi
	callq	_Z2UIiPPcPi
	testl	%eax, %eax
	je	.LBB0_2
# %bb.1:
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	jmp	.LBB0_8
.LBB0_2:
	movl	52(%rsp), %ecx
	movl	56(%rsp), %eax
	movl	60(%rsp), %ebx
	movl	%eax, 20(%rsp)                  # 4-byte Spill
	movl	%ecx, 36(%rsp)                  # 4-byte Spill
	imull	%ecx, %eax
	movl	%eax, 16(%rsp)                  # 4-byte Spill
	movslq	%eax, %rbp
	leaq	(,%rbp,4), %rdi
	movq	%rdi, 96(%rsp)                  # 8-byte Spill
	callq	malloc
	movq	%rax, %r12
	movl	%ebx, 12(%rsp)                  # 4-byte Spill
	movl	%ebx, %r13d
	imull	%r13d, %r13d
	leaq	(,%r13,4), %rdi
	movq	%rdi, 72(%rsp)                  # 8-byte Spill
	callq	malloc
	movq	%rax, %r15
	testl	%ebp, %ebp
	jle	.LBB0_5
# %bb.3:                                # %.lr.ph.preheader.i
	movl	16(%rsp), %ebx                  # 4-byte Reload
	xorl	%r14d, %r14d
.LBB0_4:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
                                        # kill: def $eax killed $eax def $rax
	leal	15(%rax), %ecx
	testl	%eax, %eax
	cmovnsl	%eax, %ecx
	andl	$-16, %ecx
	subl	%ecx, %eax
	movl	%eax, (%r12,%r14,4)
	incq	%r14
	cmpq	%r14, %rbx
	jne	.LBB0_4
.LBB0_5:                                # %_Z8initDataPiii.exit
	movq	%r15, %rdi
	movl	12(%rsp), %ebx                  # 4-byte Reload
	movl	%ebx, %esi
	callq	_Z8initMaskPfi
	movq	%r12, %rdi
	movl	%ebp, %esi
	callq	_Z8showDataPii
	movq	%r15, %rdi
	movq	%r15, %r14
	movq	%r15, 64(%rsp)                  # 8-byte Spill
	movl	%r13d, %esi
	callq	_Z8showMaskPfi
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$4, %esi
	movq	%rbp, %rdi
	callq	calloc
	movq	%rax, %r15
	movl	$4, %esi
	movq	%rbp, %rdi
	callq	calloc
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	callq	clock
	movq	%rax, %r13
	movq	%r12, %rdi
	movq	%r14, %rsi
	movq	%r15, %rdx
	movl	36(%rsp), %r14d                 # 4-byte Reload
	movl	%r14d, %ecx
	movl	20(%rsp), %r8d                  # 4-byte Reload
	movl	%ebx, %r9d
	callq	_Z7convCPUPiPfS0_iii
	callq	clock
	movq	%rax, %rbx
	subl	%r13d, %ebx
	movq	%r15, 88(%rsp)                  # 8-byte Spill
	movq	%r15, %rdi
	movl	%ebp, %esi
	movl	%r14d, %ebp
	callq	_Z8showMaskPfi
	movl	$.L.str.3, %edi
	movl	%ebx, %esi
	xorl	%eax, %eax
	callq	printf
	leaq	40(%rsp), %rbx
	movq	%rbx, %rdi
	movq	96(%rsp), %r13                  # 8-byte Reload
	movq	%r13, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z14checkCUDAError10hipError_t
	leaq	24(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z14checkCUDAError10hipError_t
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	movq	(%rbx), %rdi
	movq	%r12, %rsi
	movl	20(%rsp), %r12d                 # 4-byte Reload
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z14checkCUDAError10hipError_t
	movl	$M, %edi
	movq	64(%rsp), %rsi                  # 8-byte Reload
	movq	72(%rsp), %rdx                  # 8-byte Reload
	xorl	%ecx, %ecx
	movl	$1, %r8d
	callq	hipMemcpyToSymbol
	movl	%eax, %edi
	callq	_Z14checkCUDAError10hipError_t
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	cvtsi2sd	%r14d, %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	callq	ceil@PLT
	cvttsd2si	%xmm0, %r14d
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r12d, %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	callq	ceil@PLT
	cvttsd2si	%xmm0, %ebx
	shlq	$32, %rbx
	orq	%r14, %rbx
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	callq	clock
	movq	%rax, %r15
	movabsq	$68719476752, %rdx              # imm = 0x1000000010
	movq	%rbx, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB0_7
# %bb.6:
	movq	40(%rsp), %rdi
	movq	24(%rsp), %rsi
	movl	%ebp, %edx
	movl	%r12d, %ecx
	movl	12(%rsp), %r8d                  # 4-byte Reload
	callq	_Z22__device_stub__convGPUPiPfiii
.LBB0_7:
	callq	hipDeviceSynchronize
	movl	%eax, %edi
	callq	_Z14checkCUDAError10hipError_t
	callq	clock
	subl	%r15d, %eax
	movl	$.L.str.7, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
	movq	24(%rsp), %rsi
	movq	80(%rsp), %r14                  # 8-byte Reload
	movq	%r14, %rdi
	movq	%r13, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z14checkCUDAError10hipError_t
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
	movq	%r14, %rdi
	movl	16(%rsp), %ebx                  # 4-byte Reload
	movl	%ebx, %esi
	callq	_Z8showMaskPfi
	movq	24(%rsp), %rdi
	callq	hipFree
	movl	%eax, %edi
	callq	_Z14checkCUDAError10hipError_t
	movq	40(%rsp), %rdi
	callq	hipFree
	movl	%eax, %edi
	callq	_Z14checkCUDAError10hipError_t
	movq	88(%rsp), %rdi                  # 8-byte Reload
	movq	%r14, %rsi
	movl	%ebx, %edx
	callq	_Z11resultCheckPfS_i
.LBB0_8:
	xorl	%eax, %eax
	addq	$1576, %rsp                     # imm = 0x628
	.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_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z2UIiPPcPi                     # -- Begin function _Z2UIiPPcPi
	.type	_Z2UIiPPcPi,@function
_Z2UIiPPcPi:                            # @_Z2UIiPPcPi
	.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
	movq	%rsi, %r15
	cmpl	$5, %edi
	je	.LBB1_5
# %bb.1:
	cmpl	$2, %edi
	jne	.LBB1_16
# %bb.2:
	movq	8(%r15), %rbx
	movl	$.L.str.9, %esi
	movq	%rbx, %rdi
	callq	strcmp
	testl	%eax, %eax
	je	.LBB1_4
# %bb.3:
	movl	$.L.str.10, %esi
	movq	%rbx, %rdi
	callq	strcmp
	testl	%eax, %eax
	jne	.LBB1_16
.LBB1_4:
	movl	$.Lstr.6, %edi
	callq	puts@PLT
	movl	$.Lstr.7, %edi
	callq	puts@PLT
	movl	$.Lstr.8, %edi
	callq	puts@PLT
	movl	$.L.str.14, %edi
	movl	$.L.str.10, %esi
	movl	$.L.str.9, %edx
	movl	$.L.str.15, %ecx
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.14, %edi
	movl	$.L.str.16, %esi
	movl	$.L.str.17, %edx
	movl	$.L.str.18, %ecx
	xorl	%eax, %eax
	callq	printf
	movl	$.Lstr.9, %edi
	callq	puts@PLT
	movl	$.Lstr.10, %edi
	callq	puts@PLT
	movl	$.Lstr.11, %edi
	callq	puts@PLT
	movl	$.Lstr.12, %edi
	callq	puts@PLT
	movl	$.Lstr.13, %edi
	jmp	.LBB1_17
.LBB1_5:
	movq	%rdx, %rbx
	movq	8(%r15), %r14
	movl	$.L.str.16, %esi
	movq	%r14, %rdi
	callq	strcmp
	testl	%eax, %eax
	je	.LBB1_7
# %bb.6:
	movl	$.L.str.17, %esi
	movq	%r14, %rdi
	callq	strcmp
	testl	%eax, %eax
	je	.LBB1_7
.LBB1_16:
	movl	$.Lstr.2, %edi
.LBB1_17:
	callq	puts@PLT
	movl	$1, %r12d
.LBB1_18:
	movl	%r12d, %eax
	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
.LBB1_7:
	.cfi_def_cfa_offset 48
	movq	16(%r15), %rdi
	callq	atoi
	movl	%eax, %ebp
	movq	24(%r15), %rdi
	callq	atoi
	movl	%eax, %r14d
	movq	32(%r15), %rdi
	callq	atoi
	testl	%ebp, %ebp
	jle	.LBB1_10
# %bb.8:
	testl	%r14d, %r14d
	jle	.LBB1_10
# %bb.9:
	movl	%eax, %r15d
	testl	%eax, %eax
	jle	.LBB1_10
# %bb.11:
	testb	$1, %r15b
	jne	.LBB1_13
# %bb.12:
	movl	$.Lstr.4, %edi
	jmp	.LBB1_17
.LBB1_10:
	movl	$.Lstr.5, %edi
	jmp	.LBB1_17
.LBB1_13:
	cmpl	$129, %r15d
	jb	.LBB1_15
# %bb.14:
	movl	$.Lstr.3, %edi
	jmp	.LBB1_17
.LBB1_15:
	xorl	%r12d, %r12d
	movl	$.L.str.27, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.28, %edi
	movl	%r14d, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.29, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
	movl	%ebp, (%rbx)
	movl	%r14d, 4(%rbx)
	movl	%r15d, 8(%rbx)
	jmp	.LBB1_18
.Lfunc_end1:
	.size	_Z2UIiPPcPi, .Lfunc_end1-_Z2UIiPPcPi
	.cfi_endproc
                                        # -- End function
	.globl	_Z8initDataPiii                 # -- Begin function _Z8initDataPiii
	.type	_Z8initDataPiii,@function
_Z8initDataPiii:                        # @_Z8initDataPiii
	.cfi_startproc
# %bb.0:
	imull	%edx, %esi
	testl	%esi, %esi
	jle	.LBB2_4
# %bb.1:                                # %.lr.ph.preheader
	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
	movl	%esi, %r14d
	xorl	%r15d, %r15d
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
                                        # kill: def $eax killed $eax def $rax
	leal	15(%rax), %ecx
	testl	%eax, %eax
	cmovnsl	%eax, %ecx
	andl	$-16, %ecx
	subl	%ecx, %eax
	movl	%eax, (%rbx,%r15,4)
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB2_2
# %bb.3:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
.LBB2_4:                                # %._crit_edge
	retq
.Lfunc_end2:
	.size	_Z8initDataPiii, .Lfunc_end2-_Z8initDataPiii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z8initMaskPfi
.LCPI3_0:
	.long	0x42c80000                      # float 100
	.text
	.globl	_Z8initMaskPfi
	.type	_Z8initMaskPfi,@function
_Z8initMaskPfi:                         # @_Z8initMaskPfi
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	je	.LBB3_7
# %bb.1:                                # %.lr.ph.preheader
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebp
	movq	%rdi, %rbx
	movl	%esi, %r14d
	imull	%r14d, %r14d
	cmpl	$1, %r14d
	movl	%r14d, %r15d
	adcl	$0, %r15d
	xorps	%xmm1, %xmm1
	xorl	%r12d, %r12d
.LBB3_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movss	%xmm1, 12(%rsp)                 # 4-byte Spill
	callq	rand
	movss	12(%rsp), %xmm1                 # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	cltq
	imulq	$1374389535, %rax, %rcx         # imm = 0x51EB851F
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$37, %rcx
	addl	%edx, %ecx
	imull	$100, %ecx, %ecx
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	divss	.LCPI3_0(%rip), %xmm0
	movss	%xmm0, (%rbx,%r12,4)
	addss	%xmm0, %xmm1
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB3_2
# %bb.3:                                # %.preheader
	testl	%ebp, %ebp
	je	.LBB3_6
# %bb.4:                                # %.lr.ph22.preheader
	cmpl	$1, %r14d
	adcl	$0, %r14d
	xorl	%eax, %eax
.LBB3_5:                                # %.lr.ph22
                                        # =>This Inner Loop Header: Depth=1
	movss	(%rbx,%rax,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	divss	%xmm1, %xmm0
	movss	%xmm0, (%rbx,%rax,4)
	incq	%rax
	cmpq	%rax, %r14
	jne	.LBB3_5
.LBB3_6:
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r14
	.cfi_restore %r15
	.cfi_restore %rbp
.LBB3_7:                                # %._crit_edge
	retq
.Lfunc_end3:
	.size	_Z8initMaskPfi, .Lfunc_end3-_Z8initMaskPfi
	.cfi_endproc
                                        # -- End function
	.globl	_Z8showDataPii                  # -- Begin function _Z8showDataPii
	.type	_Z8showDataPii,@function
_Z8showDataPii:                         # @_Z8showDataPii
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	%esi, %ebx
	movq	%rdi, %r14
	movl	$.L.str.31, %edi
	xorl	%eax, %eax
	callq	printf
	testl	%ebx, %ebx
	jle	.LBB4_7
# %bb.1:                                # %.lr.ph.preheader
	leal	-1(%rbx), %eax
	cmpl	$9, %eax
	movl	$9, %r15d
	cmovbl	%eax, %r15d
	incl	%r15d
	xorl	%r12d, %r12d
.LBB4_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	testq	%r12, %r12
	je	.LBB4_4
# %bb.3:                                #   in Loop: Header=BB4_2 Depth=1
	movl	$44, %edi
	callq	putchar@PLT
.LBB4_4:                                #   in Loop: Header=BB4_2 Depth=1
	movl	(%r14,%r12,4), %esi
	movl	$.L.str.33, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB4_2
# %bb.5:                                # %._crit_edge
	cmpl	$10, %ebx
	jle	.LBB4_7
# %bb.6:
	movl	$.L.str.34, %edi
	xorl	%eax, %eax
	callq	printf
.LBB4_7:                                # %._crit_edge.thread
	movl	$.Lstr.15, %edi
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	puts@PLT                        # TAILCALL
.Lfunc_end4:
	.size	_Z8showDataPii, .Lfunc_end4-_Z8showDataPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z8showMaskPfi                  # -- Begin function _Z8showMaskPfi
	.type	_Z8showMaskPfi,@function
_Z8showMaskPfi:                         # @_Z8showMaskPfi
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	%esi, %ebx
	movq	%rdi, %r14
	movl	$.L.str.31, %edi
	xorl	%eax, %eax
	callq	printf
	testl	%ebx, %ebx
	jle	.LBB5_7
# %bb.1:                                # %.lr.ph.preheader
	leal	-1(%rbx), %eax
	cmpl	$9, %eax
	movl	$9, %r15d
	cmovbl	%eax, %r15d
	incl	%r15d
	xorl	%r12d, %r12d
.LBB5_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	testq	%r12, %r12
	je	.LBB5_4
# %bb.3:                                #   in Loop: Header=BB5_2 Depth=1
	movl	$44, %edi
	callq	putchar@PLT
.LBB5_4:                                #   in Loop: Header=BB5_2 Depth=1
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r14,%r12,4), %xmm0
	movl	$.L.str.36, %edi
	movb	$1, %al
	callq	printf
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB5_2
# %bb.5:                                # %._crit_edge
	cmpl	$10, %ebx
	jle	.LBB5_7
# %bb.6:
	movl	$.L.str.34, %edi
	xorl	%eax, %eax
	callq	printf
.LBB5_7:                                # %._crit_edge.thread
	movl	$.Lstr.15, %edi
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	puts@PLT                        # TAILCALL
.Lfunc_end5:
	.size	_Z8showMaskPfi, .Lfunc_end5-_Z8showMaskPfi
	.cfi_endproc
                                        # -- End function
	.globl	_Z7convCPUPiPfS0_iii            # -- Begin function _Z7convCPUPiPfS0_iii
	.type	_Z7convCPUPiPfS0_iii,@function
_Z7convCPUPiPfS0_iii:                   # @_Z7convCPUPiPfS0_iii
	.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
	.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	%r9d, -68(%rsp)                 # 4-byte Spill
                                        # kill: def $ecx killed $ecx def $rcx
	movq	%rdx, -40(%rsp)                 # 8-byte Spill
	movq	%rsi, -24(%rsp)                 # 8-byte Spill
	testl	%r8d, %r8d
	jle	.LBB6_14
# %bb.1:                                # %.preheader54.lr.ph
	movl	-68(%rsp), %eax                 # 4-byte Reload
	movl	%eax, %edx
	shrl	$31, %edx
	addl	%eax, %edx
	sarl	%edx
	negl	%edx
	movslq	%eax, %r10
	movl	%r8d, %eax
	movq	%rax, -32(%rsp)                 # 8-byte Spill
	movl	%ecx, %ebx
	movl	%r10d, %r14d
	shlq	$2, %r10
	leal	1(%rcx), %eax
	movq	%rdx, -48(%rsp)                 # 8-byte Spill
	imull	%edx, %eax
	movq	%rax, -56(%rsp)                 # 8-byte Spill
	xorl	%eax, %eax
	movq	%rax, -64(%rsp)                 # 8-byte Spill
.LBB6_2:                                # %.preheader54
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB6_4 Depth 2
                                        #       Child Loop BB6_6 Depth 3
                                        #         Child Loop BB6_7 Depth 4
	testl	%ecx, %ecx
	jle	.LBB6_13
# %bb.3:                                # %.preheader53.lr.ph
                                        #   in Loop: Header=BB6_2 Depth=1
	movq	-48(%rsp), %rsi                 # 8-byte Reload
	movq	-64(%rsp), %rax                 # 8-byte Reload
	leal	(%rsi,%rax), %r13d
                                        # kill: def $eax killed $eax killed $rax def $rax
	imull	%ecx, %eax
	movq	-40(%rsp), %rdx                 # 8-byte Reload
	leaq	(%rdx,%rax,4), %rax
	movq	%rax, -16(%rsp)                 # 8-byte Spill
	movq	-56(%rsp), %r15                 # 8-byte Reload
	movq	%rsi, %rax
	xorl	%edx, %edx
.LBB6_4:                                # %.preheader53
                                        #   Parent Loop BB6_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB6_6 Depth 3
                                        #         Child Loop BB6_7 Depth 4
	cmpl	$0, -68(%rsp)                   # 4-byte Folded Reload
	movq	%r15, -8(%rsp)                  # 8-byte Spill
	xorps	%xmm0, %xmm0
	jle	.LBB6_12
# %bb.5:                                # %.preheader.lr.ph
                                        #   in Loop: Header=BB6_4 Depth=2
	movq	-24(%rsp), %rsi                 # 8-byte Reload
	xorl	%r12d, %r12d
.LBB6_6:                                # %.preheader
                                        #   Parent Loop BB6_2 Depth=1
                                        #     Parent Loop BB6_4 Depth=2
                                        # =>    This Loop Header: Depth=3
                                        #         Child Loop BB6_7 Depth 4
	leal	(%r12,%r13), %ebp
	xorl	%r9d, %r9d
.LBB6_7:                                #   Parent Loop BB6_2 Depth=1
                                        #     Parent Loop BB6_4 Depth=2
                                        #       Parent Loop BB6_6 Depth=3
                                        # =>      This Inner Loop Header: Depth=4
	cmpl	%r8d, %ebp
	jae	.LBB6_10
# %bb.8:                                #   in Loop: Header=BB6_7 Depth=4
	leal	(%rax,%r9), %r11d
	cmpl	%ecx, %r11d
	jae	.LBB6_10
# %bb.9:                                #   in Loop: Header=BB6_7 Depth=4
	leal	(%r15,%r9), %r11d
	movslq	%r11d, %r11
	xorps	%xmm1, %xmm1
	cvtsi2ssl	(%rdi,%r11,4), %xmm1
	mulss	(%rsi,%r9,4), %xmm1
	addss	%xmm1, %xmm0
.LBB6_10:                               #   in Loop: Header=BB6_7 Depth=4
	incq	%r9
	cmpq	%r9, %r14
	jne	.LBB6_7
# %bb.11:                               # %._crit_edge
                                        #   in Loop: Header=BB6_6 Depth=3
	incq	%r12
	addq	%r10, %rsi
	addq	%rbx, %r15
	cmpq	%r14, %r12
	jne	.LBB6_6
.LBB6_12:                               # %._crit_edge59
                                        #   in Loop: Header=BB6_4 Depth=2
	movq	-16(%rsp), %rsi                 # 8-byte Reload
	movss	%xmm0, (%rsi,%rdx,4)
	incq	%rdx
	incq	%rax
	movq	-8(%rsp), %r15                  # 8-byte Reload
	incq	%r15
	cmpq	%rbx, %rdx
	jne	.LBB6_4
.LBB6_13:                               # %._crit_edge62
                                        #   in Loop: Header=BB6_2 Depth=1
	movq	-64(%rsp), %rdx                 # 8-byte Reload
	incq	%rdx
	addq	%rbx, -56(%rsp)                 # 8-byte Folded Spill
	movq	%rdx, %rax
	movq	%rdx, -64(%rsp)                 # 8-byte Spill
	cmpq	-32(%rsp), %rdx                 # 8-byte Folded Reload
	jne	.LBB6_2
.LBB6_14:                               # %._crit_edge64
	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_end6:
	.size	_Z7convCPUPiPfS0_iii, .Lfunc_end6-_Z7convCPUPiPfS0_iii
	.cfi_endproc
                                        # -- End function
	.globl	_Z14checkCUDAError10hipError_t  # -- Begin function _Z14checkCUDAError10hipError_t
	.type	_Z14checkCUDAError10hipError_t,@function
_Z14checkCUDAError10hipError_t:         # @_Z14checkCUDAError10hipError_t
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jne	.LBB7_2
# %bb.1:
	retq
.LBB7_2:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%edi, %ebx
	callq	hipGetErrorName
	movl	$.L.str.37, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	%ebx, %edi
	callq	hipGetErrorString
	movq	%rax, %rdi
	callq	puts@PLT
	xorl	%edi, %edi
	callq	exit
.Lfunc_end7:
	.size	_Z14checkCUDAError10hipError_t, .Lfunc_end7-_Z14checkCUDAError10hipError_t
	.cfi_endproc
                                        # -- End function
	.globl	_Z22__device_stub__convGPUPiPfiii # -- Begin function _Z22__device_stub__convGPUPiPfiii
	.type	_Z22__device_stub__convGPUPiPfiii,@function
_Z22__device_stub__convGPUPiPfiii:      # @_Z22__device_stub__convGPUPiPfiii
	.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	$128, %rsp
	.cfi_def_cfa_offset 176
	.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), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	4(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%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	$_Z7convGPUPiPfiii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	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_end8:
	.size	_Z22__device_stub__convGPUPiPfiii, .Lfunc_end8-_Z22__device_stub__convGPUPiPfiii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z11resultCheckPfS_i
.LCPI9_0:
	.quad	0x3ff004189374bc6a              # double 1.0009999999999999
.LCPI9_1:
	.quad	0x3feff7ced916872b              # double 0.99899999999999999
	.text
	.globl	_Z11resultCheckPfS_i
	.type	_Z11resultCheckPfS_i,@function
_Z11resultCheckPfS_i:                   # @_Z11resultCheckPfS_i
	.cfi_startproc
# %bb.0:
	testl	%edx, %edx
	jle	.LBB9_1
# %bb.3:                                # %.lr.ph.preheader
	movl	%edx, %ecx
	xorl	%edx, %edx
	movsd	.LCPI9_0(%rip), %xmm0           # xmm0 = mem[0],zero
	movsd	.LCPI9_1(%rip), %xmm1           # xmm1 = mem[0],zero
	movl	$.L.str.39, %eax
.LBB9_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	xorps	%xmm2, %xmm2
	cvtss2sd	(%rdi,%rdx,4), %xmm2
	movaps	%xmm2, %xmm4
	mulsd	%xmm0, %xmm4
	xorps	%xmm3, %xmm3
	cvtss2sd	(%rsi,%rdx,4), %xmm3
	ucomisd	%xmm4, %xmm3
	mulsd	%xmm1, %xmm2
	jae	.LBB9_2
# %bb.5:                                # %.lr.ph
                                        #   in Loop: Header=BB9_4 Depth=1
	ucomisd	%xmm3, %xmm2
	jae	.LBB9_2
# %bb.6:                                #   in Loop: Header=BB9_4 Depth=1
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB9_4
.LBB9_1:
	movl	$.L.str.40, %eax
.LBB9_2:                                # %._crit_edge
	movq	%rax, %rdi
	xorl	%eax, %eax
	jmp	printf                          # TAILCALL
.Lfunc_end9:
	.size	_Z11resultCheckPfS_i, .Lfunc_end9-_Z11resultCheckPfS_i
	.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	.LBB10_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB10_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z7convGPUPiPfiii, %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	$M, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movl	$65536, %r9d                    # imm = 0x10000
	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_end10:
	.size	__hip_module_ctor, .Lfunc_end10-__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	.LBB11_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
.LBB11_2:
	retq
.Lfunc_end11:
	.size	__hip_module_dtor, .Lfunc_end11-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	M,@object                       # @M
	.local	M
	.comm	M,65536,16
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Total constant memory: %zd \n"
	.size	.L.str, 29

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Done convolution with CPU in %d miliseconds.\n"
	.size	.L.str.3, 46

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Done allocating space in device."
	.size	.L.str.4, 33

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"\nDone copying memory from host to device."
	.size	.L.str.5, 42

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"\nDone initializing block dimention and grid dimention."
	.size	.L.str.6, 55

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

	.type	.L.str.7,@object                # @.str.7
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.7:
	.asciz	"\nDone matrix multiplication with GPU in %d miliseconds.\n"
	.size	.L.str.7, 57

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"conv_GPU:"
	.size	.L.str.8, 10

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"--help"
	.size	.L.str.9, 7

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"-h"
	.size	.L.str.10, 3

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"%5s, %-10s %-50s\n"
	.size	.L.str.14, 18

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"Show helping information."
	.size	.L.str.15, 26

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"-i"
	.size	.L.str.16, 3

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"--input"
	.size	.L.str.17, 8

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"Followed by 3 integers as input parameters."
	.size	.L.str.18, 44

	.type	.L.str.27,@object               # @.str.27
.L.str.27:
	.asciz	"x: %d\n"
	.size	.L.str.27, 7

	.type	.L.str.28,@object               # @.str.28
.L.str.28:
	.asciz	"y: %d\n"
	.size	.L.str.28, 7

	.type	.L.str.29,@object               # @.str.29
.L.str.29:
	.asciz	"k: %d\n"
	.size	.L.str.29, 7

	.type	.L.str.31,@object               # @.str.31
.L.str.31:
	.asciz	"data:\n["
	.size	.L.str.31, 8

	.type	.L.str.33,@object               # @.str.33
.L.str.33:
	.asciz	"%4d"
	.size	.L.str.33, 4

	.type	.L.str.34,@object               # @.str.34
.L.str.34:
	.asciz	"..."
	.size	.L.str.34, 4

	.type	.L.str.36,@object               # @.str.36
.L.str.36:
	.asciz	"%4f"
	.size	.L.str.36, 4

	.type	.L.str.37,@object               # @.str.37
.L.str.37:
	.asciz	"\nError: %s\n"
	.size	.L.str.37, 12

	.type	.L.str.39,@object               # @.str.39
.L.str.39:
	.asciz	"\nResult check: Error!!!! Didn't pass."
	.size	.L.str.39, 38

	.type	.L.str.40,@object               # @.str.40
.L.str.40:
	.asciz	"\nResult check: ---PASS---."
	.size	.L.str.40, 27

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z7convGPUPiPfiii"
	.size	.L__unnamed_1, 18

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"M"
	.size	.L__unnamed_2, 2

	.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	"Done initializing data array."
	.size	.Lstr, 30

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"\nApplication terminates."
	.size	.Lstr.1, 25

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Invalid command. Please check how to make valid command by '-h' or '--help'."
	.size	.Lstr.2, 77

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"Invalid k, k is too big. Can't store it in texture memory."
	.size	.Lstr.3, 59

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"Invalid k, k should be odd."
	.size	.Lstr.4, 28

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"Invalid array length. The input values should be an integer greater than 0."
	.size	.Lstr.5, 76

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"CUDA Programming Homework. Histogram Algorithm."
	.size	.Lstr.6, 48

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"\nUsage: hist [OPTION]..."
	.size	.Lstr.7, 25

	.type	.Lstr.8,@object                 # @str.8
.Lstr.8:
	.asciz	"\nOptions:"
	.size	.Lstr.8, 10

	.type	.Lstr.9,@object                 # @str.9
.Lstr.9:
	.asciz	"\nExamples:"
	.size	.Lstr.9, 11

	.type	.Lstr.10,@object                # @str.10
.Lstr.10:
	.asciz	"hist -h"
	.size	.Lstr.10, 8

	.type	.Lstr.11,@object                # @str.11
.Lstr.11:
	.asciz	"  Shows the helping information."
	.size	.Lstr.11, 33

	.type	.Lstr.12,@object                # @str.12
.Lstr.12:
	.asciz	"hist -i 1960 1080 9"
	.size	.Lstr.12, 20

	.type	.Lstr.13,@object                # @str.13
.Lstr.13:
	.asciz	"  1960 1080 represents the picture is 1960 * 1080, 9 means the 2D mask is 9 * 9"
	.size	.Lstr.13, 80

	.type	.Lstr.15,@object                # @str.15
.Lstr.15:
	.asciz	"]"
	.size	.Lstr.15, 2

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z22__device_stub__convGPUPiPfiii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym M
	.addrsig_sym _Z7convGPUPiPfiii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z7convGPUPiPfiii       ; -- Begin function _Z7convGPUPiPfiii
	.globl	_Z7convGPUPiPfiii
	.p2align	8
	.type	_Z7convGPUPiPfiii,@function
_Z7convGPUPiPfiii:                      ; @_Z7convGPUPiPfiii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x2c
	s_load_b128 s[8:11], s[0:1], 0x10
	v_and_b32_e32 v5, 0x3ff, v0
	v_bfe_u32 v6, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s3, s2, 16
	s_and_b32 s4, s2, 0xffff
	s_mul_i32 s5, s15, s3
	s_mul_i32 s11, s14, s4
	v_add_nc_u32_e32 v4, s5, v6
	v_add_nc_u32_e32 v3, s11, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_gt_u32_e32 vcc_lo, s9, v4
	v_cmp_gt_u32_e64 s2, s8, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, s2, vcc_lo
	s_and_saveexec_b32 s6, s2
	s_cbranch_execz .LBB0_15
; %bb.1:
	s_load_b128 s[12:15], s[0:1], 0x0
	v_mad_u64_u32 v[1:2], null, v4, s8, v[3:4]
	v_dual_mov_b32 v2, 0 :: v_dual_lshlrev_b32 v5, 2, v5
	s_cmp_lt_i32 s10, 1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_lshl_add_u32 v6, v6, 6, v5
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v7, vcc_lo, s12, v0
	v_add_co_ci_u32_e32 v8, vcc_lo, s13, v1, vcc_lo
	global_load_b32 v7, v[7:8], off
	s_waitcnt vmcnt(0)
	v_cvt_f32_i32_e32 v5, v7
	ds_store_b32 v6, v5
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_14
; %bb.2:                                ; %.preheader.lr.ph
	s_lshr_b32 s0, s10, 1
	v_mov_b32_e32 v2, 0
	v_subrev_nc_u32_e32 v4, s0, v4
	s_mul_i32 s1, s0, 0x44
	v_subrev_nc_u32_e32 v3, s0, v3
	v_subrev_nc_u32_e32 v6, s1, v6
	s_add_i32 s16, s11, s4
	v_mul_lo_u32 v5, s8, v4
	s_add_i32 s17, s5, s3
	s_mov_b32 s7, 0
	s_mov_b32 s18, 0
	s_mov_b32 s19, 0
.LBB0_3:                                ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_4 Depth 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_dual_mov_b32 v7, v6 :: v_dual_add_nc_u32 v8, s19, v4
	s_mov_b32 s6, s18
	s_mov_b32 s20, s10
	v_cmp_lt_i32_e32 vcc_lo, -1, v8
	v_cmp_gt_i32_e64 s0, s9, v8
	v_cmp_gt_u32_e64 s1, s5, v8
	v_cmp_le_u32_e64 s2, s17, v8
	v_mov_b32_e32 v8, v3
.LBB0_4:                                ;   Parent Loop BB0_3 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_mov_b32 s21, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_lt_i32_e32 -1, v8
	s_cbranch_execz .LBB0_12
; %bb.5:                                ;   in Loop: Header=BB0_4 Depth=2
	v_cmp_gt_i32_e64 s3, s8, v8
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s3, vcc_lo, s3
	s_and_b32 s3, s0, s3
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s22, s3
	s_cbranch_execz .LBB0_11
; %bb.6:                                ;   in Loop: Header=BB0_4 Depth=2
	v_cmp_le_u32_e64 s3, s16, v8
	v_cmp_gt_u32_e64 s4, s11, v8
                                        ; implicit-def: $vgpr9
                                        ; implicit-def: $sgpr23
	s_delay_alu instid0(VALU_DEP_2)
	s_or_b32 s3, s1, s3
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	s_or_b32 s3, s4, s3
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s3, s3, s2
	s_xor_b32 s3, s3, -1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_saveexec_b32 s4, s3
	s_xor_b32 s3, exec_lo, s4
	s_cbranch_execz .LBB0_8
; %bb.7:                                ;   in Loop: Header=BB0_4 Depth=2
	s_getpc_b64 s[24:25]
	s_add_u32 s24, s24, M@rel32@lo+4
	s_addc_u32 s25, s25, M@rel32@hi+12
	s_lshl_b64 s[26:27], s[6:7], 2
	ds_load_b32 v9, v7
	s_add_u32 s24, s26, s24
	s_addc_u32 s25, s27, s25
	s_load_b32 s23, s[24:25], 0x0
.LBB0_8:                                ; %Flow
                                        ;   in Loop: Header=BB0_4 Depth=2
	s_or_saveexec_b32 s4, s3
	s_waitcnt lgkmcnt(0)
	v_mov_b32_e32 v10, s23
	s_xor_b32 exec_lo, exec_lo, s4
	s_cbranch_execz .LBB0_10
; %bb.9:                                ;   in Loop: Header=BB0_4 Depth=2
	v_add_nc_u32_e32 v9, v5, v8
	s_getpc_b64 s[24:25]
	s_add_u32 s24, s24, M@rel32@lo+4
	s_addc_u32 s25, s25, M@rel32@hi+12
	s_lshl_b64 s[26:27], s[6:7], 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	s_add_u32 s24, s26, s24
	v_ashrrev_i32_e32 v10, 31, v9
	s_addc_u32 s25, s27, s25
	v_lshlrev_b64 v[9:10], 2, v[9:10]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v9, s3, s12, v9
	v_add_co_ci_u32_e64 v10, s3, s13, v10, s3
	s_load_b32 s3, s[24:25], 0x0
	global_load_b32 v9, v[9:10], off
	s_waitcnt vmcnt(0)
	v_cvt_f32_i32_e32 v10, v9
	s_waitcnt lgkmcnt(0)
	v_mov_b32_e32 v9, s3
.LBB0_10:                               ; %.sink.split
                                        ;   in Loop: Header=BB0_4 Depth=2
	s_or_b32 exec_lo, exec_lo, s4
	s_delay_alu instid0(VALU_DEP_1)
	v_fmac_f32_e32 v2, v9, v10
.LBB0_11:                               ; %Flow110
                                        ;   in Loop: Header=BB0_4 Depth=2
	s_or_b32 exec_lo, exec_lo, s22
.LBB0_12:                               ;   in Loop: Header=BB0_4 Depth=2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 exec_lo, exec_lo, s21
	v_add_nc_u32_e32 v8, 1, v8
	v_add_nc_u32_e32 v7, 4, v7
	s_add_i32 s20, s20, -1
	s_add_i32 s6, s6, 1
	s_cmp_eq_u32 s20, 0
	s_cbranch_scc0 .LBB0_4
; %bb.13:                               ; %._crit_edge
                                        ;   in Loop: Header=BB0_3 Depth=1
	v_add_nc_u32_e32 v5, s8, v5
	v_add_nc_u32_e32 v6, 64, v6
	s_add_i32 s19, s19, 1
	s_add_i32 s18, s18, s10
	s_cmp_eq_u32 s19, s10
	s_cbranch_scc0 .LBB0_3
.LBB0_14:                               ; %._crit_edge75
	v_add_co_u32 v0, vcc_lo, s14, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s15, v1, vcc_lo
	global_store_b32 v[0:1], v2, off
.LBB0_15:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z7convGPUPiPfiii
		.amdhsa_group_segment_fixed_size 1024
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.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 11
		.amdhsa_next_free_sgpr 28
		.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	_Z7convGPUPiPfiii, .Lfunc_end0-_Z7convGPUPiPfiii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 704
; NumSgprs: 30
; NumVgprs: 11
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 1024 bytes/workgroup (compile time only)
; SGPRBlocks: 3
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 30
; NumVGPRsForWavesPerEU: 11
; 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	M                       ; @M
	.type	M,@object
	.section	.bss,"aw",@nobits
	.globl	M
	.p2align	4, 0x0
M:
	.zero	65536
	.size	M, 65536

	.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 M
	.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
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 1024
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z7convGPUPiPfiii
    .private_segment_fixed_size: 0
    .sgpr_count:     30
    .sgpr_spill_count: 0
    .symbol:         _Z7convGPUPiPfiii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     11
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
