#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <sys/time.h> 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


__global__
void conv2(float *A, float *kernel,int inputSize, int depth, int kernelSize , int stride, int pad, float *B, int outputSize) {

    // 计算元素output(i,j)的值 一次卷积运算
    int i = threadIdx.x + blockDim.x * blockIdx.x;
    int j = threadIdx.y + blockDim.y * blockIdx.y;

    if( !(i < outputSize) || !(j < outputSize) ) return;
    
    int Ai = i*stride;
    int Aj = j*stride;

    // 除去填充的0
    int startk = (pad-Ai) < 0? 0 : pad-Ai;
    int endk = kernelSize < (inputSize + pad - Ai) ? kernelSize : (inputSize + pad - Ai);
    int startl = (pad-Aj) < 0? 0 : pad-Aj;
    int endl = kernelSize < (inputSize + pad - Aj) ? kernelSize : (inputSize + pad - Aj);
    float sum = 0;

    for(int d = 0; d < depth; d++) {
        for( int k = startk ; k < endk; k++) {
            for( int l = startl; l < endl; l++) {
                sum += A[d*inputSize*inputSize + (Ai+k-pad)*inputSize + Aj+l-pad]*kernel[d*kernelSize*kernelSize + k*kernelSize+l];
            }
        }
        B[d*outputSize*outputSize + i*outputSize + j] = sum;
    }
    B[i*outputSize + j] = sum;
}


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

    // input: inputSize*inputSize*depth
    // kernel: kernelSize*kernelSize*depth
    // output: outputSize*outputSize

    int inputSize = 7;
    int depth = 3;
    int kernelSize = 3;
    int kernelNum = 3;
    int stride[3] = {1 , 2 , 3 };
    int pad[3] = {0,0,0};
    int outputSize[3];

    // 计算不同stride下需要的padding数量pad和output的规模outputSize
    
    for(int i = 0; i < kernelNum; i++) {
        if((inputSize - kernelSize)%stride[i] != 0) {
            pad[i] = (stride[i] - ((inputSize - kernelSize)%stride[i])) / 2;
        }
        outputSize[i] = (inputSize - kernelSize + 2*pad[i] ) / stride[i] + 1;
    }

    // ============================= 资源申请的初始化 =========================

    // ==== CPU资源申请和初始化
    // input:A kernel:kernel output:B
    
    float *A, *kernel[3], *B[3];
    A = (float *)malloc(sizeof(float)*inputSize*inputSize*depth);
    for(int i = 0; i < 3; i++) {
        kernel[i] = (float *)malloc(sizeof(float)*kernelSize*kernelSize*depth);
        B[i] = (float *)malloc(sizeof(float)*outputSize[i]*outputSize[i]*depth);
    }

   // 初始化input A
    for(int d = 0; d < depth; d++) {
        for(int i=0; i<inputSize*inputSize; i++) {
            A[d*inputSize*inputSize + i] = i;
        }
    }

    // 初始化kernel
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < kernelSize*kernelSize*depth; j++) {
            kernel[i][j] = 1;
        }
    }

    // ==== GPU资源申请和初始化

    float *d_A, *d_kernel[3], *d_B[3];

    cudaMalloc((void**)&d_A,sizeof(float)*inputSize*inputSize*depth);

    for(int i = 0; i < 3; i++) {
        cudaMalloc((void**)&d_kernel[i], sizeof(float)*kernelSize*kernelSize*depth);
        cudaMalloc((void**)&d_B[i],sizeof(float)*outputSize[i]*outputSize[i]);
    }

    cudaMemcpy(d_A,A,sizeof(float)*inputSize*inputSize*depth,cudaMemcpyHostToDevice);
    for(int i = 0; i < 3; i++) {
        cudaMemcpy(d_kernel[i],kernel[i],sizeof(float)*kernelSize*kernelSize*depth,cudaMemcpyHostToDevice);
    }

    // ============================= 调用核函数 =========================
    struct timeval start, end;
    gettimeofday( &start, NULL );

    for( int i = 0; i < 3; i++ ) {
        int blockx = (int) (log2(outputSize[i])+ 1);
        int blocky = blockx;

        dim3 Block(blockx,blocky);
        dim3 Grid((inputSize+Block.x-1) / Block.x,(inputSize+Block.y-1) / Block.y ); 

        conv2 <<< Grid, Block >>> (d_A,d_kernel[i],inputSize,depth,kernelSize,stride[i],pad[i],d_B[i],outputSize[i]);

    }
    
    // 结果回传
    for( int i = 0; i < 3; i++ ) {
        cudaMemcpy(B[i],d_B[i],sizeof(float)*outputSize[i]*outputSize[i],cudaMemcpyDeviceToHost);
    }
    
    gettimeofday( &end, NULL );
    int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
    //printf("Block(%d,%d)   Grid(%d,%d).\n", Block.x, Block.y, Grid.x, Grid.y);
    printf("total time is %f ms\n", timeuse/(float)1000);

    // 输出结果
    FILE *b[3];
    b[0] = fopen("matrixB11.m", "wb");
    b[1] = fopen("matrixB12.m", "wb");
    b[2] = fopen("matrixB13.m", "wb");


    for(int k = 0; k < 3; k++ ) {
        fprintf(b[k], "B = [ \n");
        for (int i = 0; i < outputSize[k]; i++)
        {
            for (int j = 0; j < outputSize[k]; j++)
                fprintf(b[k], "%f ", B[k][i * outputSize[k] + j]);
            fprintf(b[k], "\n");
        }
        fprintf(b[k], "];");
    }

    // ============================= 资源释放 =========================

    free(A);
    cudaFree(d_A);

    for(int i = 0; i < 3; i++) {

        free(kernel[i]);
        free(B[i]);

        cudaFree(d_B[i]);
        cudaFree(d_kernel[i]);
        
        fclose(b[i]);
    }

    return 0;
}
	.file	"tmpxft_0023dc1d_00000000-6_p1.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z34__device_stub__Z5conv2PfS_iiiiiS_iPfS_iiiiiS_i
	.type	_Z34__device_stub__Z5conv2PfS_iiiiiS_iPfS_iiiiiS_i, @function
