// mmm_cuda.cu, Crispin Bernier, chb2ab
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

//----------------------------------- Structures and Globals---------------------------------------------

typedef struct {
	int dimension1;
	int dimension2;	
} ArrayMetadata2D;

// metadata variables describing dimensionalities of all data structures involved in the computation
ArrayMetadata2D A_MD, B_MD, C_MD;
// pointers for input and output arrays in the host memory  
float *A, *B, *C, *C_CPU;
// pointers for input and output arrays in the device memory (NVIDIA DRAM)
float *A_GPU, *B_GPU, *C_GPU;

//----------------------------------- host function definitions -----------------------------------------

void allocateAndInitializeAB();
void computeCpuMMM();
void copyMatricesToGPU();
void copyResultFromGPU();
void compareHostAndGpuOutput();
void die(const char *error); 
void check_error(cudaError e);

//----------------------------------- CUDA function definitions -----------------------------------------

void computeGpuMMM();

// For 10,000x10,000 matrices use a 313x313 block grid to completely cover the matrix
const int blocks2d_sz  = 313;
const int threads2d_sz = 32;
//-------------------------------------------------------------------------------------------------------
int main(int argc, char **argv) {
	// Read in the matrix sizes
	A_MD.dimension1 = (argc > 1) ? atoi(argv[1]) : 100;
	A_MD.dimension2 = (argc > 2) ? atoi(argv[2]) : A_MD.dimension1;
	B_MD.dimension1 = (argc > 3) ? atoi(argv[3]) : A_MD.dimension2;
	B_MD.dimension2 = (argc > 4) ? atoi(argv[4]) : B_MD.dimension1;
	C_MD.dimension1 = A_MD.dimension1;
	C_MD.dimension2 = B_MD.dimension2;

	printf("Matrix A is %d-by-%d\n", A_MD.dimension1, A_MD.dimension2);
	printf("Matrix B is %d-by-%d\n", B_MD.dimension1, B_MD.dimension2);
	printf("Matrix C is %d-by-%d\n", C_MD.dimension1, C_MD.dimension2);

	// Initialize A and B to random floats.
	// Initialization is not part of total runtime.
	allocateAndInitializeAB();

	clock_t start;
	clock_t end;
	double elapsed;
//	// matrix matrix multiplication in the CPU for comparison purposes
//	start = clock();	
//	computeCpuMMM();
//	end = clock();
//  elapsed = (end - start) / (double) CLOCKS_PER_SEC;
//  printf("Computation time in the CPU: %f seconds\n", elapsed);

	// GPU matrix multiplication
	start = clock();	
	computeGpuMMM();
	end = clock();
    elapsed = (end - start) / (double) CLOCKS_PER_SEC;
    printf("Computation time in the GPU: %f seconds\n", elapsed);

//  // compare if GPU implementation is correct
//	compareHostAndGpuOutput();
	return 0;
}

// Matrix multiply kernel
__global__ void mm_multiply_kernel(float *A, float *B, float *C, int N) {
	// calculate index into the result matrix
	int r = blockIdx.y * blockDim.y + threadIdx.y;
	int c = blockIdx.x * blockDim.x + threadIdx.x;
	// shared matrices to hold a tile from the global matrices
	__shared__ float a_shared[threads2d_sz][threads2d_sz];
	__shared__ float b_shared[threads2d_sz][threads2d_sz];
	// final result of vector product
	float result = 0;
	int tile_iter, k, A_row_glob, A_col_glob, B_row_glob, B_col_glob;
	// iterate over the tiles in the global matrices
	int tile_length = N/blockDim.x;
	for (tile_iter = 0; tile_iter < tile_length; tile_iter++) {
		// calculate the indexes into A and B to load into shared memory
		A_row_glob = blockIdx.y*blockDim.y + threadIdx.y; 
		A_col_glob = tile_iter*blockDim.x + threadIdx.x;

		B_row_glob = tile_iter*blockDim.y + threadIdx.y;
		B_col_glob = blockIdx.x*blockDim.x + threadIdx.x;
		// each thread loads 1 entry from A and B into shared memory
		a_shared[threadIdx.y][threadIdx.x] = A[A_row_glob*N + A_col_glob];
		b_shared[threadIdx.y][threadIdx.x] = B[B_row_glob*N + B_col_glob];
		// wait for all threads in the block to load
		__syncthreads();
		// Unrolled loop to calculate vector product
		result += a_shared[threadIdx.y][k] * b_shared[k][threadIdx.x];
		result += a_shared[threadIdx.y][k+1]  * b_shared[k+1][threadIdx.x];
		result += a_shared[threadIdx.y][k+2]  * b_shared[k+2][threadIdx.x];
		result += a_shared[threadIdx.y][k+3]  * b_shared[k+3][threadIdx.x];
		result += a_shared[threadIdx.y][k+4]  * b_shared[k+4][threadIdx.x];
		result += a_shared[threadIdx.y][k+5]  * b_shared[k+5][threadIdx.x];
		result += a_shared[threadIdx.y][k+6]  * b_shared[k+6][threadIdx.x];
		result += a_shared[threadIdx.y][k+7]  * b_shared[k+7][threadIdx.x];
		result += a_shared[threadIdx.y][k+8]  * b_shared[k+8][threadIdx.x];
		result += a_shared[threadIdx.y][k+9]  * b_shared[k+9][threadIdx.x];
		result += a_shared[threadIdx.y][k+10] * b_shared[k+10][threadIdx.x];
		result += a_shared[threadIdx.y][k+11] * b_shared[k+11][threadIdx.x];
		result += a_shared[threadIdx.y][k+12] * b_shared[k+12][threadIdx.x];
		result += a_shared[threadIdx.y][k+13] * b_shared[k+13][threadIdx.x];
		result += a_shared[threadIdx.y][k+14] * b_shared[k+14][threadIdx.x];
		result += a_shared[threadIdx.y][k+15] * b_shared[k+15][threadIdx.x];
		result += a_shared[threadIdx.y][k+16] * b_shared[k+16][threadIdx.x];
		result += a_shared[threadIdx.y][k+17] * b_shared[k+17][threadIdx.x];
		result += a_shared[threadIdx.y][k+18] * b_shared[k+18][threadIdx.x];
		result += a_shared[threadIdx.y][k+19] * b_shared[k+19][threadIdx.x];
		result += a_shared[threadIdx.y][k+20] * b_shared[k+20][threadIdx.x];
		result += a_shared[threadIdx.y][k+21] * b_shared[k+21][threadIdx.x];
		result += a_shared[threadIdx.y][k+22] * b_shared[k+22][threadIdx.x];
		result += a_shared[threadIdx.y][k+23] * b_shared[k+23][threadIdx.x];
		result += a_shared[threadIdx.y][k+24] * b_shared[k+24][threadIdx.x];
		result += a_shared[threadIdx.y][k+25] * b_shared[k+25][threadIdx.x];
		result += a_shared[threadIdx.y][k+26] * b_shared[k+26][threadIdx.x];
		result += a_shared[threadIdx.y][k+27] * b_shared[k+27][threadIdx.x];
		result += a_shared[threadIdx.y][k+28] * b_shared[k+28][threadIdx.x];
		result += a_shared[threadIdx.y][k+29] * b_shared[k+29][threadIdx.x];
		result += a_shared[threadIdx.y][k+30] * b_shared[k+30][threadIdx.x];
		result += a_shared[threadIdx.y][k+31] * b_shared[k+31][threadIdx.x];
		__syncthreads();
		// wait for all threads in the block before loading
	}
	// handle leftover rows and columns for matrix sizes that aren't evenly divisible
	int leftover = N-tile_length*blockDim.x;
	if (leftover > 0) {
		// calculate the indexes into A and B to load into shared memory
		A_row_glob = blockIdx.y*blockDim.y + threadIdx.y; 
		A_col_glob = tile_iter*blockDim.x + threadIdx.x;

		B_row_glob = tile_iter*blockDim.y + threadIdx.y;
		B_col_glob = blockIdx.x*blockDim.x + threadIdx.x;

		// if the indexes are within bounds, load them
		// only loads the leftover entries into the tile
		if (A_col_glob < N) {
			a_shared[threadIdx.y][threadIdx.x] = A[A_row_glob*N + A_col_glob];
		}
		if (B_row_glob < N) {
			b_shared[threadIdx.y][threadIdx.x] = B[B_row_glob*N + B_col_glob];
		}
		__syncthreads();
		// edge case calculations are not unrolled because they don't have a big impact on performance
		for (k = 0; k < leftover; k++) {
			result += a_shared[threadIdx.y][k] * b_shared[k][threadIdx.x];
		}
		__syncthreads();
	}
	// put result into global memory if it is a valid index
	if (r < N && c < N) {
		C[r*N + c] = result;
	}
}

