/*
Author: Vedanta Pawar
NetID: vp273
Class: M.Eng ECE, Cornell University
Email: vp273@cornell.edu

Instructions for Compiling and Executing Code:
Compile: /usr/local/cuda-10.1/bin/nvcc -o vp273_hw5_2.out vp273_hw5_2.cu
Run: ./vp273_hw5_2.out "Enter the dimension of the matrix:" "Enter the Block Size:"
Example: ./vp273_hw5_2.out 4096 8
*/

#include <stdio.h> 	// For printf() 
#include <stdint.h>	// For uint64
#include <stdlib.h> // For srand48() and drand48()
#include <time.h>   // For clock_gettime()

#define BILLION 1000000000L	  // To convert clock time in floating point seconds to nanoseconds/

__global__ void matrixMul( double** dev_matA , double** dev_matB , double** dev_matC , int ndim , int tile_size )
{
	extern __shared__ double A_B_shared[];
	double *A_tile = &A_B_shared[ 0 ] ;
	double *B_tile = &A_B_shared[ tile_size * tile_size * sizeof( double ) ] ;

	double partial = 0.0;
	int bx = blockIdx.x; int by = blockIdx.y;
	int tx = threadIdx.x; int ty = threadIdx.y;
	int row = by * blockDim.y + ty;
	int col = bx * blockDim.x + tx;
	/* transpose B while loading to shared memory */
	for (int m = 0; m < ndim / blockDim.x ; ++m) 
	{
		A_tile[ ty * tile_size + tx ] = dev_matA[row][m*blockDim.x + tx]; /* load coalesced */
		B_tile[ ty * tile_size + tx ] = dev_matB[( m * blockDim.y + ty )][col]; /* not load coalesced */
		__syncthreads();
		for (int k = 0; k < blockDim.x; ++k)
		{
			partial += A_tile[ ty *tile_size + k ] * B_tile[ k *tile_size + tx ]; /*Bank conflicts */
		}
		__syncthreads();
		dev_matC[row][col] = partial; 
	}
}



int main( int argc, char *argv[] )
{
	uint64_t diff; 				// Stores the time in nanoseconds
	struct timespec start, end; // Used to implement the high resolution timer included in <time.h>
	int ndim = atoi( argv[1] );	// Ask user and store the dimension of the square matrix
	FILE *ptr_file ;            // File pointer used to point to a .csv file for storing the time vs ndim
	int block_size ;
	/*
	Create matrices on heap by malloc and storing and assigning pointers to
	the individual matrices as matA, matB, matC.
	Each matrix is a linear array of size ndim x ndim
	Total memory malloced is 3 x ndim^2
	 */
	double *matA[ndim] ;
	double *matB[ndim] ;
	double *matC[ndim] ;
	for ( int i = 0 ; i < ndim ; i ++ )
	{
		matA[i] = ( double * )malloc( ndim * sizeof( double ) );
		matB[i] = ( double * )malloc( ndim * sizeof( double ) );
		matC[i] = ( double * )malloc( ndim * sizeof( double ) );
	}

	clock_gettime( CLOCK_MONOTONIC , &start );
	// Iterate through the rows of the Matrix A and B
	for ( int i = 0 ; i < ndim ; i++ )
	{
		// Iterate through the columns of the Matrix A and B
		for ( int j = 0 ; j < ndim ; j++ )
		{
			clock_gettime( CLOCK_MONOTONIC , &end );	// End clock timer.
			diff = BILLION * ( end.tv_sec - start.tv_sec ) + end.tv_nsec - start.tv_nsec;
			srand48( diff ) ; // Set random seed to for initializing drand48() later
			// Store same random numbers in A and B
			matA[i][j] = drand48() ;
			matB[i][j] = drand48() ;
		}
	}

	double *dev_matA[ndim] , *dev_matB[ndim] , *dev_matC[ndim];
	for ( int i = 0 ; i < ndim ; i++ )
	{
		cudaMalloc( ( void** )&dev_matA[i], ndim * sizeof( double ) );
		cudaMalloc( ( void** )&dev_matB[i], ndim * sizeof( double ) );
		cudaMalloc( ( void** )&dev_matC[i], ndim * sizeof( double ) );
	}	

	// Start high resolution clock timer
	clock_gettime( CLOCK_MONOTONIC , &start );
	
	for ( int i = 0 ; i < ndim ; i++ )
	{
		cudaMemcpy( dev_matA[i] , matA[i] , ndim * sizeof( double ) , cudaMemcpyHostToDevice );
		cudaMemcpy( dev_matB[i] , matB[i] , ndim * sizeof( double ) , cudaMemcpyHostToDevice );
	}
	

	block_size = atoi( argv[2] ) ;
	int shared_mem_size =  2 * block_size * block_size * sizeof( double ) ;
	dim3 Block( block_size , block_size) ;
	dim3 Grid( ndim / Block.x , ndim / Block.y) ;
	matrixMul<<< Grid, Block , shared_mem_size >>>( dev_matA , dev_matB , dev_matC , ndim , block_size );
	cudaMemcpy( matC , dev_matC , ndim * ndim * sizeof( double ) , cudaMemcpyDeviceToHost );	

	cudaDeviceSynchronize( );

	clock_gettime( CLOCK_MONOTONIC , &end );	// End clock timer.
	//Calculate the difference in timer and convert it to nanosecond by multiplying by 10^9
	diff = BILLION * ( end.tv_sec - start.tv_sec ) + end.tv_nsec - start.tv_nsec;
	printf( "elapsed time = %llu nanoseconds\n", ( long long unsigned int ) diff );

	ptr_file = fopen("output_hw5_2.csv", "a");  // Save the time and corresponding matrix dim. in a csv file
	// Store the time and corresponding matrix dimension in a csv file
	fprintf( ptr_file ,"%d , %d , %llu\n", ndim , block_size ,  ( long long unsigned int ) diff );

	//Deallocate the memory allocated to matrices A, B and C
	for ( int i = 0 ; i < ndim ; i++ ) 
	{ 
		free ( matA[i] ) ;
		free ( matB[i] ) ;
		free ( matC[i] ) ;
		
	}
	cudaFree( dev_matA ) ;
	cudaFree( dev_matB ) ;
	cudaFree( dev_matC ) ;
	
	exit( 0 ) ;
}
	.file	"tmpxft_002b7f06_00000000-6_vp273_hw5_2.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	_Z37__device_stub__Z9matrixMulPPdS0_S0_iiPPdS0_S0_ii
	.type	_Z37__device_stub__Z9matrixMulPPdS0_S0_iiPPdS0_S0_ii, @function
