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

#define WIDTH       512  // 64 ~ 512
#define TILE_WIDTH  16

#define ARYTYPE float

ARYTYPE M[WIDTH][WIDTH] = {0};
ARYTYPE N[WIDTH][WIDTH] = {0};
ARYTYPE P[WIDTH][WIDTH] = {0};
ARYTYPE MxN[WIDTH][WIDTH] = {0};

__device__ ARYTYPE GetElement(ARYTYPE *matrix, int row, int col, int width);
__device__ void SetElement(ARYTYPE *matrix, int row, int col, int width, ARYTYPE value);
__device__ ARYTYPE *GetSubMatrix(ARYTYPE *matrix, int blockrow, int blockcol, int width);
__global__ void MatMulKernel(ARYTYPE *Md, ARYTYPE *Nd, ARYTYPE *Pd, int width);
void MatMul(ARYTYPE *M, ARYTYPE *N, ARYTYPE *P, int width);


int main(int argc, char *argv[])
{
    int width = WIDTH;
    int pass = 1;
    
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < width; ++j) {
            M[i][j] = rand() % 30;
            N[i][j] = rand() % 30;
        }
    }
    
    struct timeval starttime, endtime;
    gettimeofday(&starttime, NULL);
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < width; ++j) {
            for (int k = 0; k < width; ++k) {
                MxN[i][j] += M[i][k] * N[k][j];
            }
        }
    }
    gettimeofday(&endtime, NULL);
    double executime;
    executime = (endtime.tv_sec - starttime.tv_sec) * 1000.0;
    executime += (endtime.tv_usec - starttime.tv_usec) / 1000.0;
    printf("CPU time: %13lf msec\n", executime);
    
    MatMul((ARYTYPE *)M, (ARYTYPE *)N, (ARYTYPE *)P, width);
    
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < width; ++j) {
            if(MxN[i][j] != P[i][j]) {
				printf("MxN[%d][%d] = %2.0f   P[%d][%d] = %2.0f\n", i, j, (float)MxN[i][j], i, j, (float)P[i][j]);
                pass = 0;
            }
        }
    }
    
    printf("Test %s\n", (pass)?"PASSED":"FAILED");
    
    return 0;
}

// Get a matrix element
__device__ ARYTYPE GetElement(ARYTYPE *matrix, int row, int col, int width)
{
    return *(matrix + row*width + col);
}

// Set a matrix element
__device__ void SetElement(ARYTYPE *matrix, int row, int col, int width, ARYTYPE value)
{
    *(matrix + row*width + col) = value;
}

// Get the TILE_WIDTHxTILE_WIDTH sub-matrix matsub of matrix that is
// located col sub-matrices to the right and row sub-matrices down
// from the upper-left corner of matrix
__device__ ARYTYPE *GetSubMatrix(ARYTYPE *matrix, int blockrow, int blockcol, int width)
{
    return (matrix + blockrow*TILE_WIDTH*width + blockcol*TILE_WIDTH);
}

// Matrix multiplication kernel called by MatMul()
__global__ void MatMulKernel(ARYTYPE *Md, ARYTYPE *Nd, ARYTYPE *Pd, int width)
{
    // Block row and column
    int blockRow = blockIdx.y;
    int blockCol = blockIdx.x;
    
    // Each thread block computes one sub-matrix Psub of P
    ARYTYPE *Pd_sub = GetSubMatrix(Pd, blockRow, blockCol, width);
    
    // Thread row and column within sub-matrix
    int row = threadIdx.y;
    int col = threadIdx.x;
    
    // Each thread computes one element of Psub
    // by accumulating results into Pvalue
    ARYTYPE Pvalue = 0;
    
    // Shared memory used to store Msub and Nsub respectively
    __shared__ ARYTYPE Mds[TILE_WIDTH][TILE_WIDTH];
    __shared__ ARYTYPE Nds[TILE_WIDTH][TILE_WIDTH];
    
    // Loop over all the sub-matrices of M and N that are
    // required to compute Psub
    // Multiply each pair of sub-matrices together
    // and accumulate the results
    for (int m = 0; m < (width / TILE_WIDTH); ++m) {
        // Get sub-matrix Msub of M
        ARYTYPE *Md_sub = GetSubMatrix(Md, blockRow, m, width);
        // Get sub-matrix Nsub of N
        ARYTYPE *Nd_sub = GetSubMatrix(Nd, m, blockCol, width);
        
        // Load Msub and Nsub from device memory to shared memory
        // Each thread loads one element of each sub-matrix
        Mds[row][col] = GetElement(Md_sub, row, col, width);
        Nds[row][col] = GetElement(Nd_sub, row, col, width);
        
        // Synchronize to make sure the sub-matrices are loaded
        // before starting the computation
        __syncthreads();
        
        // Multiply Msub and Nsub together
        for (int k = 0; k < TILE_WIDTH; ++k) {
            Pvalue += Mds[row][k] * Nds[k][col];
            //Pvalue += __mul24(Mds[row][k],Nds[k][col]);
        }
        
        // Synchronize to make sure that the preceding
        // computation is done before loading two new
        // sub-matrices of M and N in the next iteration
        __syncthreads();
    }
    
    // Write Psub to device memory
    // Each thread writes one element
    SetElement(Pd_sub, row, col, width, Pvalue);
}