void computeGpuMMM() {
	// Allocate GPU memory for the inputs and the result
	clock_t start = clock();	
	copyMatricesToGPU();
	clock_t end = clock();
    double elapsed = (end - start) / (double) CLOCKS_PER_SEC;
    printf("\nGPU: \tTransfer to GPU: %f seconds\n", elapsed);
	
	// block size and grid size
	dim3 blocks2d(blocks2d_sz,blocks2d_sz);
	dim3 threads2d(threads2d_sz,threads2d_sz);

	// Execute the kernel to compute the vector sum on the GPU
	start = clock();
	mm_multiply_kernel <<<blocks2d, threads2d>>> (A_GPU, B_GPU, C_GPU, C_MD.dimension1);
	// make the CPU main thread waite for the GPU kernel call to complete
	cudaThreadSynchronize();  // This is only needed for timing and error-checking purposes
	end = clock();
    elapsed = (end - start) / (double) CLOCKS_PER_SEC;
    printf("\tKernel execution: %f seconds\n", elapsed);
	
	// Check for kernel errors
	check_error(cudaGetLastError());

	// Transfer the result from the GPU to the CPU
	start = clock();
	copyResultFromGPU();
	end = clock();
	elapsed = (end - start) / (double) CLOCKS_PER_SEC;
	printf("\tTransfer from GPU: %f seconds\n", elapsed);
	
	// Free the GPU memory
	check_error(cudaFree(A_GPU));
	check_error(cudaFree(B_GPU));
	check_error(cudaFree(C_GPU));	
}

// allocate and initialize A and B using a random number generator
void allocateAndInitializeAB() {
	
	size_t sizeofA = A_MD.dimension1 * A_MD.dimension2 * sizeof(float);
	A = (float*) malloc(sizeofA);
	
	srand(time(NULL));
  	for (int i = 0; i < A_MD.dimension1; i++) {
		for (int j = 0; j < A_MD.dimension2; j++) {
			int index = i * A_MD.dimension2 + j;
			A[index] = (rand() % 1000) * 0.001; 
		}
	}
	
	size_t sizeofB = B_MD.dimension1 * B_MD.dimension2 * sizeof(float);
	B = (float*) malloc(sizeofB);
  	for (int i = 0; i < B_MD.dimension1; i++) {
		for (int j = 0; j < B_MD.dimension2; j++) {
			int index = i * B_MD.dimension2 + j;
			B[index] = (rand() % 1000) * 0.001; 
		}
	}
}

// allocate memory in the GPU for all matrices, and copy A and B content from the host CPU memory to the GPU memory
void copyMatricesToGPU() {
	
	size_t sizeofA = A_MD.dimension1 * A_MD.dimension2 * sizeof(float);
	check_error(cudaMalloc((void **) &A_GPU, sizeofA));
	check_error(cudaMemcpy(A_GPU, A, sizeofA, cudaMemcpyHostToDevice));
	
	size_t sizeofB = B_MD.dimension1 * B_MD.dimension2 * sizeof(float);
	check_error(cudaMalloc((void **) &B_GPU, sizeofB));
	check_error(cudaMemcpy(B_GPU, B, sizeofB, cudaMemcpyHostToDevice));
	
	size_t sizeofC = C_MD.dimension1 * C_MD.dimension2 * sizeof(float);
	check_error(cudaMalloc((void **) &C_GPU, sizeofC));
}

// copy results from C_GPU which is in GPU card memory to C_CPU which is in the host CPU for result comparison
void copyResultFromGPU() {
	size_t sizeofC = C_MD.dimension1 * C_MD.dimension2 * sizeof(float);
	C_CPU = (float*) malloc(sizeofC);
	check_error(cudaMemcpy(C_CPU, C_GPU, sizeofC, cudaMemcpyDeviceToHost));
}

// do a straightforward matrix-matrix multiplication in the CPU
// notice that this implementation can be massively improved in the CPU by doing proper cache blocking but we are
// not providing you the efficient CPU implementation as that reveals too much about the ideal GPU implementation
void computeCpuMMM() {
	
	// allocate the result matrix for the CPU computation
	size_t sizeofC = C_MD.dimension1 * C_MD.dimension2 * sizeof(float);
	C = (float*) malloc(sizeofC);
	
	// compute C[i][j] as the sum of A[i][k] * B[k][j] for all columns k of A
	for (int i = 0; i < A_MD.dimension1; i++) {
		int a_i = i * A_MD.dimension2;
		int c_i = i * C_MD.dimension2;
		for (int j = 0; j < B_MD.dimension2; j++) {
			int c_index = c_i + j;
			C[c_index] = 0;
			for (int k = 0; k < B_MD.dimension1; k++) {
				int a_index = a_i + k;
				int b_index = k * B_MD.dimension2 + j;
				C[c_index] += A[a_index] * B[b_index];
			}
		}
	}
}

// function to determine if the GPU computation is done correctly by comparing the output from the GPU with that
// from the CPU
void compareHostAndGpuOutput() {
	int totalElements = C_MD.dimension1 * C_MD.dimension2;
	int missmatchCount = 0;
	for (int i = 0; i < totalElements; i++) {
		if (fabs(C[i] - C_CPU[i]) > 0.01) {
			missmatchCount++;
			printf("mismatch at index %i: %f\t%f\n", i, C[i], C_CPU[i]);
		}
	}
	if (missmatchCount > 0) {
		printf("Computation is incorrect: outputs do not match in %d indexes\n", missmatchCount);
	} else {
		printf("Computation is correct: CPU and GPU outputs match\n");
	}
}

// Prints the specified error message and then exits
void die(const char *error) {
        printf("%s", error);
        exit(1);
}

// If the specified error code refers to a real error, report it and quit the program
void check_error(cudaError e) {
        if (e != cudaSuccess) {
                printf("\nCUDA error: %s\n", cudaGetErrorString(e));
                exit(1);
        }
}


	.file	"tmpxft_003b6274_00000000-6_matrix_multiply_cuda.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3646:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3646:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z23allocateAndInitializeABv
	.type	_Z23allocateAndInitializeABv, @function
_Z23allocateAndInitializeABv:
.LFB3637:
	.cfi_startproc
	endbr64
	movl	A_MD(%rip), %edi
	imull	4+A_MD(%rip), %edi
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movslq	%edi, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	xorl	%edi, %edi
	movq	%rax, A(%rip)
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
.L3:
	cmpl	%ebp, A_MD(%rip)
	jle	.L13
	xorl	%r12d, %r12d
.L5:
	movl	4+A_MD(%rip), %eax
	cmpl	%r12d, %eax
	jle	.L14
	imull	%ebp, %eax
	leal	(%rax,%r12), %ebx
	incl	%r12d
	call	rand@PLT
	movl	$1000, %ecx
	movslq	%ebx, %rbx
	cltd
	idivl	%ecx
	movq	A(%rip), %rax
	cvtsi2sdl	%edx, %xmm0
	mulsd	.LC0(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rax,%rbx,4)
	jmp	.L5
.L14:
	incl	%ebp
	jmp	.L3
.L13:
	movl	B_MD(%rip), %edi
	imull	4+B_MD(%rip), %edi
	xorl	%ebp, %ebp
	movslq	%edi, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rax, B(%rip)
.L6:
	cmpl	%ebp, B_MD(%rip)
	jle	.L15
	xorl	%r12d, %r12d
.L8:
	movl	4+B_MD(%rip), %eax
	cmpl	%r12d, %eax
	jle	.L16
	imull	%ebp, %eax
	leal	(%rax,%r12), %ebx
	incl	%r12d
	call	rand@PLT
	movl	$1000, %ecx
	movslq	%ebx, %rbx
	cltd
	idivl	%ecx
	movq	B(%rip), %rax
	cvtsi2sdl	%edx, %xmm0
	mulsd	.LC0(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rax,%rbx,4)
	jmp	.L8
.L16:
	incl	%ebp
	jmp	.L6
.L15:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3637:
	.size	_Z23allocateAndInitializeABv, .-_Z23allocateAndInitializeABv
	.globl	_Z13computeCpuMMMv
	.type	_Z13computeCpuMMMv, @function
_Z13computeCpuMMMv:
.LFB3640:
	.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
	pushq	%rdx
	.cfi_def_cfa_offset 64
	movslq	4+C_MD(%rip), %rbx
	movl	C_MD(%rip), %edi
	imull	%ebx, %edi
	salq	$2, %rbx
	movslq	%edi, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movslq	4+B_MD(%rip), %rsi
	xorl	%ecx, %ecx
	movslq	4+A_MD(%rip), %r8
	movq	%rax, C(%rip)
	movl	A_MD(%rip), %r10d
	movl	B_MD(%rip), %ebp
	movq	B(%rip), %r11
	movq	%rsi, %r9
	salq	$2, %r8
	movq	A(%rip), %rdi
	salq	$2, %rsi
.L18:
	cmpl	%ecx, %r10d
	jle	.L25
	movq	%r11, %r13
	movq	%rax, %r14
	xorl	%r12d, %r12d
.L19:
	cmpl	%r12d, %r9d
	jle	.L26
	movq	%r13, %r15
	xorl	%edx, %edx
	xorps	%xmm0, %xmm0
.L21:
	movss	%xmm0, (%r14)
	cmpl	%edx, %ebp
	jle	.L27
	movss	(%rdi,%rdx,4), %xmm1
	mulss	(%r15), %xmm1
	incq	%rdx
	addq	%rsi, %r15
	addss	%xmm1, %xmm0
	jmp	.L21
