#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "driver_types.h"
#include <stdio.h>
#include <fstream>

#define BLOCK_SIZE 16

typedef struct
{
    int width;
    int height;
    float* elements;
} Matrix;

__global__ void mat_mul_kernel(const Matrix a, const Matrix b, Matrix c) {
    int row = blockIdx.y*blockDim.y + threadIdx.y;
    int col = blockIdx.x*blockDim.x + threadIdx.x;

    float res = 0.0;
    for (int i = 0; i < a.width; ++i) {
        res += a.elements[row*a.height + i] * b.elements[i*b.width + col];
    }
    c.elements[row*c.width + col] = res;
}

int mat_mul(const Matrix a, const Matrix b, Matrix c) 
{
    //1 copy host memory to device(cudaMalloc + cudaMemcpy)
    cudaError_t cuda_error = cudaSuccess;
    Matrix d_a;
    d_a.width = a.width;
    d_a.height = a.height;
    cuda_error = cudaMalloc(&(d_a.elements), d_a.width*d_a.height*sizeof(float));
    if (cuda_error != cudaSuccess) {
        printf("cuda malloc failed." );
        return -1;
    }
    cuda_error = cudaMemcpy(d_a.elements, a.elements, d_a.width*d_a.height * sizeof(float), cudaMemcpyHostToDevice);
    if (cuda_error != cudaSuccess) {
        printf("cuda memcpy failed.");
        return -1;
    }

    Matrix d_b;
    d_b.width = b.width;
    d_b.height = b.height;
    cuda_error = cudaMalloc(&(d_b.elements), d_b.width*d_b.height * sizeof(float));
    if (cuda_error != cudaSuccess) {
        printf("cuda malloc failed.");
        return -1;
    }
    cuda_error = cudaMemcpy(d_b.elements, b.elements, d_b.width*d_b.height * sizeof(float), cudaMemcpyHostToDevice);
    if (cuda_error != cudaSuccess) {
        printf("cuda memcpy failed.");
        return -1;
    }

    Matrix d_c;
    d_c.width = c.width;
    d_c.height = c.height;
    cuda_error = cudaMalloc(&(d_c.elements), d_c.width*d_c.height * sizeof(float));
    if (cuda_error != cudaSuccess) {
        printf("cuda malloc failed.");
        return -1;
    }

    //2 invoke kernel to calculate (block thread)
    dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);
    dim3 dimGrid(b.width / dimBlock.x, a.height / dimBlock.y);
    //dim3 dimGrid = (4, 4);
    mat_mul_kernel<<<dimGrid,dimBlock>>>(d_a, d_b, d_c);

    //3 download to host
    cudaMemcpy(c.elements, d_c.elements, c.width*c.height*sizeof(float) , cudaMemcpyDeviceToHost);
    if (cuda_error != cudaSuccess) {
        printf("cuda memcpy failed.");
        return -1;
    }

    //4 release the device memeory
    cudaFree(d_a.elements);
    cudaFree(d_b.elements);
    cudaFree(d_c.elements);

    return 0;
}

int save_matrix(Matrix m, const char* file_name) {
    std::ofstream out(file_name, std::ios::out);
    if (!out.is_open()) {
        return -1;
    }

    for (int row = 0; row < m.height; ++row) {
        for (int col= 0; col < m.width; ++col) {
            out << m.elements[row*m.height + col] << " ";
        }
        out << std::endl;
    }

    out.close();
    return 0;
}

int cuda_matrix_mul(int argc , char* argv[]) 
{
    Matrix a;
    a.width = 64;
    a.height = 64;
    a.elements = new float[64*64];
    for (int i = 0; i < 64 * 64; ++i) {
        a.elements[i] = 1.0f;
    }

    Matrix b;
    b.width = 64;
    b.height = 64;
    b.elements = new float[64 * 64];
    for (int i = 0; i < 64 * 64; ++i) {
        b.elements[i] = 2.0f;
    }

    Matrix c;
    c.width = 64;
    c.height = 64;
    c.elements = new float[64 * 64];
    for (int i = 0; i < 64 * 64; ++i) {
        c.elements[i] = 0;
    }

    if (0 == mat_mul(a, b, c)) {
        printf("Success.\n");
        save_matrix(c, "D:/temp/mat.txt");
    }
    else {
        printf("Failed.\n");
    }
    
    return 0;
}
	.file	"tmpxft_00318ca1_00000000-6_cuda_matrix_mul.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3771:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3771:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z43__device_stub__Z14mat_mul_kernel6MatrixS_S_RK6MatrixS1_RS_
	.type	_Z43__device_stub__Z14mat_mul_kernel6MatrixS_S_RK6MatrixS1_RS_, @function