// Matrix multiplication - Host code
// Matrix dimensions are assumed to be multiples of TILE_WIDTH
void MatMul(ARYTYPE *M, ARYTYPE *N, ARYTYPE *P, int width)
{
    size_t size = width * width * sizeof(ARYTYPE);
    ARYTYPE *Md, *Nd, *Pd;
    
    // Allocate and Load M, N to device memory
    cudaMalloc((void **)&Md, size);
    cudaMemcpy(Md, M, size, cudaMemcpyHostToDevice);
    
    cudaMalloc((void **)&Nd, size);
    cudaMemcpy(Nd, N, size, cudaMemcpyHostToDevice);
    
    // Allocate P on the device
    cudaMalloc((void **)&Pd, size);
    
    // Setup the execution configuration
    dim3 dimGrid(width/TILE_WIDTH, width/TILE_WIDTH);
    dim3 dimBlock(TILE_WIDTH, TILE_WIDTH);
    
    // Get start time event
    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    cudaEventRecord(start, 0);
    
    // Invoke kernel
    MatMulKernel<<<dimGrid, dimBlock>>>(Md, Nd, Pd, width);
    cudaError_t cuda_err = cudaGetLastError();
    if ( cudaSuccess != cuda_err ){
        printf("before kernel call: error = %s\n", cudaGetErrorString (cuda_err));
        exit(1) ;
    }
    
    // Get stop time event
    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    // Compute execution time
    float elapsedTime;
    cudaEventElapsedTime(&elapsedTime, start, stop);
    printf("GPU time: %13f msec\n", elapsedTime);
    cudaEventDestroy(start);
    cudaEventDestroy(stop);
    
    // Read P from device memory
    cudaMemcpy(P, Pd, size, cudaMemcpyDeviceToHost);
    
    // Free device memory
    cudaFree(Md);
    cudaFree(Nd);
    cudaFree(Pd);
}

	.file	"tmpxft_0033750f_00000000-6_MatMul.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z10GetElementPfiii
	.type	_Z10GetElementPfiii, @function
_Z10GetElementPfiii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z10GetElementPfiii, .-_Z10GetElementPfiii
	.globl	_Z10SetElementPfiiif
	.type	_Z10SetElementPfiiif, @function
_Z10SetElementPfiiif:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z10SetElementPfiiif, .-_Z10SetElementPfiiif
	.globl	_Z12GetSubMatrixPfiii
	.type	_Z12GetSubMatrixPfiii, @function
_Z12GetSubMatrixPfiii:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2030:
	.size	_Z12GetSubMatrixPfiii, .-_Z12GetSubMatrixPfiii
	.globl	_Z37__device_stub__Z12MatMulKernelPfS_S_iPfS_S_i
	.type	_Z37__device_stub__Z12MatMulKernelPfS_S_iPfS_S_i, @function
