#include <stdio.h>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

// CUDA RumTime API
#define TILE_WIDTH 32

void MatrixMultiplyOnHost(float* M, float* N, float* P, int width)
{
	for(int i=0; i<width; ++i)
	{
		for (int j=0; j<width; ++j)
		{
			float sum = 0;
			for(int k=0; k<width; ++k)
			{
				float a = M[i*width+k];
				float b = N[k*width+j];
				sum += a*b;
			}
			P[i*width+j] = sum;
		}
	}

}

__global__ void MatirxMultiplyKernel(const float* devM, const float* devN, float* devP, const int width, const int tile_width)
{
	__shared__ float sM[TILE_WIDTH][TILE_WIDTH];
	__shared__ float sN[TILE_WIDTH][TILE_WIDTH];

	int bx = blockIdx.x;		int by = blockIdx.y;
	int tx = threadIdx.x;		int ty = threadIdx.y;

	int col = bx*tile_width+tx;
	int row = by*tile_width+ty;

	//Initialize accumulator to 0
	float pValue = 0;

	//Multiply and add
	for(int m=0; m<width/tile_width;m++)
	{
		sM[ty][tx] = devM[row*width+(m*tile_width+tx)];
		sN[ty][tx] = devN[col+(m*tile_width+ty)*width];
		//each time bring one element from devM and devN into shared memory
		//threads are blocked until all the threads reach this point
		__syncthreads();

		for(int k=0; k<tile_width; k++)
			pValue += sM[ty][k]*sN[k][tx];
		__syncthreads();
	}
	
	//Write value to device memory - each thread has unique index to write to
	devP[row*width+col] = pValue;
}

void MatrixMultiplyOnDevice(const float* hostM, const float* hostN, float* hostP, const int width, const int tile_width)
{
	int sizeInBytes = width*width*sizeof(float);
	float *devM, *devN, *devP;

	//Allocate M and N on devide
	cudaMalloc((void**)&devM, sizeInBytes);
	cudaMalloc((void**)&devN, sizeInBytes);

	//Allocate P
	cudaMalloc((void**)&devP, sizeInBytes);

	//Copy M and N from host to device
	cudaMemcpy(devM, hostM, sizeInBytes, cudaMemcpyHostToDevice);
	cudaMemcpy(devN, hostN, sizeInBytes, cudaMemcpyHostToDevice);

	//Setup thread/block execution configuration
	dim3 dimBlocks(tile_width,tile_width); //Each block has (width,width) threads
	dim3 dimGrid(width/tile_width,width/tile_width); //Launch 1 block


	//Launch the kernel
	clock_t begin = clock();
	MatirxMultiplyKernel<<<dimGrid,dimBlocks>>>(devM,devN,devP,width,tile_width);
	clock_t end = clock();
	float elapsed_secs = float(end - begin) / CLOCKS_PER_SEC;
	printf("Matrix Multiply on Device: %fs\n",elapsed_secs);
	

	//Copy P matrix from device to host
	cudaMemcpy(hostP, devP, sizeInBytes, cudaMemcpyDeviceToHost);

	//Free allocated memory
	cudaFree(devM); cudaFree(devN); cudaFree(devP);
}

void PrintMatrix(float* M, int width)
{
	for(int i=0; i<width; i++)
	{
		for(int j=0; j<width; j++)
		{
			printf("%f ",M[i*width+j]);
		}
		printf("\n");
	}
}

int main()
{
	int width = 1024;
	int tile_width = 32;
	int size = width*width;

	float* M = new float[size];
	float* N = new float[size];
	float* P = new float[size];
	float* Q = new float[size];
	srand (time(NULL));
	for(int i=0; i<size; i++)
	{
		M[i] = rand() / (RAND_MAX + 1.);
		N[i] = rand() / (RAND_MAX + 1.);
	}

	//multiply on host
	clock_t begin = clock();
	MatrixMultiplyOnHost(M,N,P,width);
	clock_t end = clock();
	float elapsed_secs = float(end - begin) / CLOCKS_PER_SEC;
	printf("Matrix Multiply on Host: %fs\n",elapsed_secs);
	//std::cout << "Matrix Multiply on Host: " << elapsed_secs << std::endl;
	
	//multiply on device
	//1. Copy M,N matrices to device
	//2. M*N on device
	//3. Copy P matrix to host and output
	MatrixMultiplyOnDevice(M,N,Q,width,tile_width);

	float avg_err = 0;
	for(int i=0; i<size; i++)
		avg_err += fabs(P[i]-Q[i]);
	avg_err /= size;
	printf("Average error is: %f\n",avg_err);
	//PrintMatrix(M,width);
	//PrintMatrix(N,width);
	//PrintMatrix(P,width);
	//PrintMatrix(Q,width);

	return 0;
}
	.file	"tmpxft_002f921e_00000000-6_multiply.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2033:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2033:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z20MatrixMultiplyOnHostPfS_S_i
	.type	_Z20MatrixMultiplyOnHostPfS_S_i, @function