_Z43__device_stub__Z14mat_mul_kernel6MatrixS_S_RK6MatrixS1_RS_:
.LFB3793:
	.cfi_startproc
	endbr64
	subq	$104, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rcx
	movq	%rdi, 64(%rsp)
	movabsq	$4294967297, %rax
	movq	%rsi, 72(%rsp)
	leaq	16(%rsp), %rdi
	leaq	28(%rsp), %rsi
	movq	%rdx, 80(%rsp)
	movq	%rsp, %rdx
	movq	%rax, 16(%rsp)
	movl	$1, 24(%rsp)
	movq	%rax, 28(%rsp)
	movl	$1, 36(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	8(%rsp)
	.cfi_def_cfa_offset 120
	leaq	_Z14mat_mul_kernel6MatrixS_S_(%rip), %rdi
	pushq	8(%rsp)
	.cfi_def_cfa_offset 128
	movq	44(%rsp), %rcx
	movl	52(%rsp), %r8d
	movq	32(%rsp), %rsi
	movl	40(%rsp), %edx
	leaq	80(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 120
	popq	%rdx
	.cfi_def_cfa_offset 112
.L2:
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$104, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3793:
	.size	_Z43__device_stub__Z14mat_mul_kernel6MatrixS_S_RK6MatrixS1_RS_, .-_Z43__device_stub__Z14mat_mul_kernel6MatrixS_S_RK6MatrixS1_RS_
	.globl	_Z14mat_mul_kernel6MatrixS_S_
	.type	_Z14mat_mul_kernel6MatrixS_S_, @function
_Z14mat_mul_kernel6MatrixS_S_:
.LFB3794:
	.cfi_startproc
	endbr64
	subq	$56, %rsp
	.cfi_def_cfa_offset 64
	movq	%rdi, 32(%rsp)
	leaq	32(%rsp), %rdi
	movq	%rsi, 40(%rsp)
	leaq	16(%rsp), %rsi
	movq	%rdx, 16(%rsp)
	movq	%rsp, %rdx
	movq	%rcx, 24(%rsp)
	movq	%r8, (%rsp)
	movq	%r9, 8(%rsp)
	call	_Z43__device_stub__Z14mat_mul_kernel6MatrixS_S_RK6MatrixS1_RS_
	addq	$56, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3794:
	.size	_Z14mat_mul_kernel6MatrixS_S_, .-_Z14mat_mul_kernel6MatrixS_S_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"cuda malloc failed."
.LC1:
	.string	"cuda memcpy failed."
	.text
	.globl	_Z7mat_mul6MatrixS_S_
	.type	_Z7mat_mul6MatrixS_S_, @function
_Z7mat_mul6MatrixS_S_:
.LFB3766:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %rax
	movq	%rsi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rcx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%r8, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	sarq	$32, %r12
	movq	%rdx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	imull	%r12d, %eax
	movq	%r8, %rbx
	sarq	$32, %rbx
	subq	$152, %rsp
	.cfi_def_cfa_offset 208
	movslq	%eax, %rsi
	movq	%r9, 8(%rsp)
	salq	$2, %rsi
	movq	%fs:40, %rdx
	movq	%rdx, 136(%rsp)
	xorl	%edx, %edx
	movl	%edi, 40(%rsp)
	leaq	48(%rsp), %rdi
	movl	%r12d, 44(%rsp)
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L10
.L14:
	leaq	.LC0(%rip), %rsi
	jmp	.L19
.L10:
	movl	40(%rsp), %edx
	imull	44(%rsp), %edx
	movl	$1, %ecx
	movq	%r15, %rsi
	movq	48(%rsp), %rdi
	movslq	%edx, %rdx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L12
.L15:
	leaq	.LC1(%rip), %rsi
.L19:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	orl	$-1, %eax
	jmp	.L9
.L12:
	movq	%rbp, %rsi
	leaq	64(%rsp), %rdi
	movl	%ebp, 56(%rsp)
	sarq	$32, %rsi
	movl	%esi, 60(%rsp)
	imull	%ebp, %esi
	movslq	%esi, %rsi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	testl	%eax, %eax
	jne	.L14
	movl	56(%rsp), %edx
	imull	60(%rsp), %edx
	movl	$1, %ecx
	movq	%r14, %rsi
	movq	64(%rsp), %rdi
	movslq	%edx, %rdx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	jne	.L15
	movl	%ebx, 76(%rsp)
	imull	%r13d, %ebx
	leaq	80(%rsp), %rdi
	movl	%r13d, 72(%rsp)
	movslq	%ebx, %rbx
	salq	$2, %rbx
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	testl	%eax, %eax
	jne	.L14
	shrl	$4, %ebp
	shrl	$4, %r12d
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	%ebp, 28(%rsp)
	movl	$1, %ecx
	movl	$1, %esi
	movabsq	$68719476752, %rdx
	movl	%r12d, 32(%rsp)
	movq	28(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L16
	movups	40(%rsp), %xmm0
	movups	56(%rsp), %xmm1
	leaq	120(%rsp), %rdx
	leaq	104(%rsp), %rsi
	movups	72(%rsp), %xmm2
	leaq	88(%rsp), %rdi
	movups	%xmm0, 88(%rsp)
	movups	%xmm1, 104(%rsp)
	movups	%xmm2, 120(%rsp)
	call	_Z43__device_stub__Z14mat_mul_kernel6MatrixS_S_RK6MatrixS1_RS_
.L16:
	movq	80(%rsp), %rsi
	movq	8(%rsp), %rdi
	movl	$2, %ecx
	movq	%rbx, %rdx
	call	cudaMemcpy@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	xorl	%eax, %eax
.L9:
	movq	136(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L17
	call	__stack_chk_fail@PLT
.L17:
	addq	$152, %rsp
	.cfi_def_cfa_offset 56
	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
.LFE3766:
	.size	_Z7mat_mul6MatrixS_S_, .-_Z7mat_mul6MatrixS_S_
	.section	.rodata.str1.1
.LC2:
	.string	"_Z14mat_mul_kernel6MatrixS_S_"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3796:
	.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	.LC2(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z14mat_mul_kernel6MatrixS_S_(%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
.LFE3796:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.rodata.str1.1
.LC3:
	.string	" "
	.text
	.globl	_Z11save_matrix6MatrixPKc
	.type	_Z11save_matrix6MatrixPKc, @function
_Z11save_matrix6MatrixPKc:
.LFB3767:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA3767
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rsi, %r15
	movq	%rdx, %rsi
	movl	$16, %edx
	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	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rdi, %rbx
	sarq	$32, %rbx
	subq	$552, %rsp
	.cfi_def_cfa_offset 608
	movq	%fs:40, %rax
	movq	%rax, 536(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	%ebx, 8(%rsp)
	movq	%rax, %rdi
	movq	%rax, (%rsp)
.LEHB0:
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode@PLT
.LEHE0:
	leaq	136(%rsp), %rdi
	call	_ZNKSt12__basic_fileIcE7is_openEv@PLT
	testb	%al, %al
	je	.L31
	movl	%ebx, 12(%rsp)
	xorl	%r13d, %r13d
	xorl	%r14d, %r14d
.L24:
	movl	8(%rsp), %eax
	cmpl	%eax, %r14d
	jge	.L26
	movslq	%r13d, %rbx
	xorl	%r12d, %r12d
	salq	$2, %rbx
.L27:
	cmpl	%ebp, %r12d
	jge	.L35
	movq	(%rsp), %rdi
	cvtss2sd	(%rbx,%r15), %xmm0
.LEHB1:
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	movq	%rax, %rdi
	movl	$1, %edx
	leaq	.LC3(%rip), %rsi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
	incl	%r12d
	addq	$4, %rbx
	jmp	.L27
.L35:
	movq	(%rsp), %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movl	12(%rsp), %eax
	incl	%r14d
	addl	%eax, %r13d
	jmp	.L24
.L26:
	movq	(%rsp), %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEE5closeEv@PLT
.LEHE1:
	xorl	%ebx, %ebx
	jmp	.L23
.L31:
	orl	$-1, %ebx
.L23:
	movq	(%rsp), %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	536(%rsp), %rax
	subq	%fs:40, %rax
	je	.L30
	jmp	.L33
.L32:
	endbr64
	movq	%rax, %rbx
.L28:
	movq	(%rsp), %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	536(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L33
	movq	%rbx, %rdi
.LEHB2:
	call	_Unwind_Resume@PLT
.LEHE2:
.L33:
	call	__stack_chk_fail@PLT
.L30:
	addq	$552, %rsp
	.cfi_def_cfa_offset 56
	movl	%ebx, %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
.LFE3767:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA3767:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE3767-.LLSDACSB3767
.LLSDACSB3767:
	.uleb128 .LEHB0-.LFB3767
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB3767
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L32-.LFB3767
	.uleb128 0
	.uleb128 .LEHB2-.LFB3767
	.uleb128 .LEHE2-.LEHB2
	.uleb128 0
	.uleb128 0
.LLSDACSE3767:
	.text
	.size	_Z11save_matrix6MatrixPKc, .-_Z11save_matrix6MatrixPKc
	.section	.rodata.str1.1
.LC6:
	.string	"Success.\n"
.LC7:
	.string	"D:/temp/mat.txt"
.LC8:
	.string	"Failed.\n"
	.text
	.globl	_Z15cuda_matrix_muliPPc
	.type	_Z15cuda_matrix_muliPPc, @function
_Z15cuda_matrix_muliPPc:
.LFB3768:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	$16384, %edi
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	call	_Znam@PLT
	movss	.LC4(%rip), %xmm0
	movq	%rax, %r12
	xorl	%eax, %eax
.L37:
	movss	%xmm0, (%r12,%rax,4)
	incq	%rax
	cmpq	$4096, %rax
	jne	.L37
	movl	$16384, %edi
	xorl	%ebx, %ebx
	call	_Znam@PLT
	movss	.LC5(%rip), %xmm0
	movq	%rax, %r13
.L38:
	movss	%xmm0, 0(%r13,%rbx,4)
	incq	%rbx
	cmpq	$4096, %rbx
	jne	.L38
	movabsq	$274877907008, %r14
	movl	$16384, %edi
	call	_Znam@PLT
	movq	%rbx, %rcx
	movq	%r14, %r8
	movq	%r14, %rdx
	movq	%rax, %rbp
	xorl	%eax, %eax
	movq	%r12, %rsi
	movq	%rbp, %rdi
	movq	%rbp, %r9
	rep stosl
	movq	%r13, %rcx
	movq	%r14, %rdi
	call	_Z7mat_mul6MatrixS_S_
	testl	%eax, %eax
	jne	.L39
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC7(%rip), %rdx
	movq	%r14, %rdi
	movq	%rbp, %rsi
	call	_Z11save_matrix6MatrixPKc
	jmp	.L40
.L39:
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L40:
	popq	%rbx
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3768:
	.size	_Z15cuda_matrix_muliPPc, .-_Z15cuda_matrix_muliPPc
	.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
.LC4:
	.long	1065353216
	.align 4
.LC5:
	.long	1073741824
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.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 "hip/driver_types.h"
#include <stdio.h>
#include <fstream>

#define BLOCK_SIZE 16

typedef struct
{
    int width;
    int height;
    float* elements;
} Matrix;

__global__ void mat_mul_kernel(const Matrix a, const Matrix b, Matrix c) {
    int row = blockIdx.y*blockDim.y + threadIdx.y;
    int col = blockIdx.x*blockDim.x + threadIdx.x;

    float res = 0.0;
    for (int i = 0; i < a.width; ++i) {
        res += a.elements[row*a.height + i] * b.elements[i*b.width + col];
    }
    c.elements[row*c.width + col] = res;
}

int mat_mul(const Matrix a, const Matrix b, Matrix c) 
{
    //1 copy host memory to device(cudaMalloc + cudaMemcpy)
    hipError_t cuda_error = hipSuccess;
    Matrix d_a;
    d_a.width = a.width;
    d_a.height = a.height;
    cuda_error = hipMalloc(&(d_a.elements), d_a.width*d_a.height*sizeof(float));
    if (cuda_error != hipSuccess) {
        printf("cuda malloc failed." );
        return -1;
    }
    cuda_error = hipMemcpy(d_a.elements, a.elements, d_a.width*d_a.height * sizeof(float), hipMemcpyHostToDevice);
    if (cuda_error != hipSuccess) {
        printf("cuda memcpy failed.");
        return -1;
    }

    Matrix d_b;
    d_b.width = b.width;
    d_b.height = b.height;
    cuda_error = hipMalloc(&(d_b.elements), d_b.width*d_b.height * sizeof(float));
    if (cuda_error != hipSuccess) {
        printf("cuda malloc failed.");
        return -1;
    }
    cuda_error = hipMemcpy(d_b.elements, b.elements, d_b.width*d_b.height * sizeof(float), hipMemcpyHostToDevice);
    if (cuda_error != hipSuccess) {
        printf("cuda memcpy failed.");
        return -1;
    }

    Matrix d_c;
    d_c.width = c.width;
    d_c.height = c.height;
    cuda_error = hipMalloc(&(d_c.elements), d_c.width*d_c.height * sizeof(float));
    if (cuda_error != hipSuccess) {
        printf("cuda malloc failed.");
        return -1;
    }

    //2 invoke kernel to calculate (block thread)
    dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);
    dim3 dimGrid(b.width / dimBlock.x, a.height / dimBlock.y);
    //dim3 dimGrid = (4, 4);
    mat_mul_kernel<<<dimGrid,dimBlock>>>(d_a, d_b, d_c);

    //3 download to host
    hipMemcpy(c.elements, d_c.elements, c.width*c.height*sizeof(float) , hipMemcpyDeviceToHost);
    if (cuda_error != hipSuccess) {
        printf("cuda memcpy failed.");
        return -1;
    }

    //4 release the device memeory
    hipFree(d_a.elements);
    hipFree(d_b.elements);
    hipFree(d_c.elements);

    return 0;
}

int save_matrix(Matrix m, const char* file_name) {
    std::ofstream out(file_name, std::ios::out);
    if (!out.is_open()) {
        return -1;
    }

    for (int row = 0; row < m.height; ++row) {
        for (int col= 0; col < m.width; ++col) {
            out << m.elements[row*m.height + col] << " ";
        }
        out << std::endl;
    }

    out.close();
    return 0;
}

int cuda_matrix_mul(int argc , char* argv[]) 
{
    Matrix a;
    a.width = 64;
    a.height = 64;
    a.elements = new float[64*64];
    for (int i = 0; i < 64 * 64; ++i) {
        a.elements[i] = 1.0f;
    }

    Matrix b;
    b.width = 64;
    b.height = 64;
    b.elements = new float[64 * 64];
    for (int i = 0; i < 64 * 64; ++i) {
        b.elements[i] = 2.0f;
    }

    Matrix c;
    c.width = 64;
    c.height = 64;
    c.elements = new float[64 * 64];
    for (int i = 0; i < 64 * 64; ++i) {
        c.elements[i] = 0;
    }

    if (0 == mat_mul(a, b, c)) {
        printf("Success.\n");
        save_matrix(c, "D:/temp/mat.txt");
    }
    else {
        printf("Failed.\n");
    }
    
    return 0;
}
	.text
	.file	"cuda_matrix_mul.hip"
	.globl	_Z29__device_stub__mat_mul_kernel6MatrixS_S_ # -- Begin function _Z29__device_stub__mat_mul_kernel6MatrixS_S_
	.type	_Z29__device_stub__mat_mul_kernel6MatrixS_S_,@function
_Z29__device_stub__mat_mul_kernel6MatrixS_S_: # @_Z29__device_stub__mat_mul_kernel6MatrixS_S_
	.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	80(%rsp), %rax
	movq	%rdi, (%rax)
	movq	%rsi, 8(%rax)
	leaq	64(%rsp), %rsi
	movq	%rdx, (%rsi)
	movq	%rcx, 8(%rsi)
	leaq	48(%rsp), %rcx
	movq	%r8, (%rcx)
	movq	%r9, 8(%rcx)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rsi, 8(%rbx)
	movq	%rcx, 16(%rbx)
	leaq	32(%rsp), %r14
	leaq	16(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%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	$_Z14mat_mul_kernel6MatrixS_S_, %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_end0:
	.size	_Z29__device_stub__mat_mul_kernel6MatrixS_S_, .Lfunc_end0-_Z29__device_stub__mat_mul_kernel6MatrixS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z7mat_mul6MatrixS_S_           # -- Begin function _Z7mat_mul6MatrixS_S_
	.type	_Z7mat_mul6MatrixS_S_,@function
_Z7mat_mul6MatrixS_S_:                  # @_Z7mat_mul6MatrixS_S_
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%r9, %rbx
	movq	%r8, %r12
	movq	%rcx, %r13
	movq	%rdx, %r14
	movq	%rsi, %rbp
	movq	%rdi, %r15
	movq	%rdi, %rax
	shrq	$32, %rax
	leaq	32(%rsp), %rdi
	movq	%r15, -8(%rdi)
	imull	%r15d, %eax
	movslq	%eax, %rsi
	shlq	$2, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	je	.LBB1_3
.LBB1_1:
	movl	$.L.str, %edi
	jmp	.LBB1_2
.LBB1_3:
	movq	32(%rsp), %rdi
	movslq	24(%rsp), %rax
	movslq	28(%rsp), %rdx
	imulq	%rax, %rdx
	shlq	$2, %rdx
	movq	%rbp, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	je	.LBB1_5
.LBB1_4:
	movl	$.L.str.1, %edi
.LBB1_2:
	xorl	%eax, %eax
	callq	printf
	movl	$-1, %r14d
.LBB1_11:
	movl	%r14d, %eax
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB1_5:
	.cfi_def_cfa_offset 112
	movq	%r14, %rax
	shrq	$32, %rax
	leaq	16(%rsp), %rdi
	movl	%r14d, -8(%rdi)
	movl	%eax, -4(%rdi)
	imull	%r14d, %eax
	movslq	%eax, %rsi
	shlq	$2, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB1_1
# %bb.6:
	movq	16(%rsp), %rdi
	movslq	8(%rsp), %rax
	movslq	12(%rsp), %rdx
	imulq	%rax, %rdx
	shlq	$2, %rdx
	movq	%r13, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB1_4
# %bb.7:
	movq	%r12, %rax
	shrq	$32, %rax
	leaq	48(%rsp), %rdi
	movl	%r12d, -8(%rdi)
	movl	%eax, -4(%rdi)
	imull	%r12d, %eax
	movslq	%eax, %r12
	shlq	$2, %r12
	movq	%r12, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB1_1
# %bb.8:
	shrl	$4, %r14d
	shrq	$4, %r15
	movabsq	$1152921500311879680, %rdi      # imm = 0xFFFFFFF00000000
	andq	%r15, %rdi
	orq	%r14, %rdi
	xorl	%r14d, %r14d
	movabsq	$68719476752, %rdx              # imm = 0x1000000010
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_10
# %bb.9:
	movq	24(%rsp), %rdi
	movq	32(%rsp), %rsi
	movq	8(%rsp), %rdx
	movq	16(%rsp), %rcx
	movq	40(%rsp), %r8
	movq	48(%rsp), %r9
	callq	_Z29__device_stub__mat_mul_kernel6MatrixS_S_
.LBB1_10:
	movq	48(%rsp), %rsi
	movq	%rbx, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	48(%rsp), %rdi
	callq	hipFree
	jmp	.LBB1_11
.Lfunc_end1:
	.size	_Z7mat_mul6MatrixS_S_, .Lfunc_end1-_Z7mat_mul6MatrixS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z11save_matrix6MatrixPKc       # -- Begin function _Z11save_matrix6MatrixPKc
	.type	_Z11save_matrix6MatrixPKc,@function
_Z11save_matrix6MatrixPKc:              # @_Z11save_matrix6MatrixPKc
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %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	$536, %rsp                      # imm = 0x218
	.cfi_def_cfa_offset 592
	.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, (%rsp)                    # 8-byte Spill
	movq	%rdi, %r14
	leaq	24(%rsp), %r15
	movq	%r15, %rdi
	movq	%rdx, %rsi
	movl	$16, %edx
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode
	leaq	136(%rsp), %rdi
	callq	_ZNKSt12__basic_fileIcE7is_openEv
	movl	$-1, %ebp
	testb	%al, %al
	je	.LBB2_13
# %bb.1:                                # %.preheader19
	movq	%r14, %rax
	shrq	$32, %rax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	testl	%eax, %eax
	jle	.LBB2_12
# %bb.2:                                # %.preheader.lr.ph
	movl	%r14d, %r13d
	xorl	%ebp, %ebp
	xorl	%ebx, %ebx
	movq	%r14, 8(%rsp)                   # 8-byte Spill
.LBB2_3:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_5 Depth 2
	testl	%r14d, %r14d
	jle	.LBB2_8
# %bb.4:                                # %.lr.ph
                                        #   in Loop: Header=BB2_3 Depth=1
	movl	%ebp, %eax
	movq	(%rsp), %rcx                    # 8-byte Reload
	leaq	(%rcx,%rax,4), %r14
	xorl	%r12d, %r12d
.LBB2_5:                                #   Parent Loop BB2_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r14,%r12,4), %xmm0
.Ltmp0:
	movq	%r15, %rdi
	callq	_ZNSo9_M_insertIdEERSoT_
.Ltmp1:
# %bb.6:                                # %_ZNSolsEf.exit
                                        #   in Loop: Header=BB2_5 Depth=2
.Ltmp2:
	movl	$.L.str.2, %esi
	movl	$1, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp3:
# %bb.7:                                # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit
                                        #   in Loop: Header=BB2_5 Depth=2
	incq	%r12
	cmpq	%r12, %r13
	jne	.LBB2_5
.LBB2_8:                                # %._crit_edge
                                        #   in Loop: Header=BB2_3 Depth=1
	movq	24(%rsp), %rax
	movq	-24(%rax), %rdi
	addq	%r15, %rdi
.Ltmp5:
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
.Ltmp6:
# %bb.9:                                # %.noexc
                                        #   in Loop: Header=BB2_3 Depth=1
.Ltmp7:
	movsbl	%al, %esi
	movq	%r15, %rdi
	callq	_ZNSo3putEc
.Ltmp8:
# %bb.10:                               # %.noexc17
                                        #   in Loop: Header=BB2_3 Depth=1
.Ltmp9:
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.Ltmp10:
# %bb.11:                               # %_ZNSolsEPFRSoS_E.exit
                                        #   in Loop: Header=BB2_3 Depth=1
	incq	%rbx
	movq	16(%rsp), %rax                  # 8-byte Reload
	addl	%eax, %ebp
	cmpq	%rax, %rbx
	movq	8(%rsp), %r14                   # 8-byte Reload
	jne	.LBB2_3
.LBB2_12:                               # %._crit_edge22
	xorl	%ebp, %ebp
.Ltmp12:
	leaq	24(%rsp), %rdi
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEE5closeEv
.Ltmp13:
.LBB2_13:
	movq	_ZTTSt14basic_ofstreamIcSt11char_traitsIcEE(%rip), %rax
	leaq	32(%rsp), %rdi
	movq	%rax, -8(%rdi)
	movq	_ZTTSt14basic_ofstreamIcSt11char_traitsIcEE+24(%rip), %rcx
	movq	-24(%rax), %rax
	movq	%rcx, 24(%rsp,%rax)
	callq	_ZNSt13basic_filebufIcSt11char_traitsIcEED2Ev
	leaq	272(%rsp), %rdi
	callq	_ZNSt8ios_baseD2Ev
	movl	%ebp, %eax
	addq	$536, %rsp                      # imm = 0x218
	.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
.LBB2_14:
	.cfi_def_cfa_offset 592
.Ltmp14:
	jmp	.LBB2_15
.LBB2_17:
.Ltmp11:
	jmp	.LBB2_15
.LBB2_16:
.Ltmp4:
.LBB2_15:
	movq	%rax, %rbx
	leaq	24(%rsp), %rdi
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev
	movq	%rbx, %rdi
	callq	_Unwind_Resume@PLT
.Lfunc_end2:
	.size	_Z11save_matrix6MatrixPKc, .Lfunc_end2-_Z11save_matrix6MatrixPKc
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table2:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	255                             # @TType Encoding = omit
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Lfunc_begin0-.Lfunc_begin0    # >> Call Site 1 <<
	.uleb128 .Ltmp0-.Lfunc_begin0           #   Call between .Lfunc_begin0 and .Ltmp0
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 2 <<
	.uleb128 .Ltmp3-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp3
	.uleb128 .Ltmp4-.Lfunc_begin0           #     jumps to .Ltmp4
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp5-.Lfunc_begin0           # >> Call Site 3 <<
	.uleb128 .Ltmp10-.Ltmp5                 #   Call between .Ltmp5 and .Ltmp10
	.uleb128 .Ltmp11-.Lfunc_begin0          #     jumps to .Ltmp11
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp12-.Lfunc_begin0          # >> Call Site 4 <<
	.uleb128 .Ltmp13-.Ltmp12                #   Call between .Ltmp12 and .Ltmp13
	.uleb128 .Ltmp14-.Lfunc_begin0          #     jumps to .Ltmp14
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp13-.Lfunc_begin0          # >> Call Site 5 <<
	.uleb128 .Lfunc_end2-.Ltmp13            #   Call between .Ltmp13 and .Lfunc_end2
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
.Lcst_end0:
	.p2align	2, 0x0
                                        # -- End function
	.text
	.globl	_Z15cuda_matrix_muliPPc         # -- Begin function _Z15cuda_matrix_muliPPc
	.type	_Z15cuda_matrix_muliPPc,@function
_Z15cuda_matrix_muliPPc:                # @_Z15cuda_matrix_muliPPc
	.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	$16384, %edi                    # imm = 0x4000
	callq	_Znam
	movq	%rax, %rbx
	xorl	%eax, %eax
.LBB3_1:                                # =>This Inner Loop Header: Depth=1
	movl	$1065353216, (%rbx,%rax,4)      # imm = 0x3F800000
	incq	%rax
	cmpq	$4096, %rax                     # imm = 0x1000
	jne	.LBB3_1
# %bb.2:
	movl	$16384, %edi                    # imm = 0x4000
	callq	_Znam
	movq	%rax, %r15
	xorl	%eax, %eax
.LBB3_3:                                # =>This Inner Loop Header: Depth=1
	movl	$1073741824, (%r15,%rax,4)      # imm = 0x40000000
	incq	%rax
	cmpq	$4096, %rax                     # imm = 0x1000
	jne	.LBB3_3
# %bb.4:
	movabsq	$274877907008, %r14             # imm = 0x4000000040
	movl	$16384, %edi                    # imm = 0x4000
	callq	_Znam
	movq	%rax, %r12
	movl	$16384, %edx                    # imm = 0x4000
	movq	%rax, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
	movq	%r14, %rdi
	movq	%rbx, %rsi
	movq	%r14, %rdx
	movq	%r15, %rcx
	movq	%r14, %r8
	movq	%r12, %r9
	callq	_Z7mat_mul6MatrixS_S_
	testl	%eax, %eax
	je	.LBB3_5
# %bb.6:
	movl	$.Lstr, %edi
	callq	puts@PLT
	jmp	.LBB3_7
.LBB3_5:
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	$.L.str.4, %edx
	movq	%r14, %rdi
	movq	%r12, %rsi
	callq	_Z11save_matrix6MatrixPKc
.LBB3_7:
	xorl	%eax, %eax
	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
	retq
.Lfunc_end3:
	.size	_Z15cuda_matrix_muliPPc, .Lfunc_end3-_Z15cuda_matrix_muliPPc
	.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	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z14mat_mul_kernel6MatrixS_S_, %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_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__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	.LBB5_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
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z14mat_mul_kernel6MatrixS_S_,@object # @_Z14mat_mul_kernel6MatrixS_S_
	.section	.rodata,"a",@progbits
	.globl	_Z14mat_mul_kernel6MatrixS_S_
	.p2align	3, 0x0
_Z14mat_mul_kernel6MatrixS_S_:
	.quad	_Z29__device_stub__mat_mul_kernel6MatrixS_S_
	.size	_Z14mat_mul_kernel6MatrixS_S_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"cuda malloc failed."
	.size	.L.str, 20

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"cuda memcpy failed."
	.size	.L.str.1, 20

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"D:/temp/mat.txt"
	.size	.L.str.4, 16

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z14mat_mul_kernel6MatrixS_S_"
	.size	.L__unnamed_1, 30

	.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	"Failed."
	.size	.Lstr, 8

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

	.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 _Z29__device_stub__mat_mul_kernel6MatrixS_S_
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Unwind_Resume
	.addrsig_sym _Z14mat_mul_kernel6MatrixS_S_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z14mat_mul_kernel6MatrixS_S_ ; -- Begin function _Z14mat_mul_kernel6MatrixS_S_
	.globl	_Z14mat_mul_kernel6MatrixS_S_
	.p2align	8
	.type	_Z14mat_mul_kernel6MatrixS_S_,@function
_Z14mat_mul_kernel6MatrixS_S_:          ; @_Z14mat_mul_kernel6MatrixS_S_
; %bb.0:
	s_clause 0x2
	s_load_b32 s4, s[0:1], 0x3c
	s_load_b32 s6, s[0:1], 0x0
	s_load_b64 s[2:3], s[0:1], 0x28
	v_bfe_u32 v2, v0, 10, 10
	v_and_b32_e32 v3, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s5, s4, 16
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(VALU_DEP_1)
	v_mad_u64_u32 v[0:1], null, s15, s5, v[2:3]
	v_mad_u64_u32 v[1:2], null, s14, s4, v[3:4]
	s_cmp_lt_i32 s6, 1
	s_cbranch_scc1 .LBB0_3
; %bb.1:                                ; %.lr.ph
	s_clause 0x3
	s_load_b32 s7, s[0:1], 0x10
	s_load_b64 s[4:5], s[0:1], 0x18
	s_load_b32 s10, s[0:1], 0x4
	s_load_b64 s[8:9], s[0:1], 0x8
	v_mov_b32_e32 v6, 0
	v_mov_b32_e32 v4, v1
	s_waitcnt lgkmcnt(0)
	v_mul_lo_u32 v2, v0, s10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v3, 31, v2
	v_lshlrev_b64 v[2:3], 2, v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v2, vcc_lo, s8, v2
	v_add_co_ci_u32_e32 v3, vcc_lo, s9, v3, vcc_lo
.LBB0_2:                                ; =>This Inner Loop Header: Depth=1
	v_ashrrev_i32_e32 v5, 31, v4
	s_add_i32 s6, s6, -1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_cmp_eq_u32 s6, 0
	v_lshlrev_b64 v[7:8], 2, v[4:5]
	v_add_nc_u32_e32 v4, s7, v4
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_u32 v7, vcc_lo, s4, v7
	v_add_co_ci_u32_e32 v8, vcc_lo, s5, v8, vcc_lo
	global_load_b32 v5, v[2:3], off
	global_load_b32 v7, v[7:8], off
	v_add_co_u32 v2, vcc_lo, v2, 4
	v_add_co_ci_u32_e32 v3, vcc_lo, 0, v3, vcc_lo
	s_waitcnt vmcnt(0)
	v_fmac_f32_e32 v6, v5, v7
	s_cbranch_scc0 .LBB0_2
	s_branch .LBB0_4
.LBB0_3:
	v_mov_b32_e32 v6, 0
.LBB0_4:                                ; %Flow28
	s_load_b32 s0, s[0:1], 0x20
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[2:3], null, v0, s0, v[1:2]
	v_ashrrev_i32_e32 v3, 31, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[2:3]
	v_add_co_u32 v0, vcc_lo, s2, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	global_store_b32 v[0:1], v6, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z14mat_mul_kernel6MatrixS_S_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 304
		.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 9
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z14mat_mul_kernel6MatrixS_S_, .Lfunc_end0-_Z14mat_mul_kernel6MatrixS_S_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 344
; NumSgprs: 18
; NumVgprs: 9
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 9
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .offset:         0
        .size:           16
        .value_kind:     by_value
      - .offset:         16
        .size:           16
        .value_kind:     by_value
      - .offset:         32
        .size:           16
        .value_kind:     by_value
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         52
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         60
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         62
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         64
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         66
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         68
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         70
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         112
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 304
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z14mat_mul_kernel6MatrixS_S_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z14mat_mul_kernel6MatrixS_S_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     9
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