.L27:
	incl	%r12d
	addq	$4, %r13
	addq	$4, %r14
	jmp	.L19
.L26:
	incl	%ecx
	addq	%rbx, %rax
	addq	%r8, %rdi
	jmp	.L18
.L25:
	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
.LFE3640:
	.size	_Z13computeCpuMMMv, .-_Z13computeCpuMMMv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC4:
	.string	"mismatch at index %i: %f\t%f\n"
.LC5:
	.string	"Computation is incorrect: outputs do not match in %d indexes\n"
.LC6:
	.string	"Computation is correct: CPU and GPU outputs match\n"
	.text
	.globl	_Z23compareHostAndGpuOutputv
	.type	_Z23compareHostAndGpuOutputv, @function
_Z23compareHostAndGpuOutputv:
.LFB3641:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	C_MD(%rip), %r12d
	imull	4+C_MD(%rip), %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	xorl	%ebx, %ebx
.L29:
	cmpl	%ebx, %r12d
	jle	.L36
	movq	C(%rip), %rax
	movss	(%rax,%rbx,4), %xmm0
	movq	C_CPU(%rip), %rax
	movss	(%rax,%rbx,4), %xmm1
	movaps	%xmm0, %xmm2
	subss	%xmm1, %xmm2
	andps	.LC2(%rip), %xmm2
	cvtss2sd	%xmm2, %xmm2
	comisd	.LC3(%rip), %xmm2
	jbe	.L30
	movl	%ebx, %edx
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	movb	$2, %al
	incl	%ebp
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	call	__printf_chk@PLT
.L30:
	incq	%rbx
	jmp	.L29
.L36:
	testl	%ebp, %ebp
	jle	.L33
	popq	%rbx
	.cfi_remember_state
	.cfi_def_cfa_offset 24
	movl	%ebp, %edx
	leaq	.LC5(%rip), %rsi
	popq	%rbp
	.cfi_def_cfa_offset 16
	movl	$2, %edi
	xorl	%eax, %eax
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
.L33:
	.cfi_restore_state
	popq	%rbx
	.cfi_def_cfa_offset 24
	leaq	.LC6(%rip), %rsi
	popq	%rbp
	.cfi_def_cfa_offset 16
	movl	$2, %edi
	xorl	%eax, %eax
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE3641:
	.size	_Z23compareHostAndGpuOutputv, .-_Z23compareHostAndGpuOutputv
	.section	.rodata.str1.1
.LC7:
	.string	"%s"
	.text
	.globl	_Z3diePKc
	.type	_Z3diePKc, @function
_Z3diePKc:
.LFB3642:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	leaq	.LC7(%rip), %rsi
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdi, %rdx
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
	.cfi_endproc
.LFE3642:
	.size	_Z3diePKc, .-_Z3diePKc
	.section	.rodata.str1.1
.LC8:
	.string	"\nCUDA error: %s\n"
	.text
	.globl	_Z11check_error9cudaError
	.type	_Z11check_error9cudaError, @function
_Z11check_error9cudaError:
.LFB3643:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L39
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	cudaGetErrorString@PLT
	movl	$2, %edi
	leaq	.LC8(%rip), %rsi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L39:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3643:
	.size	_Z11check_error9cudaError, .-_Z11check_error9cudaError
	.globl	_Z17copyMatricesToGPUv
	.type	_Z17copyMatricesToGPUv, @function
_Z17copyMatricesToGPUv:
.LFB3638:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	A_MD(%rip), %ebx
	leaq	A_GPU(%rip), %rdi
	imull	4+A_MD(%rip), %ebx
	movslq	%ebx, %rbx
	salq	$2, %rbx
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movq	A(%rip), %rsi
	movq	%rbx, %rdx
	movq	A_GPU(%rip), %rdi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movl	B_MD(%rip), %ebx
	imull	4+B_MD(%rip), %ebx
	leaq	B_GPU(%rip), %rdi
	movslq	%ebx, %rbx
	salq	$2, %rbx
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movq	B(%rip), %rsi
	movq	%rbx, %rdx
	movq	B_GPU(%rip), %rdi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movl	C_MD(%rip), %esi
	imull	4+C_MD(%rip), %esi
	leaq	C_GPU(%rip), %rdi
	movslq	%esi, %rsi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	popq	%rbx
	.cfi_def_cfa_offset 8
	movl	%eax, %edi
	jmp	_Z11check_error9cudaError
	.cfi_endproc
.LFE3638:
	.size	_Z17copyMatricesToGPUv, .-_Z17copyMatricesToGPUv
	.globl	_Z17copyResultFromGPUv
	.type	_Z17copyResultFromGPUv, @function
_Z17copyResultFromGPUv:
.LFB3639:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	C_MD(%rip), %ebx
	imull	4+C_MD(%rip), %ebx
	movslq	%ebx, %rbx
	salq	$2, %rbx
	movq	%rbx, %rdi
	call	malloc@PLT
	movq	C_GPU(%rip), %rsi
	movq	%rbx, %rdx
	movl	$2, %ecx
	movq	%rax, %rdi
	movq	%rax, C_CPU(%rip)
	call	cudaMemcpy@PLT
	popq	%rbx
	.cfi_def_cfa_offset 8
	movl	%eax, %edi
	jmp	_Z11check_error9cudaError
	.cfi_endproc
.LFE3639:
	.size	_Z17copyResultFromGPUv, .-_Z17copyResultFromGPUv
	.globl	_Z43__device_stub__Z18mm_multiply_kernelPfS_S_iPfS_S_i
	.type	_Z43__device_stub__Z18mm_multiply_kernelPfS_S_iPfS_S_i, @function