_Z20MatrixMultiplyOnHostPfS_S_i:
.LFB2027:
	.cfi_startproc
	endbr64
	movq	%rdi, %r10
	xorl	%r9d, %r9d
	movslq	%ecx, %rdi
	movq	%rsi, %r11
	salq	$2, %rdi
	xorl	%esi, %esi
	cmpl	%ecx, %r9d
	jl	.L11
	ret
.L11:
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
.L7:
	xorl	%eax, %eax
	leaq	(%r10,%rsi), %rbp
	leaq	(%rdx,%rsi), %r12
.L6:
	leaq	(%r11,%rax,4), %rbx
	xorl	%r8d, %r8d
	xorps	%xmm0, %xmm0
.L4:
	movss	0(%rbp,%r8,4), %xmm1
	mulss	(%rbx), %xmm1
	incq	%r8
	addq	%rdi, %rbx
	addss	%xmm1, %xmm0
	cmpl	%r8d, %ecx
	jg	.L4
	movss	%xmm0, (%r12,%rax,4)
	incq	%rax
	cmpl	%eax, %ecx
	jg	.L6
	incl	%r9d
	addq	%rdi, %rsi
	cmpl	%ecx, %r9d
	jl	.L7
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z20MatrixMultiplyOnHostPfS_S_i, .-_Z20MatrixMultiplyOnHostPfS_S_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"%f "
.LC2:
	.string	"\n"
	.text
	.globl	_Z11PrintMatrixPfi
	.type	_Z11PrintMatrixPfi, @function
