//This is a matrix multiplication program in CUDA without any optimizations
//like tiling, using shared memory etc

#include<stdio.h>
#include<stdlib.h>
#include<cuda_runtime.h>
#include<assert.h>


__global__ void MatrixMulKernel(float* Md, float* Nd, float* Pd, int width)
{

	//2D thread ID 
	int bx=blockIdx.x;
	int by=blockIdx.y;
	int tdx=threadIdx.x;
	int tdy=threadIdx.y;

	int tx=bx*blockDim.x+tdx;
	int ty=by*blockDim.y+tdy;
	
	//Pvalue stores the Pd element that is computed by the thread
	float Pvalue=0;
	for(int k=0;k<width;++k){
		float Mdelement=Md[ty*width+k];
		float Ndelement=Nd[k*width+tx];
		Pvalue += Mdelement*Ndelement;
	}
	//Write the matrix to device memory each thread writes one element
	Pd[ty*width+tx]=Pvalue;
}


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

	int width;
	int BlockDim;
	int GridDim;

	if (argc == 3){
		width=atoi(argv[1]);
		BlockDim=atoi(argv[2]);
		GridDim=width/BlockDim;
		printf("Using matrix dimension %dx%d ,Block Dim %dx%d threads per block, Grid Dim %dx%d blocks per grid\n",width,width,BlockDim,BlockDim,GridDim,GridDim);
	}else{
		width=512;
		BlockDim=16;
		GridDim=width/BlockDim;
		printf("Using Default Parameters: matrix dimension %dx%d ,Block Dim %dx%d threads per block, Grid Dim %dx%d blocks per grid\n",width,width,BlockDim,BlockDim,GridDim,GridDim);		
	}
	dim3 dimBlock(BlockDim,BlockDim);
	dim3 dimGrid(GridDim,GridDim);
	cudaError_t error;
	cudaDeviceProp deviceProp;
	int devID=0;
	error=cudaGetDevice(&devID);
	if (error != cudaSuccess)
	{
		printf("cudaGetDevice returned error code %d, line(%d)\n", error, __LINE__);
	}

	error=cudaGetDeviceProperties(&deviceProp,devID);
	if (error != cudaSuccess){
		printf("cudaGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
	}else{
		printf("GPU Device %d: \"%s\" with compute capability %d.%d\n\n", devID, deviceProp.name, deviceProp.major, deviceProp.minor);
	}

	int size=width*width*sizeof(float);
	float* M=(float*)malloc(size);
	float* N=(float*)malloc(size);
	float* P=(float*)malloc(size);

	float* Md,*Nd,*Pd;

	if(!(M&&N)){
		printf("Malloc failed\n");
		exit(-1);
	}

	 // initialization of host data
	for (int j = 0; j < width; j++) {
		for (int i = 0; i < width; i++) {
			M[j*width + i] = (float)(rand()%50);
			N[j*width + i] = (float)(rand()%50);
			P[j*width + i] = 0;
		}
	}

	error=cudaMalloc((void**)&Md,size);
	if(error!=cudaSuccess){
		printf("Device memory allocation for M failed \n");
		exit(-1);
	}
	error=cudaMalloc((void**)&Nd,size);
	if(error!=cudaSuccess){
		printf("Device memory allocation for N failed \n");
		exit(-1);
	}
	error=cudaMalloc((void**)&Pd,size);
	if(error!=cudaSuccess){
		printf("Device memory allocation for P failed \n");
		exit(-1);
	}

	error=cudaMemcpy(Md,M,size,cudaMemcpyHostToDevice);
	if(error!=cudaSuccess){
		printf("Device memory copy for M failed \n");
		exit(-1);
	}
	
	error=cudaMemcpy(Nd,N,size,cudaMemcpyHostToDevice);
	if(error!=cudaSuccess){
		printf("Device memory copy for N failed \n");
		exit(-1);
	}

	
	cudaEvent_t start;
	error=cudaEventCreate(&start);
	if(error!=cudaSuccess){
		printf("cuda event start failed \n");
		exit(-1);
	}

	cudaEvent_t stop;
	error=cudaEventCreate(&stop);
	if(error!=cudaSuccess){
		printf("cuda event stop failed \n");
		exit(-1);
	}

	error =cudaEventRecord(start,NULL);
	if(error!=cudaSuccess){
		printf("cuda event start record failed \n");
		exit(-1);
	}
	
	
	MatrixMulKernel<<<dimGrid,dimBlock>>>(Md,Nd,Pd,width);

//	error=cudaDeviceSynchronize();
	error =cudaEventRecord(stop,NULL);
	if(error!=cudaSuccess){
		printf("cuda event stop record failed with error=%s\n",cudaGetErrorString(error));
		exit(-1);
	}

	error = cudaEventSynchronize(stop);
	if(error!=cudaSuccess){
		printf("cuda event sync failed :%s\n",cudaGetErrorString(error));
		exit(-1);
	}
	


	float msecTotal=0.0f;
	error = cudaEventElapsedTime(&msecTotal,start,stop);
	if(error!=cudaSuccess){
		printf("cuda elapsed time calculation failed \n");
		exit(-1);
	}

	float msecPerMatrixMul = msecTotal;
	double flopsPerMatrixMul = 2*width*width*width;
	double gigaFlops=(flopsPerMatrixMul*1.0e-9f)/(msecPerMatrixMul/1000.0f);
	printf("Performance= %.2f GFlop/s, Time= %.3f msec, Size= %.0f Ops, WorkgroupSize= %u threads/block\n",
		    gigaFlops,
			msecPerMatrixMul,
			flopsPerMatrixMul,
			width * width);



	error=cudaMemcpy(P,Pd,size,cudaMemcpyDeviceToHost);
	if(error!=cudaSuccess){
		printf("Device memoory copy back for Pd failed \n");
		exit(-1);
	}

	printf("Very slow Host Matrix Mult \n");
	float temp;
	// initialization of host data
	for (int i = 0; i < width; ++i) {
		for ( int j = 0; j < width; ++j) {
			temp=0;
			for(int k=0; k<width; ++k)
				temp+=M[i*width+k]*N[k*width+j];
			if(temp != P[i*width+j]){
				printf("Matrix Mult Screwed Up!! differ in values CPU:%f and GPU:%f \n",temp,P[i*width+j]);
				exit(-1);
			}
		}
		
	}
	
	
	free(M);
	free(N);
	free(P);
	cudaFree(Md);	
	cudaFree(Nd);	
	cudaFree(Pd);	
	cudaDeviceReset();
	return 1;

}

	.file	"tmpxft_0039a235_00000000-6_matmul.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z40__device_stub__Z15MatrixMulKernelPfS_S_iPfS_S_i
	.type	_Z40__device_stub__Z15MatrixMulKernelPfS_S_iPfS_S_i, @function
