/**
 * 
 * Matrix Multiplication - CUDA for GPUs
 *
 * CS3210
 *
 **/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>

#define BLOCK_SIZE 32

int size;

typedef struct
{
	float **element;
} matrix;

long long wall_clock_time()
{
#ifdef __linux__
	struct timespec tp;
	clock_gettime(CLOCK_REALTIME, &tp);
	return (long long)(tp.tv_nsec + (long long)tp.tv_sec * 1000000000ll);
#else
	struct timeval tv;
	gettimeofday(&tv, NULL);
	return (long long)(tv.tv_usec * 1000 + (long long)tv.tv_sec * 1000000000ll);
#endif
}

__device__ float getElement(matrix A, int row, int col) {
    return A.element[row][col];
}

__device__ void setElement(matrix A, int row, int col, float value) {
    A.element[row][col] = value;
}

__device__ matrix getSubMatrix(matrix A, int blockRow, int blockCol) {
    int startingRow = BLOCK_SIZE * blockRow;
    int startingCol = BLOCK_SIZE * blockCol;

    // Allocate memory for sub matrix
    matrix subA;
    subA.element = (float**)malloc(sizeof(float*) * BLOCK_SIZE);
    int row;
    for (row = 0; row < BLOCK_SIZE; row++) {
        // subA.element[row] = (float*)malloc(sizeof(float) * BLOCK_SIZE);
        subA.element[row] = A.element[startingRow + row] + startingCol;
    }

    // int row, col;
    // for (row = 0; row < BLOCK_SIZE; row++) {
    //     subA.element[row] = A.element[startingRow + row] + startingCol;
    //     // for (col = 0; col < BLOCK_SIZE; col++) {
    //     //     printf("%f ", A.element[startingRow + row][startingCol + col]);
    //     // }
    //     // printf("\n");
    // }

    // int i, j;
    // for (i = 0; i < BLOCK_SIZE; i++) {
    //     for (j = 0; j < BLOCK_SIZE; j++) {
    //         printf("%f ", subA.element[i][j]);
    //     }
    //     printf("\n");
    // }
    return subA;
}

/**
 * Allocates memory for a matrix of size SIZE
 * The memory is allocated row-major order, i.e. 
 *  elements from the same row are allocated at contiguous 
 *  memory addresses.
 **/
void allocate_matrix(matrix* m)
{
	int i;
	cudaError_t rc;
	
	// allocate array for all the rows
	rc = cudaMallocManaged((void**)&(m->element), sizeof(float*) * size);
	if (rc != cudaSuccess)
	{
		fprintf(stderr, "CUDA error: %s\n", cudaGetErrorString(rc));
		exit(1);
	}
	
	// allocate an array for each row of the matrix
	for (i = 0; i < size; i++)
	{
		rc = cudaMallocManaged((void**)&(m->element[i]), sizeof(float) * size);
		if (rc != cudaSuccess)
		{
			fprintf(stderr, "CUDA error: %s\n", cudaGetErrorString(rc));
			exit(1);
		}
	}
}

/**
 * Free the memory allocated for a matrix.
 **/
void free_matrix(matrix* m) {
	int i;
	for (i = 0; i < size; i++)
		cudaFree(m->element[i]);
	cudaFree(m->element);
}

/**
 * Initializes the elements of the matrix with
 * random values between 0 and 9
 **/
void init_matrix(matrix m)
{
	int i, j;
	
	for (i = 0; i < size; i++)
		for (j = 0; j < size; j++)
		{
			m.element[i][j] = rand() % 10;
		}
}

/**
 * Initializes the elements of the matrix with
 * element 0.
 **/
void init_matrix_zero(matrix m)
{
	int i, j;
	
	for (i = 0; i < size; i++)
		for (j = 0; j < size; j++)
		{
			m.element[i][j] = 0.0;
		}
}


/**
 * Multiplies matrix @a with matrix @b storing
 * the result in matrix @result
 * 
 * The multiplication algorithm is the O(n^3) 
 * algorithm
 */
void mm(matrix a, matrix b, matrix result)
{
	int i, j, k;
	
	// Do the multiplication
	for (i = 0; i < size; i++)
		for (j = 0; j < size; j++)
			for(k = 0; k < size; k++)
				result.element[i][j] += a.element[i][k] * b.element[k][j];
}

/**
 * Each kernel computes the result element (i,j).
 */
__global__ void mm_kernel(matrix a, matrix b, matrix result, int size)
{
	int i = blockIdx.y * blockDim.y + threadIdx.y;
	int j = blockIdx.x * blockDim.x + threadIdx.x; 
	int k;
    float resultValue = 0;

	// if (i >= size || j >= size)
	// 		return;

    for (k = 0; k < (size / BLOCK_SIZE); k++) {
        __shared__ float sharedA[BLOCK_SIZE][BLOCK_SIZE];
        __shared__ float sharedB[BLOCK_SIZE][BLOCK_SIZE];

        sharedA[threadIdx.y][threadIdx.x] = a.element[i][k * BLOCK_SIZE + threadIdx.x];
        sharedB[threadIdx.y][threadIdx.x] = b.element[k * BLOCK_SIZE + threadIdx.y][j];

        __syncthreads();

        int i;
        for (i = 0; i < BLOCK_SIZE; i++) {
            resultValue += sharedA[threadIdx.y][i] * sharedB[i][threadIdx.x];
        }

        __syncthreads();
	}
	
	result.element[i][j] = resultValue;
}

void print_matrix(matrix m)
{
	int i, j;
	
	for (i = 0; i < size; i++)
	{
		printf("row %4d: ", i);
		for (j = 0; j < size; j++)
			printf("%6.2f  ", m.element[i][j]);
		printf("\n");
	}
}