_Z11PrintMatrixPfi:
.LFB2029:
	.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
	leaq	.LC1(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movslq	%esi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%r13, %rbp
	salq	$2, %r13
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rdi, %rbx
	pushq	%rdx
	.cfi_def_cfa_offset 64
.L14:
	cmpl	%ebp, %r12d
	jge	.L19
	leaq	(%rbx,%r13), %r15
.L15:
	cvtss2sd	(%rbx), %xmm0
	movq	%r14, %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	addq	$4, %rbx
	cmpq	%r15, %rbx
	jne	.L15
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incl	%r12d
	call	__printf_chk@PLT
	jmp	.L14
.L19:
	popq	%rax
	.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
.LFE2029:
	.size	_Z11PrintMatrixPfi, .-_Z11PrintMatrixPfi
	.globl	_Z48__device_stub__Z20MatirxMultiplyKernelPKfS0_PfiiPKfS0_Pfii
	.type	_Z48__device_stub__Z20MatirxMultiplyKernelPKfS0_PfiiPKfS0_Pfii, @function
_Z48__device_stub__Z20MatirxMultiplyKernelPKfS0_PfiiPKfS0_Pfii:
.LFB2055:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 4(%rsp)
	leaq	40(%rsp), %rcx
	movl	%r8d, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 120(%rsp)
	movq	%rsp, %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L20
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z20MatirxMultiplyKernelPKfS0_Pfii(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L20:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_Z48__device_stub__Z20MatirxMultiplyKernelPKfS0_PfiiPKfS0_Pfii, .-_Z48__device_stub__Z20MatirxMultiplyKernelPKfS0_PfiiPKfS0_Pfii
	.globl	_Z20MatirxMultiplyKernelPKfS0_Pfii
	.type	_Z20MatirxMultiplyKernelPKfS0_Pfii, @function
_Z20MatirxMultiplyKernelPKfS0_Pfii:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_Z48__device_stub__Z20MatirxMultiplyKernelPKfS0_PfiiPKfS0_Pfii
	.cfi_endproc
.LFE2056:
	.size	_Z20MatirxMultiplyKernelPKfS0_Pfii, .-_Z20MatirxMultiplyKernelPKfS0_Pfii
	.section	.rodata.str1.1
.LC4:
	.string	"Matrix Multiply on Device: %fs\n"
	.text
	.globl	_Z22MatrixMultiplyOnDevicePKfS0_Pfii
	.type	_Z22MatrixMultiplyOnDevicePKfS0_Pfii, @function
_Z22MatrixMultiplyOnDevicePKfS0_Pfii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rsi, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%r8d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%ecx, %ebx
	imull	%ecx, %ebx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	sall	$2, %ebx
	leaq	8(%rsp), %rdi
	movslq	%ebx, %rbx
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	16(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r15, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rdi
	movq	%r14, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%ebp, %eax
	movl	%r12d, 32(%rsp)
	cltd
	movl	%r12d, 36(%rsp)
	idivl	%r12d
	movl	$1, 40(%rsp)
	movl	$1, 52(%rsp)
	movl	%eax, 44(%rsp)
	movl	%eax, 48(%rsp)
	call	clock@PLT
	movl	40(%rsp), %ecx
	movl	52(%rsp), %esi
	xorl	%r9d, %r9d
	movq	32(%rsp), %rdx
	movq	44(%rsp), %rdi
	xorl	%r8d, %r8d
	movq	%rax, %r14
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L26
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rsi
	movl	%r12d, %r8d
	movl	%ebp, %ecx
	movq	8(%rsp), %rdi
	call	_Z48__device_stub__Z20MatirxMultiplyKernelPKfS0_PfiiPKfS0_Pfii
.L26:
	call	clock@PLT
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	subq	%r14, %rax
	cvtsi2ssq	%rax, %xmm0
	movb	$1, %al
	divss	.LC3(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	24(%rsp), %rsi
	movq	%rbx, %rdx
	movq	%r13, %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	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	addq	$72, %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
.LFE2028:
	.size	_Z22MatrixMultiplyOnDevicePKfS0_Pfii, .-_Z22MatrixMultiplyOnDevicePKfS0_Pfii
	.section	.rodata.str1.1
.LC6:
	.string	"Matrix Multiply on Host: %fs\n"
.LC9:
	.string	"Average error is: %f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	$4194304, %edi
	xorl	%r14d, %r14d
	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
	movl	$4194304, %edi
	movq	%rax, %rbx
	call	_Znam@PLT
	movl	$4194304, %edi
	movq	%rax, %rbp
	call	_Znam@PLT
	movl	$4194304, %edi
	movq	%rax, %r13
	call	_Znam@PLT
	xorl	%edi, %edi
	movq	%rax, %r12
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
.L30:
	call	rand@PLT
	cvtsi2sdl	%eax, %xmm0
	mulsd	.LC5(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rbx,%r14)
	call	rand@PLT
	cvtsi2sdl	%eax, %xmm0
	mulsd	.LC5(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 0(%rbp,%r14)
	addq	$4, %r14
	cmpq	$4194304, %r14
	jne	.L30
	call	clock@PLT
	movl	$1024, %ecx
	movq	%r13, %rdx
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	movq	%rax, %r14
	call	_Z20MatrixMultiplyOnHostPfS_S_i
	call	clock@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	subq	%r14, %rax
	cvtsi2ssq	%rax, %xmm0
	movb	$1, %al
	divss	.LC3(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movl	$1024, %ecx
	movq	%r12, %rdx
	movq	%rbp, %rsi
	movl	$32, %r8d
	movq	%rbx, %rdi
	call	_Z22MatrixMultiplyOnDevicePKfS0_Pfii
	xorl	%eax, %eax
	xorps	%xmm0, %xmm0
	movss	.LC7(%rip), %xmm2
.L31:
	movss	0(%r13,%rax), %xmm1
	subss	(%r12,%rax), %xmm1
	addq	$4, %rax
	andps	%xmm2, %xmm1
	addss	%xmm1, %xmm0
	cmpq	$4194304, %rax
	jne	.L31
	mulss	.LC8(%rip), %xmm0
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	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
.LFE2030:
	.size	main, .-main
	.section	.rodata.str1.1
.LC10:
	.string	"_Z20MatirxMultiplyKernelPKfS0_Pfii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2058:
	.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	.LC10(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z20MatirxMultiplyKernelPKfS0_Pfii(%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
.LFE2058:
	.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
.LC3:
	.long	1232348160
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC5:
	.long	0
	.long	1040187392
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC7:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst4
	.align 4
.LC8:
	.long	897581056
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

// CUDA RumTime API
#define TILE_WIDTH 32

void MatrixMultiplyOnHost(float* M, float* N, float* P, int width)
{
	for(int i=0; i<width; ++i)
	{
		for (int j=0; j<width; ++j)
		{
			float sum = 0;
			for(int k=0; k<width; ++k)
			{
				float a = M[i*width+k];
				float b = N[k*width+j];
				sum += a*b;
			}
			P[i*width+j] = sum;
		}
	}

}

__global__ void MatirxMultiplyKernel(const float* devM, const float* devN, float* devP, const int width, const int tile_width)
{
	__shared__ float sM[TILE_WIDTH][TILE_WIDTH];
	__shared__ float sN[TILE_WIDTH][TILE_WIDTH];

	int bx = blockIdx.x;		int by = blockIdx.y;
	int tx = threadIdx.x;		int ty = threadIdx.y;

	int col = bx*tile_width+tx;
	int row = by*tile_width+ty;

	//Initialize accumulator to 0
	float pValue = 0;

	//Multiply and add
	for(int m=0; m<width/tile_width;m++)
	{
		sM[ty][tx] = devM[row*width+(m*tile_width+tx)];
		sN[ty][tx] = devN[col+(m*tile_width+ty)*width];
		//each time bring one element from devM and devN into shared memory
		//threads are blocked until all the threads reach this point
		__syncthreads();

		for(int k=0; k<tile_width; k++)
			pValue += sM[ty][k]*sN[k][tx];
		__syncthreads();
	}
	
	//Write value to device memory - each thread has unique index to write to
	devP[row*width+col] = pValue;
}

void MatrixMultiplyOnDevice(const float* hostM, const float* hostN, float* hostP, const int width, const int tile_width)
{
	int sizeInBytes = width*width*sizeof(float);
	float *devM, *devN, *devP;

	//Allocate M and N on devide
	hipMalloc((void**)&devM, sizeInBytes);
	hipMalloc((void**)&devN, sizeInBytes);

	//Allocate P
	hipMalloc((void**)&devP, sizeInBytes);

	//Copy M and N from host to device
	hipMemcpy(devM, hostM, sizeInBytes, hipMemcpyHostToDevice);
	hipMemcpy(devN, hostN, sizeInBytes, hipMemcpyHostToDevice);

	//Setup thread/block execution configuration
	dim3 dimBlocks(tile_width,tile_width); //Each block has (width,width) threads
	dim3 dimGrid(width/tile_width,width/tile_width); //Launch 1 block


	//Launch the kernel
	clock_t begin = clock();
	MatirxMultiplyKernel<<<dimGrid,dimBlocks>>>(devM,devN,devP,width,tile_width);
	clock_t end = clock();
	float elapsed_secs = float(end - begin) / CLOCKS_PER_SEC;
	printf("Matrix Multiply on Device: %fs\n",elapsed_secs);
	

	//Copy P matrix from device to host
	hipMemcpy(hostP, devP, sizeInBytes, hipMemcpyDeviceToHost);

	//Free allocated memory
	hipFree(devM); hipFree(devN); hipFree(devP);
}

void PrintMatrix(float* M, int width)
{
	for(int i=0; i<width; i++)
	{
		for(int j=0; j<width; j++)
		{
			printf("%f ",M[i*width+j]);
		}
		printf("\n");
	}
}

int main()
{
	int width = 1024;
	int tile_width = 32;
	int size = width*width;

	float* M = new float[size];
	float* N = new float[size];
	float* P = new float[size];
	float* Q = new float[size];
	srand (time(NULL));
	for(int i=0; i<size; i++)
	{
		M[i] = rand() / (RAND_MAX + 1.);
		N[i] = rand() / (RAND_MAX + 1.);
	}

	//multiply on host
	clock_t begin = clock();
	MatrixMultiplyOnHost(M,N,P,width);
	clock_t end = clock();
	float elapsed_secs = float(end - begin) / CLOCKS_PER_SEC;
	printf("Matrix Multiply on Host: %fs\n",elapsed_secs);
	//std::cout << "Matrix Multiply on Host: " << elapsed_secs << std::endl;
	
	//multiply on device
	//1. Copy M,N matrices to device
	//2. M*N on device
	//3. Copy P matrix to host and output
	MatrixMultiplyOnDevice(M,N,Q,width,tile_width);

	float avg_err = 0;
	for(int i=0; i<size; i++)
		avg_err += fabs(P[i]-Q[i]);
	avg_err /= size;
	printf("Average error is: %f\n",avg_err);
	//PrintMatrix(M,width);
	//PrintMatrix(N,width);
	//PrintMatrix(P,width);
	//PrintMatrix(Q,width);

	return 0;
}
	.text
	.file	"multiply.hip"
	.globl	_Z20MatrixMultiplyOnHostPfS_S_i # -- Begin function _Z20MatrixMultiplyOnHostPfS_S_i
	.type	_Z20MatrixMultiplyOnHostPfS_S_i,@function
_Z20MatrixMultiplyOnHostPfS_S_i:        # @_Z20MatrixMultiplyOnHostPfS_S_i
	.cfi_startproc
# %bb.0:
	testl	%ecx, %ecx
	jle	.LBB0_8
# %bb.1:                                # %.preheader28.lr.ph
	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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	%ecx, %eax
	leaq	(,%rax,4), %r8
	xorl	%r9d, %r9d
	xorl	%r10d, %r10d
.LBB0_2:                                # %.preheader.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_3 Depth 2
                                        #       Child Loop BB0_4 Depth 3
	movl	%r9d, %r11d
	leaq	(%rdi,%r11,4), %r11
	movq	%r10, %rbx
	imulq	%rax, %rbx
	leaq	(%rdx,%rbx,4), %rbx
	movq	%rsi, %r14
	xorl	%r15d, %r15d
.LBB0_3:                                # %.preheader
                                        #   Parent Loop BB0_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB0_4 Depth 3
	xorps	%xmm0, %xmm0
	movq	%r14, %r12
	xorl	%r13d, %r13d
.LBB0_4:                                #   Parent Loop BB0_2 Depth=1
                                        #     Parent Loop BB0_3 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%r11,%r13,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	mulss	(%r12), %xmm1
	addss	%xmm1, %xmm0
	incq	%r13
	addq	%r8, %r12
	cmpq	%r13, %rax
	jne	.LBB0_4
# %bb.5:                                # %._crit_edge
                                        #   in Loop: Header=BB0_3 Depth=2
	movss	%xmm0, (%rbx,%r15,4)
	incq	%r15
	addq	$4, %r14
	cmpq	%rax, %r15
	jne	.LBB0_3
# %bb.6:                                # %._crit_edge32
                                        #   in Loop: Header=BB0_2 Depth=1
	incq	%r10
	addl	%ecx, %r9d
	cmpq	%rax, %r10
	jne	.LBB0_2
# %bb.7:
	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
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r13
	.cfi_restore %r14
	.cfi_restore %r15
.LBB0_8:                                # %._crit_edge34
	retq
.Lfunc_end0:
	.size	_Z20MatrixMultiplyOnHostPfS_S_i, .Lfunc_end0-_Z20MatrixMultiplyOnHostPfS_S_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z35__device_stub__MatirxMultiplyKernelPKfS0_Pfii # -- Begin function _Z35__device_stub__MatirxMultiplyKernelPKfS0_Pfii
	.type	_Z35__device_stub__MatirxMultiplyKernelPKfS0_Pfii,@function
_Z35__device_stub__MatirxMultiplyKernelPKfS0_Pfii: # @_Z35__device_stub__MatirxMultiplyKernelPKfS0_Pfii
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$128, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	movq	%rsp, %rcx
	movl	%r8d, (%rcx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	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	$_Z20MatirxMultiplyKernelPKfS0_Pfii, %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_end1:
	.size	_Z35__device_stub__MatirxMultiplyKernelPKfS0_Pfii, .Lfunc_end1-_Z35__device_stub__MatirxMultiplyKernelPKfS0_Pfii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z22MatrixMultiplyOnDevicePKfS0_Pfii
.LCPI2_0:
	.long	0x49742400                      # float 1.0E+6
	.text
	.globl	_Z22MatrixMultiplyOnDevicePKfS0_Pfii
	.type	_Z22MatrixMultiplyOnDevicePKfS0_Pfii,@function
_Z22MatrixMultiplyOnDevicePKfS0_Pfii:   # @_Z22MatrixMultiplyOnDevicePKfS0_Pfii
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	.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	%r8d, 12(%rsp)                  # 4-byte Spill
	movl	%ecx, %r15d
	movq	%rdx, 32(%rsp)                  # 8-byte Spill
	movq	%rsi, %r12
	movq	%rdi, %r13
	movl	%ecx, %eax
	imull	%ecx, %eax
	shll	$2, %eax
	movslq	%eax, %r14
	leaq	24(%rsp), %rbx
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	16(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	%rsp, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	(%rbx), %rdi
	movq	%r13, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%rbp), %rdi
	movl	12(%rsp), %ebp                  # 4-byte Reload
	movq	%r12, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%ebp, %eax
	movq	%rax, %rbx
	shlq	$32, %rbx
	orq	%rax, %rbx
	movl	%r15d, %eax
	cltd
	idivl	%ebp
                                        # kill: def $eax killed $eax def $rax
	movq	%rax, %r13
	shlq	$32, %r13
	orq	%rax, %r13
	callq	clock
	movq	%rax, %r12
	movq	%r13, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_2
# %bb.1:
	movq	24(%rsp), %rdi
	movq	16(%rsp), %rsi
	movq	(%rsp), %rdx
	movl	%r15d, %ecx
	movl	%ebp, %r8d
	callq	_Z35__device_stub__MatirxMultiplyKernelPKfS0_Pfii
.LBB2_2:
	callq	clock
	subq	%r12, %rax
	cvtsi2ss	%rax, %xmm0
	divss	.LCPI2_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	movq	(%rsp), %rsi
	movq	32(%rsp), %rdi                  # 8-byte Reload
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	addq	$40, %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
.Lfunc_end2:
	.size	_Z22MatrixMultiplyOnDevicePKfS0_Pfii, .Lfunc_end2-_Z22MatrixMultiplyOnDevicePKfS0_Pfii
	.cfi_endproc
                                        # -- End function
	.globl	_Z11PrintMatrixPfi              # -- Begin function _Z11PrintMatrixPfi
	.type	_Z11PrintMatrixPfi,@function
_Z11PrintMatrixPfi:                     # @_Z11PrintMatrixPfi
	.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
	pushq	%rax
	.cfi_def_cfa_offset 64
	.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	%rdi, (%rsp)                    # 8-byte Spill
	testl	%esi, %esi
	jle	.LBB3_5
# %bb.1:                                # %.preheader.lr.ph
	movl	%esi, %ebx
	movl	%esi, %r15d
	xorl	%r12d, %r12d
	xorl	%r13d, %r13d
.LBB3_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_3 Depth 2
	movl	%r12d, %eax
	movq	(%rsp), %rcx                    # 8-byte Reload
	leaq	(%rcx,%rax,4), %rbp
	xorl	%r14d, %r14d
.LBB3_3:                                #   Parent Loop BB3_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbp,%r14,4), %xmm0
	movl	$.L.str.1, %edi
	movb	$1, %al
	callq	printf
	incq	%r14
	cmpq	%r14, %r15
	jne	.LBB3_3
# %bb.4:                                # %._crit_edge
                                        #   in Loop: Header=BB3_2 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r13
	addl	%ebx, %r12d
	cmpq	%r15, %r13
	jne	.LBB3_2
.LBB3_5:                                # %._crit_edge13
	addq	$8, %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
.Lfunc_end3:
	.size	_Z11PrintMatrixPfi, .Lfunc_end3-_Z11PrintMatrixPfi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI4_0:
	.quad	0x3e00000000000000              # double 4.6566128730773926E-10
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI4_1:
	.long	0x49742400                      # float 1.0E+6
.LCPI4_3:
	.long	0x35800000                      # float 9.53674316E-7
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI4_2:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.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
	pushq	%rax
	.cfi_def_cfa_offset 64
	.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	$4194304, %edi                  # imm = 0x400000
	callq	_Znam
	movq	%rax, %r15
	movl	$4194304, %edi                  # imm = 0x400000
	callq	_Znam
	movq	%rax, %r12
	movl	$4194304, %edi                  # imm = 0x400000
	callq	_Znam
	movq	%rax, %rbx
	movl	$4194304, %edi                  # imm = 0x400000
	callq	_Znam
	movq	%rax, %r14
	xorl	%r13d, %r13d
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
.LBB4_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	movsd	.LCPI4_0(%rip), %xmm1           # xmm1 = mem[0],zero
	mulsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%r15,%r13,4)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	mulsd	.LCPI4_0(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%r12,%r13,4)
	incq	%r13
	cmpq	$1048576, %r13                  # imm = 0x100000
	jne	.LBB4_1
# %bb.2:
	xorl	%ebp, %ebp
	callq	clock
	movq	%rax, %r13
	movq	%r15, %rax
.LBB4_3:                                # %.preheader.lr.ph.i
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_4 Depth 2
                                        #       Child Loop BB4_5 Depth 3
	movq	%rbp, %rcx
	shlq	$12, %rcx
	addq	%rbx, %rcx
	movq	%r12, %rdx
	xorl	%esi, %esi
.LBB4_4:                                # %.preheader.i
                                        #   Parent Loop BB4_3 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB4_5 Depth 3
	xorps	%xmm0, %xmm0
	movq	%rdx, %rdi
	xorl	%r8d, %r8d
.LBB4_5:                                #   Parent Loop BB4_3 Depth=1
                                        #     Parent Loop BB4_4 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%rax,%r8,4), %xmm1             # xmm1 = mem[0],zero,zero,zero
	mulss	(%rdi), %xmm1
	addss	%xmm1, %xmm0
	incq	%r8
	addq	$4096, %rdi                     # imm = 0x1000
	cmpq	$1024, %r8                      # imm = 0x400
	jne	.LBB4_5
# %bb.6:                                # %._crit_edge.i
                                        #   in Loop: Header=BB4_4 Depth=2
	movss	%xmm0, (%rcx,%rsi,4)
	incq	%rsi
	addq	$4, %rdx
	cmpq	$1024, %rsi                     # imm = 0x400
	jne	.LBB4_4
# %bb.7:                                # %._crit_edge32.i
                                        #   in Loop: Header=BB4_3 Depth=1
	incq	%rbp
	addq	$4096, %rax                     # imm = 0x1000
	cmpq	$1024, %rbp                     # imm = 0x400
	jne	.LBB4_3
# %bb.8:                                # %_Z20MatrixMultiplyOnHostPfS_S_i.exit
	callq	clock
	subq	%r13, %rax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
	divss	.LCPI4_1(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.3, %edi
	movb	$1, %al
	callq	printf
	movq	%r15, %rdi
	movq	%r12, %rsi
	movq	%r14, %rdx
	movl	$1024, %ecx                     # imm = 0x400
	movl	$32, %r8d
	callq	_Z22MatrixMultiplyOnDevicePKfS0_Pfii
	xorps	%xmm0, %xmm0
	xorl	%eax, %eax
	movaps	.LCPI4_2(%rip), %xmm1           # xmm1 = [NaN,NaN,NaN,NaN]
.LBB4_9:                                # =>This Inner Loop Header: Depth=1
	movss	(%rbx,%rax,4), %xmm2            # xmm2 = mem[0],zero,zero,zero
	subss	(%r14,%rax,4), %xmm2
	andps	%xmm1, %xmm2
	addss	%xmm2, %xmm0
	incq	%rax
	cmpq	$1048576, %rax                  # imm = 0x100000
	jne	.LBB4_9
# %bb.10:
	mulss	.LCPI4_3(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	xorl	%eax, %eax
	addq	$8, %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
.Lfunc_end4:
	.size	main, .Lfunc_end4-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z20MatirxMultiplyKernelPKfS0_Pfii, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB6_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z20MatirxMultiplyKernelPKfS0_Pfii,@object # @_Z20MatirxMultiplyKernelPKfS0_Pfii
	.section	.rodata,"a",@progbits
	.globl	_Z20MatirxMultiplyKernelPKfS0_Pfii
	.p2align	3, 0x0
_Z20MatirxMultiplyKernelPKfS0_Pfii:
	.quad	_Z35__device_stub__MatirxMultiplyKernelPKfS0_Pfii
	.size	_Z20MatirxMultiplyKernelPKfS0_Pfii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Matrix Multiply on Device: %fs\n"
	.size	.L.str, 32

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

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Matrix Multiply on Host: %fs\n"
	.size	.L.str.3, 30

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Average error is: %f\n"
	.size	.L.str.4, 22

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z20MatirxMultiplyKernelPKfS0_Pfii"
	.size	.L__unnamed_1, 35

	.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 _Z35__device_stub__MatirxMultiplyKernelPKfS0_Pfii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z20MatirxMultiplyKernelPKfS0_Pfii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z20MatirxMultiplyKernelPKfS0_Pfii ; -- Begin function _Z20MatirxMultiplyKernelPKfS0_Pfii
	.globl	_Z20MatirxMultiplyKernelPKfS0_Pfii
	.p2align	8
	.type	_Z20MatirxMultiplyKernelPKfS0_Pfii,@function
_Z20MatirxMultiplyKernelPKfS0_Pfii:     ; @_Z20MatirxMultiplyKernelPKfS0_Pfii
; %bb.0:
	s_load_b256 s[4:11], s[0:1], 0x0
	v_and_b32_e32 v3, 0x3ff, v0
	v_bfe_u32 v2, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_ashr_i32 s0, s11, 31
	s_ashr_i32 s12, s10, 31
	s_add_i32 s1, s11, s0
	s_add_i32 s13, s10, s12
	s_xor_b32 s1, s1, s0
	s_xor_b32 s13, s13, s12
	v_cvt_f32_u32_e32 v1, s1
	s_sub_i32 s3, 0, s1
	v_mad_u64_u32 v[4:5], null, s15, s11, v[2:3]
	s_xor_b32 s0, s12, s0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_iflag_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x4f7ffffe, v1
	v_cvt_u32_f32_e32 v1, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_readfirstlane_b32 s2, v1
	v_mad_u64_u32 v[0:1], null, s14, s11, v[3:4]
	v_mul_lo_u32 v1, v4, s10
	s_mul_i32 s3, s3, s2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_hi_u32 s3, s2, s3
	s_add_i32 s2, s2, s3
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_hi_u32 s2, s13, s2
	s_mul_i32 s3, s2, s1
	s_add_i32 s12, s2, 1
	s_sub_i32 s3, s13, s3
	s_delay_alu instid0(SALU_CYCLE_1)
	s_sub_i32 s13, s3, s1
	s_cmp_ge_u32 s3, s1
	s_cselect_b32 s2, s12, s2
	s_cselect_b32 s3, s13, s3
	s_add_i32 s12, s2, 1
	s_cmp_ge_u32 s3, s1
	s_cselect_b32 s1, s12, s2
	s_mov_b32 s2, 0
	s_xor_b32 s1, s1, s0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_sub_i32 s1, s1, s0
	s_cmp_lt_i32 s1, 1
	s_cbranch_scc1 .LBB0_5
; %bb.1:                                ; %.lr.ph48
	v_lshlrev_b32_e32 v7, 2, v3
	s_cmp_gt_i32 s11, 0
	v_lshlrev_b32_e32 v4, 7, v2
	s_cselect_b32 s0, -1, 0
	v_dual_mov_b32 v3, 0 :: v_dual_add_nc_u32 v6, v1, v3
	v_or_b32_e32 v5, 0x1000, v7
	v_cndmask_b32_e64 v9, 0, 1, s0
	v_add_nc_u32_e32 v7, v4, v7
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v8, v5, v4
	v_cmp_ne_u32_e64 s0, 1, v9
.LBB0_2:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_3 Depth 2
	s_mul_i32 s3, s2, s11
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v10, s3, v2
	v_add_nc_u32_e32 v9, s3, v6
	s_mov_b32 s3, s11
	v_mad_u64_u32 v[11:12], null, v10, s10, v[0:1]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v10, 31, v9
	v_lshlrev_b64 v[9:10], 2, v[9:10]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_ashrrev_i32_e32 v12, 31, v11
	v_add_co_u32 v9, vcc_lo, s4, v9
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_lshlrev_b64 v[11:12], 2, v[11:12]
	v_add_co_ci_u32_e32 v10, vcc_lo, s5, v10, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_u32 v11, vcc_lo, s6, v11
	v_add_co_ci_u32_e32 v12, vcc_lo, s7, v12, vcc_lo
	global_load_b32 v13, v[9:10], off
	global_load_b32 v11, v[11:12], off
	v_dual_mov_b32 v10, v4 :: v_dual_mov_b32 v9, v5
	s_and_b32 vcc_lo, exec_lo, s0
	s_waitcnt vmcnt(1)
	ds_store_b32 v7, v13
	s_waitcnt vmcnt(0)
	ds_store_b32 v8, v11
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_vccnz .LBB0_4
.LBB0_3:                                ; %.lr.ph
                                        ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	ds_load_b32 v11, v10
	ds_load_b32 v12, v9
	v_add_nc_u32_e32 v10, 4, v10
	v_add_nc_u32_e32 v9, 0x80, v9
	s_add_i32 s3, s3, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s3, 0
	s_waitcnt lgkmcnt(0)
	v_fmac_f32_e32 v3, v11, v12
	s_cbranch_scc0 .LBB0_3
.LBB0_4:                                ; %._crit_edge
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_add_i32 s2, s2, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s2, s1
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB0_2
	s_branch .LBB0_6
.LBB0_5:
	v_mov_b32_e32 v3, 0
.LBB0_6:                                ; %Flow73
	v_add_nc_u32_e32 v0, v1, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v1, 31, v0
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v0, vcc_lo, s8, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s9, v1, vcc_lo
	global_store_b32 v[0:1], v3, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z20MatirxMultiplyKernelPKfS0_Pfii
		.amdhsa_group_segment_fixed_size 8192
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 32
		.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 14
		.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	_Z20MatirxMultiplyKernelPKfS0_Pfii, .Lfunc_end0-_Z20MatirxMultiplyKernelPKfS0_Pfii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 580
; NumSgprs: 18
; NumVgprs: 14
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 8192 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 14
; 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
      - .offset:         28
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 8192
    .kernarg_segment_align: 8
    .kernarg_segment_size: 32
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z20MatirxMultiplyKernelPKfS0_Pfii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z20MatirxMultiplyKernelPKfS0_Pfii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     14
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