_Z34__device_stub__Z5conv2PfS_iiiiiS_iPfS_iiiiiS_i:
.LFB2052:
	.cfi_startproc
	endbr64
	subq	$200, %rsp
	.cfi_def_cfa_offset 208
	movq	216(%rsp), %rax
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movl	%edx, 28(%rsp)
	leaq	48(%rsp), %rdx
	movl	%ecx, 24(%rsp)
	leaq	56(%rsp), %rcx
	movl	%r8d, 20(%rsp)
	movl	%r9d, 16(%rsp)
	movq	%rax, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 184(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	28(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	208(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	224(%rsp), %rax
	movq	%rax, 176(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	56(%rsp)
	.cfi_def_cfa_offset 216
	leaq	_Z5conv2PfS_iiiiiS_i(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 224
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 216
	popq	%rdx
	.cfi_def_cfa_offset 208
.L2:
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$200, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2052:
	.size	_Z34__device_stub__Z5conv2PfS_iiiiiS_iPfS_iiiiiS_i, .-_Z34__device_stub__Z5conv2PfS_iiiiiS_iPfS_iiiiiS_i
	.globl	_Z5conv2PfS_iiiiiS_i
	.type	_Z5conv2PfS_iiiiiS_i, @function
_Z5conv2PfS_iiiiiS_i:
.LFB2053:
	.cfi_startproc
	endbr64
	jmp	_Z34__device_stub__Z5conv2PfS_iiiiiS_iPfS_iiiiiS_i
	.cfi_endproc
.LFE2053:
	.size	_Z5conv2PfS_iiiiiS_i, .-_Z5conv2PfS_iiiiiS_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"total time is %f ms\n"
.LC4:
	.string	"wb"
.LC5:
	.string	"matrixB11.m"
.LC6:
	.string	"matrixB12.m"
.LC7:
	.string	"matrixB13.m"
.LC8:
	.string	"B = [ \n"
.LC9:
	.string	"%f "
.LC10:
	.string	"\n"
.LC11:
	.string	"];"
	.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
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	movl	$4, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movl	$2, %r8d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$280, %rsp
	.cfi_def_cfa_offset 336
	movq	%fs:40, %rax
	movq	%rax, 264(%rsp)
	movabsq	$8589934593, %rax
	movl	$3, 116(%rsp)
	leaq	108(%rsp), %rbp
	leaq	132(%rsp), %r12
	movq	%rdx, 120(%rsp)
	movl	%ecx, 128(%rsp)
	xorl	%ecx, %ecx
	movq	%rax, 108(%rsp)
.L10:
	movl	%edi, %eax
	movl	(%rcx,%rbp), %esi
	cltd
	idivl	%esi
	testl	%edx, %edx
	je	.L9
	movl	%esi, %eax
	subl	%edx, %eax
	cltd
	idivl	%r8d
	movl	%eax, 120(%rsp,%rcx)
.L9:
	movl	120(%rsp,%rcx), %eax
	addq	$4, %rcx
	leal	4(%rax,%rax), %eax
	cltd
	idivl	%esi
	incl	%eax
	movl	%eax, -4(%rcx,%r12)
	cmpq	$12, %rcx
	jne	.L10
	movl	$588, %edi
	xorl	%ebx, %ebx
	leaq	144(%rsp), %r15
	call	malloc@PLT
	movq	%rax, 16(%rsp)
.L11:
	movl	$108, %edi
	leaq	168(%rsp), %r14
	call	malloc@PLT
	movslq	(%r12,%rbx,4), %rdi
	movq	%rax, (%r15,%rbx,8)
	imulq	%rdi, %rdi
	imulq	$12, %rdi, %rdi
	call	malloc@PLT
	movq	%rax, (%r14,%rbx,8)
	incq	%rbx
	cmpq	$3, %rbx
	jne	.L11
	movq	16(%rsp), %rcx
	xorl	%edx, %edx
.L14:
	xorl	%eax, %eax
.L13:
	cvtsi2ssl	%eax, %xmm0
	movss	%xmm0, (%rcx,%rax,4)
	incq	%rax
	cmpq	$49, %rax
	jne	.L13
	addl	$49, %edx
	addq	$196, %rcx
	cmpl	$147, %edx
	jne	.L14
	movss	.LC0(%rip), %xmm0
	xorl	%edx, %edx
.L15:
	movq	(%r15,%rdx,8), %rcx
	xorl	%eax, %eax
.L16:
	movss	%xmm0, (%rcx,%rax,4)
	incq	%rax
	cmpq	$27, %rax
	jne	.L16
	incq	%rdx
	cmpq	$3, %rdx
	jne	.L15
	leaq	48(%rsp), %rdi
	movl	$588, %esi
	xorl	%ebx, %ebx
	call	cudaMalloc@PLT
	movq	%r12, 8(%rsp)
	leaq	192(%rsp), %r13
.L18:
	leaq	0(%r13,%rbx), %rdi
	movl	$108, %esi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rax
	leaq	216(%rsp,%rbx), %rdi
	addq	$8, %rbx
	movslq	(%rax), %rsi
	imulq	%rsi, %rsi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	addq	$4, 8(%rsp)
	cmpq	$24, %rbx
	jne	.L18
	movq	16(%rsp), %rsi
	movq	48(%rsp), %rdi
	movl	$1, %ecx
	xorl	%ebx, %ebx
	movl	$588, %edx
	call	cudaMemcpy@PLT
.L19:
	movq	(%r15,%rbx), %rsi
	movq	0(%r13,%rbx), %rdi
	movl	$1, %ecx
	movl	$108, %edx
	addq	$8, %rbx
	call	cudaMemcpy@PLT
	cmpq	$24, %rbx
	jne	.L19
	leaq	72(%rsp), %rdi
	xorl	%esi, %esi
	xorl	%ebx, %ebx
	call	gettimeofday@PLT
.L21:
	movl	(%r12,%rbx), %r10d
	cvtsi2sdl	%r10d, %xmm0
	movl	%r10d, 8(%rsp)
	call	log2@PLT
	xorl	%edx, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	addsd	.LC1(%rip), %xmm0
	movl	$1, %esi
	movl	$1, 68(%rsp)
	movl	$1, 96(%rsp)
	cvttsd2sil	%xmm0, %ecx
	leal	6(%rcx), %eax
	movl	%ecx, 60(%rsp)
	divl	%ecx
	movl	%ecx, 64(%rsp)
	movq	60(%rsp), %rdx
	movl	$1, %ecx
	movl	%eax, 88(%rsp)
	movl	%eax, 92(%rsp)
	movq	88(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	movl	8(%rsp), %r10d
	testl	%eax, %eax
	jne	.L20
	movq	0(%r13,%rbx,2), %rsi
	pushq	%rax
	.cfi_def_cfa_offset 344
	movl	$3, %ecx
	movl	$7, %edx
	pushq	%r10
	.cfi_def_cfa_offset 352
	movl	$3, %r8d
	pushq	232(%rsp,%rbx,2)
	.cfi_def_cfa_offset 360
	movl	144(%rsp,%rbx), %eax
	pushq	%rax
	.cfi_def_cfa_offset 368
	movq	80(%rsp), %rdi
	movl	0(%rbp,%rbx), %r9d
	call	_Z34__device_stub__Z5conv2PfS_iiiiiS_iPfS_iiiiiS_i
	addq	$32, %rsp
	.cfi_def_cfa_offset 336
.L20:
	addq	$4, %rbx
	cmpq	$12, %rbx
	jne	.L21
	leaq	216(%rsp), %rax
	xorl	%ebx, %ebx
	movq	%rax, 24(%rsp)
.L22:
	movslq	(%r12,%rbx,4), %rdx
	movq	24(%rsp), %rax
	movl	$2, %ecx
	movq	(%r14,%rbx,8), %rdi
	imulq	%rdx, %rdx
	movq	(%rax,%rbx,8), %rsi
	incq	%rbx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	cmpq	$3, %rbx
	jne	.L22
	leaq	88(%rsp), %rdi
	xorl	%esi, %esi
	leaq	.LC4(%rip), %rbx
	xorl	%ebp, %ebp
	call	gettimeofday@PLT
	movq	88(%rsp), %rax
	subq	72(%rsp), %rax
	leaq	.LC3(%rip), %rsi
	imull	$1000000, %eax, %eax
	subl	80(%rsp), %eax
	addl	96(%rsp), %eax
	movl	$2, %edi
	cvtsi2ssl	%eax, %xmm0
	movb	$1, %al
	divss	.LC2(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	%rbx, %rsi
	leaq	.LC5(%rip), %rdi
	call	fopen@PLT
	movq	%rbx, %rsi
	leaq	.LC6(%rip), %rdi
	movq	%rax, 240(%rsp)
	call	fopen@PLT
	movq	%rbx, %rsi
	leaq	.LC7(%rip), %rdi
	movq	%rax, 248(%rsp)
	call	fopen@PLT
	movq	%rax, 256(%rsp)
.L26:
	movq	240(%rsp,%rbp,8), %rax
	leaq	.LC8(%rip), %rdx
	movl	$2, %esi
	movq	%rax, 8(%rsp)
	movq	%rax, %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	xorl	%ecx, %ecx
.L23:
	movslq	(%r12,%rbp,4), %rbx
	cmpl	%ecx, %ebx
	jle	.L46
	movl	%ebx, %eax
	imull	%ecx, %eax
	cltq
	addq	%rax, %rbx
	leaq	0(,%rax,4), %r8
	salq	$2, %rbx
.L24:
	movq	(%r14,%rbp,8), %rax
	movq	8(%rsp), %rdi
	leaq	.LC9(%rip), %rdx
	movl	$2, %esi
	movl	%ecx, 44(%rsp)
	cvtss2sd	(%rax,%r8), %xmm0
	movb	$1, %al
	movq	%r8, 32(%rsp)
	call	__fprintf_chk@PLT
	movq	32(%rsp), %r8
	movl	44(%rsp), %ecx
	addq	$4, %r8
	cmpq	%rbx, %r8
	jne	.L24
	movq	8(%rsp), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	movl	%ecx, 32(%rsp)
	leaq	.LC10(%rip), %rdx
	call	__fprintf_chk@PLT
	movl	32(%rsp), %ecx
	incl	%ecx
	jmp	.L23
.L46:
	movq	8(%rsp), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	incq	%rbp
	leaq	.LC11(%rip), %rdx
	call	__fprintf_chk@PLT
	cmpq	$3, %rbp
	jne	.L26
	movq	16(%rsp), %rdi
	xorl	%ebx, %ebx
	call	free@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
.L27:
	movq	(%r15,%rbx), %rdi
	call	free@PLT
	movq	(%r14,%rbx), %rdi
	call	free@PLT
	movq	24(%rsp), %rax
	movq	(%rax,%rbx), %rdi
	call	cudaFree@PLT
	movq	0(%r13,%rbx), %rdi
	call	cudaFree@PLT
	movq	240(%rsp,%rbx), %rdi
	addq	$8, %rbx
	call	fclose@PLT
	cmpq	$24, %rbx
	jne	.L27
	movq	264(%rsp), %rax
	subq	%fs:40, %rax
	je	.L28
	call	__stack_chk_fail@PLT
.L28:
	addq	$280, %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
.LC12:
	.string	"_Z5conv2PfS_iiiiiS_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.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	.LC12(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z5conv2PfS_iiiiiS_i(%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
.LFE2055:
	.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.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	1065353216
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	0
	.long	1072693248
	.section	.rodata.cst4
	.align 4
.LC2:
	.long	1148846080
	.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 <sys/time.h> 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


__global__
void conv2(float *A, float *kernel,int inputSize, int depth, int kernelSize , int stride, int pad, float *B, int outputSize) {

    // 计算元素output(i,j)的值 一次卷积运算
    int i = threadIdx.x + blockDim.x * blockIdx.x;
    int j = threadIdx.y + blockDim.y * blockIdx.y;

    if( !(i < outputSize) || !(j < outputSize) ) return;
    
    int Ai = i*stride;
    int Aj = j*stride;

    // 除去填充的0
    int startk = (pad-Ai) < 0? 0 : pad-Ai;
    int endk = kernelSize < (inputSize + pad - Ai) ? kernelSize : (inputSize + pad - Ai);
    int startl = (pad-Aj) < 0? 0 : pad-Aj;
    int endl = kernelSize < (inputSize + pad - Aj) ? kernelSize : (inputSize + pad - Aj);
    float sum = 0;

    for(int d = 0; d < depth; d++) {
        for( int k = startk ; k < endk; k++) {
            for( int l = startl; l < endl; l++) {
                sum += A[d*inputSize*inputSize + (Ai+k-pad)*inputSize + Aj+l-pad]*kernel[d*kernelSize*kernelSize + k*kernelSize+l];
            }
        }
        B[d*outputSize*outputSize + i*outputSize + j] = sum;
    }
    B[i*outputSize + j] = sum;
}


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

    // input: inputSize*inputSize*depth
    // kernel: kernelSize*kernelSize*depth
    // output: outputSize*outputSize

    int inputSize = 7;
    int depth = 3;
    int kernelSize = 3;
    int kernelNum = 3;
    int stride[3] = {1 , 2 , 3 };
    int pad[3] = {0,0,0};
    int outputSize[3];

    // 计算不同stride下需要的padding数量pad和output的规模outputSize
    
    for(int i = 0; i < kernelNum; i++) {
        if((inputSize - kernelSize)%stride[i] != 0) {
            pad[i] = (stride[i] - ((inputSize - kernelSize)%stride[i])) / 2;
        }
        outputSize[i] = (inputSize - kernelSize + 2*pad[i] ) / stride[i] + 1;
    }

    // ============================= 资源申请的初始化 =========================

    // ==== CPU资源申请和初始化
    // input:A kernel:kernel output:B
    
    float *A, *kernel[3], *B[3];
    A = (float *)malloc(sizeof(float)*inputSize*inputSize*depth);
    for(int i = 0; i < 3; i++) {
        kernel[i] = (float *)malloc(sizeof(float)*kernelSize*kernelSize*depth);
        B[i] = (float *)malloc(sizeof(float)*outputSize[i]*outputSize[i]*depth);
    }

   // 初始化input A
    for(int d = 0; d < depth; d++) {
        for(int i=0; i<inputSize*inputSize; i++) {
            A[d*inputSize*inputSize + i] = i;
        }
    }

    // 初始化kernel
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < kernelSize*kernelSize*depth; j++) {
            kernel[i][j] = 1;
        }
    }

    // ==== GPU资源申请和初始化

    float *d_A, *d_kernel[3], *d_B[3];

    hipMalloc((void**)&d_A,sizeof(float)*inputSize*inputSize*depth);

    for(int i = 0; i < 3; i++) {
        hipMalloc((void**)&d_kernel[i], sizeof(float)*kernelSize*kernelSize*depth);
        hipMalloc((void**)&d_B[i],sizeof(float)*outputSize[i]*outputSize[i]);
    }

    hipMemcpy(d_A,A,sizeof(float)*inputSize*inputSize*depth,hipMemcpyHostToDevice);
    for(int i = 0; i < 3; i++) {
        hipMemcpy(d_kernel[i],kernel[i],sizeof(float)*kernelSize*kernelSize*depth,hipMemcpyHostToDevice);
    }

    // ============================= 调用核函数 =========================
    struct timeval start, end;
    gettimeofday( &start, NULL );

    for( int i = 0; i < 3; i++ ) {
        int blockx = (int) (log2(outputSize[i])+ 1);
        int blocky = blockx;

        dim3 Block(blockx,blocky);
        dim3 Grid((inputSize+Block.x-1) / Block.x,(inputSize+Block.y-1) / Block.y ); 

        conv2 <<< Grid, Block >>> (d_A,d_kernel[i],inputSize,depth,kernelSize,stride[i],pad[i],d_B[i],outputSize[i]);

    }
    
    // 结果回传
    for( int i = 0; i < 3; i++ ) {
        hipMemcpy(B[i],d_B[i],sizeof(float)*outputSize[i]*outputSize[i],hipMemcpyDeviceToHost);
    }
    
    gettimeofday( &end, NULL );
    int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
    //printf("Block(%d,%d)   Grid(%d,%d).\n", Block.x, Block.y, Grid.x, Grid.y);
    printf("total time is %f ms\n", timeuse/(float)1000);

    // 输出结果
    FILE *b[3];
    b[0] = fopen("matrixB11.m", "wb");
    b[1] = fopen("matrixB12.m", "wb");
    b[2] = fopen("matrixB13.m", "wb");


    for(int k = 0; k < 3; k++ ) {
        fprintf(b[k], "B = [ \n");
        for (int i = 0; i < outputSize[k]; i++)
        {
            for (int j = 0; j < outputSize[k]; j++)
                fprintf(b[k], "%f ", B[k][i * outputSize[k] + j]);
            fprintf(b[k], "\n");
        }
        fprintf(b[k], "];");
    }

    // ============================= 资源释放 =========================

    free(A);
    hipFree(d_A);

    for(int i = 0; i < 3; i++) {

        free(kernel[i]);
        free(B[i]);

        hipFree(d_B[i]);
        hipFree(d_kernel[i]);
        
        fclose(b[i]);
    }

    return 0;
}
	.text
	.file	"p1.hip"
	.globl	_Z20__device_stub__conv2PfS_iiiiiS_i # -- Begin function _Z20__device_stub__conv2PfS_iiiiiS_i
	.type	_Z20__device_stub__conv2PfS_iiiiiS_i,@function
_Z20__device_stub__conv2PfS_iiiiiS_i:   # @_Z20__device_stub__conv2PfS_iiiiiS_i
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$160, %rsp
	.cfi_def_cfa_offset 208
	.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)
	movq	%rsp, %r8
	movl	%r9d, (%r8)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	208(%rsp), %rax
	movq	%rax, 48(%rbx)
	leaq	216(%rsp), %rax
	movq	%rax, 56(%rbx)
	leaq	224(%rsp), %rax
	movq	%rax, 64(%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	$_Z5conv2PfS_iiiiiS_i, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$176, %rsp
	.cfi_adjust_cfa_offset -176
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z20__device_stub__conv2PfS_iiiiiS_i, .Lfunc_end0-_Z20__device_stub__conv2PfS_iiiiiS_i
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0x3ff0000000000000              # double 1
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI1_1:
	.long	0x447a0000                      # float 1000
	.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	$264, %rsp                      # imm = 0x108
	.cfi_def_cfa_offset 320
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movabsq	$8589934593, %rax               # imm = 0x200000001
	movq	%rax, 52(%rsp)
	movl	$3, 60(%rsp)
	movl	$0, 32(%rsp)
	movq	$0, 24(%rsp)
	xorl	%ecx, %ecx
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	movl	52(%rsp,%rcx,4), %esi
	movl	$4, %eax
	xorl	%edx, %edx
	idivl	%esi
	testl	%edx, %edx
	je	.LBB1_2
# %bb.3:                                #   in Loop: Header=BB1_1 Depth=1
	movl	%esi, %edi
	subl	%edx, %edi
	movl	%edi, %eax
	shrl	$31, %eax
	addl	%edi, %eax
	sarl	%eax
	movl	%eax, 24(%rsp,%rcx,4)
	jmp	.LBB1_4
.LBB1_2:                                # %._crit_edge219
                                        #   in Loop: Header=BB1_1 Depth=1
	movl	24(%rsp,%rcx,4), %eax
.LBB1_4:                                #   in Loop: Header=BB1_1 Depth=1
	leal	4(,%rax,2), %eax
	cltd
	idivl	%esi
	incl	%eax
	movl	%eax, 12(%rsp,%rcx,4)
	incq	%rcx
	cmpq	$3, %rcx
	jne	.LBB1_1
# %bb.5:
	movl	$588, %edi                      # imm = 0x24C
	callq	malloc
	movq	%rax, %r12
	xorl	%ebx, %ebx
.LBB1_6:                                # =>This Inner Loop Header: Depth=1
	movl	$108, %edi
	callq	malloc
	movq	%rax, 224(%rsp,%rbx,8)
	movslq	12(%rsp,%rbx,4), %rax
	imulq	%rax, %rax
	shlq	$2, %rax
	leaq	(%rax,%rax,2), %rdi
	callq	malloc
	movq	%rax, 192(%rsp,%rbx,8)
	incq	%rbx
	cmpq	$3, %rbx
	jne	.LBB1_6
# %bb.7:                                # %.preheader148.preheader
	xorl	%eax, %eax
	movq	%r12, %rcx
.LBB1_8:                                # %.preheader148
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_9 Depth 2
	xorl	%edx, %edx
.LBB1_9:                                #   Parent Loop BB1_8 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtsi2ss	%edx, %xmm0
	movss	%xmm0, (%rcx,%rdx,4)
	incq	%rdx
	cmpq	$49, %rdx
	jne	.LBB1_9
# %bb.10:                               #   in Loop: Header=BB1_8 Depth=1
	incq	%rax
	addq	$196, %rcx
	cmpq	$3, %rax
	jne	.LBB1_8
# %bb.11:                               # %.preheader146.preheader
	xorl	%eax, %eax
.LBB1_12:                               # %.preheader146
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_13 Depth 2
	movq	224(%rsp,%rax,8), %rcx
	xorl	%edx, %edx
.LBB1_13:                               #   Parent Loop BB1_12 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	$1065353216, (%rcx,%rdx,4)      # imm = 0x3F800000
	incq	%rdx
	cmpq	$27, %rdx
	jne	.LBB1_13
# %bb.14:                               #   in Loop: Header=BB1_12 Depth=1
	incq	%rax
	cmpq	$3, %rax
	jne	.LBB1_12
# %bb.15:
	movq	%rsp, %rdi
	movl	$588, %esi                      # imm = 0x24C
	callq	hipMalloc
	leaq	128(%rsp), %r14
	leaq	160(%rsp), %r15
	xorl	%ebx, %ebx
.LBB1_16:                               # =>This Inner Loop Header: Depth=1
	movl	$108, %esi
	movq	%r15, %rdi
	callq	hipMalloc
	movslq	12(%rsp,%rbx,4), %rsi
	imulq	%rsi, %rsi
	shlq	$2, %rsi
	movq	%r14, %rdi
	callq	hipMalloc
	incq	%rbx
	addq	$8, %r14
	addq	$8, %r15
	cmpq	$3, %rbx
	jne	.LBB1_16
# %bb.17:
	movq	(%rsp), %rdi
	movl	$588, %edx                      # imm = 0x24C
	movq	%r12, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	xorl	%ebx, %ebx
.LBB1_18:                               # =>This Inner Loop Header: Depth=1
	movq	160(%rsp,%rbx,8), %rdi
	movq	224(%rsp,%rbx,8), %rsi
	movl	$108, %edx
	movl	$1, %ecx
	callq	hipMemcpy
	incq	%rbx
	cmpq	$3, %rbx
	jne	.LBB1_18
# %bb.19:
	xorl	%r14d, %r14d
	leaq	112(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
.LBB1_20:                               # =>This Inner Loop Header: Depth=1
	movl	12(%rsp,%r14), %r15d
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r15d, %xmm0
	callq	log2
	addsd	.LCPI1_0(%rip), %xmm0
	cvttsd2si	%xmm0, %esi
	movq	%rsi, %rcx
	shlq	$32, %rcx
	orq	%rsi, %rcx
	leal	6(%rsi), %eax
	xorl	%edx, %edx
	divl	%esi
                                        # kill: def $eax killed $eax def $rax
	movq	%rax, %rdi
	shlq	$32, %rdi
	orq	%rax, %rdi
	movl	$1, %esi
	movq	%rcx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_22
# %bb.21:                               #   in Loop: Header=BB1_20 Depth=1
	movq	(%rsp), %rdi
	movq	160(%rsp,%r14,2), %rsi
	movl	52(%rsp,%r14), %r9d
	movl	24(%rsp,%r14), %eax
	subq	$8, %rsp
	.cfi_adjust_cfa_offset 8
	movl	$7, %edx
	movl	$3, %ecx
	movl	$3, %r8d
	pushq	%r15
	.cfi_adjust_cfa_offset 8
	pushq	144(%rsp,%r14,2)
	.cfi_adjust_cfa_offset 8
	pushq	%rax
	.cfi_adjust_cfa_offset 8
	callq	_Z20__device_stub__conv2PfS_iiiiiS_i
	addq	$32, %rsp
	.cfi_adjust_cfa_offset -32
.LBB1_22:                               #   in Loop: Header=BB1_20 Depth=1
	addq	$4, %r14
	cmpq	$12, %r14
	jne	.LBB1_20
# %bb.23:                               # %.preheader145.preheader
	movq	%r12, 64(%rsp)                  # 8-byte Spill
	xorl	%ebx, %ebx
.LBB1_24:                               # %.preheader145
                                        # =>This Inner Loop Header: Depth=1
	movq	192(%rsp,%rbx,8), %rdi
	movq	128(%rsp,%rbx,8), %rsi
	movslq	12(%rsp,%rbx,4), %rdx
	imulq	%rdx, %rdx
	shlq	$2, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	incq	%rbx
	cmpq	$3, %rbx
	jne	.LBB1_24
# %bb.25:
	xorl	%ebx, %ebx
	leaq	248(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movl	(%r14), %eax
	subl	112(%rsp), %eax
	imull	$1000000, %eax, %eax            # imm = 0xF4240
	addl	8(%r14), %eax
	subl	120(%rsp), %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	divss	.LCPI1_1(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.1, %edi
	movl	$.L.str.2, %esi
	callq	fopen
	movq	%rax, 80(%rsp)
	movl	$.L.str.3, %edi
	movl	$.L.str.2, %esi
	callq	fopen
	movq	%rax, 88(%rsp)
	movl	$.L.str.4, %edi
	movl	$.L.str.2, %esi
	callq	fopen
	movq	%rax, 96(%rsp)
.LBB1_26:                               # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_28 Depth 2
                                        #       Child Loop BB1_29 Depth 3
	movq	80(%rsp,%rbx,8), %r14
	movl	$.L.str.5, %edi
	movl	$7, %esi
	movl	$1, %edx
	movq	%r14, %rcx
	callq	fwrite@PLT
	movq	%rbx, 40(%rsp)                  # 8-byte Spill
	movl	12(%rsp,%rbx,4), %r12d
	testl	%r12d, %r12d
	jle	.LBB1_31
# %bb.27:                               # %.preheader.lr.ph
                                        #   in Loop: Header=BB1_26 Depth=1
	movq	40(%rsp), %rax                  # 8-byte Reload
	movq	192(%rsp,%rax,8), %rax
	movq	%rax, 72(%rsp)                  # 8-byte Spill
	xorl	%ebp, %ebp
	xorl	%ebx, %ebx
.LBB1_28:                               # %.preheader
                                        #   Parent Loop BB1_26 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_29 Depth 3
	movl	%ebp, %eax
	movq	72(%rsp), %rcx                  # 8-byte Reload
	leaq	(%rcx,%rax,4), %r15
	xorl	%r13d, %r13d
.LBB1_29:                               #   Parent Loop BB1_26 Depth=1
                                        #     Parent Loop BB1_28 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r15,%r13,4), %xmm0
	movl	$.L.str.6, %esi
	movq	%r14, %rdi
	movb	$1, %al
	callq	fprintf
	incq	%r13
	cmpq	%r13, %r12
	jne	.LBB1_29
# %bb.30:                               # %._crit_edge
                                        #   in Loop: Header=BB1_28 Depth=2
	movl	$10, %edi
	movq	%r14, %rsi
	callq	fputc@PLT
	incq	%rbx
	addl	%r12d, %ebp
	cmpq	%r12, %rbx
	jne	.LBB1_28
.LBB1_31:                               # %._crit_edge162
                                        #   in Loop: Header=BB1_26 Depth=1
	movl	$.L.str.8, %edi
	movl	$2, %esi
	movl	$1, %edx
	movq	%r14, %rcx
	callq	fwrite@PLT
	movq	40(%rsp), %rbx                  # 8-byte Reload
	incq	%rbx
	cmpq	$3, %rbx
	jne	.LBB1_26
# %bb.32:
	movq	64(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	(%rsp), %rdi
	callq	hipFree
	xorl	%ebx, %ebx
.LBB1_33:                               # =>This Inner Loop Header: Depth=1
	movq	224(%rsp,%rbx,8), %rdi
	callq	free
	movq	192(%rsp,%rbx,8), %rdi
	callq	free
	movq	128(%rsp,%rbx,8), %rdi
	callq	hipFree
	movq	160(%rsp,%rbx,8), %rdi
	callq	hipFree
	movq	80(%rsp,%rbx,8), %rdi
	callq	fclose
	incq	%rbx
	cmpq	$3, %rbx
	jne	.LBB1_33
# %bb.34:
	xorl	%eax, %eax
	addq	$264, %rsp                      # imm = 0x108
	.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_end1:
	.size	main, .Lfunc_end1-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	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z5conv2PfS_iiiiiS_i, %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_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z5conv2PfS_iiiiiS_i,@object    # @_Z5conv2PfS_iiiiiS_i
	.section	.rodata,"a",@progbits
	.globl	_Z5conv2PfS_iiiiiS_i
	.p2align	3, 0x0
_Z5conv2PfS_iiiiiS_i:
	.quad	_Z20__device_stub__conv2PfS_iiiiiS_i
	.size	_Z5conv2PfS_iiiiiS_i, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"total time is %f ms\n"
	.size	.L.str, 21

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"matrixB11.m"
	.size	.L.str.1, 12

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"wb"
	.size	.L.str.2, 3

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"matrixB12.m"
	.size	.L.str.3, 12

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"matrixB13.m"
	.size	.L.str.4, 12

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"B = [ \n"
	.size	.L.str.5, 8

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

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"];"
	.size	.L.str.8, 3

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z5conv2PfS_iiiiiS_i"
	.size	.L__unnamed_1, 21

	.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

	.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 _Z20__device_stub__conv2PfS_iiiiiS_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z5conv2PfS_iiiiiS_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z5conv2PfS_iiiiiS_i    ; -- Begin function _Z5conv2PfS_iiiiiS_i
	.globl	_Z5conv2PfS_iiiiiS_i
	.p2align	8
	.type	_Z5conv2PfS_iiiiiS_i,@function
_Z5conv2PfS_iiiiiS_i:                   ; @_Z5conv2PfS_iiiiiS_i
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x44
	s_load_b32 s3, s[0:1], 0x30
	v_and_b32_e32 v2, 0x3ff, v0
	v_bfe_u32 v3, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s4, s2, 16
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[0:1], null, s14, s2, v[2:3]
	v_mad_u64_u32 v[1:2], null, s15, s4, v[3:4]
	s_mov_b32 s2, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_max_i32_e32 v2, v0, v1
	v_cmpx_gt_i32_e64 s3, v2
	s_cbranch_execz .LBB0_14
; %bb.1:
	s_clause 0x1
	s_load_b128 s[4:7], s[0:1], 0x10
	s_load_b64 s[12:13], s[0:1], 0x28
	s_waitcnt lgkmcnt(0)
	s_cmp_lt_i32 s5, 1
	s_cbranch_scc1 .LBB0_12
; %bb.2:                                ; %.preheader88.lr.ph
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x20
	s_load_b128 s[8:11], s[0:1], 0x0
	v_mul_lo_u32 v2, v0, s7
	v_mov_b32_e32 v10, 0
	s_mul_i32 s14, s6, s6
	s_mul_i32 s15, s4, s4
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_sub_nc_u32_e32 v3, s2, v2
	s_add_i32 s0, s2, s4
	v_sub_nc_u32_e32 v6, s0, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_max_i32_e32 v9, 0, v3
	v_mul_lo_u32 v3, v1, s7
	s_mov_b32 s7, 0
	v_min_i32_e32 v11, s6, v6
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v4, v9, v2
	v_cmp_lt_i32_e32 vcc_lo, v9, v11
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_sub_nc_u32_e32 v5, s2, v3
	v_subrev_nc_u32_e32 v4, s2, v4
	v_sub_nc_u32_e32 v7, s0, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_max_i32_e32 v2, 0, v5
	v_mul_lo_u32 v4, s4, v4
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_min_i32_e32 v12, s6, v7
	v_cmp_lt_i32_e64 s0, v2, v12
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add3_u32 v5, v2, v4, v3
	v_mad_u64_u32 v[3:4], null, s6, v9, v[2:3]
	v_subrev_nc_u32_e32 v13, s2, v5
.LBB0_3:                                ; %.preheader88
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_5 Depth 2
                                        ;       Child Loop BB0_7 Depth 3
	s_and_saveexec_b32 s16, vcc_lo
	s_cbranch_execz .LBB0_11
; %bb.4:                                ; %.preheader.lr.ph
                                        ;   in Loop: Header=BB0_3 Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_mov_b32_e32 v14, v13
	v_dual_mov_b32 v4, v3 :: v_dual_mov_b32 v15, v9
	s_mov_b32 s17, 0
.LBB0_5:                                ; %.preheader
                                        ;   Parent Loop BB0_3 Depth=1
                                        ; =>  This Loop Header: Depth=2
                                        ;       Child Loop BB0_7 Depth 3
	s_and_saveexec_b32 s18, s0
	s_cbranch_execz .LBB0_9
; %bb.6:                                ; %.lr.ph
                                        ;   in Loop: Header=BB0_5 Depth=2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_ashrrev_i32_e32 v5, 31, v4
	v_mov_b32_e32 v16, v2
	s_mov_b32 s19, 0
	v_lshlrev_b64 v[7:8], 2, v[4:5]
	v_mov_b32_e32 v5, v14
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v7, s1, s10, v7
	v_add_co_ci_u32_e64 v8, s1, s11, v8, s1
.LBB0_7:                                ;   Parent Loop BB0_3 Depth=1
                                        ;     Parent Loop BB0_5 Depth=2
                                        ; =>    This Inner Loop Header: Depth=3
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_ashrrev_i32_e32 v6, 31, v5
	v_add_nc_u32_e32 v16, 1, v16
	v_lshlrev_b64 v[17:18], 2, v[5:6]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ge_i32_e64 s2, v16, v12
	s_or_b32 s19, s2, s19
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v17, s1, s8, v17
	v_add_co_ci_u32_e64 v18, s1, s9, v18, s1
	global_load_b32 v6, v[7:8], off
	global_load_b32 v17, v[17:18], off
	v_add_co_u32 v7, s1, v7, 4
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v8, s1, 0, v8, s1
	s_waitcnt vmcnt(0)
	v_dual_fmac_f32 v10, v17, v6 :: v_dual_add_nc_u32 v5, 1, v5
	s_and_not1_b32 exec_lo, exec_lo, s19
	s_cbranch_execnz .LBB0_7
; %bb.8:                                ; %Flow
                                        ;   in Loop: Header=BB0_5 Depth=2
	s_or_b32 exec_lo, exec_lo, s19
.LBB0_9:                                ; %Flow135
                                        ;   in Loop: Header=BB0_5 Depth=2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(VALU_DEP_3)
	s_or_b32 exec_lo, exec_lo, s18
	v_add_nc_u32_e32 v15, 1, v15
	v_add_nc_u32_e32 v4, s6, v4
	v_add_nc_u32_e32 v14, s4, v14
	v_cmp_ge_i32_e64 s1, v15, v11
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s17, s1, s17
	s_and_not1_b32 exec_lo, exec_lo, s17
	s_cbranch_execnz .LBB0_5
; %bb.10:                               ; %Flow136
                                        ;   in Loop: Header=BB0_3 Depth=1
	s_or_b32 exec_lo, exec_lo, s17
.LBB0_11:                               ; %Flow137
                                        ;   in Loop: Header=BB0_3 Depth=1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	s_or_b32 exec_lo, exec_lo, s16
	v_mad_u64_u32 v[4:5], null, s7, s3, v[0:1]
	v_add_nc_u32_e32 v3, s14, v3
	v_add_nc_u32_e32 v13, s15, v13
	s_add_i32 s7, s7, 1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	s_cmp_eq_u32 s7, s5
	v_mad_u64_u32 v[5:6], null, v4, s3, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v6, 31, v5
	v_lshlrev_b64 v[4:5], 2, v[5:6]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v4, s1, s12, v4
	v_add_co_ci_u32_e64 v5, s1, s13, v5, s1
	global_store_b32 v[4:5], v10, off
	s_cbranch_scc0 .LBB0_3
	s_branch .LBB0_13
.LBB0_12:
	v_mov_b32_e32 v10, 0
.LBB0_13:                               ; %._crit_edge97
	v_mad_u64_u32 v[2:3], null, v0, s3, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v3, 31, v2
	v_lshlrev_b64 v[0:1], 2, v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v0, vcc_lo, s12, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s13, v1, vcc_lo
	global_store_b32 v[0:1], v10, off
.LBB0_14:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z5conv2PfS_iiiiiS_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 312
		.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 19
		.amdhsa_next_free_sgpr 20
		.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	_Z5conv2PfS_iiiiiS_i, .Lfunc_end0-_Z5conv2PfS_iiiiiS_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 704
; NumSgprs: 22
; NumVgprs: 19
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 22
; NumVGPRsForWavesPerEU: 19
; 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:
      - .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:         28
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         40
        .size:           8
        .value_kind:     global_buffer
      - .offset:         48
        .size:           4
        .value_kind:     by_value
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         60
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         64
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         68
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         70
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         72
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         74
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         76
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         78
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         112
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         120
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 312
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z5conv2PfS_iiiiiS_i
    .private_segment_fixed_size: 0
    .sgpr_count:     22
    .sgpr_spill_count: 0
    .symbol:         _Z5conv2PfS_iiiiiS_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     19
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