_Z43__device_stub__Z18mm_multiply_kernelPfS_S_iPfS_S_i:
.LFB3668:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 4(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L48
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z18mm_multiply_kernelPfS_S_i(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L48:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L50
	call	__stack_chk_fail@PLT
.L50:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3668:
	.size	_Z43__device_stub__Z18mm_multiply_kernelPfS_S_iPfS_S_i, .-_Z43__device_stub__Z18mm_multiply_kernelPfS_S_iPfS_S_i
	.globl	_Z18mm_multiply_kernelPfS_S_i
	.type	_Z18mm_multiply_kernelPfS_S_i, @function
_Z18mm_multiply_kernelPfS_S_i:
.LFB3669:
	.cfi_startproc
	endbr64
	jmp	_Z43__device_stub__Z18mm_multiply_kernelPfS_S_iPfS_S_i
	.cfi_endproc
.LFE3669:
	.size	_Z18mm_multiply_kernelPfS_S_i, .-_Z18mm_multiply_kernelPfS_S_i
	.section	.rodata.str1.1
.LC10:
	.string	"\nGPU: \tTransfer to GPU: %f seconds\n"
.LC11:
	.string	"\tKernel execution: %f seconds\n"
.LC12:
	.string	"\tTransfer from GPU: %f seconds\n"
	.text
	.globl	_Z13computeGpuMMMv
	.type	_Z13computeGpuMMMv, @function
_Z13computeGpuMMMv:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	call	clock@PLT
	movq	%rax, %rbx
	call	_Z17copyMatricesToGPUv
	call	clock@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	subq	%rbx, %rax
	cvtsi2sdq	%rax, %xmm0
	movb	$1, %al
	divsd	.LC9(%rip), %xmm0
	call	__printf_chk@PLT
	movl	$1, 16(%rsp)
	movabsq	$1344324763961, %rax
	movq	%rax, 8(%rsp)
	movabsq	$137438953504, %rax
	movq	%rax, 20(%rsp)
	movl	$1, 28(%rsp)
	call	clock@PLT
	movl	28(%rsp), %ecx
	movl	16(%rsp), %esi
	xorl	%r9d, %r9d
	movq	20(%rsp), %rdx
	movq	8(%rsp), %rdi
	xorl	%r8d, %r8d
	movq	%rax, %rbx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L54
	movl	C_MD(%rip), %ecx
	movq	C_GPU(%rip), %rdx
	movq	B_GPU(%rip), %rsi
	movq	A_GPU(%rip), %rdi
	call	_Z43__device_stub__Z18mm_multiply_kernelPfS_S_iPfS_S_i
.L54:
	call	cudaThreadSynchronize@PLT
	call	clock@PLT
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	subq	%rbx, %rax
	cvtsi2sdq	%rax, %xmm0
	movb	$1, %al
	divsd	.LC9(%rip), %xmm0
	call	__printf_chk@PLT
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	call	clock@PLT
	movq	%rax, %rbx
	call	_Z17copyResultFromGPUv
	call	clock@PLT
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	subq	%rbx, %rax
	cvtsi2sdq	%rax, %xmm0
	movb	$1, %al
	divsd	.LC9(%rip), %xmm0
	call	__printf_chk@PLT
	movq	A_GPU(%rip), %rdi
	call	cudaFree@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movq	B_GPU(%rip), %rdi
	call	cudaFree@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movq	C_GPU(%rip), %rdi
	call	cudaFree@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movl	%eax, %edi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	_Z11check_error9cudaError
	.cfi_endproc
.LFE3636:
	.size	_Z13computeGpuMMMv, .-_Z13computeGpuMMMv
	.section	.rodata.str1.1
.LC13:
	.string	"Matrix A is %d-by-%d\n"
.LC14:
	.string	"Matrix B is %d-by-%d\n"
.LC15:
	.string	"Matrix C is %d-by-%d\n"
.LC16:
	.string	"Computation time in the GPU: %f seconds\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	%edi, %r13d
	decl	%edi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movl	$100, %ebx
	pushq	%rcx
	.cfi_def_cfa_offset 48
	jle	.L57
	movq	8(%rsi), %rdi
	call	atoi@PLT
	movl	%eax, %ebx
.L57:
	movl	%ebx, A_MD(%rip)
	movl	%ebx, %ebp
	cmpl	$2, %r13d
	jle	.L58
	movq	16(%r12), %rdi
	call	atoi@PLT
	movl	%eax, %ebp
.L58:
	movl	%ebp, 4+A_MD(%rip)
	movl	%ebp, %eax
	cmpl	$3, %r13d
	jle	.L59
	movq	24(%r12), %rdi
	call	atoi@PLT
.L59:
	movl	%eax, B_MD(%rip)
	cmpl	$4, %r13d
	jle	.L60
	movq	32(%r12), %rdi
	call	atoi@PLT
.L60:
	movl	%ebp, %ecx
	movl	%ebx, %edx
	movl	%eax, 4+B_MD(%rip)
	movl	$2, %edi
	movl	%eax, 4+C_MD(%rip)
	leaq	.LC13(%rip), %rsi
	xorl	%eax, %eax
	movl	%ebx, C_MD(%rip)
	call	__printf_chk@PLT
	movl	4+B_MD(%rip), %ecx
	movl	B_MD(%rip), %edx
	xorl	%eax, %eax
	leaq	.LC14(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	C_MD(%rip), %edx
	movl	4+C_MD(%rip), %ecx
	xorl	%eax, %eax
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	call	_Z23allocateAndInitializeABv
	call	clock@PLT
	movq	%rax, %rbx
	call	_Z13computeGpuMMMv
	call	clock@PLT
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	subq	%rbx, %rax
	cvtsi2sdq	%rax, %xmm0
	movb	$1, %al
	divsd	.LC9(%rip), %xmm0
	call	__printf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3635:
	.size	main, .-main
	.section	.rodata.str1.1
.LC17:
	.string	"_Z18mm_multiply_kernelPfS_S_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3671:
	.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	.LC17(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z18mm_multiply_kernelPfS_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
.LFE3671:
	.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	C_GPU
	.bss
	.align 8
	.type	C_GPU, @object
	.size	C_GPU, 8
C_GPU:
	.zero	8
	.globl	B_GPU
	.align 8
	.type	B_GPU, @object
	.size	B_GPU, 8
B_GPU:
	.zero	8
	.globl	A_GPU
	.align 8
	.type	A_GPU, @object
	.size	A_GPU, 8
A_GPU:
	.zero	8
	.globl	C_CPU
	.align 8
	.type	C_CPU, @object
	.size	C_CPU, 8
C_CPU:
	.zero	8
	.globl	C
	.align 8
	.type	C, @object
	.size	C, 8
C:
	.zero	8
	.globl	B
	.align 8
	.type	B, @object
	.size	B, 8
B:
	.zero	8
	.globl	A
	.align 8
	.type	A, @object
	.size	A, 8
A:
	.zero	8
	.globl	C_MD
	.align 8
	.type	C_MD, @object
	.size	C_MD, 8
C_MD:
	.zero	8
	.globl	B_MD
	.align 8
	.type	B_MD, @object
	.size	B_MD, 8
B_MD:
	.zero	8
	.globl	A_MD
	.align 8
	.type	A_MD, @object
	.size	A_MD, 8
A_MD:
	.zero	8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	-755914244
	.long	1062232653
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC2:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst8
	.align 8
.LC3:
	.long	1202590843
	.long	1065646817
	.align 8
.LC9:
	.long	0
	.long	1093567616
	.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:

// mmm_cuda.cu, Crispin Bernier, chb2ab

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

using namespace std;

//----------------------------------- Structures and Globals---------------------------------------------

typedef struct {
	int dimension1;
	int dimension2;	
} ArrayMetadata2D;

// metadata variables describing dimensionalities of all data structures involved in the computation
ArrayMetadata2D A_MD, B_MD, C_MD;
// pointers for input and output arrays in the host memory  
float *A, *B, *C, *C_CPU;
// pointers for input and output arrays in the device memory (NVIDIA DRAM)
float *A_GPU, *B_GPU, *C_GPU;

//----------------------------------- host function definitions -----------------------------------------

void allocateAndInitializeAB();
void computeCpuMMM();
void copyMatricesToGPU();
void copyResultFromGPU();
void compareHostAndGpuOutput();
void die(const char *error); 
void check_error(hipError_t e);

//----------------------------------- CUDA function definitions -----------------------------------------

void computeGpuMMM();

// For 10,000x10,000 matrices use a 313x313 block grid to completely cover the matrix
const int blocks2d_sz  = 313;
const int threads2d_sz = 32;
//-------------------------------------------------------------------------------------------------------
int main(int argc, char **argv) {
	// Read in the matrix sizes
	A_MD.dimension1 = (argc > 1) ? atoi(argv[1]) : 100;
	A_MD.dimension2 = (argc > 2) ? atoi(argv[2]) : A_MD.dimension1;
	B_MD.dimension1 = (argc > 3) ? atoi(argv[3]) : A_MD.dimension2;
	B_MD.dimension2 = (argc > 4) ? atoi(argv[4]) : B_MD.dimension1;
	C_MD.dimension1 = A_MD.dimension1;
	C_MD.dimension2 = B_MD.dimension2;

	printf("Matrix A is %d-by-%d\n", A_MD.dimension1, A_MD.dimension2);
	printf("Matrix B is %d-by-%d\n", B_MD.dimension1, B_MD.dimension2);
	printf("Matrix C is %d-by-%d\n", C_MD.dimension1, C_MD.dimension2);

	// Initialize A and B to random floats.
	// Initialization is not part of total runtime.
	allocateAndInitializeAB();

	clock_t start;
	clock_t end;
	double elapsed;
//	// matrix matrix multiplication in the CPU for comparison purposes
//	start = clock();	
//	computeCpuMMM();
//	end = clock();
//  elapsed = (end - start) / (double) CLOCKS_PER_SEC;
//  printf("Computation time in the CPU: %f seconds\n", elapsed);

	// GPU matrix multiplication
	start = clock();	
	computeGpuMMM();
	end = clock();
    elapsed = (end - start) / (double) CLOCKS_PER_SEC;
    printf("Computation time in the GPU: %f seconds\n", elapsed);

//  // compare if GPU implementation is correct
//	compareHostAndGpuOutput();
	return 0;
}

// Matrix multiply kernel
__global__ void mm_multiply_kernel(float *A, float *B, float *C, int N) {
	// calculate index into the result matrix
	int r = blockIdx.y * blockDim.y + threadIdx.y;
	int c = blockIdx.x * blockDim.x + threadIdx.x;
	// shared matrices to hold a tile from the global matrices
	__shared__ float a_shared[threads2d_sz][threads2d_sz];
	__shared__ float b_shared[threads2d_sz][threads2d_sz];
	// final result of vector product
	float result = 0;
	int tile_iter, k, A_row_glob, A_col_glob, B_row_glob, B_col_glob;
	// iterate over the tiles in the global matrices
	int tile_length = N/blockDim.x;
	for (tile_iter = 0; tile_iter < tile_length; tile_iter++) {
		// calculate the indexes into A and B to load into shared memory
		A_row_glob = blockIdx.y*blockDim.y + threadIdx.y; 
		A_col_glob = tile_iter*blockDim.x + threadIdx.x;

		B_row_glob = tile_iter*blockDim.y + threadIdx.y;
		B_col_glob = blockIdx.x*blockDim.x + threadIdx.x;
		// each thread loads 1 entry from A and B into shared memory
		a_shared[threadIdx.y][threadIdx.x] = A[A_row_glob*N + A_col_glob];
		b_shared[threadIdx.y][threadIdx.x] = B[B_row_glob*N + B_col_glob];
		// wait for all threads in the block to load
		__syncthreads();
		// Unrolled loop to calculate vector product
		result += a_shared[threadIdx.y][k] * b_shared[k][threadIdx.x];
		result += a_shared[threadIdx.y][k+1]  * b_shared[k+1][threadIdx.x];
		result += a_shared[threadIdx.y][k+2]  * b_shared[k+2][threadIdx.x];
		result += a_shared[threadIdx.y][k+3]  * b_shared[k+3][threadIdx.x];
		result += a_shared[threadIdx.y][k+4]  * b_shared[k+4][threadIdx.x];
		result += a_shared[threadIdx.y][k+5]  * b_shared[k+5][threadIdx.x];
		result += a_shared[threadIdx.y][k+6]  * b_shared[k+6][threadIdx.x];
		result += a_shared[threadIdx.y][k+7]  * b_shared[k+7][threadIdx.x];
		result += a_shared[threadIdx.y][k+8]  * b_shared[k+8][threadIdx.x];
		result += a_shared[threadIdx.y][k+9]  * b_shared[k+9][threadIdx.x];
		result += a_shared[threadIdx.y][k+10] * b_shared[k+10][threadIdx.x];
		result += a_shared[threadIdx.y][k+11] * b_shared[k+11][threadIdx.x];
		result += a_shared[threadIdx.y][k+12] * b_shared[k+12][threadIdx.x];
		result += a_shared[threadIdx.y][k+13] * b_shared[k+13][threadIdx.x];
		result += a_shared[threadIdx.y][k+14] * b_shared[k+14][threadIdx.x];
		result += a_shared[threadIdx.y][k+15] * b_shared[k+15][threadIdx.x];
		result += a_shared[threadIdx.y][k+16] * b_shared[k+16][threadIdx.x];
		result += a_shared[threadIdx.y][k+17] * b_shared[k+17][threadIdx.x];
		result += a_shared[threadIdx.y][k+18] * b_shared[k+18][threadIdx.x];
		result += a_shared[threadIdx.y][k+19] * b_shared[k+19][threadIdx.x];
		result += a_shared[threadIdx.y][k+20] * b_shared[k+20][threadIdx.x];
		result += a_shared[threadIdx.y][k+21] * b_shared[k+21][threadIdx.x];
		result += a_shared[threadIdx.y][k+22] * b_shared[k+22][threadIdx.x];
		result += a_shared[threadIdx.y][k+23] * b_shared[k+23][threadIdx.x];
		result += a_shared[threadIdx.y][k+24] * b_shared[k+24][threadIdx.x];
		result += a_shared[threadIdx.y][k+25] * b_shared[k+25][threadIdx.x];
		result += a_shared[threadIdx.y][k+26] * b_shared[k+26][threadIdx.x];
		result += a_shared[threadIdx.y][k+27] * b_shared[k+27][threadIdx.x];
		result += a_shared[threadIdx.y][k+28] * b_shared[k+28][threadIdx.x];
		result += a_shared[threadIdx.y][k+29] * b_shared[k+29][threadIdx.x];
		result += a_shared[threadIdx.y][k+30] * b_shared[k+30][threadIdx.x];
		result += a_shared[threadIdx.y][k+31] * b_shared[k+31][threadIdx.x];
		__syncthreads();
		// wait for all threads in the block before loading
	}
	// handle leftover rows and columns for matrix sizes that aren't evenly divisible
	int leftover = N-tile_length*blockDim.x;
	if (leftover > 0) {
		// calculate the indexes into A and B to load into shared memory
		A_row_glob = blockIdx.y*blockDim.y + threadIdx.y; 
		A_col_glob = tile_iter*blockDim.x + threadIdx.x;

		B_row_glob = tile_iter*blockDim.y + threadIdx.y;
		B_col_glob = blockIdx.x*blockDim.x + threadIdx.x;

		// if the indexes are within bounds, load them
		// only loads the leftover entries into the tile
		if (A_col_glob < N) {
			a_shared[threadIdx.y][threadIdx.x] = A[A_row_glob*N + A_col_glob];
		}
		if (B_row_glob < N) {
			b_shared[threadIdx.y][threadIdx.x] = B[B_row_glob*N + B_col_glob];
		}
		__syncthreads();
		// edge case calculations are not unrolled because they don't have a big impact on performance
		for (k = 0; k < leftover; k++) {
			result += a_shared[threadIdx.y][k] * b_shared[k][threadIdx.x];
		}
		__syncthreads();
	}
	// put result into global memory if it is a valid index
	if (r < N && c < N) {
		C[r*N + c] = result;
	}
}

void computeGpuMMM() {
	// Allocate GPU memory for the inputs and the result
	clock_t start = clock();	
	copyMatricesToGPU();
	clock_t end = clock();
    double elapsed = (end - start) / (double) CLOCKS_PER_SEC;
    printf("\nGPU: \tTransfer to GPU: %f seconds\n", elapsed);
	
	// block size and grid size
	dim3 blocks2d(blocks2d_sz,blocks2d_sz);
	dim3 threads2d(threads2d_sz,threads2d_sz);

	// Execute the kernel to compute the vector sum on the GPU
	start = clock();
	mm_multiply_kernel <<<blocks2d, threads2d>>> (A_GPU, B_GPU, C_GPU, C_MD.dimension1);
	// make the CPU main thread waite for the GPU kernel call to complete
	hipDeviceSynchronize();  // This is only needed for timing and error-checking purposes
	end = clock();
    elapsed = (end - start) / (double) CLOCKS_PER_SEC;
    printf("\tKernel execution: %f seconds\n", elapsed);
	
	// Check for kernel errors
	check_error(hipGetLastError());

	// Transfer the result from the GPU to the CPU
	start = clock();
	copyResultFromGPU();
	end = clock();
	elapsed = (end - start) / (double) CLOCKS_PER_SEC;
	printf("\tTransfer from GPU: %f seconds\n", elapsed);
	
	// Free the GPU memory
	check_error(hipFree(A_GPU));
	check_error(hipFree(B_GPU));
	check_error(hipFree(C_GPU));	
}

// allocate and initialize A and B using a random number generator
void allocateAndInitializeAB() {
	
	size_t sizeofA = A_MD.dimension1 * A_MD.dimension2 * sizeof(float);
	A = (float*) malloc(sizeofA);
	
	srand(time(NULL));
  	for (int i = 0; i < A_MD.dimension1; i++) {
		for (int j = 0; j < A_MD.dimension2; j++) {
			int index = i * A_MD.dimension2 + j;
			A[index] = (rand() % 1000) * 0.001; 
		}
	}
	
	size_t sizeofB = B_MD.dimension1 * B_MD.dimension2 * sizeof(float);
	B = (float*) malloc(sizeofB);
  	for (int i = 0; i < B_MD.dimension1; i++) {
		for (int j = 0; j < B_MD.dimension2; j++) {
			int index = i * B_MD.dimension2 + j;
			B[index] = (rand() % 1000) * 0.001; 
		}
	}
}

// allocate memory in the GPU for all matrices, and copy A and B content from the host CPU memory to the GPU memory
void copyMatricesToGPU() {
	
	size_t sizeofA = A_MD.dimension1 * A_MD.dimension2 * sizeof(float);
	check_error(hipMalloc((void **) &A_GPU, sizeofA));
	check_error(hipMemcpy(A_GPU, A, sizeofA, hipMemcpyHostToDevice));
	
	size_t sizeofB = B_MD.dimension1 * B_MD.dimension2 * sizeof(float);
	check_error(hipMalloc((void **) &B_GPU, sizeofB));
	check_error(hipMemcpy(B_GPU, B, sizeofB, hipMemcpyHostToDevice));
	
	size_t sizeofC = C_MD.dimension1 * C_MD.dimension2 * sizeof(float);
	check_error(hipMalloc((void **) &C_GPU, sizeofC));
}

// copy results from C_GPU which is in GPU card memory to C_CPU which is in the host CPU for result comparison
void copyResultFromGPU() {
	size_t sizeofC = C_MD.dimension1 * C_MD.dimension2 * sizeof(float);
	C_CPU = (float*) malloc(sizeofC);
	check_error(hipMemcpy(C_CPU, C_GPU, sizeofC, hipMemcpyDeviceToHost));
}

// do a straightforward matrix-matrix multiplication in the CPU
// notice that this implementation can be massively improved in the CPU by doing proper cache blocking but we are
// not providing you the efficient CPU implementation as that reveals too much about the ideal GPU implementation
void computeCpuMMM() {
	
	// allocate the result matrix for the CPU computation
	size_t sizeofC = C_MD.dimension1 * C_MD.dimension2 * sizeof(float);
	C = (float*) malloc(sizeofC);
	
	// compute C[i][j] as the sum of A[i][k] * B[k][j] for all columns k of A
	for (int i = 0; i < A_MD.dimension1; i++) {
		int a_i = i * A_MD.dimension2;
		int c_i = i * C_MD.dimension2;
		for (int j = 0; j < B_MD.dimension2; j++) {
			int c_index = c_i + j;
			C[c_index] = 0;
			for (int k = 0; k < B_MD.dimension1; k++) {
				int a_index = a_i + k;
				int b_index = k * B_MD.dimension2 + j;
				C[c_index] += A[a_index] * B[b_index];
			}
		}
	}
}

// function to determine if the GPU computation is done correctly by comparing the output from the GPU with that
// from the CPU
void compareHostAndGpuOutput() {
	int totalElements = C_MD.dimension1 * C_MD.dimension2;
	int missmatchCount = 0;
	for (int i = 0; i < totalElements; i++) {
		if (fabs(C[i] - C_CPU[i]) > 0.01) {
			missmatchCount++;
			printf("mismatch at index %i: %f\t%f\n", i, C[i], C_CPU[i]);
		}
	}
	if (missmatchCount > 0) {
		printf("Computation is incorrect: outputs do not match in %d indexes\n", missmatchCount);
	} else {
		printf("Computation is correct: CPU and GPU outputs match\n");
	}
}

// Prints the specified error message and then exits
void die(const char *error) {
        printf("%s", error);
        exit(1);
}

// If the specified error code refers to a real error, report it and quit the program
void check_error(hipError_t e) {
        if (e != hipSuccess) {
                printf("\nCUDA error: %s\n", hipGetErrorString(e));
                exit(1);
        }
}


	.text
	.file	"matrix_multiply_cuda.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI0_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$1, %edi
	jle	.LBB0_1
# %bb.2:
	movq	%rsi, %r14
	movl	%edi, %r15d
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %ebx
	movl	%eax, A_MD(%rip)
	cmpl	$2, %r15d
	je	.LBB0_3
# %bb.4:
	movq	16(%r14), %rdi
	callq	atoi
	movl	%eax, %ebp
	movl	%eax, A_MD+4(%rip)
	cmpl	$3, %r15d
	jbe	.LBB0_5
# %bb.6:
	movq	24(%r14), %rdi
	callq	atoi
	movl	%eax, B_MD(%rip)
	cmpl	$4, %r15d
	je	.LBB0_8
# %bb.7:
	movq	32(%r14), %rdi
	callq	atoi
	jmp	.LBB0_8
.LBB0_1:                                # %.thread
	movl	$100, A_MD(%rip)
	movl	$100, %ebx
.LBB0_3:                                # %.thread11
	movl	%ebx, A_MD+4(%rip)
	movl	%ebx, %ebp
.LBB0_5:                                # %.thread16
	movl	%ebp, B_MD(%rip)
	movl	%ebp, %eax
.LBB0_8:
	movl	%eax, B_MD+4(%rip)
	movl	%ebx, C_MD(%rip)
	movl	%eax, C_MD+4(%rip)
	movl	$.L.str, %edi
	movl	%ebx, %esi
	movl	%ebp, %edx
	xorl	%eax, %eax
	callq	printf
	movl	B_MD(%rip), %esi
	movl	B_MD+4(%rip), %edx
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	movl	C_MD(%rip), %esi
	movl	C_MD+4(%rip), %edx
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	callq	_Z23allocateAndInitializeABv
	callq	clock
	movq	%rax, %rbx
	callq	_Z13computeGpuMMMv
	callq	clock
	subq	%rbx, %rax
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI0_0(%rip), %xmm0
	movl	$.L.str.3, %edi
	movb	$1, %al
	callq	printf
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.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_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z23allocateAndInitializeABv
.LCPI1_0:
	.quad	0x3f50624dd2f1a9fc              # double 0.001
	.text
	.globl	_Z23allocateAndInitializeABv
	.type	_Z23allocateAndInitializeABv,@function
_Z23allocateAndInitializeABv:           # @_Z23allocateAndInitializeABv
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.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 %rbp, -16
	movslq	A_MD(%rip), %rax
	movslq	A_MD+4(%rip), %rdi
	imulq	%rax, %rdi
	shlq	$2, %rdi
	callq	malloc
	movq	%rax, A(%rip)
	xorl	%ebx, %ebx
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	movl	A_MD(%rip), %eax
	testl	%eax, %eax
	jle	.LBB1_7
# %bb.1:                                # %.preheader21.preheader
	movl	A_MD+4(%rip), %ebp
.LBB1_2:                                # %.preheader21
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
	testl	%ebp, %ebp
	jle	.LBB1_6
# %bb.3:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB1_2 Depth=1
	xorl	%r14d, %r14d
.LBB1_4:                                # %.lr.ph
                                        #   Parent Loop BB1_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	imull	%ebx, %ebp
	addl	%r14d, %ebp
	callq	rand
	movsd	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	mulsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movq	A(%rip), %rax
	movslq	%ebp, %rcx
	movss	%xmm0, (%rax,%rcx,4)
	incl	%r14d
	movl	A_MD+4(%rip), %ebp
	cmpl	%ebp, %r14d
	jl	.LBB1_4
# %bb.5:                                # %._crit_edge.loopexit
                                        #   in Loop: Header=BB1_2 Depth=1
	movl	A_MD(%rip), %eax
.LBB1_6:                                # %._crit_edge
                                        #   in Loop: Header=BB1_2 Depth=1
	incl	%ebx
	cmpl	%eax, %ebx
	jl	.LBB1_2
.LBB1_7:                                # %._crit_edge24
	movl	B_MD(%rip), %r14d
	movl	B_MD+4(%rip), %ebx
	movl	%ebx, %eax
	imull	%r14d, %eax
	movslq	%eax, %rdi
	shlq	$2, %rdi
	callq	malloc
	movq	%rax, B(%rip)
	testl	%r14d, %r14d
	jle	.LBB1_14
# %bb.8:                                # %.preheader.preheader
	xorl	%ebp, %ebp
.LBB1_9:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_11 Depth 2
	testl	%ebx, %ebx
	jle	.LBB1_13
# %bb.10:                               # %.lr.ph26.preheader
                                        #   in Loop: Header=BB1_9 Depth=1
	xorl	%r14d, %r14d
.LBB1_11:                               # %.lr.ph26
                                        #   Parent Loop BB1_9 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	imull	%ebp, %ebx
	addl	%r14d, %ebx
	callq	rand
	movsd	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	mulsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movq	B(%rip), %rax
	movslq	%ebx, %rcx
	movss	%xmm0, (%rax,%rcx,4)
	incl	%r14d
	movl	B_MD+4(%rip), %ebx
	cmpl	%ebx, %r14d
	jl	.LBB1_11
# %bb.12:                               # %._crit_edge27.loopexit
                                        #   in Loop: Header=BB1_9 Depth=1
	movl	B_MD(%rip), %r14d
.LBB1_13:                               # %._crit_edge27
                                        #   in Loop: Header=BB1_9 Depth=1
	incl	%ebp
	cmpl	%r14d, %ebp
	jl	.LBB1_9
.LBB1_14:                               # %._crit_edge29
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z23allocateAndInitializeABv, .Lfunc_end1-_Z23allocateAndInitializeABv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z13computeGpuMMMv
.LCPI2_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	_Z13computeGpuMMMv
	.type	_Z13computeGpuMMMv,@function
_Z13computeGpuMMMv:                     # @_Z13computeGpuMMMv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	callq	clock
	movq	%rax, %rbx
	callq	_Z17copyMatricesToGPUv
	callq	clock
	subq	%rbx, %rax
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI2_0(%rip), %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	callq	clock
	movq	%rax, %rbx
	movabsq	$1344324763961, %rdi            # imm = 0x13900000139
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_2
# %bb.1:
	movq	A_GPU(%rip), %rdi
	movq	B_GPU(%rip), %rsi
	movq	C_GPU(%rip), %rdx
	movl	C_MD(%rip), %ecx
	callq	_Z33__device_stub__mm_multiply_kernelPfS_S_i
.LBB2_2:
	callq	hipDeviceSynchronize
	callq	clock
	subq	%rbx, %rax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI2_0(%rip), %xmm0
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	callq	hipGetLastError
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	callq	clock
	movq	%rax, %rbx
	callq	_Z17copyResultFromGPUv
	callq	clock
	subq	%rbx, %rax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI2_0(%rip), %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	movq	A_GPU(%rip), %rdi
	callq	hipFree
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movq	B_GPU(%rip), %rdi
	callq	hipFree
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movq	C_GPU(%rip), %rdi
	callq	hipFree
	movl	%eax, %edi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	_Z11check_error10hipError_t     # TAILCALL
.Lfunc_end2:
	.size	_Z13computeGpuMMMv, .Lfunc_end2-_Z13computeGpuMMMv
	.cfi_endproc
                                        # -- End function
	.globl	_Z33__device_stub__mm_multiply_kernelPfS_S_i # -- Begin function _Z33__device_stub__mm_multiply_kernelPfS_S_i
	.type	_Z33__device_stub__mm_multiply_kernelPfS_S_i,@function
_Z33__device_stub__mm_multiply_kernelPfS_S_i: # @_Z33__device_stub__mm_multiply_kernelPfS_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	$_Z18mm_multiply_kernelPfS_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_end3:
	.size	_Z33__device_stub__mm_multiply_kernelPfS_S_i, .Lfunc_end3-_Z33__device_stub__mm_multiply_kernelPfS_S_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z17copyMatricesToGPUv          # -- Begin function _Z17copyMatricesToGPUv
	.type	_Z17copyMatricesToGPUv,@function
_Z17copyMatricesToGPUv:                 # @_Z17copyMatricesToGPUv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movslq	A_MD(%rip), %rax
	movslq	A_MD+4(%rip), %rbx
	imulq	%rax, %rbx
	shlq	$2, %rbx
	movl	$A_GPU, %edi
	movq	%rbx, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movq	A_GPU(%rip), %rdi
	movq	A(%rip), %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movslq	B_MD(%rip), %rax
	movslq	B_MD+4(%rip), %rbx
	imulq	%rax, %rbx
	shlq	$2, %rbx
	movl	$B_GPU, %edi
	movq	%rbx, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movq	B_GPU(%rip), %rdi
	movq	B(%rip), %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movslq	C_MD(%rip), %rax
	movslq	C_MD+4(%rip), %rsi
	imulq	%rax, %rsi
	shlq	$2, %rsi
	movl	$C_GPU, %edi
	callq	hipMalloc
	movl	%eax, %edi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	_Z11check_error10hipError_t     # TAILCALL
.Lfunc_end4:
	.size	_Z17copyMatricesToGPUv, .Lfunc_end4-_Z17copyMatricesToGPUv
	.cfi_endproc
                                        # -- End function
	.globl	_Z11check_error10hipError_t     # -- Begin function _Z11check_error10hipError_t
	.type	_Z11check_error10hipError_t,@function
_Z11check_error10hipError_t:            # @_Z11check_error10hipError_t
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jne	.LBB5_2
# %bb.1:
	retq
.LBB5_2:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	hipGetErrorString
	movl	$.L.str.11, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end5:
	.size	_Z11check_error10hipError_t, .Lfunc_end5-_Z11check_error10hipError_t
	.cfi_endproc
                                        # -- End function
	.globl	_Z17copyResultFromGPUv          # -- Begin function _Z17copyResultFromGPUv
	.type	_Z17copyResultFromGPUv,@function
_Z17copyResultFromGPUv:                 # @_Z17copyResultFromGPUv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movslq	C_MD(%rip), %rax
	movslq	C_MD+4(%rip), %rbx
	imulq	%rax, %rbx
	shlq	$2, %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, C_CPU(%rip)
	movq	C_GPU(%rip), %rsi
	movq	%rax, %rdi
	movq	%rbx, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	_Z11check_error10hipError_t     # TAILCALL
.Lfunc_end6:
	.size	_Z17copyResultFromGPUv, .Lfunc_end6-_Z17copyResultFromGPUv
	.cfi_endproc
                                        # -- End function
	.globl	_Z13computeCpuMMMv              # -- Begin function _Z13computeCpuMMMv
	.type	_Z13computeCpuMMMv,@function
_Z13computeCpuMMMv:                     # @_Z13computeCpuMMMv
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movslq	C_MD(%rip), %rdi
	movslq	C_MD+4(%rip), %rbx
	imulq	%rbx, %rdi
	shlq	$2, %rdi
	callq	malloc
	movq	%rax, C(%rip)
	movl	A_MD(%rip), %ecx
	testl	%ecx, %ecx
	jle	.LBB7_9
# %bb.1:                                # %.lr.ph28
	movslq	A_MD+4(%rip), %rdx
	movl	B_MD+4(%rip), %esi
	movl	B_MD(%rip), %edi
	movq	A(%rip), %r8
	movq	B(%rip), %r9
	movslq	%esi, %r10
	shlq	$2, %rdx
	shlq	$2, %r10
	xorl	%r11d, %r11d
.LBB7_2:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB7_4 Depth 2
                                        #       Child Loop BB7_6 Depth 3
	testl	%esi, %esi
	jle	.LBB7_8
# %bb.3:                                # %.lr.ph24.preheader
                                        #   in Loop: Header=BB7_2 Depth=1
	movq	%r11, %r14
	imulq	%rbx, %r14
	leaq	(%rax,%r14,4), %r14
	movq	%r9, %r15
	xorl	%r12d, %r12d
.LBB7_4:                                # %.lr.ph24
                                        #   Parent Loop BB7_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB7_6 Depth 3
	movl	$0, (%r14,%r12,4)
	testl	%edi, %edi
	jle	.LBB7_7
# %bb.5:                                # %.lr.ph
                                        #   in Loop: Header=BB7_4 Depth=2
	xorps	%xmm0, %xmm0
	movq	%r15, %r13
	xorl	%ebp, %ebp
.LBB7_6:                                #   Parent Loop BB7_2 Depth=1
                                        #     Parent Loop BB7_4 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%r8,%rbp,4), %xmm1             # xmm1 = mem[0],zero,zero,zero
	mulss	(%r13), %xmm1
	addss	%xmm1, %xmm0
	movss	%xmm0, (%r14,%r12,4)
	incq	%rbp
	addq	%r10, %r13
	cmpq	%rbp, %rdi
	jne	.LBB7_6
.LBB7_7:                                # %._crit_edge
                                        #   in Loop: Header=BB7_4 Depth=2
	incq	%r12
	addq	$4, %r15
	cmpq	%rsi, %r12
	jne	.LBB7_4
.LBB7_8:                                # %._crit_edge25
                                        #   in Loop: Header=BB7_2 Depth=1
	incq	%r11
	addq	%rdx, %r8
	cmpq	%rcx, %r11
	jne	.LBB7_2
.LBB7_9:                                # %._crit_edge29
	addq	$8, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end7:
	.size	_Z13computeCpuMMMv, .Lfunc_end7-_Z13computeCpuMMMv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z23compareHostAndGpuOutputv
.LCPI8_0:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI8_1:
	.quad	0x3f847ae147ae147b              # double 0.01
	.text
	.globl	_Z23compareHostAndGpuOutputv
	.type	_Z23compareHostAndGpuOutputv,@function
_Z23compareHostAndGpuOutputv:           # @_Z23compareHostAndGpuOutputv
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.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 %rbp, -16
	movl	C_MD+4(%rip), %eax
	imull	C_MD(%rip), %eax
	testl	%eax, %eax
	jle	.LBB8_7
# %bb.1:                                # %.lr.ph.preheader
	movl	%eax, %r14d
	movq	C(%rip), %rax
	xorl	%ebx, %ebx
	movq	C_CPU(%rip), %rcx
	movaps	.LCPI8_0(%rip), %xmm3           # xmm3 = [NaN,NaN,NaN,NaN]
	movsd	.LCPI8_1(%rip), %xmm4           # xmm4 = mem[0],zero
	xorl	%ebp, %ebp
.LBB8_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movss	(%rax,%rbx,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	(%rcx,%rbx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	movaps	%xmm0, %xmm2
	subss	%xmm1, %xmm2
	andps	%xmm3, %xmm2
	cvtss2sd	%xmm2, %xmm2
	ucomisd	%xmm4, %xmm2
	jbe	.LBB8_4
# %bb.3:                                #   in Loop: Header=BB8_2 Depth=1
	incl	%ebp
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	movl	$.L.str.7, %edi
	movl	%ebx, %esi
	movb	$2, %al
	callq	printf
	movsd	.LCPI8_1(%rip), %xmm4           # xmm4 = mem[0],zero
	movaps	.LCPI8_0(%rip), %xmm3           # xmm3 = [NaN,NaN,NaN,NaN]
	movq	C(%rip), %rax
	movq	C_CPU(%rip), %rcx
.LBB8_4:                                #   in Loop: Header=BB8_2 Depth=1
	incq	%rbx
	cmpq	%rbx, %r14
	jne	.LBB8_2
# %bb.5:                                # %._crit_edge
	testl	%ebp, %ebp
	jle	.LBB8_7
# %bb.6:
	movl	$.L.str.8, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	printf                          # TAILCALL
.LBB8_7:                                # %._crit_edge.thread
	.cfi_def_cfa_offset 32
	movl	$.Lstr, %edi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	puts@PLT                        # TAILCALL
.Lfunc_end8:
	.size	_Z23compareHostAndGpuOutputv, .Lfunc_end8-_Z23compareHostAndGpuOutputv
	.cfi_endproc
                                        # -- End function
	.globl	_Z3diePKc                       # -- Begin function _Z3diePKc
	.type	_Z3diePKc,@function
_Z3diePKc:                              # @_Z3diePKc
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdi, %rsi
	movl	$.L.str.10, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end9:
	.size	_Z3diePKc, .Lfunc_end9-_Z3diePKc
	.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	$_Z18mm_multiply_kernelPfS_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	A_MD,@object                    # @A_MD
	.bss
	.globl	A_MD
	.p2align	2, 0x0
A_MD:
	.zero	8
	.size	A_MD, 8

	.type	B_MD,@object                    # @B_MD
	.globl	B_MD
	.p2align	2, 0x0
B_MD:
	.zero	8
	.size	B_MD, 8

	.type	C_MD,@object                    # @C_MD
	.globl	C_MD
	.p2align	2, 0x0
C_MD:
	.zero	8
	.size	C_MD, 8

	.type	A,@object                       # @A
	.globl	A
	.p2align	3, 0x0
A:
	.quad	0
	.size	A, 8

	.type	B,@object                       # @B
	.globl	B
	.p2align	3, 0x0
B:
	.quad	0
	.size	B, 8

	.type	C,@object                       # @C
	.globl	C
	.p2align	3, 0x0
C:
	.quad	0
	.size	C, 8

	.type	C_CPU,@object                   # @C_CPU
	.globl	C_CPU
	.p2align	3, 0x0
C_CPU:
	.quad	0
	.size	C_CPU, 8

	.type	A_GPU,@object                   # @A_GPU
	.globl	A_GPU
	.p2align	3, 0x0
A_GPU:
	.quad	0
	.size	A_GPU, 8

	.type	B_GPU,@object                   # @B_GPU
	.globl	B_GPU
	.p2align	3, 0x0
B_GPU:
	.quad	0
	.size	B_GPU, 8

	.type	C_GPU,@object                   # @C_GPU
	.globl	C_GPU
	.p2align	3, 0x0
C_GPU:
	.quad	0
	.size	C_GPU, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Matrix A is %d-by-%d\n"
	.size	.L.str, 22

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Matrix B is %d-by-%d\n"
	.size	.L.str.1, 22

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Matrix C is %d-by-%d\n"
	.size	.L.str.2, 22

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Computation time in the GPU: %f seconds\n"
	.size	.L.str.3, 41

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

	.type	.L.str.4,@object                # @.str.4
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.4:
	.asciz	"\nGPU: \tTransfer to GPU: %f seconds\n"
	.size	.L.str.4, 36

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"\tKernel execution: %f seconds\n"
	.size	.L.str.5, 31

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"\tTransfer from GPU: %f seconds\n"
	.size	.L.str.6, 32

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"mismatch at index %i: %f\t%f\n"
	.size	.L.str.7, 29

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Computation is incorrect: outputs do not match in %d indexes\n"
	.size	.L.str.8, 62

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"%s"
	.size	.L.str.10, 3

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"\nCUDA error: %s\n"
	.size	.L.str.11, 17

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z18mm_multiply_kernelPfS_S_i"
	.size	.L__unnamed_1, 30

	.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	"Computation is correct: CPU and GPU outputs match"
	.size	.Lstr, 50

	.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 _Z33__device_stub__mm_multiply_kernelPfS_S_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym A_GPU
	.addrsig_sym B_GPU
	.addrsig_sym C_GPU
	.addrsig_sym _Z18mm_multiply_kernelPfS_S_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z18mm_multiply_kernelPfS_S_i ; -- Begin function _Z18mm_multiply_kernelPfS_S_i
	.globl	_Z18mm_multiply_kernelPfS_S_i
	.p2align	8
	.type	_Z18mm_multiply_kernelPfS_S_i,@function
_Z18mm_multiply_kernelPfS_S_i:          ; @_Z18mm_multiply_kernelPfS_S_i
; %bb.0:
	s_clause 0x1
	s_load_b32 s3, s[0:1], 0x2c
	s_load_b32 s2, s[0:1], 0x18
	v_and_b32_e32 v2, 0x3ff, v0
	v_bfe_u32 v3, v0, 10, 10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_lshlrev_b32_e32 v10, 2, v2
	v_lshlrev_b32_e32 v9, 7, v3
	s_waitcnt lgkmcnt(0)
	s_and_b32 s8, s3, 0xffff
	s_lshr_b32 s3, s3, 16
	v_cvt_f32_u32_e32 v1, s8
	s_sub_i32 s4, 0, s8
	s_mul_i32 s14, s14, s8
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_iflag_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x4f7ffffe, v1
	v_cvt_u32_f32_e32 v1, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_readfirstlane_b32 s9, v1
	v_mad_u64_u32 v[0:1], null, s15, s3, v[3:4]
	s_mul_i32 s4, s4, s9
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_4) | instid1(SALU_CYCLE_1)
	s_mul_hi_u32 s10, s9, s4
	s_clause 0x1
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[0:1], s[0:1], 0x10
	s_add_i32 s9, s9, s10
	s_mul_hi_u32 s9, s2, s9
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	s_mul_i32 s10, s9, s8
	s_add_i32 s11, s9, 1
	s_sub_i32 s10, s2, s10
	s_sub_i32 s12, s10, s8
	s_cmp_ge_u32 s10, s8
	s_cselect_b32 s9, s11, s9
	s_cselect_b32 s10, s12, s10
	s_add_i32 s11, s9, 1
	s_cmp_ge_u32 s10, s8
	s_cselect_b32 s10, s11, s9
	s_mov_b32 s9, 0
	s_cmp_lt_i32 s10, 1
	s_cbranch_scc1 .LBB0_3
; %bb.1:                                ; %.lr.ph
	v_mul_lo_u32 v6, v3, s2
	v_dual_mov_b32 v8, 0 :: v_dual_lshlrev_b32 v1, 7, v3
	v_or_b32_e32 v11, 0x1000, v10
	v_mad_u64_u32 v[4:5], null, v0, s2, v[2:3]
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v12, v1, v10
	s_mul_i32 s11, s2, s3
	v_add_nc_u32_e32 v13, v11, v1
	v_add3_u32 v6, v2, v6, s14
.LBB0_2:                                ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_ashrrev_i32_e32 v5, 31, v4
	v_ashrrev_i32_e32 v7, 31, v6
	s_add_i32 s9, s9, 1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	s_cmp_lt_i32 s9, s10
	v_lshlrev_b64 v[14:15], 2, v[4:5]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_4)
	v_lshlrev_b64 v[16:17], 2, v[6:7]
	v_add_nc_u32_e32 v6, s11, v6
	v_add_nc_u32_e32 v4, s8, v4
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v14, vcc_lo, s4, v14
	v_add_co_ci_u32_e32 v15, vcc_lo, s5, v15, vcc_lo
	v_add_co_u32 v16, vcc_lo, s6, v16
	v_add_co_ci_u32_e32 v17, vcc_lo, s7, v17, vcc_lo
	global_load_b32 v5, v[14:15], off
	global_load_b32 v7, v[16:17], off
	s_waitcnt vmcnt(1)
	ds_store_b32 v12, v5
	s_waitcnt vmcnt(0)
	ds_store_b32 v13, v7
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_b32 v5, v1
	ds_load_b32 v7, v11
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	v_fmac_f32_e32 v8, v5, v7
	s_delay_alu instid0(VALU_DEP_1)
	v_fmac_f32_e32 v8, v5, v7
	s_cbranch_scc1 .LBB0_2
	s_branch .LBB0_4
.LBB0_3:
	v_mov_b32_e32 v8, 0
.LBB0_4:                                ; %._crit_edge
	s_mul_i32 s10, s10, s8
	v_add_nc_u32_e32 v1, s14, v2
	s_sub_i32 s10, s2, s10
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lt_i32 s10, 1
	s_cbranch_scc1 .LBB0_12
; %bb.5:
	v_mad_u64_u32 v[4:5], null, s9, s8, v[2:3]
	s_mov_b32 s8, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e64 s2, v4
	s_cbranch_execz .LBB0_7
; %bb.6:
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[5:6], null, v0, s2, v[4:5]
	v_ashrrev_i32_e32 v6, 31, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[4:5], 2, v[5:6]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v4, vcc_lo, s4, v4
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v5, vcc_lo, s5, v5, vcc_lo
	global_load_b32 v4, v[4:5], off
	v_lshl_add_u32 v5, v3, 7, v10
	s_waitcnt vmcnt(0)
	ds_store_b32 v5, v4
.LBB0_7:
	s_or_b32 exec_lo, exec_lo, s8
	v_mad_u64_u32 v[4:5], null, s9, s3, v[3:4]
	s_mov_b32 s3, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e64 s2, v4
	s_cbranch_execz .LBB0_9
; %bb.8:
	v_mad_u64_u32 v[5:6], null, v4, s2, v[1:2]
	v_lshl_add_u32 v3, v3, 7, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v6, 31, v5
	v_lshlrev_b64 v[4:5], 2, v[5:6]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v4, vcc_lo, s6, v4
	v_add_co_ci_u32_e32 v5, vcc_lo, s7, v5, vcc_lo
	global_load_b32 v4, v[4:5], off
	s_waitcnt vmcnt(0)
	ds_store_b32 v3, v4 offset:4096
.LBB0_9:                                ; %.lr.ph172.preheader
	s_or_b32 exec_lo, exec_lo, s3
	v_lshl_or_b32 v2, v2, 2, 0x1000
	s_mov_b32 s3, 0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
.LBB0_10:                               ; %.lr.ph172
                                        ; =>This Inner Loop Header: Depth=1
	ds_load_b32 v3, v9
	ds_load_b32 v4, v2
	v_add_nc_u32_e32 v9, 4, v9
	v_add_nc_u32_e32 v2, 0x80, v2
	s_add_i32 s3, s3, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lt_i32 s3, s10
	s_waitcnt lgkmcnt(0)
	v_fmac_f32_e32 v8, v3, v4
	s_cbranch_scc1 .LBB0_10
; %bb.11:                               ; %._crit_edge173
	s_barrier
	buffer_gl0_inv
.LBB0_12:
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_max_i32_e32 v2, v0, v1
	s_mov_b32 s3, exec_lo
	v_cmpx_gt_i32_e64 s2, v2
	s_cbranch_execz .LBB0_14
; %bb.13:
	v_mad_u64_u32 v[2:3], null, v0, s2, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v3, 31, v2
	v_lshlrev_b64 v[0:1], 2, v[2:3]
	s_waitcnt lgkmcnt(0)
	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_b32 v[0:1], v8, off
.LBB0_14:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z18mm_multiply_kernelPfS_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 18
		.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	_Z18mm_multiply_kernelPfS_S_i, .Lfunc_end0-_Z18mm_multiply_kernelPfS_S_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1060
; NumSgprs: 18
; NumVgprs: 18
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 8192 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 18
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

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

	.end_amdgpu_metadata