_Z40__device_stub__Z15MatrixMulKernelPfS_S_iPfS_S_i:
.LFB2052:
	.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	.L2
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z15MatrixMulKernelPfS_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
.L2:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2052:
	.size	_Z40__device_stub__Z15MatrixMulKernelPfS_S_iPfS_S_i, .-_Z40__device_stub__Z15MatrixMulKernelPfS_S_iPfS_S_i
	.globl	_Z15MatrixMulKernelPfS_S_i
	.type	_Z15MatrixMulKernelPfS_S_i, @function
_Z15MatrixMulKernelPfS_S_i:
.LFB2053:
	.cfi_startproc
	endbr64
	jmp	_Z40__device_stub__Z15MatrixMulKernelPfS_S_iPfS_S_i
	.cfi_endproc
.LFE2053:
	.size	_Z15MatrixMulKernelPfS_S_i, .-_Z15MatrixMulKernelPfS_S_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"Using matrix dimension %dx%d ,Block Dim %dx%d threads per block, Grid Dim %dx%d blocks per grid\n"
.LC2:
	.string	"Using Default Parameters: matrix dimension %dx%d ,Block Dim %dx%d threads per block, Grid Dim %dx%d blocks per grid\n"
.LC3:
	.string	"cudaGetDevice returned error code %d, line(%d)\n"
.LC4:
	.string	"cudaGetDeviceProperties returned error code %d, line(%d)\n"
.LC5:
	.string	"GPU Device %d: \"%s\" with compute capability %d.%d\n\n"
.LC6:
	.string	"Malloc failed\n"
.LC7:
	.string	"Device memory allocation for M failed \n"
.LC8:
	.string	"Device memory allocation for N failed \n"
.LC9:
	.string	"Device memory allocation for P failed \n"
.LC10:
	.string	"Device memory copy for M failed \n"
.LC11:
	.string	"Device memory copy for N failed \n"
.LC12:
	.string	"cuda event start failed \n"
.LC13:
	.string	"cuda event stop failed \n"
.LC14:
	.string	"cuda event start record failed \n"
.LC15:
	.string	"cuda event stop record failed with error=%s\n"
.LC16:
	.string	"cuda event sync failed :%s\n"
.LC17:
	.string	"cuda elapsed time calculation failed \n"
.LC20:
	.string	"Performance= %.2f GFlop/s, Time= %.3f msec, Size= %.0f Ops, WorkgroupSize= %u threads/block\n"
.LC21:
	.string	"Device memoory copy back for Pd failed \n"
.LC22:
	.string	"Very slow Host Matrix Mult \n"
.LC23:
	.string	"Matrix Mult Screwed Up!! differ in values CPU:%f and GPU:%f \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
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$1160, %rsp
	.cfi_def_cfa_offset 1216
	movq	%fs:40, %rax
	movq	%rax, 1144(%rsp)
	xorl	%eax, %eax
	cmpl	$3, %edi
	jne	.L9
	movq	8(%rsi), %rdi
	movq	%rsi, %rbp
	call	atoi@PLT
	movq	16(%rbp), %rdi
	movl	%eax, %ebx
	call	atoi@PLT
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	movl	%ebx, %ecx
	movl	%eax, %ebp
	movl	%ebx, %eax
	cltd
	movl	%ebp, %r9d
	movl	%ebp, %r8d
	idivl	%ebp
	movl	%ebx, %edx
	pushq	%rax
	.cfi_def_cfa_offset 1224
	movl	%eax, %r12d
	pushq	%rax
	.cfi_def_cfa_offset 1232
	xorl	%eax, %eax
	call	__printf_chk@PLT
	popq	%rsi
	.cfi_def_cfa_offset 1224
	popq	%rdi
	.cfi_def_cfa_offset 1216
	jmp	.L10
