#include <stdlib.h>
#include <stdio.h>
#include <cuda.h>
#include <stdlib.h>


void fill_matrix(double *mat, unsigned numRows, unsigned numCols)
{
    for(unsigned i=0; i < numRows; i++)
       for(unsigned j=0; j < numCols; j++)
       {
         mat[i*numCols + j] = i*2.1f + j*3.2f;
       }
}

void print_matrix_to_file(double *mat, unsigned numRows, unsigned numCols)
{
  const char *fname = "assignment2_3_out";
  FILE *f = fopen(fname, "w");
  for(unsigned i=0; i < numRows; i++)
  {
     for(unsigned j=0; j < numCols; j++)
     fprintf(f,"%4.4f ", mat[i*numCols + j]);
     fprintf(f,"\n");
}
fclose(f); }

template<int TILE_WIDTH>
__global__ void MatrixMulKernel_col_maj(double* M, double* N, double* P, int Width) { 
    extern __shared__ double buffer[];

    double *ds_M = &buffer[0]; // TILE_WIDTH WIDTH
    double *ds_N = &buffer[TILE_WIDTH*Width]; // WIDTH TILE_WIDTH

    //__shared__ float ds_M[Width][Width];
    //__shared__ float ds_N[Width][Width];
    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;
    
    
    // Loop over the M and N tiles required to compute the P element
    for (int p = 0; p < Width/TILE_WIDTH; ++p) {
    // Collaborative loading of M and N tiles into shared memory
    ds_M[ty*Width + tx + p*blockDim.x ] = M[Row*Width + p*TILE_WIDTH+tx];
    ds_N[ty*TILE_WIDTH + blockDim.y*TILE_WIDTH*p + tx] = N[(p*TILE_WIDTH+ty)*Width + Col];
    __syncthreads();
    }

    double Pvalue = 0;
    for (int i = 0; i < TILE_WIDTH; ++i){
        Pvalue += ds_M[ty*Width + i] * ds_N[i*Width + tx];
    }
    __syncthreads();
    P[Row*Width+Col] = Pvalue;
}

int main(int argc,char **argv) {
    int N_ll[2]; int N;
    

    int loop, loop1, loop2; // loop variables
    float time_spent;

    N_ll[0]=16; N_ll[1]=8192;

    for (loop=0;loop<2;loop++){

        N=N_ll[loop];

        size_t size = N *N* sizeof(double);

        double*h_matA = (double*)malloc(size);
        double*h_matB = (double*)malloc(size);
        double*h_matC = (double*)malloc(size); // result

        fill_matrix(h_matA,N,N);
        fill_matrix(h_matB,N,N);

        printf("\nMatrix A (first 10*10 inputs)\n");
        for(loop1 = 0; loop1 < 10; loop1++){
            for (loop2=0;loop2 < 10; loop2++)
                printf("%f ", *(h_matA + N*loop1 + loop2));
            printf("\n");
        }

        printf("\n\nMatrix B (first 10*10 inputs)\n");
        for(loop1 = 0; loop1 < 10; loop1++){
            for (loop2=0;loop2 < 10; loop2++)
                printf("%f ", *(h_matB + N*loop1 + loop2));
            printf("\n");
        }

        double* d_matA;   cudaMalloc(&d_matA, size);
        double* d_matB;   cudaMalloc(&d_matB, size);
        double* d_matC;   cudaMalloc(&d_matC, size);

        //GPU timing
        cudaEvent_t start, stop;
        cudaEventCreate(&start);
        cudaEventCreate(&stop);

        // Copy vectors from host memory to device memory
        cudaMemcpy(d_matA, h_matA, size,cudaMemcpyHostToDevice);
        cudaMemcpy(d_matB, h_matB, size,cudaMemcpyHostToDevice);

        // Invoke kernel
        dim3 threadsPerBlock (16,16);
        dim3 blocksPerGrid ((N + threadsPerBlock.x - 1) /threadsPerBlock.x,(N + threadsPerBlock.y - 1) /threadsPerBlock.y);

        cudaEventRecord(start, 0);
        size_t blocksize = 2 * N * 16;
        (MatrixMulKernel_col_maj<16>)<<<blocksPerGrid, threadsPerBlock, sizeof(double)*blocksize>>>(d_matA,d_matB, d_matC, N);
        cudaError_t err1 = cudaPeekAtLastError();
        cudaEventRecord(stop, 0);
        cudaEventSynchronize(stop);
        cudaEventElapsedTime(&time_spent, start, stop);
        printf("\nTime spent in col maj %f\n",time_spent);

        // h_C contains the result in host memory
        cudaMemcpy(h_matC, d_matC, size,cudaMemcpyDeviceToHost);

        printf("\n\nMatrix C (first 10*10 outputs)\n");
        for(loop1 = 0; loop1 < 10; loop1++){
            for (loop2=0;loop2 < 10; loop2++)
                printf("%f ", *(h_matC + N*loop1 + loop2));
            printf("\n");
        }

        // Log outputs
        printf("\nWritting to file assignment_2_1_out as Mat C");
        print_matrix_to_file(h_matC,N,N);

        // Free device memory
        cudaFree(d_matA);
        cudaFree(d_matB);
        cudaFree(d_matC);

        // Free host memory
        free(h_matA);
        free(h_matB);
        free(h_matC);
        
        
    }   
    return 0;
}

	.file	"tmpxft_002ef008_00000000-6_ee16b068_3.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.type	_ZL58__device_stub__Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_iPdS_S_i, @function