_Z37__device_stub__Z9matrixMulPPdS0_S0_iiPPdS0_S0_ii:
.LFB2052:
	.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	.L2
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z9matrixMulPPdS0_S0_ii(%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
.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	_Z37__device_stub__Z9matrixMulPPdS0_S0_iiPPdS0_S0_ii, .-_Z37__device_stub__Z9matrixMulPPdS0_S0_iiPPdS0_S0_ii
	.globl	_Z9matrixMulPPdS0_S0_ii
	.type	_Z9matrixMulPPdS0_S0_ii, @function
_Z9matrixMulPPdS0_S0_ii:
.LFB2053:
	.cfi_startproc
	endbr64
	jmp	_Z37__device_stub__Z9matrixMulPPdS0_S0_iiPPdS0_S0_ii
	.cfi_endproc
.LFE2053:
	.size	_Z9matrixMulPPdS0_S0_ii, .-_Z9matrixMulPPdS0_S0_ii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"elapsed time = %llu nanoseconds\n"
.LC1:
	.string	"a"
.LC2:
	.string	"output_hw5_2.csv"
.LC3:
	.string	"%d , %d , %llu\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$136, %rsp
	.cfi_offset 15, -24
	.cfi_offset 14, -32
	.cfi_offset 13, -40
	.cfi_offset 12, -48
	.cfi_offset 3, -56
	movq	%rsi, -152(%rbp)
	movq	%fs:40, %rax
	movq	%rax, -56(%rbp)
	movq	%rsi, %rax
	movq	8(%rax), %rdi
	call	atoi@PLT
	movq	%rsp, %rcx
	movl	%eax, %ebx
	cltq
	salq	$3, %rax
	leaq	15(%rax), %rdx
	movq	%rax, -120(%rbp)
	movq	%rdx, %rsi
	movq	%rdx, %rax
	andq	$-4096, %rsi
	andq	$-16, %rax
	subq	%rsi, %rcx
.L9:
	cmpq	%rcx, %rsp
	je	.L10
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L9
.L10:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L11
	orq	$0, -8(%rsp,%rax)
.L11:
	movq	%rdx, %rsi
	movq	%rdx, %rax
	movq	%rsp, %rcx
	movq	%rsp, %r13
	andq	$-4096, %rsi
	andq	$-16, %rax
	subq	%rsi, %rcx
.L12:
	cmpq	%rcx, %rsp
	je	.L13
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L12
.L13:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L14
	orq	$0, -8(%rsp,%rax)
.L14:
	movq	%rdx, %rsi
	movq	%rdx, %rax
	movq	%rsp, %rcx
	movq	%rsp, %r14
	andq	$-4096, %rsi
	andq	$-16, %rax
	subq	%rsi, %rcx
.L15:
	cmpq	%rcx, %rsp
	je	.L16
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L15
.L16:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L17
	orq	$0, -8(%rsp,%rax)
.L17:
	movq	%rsp, -144(%rbp)
	xorl	%r12d, %r12d
.L18:
	movq	%rdx, -128(%rbp)
	cmpl	%r12d, %ebx
	jle	.L60
	movq	-120(%rbp), %rdi
	call	malloc@PLT
	movq	-120(%rbp), %rdi
	movq	%rax, 0(%r13,%r12,8)
	call	malloc@PLT
	movq	-120(%rbp), %rdi
	movq	%rax, (%r14,%r12,8)
	call	malloc@PLT
	movq	-144(%rbp), %rcx
	movq	-128(%rbp), %rdx
	movq	%rax, (%rcx,%r12,8)
	incq	%r12
	jmp	.L18
.L60:
	leaq	-88(%rbp), %rax
	movl	$1, %edi
	xorl	%r12d, %r12d
	movq	%rax, %rsi
	movq	%rax, -160(%rbp)
	call	clock_gettime@PLT
	leaq	-72(%rbp), %rax
	movq	-128(%rbp), %rdx
	movq	%rax, -136(%rbp)
.L20:
	cmpl	%r12d, %ebx
	jle	.L61
	xorl	%r15d, %r15d
.L21:
	movq	-136(%rbp), %rsi
	movl	$1, %edi
	movq	%rdx, -176(%rbp)
	call	clock_gettime@PLT
	movq	-72(%rbp), %rdi
	subq	-88(%rbp), %rdi
	imulq	$1000000000, %rdi, %rdi
	addq	-64(%rbp), %rdi
	subq	-80(%rbp), %rdi
	call	srand48@PLT
	leaq	0(,%r15,8), %rax
	incq	%r15
	movq	%rax, %rcx
	addq	0(%r13,%r12,8), %rcx
	movq	%rax, -168(%rbp)
	movq	%rcx, -128(%rbp)
	call	drand48@PLT
	movq	-128(%rbp), %rax
	movsd	%xmm0, (%rax)
	movq	-168(%rbp), %rax
	addq	(%r14,%r12,8), %rax
	movq	%rax, -128(%rbp)
	call	drand48@PLT
	movq	-128(%rbp), %rax
	cmpl	%r15d, %ebx
	movq	-176(%rbp), %rdx
	movsd	%xmm0, (%rax)
	jg	.L21
	incq	%r12
	jmp	.L20
.L61:
	movq	%rdx, %rsi
	movq	%rdx, %rax
	movq	%rsp, %rcx
	andq	$-4096, %rsi
	andq	$-16, %rax
	subq	%rsi, %rcx
.L22:
	cmpq	%rcx, %rsp
	je	.L23
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L22
.L23:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L24
	orq	$0, -8(%rsp,%rax)
.L24:
	movq	%rdx, %rsi
	movq	%rdx, %rax
	movq	%rsp, %rcx
	movq	%rsp, %r15
	andq	$-4096, %rsi
	andq	$-16, %rax
	subq	%rsi, %rcx
.L25:
	cmpq	%rcx, %rsp
	je	.L26
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L25
.L26:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L27
	orq	$0, -8(%rsp,%rax)
.L27:
	movq	%rsp, -128(%rbp)
	movq	%rdx, %rax
	movq	%rsp, %rcx
	andq	$-4096, %rdx
	andq	$-16, %rax
	subq	%rdx, %rcx
.L28:
	cmpq	%rcx, %rsp
	je	.L29
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L28
.L29:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L30
	orq	$0, -8(%rsp,%rax)
.L30:
	movq	%rsp, -136(%rbp)
	xorl	%r12d, %r12d
	xorl	%edx, %edx
.L31:
	cmpl	%ebx, %edx
	jge	.L62
	movq	-120(%rbp), %rsi
	leaq	(%r15,%r12), %rdi
	movl	%edx, -168(%rbp)
	call	cudaMalloc@PLT
	movq	-128(%rbp), %rax
	movq	-120(%rbp), %rsi
	leaq	(%rax,%r12), %rdi
	call	cudaMalloc@PLT
	movq	-136(%rbp), %rax
	movq	-120(%rbp), %rsi
	leaq	(%rax,%r12), %rdi
	addq	$8, %r12
	call	cudaMalloc@PLT
	movl	-168(%rbp), %edx
	incl	%edx
	jmp	.L31
.L62:
	movq	-160(%rbp), %rsi
	movl	$1, %edi
	xorl	%r12d, %r12d
	call	clock_gettime@PLT
.L33:
	cmpl	%r12d, %ebx
	jle	.L63
	movq	0(%r13,%r12,8), %rsi
	movq	(%r15,%r12,8), %rdi
	movl	$1, %ecx
	movq	-120(%rbp), %rdx
	call	cudaMemcpy@PLT
	movq	-128(%rbp), %rax
	movq	(%r14,%r12,8), %rsi
	movl	$1, %ecx
	movq	-120(%rbp), %rdx
	movq	(%rax,%r12,8), %rdi
	incq	%r12
	call	cudaMemcpy@PLT
	jmp	.L33
.L63:
	movq	-152(%rbp), %rax
	movq	16(%rax), %rdi
	call	atoi@PLT
	xorl	%edx, %edx
	xorl	%r9d, %r9d
	movl	$1, %ecx
	movl	%eax, %r12d
	movl	%ebx, %eax
	movl	$1, %esi
	movl	$1, -104(%rbp)
	divl	%r12d
	movl	%r12d, -112(%rbp)
	movl	%r12d, -108(%rbp)
	movq	-112(%rbp), %rdx
	movl	$1, -92(%rbp)
	movl	%r12d, %r8d
	imull	%r12d, %r8d
	sall	$4, %r8d
	movslq	%r8d, %r8
	movl	%eax, -100(%rbp)
	movl	%eax, -96(%rbp)
	movq	-100(%rbp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L35
	movq	-136(%rbp), %rdx
	movq	-128(%rbp), %rsi
	movl	%r12d, %r8d
	movl	%ebx, %ecx
	movq	%r15, %rdi
	call	_Z37__device_stub__Z9matrixMulPPdS0_S0_iiPPdS0_S0_ii
.L35:
	movl	%ebx, %edx
	movq	-136(%rbp), %rsi
	movl	$2, %ecx
	movq	-144(%rbp), %rdi
	imull	%ebx, %edx
	movslq	%edx, %rdx
	salq	$3, %rdx
	call	cudaMemcpy@PLT
	call	cudaDeviceSynchronize@PLT
	leaq	-72(%rbp), %rsi
	movl	$1, %edi
	call	clock_gettime@PLT
	movq	-72(%rbp), %r9
	subq	-88(%rbp), %r9
	xorl	%eax, %eax
	imulq	$1000000000, %r9, %r9
	addq	-64(%rbp), %r9
	subq	-80(%rbp), %r9
	movl	$2, %edi
	movq	%r9, %rdx
	leaq	.LC0(%rip), %rsi
	movq	%r9, -120(%rbp)
	call	__printf_chk@PLT
	leaq	.LC1(%rip), %rsi
	leaq	.LC2(%rip), %rdi
	call	fopen@PLT
	movq	-120(%rbp), %r9
	movl	%r12d, %r8d
	movl	%ebx, %ecx
	movq	%rax, %rdi
	leaq	.LC3(%rip), %rdx
	xorl	%eax, %eax
	xorl	%r12d, %r12d
	movl	$2, %esi
	call	__fprintf_chk@PLT
.L36:
	cmpl	%r12d, %ebx
	jle	.L64
	movq	0(%r13,%r12,8), %rdi
	call	free@PLT
	movq	(%r14,%r12,8), %rdi
	call	free@PLT
	movq	-144(%rbp), %rax
	movq	(%rax,%r12,8), %rdi
	incq	%r12
	call	free@PLT
	jmp	.L36
.L64:
	movq	%r15, %rdi
	call	cudaFree@PLT
	movq	-128(%rbp), %rdi
	call	cudaFree@PLT
	movq	-136(%rbp), %rdi
	call	cudaFree@PLT
	xorl	%edi, %edi
	call	exit@PLT
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC4:
	.string	"_Z9matrixMulPPdS0_S0_ii"
	.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	.LC4(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z9matrixMulPPdS0_S0_ii(%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
	.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:

/*
Author: Vedanta Pawar
NetID: vp273
Class: M.Eng ECE, Cornell University
Email: vp273@cornell.edu

Instructions for Compiling and Executing Code:
Compile: /usr/local/cuda-10.1/bin/nvcc -o vp273_hw5_2.out vp273_hw5_2.cu
Run: ./vp273_hw5_2.out "Enter the dimension of the matrix:" "Enter the Block Size:"
Example: ./vp273_hw5_2.out 4096 8
*/


#include <hip/hip_runtime.h>
#include <stdio.h> 	// For printf() 
#include <stdint.h>	// For uint64
#include <stdlib.h> // For srand48() and drand48()
#include <time.h>   // For clock_gettime()

#define BILLION 1000000000L	  // To convert clock time in floating point seconds to nanoseconds/

__global__ void matrixMul( double** dev_matA , double** dev_matB , double** dev_matC , int ndim , int tile_size )
{
	extern __shared__ double A_B_shared[];
	double *A_tile = &A_B_shared[ 0 ] ;
	double *B_tile = &A_B_shared[ tile_size * tile_size * sizeof( double ) ] ;

	double partial = 0.0;
	int bx = blockIdx.x; int by = blockIdx.y;
	int tx = threadIdx.x; int ty = threadIdx.y;
	int row = by * blockDim.y + ty;
	int col = bx * blockDim.x + tx;
	/* transpose B while loading to shared memory */
	for (int m = 0; m < ndim / blockDim.x ; ++m) 
	{
		A_tile[ ty * tile_size + tx ] = dev_matA[row][m*blockDim.x + tx]; /* load coalesced */
		B_tile[ ty * tile_size + tx ] = dev_matB[( m * blockDim.y + ty )][col]; /* not load coalesced */
		__syncthreads();
		for (int k = 0; k < blockDim.x; ++k)
		{
			partial += A_tile[ ty *tile_size + k ] * B_tile[ k *tile_size + tx ]; /*Bank conflicts */
		}
		__syncthreads();
		dev_matC[row][col] = partial; 
	}
}



int main( int argc, char *argv[] )
{
	uint64_t diff; 				// Stores the time in nanoseconds
	struct timespec start, end; // Used to implement the high resolution timer included in <time.h>
	int ndim = atoi( argv[1] );	// Ask user and store the dimension of the square matrix
	FILE *ptr_file ;            // File pointer used to point to a .csv file for storing the time vs ndim
	int block_size ;
	/*
	Create matrices on heap by malloc and storing and assigning pointers to
	the individual matrices as matA, matB, matC.
	Each matrix is a linear array of size ndim x ndim
	Total memory malloced is 3 x ndim^2
	 */
	double *matA[ndim] ;
	double *matB[ndim] ;
	double *matC[ndim] ;
	for ( int i = 0 ; i < ndim ; i ++ )
	{
		matA[i] = ( double * )malloc( ndim * sizeof( double ) );
		matB[i] = ( double * )malloc( ndim * sizeof( double ) );
		matC[i] = ( double * )malloc( ndim * sizeof( double ) );
	}

	clock_gettime( CLOCK_MONOTONIC , &start );
	// Iterate through the rows of the Matrix A and B
	for ( int i = 0 ; i < ndim ; i++ )
	{
		// Iterate through the columns of the Matrix A and B
		for ( int j = 0 ; j < ndim ; j++ )
		{
			clock_gettime( CLOCK_MONOTONIC , &end );	// End clock timer.
			diff = BILLION * ( end.tv_sec - start.tv_sec ) + end.tv_nsec - start.tv_nsec;
			srand48( diff ) ; // Set random seed to for initializing drand48() later
			// Store same random numbers in A and B
			matA[i][j] = drand48() ;
			matB[i][j] = drand48() ;
		}
	}

	double *dev_matA[ndim] , *dev_matB[ndim] , *dev_matC[ndim];
	for ( int i = 0 ; i < ndim ; i++ )
	{
		hipMalloc( ( void** )&dev_matA[i], ndim * sizeof( double ) );
		hipMalloc( ( void** )&dev_matB[i], ndim * sizeof( double ) );
		hipMalloc( ( void** )&dev_matC[i], ndim * sizeof( double ) );
	}	

	// Start high resolution clock timer
	clock_gettime( CLOCK_MONOTONIC , &start );
	
	for ( int i = 0 ; i < ndim ; i++ )
	{
		hipMemcpy( dev_matA[i] , matA[i] , ndim * sizeof( double ) , hipMemcpyHostToDevice );
		hipMemcpy( dev_matB[i] , matB[i] , ndim * sizeof( double ) , hipMemcpyHostToDevice );
	}
	

	block_size = atoi( argv[2] ) ;
	int shared_mem_size =  2 * block_size * block_size * sizeof( double ) ;
	dim3 Block( block_size , block_size) ;
	dim3 Grid( ndim / Block.x , ndim / Block.y) ;
	matrixMul<<< Grid, Block , shared_mem_size >>>( dev_matA , dev_matB , dev_matC , ndim , block_size );
	hipMemcpy( matC , dev_matC , ndim * ndim * sizeof( double ) , hipMemcpyDeviceToHost );	

	hipDeviceSynchronize( );

	clock_gettime( CLOCK_MONOTONIC , &end );	// End clock timer.
	//Calculate the difference in timer and convert it to nanosecond by multiplying by 10^9
	diff = BILLION * ( end.tv_sec - start.tv_sec ) + end.tv_nsec - start.tv_nsec;
	printf( "elapsed time = %llu nanoseconds\n", ( long long unsigned int ) diff );

	ptr_file = fopen("output_hw5_2.csv", "a");  // Save the time and corresponding matrix dim. in a csv file
	// Store the time and corresponding matrix dimension in a csv file
	fprintf( ptr_file ,"%d , %d , %llu\n", ndim , block_size ,  ( long long unsigned int ) diff );

	//Deallocate the memory allocated to matrices A, B and C
	for ( int i = 0 ; i < ndim ; i++ ) 
	{ 
		free ( matA[i] ) ;
		free ( matB[i] ) ;
		free ( matC[i] ) ;
		
	}
	hipFree( dev_matA ) ;
	hipFree( dev_matB ) ;
	hipFree( dev_matC ) ;
	
	exit( 0 ) ;
}
	.text
	.file	"vp273_hw5_2.hip"
	.globl	_Z24__device_stub__matrixMulPPdS0_S0_ii # -- Begin function _Z24__device_stub__matrixMulPPdS0_S0_ii
	.type	_Z24__device_stub__matrixMulPPdS0_S0_ii,@function
_Z24__device_stub__matrixMulPPdS0_S0_ii: # @_Z24__device_stub__matrixMulPPdS0_S0_ii
	.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	$_Z9matrixMulPPdS0_S0_ii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z24__device_stub__matrixMulPPdS0_S0_ii, .Lfunc_end0-_Z24__device_stub__matrixMulPPdS0_S0_ii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$104, %rsp
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	movq	%rsi, %r15
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %ebx
	movq	%rsp, %r13
	leaq	15(,%rbx,8), %rcx
	andq	$-16, %rcx
	subq	%rcx, %r13
	movq	%r13, %rsp
	movq	%rsp, %r14
	subq	%rcx, %r14
	movq	%r14, %rsp
	movq	%rsp, %r12
	movq	%rcx, -56(%rbp)                 # 8-byte Spill
	subq	%rcx, %r12
	movq	%r12, %rsp
	testl	%eax, %eax
	movq	%r12, -128(%rbp)                # 8-byte Spill
	jle	.LBB1_1
# %bb.17:                               # %.lr.ph
	movq	%r15, -88(%rbp)                 # 8-byte Spill
	movq	%rbx, %rax
	movq	%r14, -48(%rbp)                 # 8-byte Spill
	movq	%r13, %r14
	leaq	(,%rbx,8), %r13
	xorl	%r15d, %r15d
.LBB1_18:                               # =>This Inner Loop Header: Depth=1
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, (%r14,%r15,8)
	movq	%r13, %rdi
	callq	malloc
	movq	-48(%rbp), %rcx                 # 8-byte Reload
	movq	%rax, (%rcx,%r15,8)
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, (%r12,%r15,8)
	incq	%r15
	cmpq	%r15, %rbx
	jne	.LBB1_18
# %bb.10:                               # %._crit_edge
	movq	%r13, -80(%rbp)                 # 8-byte Spill
	leaq	-120(%rbp), %rsi
	movl	$1, %edi
	callq	clock_gettime
	testl	%ebx, %ebx
	movq	-88(%rbp), %r15                 # 8-byte Reload
	movq	%r14, %r13
	movq	-48(%rbp), %r14                 # 8-byte Reload
	jle	.LBB1_2
# %bb.11:                               # %.preheader.lr.ph
	movq	%r13, -104(%rbp)                # 8-byte Spill
	xorl	%r15d, %r15d
	leaq	-144(%rbp), %r12
	movq	%rbx, -96(%rbp)                 # 8-byte Spill
	movq	-104(%rbp), %r14                # 8-byte Reload
.LBB1_12:                               # %.lr.ph84
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_13 Depth 2
	xorl	%r13d, %r13d
	movq	-48(%rbp), %rbx                 # 8-byte Reload
.LBB1_13:                               #   Parent Loop BB1_12 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	$1, %edi
	movq	%r12, %rsi
	callq	clock_gettime
	movq	-144(%rbp), %rax
	subq	-120(%rbp), %rax
	imulq	$1000000000, %rax, %rdi         # imm = 0x3B9ACA00
	addq	-136(%rbp), %rdi
	subq	-112(%rbp), %rdi
	callq	srand48
	callq	drand48
	movq	(%r14,%r15,8), %rax
	movsd	%xmm0, (%rax,%r13,8)
	callq	drand48
	movq	(%rbx,%r15,8), %rax
	movsd	%xmm0, (%rax,%r13,8)
	incq	%r13
	cmpq	%r13, -96(%rbp)                 # 8-byte Folded Reload
	jne	.LBB1_13
# %bb.14:                               # %._crit_edge85
                                        #   in Loop: Header=BB1_12 Depth=1
	incq	%r15
	movq	-96(%rbp), %rbx                 # 8-byte Reload
	cmpq	%rbx, %r15
	jne	.LBB1_12
# %bb.15:                               # %._crit_edge87
	movq	%rsp, %rax
	movq	-56(%rbp), %rcx                 # 8-byte Reload
	subq	%rcx, %rax
	movq	%rax, -64(%rbp)                 # 8-byte Spill
	movq	%rax, %rsp
	movq	%rsp, %r12
	subq	%rcx, %r12
	movq	%r12, %rsp
	movq	%rsp, %rax
	subq	%rcx, %rax
	movq	%rax, -56(%rbp)                 # 8-byte Spill
	movq	%rax, %rsp
	testl	%ebx, %ebx
	movq	%r12, -72(%rbp)                 # 8-byte Spill
	jle	.LBB1_16
# %bb.21:                               # %.lr.ph90
	xorl	%r15d, %r15d
	movq	-104(%rbp), %r13                # 8-byte Reload
	movq	-48(%rbp), %r14                 # 8-byte Reload
	movq	-56(%rbp), %r12                 # 8-byte Reload
.LBB1_22:                               # =>This Inner Loop Header: Depth=1
	movq	-64(%rbp), %rax                 # 8-byte Reload
	leaq	(%rax,%r15), %rdi
	movq	-80(%rbp), %rsi                 # 8-byte Reload
	callq	hipMalloc
	movq	-72(%rbp), %rax                 # 8-byte Reload
	leaq	(%rax,%r15), %rdi
	movq	-80(%rbp), %rsi                 # 8-byte Reload
	callq	hipMalloc
	leaq	(%r12,%r15), %rdi
	movq	-80(%rbp), %rsi                 # 8-byte Reload
	callq	hipMalloc
	addq	$8, %r15
	cmpq	%r15, -80(%rbp)                 # 8-byte Folded Reload
	jne	.LBB1_22
# %bb.19:                               # %._crit_edge91
	leaq	-120(%rbp), %rsi
	movl	$1, %edi
	callq	clock_gettime
	testl	%ebx, %ebx
	jle	.LBB1_20
# %bb.23:                               # %.lr.ph94
	xorl	%r15d, %r15d
	movq	-72(%rbp), %r12                 # 8-byte Reload
	movq	%r14, %rbx
	movq	%r13, %r14
	movq	-80(%rbp), %r13                 # 8-byte Reload
.LBB1_24:                               # =>This Inner Loop Header: Depth=1
	movq	-64(%rbp), %rax                 # 8-byte Reload
	movq	(%rax,%r15,8), %rdi
	movq	(%r14,%r15,8), %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r12,%r15,8), %rdi
	movq	(%rbx,%r15,8), %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	incq	%r15
	cmpq	%r15, -96(%rbp)                 # 8-byte Folded Reload
	jne	.LBB1_24
# %bb.25:
	movq	-88(%rbp), %r15                 # 8-byte Reload
	movq	-64(%rbp), %r12                 # 8-byte Reload
	movq	%r14, %r13
	movq	%rbx, %r14
	movq	-96(%rbp), %rbx                 # 8-byte Reload
	jmp	.LBB1_4
.LBB1_1:                                # %._crit_edge.thread
	leaq	-120(%rbp), %rsi
	movl	$1, %edi
	callq	clock_gettime
.LBB1_2:                                # %._crit_edge87.thread
	movq	%rsp, %r12
	movq	-56(%rbp), %rcx                 # 8-byte Reload
	subq	%rcx, %r12
	movq	%r12, %rsp
	movq	%rsp, %rax
	subq	%rcx, %rax
	movq	%rax, -72(%rbp)                 # 8-byte Spill
	movq	%rax, %rsp
	movq	%rsp, %rax
	subq	%rcx, %rax
	movq	%rax, -56(%rbp)                 # 8-byte Spill
	movq	%rax, %rsp
.LBB1_3:                                # %._crit_edge91.thread
	leaq	-120(%rbp), %rsi
	movl	$1, %edi
	callq	clock_gettime
.LBB1_4:                                # %._crit_edge95
	movq	16(%r15), %rdi
	callq	atoi
	movl	%eax, %edi
	movl	%eax, %esi
	imull	%eax, %esi
	shll	$4, %esi
	movl	%eax, %eax
	movq	%rax, %rcx
	shlq	$32, %rcx
	orq	%rax, %rcx
	movl	%ebx, %eax
	xorl	%edx, %edx
	movl	%edi, -48(%rbp)                 # 4-byte Spill
	divl	%edi
                                        # kill: def $eax killed $eax def $rax
	movq	%rax, %rdi
	shlq	$32, %rdi
	orq	%rax, %rdi
	movslq	%esi, %r8
	movl	$1, %esi
	movq	%rcx, %rdx
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	movq	-56(%rbp), %r15                 # 8-byte Reload
	jne	.LBB1_6
# %bb.5:
	movq	%r12, %rdi
	movq	-72(%rbp), %rsi                 # 8-byte Reload
	movq	%r15, %rdx
	movl	%ebx, %ecx
	movl	-48(%rbp), %r8d                 # 4-byte Reload
	callq	_Z24__device_stub__matrixMulPPdS0_S0_ii
.LBB1_6:
	movq	%r12, -64(%rbp)                 # 8-byte Spill
	movl	%ebx, %edx
	imull	%edx, %edx
	shlq	$3, %rdx
	movq	-128(%rbp), %r12                # 8-byte Reload
	movq	%r12, %rdi
	movq	%r15, %rsi
	movl	$2, %ecx
	callq	hipMemcpy
	callq	hipDeviceSynchronize
	leaq	-144(%rbp), %r15
	movl	$1, %edi
	movq	%r15, %rsi
	callq	clock_gettime
	movq	(%r15), %rax
	movq	%r15, %rcx
	subq	-120(%rbp), %rax
	imulq	$1000000000, %rax, %r15         # imm = 0x3B9ACA00
	addq	8(%rcx), %r15
	subq	-112(%rbp), %r15
	movl	$.L.str, %edi
	movq	%r15, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.1, %edi
	movl	$.L.str.2, %esi
	callq	fopen
	movl	$.L.str.3, %esi
	movq	%rax, %rdi
	movl	%ebx, %edx
	movl	-48(%rbp), %ecx                 # 4-byte Reload
	movq	%r15, %r8
	xorl	%eax, %eax
	callq	fprintf
	testl	%ebx, %ebx
	jle	.LBB1_9
# %bb.7:                                # %.lr.ph98.preheader
	xorl	%r15d, %r15d
.LBB1_8:                                # %.lr.ph98
                                        # =>This Inner Loop Header: Depth=1
	movq	(%r13,%r15,8), %rdi
	callq	free
	movq	(%r14,%r15,8), %rdi
	callq	free
	movq	(%r12,%r15,8), %rdi
	callq	free
	incq	%r15
	cmpq	%r15, %rbx
	jne	.LBB1_8
.LBB1_9:                                # %._crit_edge99
	movq	-64(%rbp), %rdi                 # 8-byte Reload
	callq	hipFree
	movq	-72(%rbp), %rdi                 # 8-byte Reload
	callq	hipFree
	movq	-56(%rbp), %rdi                 # 8-byte Reload
	callq	hipFree
	xorl	%edi, %edi
	callq	exit
.LBB1_16:
	movq	-104(%rbp), %r13                # 8-byte Reload
	movq	-48(%rbp), %r14                 # 8-byte Reload
	movq	-88(%rbp), %r15                 # 8-byte Reload
	movq	-64(%rbp), %r12                 # 8-byte Reload
	jmp	.LBB1_3
.LBB1_20:
	movq	-88(%rbp), %r15                 # 8-byte Reload
	movq	-64(%rbp), %r12                 # 8-byte Reload
	jmp	.LBB1_4
.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	$_Z9matrixMulPPdS0_S0_ii, %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	_Z9matrixMulPPdS0_S0_ii,@object # @_Z9matrixMulPPdS0_S0_ii
	.section	.rodata,"a",@progbits
	.globl	_Z9matrixMulPPdS0_S0_ii
	.p2align	3, 0x0
_Z9matrixMulPPdS0_S0_ii:
	.quad	_Z24__device_stub__matrixMulPPdS0_S0_ii
	.size	_Z9matrixMulPPdS0_S0_ii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"elapsed time = %llu nanoseconds\n"
	.size	.L.str, 33

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"output_hw5_2.csv"
	.size	.L.str.1, 17

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

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"%d , %d , %llu\n"
	.size	.L.str.3, 16

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

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

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

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z24__device_stub__matrixMulPPdS0_S0_ii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z9matrixMulPPdS0_S0_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z9matrixMulPPdS0_S0_ii ; -- Begin function _Z9matrixMulPPdS0_S0_ii
	.globl	_Z9matrixMulPPdS0_S0_ii
	.p2align	8
	.type	_Z9matrixMulPPdS0_S0_ii,@function
_Z9matrixMulPPdS0_S0_ii:                ; @_Z9matrixMulPPdS0_S0_ii
; %bb.0:
	s_clause 0x1
	s_load_b32 s9, s[0:1], 0x2c
	s_load_b64 s[2:3], s[0:1], 0x18
	s_waitcnt lgkmcnt(0)
	s_and_b32 s8, s9, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_gt_u32 s8, s2
	s_cbranch_scc1 .LBB0_5
; %bb.1:                                ; %.lr.ph
	v_cvt_f32_u32_e32 v1, s8
	s_clause 0x1
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[10:11], s[0:1], 0x10
	s_lshr_b32 s0, s9, 16
	s_sub_i32 s9, 0, s8
	v_mov_b32_e32 v14, 0
	v_rcp_iflag_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x4f7ffffe, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_cvt_u32_f32_e32 v2, v1
	v_and_b32_e32 v1, 0x3ff, v0
	v_bfe_u32 v0, v0, 10, 10
	v_readfirstlane_b32 s1, v2
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_mad_u64_u32 v[6:7], null, s14, s8, v[1:2]
	v_mad_u64_u32 v[4:5], null, s15, s0, v[0:1]
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_4) | instid1(VALU_DEP_4)
	s_mul_i32 s9, s9, s1
	v_mul_lo_u32 v8, v0, s3
	s_mul_hi_u32 s9, s1, s9
	v_mov_b32_e32 v2, 0
	s_add_i32 s1, s1, s9
	v_ashrrev_i32_e32 v7, 31, v6
	s_mul_hi_u32 s1, s2, s1
	v_ashrrev_i32_e32 v5, 31, v4
	s_mul_i32 s9, s1, s8
	v_add_lshl_u32 v12, v8, v1, 3
	s_sub_i32 s2, s2, s9
	s_add_i32 s9, s1, 1
	s_sub_i32 s12, s2, s8
	s_cmp_ge_u32 s2, s8
	v_lshl_add_u32 v10, v8, 3, 0
	s_cselect_b32 s1, s9, s1
	s_cselect_b32 s2, s12, s2
	s_add_i32 s9, s1, 1
	v_lshlrev_b64 v[8:9], 3, v[4:5]
	s_cmp_ge_u32 s2, s8
	v_lshlrev_b64 v[4:5], 3, v[6:7]
	s_cselect_b32 s1, s9, s1
	s_lshl_b32 s2, s3, 3
	v_add_nc_u32_e32 v11, 0, v12
	s_mul_i32 s3, s2, s3
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v6, vcc_lo, s4, v8
	s_lshl_b32 s3, s3, 3
	v_add_co_ci_u32_e32 v7, vcc_lo, s5, v9, vcc_lo
	s_add_i32 s3, s3, 0
	v_add_co_u32 v8, vcc_lo, s10, v8
	v_dual_mov_b32 v3, 0 :: v_dual_add_nc_u32 v12, s3, v12
	v_add_co_ci_u32_e32 v9, vcc_lo, s11, v9, vcc_lo
	v_lshl_add_u32 v13, v1, 3, s3
	s_mov_b32 s3, 0
	s_max_u32 s4, s8, 1
.LBB0_2:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_3 Depth 2
	global_load_b64 v[15:16], v[6:7], off
	v_mad_u64_u32 v[17:18], null, s3, s0, v[0:1]
	v_mov_b32_e32 v18, v14
	v_mad_u64_u32 v[19:20], null, s3, s8, v[1:2]
	v_mov_b32_e32 v20, v14
	s_mov_b32 s5, s4
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[17:18], 3, v[17:18]
	v_lshlrev_b64 v[19:20], 3, v[19:20]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_u32 v17, vcc_lo, s6, v17
	v_add_co_ci_u32_e32 v18, vcc_lo, s7, v18, vcc_lo
	global_load_b64 v[17:18], v[17:18], off
	s_waitcnt vmcnt(1)
	v_add_co_u32 v15, vcc_lo, v15, v19
	v_add_co_ci_u32_e32 v16, vcc_lo, v16, v20, vcc_lo
	flat_load_b64 v[15:16], v[15:16]
	s_waitcnt vmcnt(1)
	v_add_co_u32 v17, vcc_lo, v17, v4
	v_add_co_ci_u32_e32 v18, vcc_lo, v18, v5, vcc_lo
	s_waitcnt vmcnt(0) lgkmcnt(0)
	ds_store_b64 v11, v[15:16]
	flat_load_b64 v[17:18], v[17:18]
	v_dual_mov_b32 v15, v10 :: v_dual_mov_b32 v16, v13
	s_waitcnt vmcnt(0) lgkmcnt(0)
	ds_store_b64 v12, v[17:18]
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
.LBB0_3:                                ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	ds_load_b64 v[17:18], v15
	ds_load_b64 v[19:20], v16
	v_add_nc_u32_e32 v16, s2, v16
	v_add_nc_u32_e32 v15, 8, v15
	s_add_i32 s5, s5, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s5, 0
	s_waitcnt lgkmcnt(0)
	v_fma_f64 v[2:3], v[17:18], v[19:20], v[2:3]
	s_cbranch_scc0 .LBB0_3
; %bb.4:                                ;   in Loop: Header=BB0_2 Depth=1
	s_barrier
	buffer_gl0_inv
	global_load_b64 v[15:16], v[8:9], off
	s_add_i32 s3, s3, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_ge_u32 s3, s1
	s_waitcnt vmcnt(0)
	v_add_co_u32 v15, vcc_lo, v15, v4
	v_add_co_ci_u32_e32 v16, vcc_lo, v16, v5, vcc_lo
	flat_store_b64 v[15:16], v[2:3]
	s_cbranch_scc0 .LBB0_2
.LBB0_5:                                ; %._crit_edge
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z9matrixMulPPdS0_S0_ii
		.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 21
		.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	_Z9matrixMulPPdS0_S0_ii, .Lfunc_end0-_Z9matrixMulPPdS0_S0_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 624
; NumSgprs: 18
; NumVgprs: 21
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 21
; Occupancy: 16
; WaveLimiterHint : 1
; 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
      - .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
      - .offset:         152
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .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:           _Z9matrixMulPPdS0_S0_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z9matrixMulPPdS0_S0_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     21
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