.L9:
	pushq	$32
	.cfi_def_cfa_offset 1224
	movl	$512, %ecx
	movl	$512, %edx
	xorl	%eax, %eax
	pushq	$32
	.cfi_def_cfa_offset 1232
	movl	$16, %r9d
	movl	$16, %r8d
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	movl	$32, %r12d
	movl	$16, %ebp
	movl	$512, %ebx
	call	__printf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 1224
	popq	%rcx
	.cfi_def_cfa_offset 1216
.L10:
	xorl	%eax, %eax
	leaq	40(%rsp), %rdi
	movl	%ebp, 88(%rsp)
	movl	%ebp, 92(%rsp)
	movl	$1, 96(%rsp)
	movl	%r12d, 100(%rsp)
	movl	%r12d, 104(%rsp)
	movl	$1, 108(%rsp)
	movl	%eax, 40(%rsp)
	call	cudaGetDevice@PLT
	movl	%eax, %edx
	testl	%eax, %eax
	je	.L11
	movl	$59, %ecx
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L11:
	movl	40(%rsp), %esi
	leaq	112(%rsp), %rbp
	movq	%rbp, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movl	%eax, %edx
	testl	%eax, %eax
	je	.L12
	movl	$64, %ecx
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L13
.L12:
	movl	40(%rsp), %edx
	movq	%rbp, %rcx
	leaq	.LC5(%rip), %rsi
	xorl	%eax, %eax
	movl	476(%rsp), %r9d
	movl	472(%rsp), %r8d
	movl	$2, %edi
	call	__printf_chk@PLT
.L13:
	movl	%ebx, %r15d
	imull	%ebx, %r15d
	leal	0(,%r15,4), %eax
	cltq
	movq	%rax, %rdi
	movq	%rax, (%rsp)
	call	malloc@PLT
	movq	(%rsp), %rdi
	movq	%rax, %r12
	call	malloc@PLT
	movq	(%rsp), %rdi
	movq	%rax, %rbp
	call	malloc@PLT
	movq	%rax, 8(%rsp)
	testq	%r12, %r12
	je	.L14
	testq	%rbp, %rbp
	je	.L14
	movslq	%ebx, %r14
	xorl	%r13d, %r13d
	xorl	%esi, %esi
	salq	$2, %r14
	jmp	.L15
.L14:
	leaq	.LC6(%rip), %rsi
	jmp	.L46
.L16:
	movq	%rdi, 24(%rsp)
	movl	%esi, 20(%rsp)
	call	rand@PLT
	movl	$50, %ecx
	cltd
	idivl	%ecx
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, (%r12,%r13)
	call	rand@PLT
	movl	$50, %ecx
	movq	24(%rsp), %rdi
	movl	20(%rsp), %esi
	cltd
	idivl	%ecx
	movq	8(%rsp), %rax
	movl	$0x00000000, (%rax,%r13)
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, 0(%rbp,%r13)
	addq	$4, %r13
	cmpq	%rdi, %r13
	jne	.L16
	incl	%esi
.L15:
	cmpl	%esi, %ebx
	jle	.L49
	leaq	0(%r13,%r14), %rdi
	jmp	.L16
.L49:
	movq	(%rsp), %rsi
	leaq	48(%rsp), %rdi
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L18
	leaq	.LC7(%rip), %rsi
.L46:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L47:
	orl	$-1, %edi
	call	exit@PLT