_ZL58__device_stub__Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_iPdS_S_i:
.LFB2056:
	.cfi_startproc
	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	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_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
.LFE2056:
	.size	_ZL58__device_stub__Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_iPdS_S_i, .-_ZL58__device_stub__Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_iPdS_S_i
	.section	.text._Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i,"axG",@progbits,_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i,comdat
	.weak	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i
	.type	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i, @function
_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i:
.LFB2106:
	.cfi_startproc
	endbr64
	jmp	_ZL58__device_stub__Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_iPdS_S_i
	.cfi_endproc
.LFE2106:
	.size	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i, .-_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i
	.text
	.globl	_Z11fill_matrixPdjj
	.type	_Z11fill_matrixPdjj, @function
_Z11fill_matrixPdjj:
.LFB2027:
	.cfi_startproc
	endbr64
	movss	.LC0(%rip), %xmm2
	movss	.LC1(%rip), %xmm3
	movq	%rdi, %r8
	xorl	%ecx, %ecx
	xorl	%edi, %edi
.L9:
	cmpl	%esi, %ecx
	je	.L17
	xorl	%eax, %eax
	movl	%ecx, %r9d
.L15:
	cmpl	%edx, %eax
	je	.L18
	movl	%eax, %r11d
	cvtsi2ssq	%r9, %xmm0
	leal	(%rax,%rdi), %r10d
	incl	%eax
	cvtsi2ssq	%r11, %xmm1
	mulss	%xmm2, %xmm0
	mulss	%xmm3, %xmm1
	addss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	movsd	%xmm0, (%r8,%r10,8)
	jmp	.L15
.L18:
	incl	%ecx
	addl	%edx, %edi
	jmp	.L9
.L17:
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z11fill_matrixPdjj, .-_Z11fill_matrixPdjj
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC2:
	.string	"w"
.LC3:
	.string	"assignment2_3_out"
.LC4:
	.string	"%4.4f "
.LC5:
	.string	"\n"
	.text
	.globl	_Z20print_matrix_to_filePdjj
	.type	_Z20print_matrix_to_filePdjj, @function
_Z20print_matrix_to_filePdjj:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%r15d, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movl	%edx, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdi, %r13
	leaq	.LC3(%rip), %rdi
	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
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%esi, %ebx
	leaq	.LC2(%rip), %rsi
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	call	fopen@PLT
	movq	%rax, 8(%rsp)
.L20:
	cmpq	%rbx, %r12
	je	.L25
	movl	%r15d, %ebp
.L22:
	leal	(%r14,%r15), %eax
	cmpl	%eax, %ebp
	je	.L26
	movl	%ebp, %eax
	movq	8(%rsp), %rdi
	leaq	.LC4(%rip), %rdx
	incl	%ebp
	movsd	0(%r13,%rax,8), %xmm0
	movl	$2, %esi
	movb	$1, %al
	call	__fprintf_chk@PLT
	jmp	.L22
.L26:
	movq	8(%rsp), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	incq	%r12
	leaq	.LC5(%rip), %rdx
	movl	%ebp, %r15d
	call	__fprintf_chk@PLT
	jmp	.L20
.L25:
	movq	8(%rsp), %rdi
	addq	$24, %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
	jmp	fclose@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z20print_matrix_to_filePdjj, .-_Z20print_matrix_to_filePdjj
	.section	.rodata.str1.1
.LC6:
	.string	"\nMatrix A (first 10*10 inputs)\n"
.LC7:
	.string	"%f "
.LC8:
	.string	"\n\nMatrix B (first 10*10 inputs)\n"
.LC9:
	.string	"\nTime spent in col maj %f\n"
.LC10:
	.string	"\n\nMatrix C (first 10*10 outputs)\n"