void work()
{
	matrix a, b, result1, result2;
	long long before, after;
	int correct, i, j, dim;
	cudaError_t rc;

	// Allocate memory for matrices
	allocate_matrix(&a);
	allocate_matrix(&b);
	allocate_matrix(&result1);
    allocate_matrix(&result2);

	// Initialize matrix elements
	init_matrix(a);
    init_matrix(b);
    // print_matrix(a);
    // printf("\n");
    // print_matrix(b);
    // printf("\n");

	// Perform sequential matrix multiplication
	before = wall_clock_time();
	mm(a, b, result1);
	after = wall_clock_time();
        fprintf(stderr, "Matrix multiplication on CPU took %1.2f seconds\n", ((float)(after - before))/1000000000);
    // print_matrix(result1);

	// Perform CUDA matrix  multiplication
	dim3 block(BLOCK_SIZE, BLOCK_SIZE);			// a block of 32 x 32 CUDA threads
	dim = (size % BLOCK_SIZE == 0) ? size / BLOCK_SIZE : size / BLOCK_SIZE + 1; 
	dim3 grid(dim, dim);	// a grid of CUDA thread blocks
	before = wall_clock_time();
	mm_kernel<<<grid, block>>>(a, b, result2, size);
	cudaDeviceSynchronize();
	after = wall_clock_time();
	fprintf(stderr, "Matrix multiplication on GPU took %1.2f seconds\n", ((float)(after - before))/1000000000);
    // print_matrix(result2);

	// was there any error?
        rc = cudaGetLastError();
        if (rc != cudaSuccess)
                printf("Last CUDA error %s\n", cudaGetErrorString(rc));

	// Compare the results
	correct = 1;
	for (i = 0; correct && i < size; i++)
		for (j = 0; j < size; j++)
			if (result1.element[i][j] != result2.element[i][j]) {
                printf("correct: %f, actual: %f\n", result1.element[i][j], result2.element[i][j]);
				correct = 0;
				break;
			}

	if (correct)
		printf("The result matrices are identical!\n");
	else
		printf("Difference in result matrices at element (%d, %d)!\n", i, j);

	free_matrix(&a);
	free_matrix(&b);
	free_matrix(&result1);
	free_matrix(&result2);
}


int main(int argc, char ** argv)
{
	srand(0); 

	printf("Usage: %s <size>\n", argv[0]);
    
	if (argc >= 2)
		size = atoi(argv[1]);
	else
		size = 1024;
		
	fprintf(stderr,"Sequential matrix multiplication of size %d\n", size);
    
	// Multiply the matrices
	work();

	return 0;
}

	.file	"tmpxft_0023afdc_00000000-6_mm-sm.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2041:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2041:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z15wall_clock_timev
	.type	_Z15wall_clock_timev, @function