_Z37__device_stub__Z12MatMulKernelPfS_S_iPfS_S_i:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 4(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%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, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L8
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z12MatMulKernelPfS_S_i(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L8:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z37__device_stub__Z12MatMulKernelPfS_S_iPfS_S_i, .-_Z37__device_stub__Z12MatMulKernelPfS_S_iPfS_S_i
	.globl	_Z12MatMulKernelPfS_S_i
	.type	_Z12MatMulKernelPfS_S_i, @function
_Z12MatMulKernelPfS_S_i:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z37__device_stub__Z12MatMulKernelPfS_S_iPfS_S_i
	.cfi_endproc
.LFE2057:
	.size	_Z12MatMulKernelPfS_S_i, .-_Z12MatMulKernelPfS_S_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"before kernel call: error = %s\n"
.LC1:
	.string	"GPU time: %13f msec\n"
	.text
	.globl	_Z6MatMulPfS_S_i
	.type	_Z6MatMulPfS_S_i, @function
_Z6MatMulPfS_S_i:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movq	%rdi, %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rsi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movl	%ecx, %ebx
	imull	%ecx, %ebx
	subq	$80, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	movslq	%ebx, %rbx
	leaq	8(%rsp), %rdi
	salq	$2, %rbx
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r14, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	16(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r13, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	%rbx, %rsi
	leaq	24(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%ebp, %eax
	movl	$16, %ecx
	leaq	32(%rsp), %rdi
	cltd
	movl	$1, 56(%rsp)
	idivl	%ecx
	movl	$1, 68(%rsp)
	movl	%eax, 48(%rsp)
	movl	%eax, 52(%rsp)
	movabsq	$68719476752, %rax
	movq	%rax, 60(%rsp)
	call	cudaEventCreate@PLT
	leaq	40(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	68(%rsp), %ecx
	movl	56(%rsp), %esi
	xorl	%r9d, %r9d
	movq	60(%rsp), %rdx
	movq	48(%rsp), %rdi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L15
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rsi
	movl	%ebp, %ecx
	movq	8(%rsp), %rdi
	call	_Z37__device_stub__Z12MatMulKernelPfS_S_iPfS_S_i
.L15:
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L16
	call	cudaGetErrorString@PLT
	movl	$2, %edi
	leaq	.LC0(%rip), %rsi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L16:
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	40(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	leaq	4(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	4(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	32(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	40(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	24(%rsp), %rsi
	movq	%rbx, %rdx
	movq	%r12, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L17
	call	__stack_chk_fail@PLT
.L17:
	addq	$80, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z6MatMulPfS_S_i, .-_Z6MatMulPfS_S_i
	.section	.rodata.str1.1
.LC2:
	.string	"PASSED"
.LC3:
	.string	"FAILED"
.LC5:
	.string	"CPU time: %13lf msec\n"
.LC6:
	.string	"MxN[%d][%d] = %2.0f   P[%d][%d] = %2.0f\n"
.LC7:
	.string	"Test %s\n"
	.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
	xorl	%r13d, %r13d
	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
	leaq	M(%rip), %rbx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
.L20:
	movq	%r13, %r12
	movl	$512, %r15d
.L21:
	call	rand@PLT
	movl	$30, %r14d
	leaq	N(%rip), %rbp
	cltd
	idivl	%r14d
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, (%rbx,%r12)
	call	rand@PLT
	cltd
	idivl	%r14d
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, 0(%rbp,%r12)
	addq	$4, %r12
	decl	%r15d
	jne	.L21
	addq	$2048, %r13
	cmpq	$1048576, %r13
	jne	.L20
	leaq	24(%rsp), %rdi
	xorl	%esi, %esi
	leaq	MxN(%rip), %r12
	call	gettimeofday@PLT
	xorl	%eax, %eax
	leaq	2048+N(%rip), %r9
.L23:
	leaq	(%rax,%r12), %rcx
	leaq	N(%rip), %rdx
	leaq	(%rbx,%rax), %r8
.L27:
	movss	(%rcx), %xmm0
	movq	%rdx, %rdi
	xorl	%esi, %esi
.L24:
	movss	(%r8,%rsi,4), %xmm1
	mulss	(%rdi), %xmm1
	incq	%rsi
	addq	$2048, %rdi
	addss	%xmm1, %xmm0
	cmpq	$512, %rsi
	jne	.L24
	addq	$4, %rdx
	movss	%xmm0, (%rcx)
	addq	$4, %rcx
	cmpq	%r9, %rdx
	jne	.L27
	addq	$2048, %rax
	cmpq	$1048576, %rax
	jne	.L23
	leaq	40(%rsp), %rdi
	xorl	%esi, %esi
	leaq	P(%rip), %r13
	call	gettimeofday@PLT
	movq	40(%rsp), %rax
	subq	24(%rsp), %rax
	leaq	.LC5(%rip), %rsi
	cvtsi2sdq	%rax, %xmm1
	movq	48(%rsp), %rax
	subq	32(%rsp), %rax
	movl	$2, %edi
	cvtsi2sdq	%rax, %xmm0
	movb	$1, %al
	movsd	.LC4(%rip), %xmm2
	leaq	.LC6(%rip), %r15
	mulsd	%xmm2, %xmm1
	divsd	%xmm2, %xmm0
	addsd	%xmm1, %xmm0
	call	__printf_chk@PLT
	movq	%rbp, %rsi
	movl	$512, %ecx
	movq	%r13, %rdx
	movq	%rbx, %rdi
	xorl	%ebp, %ebp
	call	_Z6MatMulPfS_S_i
	movl	$1, %eax
.L28:
	movq	%rbp, %r14
	movl	%ebp, %r8d
	xorl	%ebx, %ebx
	salq	$11, %r14
.L31:
	movss	(%r12,%r14), %xmm0
	movss	0(%r13,%r14), %xmm1
	ucomiss	%xmm1, %xmm0
	jp	.L36
	je	.L29
.L36:
	movl	%r8d, %edx
	movl	%ebx, %r9d
	movl	%ebx, %ecx
	movq	%r15, %rsi
	movl	$2, %edi
	movb	$2, %al
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	movl	%r8d, 12(%rsp)
	call	__printf_chk@PLT
	movl	12(%rsp), %r8d
	xorl	%eax, %eax
.L29:
	incl	%ebx
	addq	$4, %r14
	cmpl	$512, %ebx
	jne	.L31
	incq	%rbp
	cmpq	$512, %rbp
	jne	.L28
	testl	%eax, %eax
	leaq	.LC3(%rip), %rdx
	movl	$2, %edi
	leaq	.LC2(%rip), %rax
	leaq	.LC7(%rip), %rsi
	cmovne	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L34
	call	__stack_chk_fail@PLT
.L34:
	addq	$72, %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
.LC8:
	.string	"_Z12MatMulKernelPfS_S_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.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	.LC8(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z12MatMulKernelPfS_S_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
.LFE2059:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.globl	MxN
	.bss
	.align 32
	.type	MxN, @object
	.size	MxN, 1048576
MxN:
	.zero	1048576
	.globl	P
	.align 32
	.type	P, @object
	.size	P, 1048576
P:
	.zero	1048576
	.globl	N
	.align 32
	.type	N, @object
	.size	N, 1048576
N:
	.zero	1048576
	.globl	M
	.align 32
	.type	M, @object
	.size	M, 1048576
M:
	.zero	1048576
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC4:
	.long	0
	.long	1083129856
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

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

#define WIDTH       512  // 64 ~ 512
#define TILE_WIDTH  16

#define ARYTYPE float

ARYTYPE M[WIDTH][WIDTH] = {0};
ARYTYPE N[WIDTH][WIDTH] = {0};
ARYTYPE P[WIDTH][WIDTH] = {0};
ARYTYPE MxN[WIDTH][WIDTH] = {0};

__device__ ARYTYPE GetElement(ARYTYPE *matrix, int row, int col, int width);
__device__ void SetElement(ARYTYPE *matrix, int row, int col, int width, ARYTYPE value);
__device__ ARYTYPE *GetSubMatrix(ARYTYPE *matrix, int blockrow, int blockcol, int width);
__global__ void MatMulKernel(ARYTYPE *Md, ARYTYPE *Nd, ARYTYPE *Pd, int width);
void MatMul(ARYTYPE *M, ARYTYPE *N, ARYTYPE *P, int width);


int main(int argc, char *argv[])
{
    int width = WIDTH;
    int pass = 1;
    
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < width; ++j) {
            M[i][j] = rand() % 30;
            N[i][j] = rand() % 30;
        }
    }
    
    struct timeval starttime, endtime;
    gettimeofday(&starttime, NULL);
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < width; ++j) {
            for (int k = 0; k < width; ++k) {
                MxN[i][j] += M[i][k] * N[k][j];
            }
        }
    }
    gettimeofday(&endtime, NULL);
    double executime;
    executime = (endtime.tv_sec - starttime.tv_sec) * 1000.0;
    executime += (endtime.tv_usec - starttime.tv_usec) / 1000.0;
    printf("CPU time: %13lf msec\n", executime);
    
    MatMul((ARYTYPE *)M, (ARYTYPE *)N, (ARYTYPE *)P, width);
    
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < width; ++j) {
            if(MxN[i][j] != P[i][j]) {
				printf("MxN[%d][%d] = %2.0f   P[%d][%d] = %2.0f\n", i, j, (float)MxN[i][j], i, j, (float)P[i][j]);
                pass = 0;
            }
        }
    }
    
    printf("Test %s\n", (pass)?"PASSED":"FAILED");
    
    return 0;
}

// Get a matrix element
__device__ ARYTYPE GetElement(ARYTYPE *matrix, int row, int col, int width)
{
    return *(matrix + row*width + col);
}

// Set a matrix element
__device__ void SetElement(ARYTYPE *matrix, int row, int col, int width, ARYTYPE value)
{
    *(matrix + row*width + col) = value;
}

// Get the TILE_WIDTHxTILE_WIDTH sub-matrix matsub of matrix that is
// located col sub-matrices to the right and row sub-matrices down
// from the upper-left corner of matrix
__device__ ARYTYPE *GetSubMatrix(ARYTYPE *matrix, int blockrow, int blockcol, int width)
{
    return (matrix + blockrow*TILE_WIDTH*width + blockcol*TILE_WIDTH);
}

// Matrix multiplication kernel called by MatMul()
__global__ void MatMulKernel(ARYTYPE *Md, ARYTYPE *Nd, ARYTYPE *Pd, int width)
{
    // Block row and column
    int blockRow = blockIdx.y;
    int blockCol = blockIdx.x;
    
    // Each thread block computes one sub-matrix Psub of P
    ARYTYPE *Pd_sub = GetSubMatrix(Pd, blockRow, blockCol, width);
    
    // Thread row and column within sub-matrix
    int row = threadIdx.y;
    int col = threadIdx.x;
    
    // Each thread computes one element of Psub
    // by accumulating results into Pvalue
    ARYTYPE Pvalue = 0;
    
    // Shared memory used to store Msub and Nsub respectively
    __shared__ ARYTYPE Mds[TILE_WIDTH][TILE_WIDTH];
    __shared__ ARYTYPE Nds[TILE_WIDTH][TILE_WIDTH];
    
    // Loop over all the sub-matrices of M and N that are
    // required to compute Psub
    // Multiply each pair of sub-matrices together
    // and accumulate the results
    for (int m = 0; m < (width / TILE_WIDTH); ++m) {
        // Get sub-matrix Msub of M
        ARYTYPE *Md_sub = GetSubMatrix(Md, blockRow, m, width);
        // Get sub-matrix Nsub of N
        ARYTYPE *Nd_sub = GetSubMatrix(Nd, m, blockCol, width);
        
        // Load Msub and Nsub from device memory to shared memory
        // Each thread loads one element of each sub-matrix
        Mds[row][col] = GetElement(Md_sub, row, col, width);
        Nds[row][col] = GetElement(Nd_sub, row, col, width);
        
        // Synchronize to make sure the sub-matrices are loaded
        // before starting the computation
        __syncthreads();
        
        // Multiply Msub and Nsub together
        for (int k = 0; k < TILE_WIDTH; ++k) {
            Pvalue += Mds[row][k] * Nds[k][col];
            //Pvalue += __mul24(Mds[row][k],Nds[k][col]);
        }
        
        // Synchronize to make sure that the preceding
        // computation is done before loading two new
        // sub-matrices of M and N in the next iteration
        __syncthreads();
    }
    
    // Write Psub to device memory
    // Each thread writes one element
    SetElement(Pd_sub, row, col, width, Pvalue);
}

// Matrix multiplication - Host code
// Matrix dimensions are assumed to be multiples of TILE_WIDTH
void MatMul(ARYTYPE *M, ARYTYPE *N, ARYTYPE *P, int width)
{
    size_t size = width * width * sizeof(ARYTYPE);
    ARYTYPE *Md, *Nd, *Pd;
    
    // Allocate and Load M, N to device memory
    hipMalloc((void **)&Md, size);
    hipMemcpy(Md, M, size, hipMemcpyHostToDevice);
    
    hipMalloc((void **)&Nd, size);
    hipMemcpy(Nd, N, size, hipMemcpyHostToDevice);
    
    // Allocate P on the device
    hipMalloc((void **)&Pd, size);
    
    // Setup the execution configuration
    dim3 dimGrid(width/TILE_WIDTH, width/TILE_WIDTH);
    dim3 dimBlock(TILE_WIDTH, TILE_WIDTH);
    
    // Get start time event
    hipEvent_t start, stop;
    hipEventCreate(&start);
    hipEventCreate(&stop);
    hipEventRecord(start, 0);
    
    // Invoke kernel
    MatMulKernel<<<dimGrid, dimBlock>>>(Md, Nd, Pd, width);
    hipError_t cuda_err = hipGetLastError();
    if ( hipSuccess != cuda_err ){
        printf("before kernel call: error = %s\n", hipGetErrorString (cuda_err));
        exit(1) ;
    }
    
    // Get stop time event
    hipEventRecord(stop, 0);
    hipEventSynchronize(stop);
    // Compute execution time
    float elapsedTime;
    hipEventElapsedTime(&elapsedTime, start, stop);
    printf("GPU time: %13f msec\n", elapsedTime);
    hipEventDestroy(start);
    hipEventDestroy(stop);
    
    // Read P from device memory
    hipMemcpy(P, Pd, size, hipMemcpyDeviceToHost);
    
    // Free device memory
    hipFree(Md);
    hipFree(Nd);
    hipFree(Pd);
}

	.text
	.file	"MatMul.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI0_0:
	.quad	0x408f400000000000              # double 1000
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	xorl	%ebx, %ebx
	xorl	%r14d, %r14d
.LBB0_1:                                # %.preheader58
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_2 Depth 2
	movq	$-2048, %r15                    # imm = 0xF800
.LBB0_2:                                #   Parent Loop BB0_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	cltq
	imulq	$-2004318071, %rax, %rcx        # imm = 0x88888889
	shrq	$32, %rcx
	addl	%eax, %ecx
	movl	%ecx, %edx
	shrl	$31, %edx
	sarl	$4, %ecx
	addl	%edx, %ecx
	movl	%ecx, %edx
	shll	$5, %edx
	addl	%ecx, %ecx
	subl	%edx, %ecx
	addl	%eax, %ecx
	xorps	%xmm0, %xmm0
	cvtsi2ss	%ecx, %xmm0
	movss	%xmm0, M+2048(%rbx,%r15)
	callq	rand
	cltq
	imulq	$-2004318071, %rax, %rcx        # imm = 0x88888889
	shrq	$32, %rcx
	addl	%eax, %ecx
	movl	%ecx, %edx
	shrl	$31, %edx
	sarl	$4, %ecx
	addl	%edx, %ecx
	movl	%ecx, %edx
	shll	$5, %edx
	addl	%ecx, %ecx
	subl	%edx, %ecx
	addl	%eax, %ecx
	xorps	%xmm0, %xmm0
	cvtsi2ss	%ecx, %xmm0
	movss	%xmm0, N+2048(%rbx,%r15)
	addq	$4, %r15
	jne	.LBB0_2
# %bb.3:                                #   in Loop: Header=BB0_1 Depth=1
	incq	%r14
	addq	$2048, %rbx                     # imm = 0x800
	cmpq	$512, %r14                      # imm = 0x200
	jne	.LBB0_1
# %bb.4:
	xorl	%ebx, %ebx
	movq	%rsp, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movl	$M, %eax
.LBB0_5:                                # %.preheader57
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_6 Depth 2
                                        #       Child Loop BB0_7 Depth 3
	movl	$N, %ecx
	movq	%rbx, %rdx
	shlq	$11, %rdx
	xorl	%esi, %esi
.LBB0_6:                                # %.preheader56
                                        #   Parent Loop BB0_5 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB0_7 Depth 3
	leaq	(%rdx,%rsi,4), %rdi
	addq	$MxN, %rdi
	movss	(%rdi), %xmm0                   # xmm0 = mem[0],zero,zero,zero
	movq	%rcx, %r8
	xorl	%r9d, %r9d
.LBB0_7:                                #   Parent Loop BB0_5 Depth=1
                                        #     Parent Loop BB0_6 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%rax,%r9,4), %xmm1             # xmm1 = mem[0],zero,zero,zero
	mulss	(%r8), %xmm1
	addss	%xmm1, %xmm0
	incq	%r9
	addq	$2048, %r8                      # imm = 0x800
	cmpq	$512, %r9                       # imm = 0x200
	jne	.LBB0_7
# %bb.8:                                #   in Loop: Header=BB0_6 Depth=2
	movss	%xmm0, (%rdi)
	incq	%rsi
	addq	$4, %rcx
	cmpq	$512, %rsi                      # imm = 0x200
	jne	.LBB0_6
# %bb.9:                                #   in Loop: Header=BB0_5 Depth=1
	incq	%rbx
	addq	$2048, %rax                     # imm = 0x800
	cmpq	$512, %rbx                      # imm = 0x200
	jne	.LBB0_5
# %bb.10:
	xorl	%r15d, %r15d
	leaq	16(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%rbx), %rax
	movq	8(%rbx), %rcx
	subq	(%rsp), %rax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	movsd	.LCPI0_0(%rip), %xmm2           # xmm2 = mem[0],zero
	mulsd	%xmm2, %xmm1
	subq	8(%rsp), %rcx
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rcx, %xmm0
	divsd	%xmm2, %xmm0
	addsd	%xmm1, %xmm0
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	movl	$M, %edi
	movl	$N, %esi
	movl	$P, %edx
	movl	$512, %ecx                      # imm = 0x200
	callq	_Z6MatMulPfS_S_i
	movl	$1, %eax
	xorl	%ebx, %ebx
.LBB0_11:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_12 Depth 2
	xorl	%r14d, %r14d
.LBB0_12:                               #   Parent Loop BB0_11 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	MxN(%r15,%r14,4), %xmm0         # xmm0 = mem[0],zero,zero,zero
	movss	P(%r15,%r14,4), %xmm1           # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm1, %xmm0
	jne	.LBB0_13
	jnp	.LBB0_14
.LBB0_13:                               #   in Loop: Header=BB0_12 Depth=2
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	movl	$.L.str.1, %edi
	movl	%ebx, %esi
	movl	%r14d, %edx
	movl	%ebx, %ecx
	movl	%r14d, %r8d
	movb	$2, %al
	callq	printf
	xorl	%eax, %eax
.LBB0_14:                               #   in Loop: Header=BB0_12 Depth=2
	incq	%r14
	cmpq	$512, %r14                      # imm = 0x200
	jne	.LBB0_12
# %bb.15:                               #   in Loop: Header=BB0_11 Depth=1
	incq	%rbx
	addq	$2048, %r15                     # imm = 0x800
	cmpq	$512, %rbx                      # imm = 0x200
	jne	.LBB0_11
# %bb.16:
	testl	%eax, %eax
	movl	$.L.str.4, %eax
	movl	$.L.str.3, %esi
	cmoveq	%rax, %rsi
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	xorl	%eax, %eax
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z6MatMulPfS_S_i                # -- Begin function _Z6MatMulPfS_S_i
	.type	_Z6MatMulPfS_S_i,@function
_Z6MatMulPfS_S_i:                       # @_Z6MatMulPfS_S_i
	.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
	movl	%ecx, %r15d
	movq	%rdx, %rbx
	movq	%rsi, %r12
	movq	%rdi, %r13
	movl	%ecx, %r14d
	imull	%r14d, %r14d
	shlq	$2, %r14
	leaq	40(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	(%rbp), %rdi
	movq	%r13, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	32(%rsp), %r13
	movq	%r13, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	(%r13), %rdi
	movq	%r12, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	16(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leal	15(%r15), %eax
	testl	%r15d, %r15d
	cmovnsl	%r15d, %eax
	sarl	$4, %eax
	movq	%rax, %r12
	shlq	$32, %r12
	orq	%rax, %r12
	leaq	24(%rsp), %r13
	movq	%r13, %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r13), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movabsq	$68719476752, %rdx              # imm = 0x1000000010
	movq	%r12, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_2
# %bb.1:
	movq	40(%rsp), %rdi
	movq	32(%rsp), %rsi
	movq	16(%rsp), %rdx
	movl	%r15d, %ecx
	callq	_Z27__device_stub__MatMulKernelPfS_S_i
.LBB1_2:
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB1_4
# %bb.3:
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	24(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	52(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventElapsedTime
	cvtss2sd	(%r15), %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	movq	24(%rsp), %rdi
	callq	hipEventDestroy
	movq	8(%rsp), %rdi
	callq	hipEventDestroy
	movq	16(%rsp), %rsi
	movq	%rbx, %rdi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	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_4:
	.cfi_def_cfa_offset 112
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.5, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z6MatMulPfS_S_i, .Lfunc_end1-_Z6MatMulPfS_S_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z27__device_stub__MatMulKernelPfS_S_i # -- Begin function _Z27__device_stub__MatMulKernelPfS_S_i
	.type	_Z27__device_stub__MatMulKernelPfS_S_i,@function
_Z27__device_stub__MatMulKernelPfS_S_i: # @_Z27__device_stub__MatMulKernelPfS_S_i
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%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	$_Z12MatMulKernelPfS_S_i, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z27__device_stub__MatMulKernelPfS_S_i, .Lfunc_end2-_Z27__device_stub__MatMulKernelPfS_S_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:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB3_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB3_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z12MatMulKernelPfS_S_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_end3:
	.size	__hip_module_ctor, .Lfunc_end3-__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	.LBB4_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
.LBB4_2:
	retq
.Lfunc_end4:
	.size	__hip_module_dtor, .Lfunc_end4-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	M,@object                       # @M
	.bss
	.globl	M
	.p2align	4, 0x0
M:
	.zero	1048576
	.size	M, 1048576

	.type	N,@object                       # @N
	.globl	N
	.p2align	4, 0x0
N:
	.zero	1048576
	.size	N, 1048576

	.type	P,@object                       # @P
	.globl	P
	.p2align	4, 0x0
P:
	.zero	1048576
	.size	P, 1048576

	.type	MxN,@object                     # @MxN
	.globl	MxN
	.p2align	4, 0x0
MxN:
	.zero	1048576
	.size	MxN, 1048576

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"CPU time: %13lf msec\n"
	.size	.L.str, 22

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"MxN[%d][%d] = %2.0f   P[%d][%d] = %2.0f\n"
	.size	.L.str.1, 41

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Test %s\n"
	.size	.L.str.2, 9

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"PASSED"
	.size	.L.str.3, 7

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

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

	.type	.L.str.5,@object                # @.str.5
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.5:
	.asciz	"before kernel call: error = %s\n"
	.size	.L.str.5, 32

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"GPU time: %13f msec\n"
	.size	.L.str.6, 21

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z12MatMulKernelPfS_S_i"
	.size	.L__unnamed_1, 24

	.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 _Z27__device_stub__MatMulKernelPfS_S_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym M
	.addrsig_sym N
	.addrsig_sym P
	.addrsig_sym _Z12MatMulKernelPfS_S_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z12MatMulKernelPfS_S_i ; -- Begin function _Z12MatMulKernelPfS_S_i
	.globl	_Z12MatMulKernelPfS_S_i
	.p2align	8
	.type	_Z12MatMulKernelPfS_S_i,@function
_Z12MatMulKernelPfS_S_i:                ; @_Z12MatMulKernelPfS_S_i
; %bb.0:
	s_clause 0x2
	s_load_b32 s10, s[0:1], 0x18
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[0:1], s[0:1], 0x10
	v_bfe_u32 v5, v0, 10, 10
	s_lshl_b32 s2, s14, 4
	v_and_b32_e32 v3, 0x3ff, v0
	s_ashr_i32 s3, s2, 31
	s_waitcnt lgkmcnt(0)
	v_mul_lo_u32 v1, v5, s10
	s_mul_i32 s15, s15, s10
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b32 s8, s15, 4
	s_ashr_i32 s9, s8, 31
	s_cmp_gt_i32 s10, 15
	s_cbranch_scc1 .LBB0_2
; %bb.1:                                ; %.._crit_edge_crit_edge
	v_mul_lo_u32 v0, v5, s10
	v_mov_b32_e32 v4, 0
	s_mov_b32 s11, 0
	s_branch .LBB0_3
.LBB0_2:
	s_mov_b32 s11, -1
                                        ; implicit-def: $vgpr0
.LBB0_3:                                ; %Flow71
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 vcc_lo, exec_lo, s11
	s_cbranch_vccnz .LBB0_9
; %bb.4:                                ; %.lr.ph
	s_ashr_i32 s11, s10, 31
	v_lshlrev_b32_e32 v10, 2, v3
	v_lshlrev_b32_e32 v0, 6, v5
	v_mov_b32_e32 v2, 0
	s_lshr_b32 s11, s11, 28
	s_lshl_b64 s[12:13], s[2:3], 2
	s_add_i32 s11, s10, s11
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_4) | instid1(SALU_CYCLE_1)
	v_lshlrev_b64 v[8:9], 2, v[1:2]
	s_ashr_i32 s11, s11, 4
	s_add_u32 s12, s6, s12
	s_addc_u32 s13, s7, s13
	s_lshl_b64 s[6:7], s[8:9], 2
	s_add_u32 s4, s4, s6
	s_addc_u32 s5, s5, s7
	v_add_co_u32 v6, vcc_lo, s4, v8
	v_add_co_ci_u32_e32 v7, vcc_lo, s5, v9, vcc_lo
	s_mov_b32 s5, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v5, vcc_lo, v6, v10
	v_add_co_ci_u32_e32 v6, vcc_lo, 0, v7, vcc_lo
	v_add_nc_u32_e32 v7, v0, v10
	v_add_co_u32 v11, vcc_lo, s12, v8
	v_add_co_ci_u32_e32 v12, vcc_lo, s13, v9, vcc_lo
	v_add_nc_u32_e32 v8, 0x400, v10
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_add_co_u32 v9, vcc_lo, v11, v10
	v_mov_b32_e32 v4, v2
	v_add_co_ci_u32_e32 v10, vcc_lo, 0, v12, vcc_lo
	s_delay_alu instid0(VALU_DEP_4)
	v_add_nc_u32_e32 v11, v8, v0
	s_mov_b32 s6, 0
.LBB0_5:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_6 Depth 2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_lshl_b32 s4, s6, 4
	s_mov_b32 s13, s5
	s_lshl_b64 s[14:15], s[4:5], 2
	s_mul_i32 s12, s4, s10
	v_add_co_u32 v12, vcc_lo, v5, s14
	s_lshl_b64 s[12:13], s[12:13], 2
	v_add_co_ci_u32_e32 v13, vcc_lo, s15, v6, vcc_lo
	v_add_co_u32 v14, vcc_lo, v9, s12
	v_add_co_ci_u32_e32 v15, vcc_lo, s13, v10, vcc_lo
	global_load_b32 v13, v[12:13], off
	global_load_b32 v14, v[14:15], off
	v_mov_b32_e32 v12, v8
	s_mov_b32 s4, 0
	s_waitcnt vmcnt(1)
	ds_store_b32 v7, v13
	s_waitcnt vmcnt(0)
	ds_store_b32 v11, v14
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
.LBB0_6:                                ;   Parent Loop BB0_5 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	v_add_nc_u32_e32 v13, s4, v0
	s_add_i32 s4, s4, 4
	ds_load_b32 v14, v12
	ds_load_b32 v13, v13
	v_add_nc_u32_e32 v12, 64, v12
	s_cmp_eq_u32 s4, 64
	s_waitcnt lgkmcnt(0)
	v_fmac_f32_e32 v2, v13, v14
	s_cbranch_scc0 .LBB0_6
; %bb.7:                                ;   in Loop: Header=BB0_5 Depth=1
	s_add_i32 s6, s6, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s6, s11
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB0_5
; %bb.8:                                ; %Flow
	v_mov_b32_e32 v0, v1
	s_branch .LBB0_10
.LBB0_9:
	v_mov_b32_e32 v2, s11
.LBB0_10:                               ; %Flow72
	s_delay_alu instid0(VALU_DEP_1)
	v_ashrrev_i32_e32 v1, 31, v0
	s_lshl_b64 s[4:5], s[8:9], 2
	v_lshlrev_b64 v[3:4], 2, v[3:4]
	s_add_u32 s4, s0, s4
	s_addc_u32 s5, s1, s5
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	s_lshl_b64 s[0:1], s[2:3], 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	s_add_u32 s0, s4, s0
	s_addc_u32 s1, s5, s1
	v_add_co_u32 v0, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	v_add_co_u32 v0, vcc_lo, v0, v3
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, v1, v4, vcc_lo
	global_store_b32 v[0:1], v2, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z12MatMulKernelPfS_S_i
		.amdhsa_group_segment_fixed_size 2048
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 28
		.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 16
		.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	_Z12MatMulKernelPfS_S_i, .Lfunc_end0-_Z12MatMulKernelPfS_S_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 576
; NumSgprs: 18
; NumVgprs: 16
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 2048 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 16
; 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
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 2048
    .kernarg_segment_align: 8
    .kernarg_segment_size: 28
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z12MatMulKernelPfS_S_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z12MatMulKernelPfS_S_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     16
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