.LC11:
	.string	"\nWritting to file assignment_2_1_out as Mat C"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.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
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$152, %rsp
	.cfi_def_cfa_offset 208
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	movabsq	$35184372088848, %rax
	movq	%rax, 128(%rsp)
.L38:
	movslq	128(%rsp,%r12,4), %r15
	xorl	%r13d, %r13d
	movl	$10, %r14d
	movl	%r15d, %eax
	movq	%r15, %rbx
	imull	%r15d, %eax
	cltq
	salq	$3, %rax
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	movq	8(%rsp), %rdi
	movq	%rax, %rbp
	call	malloc@PLT
	movq	8(%rsp), %rdi
	movq	%rax, 16(%rsp)
	call	malloc@PLT
	movl	%r15d, %edx
	movl	%r15d, %esi
	movq	%rbp, %rdi
	movq	%rax, 24(%rsp)
	call	_Z11fill_matrixPdjj
	movq	16(%rsp), %rdi
	call	_Z11fill_matrixPdjj
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L28:
	leaq	0(%rbp,%r13,8), %rcx
	xorl	%edx, %edx
.L29:
	movsd	(%rcx,%rdx,8), %xmm0
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movq	%rcx, 40(%rsp)
	movq	%rdx, 32(%rsp)
	call	__printf_chk@PLT
	movq	32(%rsp), %rdx
	movq	40(%rsp), %rcx
	incq	%rdx
	cmpq	$10, %rdx
	jne	.L29
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	addq	%r15, %r13
	call	__printf_chk@PLT
	decl	%r14d
	jne	.L28
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%r13d, %r13d
	movl	$10, %r14d
	leaq	.LC8(%rip), %rsi
	call	__printf_chk@PLT
.L31:
	movq	16(%rsp), %rax
	xorl	%edx, %edx
	leaq	(%rax,%r13,8), %rcx