_Z15wall_clock_timev:
.LFB2027:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	xorl	%edi, %edi
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rsi
	call	clock_gettime@PLT
	imulq	$1000000000, 8(%rsp), %rax
	addq	16(%rsp), %rax
	movq	24(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L3
	call	__stack_chk_fail@PLT
.L3:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z15wall_clock_timev, .-_Z15wall_clock_timev
	.globl	_Z10getElement6matrixii
	.type	_Z10getElement6matrixii, @function
_Z10getElement6matrixii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z10getElement6matrixii, .-_Z10getElement6matrixii
	.globl	_Z10setElement6matrixiif
	.type	_Z10setElement6matrixiif, @function
_Z10setElement6matrixiif:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z10setElement6matrixiif, .-_Z10setElement6matrixiif
	.globl	_Z12getSubMatrix6matrixii
	.type	_Z12getSubMatrix6matrixii, @function
_Z12getSubMatrix6matrixii:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2030:
	.size	_Z12getSubMatrix6matrixii, .-_Z12getSubMatrix6matrixii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"CUDA error: %s\n"
	.text
	.globl	_Z15allocate_matrixP6matrix
	.type	_Z15allocate_matrixP6matrix, @function
_Z15allocate_matrixP6matrix:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	pushq	%rdx
	.cfi_def_cfa_offset 32
	movslq	size(%rip), %rsi
	movl	$1, %edx
	salq	$3, %rsi
	call	cudaMallocManaged@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	jne	.L22
	xorl	%ebx, %ebx
.L13:
	movslq	size(%rip), %rsi
	cmpl	%ebx, %esi
	jle	.L23
	movq	0(%rbp), %rax
	salq	$2, %rsi
	movl	$1, %edx
	leaq	(%rax,%rbx,8), %rdi
	incq	%rbx
	call	cudaMallocManaged@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L13
.L22:
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC0(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L23:
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z15allocate_matrixP6matrix, .-_Z15allocate_matrixP6matrix
	.globl	_Z11free_matrixP6matrix
	.type	_Z11free_matrixP6matrix, @function
_Z11free_matrixP6matrix:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 32
.L25:
	cmpl	%ebx, size(%rip)
	movq	0(%rbp), %rdi
	jle	.L28
	movq	(%rdi,%rbx,8), %rdi
	incq	%rbx
	call	cudaFree@PLT
	jmp	.L25
.L28:
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	cudaFree@PLT
	.cfi_endproc
.LFE2032:
	.size	_Z11free_matrixP6matrix, .-_Z11free_matrixP6matrix
	.globl	_Z11init_matrix6matrix
	.type	_Z11init_matrix6matrix, @function
_Z11init_matrix6matrix:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movl	$10, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
.L30:
	cmpl	%ebp, size(%rip)
	jle	.L29
	leaq	0(,%rbp,8), %r14
	xorl	%ebx, %ebx
.L33:
	cmpl	%ebx, size(%rip)
	jle	.L35
	call	rand@PLT
	movq	(%r12,%r14), %rcx
	cltd
	idivl	%r13d
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, (%rcx,%rbx,4)
	incq	%rbx
	jmp	.L33
.L35:
	incq	%rbp
	jmp	.L30
.L29:
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2033:
	.size	_Z11init_matrix6matrix, .-_Z11init_matrix6matrix
	.globl	_Z16init_matrix_zero6matrix
	.type	_Z16init_matrix_zero6matrix, @function
_Z16init_matrix_zero6matrix:
.LFB2034:
	.cfi_startproc
	endbr64
	movl	size(%rip), %r8d
	movq	%rdi, %rsi
	xorl	%edx, %edx
	leal	-1(%r8), %eax
	leaq	4(,%rax,4), %r9
	xorl	%eax, %eax
.L37:
	cmpl	%edx, %r8d
	jle	.L39
	movq	(%rsi,%rdx,8), %rdi
	movq	%r9, %rcx
	incq	%rdx
	rep stosb
	jmp	.L37
.L39:
	ret
	.cfi_endproc
.LFE2034:
	.size	_Z16init_matrix_zero6matrix, .-_Z16init_matrix_zero6matrix
	.globl	_Z2mm6matrixS_S_
	.type	_Z2mm6matrixS_S_, @function
_Z2mm6matrixS_S_:
.LFB2035:
	.cfi_startproc
	endbr64
	movl	size(%rip), %r8d
	xorl	%ecx, %ecx
	movq	%rsi, %r9
	cmpl	%ecx, %r8d
	jg	.L51
	ret
.L51:
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
.L49:
	movq	(%rdx,%rcx,8), %r10
	movq	(%rdi,%rcx,8), %r11
	xorl	%eax, %eax
.L45:
	leaq	0(,%rax,4), %rbx
	xorl	%esi, %esi
.L42:
	movq	(%r9,%rsi,8), %r14
	movss	(%r14,%rbx), %xmm0
	mulss	(%r11,%rsi,4), %xmm0
	incq	%rsi
	addss	(%r10,%rax,4), %xmm0
	movss	%xmm0, (%r10,%rax,4)
	cmpl	%esi, %r8d
	jg	.L42
	incq	%rax
	cmpl	%eax, %r8d
	jg	.L45
	incq	%rcx
	cmpl	%ecx, %r8d
	jg	.L49
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2035:
	.size	_Z2mm6matrixS_S_, .-_Z2mm6matrixS_S_
	.section	.rodata.str1.1
.LC1:
	.string	"row %4d: "
.LC2:
	.string	"%6.2f  "
.LC3:
	.string	"\n"
	.text
	.globl	_Z12print_matrix6matrix
	.type	_Z12print_matrix6matrix, @function
_Z12print_matrix6matrix:
.LFB2036:
	.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
	leaq	.LC1(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 64
.L53:
	cmpl	%ebx, size(%rip)
	movl	%ebx, %edx
	jle	.L58
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%ebp, %ebp
	call	__printf_chk@PLT
	leaq	0(,%rbx,8), %r14
	leaq	.LC2(%rip), %r15
.L54:
	cmpl	%ebp, size(%rip)
	jle	.L59
	movq	(%r12,%r14), %rax
	movq	%r15, %rsi
	movl	$2, %edi
	cvtss2sd	(%rax,%rbp,4), %xmm0
	movb	$1, %al
	incq	%rbp
	call	__printf_chk@PLT
	jmp	.L54
.L59:
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbx
	call	__printf_chk@PLT
	jmp	.L53
.L58:
	popq	%rax
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2036:
	.size	_Z12print_matrix6matrix, .-_Z12print_matrix6matrix
	.globl	_Z38__device_stub__Z9mm_kernel6matrixS_S_iR6matrixS0_S0_i
	.type	_Z38__device_stub__Z9mm_kernel6matrixS_S_iR6matrixS0_S0_i, @function
_Z38__device_stub__Z9mm_kernel6matrixS_S_iR6matrixS0_S0_i:
.LFB2063:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movl	%ecx, 12(%rsp)
	leaq	32(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	12(%rsp), %rax
	movq	%rdi, 88(%rsp)
	leaq	40(%rsp), %rdi
	movq	%rsi, 96(%rsp)
	leaq	52(%rsp), %rsi
	movq	%rdx, 104(%rsp)
	leaq	24(%rsp), %rdx
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movl	$1, 48(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L60
	pushq	32(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z9mm_kernel6matrixS_S_i(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 160
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L60:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L62
	call	__stack_chk_fail@PLT
.L62:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2063:
	.size	_Z38__device_stub__Z9mm_kernel6matrixS_S_iR6matrixS0_S0_i, .-_Z38__device_stub__Z9mm_kernel6matrixS_S_iR6matrixS0_S0_i
	.globl	_Z9mm_kernel6matrixS_S_i
	.type	_Z9mm_kernel6matrixS_S_i, @function
_Z9mm_kernel6matrixS_S_i:
.LFB2064:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	%rdi, 24(%rsp)
	leaq	24(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	16(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	8(%rsp), %rdx
	call	_Z38__device_stub__Z9mm_kernel6matrixS_S_iR6matrixS0_S0_i
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2064:
	.size	_Z9mm_kernel6matrixS_S_i, .-_Z9mm_kernel6matrixS_S_i
	.section	.rodata.str1.1
.LC5:
	.string	"Matrix multiplication on CPU took %1.2f seconds\n"
.LC6:
	.string	"Matrix multiplication on GPU took %1.2f seconds\n"
.LC7:
	.string	"Last CUDA error %s\n"
.LC8:
	.string	"correct: %f, actual: %f\n"
.LC9:
	.string	"Difference in result matrices at element (%d, %d)!\n"
.LC10:
	.string	"The result matrices are identical!\n"
	.text
	.globl	_Z4workv
	.type	_Z4workv, @function
_Z4workv:
.LFB2037:
	.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	$104, %rsp
	.cfi_def_cfa_offset 160
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %r13
	leaq	16(%rsp), %r12
	movq	%r13, %rdi
	leaq	24(%rsp), %rbp
	leaq	32(%rsp), %rbx
	call	_Z15allocate_matrixP6matrix
	movq	%r12, %rdi
	call	_Z15allocate_matrixP6matrix
	movq	%rbp, %rdi
	call	_Z15allocate_matrixP6matrix
	movq	%rbx, %rdi
	call	_Z15allocate_matrixP6matrix
	movq	8(%rsp), %rdi
	call	_Z11init_matrix6matrix
	movq	16(%rsp), %rdi
	call	_Z11init_matrix6matrix
	call	_Z15wall_clock_timev
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdi
	movq	%rax, %r14
	call	_Z2mm6matrixS_S_
	call	_Z15wall_clock_timev
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC5(%rip), %rdx
	subq	%r14, %rax
	cvtsi2ssq	%rax, %xmm0
	movb	$1, %al
	divss	.LC4(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__fprintf_chk@PLT
	movl	$1, 72(%rsp)
	movl	$32, %ecx
	movabsq	$137438953504, %rax
	movq	%rax, 64(%rsp)
	movl	size(%rip), %eax
	cltd
	testb	$31, %al
	jne	.L67
	idivl	%ecx
	jmp	.L68
.L67:
	idivl	%ecx
	incl	%eax
.L68:
	movl	%eax, 76(%rsp)
	movl	%eax, 80(%rsp)
	movl	$1, 84(%rsp)
	call	_Z15wall_clock_timev
	movl	72(%rsp), %ecx
	movl	84(%rsp), %esi
	xorl	%r9d, %r9d
	movq	64(%rsp), %rdx
	movq	76(%rsp), %rdi
	xorl	%r8d, %r8d
	movq	%rax, %r14
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L69
	movq	8(%rsp), %rax
	movl	size(%rip), %ecx
	leaq	56(%rsp), %rdx
	leaq	48(%rsp), %rsi
	leaq	40(%rsp), %rdi
	movq	%rax, 40(%rsp)
	movq	16(%rsp), %rax
	movq	%rax, 48(%rsp)
	movq	32(%rsp), %rax
	movq	%rax, 56(%rsp)
	call	_Z38__device_stub__Z9mm_kernel6matrixS_S_iR6matrixS0_S0_i
.L69:
	call	cudaDeviceSynchronize@PLT
	call	_Z15wall_clock_timev
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC6(%rip), %rdx
	subq	%r14, %rax
	cvtsi2ssq	%rax, %xmm0
	movb	$1, %al
	divss	.LC4(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__fprintf_chk@PLT
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L70
	call	cudaGetErrorString@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L70:
	movl	size(%rip), %eax
	movq	24(%rsp), %rdx
	xorl	%r14d, %r14d
	movq	32(%rsp), %rcx
.L71:
	cmpl	%r14d, %eax
	jle	.L84
	xorl	%r15d, %r15d
.L72:
	cmpl	%r15d, %eax
	jle	.L85
	movq	(%rdx,%r14,8), %rsi
	movss	(%rsi,%r15,4), %xmm0
	movq	(%rcx,%r14,8), %rsi
	movss	(%rsi,%r15,4), %xmm1
	leaq	1(%r15), %rsi
	ucomiss	%xmm1, %xmm0
	jp	.L79
	je	.L77
.L79:
	movl	$2, %edi
	movb	$2, %al
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	leaq	.LC8(%rip), %rsi
	call	__printf_chk@PLT
	leal	1(%r14), %edx
	movl	%r15d, %ecx
	xorl	%eax, %eax
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L74
.L77:
	movq	%rsi, %r15
	jmp	.L72
.L85:
	incq	%r14
	jmp	.L71
.L84:
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L74:
	movq	%r13, %rdi
	call	_Z11free_matrixP6matrix
	movq	%r12, %rdi
	call	_Z11free_matrixP6matrix
	movq	%rbp, %rdi
	call	_Z11free_matrixP6matrix
	movq	%rbx, %rdi
	call	_Z11free_matrixP6matrix
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L76
	call	__stack_chk_fail@PLT
.L76:
	addq	$104, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2037:
	.size	_Z4workv, .-_Z4workv
	.section	.rodata.str1.1
.LC11:
	.string	"Usage: %s <size>\n"
.LC12:
	.string	"Sequential matrix multiplication of size %d\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2038:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%edi, %ebp
	xorl	%edi, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rsi, %rbx
	pushq	%rcx
	.cfi_def_cfa_offset 32
	call	srand@PLT
	movq	(%rbx), %rdx
	xorl	%eax, %eax
	movl	$2, %edi
	leaq	.LC11(%rip), %rsi
	call	__printf_chk@PLT
	decl	%ebp
	movl	$1024, %ecx
	jle	.L87
	movq	8(%rbx), %rdi
	call	atoi@PLT
	movl	%eax, %ecx
.L87:
	movq	stderr(%rip), %rdi
	leaq	.LC12(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	movl	%ecx, size(%rip)
	call	__fprintf_chk@PLT
	call	_Z4workv
	popq	%rdx
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2038:
	.size	main, .-main
	.section	.rodata.str1.1
.LC13:
	.string	"_Z9mm_kernel6matrixS_S_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2066:
	.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	.LC13(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z9mm_kernel6matrixS_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
.LFE2066:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.globl	size
	.bss
	.align 4
	.type	size, @object
	.size	size, 4
size:
	.zero	4
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC4:
	.long	1315859240
	.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:

/**
 * 
 * Matrix Multiplication - CUDA for GPUs
 *
 * CS3210
 *
 **/

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

#define BLOCK_SIZE 32

int size;

typedef struct
{
	float **element;
} matrix;

long long wall_clock_time()
{
#ifdef __linux__
	struct timespec tp;
	clock_gettime(CLOCK_REALTIME, &tp);
	return (long long)(tp.tv_nsec + (long long)tp.tv_sec * 1000000000ll);
#else
	struct timeval tv;
	gettimeofday(&tv, NULL);
	return (long long)(tv.tv_usec * 1000 + (long long)tv.tv_sec * 1000000000ll);
#endif
}

__device__ float getElement(matrix A, int row, int col) {
    return A.element[row][col];
}

__device__ void setElement(matrix A, int row, int col, float value) {
    A.element[row][col] = value;
}

__device__ matrix getSubMatrix(matrix A, int blockRow, int blockCol) {
    int startingRow = BLOCK_SIZE * blockRow;
    int startingCol = BLOCK_SIZE * blockCol;

    // Allocate memory for sub matrix
    matrix subA;
    subA.element = (float**)malloc(sizeof(float*) * BLOCK_SIZE);
    int row;
    for (row = 0; row < BLOCK_SIZE; row++) {
        // subA.element[row] = (float*)malloc(sizeof(float) * BLOCK_SIZE);
        subA.element[row] = A.element[startingRow + row] + startingCol;
    }

    // int row, col;
    // for (row = 0; row < BLOCK_SIZE; row++) {
    //     subA.element[row] = A.element[startingRow + row] + startingCol;
    //     // for (col = 0; col < BLOCK_SIZE; col++) {
    //     //     printf("%f ", A.element[startingRow + row][startingCol + col]);
    //     // }
    //     // printf("\n");
    // }

    // int i, j;
    // for (i = 0; i < BLOCK_SIZE; i++) {
    //     for (j = 0; j < BLOCK_SIZE; j++) {
    //         printf("%f ", subA.element[i][j]);
    //     }
    //     printf("\n");
    // }
    return subA;
}

/**
 * Allocates memory for a matrix of size SIZE
 * The memory is allocated row-major order, i.e. 
 *  elements from the same row are allocated at contiguous 
 *  memory addresses.
 **/
void allocate_matrix(matrix* m)
{
	int i;
	hipError_t rc;
	
	// allocate array for all the rows
	rc = hipMallocManaged((void**)&(m->element), sizeof(float*) * size);
	if (rc != hipSuccess)
	{
		fprintf(stderr, "CUDA error: %s\n", hipGetErrorString(rc));
		exit(1);
	}
	
	// allocate an array for each row of the matrix
	for (i = 0; i < size; i++)
	{
		rc = hipMallocManaged((void**)&(m->element[i]), sizeof(float) * size);
		if (rc != hipSuccess)
		{
			fprintf(stderr, "CUDA error: %s\n", hipGetErrorString(rc));
			exit(1);
		}
	}
}

/**
 * Free the memory allocated for a matrix.
 **/
void free_matrix(matrix* m) {
	int i;
	for (i = 0; i < size; i++)
		hipFree(m->element[i]);
	hipFree(m->element);
}

/**
 * Initializes the elements of the matrix with
 * random values between 0 and 9
 **/
void init_matrix(matrix m)
{
	int i, j;
	
	for (i = 0; i < size; i++)
		for (j = 0; j < size; j++)
		{
			m.element[i][j] = rand() % 10;
		}
}

/**
 * Initializes the elements of the matrix with
 * element 0.
 **/
void init_matrix_zero(matrix m)
{
	int i, j;
	
	for (i = 0; i < size; i++)
		for (j = 0; j < size; j++)
		{
			m.element[i][j] = 0.0;
		}
}


/**
 * Multiplies matrix @a with matrix @b storing
 * the result in matrix @result
 * 
 * The multiplication algorithm is the O(n^3) 
 * algorithm
 */
void mm(matrix a, matrix b, matrix result)
{
	int i, j, k;
	
	// Do the multiplication
	for (i = 0; i < size; i++)
		for (j = 0; j < size; j++)
			for(k = 0; k < size; k++)
				result.element[i][j] += a.element[i][k] * b.element[k][j];
}

/**
 * Each kernel computes the result element (i,j).
 */
__global__ void mm_kernel(matrix a, matrix b, matrix result, int size)
{
	int i = blockIdx.y * blockDim.y + threadIdx.y;
	int j = blockIdx.x * blockDim.x + threadIdx.x; 
	int k;
    float resultValue = 0;

	// if (i >= size || j >= size)
	// 		return;

    for (k = 0; k < (size / BLOCK_SIZE); k++) {
        __shared__ float sharedA[BLOCK_SIZE][BLOCK_SIZE];
        __shared__ float sharedB[BLOCK_SIZE][BLOCK_SIZE];

        sharedA[threadIdx.y][threadIdx.x] = a.element[i][k * BLOCK_SIZE + threadIdx.x];
        sharedB[threadIdx.y][threadIdx.x] = b.element[k * BLOCK_SIZE + threadIdx.y][j];

        __syncthreads();

        int i;
        for (i = 0; i < BLOCK_SIZE; i++) {
            resultValue += sharedA[threadIdx.y][i] * sharedB[i][threadIdx.x];
        }

        __syncthreads();
	}
	
	result.element[i][j] = resultValue;
}

void print_matrix(matrix m)
{
	int i, j;
	
	for (i = 0; i < size; i++)
	{
		printf("row %4d: ", i);
		for (j = 0; j < size; j++)
			printf("%6.2f  ", m.element[i][j]);
		printf("\n");
	}
}



void work()
{
	matrix a, b, result1, result2;
	long long before, after;
	int correct, i, j, dim;
	hipError_t rc;

	// Allocate memory for matrices
	allocate_matrix(&a);
	allocate_matrix(&b);
	allocate_matrix(&result1);
    allocate_matrix(&result2);

	// Initialize matrix elements
	init_matrix(a);
    init_matrix(b);
    // print_matrix(a);
    // printf("\n");
    // print_matrix(b);
    // printf("\n");

	// Perform sequential matrix multiplication
	before = wall_clock_time();
	mm(a, b, result1);
	after = wall_clock_time();
        fprintf(stderr, "Matrix multiplication on CPU took %1.2f seconds\n", ((float)(after - before))/1000000000);
    // print_matrix(result1);

	// Perform CUDA matrix  multiplication
	dim3 block(BLOCK_SIZE, BLOCK_SIZE);			// a block of 32 x 32 CUDA threads
	dim = (size % BLOCK_SIZE == 0) ? size / BLOCK_SIZE : size / BLOCK_SIZE + 1; 
	dim3 grid(dim, dim);	// a grid of CUDA thread blocks
	before = wall_clock_time();
	mm_kernel<<<grid, block>>>(a, b, result2, size);
	hipDeviceSynchronize();
	after = wall_clock_time();
	fprintf(stderr, "Matrix multiplication on GPU took %1.2f seconds\n", ((float)(after - before))/1000000000);
    // print_matrix(result2);

	// was there any error?
        rc = hipGetLastError();
        if (rc != hipSuccess)
                printf("Last CUDA error %s\n", hipGetErrorString(rc));

	// Compare the results
	correct = 1;
	for (i = 0; correct && i < size; i++)
		for (j = 0; j < size; j++)
			if (result1.element[i][j] != result2.element[i][j]) {
                printf("correct: %f, actual: %f\n", result1.element[i][j], result2.element[i][j]);
				correct = 0;
				break;
			}

	if (correct)
		printf("The result matrices are identical!\n");
	else
		printf("Difference in result matrices at element (%d, %d)!\n", i, j);

	free_matrix(&a);
	free_matrix(&b);
	free_matrix(&result1);
	free_matrix(&result2);
}


int main(int argc, char ** argv)
{
	srand(0); 

	printf("Usage: %s <size>\n", argv[0]);
    
	if (argc >= 2)
		size = atoi(argv[1]);
	else
		size = 1024;
		
	fprintf(stderr,"Sequential matrix multiplication of size %d\n", size);
    
	// Multiply the matrices
	work();

	return 0;
}

	.text
	.file	"mm-sm.hip"
	.globl	_Z15wall_clock_timev            # -- Begin function _Z15wall_clock_timev
	.type	_Z15wall_clock_timev,@function
_Z15wall_clock_timev:                   # @_Z15wall_clock_timev
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	movq	%rsp, %rbx
	xorl	%edi, %edi
	movq	%rbx, %rsi
	callq	clock_gettime
	imulq	$1000000000, (%rbx), %rax       # imm = 0x3B9ACA00
	addq	8(%rbx), %rax
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z15wall_clock_timev, .Lfunc_end0-_Z15wall_clock_timev
	.cfi_endproc
                                        # -- End function
	.globl	_Z15allocate_matrixP6matrix     # -- Begin function _Z15allocate_matrixP6matrix
	.type	_Z15allocate_matrixP6matrix,@function
_Z15allocate_matrixP6matrix:            # @_Z15allocate_matrixP6matrix
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movslq	size(%rip), %rsi
	shlq	$3, %rsi
	movl	$1, %edx
	callq	hipMallocManaged
	testl	%eax, %eax
	jne	.LBB1_5
# %bb.1:                                # %.preheader
	movl	size(%rip), %eax
	testl	%eax, %eax
	jle	.LBB1_6
# %bb.2:                                # %.lr.ph.preheader
	xorl	%r14d, %r14d
	xorl	%r15d, %r15d
.LBB1_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	(%rbx), %rdi
	addq	%r14, %rdi
	movslq	%eax, %rsi
	shlq	$2, %rsi
	movl	$1, %edx
	callq	hipMallocManaged
	testl	%eax, %eax
	jne	.LBB1_5
# %bb.3:                                #   in Loop: Header=BB1_4 Depth=1
	incq	%r15
	movslq	size(%rip), %rax
	addq	$8, %r14
	cmpq	%rax, %r15
	jl	.LBB1_4
.LBB1_6:                                # %._crit_edge
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB1_5:
	.cfi_def_cfa_offset 32
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %esi
	movq	%rbx, %rdi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z15allocate_matrixP6matrix, .Lfunc_end1-_Z15allocate_matrixP6matrix
	.cfi_endproc
                                        # -- End function
	.globl	_Z11free_matrixP6matrix         # -- Begin function _Z11free_matrixP6matrix
	.type	_Z11free_matrixP6matrix,@function
_Z11free_matrixP6matrix:                # @_Z11free_matrixP6matrix
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rdi, %rbx
	cmpl	$0, size(%rip)
	jle	.LBB2_3
# %bb.1:                                # %.lr.ph.preheader
	xorl	%r14d, %r14d
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	(%rbx), %rax
	movq	(%rax,%r14,8), %rdi
	callq	hipFree
	incq	%r14
	movslq	size(%rip), %rax
	cmpq	%rax, %r14
	jl	.LBB2_2
.LBB2_3:                                # %._crit_edge
	movq	(%rbx), %rdi
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	hipFree                         # TAILCALL
.Lfunc_end2:
	.size	_Z11free_matrixP6matrix, .Lfunc_end2-_Z11free_matrixP6matrix
	.cfi_endproc
                                        # -- End function
	.globl	_Z11init_matrix6matrix          # -- Begin function _Z11init_matrix6matrix
	.type	_Z11init_matrix6matrix,@function
_Z11init_matrix6matrix:                 # @_Z11init_matrix6matrix
	.cfi_startproc
# %bb.0:
	movl	size(%rip), %eax
	testl	%eax, %eax
	jle	.LBB3_8
# %bb.1:                                # %.preheader.preheader
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	xorl	%r14d, %r14d
.LBB3_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_5 Depth 2
	testl	%eax, %eax
	jle	.LBB3_3
# %bb.4:                                # %.lr.ph
                                        #   in Loop: Header=BB3_2 Depth=1
	xorl	%r15d, %r15d
.LBB3_5:                                #   Parent Loop BB3_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	cltq
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$34, %rcx
	addl	%edx, %ecx
	addl	%ecx, %ecx
	leal	(%rcx,%rcx,4), %ecx
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movq	(%rbx,%r14,8), %rax
	movss	%xmm0, (%rax,%r15,4)
	incq	%r15
	movl	size(%rip), %eax
	movslq	%eax, %rcx
	cmpq	%rcx, %r15
	jl	.LBB3_5
	jmp	.LBB3_6
.LBB3_3:                                # %.preheader.._crit_edge_crit_edge
                                        #   in Loop: Header=BB3_2 Depth=1
	movslq	%eax, %rcx
.LBB3_6:                                # %._crit_edge
                                        #   in Loop: Header=BB3_2 Depth=1
	incq	%r14
	cmpq	%rcx, %r14
	jl	.LBB3_2
# %bb.7:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
.LBB3_8:                                # %._crit_edge9
	retq
.Lfunc_end3:
	.size	_Z11init_matrix6matrix, .Lfunc_end3-_Z11init_matrix6matrix
	.cfi_endproc
                                        # -- End function
	.globl	_Z16init_matrix_zero6matrix     # -- Begin function _Z16init_matrix_zero6matrix
	.type	_Z16init_matrix_zero6matrix,@function
_Z16init_matrix_zero6matrix:            # @_Z16init_matrix_zero6matrix
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	size(%rip), %r15d
	testl	%r15d, %r15d
	jle	.LBB4_3
# %bb.1:                                # %.preheader.lr.ph
	movq	%rdi, %rbx
	leaq	(,%r15,4), %r14
	xorl	%r12d, %r12d
.LBB4_2:                                # %._crit_edge
                                        # =>This Inner Loop Header: Depth=1
	movq	(%rbx,%r12,8), %rdi
	xorl	%esi, %esi
	movq	%r14, %rdx
	callq	memset@PLT
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB4_2
.LBB4_3:                                # %._crit_edge9
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z16init_matrix_zero6matrix, .Lfunc_end4-_Z16init_matrix_zero6matrix
	.cfi_endproc
                                        # -- End function
	.globl	_Z2mm6matrixS_S_                # -- Begin function _Z2mm6matrixS_S_
	.type	_Z2mm6matrixS_S_,@function
_Z2mm6matrixS_S_:                       # @_Z2mm6matrixS_S_
	.cfi_startproc
# %bb.0:
	movl	size(%rip), %eax
	testl	%eax, %eax
	jle	.LBB5_8
# %bb.1:                                # %.preheader16.lr.ph
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	xorl	%ecx, %ecx
.LBB5_2:                                # %.preheader.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB5_3 Depth 2
                                        #       Child Loop BB5_4 Depth 3
	movq	(%rdi,%rcx,8), %r8
	movq	(%rdx,%rcx,8), %r9
	xorl	%r10d, %r10d
.LBB5_3:                                # %.preheader
                                        #   Parent Loop BB5_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB5_4 Depth 3
	movss	(%r9,%r10,4), %xmm0             # xmm0 = mem[0],zero,zero,zero
	xorl	%r11d, %r11d
.LBB5_4:                                #   Parent Loop BB5_2 Depth=1
                                        #     Parent Loop BB5_3 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%r8,%r11,4), %xmm1             # xmm1 = mem[0],zero,zero,zero
	movq	(%rsi,%r11,8), %rbx
	mulss	(%rbx,%r10,4), %xmm1
	addss	%xmm1, %xmm0
	movss	%xmm0, (%r9,%r10,4)
	incq	%r11
	cmpq	%r11, %rax
	jne	.LBB5_4
# %bb.5:                                # %._crit_edge
                                        #   in Loop: Header=BB5_3 Depth=2
	incq	%r10
	cmpq	%rax, %r10
	jne	.LBB5_3
# %bb.6:                                # %._crit_edge19
                                        #   in Loop: Header=BB5_2 Depth=1
	incq	%rcx
	cmpq	%rax, %rcx
	jne	.LBB5_2
# %bb.7:
	popq	%rbx
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
.LBB5_8:                                # %._crit_edge21
	retq
.Lfunc_end5:
	.size	_Z2mm6matrixS_S_, .Lfunc_end5-_Z2mm6matrixS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z24__device_stub__mm_kernel6matrixS_S_i # -- Begin function _Z24__device_stub__mm_kernel6matrixS_S_i
	.type	_Z24__device_stub__mm_kernel6matrixS_S_i,@function
_Z24__device_stub__mm_kernel6matrixS_S_i: # @_Z24__device_stub__mm_kernel6matrixS_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	$_Z9mm_kernel6matrixS_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_end6:
	.size	_Z24__device_stub__mm_kernel6matrixS_S_i, .Lfunc_end6-_Z24__device_stub__mm_kernel6matrixS_S_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z12print_matrix6matrix         # -- Begin function _Z12print_matrix6matrix
	.type	_Z12print_matrix6matrix,@function
_Z12print_matrix6matrix:                # @_Z12print_matrix6matrix
	.cfi_startproc
# %bb.0:
	cmpl	$0, size(%rip)
	jle	.LBB7_7
# %bb.1:                                # %.lr.ph11.preheader
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	xorl	%r14d, %r14d
.LBB7_2:                                # %.lr.ph11
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB7_4 Depth 2
	movl	$.L.str.1, %edi
	movl	%r14d, %esi
	xorl	%eax, %eax
	callq	printf
	cmpl	$0, size(%rip)
	jle	.LBB7_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB7_2 Depth=1
	xorl	%r15d, %r15d
.LBB7_4:                                #   Parent Loop BB7_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	(%rbx,%r14,8), %rax
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rax,%r15,4), %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	incq	%r15
	movslq	size(%rip), %rax
	cmpq	%rax, %r15
	jl	.LBB7_4
.LBB7_5:                                # %._crit_edge
                                        #   in Loop: Header=BB7_2 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r14
	movslq	size(%rip), %rax
	cmpq	%rax, %r14
	jl	.LBB7_2
# %bb.6:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
.LBB7_7:                                # %._crit_edge12
	retq
.Lfunc_end7:
	.size	_Z12print_matrix6matrix, .Lfunc_end7-_Z12print_matrix6matrix
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z4workv
.LCPI8_0:
	.long	0x4e6e6b28                      # float 1.0E+9
	.text
	.globl	_Z4workv
	.type	_Z4workv,@function
_Z4workv:                               # @_Z4workv
	.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	$48, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rbx
	movq	%rbx, %rdi
	callq	_Z15allocate_matrixP6matrix
	leaq	16(%rsp), %r14
	movq	%r14, %rdi
	callq	_Z15allocate_matrixP6matrix
	leaq	8(%rsp), %r15
	movq	%r15, %rdi
	callq	_Z15allocate_matrixP6matrix
	movq	%rsp, %rdi
	callq	_Z15allocate_matrixP6matrix
	movq	(%rbx), %rdi
	callq	_Z11init_matrix6matrix
	movq	(%r14), %rdi
	callq	_Z11init_matrix6matrix
	leaq	32(%rsp), %r12
	xorl	%edi, %edi
	movq	%r12, %rsi
	callq	clock_gettime
	movq	(%r12), %r13
	movq	8(%r12), %r12
	movq	(%rbx), %rdi
	movq	(%r14), %rsi
	movq	(%r15), %rdx
	callq	_Z2mm6matrixS_S_
	leaq	32(%rsp), %rbx
	xorl	%edi, %edi
	movq	%rbx, %rsi
	callq	clock_gettime
	movq	(%rbx), %rax
	subq	%r13, %rax
	movq	8(%rbx), %rcx
	subq	%r12, %rcx
	imulq	$1000000000, %rax, %rax         # imm = 0x3B9ACA00
	addq	%rcx, %rax
	cvtsi2ss	%rax, %xmm0
	movq	stderr(%rip), %rdi
	divss	.LCPI8_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.4, %esi
	movb	$1, %al
	callq	fprintf
	movl	size(%rip), %eax
	testb	$31, %al
	jne	.LBB8_2
# %bb.1:
	sarl	$5, %eax
	jmp	.LBB8_3
.LBB8_2:
	leal	31(%rax), %ecx
	testl	%eax, %eax
	cmovnsl	%eax, %ecx
	sarl	$5, %ecx
	incl	%ecx
	movl	%ecx, %eax
.LBB8_3:
	movl	%eax, %eax
	movq	%rax, %rbx
	shlq	$32, %rbx
	orq	%rax, %rbx
	leaq	32(%rsp), %r14
	xorl	%edi, %edi
	movq	%r14, %rsi
	callq	clock_gettime
	movq	(%r14), %r15
	movq	8(%r14), %r14
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movq	%rbx, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB8_5
# %bb.4:
	movq	24(%rsp), %rdi
	movq	16(%rsp), %rsi
	movq	(%rsp), %rdx
	movl	size(%rip), %ecx
	callq	_Z24__device_stub__mm_kernel6matrixS_S_i
.LBB8_5:
	callq	hipDeviceSynchronize
	leaq	32(%rsp), %rbx
	xorl	%edi, %edi
	movq	%rbx, %rsi
	callq	clock_gettime
	movq	(%rbx), %rax
	subq	%r15, %rax
	movq	8(%rbx), %rcx
	subq	%r14, %rcx
	imulq	$1000000000, %rax, %rax         # imm = 0x3B9ACA00
	addq	%rcx, %rax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
	movq	stderr(%rip), %rdi
	divss	.LCPI8_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.5, %esi
	movb	$1, %al
	callq	fprintf
	callq	hipGetLastError
	testl	%eax, %eax
	je	.LBB8_7
# %bb.6:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.6, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
.LBB8_7:
	movl	size(%rip), %eax
	testl	%eax, %eax
	jle	.LBB8_13
# %bb.8:                                # %.preheader.preheader
	movq	8(%rsp), %rcx
	movq	(%rsp), %rdx
	xorl	%ebx, %ebx
.LBB8_9:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB8_10 Depth 2
	movq	(%rcx,%rbx,8), %rsi
	movq	(%rdx,%rbx,8), %rdi
	xorl	%r14d, %r14d
.LBB8_10:                               #   Parent Loop BB8_9 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rsi,%r14,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	(%rdi,%r14,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm1, %xmm0
	jne	.LBB8_14
	jp	.LBB8_14
# %bb.11:                               #   in Loop: Header=BB8_10 Depth=2
	incq	%r14
	cmpq	%r14, %rax
	jne	.LBB8_10
# %bb.12:                               # %.loopexit
                                        #   in Loop: Header=BB8_9 Depth=1
	incq	%rbx
	cmpq	%rax, %rbx
	jb	.LBB8_9
.LBB8_13:                               # %._crit_edge.thread
	movl	$.Lstr, %edi
	callq	puts@PLT
	jmp	.LBB8_15
.LBB8_14:
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	movl	$.L.str.7, %edi
	movb	$2, %al
	callq	printf
	incl	%ebx
	movl	$.L.str.9, %edi
	movl	%ebx, %esi
	movl	%r14d, %edx
	xorl	%eax, %eax
	callq	printf
.LBB8_15:
	leaq	24(%rsp), %rdi
	callq	_Z11free_matrixP6matrix
	leaq	16(%rsp), %rdi
	callq	_Z11free_matrixP6matrix
	leaq	8(%rsp), %rdi
	callq	_Z11free_matrixP6matrix
	movq	%rsp, %rdi
	callq	_Z11free_matrixP6matrix
	addq	$48, %rsp
	.cfi_def_cfa_offset 48
	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_end8:
	.size	_Z4workv, .Lfunc_end8-_Z4workv
	.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	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movl	%edi, %ebp
	xorl	%edi, %edi
	callq	srand
	movq	(%rbx), %rsi
	movl	$.L.str.10, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$1024, %edx                     # imm = 0x400
	cmpl	$2, %ebp
	jl	.LBB9_2
# %bb.1:
	movq	8(%rbx), %rdi
	callq	atoi
	movl	%eax, %edx
.LBB9_2:
	movl	%edx, size(%rip)
	movq	stderr(%rip), %rdi
	movl	$.L.str.11, %esi
	xorl	%eax, %eax
	callq	fprintf
	callq	_Z4workv
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end9:
	.size	main, .Lfunc_end9-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	.LBB10_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB10_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z9mm_kernel6matrixS_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_end10:
	.size	__hip_module_ctor, .Lfunc_end10-__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	.LBB11_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
.LBB11_2:
	retq
.Lfunc_end11:
	.size	__hip_module_dtor, .Lfunc_end11-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	size,@object                    # @size
	.bss
	.globl	size
	.p2align	2, 0x0
size:
	.long	0                               # 0x0
	.size	size, 4

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"CUDA error: %s\n"
	.size	.L.str, 16

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

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"row %4d: "
	.size	.L.str.1, 10

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%6.2f  "
	.size	.L.str.2, 8

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Matrix multiplication on CPU took %1.2f seconds\n"
	.size	.L.str.4, 49

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Matrix multiplication on GPU took %1.2f seconds\n"
	.size	.L.str.5, 49

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Last CUDA error %s\n"
	.size	.L.str.6, 20

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"correct: %f, actual: %f\n"
	.size	.L.str.7, 25

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Difference in result matrices at element (%d, %d)!\n"
	.size	.L.str.9, 52

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Usage: %s <size>\n"
	.size	.L.str.10, 18

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"Sequential matrix multiplication of size %d\n"
	.size	.L.str.11, 45

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z9mm_kernel6matrixS_S_i"
	.size	.L__unnamed_1, 25

	.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	"The result matrices are identical!"
	.size	.Lstr, 35

	.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__mm_kernel6matrixS_S_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z9mm_kernel6matrixS_S_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z9mm_kernel6matrixS_S_i ; -- Begin function _Z9mm_kernel6matrixS_S_i
	.globl	_Z9mm_kernel6matrixS_S_i
	.p2align	8
	.type	_Z9mm_kernel6matrixS_S_i,@function
_Z9mm_kernel6matrixS_S_i:               ; @_Z9mm_kernel6matrixS_S_i
; %bb.0:
	s_clause 0x3
	s_load_b32 s3, 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 v2, v0, 10, 10
	v_and_b32_e32 v3, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s8, s3, 16
	s_and_b32 s3, s3, 0xffff
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[4:5], null, s15, s8, v[2:3]
	s_cmp_gt_i32 s2, 31
	v_mad_u64_u32 v[0:1], null, s14, s3, v[3:4]
	v_ashrrev_i32_e32 v5, 31, v4
	s_delay_alu instid0(VALU_DEP_2)
	v_ashrrev_i32_e32 v1, 31, v0
	s_cbranch_scc1 .LBB0_2
; %bb.1:                                ; %.._crit_edge_crit_edge
	s_mov_b32 s8, 0
	s_mov_b32 s3, 0
	s_branch .LBB0_3
.LBB0_2:
	s_mov_b32 s8, -1
                                        ; implicit-def: $sgpr3
.LBB0_3:                                ; %Flow46
	s_delay_alu instid0(VALU_DEP_2)
	v_lshlrev_b64 v[4:5], 3, v[4:5]
	s_and_not1_b32 vcc_lo, exec_lo, s8
	s_cbranch_vccnz .LBB0_8
; %bb.4:                                ; %.lr.ph
	v_dual_mov_b32 v11, 0 :: v_dual_lshlrev_b32 v8, 2, v3
	v_dual_mov_b32 v12, 0 :: v_dual_lshlrev_b32 v13, 7, v2
	s_ashr_i32 s3, s2, 31
	s_delay_alu instid0(VALU_DEP_2)
	v_or_b32_e32 v14, 0x1000, v8
	v_add_co_u32 v6, vcc_lo, s4, v4
	s_lshr_b32 s3, s3, 27
	v_add_nc_u32_e32 v15, v13, v8
	v_lshlrev_b64 v[8:9], 2, v[0:1]
	v_add_co_ci_u32_e32 v7, vcc_lo, s5, v5, vcc_lo
	v_add_nc_u32_e32 v16, v14, v13
	s_add_i32 s2, s2, s3
	s_mov_b32 s3, 0
	s_ashr_i32 s2, s2, 5
.LBB0_5:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_6 Depth 2
	s_lshl_b32 s4, s3, 5
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v10, s4, v3
	v_lshlrev_b64 v[17:18], 2, v[10:11]
	v_add_nc_u32_e32 v10, s4, v2
	s_mov_b32 s4, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[19:20], 3, v[10:11]
	v_mov_b32_e32 v10, v14
	v_add_co_u32 v19, vcc_lo, s6, v19
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v20, vcc_lo, s7, v20, vcc_lo
	global_load_b64 v[21:22], v[6:7], off
	global_load_b64 v[19:20], v[19:20], off
	s_waitcnt vmcnt(1)
	v_add_co_u32 v17, vcc_lo, v21, v17
	v_add_co_ci_u32_e32 v18, vcc_lo, v22, v18, vcc_lo
	s_waitcnt vmcnt(0)
	v_add_co_u32 v19, vcc_lo, v19, v8
	v_add_co_ci_u32_e32 v20, vcc_lo, v20, v9, vcc_lo
	global_load_b32 v17, v[17:18], off
	global_load_b32 v18, v[19:20], off
	s_waitcnt vmcnt(1)
	ds_store_b32 v15, v17
	s_waitcnt vmcnt(0)
	ds_store_b32 v16, v18
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
.LBB0_6:                                ;   Parent Loop BB0_5 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	v_add_nc_u32_e32 v17, s4, v13
	s_add_i32 s4, s4, 4
	ds_load_b32 v18, v10
	ds_load_b32 v17, v17
	v_add_nc_u32_e32 v10, 0x80, v10
	s_cmpk_lg_i32 s4, 0x80
	s_waitcnt lgkmcnt(0)
	v_fmac_f32_e32 v12, v17, v18
	s_cbranch_scc1 .LBB0_6
; %bb.7:                                ;   in Loop: Header=BB0_5 Depth=1
	s_add_i32 s3, s3, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lg_u32 s3, s2
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_5
	s_branch .LBB0_9
.LBB0_8:
	v_mov_b32_e32 v12, s3
.LBB0_9:                                ; %._crit_edge
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_u32 v2, vcc_lo, s0, v4
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v5, vcc_lo
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	global_load_b64 v[2:3], v[2:3], off
	s_waitcnt vmcnt(0)
	v_add_co_u32 v0, vcc_lo, v2, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, v3, v1, vcc_lo
	global_store_b32 v[0:1], v12, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z9mm_kernel6matrixS_S_i
		.amdhsa_group_segment_fixed_size 8192
		.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 23
		.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	_Z9mm_kernel6matrixS_S_i, .Lfunc_end0-_Z9mm_kernel6matrixS_S_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 536
; NumSgprs: 18
; NumVgprs: 23
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 8192 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 23
; 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:         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: 8192
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z9mm_kernel6matrixS_S_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z9mm_kernel6matrixS_S_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     23
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