.L18:
	movq	(%rsp), %rsi
	leaq	56(%rsp), %rdi
	call	cudaMalloc@PLT
	leaq	.LC8(%rip), %rsi
	testl	%eax, %eax
	jne	.L46
	movq	(%rsp), %rsi
	leaq	64(%rsp), %rdi
	call	cudaMalloc@PLT
	leaq	.LC9(%rip), %rsi
	testl	%eax, %eax
	jne	.L46
	movq	(%rsp), %rdx
	movq	48(%rsp), %rdi
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	.LC10(%rip), %rsi
	testl	%eax, %eax
	jne	.L46
	movq	(%rsp), %rdx
	movq	56(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	.LC11(%rip), %rsi
	testl	%eax, %eax
	jne	.L46
	leaq	72(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	.LC12(%rip), %rsi
	testl	%eax, %eax
	jne	.L46
	leaq	80(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	.LC13(%rip), %rsi
	testl	%eax, %eax
	jne	.L46
	movq	72(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	leaq	.LC14(%rip), %rsi
	testl	%eax, %eax
	jne	.L46
	movl	96(%rsp), %ecx
	movq	88(%rsp), %rdx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	100(%rsp), %rdi
	movl	108(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L26
	movq	64(%rsp), %rdx
	movq	56(%rsp), %rsi
	movl	%ebx, %ecx
	movq	48(%rsp), %rdi
	call	_Z40__device_stub__Z15MatrixMulKernelPfS_S_iPfS_S_i
.L26:
	movq	80(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L27
	call	cudaGetErrorString@PLT
	leaq	.LC15(%rip), %rsi
	movq	%rax, %rdx
	jmp	.L48
.L27:
	movq	80(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L28
	call	cudaGetErrorString@PLT
	leaq	.LC16(%rip), %rsi
	movq	%rax, %rdx
.L48:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L47
.L28:
	movq	72(%rsp), %rsi
	movq	80(%rsp), %rdx
	leaq	44(%rsp), %rdi
	movl	$0x00000000, 44(%rsp)
	call	cudaEventElapsedTime@PLT
	leaq	.LC17(%rip), %rsi
	testl	%eax, %eax
	jne	.L46
	movl	%r15d, %eax
	movsd	.LC18(%rip), %xmm0
	movss	44(%rsp), %xmm1
	movl	%r15d, %edx
	imull	%ebx, %eax
	leaq	.LC20(%rip), %rsi
	movl	$2, %edi
	movaps	%xmm1, %xmm3
	divss	.LC19(%rip), %xmm3
	cvtss2sd	%xmm3, %xmm3
	cvtss2sd	%xmm1, %xmm1
	addl	%eax, %eax
	cvtsi2sdl	%eax, %xmm2
	movb	$3, %al
	mulsd	%xmm2, %xmm0
	divsd	%xmm3, %xmm0
	call	__printf_chk@PLT
	movq	64(%rsp), %rsi
	movq	(%rsp), %rdx
	movl	$2, %ecx
	movq	8(%rsp), %rdi
	call	cudaMemcpy@PLT
	leaq	.LC21(%rip), %rsi
	testl	%eax, %eax
	jne	.L46
	leaq	.LC22(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edx, %edx
	xorl	%esi, %esi
.L31:
	cmpl	%esi, %ebx
	jle	.L50
	movq	8(%rsp), %rcx
	xorl	%eax, %eax
	leaq	(%r12,%rdx), %r8
	leaq	(%rcx,%rdx), %r9
.L36:
	leaq	0(%rbp,%rax,4), %rdi
	xorl	%ecx, %ecx
	xorps	%xmm0, %xmm0
.L32:
	movss	(%r8,%rcx,4), %xmm1
	mulss	(%rdi), %xmm1
	incq	%rcx
	addq	%r14, %rdi
	addss	%xmm1, %xmm0
	cmpl	%ecx, %ebx
	jg	.L32
	movss	(%r9,%rax,4), %xmm1
	ucomiss	%xmm0, %xmm1
	jp	.L39
	je	.L33
.L39:
	movl	$2, %edi
	movb	$2, %al
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	leaq	.LC23(%rip), %rsi
	call	__printf_chk@PLT
	jmp	.L47
.L33:
	incq	%rax
	cmpl	%eax, %ebx
	jg	.L36
	incl	%esi
	addq	%r14, %rdx
	jmp	.L31
.L50:
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	8(%rsp), %rdi
	call	free@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	call	cudaDeviceReset@PLT
	movq	1144(%rsp), %rax
	subq	%fs:40, %rax
	je	.L37
	call	__stack_chk_fail@PLT
.L37:
	addq	$1160, %rsp
	.cfi_def_cfa_offset 56
	movl	$1, %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
.LC24:
	.string	"_Z15MatrixMulKernelPfS_S_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC24(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z15MatrixMulKernelPfS_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
.LFE2055:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC18:
	.long	-536870912
	.long	1041313291
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC19:
	.long	1148846080
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

//This is a matrix multiplication program in CUDA without any optimizations
//like tiling, using shared memory etc

#include<stdio.h>
#include<stdlib.h>
#include<hip/hip_runtime.h>
#include<assert.h>


__global__ void MatrixMulKernel(float* Md, float* Nd, float* Pd, int width)
{

	//2D thread ID 
	int bx=blockIdx.x;
	int by=blockIdx.y;
	int tdx=threadIdx.x;
	int tdy=threadIdx.y;

	int tx=bx*blockDim.x+tdx;
	int ty=by*blockDim.y+tdy;
	
	//Pvalue stores the Pd element that is computed by the thread
	float Pvalue=0;
	for(int k=0;k<width;++k){
		float Mdelement=Md[ty*width+k];
		float Ndelement=Nd[k*width+tx];
		Pvalue += Mdelement*Ndelement;
	}
	//Write the matrix to device memory each thread writes one element
	Pd[ty*width+tx]=Pvalue;
}


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

	int width;
	int BlockDim;
	int GridDim;

	if (argc == 3){
		width=atoi(argv[1]);
		BlockDim=atoi(argv[2]);
		GridDim=width/BlockDim;
		printf("Using matrix dimension %dx%d ,Block Dim %dx%d threads per block, Grid Dim %dx%d blocks per grid\n",width,width,BlockDim,BlockDim,GridDim,GridDim);
	}else{
		width=512;
		BlockDim=16;
		GridDim=width/BlockDim;
		printf("Using Default Parameters: matrix dimension %dx%d ,Block Dim %dx%d threads per block, Grid Dim %dx%d blocks per grid\n",width,width,BlockDim,BlockDim,GridDim,GridDim);		
	}
	dim3 dimBlock(BlockDim,BlockDim);
	dim3 dimGrid(GridDim,GridDim);
	hipError_t error;
	hipDeviceProp_t deviceProp;
	int devID=0;
	error=hipGetDevice(&devID);
	if (error != hipSuccess)
	{
		printf("hipGetDevice returned error code %d, line(%d)\n", error, __LINE__);
	}

	error=hipGetDeviceProperties(&deviceProp,devID);
	if (error != hipSuccess){
		printf("hipGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
	}else{
		printf("GPU Device %d: \"%s\" with compute capability %d.%d\n\n", devID, deviceProp.name, deviceProp.major, deviceProp.minor);
	}

	int size=width*width*sizeof(float);
	float* M=(float*)malloc(size);
	float* N=(float*)malloc(size);
	float* P=(float*)malloc(size);

	float* Md,*Nd,*Pd;

	if(!(M&&N)){
		printf("Malloc failed\n");
		exit(-1);
	}

	 // initialization of host data
	for (int j = 0; j < width; j++) {
		for (int i = 0; i < width; i++) {
			M[j*width + i] = (float)(rand()%50);
			N[j*width + i] = (float)(rand()%50);
			P[j*width + i] = 0;
		}
	}

	error=hipMalloc((void**)&Md,size);
	if(error!=hipSuccess){
		printf("Device memory allocation for M failed \n");
		exit(-1);
	}
	error=hipMalloc((void**)&Nd,size);
	if(error!=hipSuccess){
		printf("Device memory allocation for N failed \n");
		exit(-1);
	}
	error=hipMalloc((void**)&Pd,size);
	if(error!=hipSuccess){
		printf("Device memory allocation for P failed \n");
		exit(-1);
	}

	error=hipMemcpy(Md,M,size,hipMemcpyHostToDevice);
	if(error!=hipSuccess){
		printf("Device memory copy for M failed \n");
		exit(-1);
	}
	
	error=hipMemcpy(Nd,N,size,hipMemcpyHostToDevice);
	if(error!=hipSuccess){
		printf("Device memory copy for N failed \n");
		exit(-1);
	}

	
	hipEvent_t start;
	error=hipEventCreate(&start);
	if(error!=hipSuccess){
		printf("cuda event start failed \n");
		exit(-1);
	}

	hipEvent_t stop;
	error=hipEventCreate(&stop);
	if(error!=hipSuccess){
		printf("cuda event stop failed \n");
		exit(-1);
	}

	error =hipEventRecord(start,NULL);
	if(error!=hipSuccess){
		printf("cuda event start record failed \n");
		exit(-1);
	}
	
	
	MatrixMulKernel<<<dimGrid,dimBlock>>>(Md,Nd,Pd,width);

//	error=cudaDeviceSynchronize();
	error =hipEventRecord(stop,NULL);
	if(error!=hipSuccess){
		printf("cuda event stop record failed with error=%s\n",hipGetErrorString(error));
		exit(-1);
	}

	error = hipEventSynchronize(stop);
	if(error!=hipSuccess){
		printf("cuda event sync failed :%s\n",hipGetErrorString(error));
		exit(-1);
	}
	


	float msecTotal=0.0f;
	error = hipEventElapsedTime(&msecTotal,start,stop);
	if(error!=hipSuccess){
		printf("cuda elapsed time calculation failed \n");
		exit(-1);
	}

	float msecPerMatrixMul = msecTotal;
	double flopsPerMatrixMul = 2*width*width*width;
	double gigaFlops=(flopsPerMatrixMul*1.0e-9f)/(msecPerMatrixMul/1000.0f);
	printf("Performance= %.2f GFlop/s, Time= %.3f msec, Size= %.0f Ops, WorkgroupSize= %u threads/block\n",
		    gigaFlops,
			msecPerMatrixMul,
			flopsPerMatrixMul,
			width * width);



	error=hipMemcpy(P,Pd,size,hipMemcpyDeviceToHost);
	if(error!=hipSuccess){
		printf("Device memoory copy back for Pd failed \n");
		exit(-1);
	}

	printf("Very slow Host Matrix Mult \n");
	float temp;
	// initialization of host data
	for (int i = 0; i < width; ++i) {
		for ( int j = 0; j < width; ++j) {
			temp=0;
			for(int k=0; k<width; ++k)
				temp+=M[i*width+k]*N[k*width+j];
			if(temp != P[i*width+j]){
				printf("Matrix Mult Screwed Up!! differ in values CPU:%f and GPU:%f \n",temp,P[i*width+j]);
				exit(-1);
			}
		}
		
	}
	
	
	free(M);
	free(N);
	free(P);
	hipFree(Md);	
	hipFree(Nd);	
	hipFree(Pd);	
	hipDeviceReset();
	return 1;

}

	.text
	.file	"matmul.hip"
	.globl	_Z30__device_stub__MatrixMulKernelPfS_S_i # -- Begin function _Z30__device_stub__MatrixMulKernelPfS_S_i
	.type	_Z30__device_stub__MatrixMulKernelPfS_S_i,@function
_Z30__device_stub__MatrixMulKernelPfS_S_i: # @_Z30__device_stub__MatrixMulKernelPfS_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	$_Z15MatrixMulKernelPfS_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_end0:
	.size	_Z30__device_stub__MatrixMulKernelPfS_S_i, .Lfunc_end0-_Z30__device_stub__MatrixMulKernelPfS_S_i
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0x3e112e0be0000000              # double 9.9999997171806853E-10
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI1_1:
	.long	0x447a0000                      # float 1000
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$1592, %rsp                     # imm = 0x638
	.cfi_def_cfa_offset 1648
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$3, %edi
	jne	.LBB1_2
# %bb.1:
	movq	%rsi, %r14
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %r15d
	movq	16(%r14), %rdi
	callq	atoi
	movl	%eax, %ecx
	movl	%r15d, %eax
	cltd
	idivl	%ecx
	movl	%eax, (%rsp)
	movl	$.L.str, %edi
	movl	%r15d, %esi
	movl	%r15d, %edx
	movl	%ecx, 16(%rsp)                  # 4-byte Spill
	movl	%ecx, %r8d
	movl	%eax, 12(%rsp)                  # 4-byte Spill
	movl	%eax, %r9d
	jmp	.LBB1_3
.LBB1_2:
	movl	$32, (%rsp)
	movl	$512, %r15d                     # imm = 0x200
	movl	$16, 16(%rsp)                   # 4-byte Folded Spill
	movl	$32, 12(%rsp)                   # 4-byte Folded Spill
	movl	$.L.str.1, %edi
	movl	$512, %esi                      # imm = 0x200
	movl	$512, %edx                      # imm = 0x200
	movl	$16, %ecx
	movl	$16, %r8d
	movl	$32, %r9d
.LBB1_3:
	xorl	%eax, %eax
	callq	printf
	leaq	20(%rsp), %rdi
	movl	$0, (%rdi)
	callq	hipGetDevice
	testl	%eax, %eax
	je	.LBB1_5
# %bb.4:
	movl	$.L.str.2, %edi
	movl	%eax, %esi
	movl	$59, %edx
	xorl	%eax, %eax
	callq	printf
.LBB1_5:
	movl	20(%rsp), %esi
	leaq	120(%rsp), %rdi
	callq	hipGetDevicePropertiesR0600
	testl	%eax, %eax
	je	.LBB1_7
# %bb.6:
	movl	$.L.str.3, %edi
	movl	%eax, %esi
	movl	$64, %edx
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB1_8
.LBB1_7:
	movl	20(%rsp), %esi
	leaq	120(%rsp), %rdx
	movl	360(%rdx), %ecx
	movl	364(%rdx), %r8d
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
.LBB1_8:
	movl	%r15d, %eax
	imull	%eax, %eax
	movq	%rax, 72(%rsp)                  # 8-byte Spill
	leal	(,%rax,4), %eax
	movslq	%eax, %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r13
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r14
	movq	%rbx, 80(%rsp)                  # 8-byte Spill
	movq	%rbx, %rdi
	callq	malloc
	testq	%r13, %r13
	je	.LBB1_39
# %bb.9:
	testq	%r14, %r14
	je	.LBB1_39
# %bb.10:                               # %.preheader168
	movq	%rax, %r12
	movl	%r15d, %ebp
	testl	%r15d, %r15d
	jle	.LBB1_15
# %bb.11:                               # %.preheader167.lr.ph
	xorl	%eax, %eax
	xorl	%ebx, %ebx
	movl	%r15d, 56(%rsp)                 # 4-byte Spill
	movq	%r13, 104(%rsp)                 # 8-byte Spill
	movq	%r14, 96(%rsp)                  # 8-byte Spill
	movq	%r12, 88(%rsp)                  # 8-byte Spill
.LBB1_12:                               # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_13 Depth 2
	movq	%rax, 112(%rsp)                 # 8-byte Spill
	movl	%eax, %eax
	leaq	(%r12,%rax,4), %r12
	leaq	(%r14,%rax,4), %r14
	leaq	(,%rax,4), %r15
	addq	%r13, %r15
	xorl	%r13d, %r13d
.LBB1_13:                               #   Parent Loop BB1_12 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	cltq
	imulq	$1374389535, %rax, %rcx         # imm = 0x51EB851F
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$36, %rcx
	addl	%edx, %ecx
	imull	$50, %ecx, %ecx
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	%xmm0, (%r15,%r13,4)
	callq	rand
	cltq
	imulq	$1374389535, %rax, %rcx         # imm = 0x51EB851F
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$36, %rcx
	addl	%edx, %ecx
	imull	$50, %ecx, %ecx
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	%xmm0, (%r14,%r13,4)
	movl	$0, (%r12,%r13,4)
	incq	%r13
	cmpq	%r13, %rbp
	jne	.LBB1_13
# %bb.14:                               # %._crit_edge
                                        #   in Loop: Header=BB1_12 Depth=1
	incq	%rbx
	movl	56(%rsp), %r15d                 # 4-byte Reload
	movq	112(%rsp), %rax                 # 8-byte Reload
	addl	%r15d, %eax
	cmpq	%rbp, %rbx
	movq	104(%rsp), %r13                 # 8-byte Reload
	movq	96(%rsp), %r14                  # 8-byte Reload
	movq	88(%rsp), %r12                  # 8-byte Reload
	jne	.LBB1_12
.LBB1_15:                               # %._crit_edge171
	leaq	48(%rsp), %rdi
	movq	80(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB1_40
# %bb.16:
	leaq	40(%rsp), %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB1_41
# %bb.17:
	leaq	32(%rsp), %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB1_42
# %bb.18:
	movq	48(%rsp), %rdi
	movq	%r13, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB1_43
# %bb.19:
	movq	40(%rsp), %rdi
	movq	%r14, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB1_44
# %bb.20:
	leaq	64(%rsp), %rdi
	callq	hipEventCreate
	testl	%eax, %eax
	jne	.LBB1_45
# %bb.21:
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	testl	%eax, %eax
	jne	.LBB1_46
# %bb.22:
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	testl	%eax, %eax
	jne	.LBB1_47
# %bb.23:
	movl	16(%rsp), %eax                  # 4-byte Reload
	movl	12(%rsp), %ecx                  # 4-byte Reload
	movq	%rax, %rdx
	shlq	$32, %rdx
	movq	%rcx, %rdi
	shlq	$32, %rdi
	orq	%rax, %rdx
	orq	%rcx, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_25
# %bb.24:
	movq	48(%rsp), %rdi
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdx
	movl	%r15d, %ecx
	callq	_Z30__device_stub__MatrixMulKernelPfS_S_i
.LBB1_25:
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	testl	%eax, %eax
	jne	.LBB1_48
# %bb.26:
	movq	24(%rsp), %rdi
	callq	hipEventSynchronize
	testl	%eax, %eax
	jne	.LBB1_49
# %bb.27:
	leaq	60(%rsp), %rdi
	movl	$0, (%rdi)
	movq	64(%rsp), %rsi
	movq	24(%rsp), %rdx
	callq	hipEventElapsedTime
	testl	%eax, %eax
	jne	.LBB1_51
# %bb.28:
	movq	72(%rsp), %rsi                  # 8-byte Reload
	movl	%esi, %eax
	imull	%r15d, %eax
	addl	%eax, %eax
	cvtsi2sd	%eax, %xmm2
	movss	60(%rsp), %xmm3                 # xmm3 = mem[0],zero,zero,zero
	movsd	.LCPI1_0(%rip), %xmm0           # xmm0 = mem[0],zero
	mulsd	%xmm2, %xmm0
	cvtss2sd	%xmm3, %xmm1
	divss	.LCPI1_1(%rip), %xmm3
	cvtss2sd	%xmm3, %xmm3
	divsd	%xmm3, %xmm0
	movl	$.L.str.17, %edi
                                        # kill: def $esi killed $esi killed $rsi
	movb	$3, %al
	callq	printf
	movq	32(%rsp), %rsi
	movq	%r12, %rdi
	movq	%rbx, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB1_52
# %bb.29:
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	testl	%r15d, %r15d
	jle	.LBB1_37
# %bb.30:                               # %.preheader166.lr.ph
	leaq	(,%rbp,4), %rax
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.LBB1_31:                               # %.preheader.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_32 Depth 2
                                        #       Child Loop BB1_33 Depth 3
	movl	%ecx, %esi
	leaq	(,%rsi,4), %rsi
	addq	%r13, %rsi
	movq	%rdx, %rdi
	imulq	%rbp, %rdi
	leaq	(%r12,%rdi,4), %rdi
	movq	%r14, %r8
	xorl	%r9d, %r9d
.LBB1_32:                               # %.preheader
                                        #   Parent Loop BB1_31 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_33 Depth 3
	xorpd	%xmm0, %xmm0
	movq	%r8, %r10
	xorl	%r11d, %r11d
.LBB1_33:                               #   Parent Loop BB1_31 Depth=1
                                        #     Parent Loop BB1_32 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%rsi,%r11,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	mulss	(%r10), %xmm1
	addss	%xmm1, %xmm0
	incq	%r11
	addq	%rax, %r10
	cmpq	%r11, %rbp
	jne	.LBB1_33
# %bb.34:                               # %._crit_edge175
                                        #   in Loop: Header=BB1_32 Depth=2
	movss	(%rdi,%r9,4), %xmm1             # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm1, %xmm0
	jne	.LBB1_38
	jp	.LBB1_38
# %bb.35:                               #   in Loop: Header=BB1_32 Depth=2
	incq	%r9
	addq	$4, %r8
	cmpq	%rbp, %r9
	jne	.LBB1_32
# %bb.36:                               # %._crit_edge177
                                        #   in Loop: Header=BB1_31 Depth=1
	incq	%rdx
	addl	%r15d, %ecx
	cmpq	%rbp, %rdx
	jne	.LBB1_31
.LBB1_37:                               # %._crit_edge179
	movq	%r13, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r12, %rdi
	callq	free
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	callq	hipDeviceReset
	movl	$1, %eax
	addq	$1592, %rsp                     # imm = 0x638
	.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_38:
	.cfi_def_cfa_offset 1648
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	movl	$.L.str.20, %edi
	movb	$2, %al
	callq	printf
.LBB1_54:
	movl	$-1, %edi
	callq	exit
.LBB1_39:
	movl	$.Lstr, %edi
	jmp	.LBB1_53
.LBB1_40:
	movl	$.Lstr.11, %edi
	jmp	.LBB1_53
.LBB1_41:
	movl	$.Lstr.10, %edi
	jmp	.LBB1_53
.LBB1_42:
	movl	$.Lstr.9, %edi
	jmp	.LBB1_53
.LBB1_43:
	movl	$.Lstr.8, %edi
	jmp	.LBB1_53
.LBB1_44:
	movl	$.Lstr.7, %edi
	jmp	.LBB1_53
.LBB1_45:
	movl	$.Lstr.6, %edi
	jmp	.LBB1_53
.LBB1_46:
	movl	$.Lstr.5, %edi
	jmp	.LBB1_53
.LBB1_47:
	movl	$.Lstr.4, %edi
	jmp	.LBB1_53
.LBB1_48:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.14, %edi
	jmp	.LBB1_50
.LBB1_49:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.15, %edi
.LBB1_50:
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB1_54
.LBB1_51:
	movl	$.Lstr.3, %edi
	jmp	.LBB1_53
.LBB1_52:
	movl	$.Lstr.2, %edi
.LBB1_53:
	callq	puts@PLT
	jmp	.LBB1_54
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z15MatrixMulKernelPfS_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_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB3_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z15MatrixMulKernelPfS_S_i,@object # @_Z15MatrixMulKernelPfS_S_i
	.section	.rodata,"a",@progbits
	.globl	_Z15MatrixMulKernelPfS_S_i
	.p2align	3, 0x0
_Z15MatrixMulKernelPfS_S_i:
	.quad	_Z30__device_stub__MatrixMulKernelPfS_S_i
	.size	_Z15MatrixMulKernelPfS_S_i, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Using matrix dimension %dx%d ,Block Dim %dx%d threads per block, Grid Dim %dx%d blocks per grid\n"
	.size	.L.str, 97

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Using Default Parameters: matrix dimension %dx%d ,Block Dim %dx%d threads per block, Grid Dim %dx%d blocks per grid\n"
	.size	.L.str.1, 117

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"hipGetDevice returned error code %d, line(%d)\n"
	.size	.L.str.2, 47

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"hipGetDeviceProperties returned error code %d, line(%d)\n"
	.size	.L.str.3, 57

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"GPU Device %d: \"%s\" with compute capability %d.%d\n\n"
	.size	.L.str.4, 52

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"cuda event stop record failed with error=%s\n"
	.size	.L.str.14, 45

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"cuda event sync failed :%s\n"
	.size	.L.str.15, 28

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"Performance= %.2f GFlop/s, Time= %.3f msec, Size= %.0f Ops, WorkgroupSize= %u threads/block\n"
	.size	.L.str.17, 93

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"Matrix Mult Screwed Up!! differ in values CPU:%f and GPU:%f \n"
	.size	.L.str.20, 62

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z15MatrixMulKernelPfS_S_i"
	.size	.L__unnamed_1, 27

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Malloc failed"
	.size	.Lstr, 14

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Very slow Host Matrix Mult "
	.size	.Lstr.1, 28

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Device memoory copy back for Pd failed "
	.size	.Lstr.2, 40

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"cuda elapsed time calculation failed "
	.size	.Lstr.3, 38

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"cuda event start record failed "
	.size	.Lstr.4, 32

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"cuda event stop failed "
	.size	.Lstr.5, 24

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"cuda event start failed "
	.size	.Lstr.6, 25

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"Device memory copy for N failed "
	.size	.Lstr.7, 33

	.type	.Lstr.8,@object                 # @str.8
.Lstr.8:
	.asciz	"Device memory copy for M failed "
	.size	.Lstr.8, 33

	.type	.Lstr.9,@object                 # @str.9
.Lstr.9:
	.asciz	"Device memory allocation for P failed "
	.size	.Lstr.9, 39

	.type	.Lstr.10,@object                # @str.10
.Lstr.10:
	.asciz	"Device memory allocation for N failed "
	.size	.Lstr.10, 39

	.type	.Lstr.11,@object                # @str.11
.Lstr.11:
	.asciz	"Device memory allocation for M failed "
	.size	.Lstr.11, 39

	.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 _Z30__device_stub__MatrixMulKernelPfS_S_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z15MatrixMulKernelPfS_S_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

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

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .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:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z15MatrixMulKernelPfS_S_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z15MatrixMulKernelPfS_S_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     9
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