.L32:
	movsd	(%rcx,%rdx,8), %xmm0
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movq	%rcx, 40(%rsp)
	movq	%rdx, 32(%rsp)
	call	__printf_chk@PLT
	movq	32(%rsp), %rdx
	movq	40(%rsp), %rcx
	incq	%rdx
	cmpq	$10, %rdx
	jne	.L32
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	addq	%r15, %r13
	call	__printf_chk@PLT
	decl	%r14d
	jne	.L31
	movq	8(%rsp), %rsi
	leaq	64(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rsi
	leaq	72(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rsi
	leaq	80(%rsp), %rdi
	call	cudaMalloc@PLT
	leaq	88(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	96(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	8(%rsp), %rdx
	movq	64(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdx
	movq	16(%rsp), %rsi
	movl	$1, %ecx
	movq	72(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	88(%rsp), %rdi
	leal	15(%rbx), %eax
	xorl	%esi, %esi
	shrl	$4, %eax
	movl	$1, 124(%rsp)
	movl	%eax, 116(%rsp)
	movl	%eax, 120(%rsp)
	call	cudaEventRecord@PLT
	movl	%ebx, %eax
	movq	116(%rsp), %rdi
	movl	124(%rsp), %esi
	sall	$5, %eax
	xorl	%r9d, %r9d
	movabsq	$68719476752, %rdx
	movl	$1, %ecx
	cltq
	movq	%rdx, 104(%rsp)
	leaq	0(,%rax,8), %r8
	movl	$1, 112(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L34
	movq	80(%rsp), %rdx
	movq	72(%rsp), %rsi
	movl	%ebx, %ecx
	movq	64(%rsp), %rdi
	call	_ZL58__device_stub__Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_iPdS_S_i
.L34:
	call	cudaPeekAtLastError@PLT
	movq	96(%rsp), %rdi
	xorl	%esi, %esi
	xorl	%r13d, %r13d
	movl	$10, %r14d
	call	cudaEventRecord@PLT
	movq	96(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	96(%rsp), %rdx
	movq	88(%rsp), %rsi
	leaq	60(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	60(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	8(%rsp), %rdx
	movq	80(%rsp), %rsi
	movq	24(%rsp), %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L35:
	movq	24(%rsp), %rax
	xorl	%edx, %edx
	leaq	(%rax,%r13,8), %rcx
.L36:
	movsd	(%rcx,%rdx,8), %xmm0
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movq	%rcx, 32(%rsp)
	movq	%rdx, 8(%rsp)
	call	__printf_chk@PLT
	movq	8(%rsp), %rdx
	movq	32(%rsp), %rcx
	incq	%rdx
	cmpq	$10, %rdx
	jne	.L36
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	addq	%r15, %r13
	call	__printf_chk@PLT
	decl	%r14d
	jne	.L35
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	24(%rsp), %rdi
	movl	%ebx, %edx
	movl	%ebx, %esi
	call	_Z20print_matrix_to_filePdjj
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	free@PLT
	movq	24(%rsp), %rdi
	call	free@PLT
	testq	%r12, %r12
	jne	.L45
	movl	$1, %r12d
	jmp	.L38
.L45:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L39
	call	__stack_chk_fail@PLT
.L39:
	addq	$152, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	main, .-main
	.section	.rodata.str1.1
.LC12:
	.string	"_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC12(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2059:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	1074161254
	.align 4
.LC1:
	.long	1078774989
	.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 <stdlib.h>
#include <stdio.h>
#include <hip/hip_runtime.h>
#include <stdlib.h>


void fill_matrix(double *mat, unsigned numRows, unsigned numCols)
{
    for(unsigned i=0; i < numRows; i++)
       for(unsigned j=0; j < numCols; j++)
       {
         mat[i*numCols + j] = i*2.1f + j*3.2f;
       }
}

void print_matrix_to_file(double *mat, unsigned numRows, unsigned numCols)
{
  const char *fname = "assignment2_3_out";
  FILE *f = fopen(fname, "w");
  for(unsigned i=0; i < numRows; i++)
  {
     for(unsigned j=0; j < numCols; j++)
     fprintf(f,"%4.4f ", mat[i*numCols + j]);
     fprintf(f,"\n");
}
fclose(f); }

template<int TILE_WIDTH>
__global__ void MatrixMulKernel_col_maj(double* M, double* N, double* P, int Width) { 
    extern __shared__ double buffer[];

    double *ds_M = &buffer[0]; // TILE_WIDTH WIDTH
    double *ds_N = &buffer[TILE_WIDTH*Width]; // WIDTH TILE_WIDTH

    //__shared__ float ds_M[Width][Width];
    //__shared__ float ds_N[Width][Width];
    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;
    
    
    // Loop over the M and N tiles required to compute the P element
    for (int p = 0; p < Width/TILE_WIDTH; ++p) {
    // Collaborative loading of M and N tiles into shared memory
    ds_M[ty*Width + tx + p*blockDim.x ] = M[Row*Width + p*TILE_WIDTH+tx];
    ds_N[ty*TILE_WIDTH + blockDim.y*TILE_WIDTH*p + tx] = N[(p*TILE_WIDTH+ty)*Width + Col];
    __syncthreads();
    }

    double Pvalue = 0;
    for (int i = 0; i < TILE_WIDTH; ++i){
        Pvalue += ds_M[ty*Width + i] * ds_N[i*Width + tx];
    }
    __syncthreads();
    P[Row*Width+Col] = Pvalue;
}

int main(int argc,char **argv) {
    int N_ll[2]; int N;
    

    int loop, loop1, loop2; // loop variables
    float time_spent;

    N_ll[0]=16; N_ll[1]=8192;

    for (loop=0;loop<2;loop++){

        N=N_ll[loop];

        size_t size = N *N* sizeof(double);

        double*h_matA = (double*)malloc(size);
        double*h_matB = (double*)malloc(size);
        double*h_matC = (double*)malloc(size); // result

        fill_matrix(h_matA,N,N);
        fill_matrix(h_matB,N,N);

        printf("\nMatrix A (first 10*10 inputs)\n");
        for(loop1 = 0; loop1 < 10; loop1++){
            for (loop2=0;loop2 < 10; loop2++)
                printf("%f ", *(h_matA + N*loop1 + loop2));
            printf("\n");
        }

        printf("\n\nMatrix B (first 10*10 inputs)\n");
        for(loop1 = 0; loop1 < 10; loop1++){
            for (loop2=0;loop2 < 10; loop2++)
                printf("%f ", *(h_matB + N*loop1 + loop2));
            printf("\n");
        }

        double* d_matA;   hipMalloc(&d_matA, size);
        double* d_matB;   hipMalloc(&d_matB, size);
        double* d_matC;   hipMalloc(&d_matC, size);

        //GPU timing
        hipEvent_t start, stop;
        hipEventCreate(&start);
        hipEventCreate(&stop);

        // Copy vectors from host memory to device memory
        hipMemcpy(d_matA, h_matA, size,hipMemcpyHostToDevice);
        hipMemcpy(d_matB, h_matB, size,hipMemcpyHostToDevice);

        // Invoke kernel
        dim3 threadsPerBlock (16,16);
        dim3 blocksPerGrid ((N + threadsPerBlock.x - 1) /threadsPerBlock.x,(N + threadsPerBlock.y - 1) /threadsPerBlock.y);

        hipEventRecord(start, 0);
        size_t blocksize = 2 * N * 16;
        (MatrixMulKernel_col_maj<16>)<<<blocksPerGrid, threadsPerBlock, sizeof(double)*blocksize>>>(d_matA,d_matB, d_matC, N);
        hipError_t err1 = hipPeekAtLastError();
        hipEventRecord(stop, 0);
        hipEventSynchronize(stop);
        hipEventElapsedTime(&time_spent, start, stop);
        printf("\nTime spent in col maj %f\n",time_spent);

        // h_C contains the result in host memory
        hipMemcpy(h_matC, d_matC, size,hipMemcpyDeviceToHost);

        printf("\n\nMatrix C (first 10*10 outputs)\n");
        for(loop1 = 0; loop1 < 10; loop1++){
            for (loop2=0;loop2 < 10; loop2++)
                printf("%f ", *(h_matC + N*loop1 + loop2));
            printf("\n");
        }

        // Log outputs
        printf("\nWritting to file assignment_2_1_out as Mat C");
        print_matrix_to_file(h_matC,N,N);

        // Free device memory
        hipFree(d_matA);
        hipFree(d_matB);
        hipFree(d_matC);

        // Free host memory
        free(h_matA);
        free(h_matB);
        free(h_matC);
        
        
    }   
    return 0;
}

	.text
	.file	"ee16b068_3.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z11fill_matrixPdjj
.LCPI0_0:
	.long	0x40066666                      # float 2.0999999
.LCPI0_1:
	.long	0x404ccccd                      # float 3.20000005
	.text
	.globl	_Z11fill_matrixPdjj
	.type	_Z11fill_matrixPdjj,@function
_Z11fill_matrixPdjj:                    # @_Z11fill_matrixPdjj
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	je	.LBB0_6
# %bb.1:                                # %.preheader.lr.ph
	movl	%edx, %eax
	xorl	%ecx, %ecx
	movss	.LCPI0_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movss	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	xorl	%r8d, %r8d
.LBB0_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
	testl	%edx, %edx
	je	.LBB0_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB0_2 Depth=1
	movl	%r8d, %r9d
	xorps	%xmm2, %xmm2
	cvtsi2ss	%r9, %xmm2
	mulss	%xmm0, %xmm2
	xorl	%r9d, %r9d
.LBB0_4:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%r9d, %r10d
	xorps	%xmm3, %xmm3
	cvtsi2ss	%r10, %xmm3
	mulss	%xmm1, %xmm3
	addss	%xmm2, %xmm3
	cvtss2sd	%xmm3, %xmm3
	leal	(%rcx,%r9), %r10d
	movsd	%xmm3, (%rdi,%r10,8)
	incq	%r9
	cmpq	%r9, %rax
	jne	.LBB0_4
.LBB0_5:                                # %._crit_edge
                                        #   in Loop: Header=BB0_2 Depth=1
	incl	%r8d
	addq	%rax, %rcx
	cmpl	%esi, %r8d
	jne	.LBB0_2
.LBB0_6:                                # %._crit_edge15
	retq
.Lfunc_end0:
	.size	_Z11fill_matrixPdjj, .Lfunc_end0-_Z11fill_matrixPdjj
	.cfi_endproc
                                        # -- End function
	.globl	_Z20print_matrix_to_filePdjj    # -- Begin function _Z20print_matrix_to_filePdjj
	.type	_Z20print_matrix_to_filePdjj,@function
_Z20print_matrix_to_filePdjj:           # @_Z20print_matrix_to_filePdjj
	.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	$24, %rsp
	.cfi_def_cfa_offset 80
	.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	%edx, %ebx
	movl	%esi, %ebp
	movq	%rdi, %r14
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %r15
	movl	%ebp, 12(%rsp)                  # 4-byte Spill
	testl	%ebp, %ebp
	je	.LBB1_4
# %bb.1:                                # %.preheader.lr.ph
	movl	%ebx, %ecx
	movl	%ebx, %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	xorl	%ebx, %ebx
	xorl	%ebp, %ebp
	movl	%ecx, 8(%rsp)                   # 4-byte Spill
.LBB1_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_5 Depth 2
	movq	16(%rsp), %r12                  # 8-byte Reload
	movl	%ebx, %r13d
	testl	%ecx, %ecx
	je	.LBB1_3
.LBB1_5:                                #   Parent Loop BB1_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%r13d, %eax
	movsd	(%r14,%rax,8), %xmm0            # xmm0 = mem[0],zero
	movl	$.L.str.2, %esi
	movq	%r15, %rdi
	movb	$1, %al
	callq	fprintf
	incl	%r13d
	decq	%r12
	jne	.LBB1_5
.LBB1_3:                                # %._crit_edge
                                        #   in Loop: Header=BB1_2 Depth=1
	movl	$10, %edi
	movq	%r15, %rsi
	callq	fputc@PLT
	incl	%ebp
	movl	8(%rsp), %ecx                   # 4-byte Reload
	addl	%ecx, %ebx
	cmpl	12(%rsp), %ebp                  # 4-byte Folded Reload
	jne	.LBB1_2
.LBB1_4:                                # %._crit_edge17
	movq	%r15, %rdi
	addq	$24, %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
	jmp	fclose                          # TAILCALL
.Lfunc_end1:
	.size	_Z20print_matrix_to_filePdjj, .Lfunc_end1-_Z20print_matrix_to_filePdjj
	.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
	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	$88, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movabsq	$35184372088848, %rax           # imm = 0x200000000010
	movq	%rax, 80(%rsp)
	movb	$1, %cl
	xorl	%eax, %eax
.LBB2_1:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_2 Depth 2
                                        #       Child Loop BB2_3 Depth 3
                                        #     Child Loop BB2_6 Depth 2
                                        #       Child Loop BB2_7 Depth 3
                                        #     Child Loop BB2_12 Depth 2
                                        #       Child Loop BB2_13 Depth 3
	movl	%ecx, 48(%rsp)                  # 4-byte Spill
	movl	80(%rsp,%rax,4), %eax
	movq	%rax, (%rsp)                    # 8-byte Spill
	movslq	%eax, %rbx
	movl	%ebx, %ebp
	imull	%ebp, %ebp
	shlq	$3, %rbp
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, %r14
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, %r15
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, 56(%rsp)                  # 8-byte Spill
	movq	%r14, %rdi
	movl	%ebx, %esi
	movl	%ebx, %edx
	callq	_Z11fill_matrixPdjj
	movq	%r15, 8(%rsp)                   # 8-byte Spill
	movq	%r15, %rdi
	movl	%ebx, %esi
	movl	%ebx, %edx
	callq	_Z11fill_matrixPdjj
	movl	$.Lstr, %edi
	callq	puts@PLT
	leaq	(,%rbx,8), %r15
	movq	%r14, 72(%rsp)                  # 8-byte Spill
	xorl	%r12d, %r12d
.LBB2_2:                                # %.preheader74
                                        #   Parent Loop BB2_1 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB2_3 Depth 3
	xorl	%r13d, %r13d
.LBB2_3:                                #   Parent Loop BB2_1 Depth=1
                                        #     Parent Loop BB2_2 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movsd	(%r14,%r13,8), %xmm0            # xmm0 = mem[0],zero
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	incq	%r13
	cmpq	$10, %r13
	jne	.LBB2_3
# %bb.4:                                #   in Loop: Header=BB2_2 Depth=2
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r12
	addq	%r15, %r14
	cmpq	$10, %r12
	jne	.LBB2_2
# %bb.5:                                #   in Loop: Header=BB2_1 Depth=1
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movq	8(%rsp), %r14                   # 8-byte Reload
	xorl	%r12d, %r12d
.LBB2_6:                                # %.preheader73
                                        #   Parent Loop BB2_1 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB2_7 Depth 3
	xorl	%r13d, %r13d
.LBB2_7:                                #   Parent Loop BB2_1 Depth=1
                                        #     Parent Loop BB2_6 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movsd	(%r14,%r13,8), %xmm0            # xmm0 = mem[0],zero
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	incq	%r13
	cmpq	$10, %r13
	jne	.LBB2_7
# %bb.8:                                #   in Loop: Header=BB2_6 Depth=2
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r12
	addq	%r15, %r14
	cmpq	$10, %r12
	jne	.LBB2_6
# %bb.9:                                #   in Loop: Header=BB2_1 Depth=1
	leaq	40(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	leaq	32(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	leaq	24(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	leaq	64(%rsp), %rdi
	callq	hipEventCreate
	leaq	16(%rsp), %rdi
	callq	hipEventCreate
	movq	40(%rsp), %rdi
	movq	72(%rsp), %r13                  # 8-byte Reload
	movq	%r13, %rsi
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	32(%rsp), %rdi
	movq	8(%rsp), %rsi                   # 8-byte Reload
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%rsp), %rax                    # 8-byte Reload
	addl	$15, %eax
	shrl	$4, %eax
	movq	%rax, %r14
	shlq	$32, %r14
	orq	%rax, %r14
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	shlq	$8, %rbx
	movq	%r14, %rdi
	movl	$1, %esi
	movabsq	$68719476752, %rdx              # imm = 0x1000000010
	movl	$1, %ecx
	movq	%rbx, %r8
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_11
# %bb.10:                               #   in Loop: Header=BB2_1 Depth=1
	movq	40(%rsp), %rdi
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdx
	movq	(%rsp), %rcx                    # 8-byte Reload
                                        # kill: def $ecx killed $ecx killed $rcx
	callq	_Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_i
.LBB2_11:                               #   in Loop: Header=BB2_1 Depth=1
	callq	hipPeekAtLastError
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	16(%rsp), %rdi
	callq	hipEventSynchronize
	movq	64(%rsp), %rsi
	movq	16(%rsp), %rdx
	leaq	52(%rsp), %rdi
	callq	hipEventElapsedTime
	cvtss2sd	52(%rsp), %xmm0
	movl	$.L.str.7, %edi
	movb	$1, %al
	callq	printf
	movq	24(%rsp), %rsi
	movq	56(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rdi
	movq	%rbp, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	xorl	%r14d, %r14d
.LBB2_12:                               # %.preheader
                                        #   Parent Loop BB2_1 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB2_13 Depth 3
	xorl	%r12d, %r12d
.LBB2_13:                               #   Parent Loop BB2_1 Depth=1
                                        #     Parent Loop BB2_12 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movsd	(%rbx,%r12,8), %xmm0            # xmm0 = mem[0],zero
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	incq	%r12
	cmpq	$10, %r12
	jne	.LBB2_13
# %bb.14:                               #   in Loop: Header=BB2_12 Depth=2
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r14
	addq	%r15, %rbx
	cmpq	$10, %r14
	jne	.LBB2_12
# %bb.15:                               #   in Loop: Header=BB2_1 Depth=1
	movl	$.L.str.9, %edi
	xorl	%eax, %eax
	callq	printf
	movq	56(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rdi
	movq	(%rsp), %rdx                    # 8-byte Reload
	movl	%edx, %esi
                                        # kill: def $edx killed $edx killed $rdx
	callq	_Z20print_matrix_to_filePdjj
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	%r13, %rdi
	callq	free
	movq	8(%rsp), %rdi                   # 8-byte Reload
	callq	free
	movq	%rbx, %rdi
	callq	free
	movl	$1, %eax
	testb	$1, 48(%rsp)                    # 1-byte Folded Reload
	movl	$0, %ecx
	jne	.LBB2_1
# %bb.16:
	xorl	%eax, %eax
	addq	$88, %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	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.section	.text._Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_i,"axG",@progbits,_Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_i,comdat
	.weak	_Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_i # -- Begin function _Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_i
	.type	_Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_i,@function
_Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_i: # @_Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_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	$_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_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_end3:
	.size	_Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_i, .Lfunc_end3-_Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_i
	.cfi_endproc
                                        # -- End function
	.text
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_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_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB5_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"assignment2_3_out"
	.size	.L.str, 18

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

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%4.4f "
	.size	.L.str.2, 7

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

	.type	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i,@object # @_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i
	.section	.rodata._Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i,"aG",@progbits,_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i,comdat
	.weak	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i
	.p2align	3, 0x0
_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i:
	.quad	_Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_i
	.size	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i, 8

	.type	.L.str.7,@object                # @.str.7
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.7:
	.asciz	"\nTime spent in col maj %f\n"
	.size	.L.str.7, 27

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"\nWritting to file assignment_2_1_out as Mat C"
	.size	.L.str.9, 46

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i"
	.size	.L__unnamed_1, 45

	.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	"\nMatrix A (first 10*10 inputs)"
	.size	.Lstr, 31

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"\n\nMatrix B (first 10*10 inputs)"
	.size	.Lstr.1, 32

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"\n\nMatrix C (first 10*10 outputs)"
	.size	.Lstr.2, 33

	.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 _Z38__device_stub__MatrixMulKernel_col_majILi16EEvPdS0_S0_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.section	.text._Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i,"axG",@progbits,_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i,comdat
	.protected	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i ; -- Begin function _Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i
	.globl	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i
	.p2align	8
	.type	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i,@function
_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i: ; @_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i
; %bb.0:
	s_clause 0x3
	s_load_b32 s8, s[0:1], 0x2c
	s_load_b32 s2, s[0:1], 0x18
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[0:1], s[0:1], 0x10
	v_bfe_u32 v3, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s9, s8, 16
	s_delay_alu instid0(VALU_DEP_1)
	v_mul_lo_u32 v6, v3, s2
	v_mad_u64_u32 v[1:2], null, s15, s9, v[3:4]
	s_lshl_b32 s3, s2, 7
	s_and_b32 s11, s8, 0xffff
	s_cmp_gt_i32 s2, 15
	s_mov_b32 s8, 0
	s_cbranch_scc1 .LBB0_2
; %bb.1:                                ; %..preheader_crit_edge
	v_mul_lo_u32 v2, v3, s2
	s_branch .LBB0_3
.LBB0_2:
	s_mov_b32 s8, -1
                                        ; implicit-def: $vgpr2
.LBB0_3:                                ; %Flow82
	v_and_b32_e32 v0, 0x3ff, v0
	s_add_i32 s3, s3, 0
	s_and_not1_b32 vcc_lo, exec_lo, s8
	s_mul_i32 s14, s14, s11
	s_cbranch_vccnz .LBB0_7
; %bb.4:                                ; %.lr.ph
	v_lshlrev_b32_e32 v4, 3, v0
	v_lshlrev_b32_e32 v5, 3, v6
	v_lshlrev_b32_e32 v8, 7, v3
	s_ashr_i32 s10, s2, 31
	v_mad_u64_u32 v[2:3], null, v1, s2, v[0:1]
	s_lshr_b32 s10, s10, 28
	v_add3_u32 v7, 0, v4, v5
	v_add3_u32 v8, s3, v8, v4
	v_add3_u32 v4, v0, v6, s14
	s_add_i32 s10, s2, s10
	s_lshl_b32 s8, s2, 4
	s_ashr_i32 s10, s10, 4
	s_lshl_b32 s9, s9, 7
	s_lshl_b32 s11, s11, 3
.LBB0_5:                                ; =>This Inner Loop Header: Depth=1
	v_ashrrev_i32_e32 v3, 31, v2
	v_ashrrev_i32_e32 v5, 31, v4
	s_add_i32 s10, s10, -1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	s_cmp_eq_u32 s10, 0
	v_lshlrev_b64 v[9:10], 3, v[2:3]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_4)
	v_lshlrev_b64 v[11:12], 3, v[4:5]
	v_add_nc_u32_e32 v2, 16, v2
	v_add_nc_u32_e32 v4, s8, v4
	v_add_co_u32 v9, vcc_lo, s4, v9
	v_add_co_ci_u32_e32 v10, vcc_lo, s5, v10, vcc_lo
	v_add_co_u32 v11, vcc_lo, s6, v11
	v_add_co_ci_u32_e32 v12, vcc_lo, s7, v12, vcc_lo
	global_load_b64 v[9:10], v[9:10], off
	global_load_b64 v[11:12], v[11:12], off
	s_waitcnt vmcnt(1)
	ds_store_b64 v7, v[9:10]
	s_waitcnt vmcnt(0)
	ds_store_b64 v8, v[11:12]
	v_add_nc_u32_e32 v8, s9, v8
	v_add_nc_u32_e32 v7, s11, v7
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB0_5
; %bb.6:
	v_mov_b32_e32 v2, v6
.LBB0_7:                                ; %Flow83
	s_delay_alu instid0(VALU_DEP_1)
	v_lshl_add_u32 v4, v2, 3, 0
	v_mov_b32_e32 v2, 0
	v_mov_b32_e32 v3, 0
	v_lshl_add_u32 v5, v0, 3, s3
	s_mov_b32 s3, 0
	s_lshl_b32 s4, s2, 3
.LBB0_8:                                ; =>This Inner Loop Header: Depth=1
	v_add_nc_u32_e32 v8, s3, v4
	s_add_i32 s3, s3, 8
	ds_load_b64 v[6:7], v5
	ds_load_b64 v[8:9], v8
	v_add_nc_u32_e32 v5, s4, v5
	s_cmpk_eq_i32 s3, 0x80
	s_waitcnt lgkmcnt(0)
	v_fma_f64 v[2:3], v[8:9], v[6:7], v[2:3]
	s_cbranch_scc0 .LBB0_8
; %bb.9:
	v_mul_lo_u32 v1, v1, s2
	s_barrier
	buffer_gl0_inv
	v_add3_u32 v0, s14, v0, v1
	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], 3, v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v0, vcc_lo, s0, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_store_b64 v[0:1], v[2:3], off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_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 13
		.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
	.section	.text._Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i,"axG",@progbits,_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i,comdat
.Lfunc_end0:
	.size	_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i, .Lfunc_end0-_Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 516
; NumSgprs: 18
; NumVgprs: 13
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 13
; 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
      - .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:           _Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z23MatrixMulKernel_col_majILi16EEvPdS0_S0_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     13
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
