/*
 * Rectangular matrix multiplication
 * A[M][K] * B[k][N] = C[M][N]
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/timeb.h>
#include <string.h>

/* read timer in second */
double read_timer() {
    struct timeb tm;
    ftime(&tm);
    return (double) tm.time + (double) tm.millitm / 1000.0;
}

/* read timer in ms */
double read_timer_ms() {
    struct timeb tm;
    ftime(&tm);
    return (double) tm.time * 1000.0 + (double) tm.millitm;
}

#define REAL float

void init(int M, int N, REAL * A) {
    int i, j;

    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) {
            A[i*N+j] = (REAL) drand48();
        }
    }
}

double maxerror(int M, int N, REAL * A, REAL *B) {
    int i, j;
    double error = 0.0;

    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) {
            double diff = (A[i*N+j] - B[i*N+j]) / A[i*N+j];
            if (diff < 0)
                diff = -diff;
            if (diff > error)
                error = diff;
        }
    }
    return error;
}

void matmul_base(int N, REAL *A, REAL * B, REAL *C);
void matmul_openmp(int N, REAL *A, REAL *B, REAL *C, int num_tasks);
void matmul_cuda_v1_vanilla(int N, REAL *A, REAL *B, REAL *C);
void matmul_cuda_v1_shmem(int N, REAL *A, REAL *B, REAL *C);
void matmul_cuda_v1_cublas(int N, REAL *A, REAL *B, REAL *C);

int main(int argc, char *argv[]) {
    int N;
    int num_tasks = 5; /* 5 is default number of tasks */
    double elapsed_base, elapsed_openmp, elapsed_cuda_v1, elapsed_cuda_v2, elapsed_cuda_v3; /* for timing */
    if (argc < 2) {
        fprintf(stderr, "Usage: matmul <n> [<#tasks(%d)>]\n", num_tasks);
        exit(1);
    }
    N = atoi(argv[1]);
    if (argc > 2) num_tasks = atoi(argv[2]);
    REAL * heap_buffer = (REAL*)malloc(sizeof(REAL)*N*N*4); /* we use 5 matrix in this example */
    /* below is a cast from memory buffer to a 2-d row-major array */
    REAL *A = heap_buffer;
    REAL *B = &heap_buffer[N*N];
    REAL *C_base = &heap_buffer[2*N*N];
    REAL *C_openmp = &heap_buffer[3*N*N];

    srand48((1 << 12));
    init(N, N, A);
    init(N, N, B);

    /* example run */
    elapsed_base = read_timer();
    matmul_base(N, A, B, C_base);
    elapsed_base = (read_timer() - elapsed_base);

    elapsed_openmp = read_timer();
    matmul_openmp(N, A, B, C_openmp, num_tasks);
    elapsed_openmp = (read_timer() - elapsed_openmp);

    /* call and timing for the three CUDA versions */
    /* there are three devices you can use on gpu.secs.oakland.edu, 0, 2, 3. 
     * 1 is a graphical card with less computation capability.
     */
    cudaSetDevice(0); 
    //call and time for matmul_cuda_v1_vanilla(int N, REAL *A, REAL *B, REAL *C);

    //call and time for matmul_cuda_v1_shmem(int N, REAL *A, REAL *B, REAL *C);

    //call and time for matmul_cuda_v1_cublas(int N, REAL *A, REAL *B, REAL *C);

    printf("======================================================================================================\n");
    printf("Matrix Multiplication: A[M][K] * B[k][N] = C[M][N], M=K=N=%d, %d threads/tasks\n", N, num_tasks);
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("Performance:\t\tRuntime (ms)\t MFLOPS \t\tError (compared to base)\n");
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("matmul_base:\t\t%4f\t%4f \t\t%g\n", elapsed_base * 1.0e3, ((((2.0 * N) * N) * N) / (1.0e6 * elapsed_base)), maxerror(N, N, C_base, C_base));
    printf("matmul_openmp:\t\t%4f\t%4f \t\t%g\n", elapsed_openmp * 1.0e3, ((((2.0 * N) * N) * N) / (1.0e6 * elapsed_openmp)), maxerror(N, N, C_base, C_openmp));
    /* put other printf statements for outputing results for GPU execution */
    free(heap_buffer);
    return 0;
}

void matmul_base(int N, REAL *A, REAL * B, REAL *C) {
    int i, j, k;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            REAL temp = 0.0;
            for (k = 0; k < N; k++) {
                temp += A[i*N+k] * B[k*N+j];
            }
            C[i*N+j] = temp;
        }
    }
}

void matmul_openmp(int N, REAL *A, REAL *B, REAL *C, int num_tasks) {
    int i, j, k;
#pragma omp parallel for shared(N,A,B,C,num_tasks) private(i,j,k) num_threads(num_tasks)
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            REAL temp = 0.0;
            for (k = 0; k < N; k++) {
                temp += A[i*N+k] * B[k*N+j];
            }
            C[i*N+j] = temp;
        }
    }
}

/*
 * call to kernel that uses GPU global memory
 */
void matmul_cuda_v1_vanilla(int N, REAL *A, REAL *B, REAL *C) {

}

/*
 * call to kernel that use GPU shared memory
 */
void matmul_cuda_v1_shmem(int N, REAL *A, REAL *B, REAL *C) {

}

/*
 * call to sgemm of cublas library 
 */
void matmul_cuda_v1_cublas(int N, REAL *A, REAL *B, REAL *C) {

}

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

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2039:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2039:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z10read_timerv
	.type	_Z10read_timerv, @function
_Z10read_timerv:
.LFB2027:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	call	ftime@PLT
	cvtsi2sdq	8(%rsp), %xmm1
	movzwl	16(%rsp), %eax
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L3
	call	__stack_chk_fail@PLT
.L3:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z10read_timerv, .-_Z10read_timerv
	.globl	_Z13read_timer_msv
	.type	_Z13read_timer_msv, @function
_Z13read_timer_msv:
.LFB2028:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	call	ftime@PLT
	movzwl	16(%rsp), %eax
	cvtsi2sdq	8(%rsp), %xmm0
	mulsd	.LC0(%rip), %xmm0
	cvtsi2sdl	%eax, %xmm1
	addsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L7
	call	__stack_chk_fail@PLT
.L7:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z13read_timer_msv, .-_Z13read_timer_msv
	.globl	_Z4initiiPf
	.type	_Z4initiiPf, @function
_Z4initiiPf:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	%edi, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	xorl	%r14d, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	xorl	%r13d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
.L10:
	cmpl	%r15d, %r14d
	jge	.L9
	movslq	%r13d, %rax
	xorl	%ebx, %ebx
	leaq	(%r12,%rax,4), %rax
.L13:
	cmpl	%ebx, %ebp
	jle	.L15
	movq	%rax, 8(%rsp)
	call	drand48@PLT
	movq	8(%rsp), %rax
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rax,%rbx,4)
	incq	%rbx
	jmp	.L13
.L15:
	incl	%r14d
	addl	%ebp, %r13d
	jmp	.L10
.L9:
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z4initiiPf, .-_Z4initiiPf
	.globl	_Z8maxerroriiPfS_
	.type	_Z8maxerroriiPfS_, @function
_Z8maxerroriiPfS_:
.LFB2030:
	.cfi_startproc
	endbr64
	movl	%edi, %r10d
	xorl	%r8d, %r8d
	xorps	%xmm1, %xmm1
	xorl	%r9d, %r9d
	movq	.LC3(%rip), %xmm4
	xorps	%xmm3, %xmm3
.L17:
	cmpl	%r10d, %r9d
	jge	.L16
	movslq	%r8d, %rax
	xorl	%edi, %edi
	salq	$2, %rax
.L23:
	cmpl	%esi, %edi
	jge	.L25
	movss	(%rdx,%rax), %xmm2
	movaps	%xmm2, %xmm0
	subss	(%rcx,%rax), %xmm0
	divss	%xmm2, %xmm0
	comiss	%xmm0, %xmm3
	cvtss2sd	%xmm0, %xmm2
	jbe	.L18
	xorps	%xmm4, %xmm2
.L18:
	maxsd	%xmm1, %xmm2
	incl	%edi
	addq	$4, %rax
	movaps	%xmm2, %xmm1
	jmp	.L23
.L25:
	incl	%r9d
	addl	%esi, %r8d
	jmp	.L17
.L16:
	movaps	%xmm1, %xmm0
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z8maxerroriiPfS_, .-_Z8maxerroriiPfS_
	.globl	_Z11matmul_baseiPfS_S_
	.type	_Z11matmul_baseiPfS_S_, @function
_Z11matmul_baseiPfS_S_:
.LFB2032:
	.cfi_startproc
	endbr64
	movslq	%edi, %r8
	xorl	%r10d, %r10d
	movq	%rsi, %r11
	xorl	%edi, %edi
	movq	%r8, %rax
	salq	$2, %r8
	cmpl	%eax, %r10d
	jl	.L35
	ret
.L35:
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
.L31:
	xorl	%esi, %esi
	leaq	(%r11,%rdi), %rbp
	leaq	(%rcx,%rdi), %r12
.L30:
	leaq	(%rdx,%rsi,4), %rbx
	xorl	%r9d, %r9d
	xorps	%xmm0, %xmm0
.L28:
	movss	0(%rbp,%r9,4), %xmm1
	mulss	(%rbx), %xmm1
	incq	%r9
	addq	%r8, %rbx
	addss	%xmm1, %xmm0
	cmpl	%r9d, %eax
	jg	.L28
	movss	%xmm0, (%r12,%rsi,4)
	incq	%rsi
	cmpl	%esi, %eax
	jg	.L30
	incl	%r10d
	addq	%r8, %rdi
	cmpl	%eax, %r10d
	jl	.L31
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z11matmul_baseiPfS_S_, .-_Z11matmul_baseiPfS_S_
	.globl	_Z13matmul_openmpiPfS_S_i
	.type	_Z13matmul_openmpiPfS_S_i, @function
_Z13matmul_openmpiPfS_S_i:
.LFB2033:
	.cfi_startproc
	endbr64
	movslq	%edi, %r8
	xorl	%r10d, %r10d
	movq	%rsi, %r11
	xorl	%edi, %edi
	movq	%r8, %rax
	salq	$2, %r8
	cmpl	%eax, %r10d
	jl	.L45
	ret
.L45:
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
.L41:
	xorl	%esi, %esi
	leaq	(%r11,%rdi), %rbp
	leaq	(%rcx,%rdi), %r12
.L40:
	leaq	(%rdx,%rsi,4), %rbx
	xorl	%r9d, %r9d
	xorps	%xmm0, %xmm0
.L38:
	movss	0(%rbp,%r9,4), %xmm1
	mulss	(%rbx), %xmm1
	incq	%r9
	addq	%r8, %rbx
	addss	%xmm1, %xmm0
	cmpl	%r9d, %eax
	jg	.L38
	movss	%xmm0, (%r12,%rsi,4)
	incq	%rsi
	cmpl	%esi, %eax
	jg	.L40
	incl	%r10d
	addq	%r8, %rdi
	cmpl	%eax, %r10d
	jl	.L41
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2033:
	.size	_Z13matmul_openmpiPfS_S_i, .-_Z13matmul_openmpiPfS_S_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC4:
	.string	"Usage: matmul <n> [<#tasks(%d)>]\n"
.LC5:
	.string	"======================================================================================================\n"
.LC6:
	.string	"Matrix Multiplication: A[M][K] * B[k][N] = C[M][N], M=K=N=%d, %d threads/tasks\n"
.LC7:
	.string	"------------------------------------------------------------------------------------------------------\n"
.LC8:
	.string	"Performance:\t\tRuntime (ms)\t MFLOPS \t\tError (compared to base)\n"
.LC10:
	.string	"matmul_base:\t\t%4f\t%4f \t\t%g\n"
.LC11:
	.string	"matmul_openmp:\t\t%4f\t%4f \t\t%g\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	cmpl	$1, %edi
	jg	.L47
	movq	stderr(%rip), %rdi
	movl	$5, %ecx
	leaq	.LC4(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L47:
	movl	%edi, %r12d
	movq	8(%rsi), %rdi
	movq	%rsi, %rbp
	movl	$5, %r14d
	call	atoi@PLT
	movl	%eax, %ebx
	cmpl	$2, %r12d
	je	.L48
	movq	16(%rbp), %rdi
	call	atoi@PLT
	movl	%eax, %r14d
.L48:
	movslq	%ebx, %rbp
	imulq	%rbp, %rbp
	movq	%rbp, %rdi
	salq	$2, %rbp
	salq	$4, %rdi
	call	malloc@PLT
	movl	$4096, %edi
	movq	%rax, %r12
	leaq	(%rax,%rbp), %r15
	movl	%ebx, %eax
	imull	%ebx, %eax
	addl	%eax, %eax
	cltq
	leaq	(%r12,%rax,4), %r13
	call	srand48@PLT
	movq	%r12, %rdx
	movl	%ebx, %esi
	movl	%ebx, %edi
	call	_Z4initiiPf
	movq	%r15, %rdx
	movl	%ebx, %esi
	movl	%ebx, %edi
	call	_Z4initiiPf
	addq	%r13, %rbp
	call	_Z10read_timerv
	movq	%r13, %rcx
	movq	%r15, %rdx
	movq	%r12, %rsi
	movl	%ebx, %edi
	movsd	%xmm0, 8(%rsp)
	call	_Z11matmul_baseiPfS_S_
	call	_Z10read_timerv
	movaps	%xmm0, %xmm1
	subsd	8(%rsp), %xmm1
	movsd	%xmm1, 24(%rsp)
	call	_Z10read_timerv
	movl	%r14d, %r8d
	movq	%rbp, %rcx
	movq	%r15, %rdx
	movq	%r12, %rsi
	movl	%ebx, %edi
	movsd	%xmm0, 8(%rsp)
	call	_Z13matmul_openmpiPfS_S_i
	call	_Z10read_timerv
	xorl	%edi, %edi
	movaps	%xmm0, %xmm3
	subsd	8(%rsp), %xmm3
	movsd	%xmm3, 16(%rsp)
	call	cudaSetDevice@PLT
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%r14d, %ecx
	movl	%ebx, %edx
	movl	$2, %edi
	leaq	.LC7(%rip), %r14
	leaq	.LC6(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r14, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r14, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	cvtsi2sdl	%ebx, %xmm5
	movq	%r13, %rcx
	movq	%r13, %rdx
	movl	%ebx, %esi
	movl	%ebx, %edi
	call	_Z8maxerroriiPfS_
	movsd	24(%rsp), %xmm1
	movl	$2, %edi
	movb	$3, %al
	movsd	.LC9(%rip), %xmm6
	movaps	%xmm0, %xmm2
	movsd	.LC0(%rip), %xmm0
	leaq	.LC10(%rip), %rsi
	movaps	%xmm5, %xmm4
	addsd	%xmm5, %xmm4
	mulsd	%xmm1, %xmm6
	mulsd	%xmm1, %xmm0
	mulsd	%xmm5, %xmm4
	mulsd	%xmm5, %xmm4
	movaps	%xmm4, %xmm1
	movsd	%xmm4, 8(%rsp)
	divsd	%xmm6, %xmm1
	call	__printf_chk@PLT
	movq	%rbp, %rcx
	movq	%r13, %rdx
	movl	%ebx, %esi
	movl	%ebx, %edi
	call	_Z8maxerroriiPfS_
	movsd	16(%rsp), %xmm3
	movb	$3, %al
	movsd	.LC9(%rip), %xmm1
	movsd	8(%rsp), %xmm4
	movaps	%xmm0, %xmm2
	movl	$2, %edi
	movsd	.LC0(%rip), %xmm0
	mulsd	%xmm3, %xmm1
	leaq	.LC11(%rip), %rsi
	mulsd	%xmm3, %xmm0
	divsd	%xmm1, %xmm4
	movaps	%xmm4, %xmm1
	call	__printf_chk@PLT
	movq	%r12, %rdi
	call	free@PLT
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	main, .-main
	.text
	.globl	_Z22matmul_cuda_v1_vanillaiPfS_S_
	.type	_Z22matmul_cuda_v1_vanillaiPfS_S_, @function
_Z22matmul_cuda_v1_vanillaiPfS_S_:
.LFB2034:
	.cfi_startproc
	endbr64
	ret
	.cfi_endproc
.LFE2034:
	.size	_Z22matmul_cuda_v1_vanillaiPfS_S_, .-_Z22matmul_cuda_v1_vanillaiPfS_S_
	.globl	_Z20matmul_cuda_v1_shmemiPfS_S_
	.type	_Z20matmul_cuda_v1_shmemiPfS_S_, @function
_Z20matmul_cuda_v1_shmemiPfS_S_:
.LFB2178:
	.cfi_startproc
	endbr64
	ret
	.cfi_endproc
.LFE2178:
	.size	_Z20matmul_cuda_v1_shmemiPfS_S_, .-_Z20matmul_cuda_v1_shmemiPfS_S_
	.globl	_Z21matmul_cuda_v1_cublasiPfS_S_
	.type	_Z21matmul_cuda_v1_cublasiPfS_S_, @function
_Z21matmul_cuda_v1_cublasiPfS_S_:
.LFB2180:
	.cfi_startproc
	endbr64
	ret
	.cfi_endproc
.LFE2180:
	.size	_Z21matmul_cuda_v1_cublasiPfS_S_, .-_Z21matmul_cuda_v1_cublasiPfS_S_
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2062:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2062:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	0
	.long	1083129856
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC3:
	.long	0
	.long	-2147483648
	.long	0
	.long	0
	.section	.rodata.cst8
	.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:

/*
 * Rectangular matrix multiplication
 * A[M][K] * B[k][N] = C[M][N]
 *
 */

#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/timeb.h>
#include <string.h>

/* read timer in second */
double read_timer() {
    struct timeb tm;
    ftime(&tm);
    return (double) tm.time + (double) tm.millitm / 1000.0;
}

/* read timer in ms */
double read_timer_ms() {
    struct timeb tm;
    ftime(&tm);
    return (double) tm.time * 1000.0 + (double) tm.millitm;
}

#define REAL float

void init(int M, int N, REAL * A) {
    int i, j;

    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) {
            A[i*N+j] = (REAL) drand48();
        }
    }
}

double maxerror(int M, int N, REAL * A, REAL *B) {
    int i, j;
    double error = 0.0;

    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) {
            double diff = (A[i*N+j] - B[i*N+j]) / A[i*N+j];
            if (diff < 0)
                diff = -diff;
            if (diff > error)
                error = diff;
        }
    }
    return error;
}

void matmul_base(int N, REAL *A, REAL * B, REAL *C);
void matmul_openmp(int N, REAL *A, REAL *B, REAL *C, int num_tasks);
void matmul_cuda_v1_vanilla(int N, REAL *A, REAL *B, REAL *C);
void matmul_cuda_v1_shmem(int N, REAL *A, REAL *B, REAL *C);
void matmul_cuda_v1_cublas(int N, REAL *A, REAL *B, REAL *C);

int main(int argc, char *argv[]) {
    int N;
    int num_tasks = 5; /* 5 is default number of tasks */
    double elapsed_base, elapsed_openmp, elapsed_cuda_v1, elapsed_cuda_v2, elapsed_cuda_v3; /* for timing */
    if (argc < 2) {
        fprintf(stderr, "Usage: matmul <n> [<#tasks(%d)>]\n", num_tasks);
        exit(1);
    }
    N = atoi(argv[1]);
    if (argc > 2) num_tasks = atoi(argv[2]);
    REAL * heap_buffer = (REAL*)malloc(sizeof(REAL)*N*N*4); /* we use 5 matrix in this example */
    /* below is a cast from memory buffer to a 2-d row-major array */
    REAL *A = heap_buffer;
    REAL *B = &heap_buffer[N*N];
    REAL *C_base = &heap_buffer[2*N*N];
    REAL *C_openmp = &heap_buffer[3*N*N];

    srand48((1 << 12));
    init(N, N, A);
    init(N, N, B);

    /* example run */
    elapsed_base = read_timer();
    matmul_base(N, A, B, C_base);
    elapsed_base = (read_timer() - elapsed_base);

    elapsed_openmp = read_timer();
    matmul_openmp(N, A, B, C_openmp, num_tasks);
    elapsed_openmp = (read_timer() - elapsed_openmp);

    /* call and timing for the three CUDA versions */
    /* there are three devices you can use on gpu.secs.oakland.edu, 0, 2, 3. 
     * 1 is a graphical card with less computation capability.
     */
    hipSetDevice(0); 
    //call and time for matmul_cuda_v1_vanilla(int N, REAL *A, REAL *B, REAL *C);

    //call and time for matmul_cuda_v1_shmem(int N, REAL *A, REAL *B, REAL *C);

    //call and time for matmul_cuda_v1_cublas(int N, REAL *A, REAL *B, REAL *C);

    printf("======================================================================================================\n");
    printf("Matrix Multiplication: A[M][K] * B[k][N] = C[M][N], M=K=N=%d, %d threads/tasks\n", N, num_tasks);
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("Performance:\t\tRuntime (ms)\t MFLOPS \t\tError (compared to base)\n");
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("matmul_base:\t\t%4f\t%4f \t\t%g\n", elapsed_base * 1.0e3, ((((2.0 * N) * N) * N) / (1.0e6 * elapsed_base)), maxerror(N, N, C_base, C_base));
    printf("matmul_openmp:\t\t%4f\t%4f \t\t%g\n", elapsed_openmp * 1.0e3, ((((2.0 * N) * N) * N) / (1.0e6 * elapsed_openmp)), maxerror(N, N, C_base, C_openmp));
    /* put other printf statements for outputing results for GPU execution */
    free(heap_buffer);
    return 0;
}

void matmul_base(int N, REAL *A, REAL * B, REAL *C) {
    int i, j, k;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            REAL temp = 0.0;
            for (k = 0; k < N; k++) {
                temp += A[i*N+k] * B[k*N+j];
            }
            C[i*N+j] = temp;
        }
    }
}

void matmul_openmp(int N, REAL *A, REAL *B, REAL *C, int num_tasks) {
    int i, j, k;
#pragma omp parallel for shared(N,A,B,C,num_tasks) private(i,j,k) num_threads(num_tasks)
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            REAL temp = 0.0;
            for (k = 0; k < N; k++) {
                temp += A[i*N+k] * B[k*N+j];
            }
            C[i*N+j] = temp;
        }
    }
}

/*
 * call to kernel that uses GPU global memory
 */
void matmul_cuda_v1_vanilla(int N, REAL *A, REAL *B, REAL *C) {

}

/*
 * call to kernel that use GPU shared memory
 */
void matmul_cuda_v1_shmem(int N, REAL *A, REAL *B, REAL *C) {

}

/*
 * call to sgemm of cublas library 
 */
void matmul_cuda_v1_cublas(int N, REAL *A, REAL *B, REAL *C) {

}

	.text
	.file	"matmul.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z10read_timerv
.LCPI0_0:
	.quad	0x408f400000000000              # double 1000
	.text
	.globl	_Z10read_timerv
	.type	_Z10read_timerv,@function
_Z10read_timerv:                        # @_Z10read_timerv
	.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
	movq	%rbx, %rdi
	callq	ftime
	cvtsi2sdq	(%rbx), %xmm1
	movzwl	8(%rbx), %eax
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI0_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z10read_timerv, .Lfunc_end0-_Z10read_timerv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z13read_timer_msv
.LCPI1_0:
	.quad	0x408f400000000000              # double 1000
	.text
	.globl	_Z13read_timer_msv
	.type	_Z13read_timer_msv,@function
_Z13read_timer_msv:                     # @_Z13read_timer_msv
	.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
	movq	%rbx, %rdi
	callq	ftime
	cvtsi2sdq	(%rbx), %xmm1
	mulsd	.LCPI1_0(%rip), %xmm1
	movzwl	8(%rbx), %eax
	cvtsi2sd	%eax, %xmm0
	addsd	%xmm1, %xmm0
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z13read_timer_msv, .Lfunc_end1-_Z13read_timer_msv
	.cfi_endproc
                                        # -- End function
	.globl	_Z4initiiPf                     # -- Begin function _Z4initiiPf
	.type	_Z4initiiPf,@function
_Z4initiiPf:                            # @_Z4initiiPf
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdx, 8(%rsp)                   # 8-byte Spill
	testl	%edi, %edi
	jle	.LBB2_6
# %bb.1:                                # %.preheader.lr.ph
	movl	%esi, %ebp
	movl	%edi, %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	%esi, %r15d
	xorl	%r12d, %r12d
	xorl	%r13d, %r13d
.LBB2_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_4 Depth 2
	testl	%ebp, %ebp
	jle	.LBB2_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB2_2 Depth=1
	movl	%r12d, %eax
	movq	8(%rsp), %rcx                   # 8-byte Reload
	leaq	(%rcx,%rax,4), %rbx
	xorl	%r14d, %r14d
.LBB2_4:                                #   Parent Loop BB2_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	drand48
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rbx,%r14,4)
	incq	%r14
	cmpq	%r14, %r15
	jne	.LBB2_4
.LBB2_5:                                # %._crit_edge
                                        #   in Loop: Header=BB2_2 Depth=1
	incq	%r13
	addl	%ebp, %r12d
	cmpq	16(%rsp), %r13                  # 8-byte Folded Reload
	jne	.LBB2_2
.LBB2_6:                                # %._crit_edge13
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z4initiiPf, .Lfunc_end2-_Z4initiiPf
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z8maxerroriiPfS_
.LCPI3_0:
	.quad	0x8000000000000000              # double -0
	.quad	0x8000000000000000              # double -0
	.text
	.globl	_Z8maxerroriiPfS_
	.type	_Z8maxerroriiPfS_,@function
_Z8maxerroriiPfS_:                      # @_Z8maxerroriiPfS_
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jle	.LBB3_1
# %bb.3:                                # %.preheader.lr.ph
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%edi, %eax
	movl	%esi, %edi
	xorpd	%xmm3, %xmm3
	xorl	%r8d, %r8d
	xorps	%xmm1, %xmm1
	movaps	.LCPI3_0(%rip), %xmm2           # xmm2 = [-0.0E+0,-0.0E+0]
	xorl	%r9d, %r9d
.LBB3_4:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_7 Depth 2
	testl	%esi, %esi
	jle	.LBB3_5
# %bb.6:                                # %.lr.ph
                                        #   in Loop: Header=BB3_4 Depth=1
	movl	%r8d, %r11d
	leaq	(%rcx,%r11,4), %r10
	leaq	(%rdx,%r11,4), %r11
	xorl	%ebx, %ebx
.LBB3_7:                                #   Parent Loop BB3_4 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%r11,%rbx,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movaps	%xmm0, %xmm4
	subss	(%r10,%rbx,4), %xmm4
	divss	%xmm0, %xmm4
	xorps	%xmm0, %xmm0
	cvtss2sd	%xmm4, %xmm0
	ucomiss	%xmm4, %xmm1
	jbe	.LBB3_9
# %bb.8:                                #   in Loop: Header=BB3_7 Depth=2
	xorps	%xmm2, %xmm0
.LBB3_9:                                #   in Loop: Header=BB3_7 Depth=2
	maxsd	%xmm3, %xmm0
	incq	%rbx
	movapd	%xmm0, %xmm3
	cmpq	%rbx, %rdi
	jne	.LBB3_7
	jmp	.LBB3_10
.LBB3_5:                                #   in Loop: Header=BB3_4 Depth=1
	movapd	%xmm3, %xmm0
.LBB3_10:                               # %._crit_edge
                                        #   in Loop: Header=BB3_4 Depth=1
	incq	%r9
	addl	%esi, %r8d
	movapd	%xmm0, %xmm3
	cmpq	%rax, %r9
	jne	.LBB3_4
# %bb.11:
	popq	%rbx
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	retq
.LBB3_1:
	xorps	%xmm0, %xmm0
	retq
.Lfunc_end3:
	.size	_Z8maxerroriiPfS_, .Lfunc_end3-_Z8maxerroriiPfS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI4_0:
	.quad	0x408f400000000000              # double 1000
.LCPI4_1:
	.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	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$1, %edi
	jle	.LBB4_18
# %bb.1:
	movq	%rsi, %r14
	movl	%edi, %r15d
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %ebx
	movl	$5, %eax
	cmpl	$2, %r15d
	je	.LBB4_3
# %bb.2:
	movq	16(%r14), %rdi
	callq	atoi
.LBB4_3:
	movl	%eax, 24(%rsp)                  # 4-byte Spill
	movslq	%ebx, %rdi
	imulq	%rdi, %rdi
	shlq	$4, %rdi
	callq	malloc
	movq	%rax, %r14
	movl	%ebx, %eax
	imull	%eax, %eax
	leaq	(%r14,%rax,4), %r13
	leal	(%rax,%rax), %ecx
	movslq	%ecx, %rcx
	leaq	(%r14,%rcx,4), %rbp
	leal	(%rax,%rax,2), %eax
	movslq	%eax, %r12
	movl	$4096, %edi                     # imm = 0x1000
	callq	srand48
	movl	%ebx, %edi
	movl	%ebx, %esi
	movq	%r14, %rdx
	callq	_Z4initiiPf
	movl	%ebx, %edi
	movl	%ebx, %esi
	movq	%r13, %rdx
	callq	_Z4initiiPf
	leaq	72(%rsp), %rdi
	callq	ftime
	movl	%ebx, %r15d
	testl	%ebx, %ebx
	jle	.LBB4_10
# %bb.4:                                # %.preheader26.lr.ph.i
	leaq	(,%r15,4), %rax
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.LBB4_5:                                # %.preheader.lr.ph.i
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_6 Depth 2
                                        #       Child Loop BB4_7 Depth 3
	movl	%ecx, %esi
	leaq	(%r14,%rsi,4), %rsi
	movq	%rdx, %rdi
	imulq	%r15, %rdi
	leaq	(,%rdi,4), %rdi
	addq	%rbp, %rdi
	movq	%r13, %r8
	xorl	%r9d, %r9d
.LBB4_6:                                # %.preheader.i
                                        #   Parent Loop BB4_5 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB4_7 Depth 3
	xorps	%xmm0, %xmm0
	movq	%r8, %r10
	xorl	%r11d, %r11d
.LBB4_7:                                #   Parent Loop BB4_5 Depth=1
                                        #     Parent Loop BB4_6 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%rsi,%r11), %xmm1              # xmm1 = mem[0],zero,zero,zero
	mulss	(%r10), %xmm1
	addss	%xmm1, %xmm0
	addq	$4, %r11
	addq	%rax, %r10
	cmpq	%r11, %rax
	jne	.LBB4_7
# %bb.8:                                # %._crit_edge.i
                                        #   in Loop: Header=BB4_6 Depth=2
	movss	%xmm0, (%rdi,%r9,4)
	incq	%r9
	addq	$4, %r8
	cmpq	%r15, %r9
	jne	.LBB4_6
# %bb.9:                                # %._crit_edge30.i
                                        #   in Loop: Header=BB4_5 Depth=1
	incq	%rdx
	addl	%ebx, %ecx
	cmpq	%r15, %rdx
	jne	.LBB4_5
.LBB4_10:                               # %_Z11matmul_baseiPfS_S_.exit
	movq	%rbp, 64(%rsp)                  # 8-byte Spill
	leaq	(%r14,%r12,4), %r12
	leaq	72(%rsp), %rbp
	movq	(%rbp), %rax
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	movzwl	8(%rbp), %eax
	movw	%ax, 16(%rsp)                   # 2-byte Spill
	movq	%rbp, %rdi
	callq	ftime
	movq	(%rbp), %rax
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	movzwl	8(%rbp), %eax
	movw	%ax, 14(%rsp)                   # 2-byte Spill
	leaq	72(%rsp), %rbp
	movq	%rbp, %rdi
	callq	ftime
	movq	(%rbp), %rax
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movzwl	8(%rbp), %ecx
	testl	%ebx, %ebx
	jle	.LBB4_17
# %bb.11:                               # %.preheader26.lr.ph.i58
	leaq	(,%r15,4), %rdx
	xorl	%esi, %esi
	xorl	%edi, %edi
.LBB4_12:                               # %.preheader.lr.ph.i60
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_13 Depth 2
                                        #       Child Loop BB4_14 Depth 3
	movl	%esi, %eax
	leaq	(%r14,%rax,4), %r8
	movq	%rdi, %rax
	imulq	%r15, %rax
	leaq	(%r12,%rax,4), %r9
	movq	%r13, %r10
	xorl	%r11d, %r11d
.LBB4_13:                               # %.preheader.i64
                                        #   Parent Loop BB4_12 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB4_14 Depth 3
	xorps	%xmm0, %xmm0
	movq	%r10, %rbp
	xorl	%eax, %eax
.LBB4_14:                               #   Parent Loop BB4_12 Depth=1
                                        #     Parent Loop BB4_13 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%r8,%rax), %xmm1               # xmm1 = mem[0],zero,zero,zero
	mulss	(%rbp), %xmm1
	addss	%xmm1, %xmm0
	addq	$4, %rax
	addq	%rdx, %rbp
	cmpq	%rax, %rdx
	jne	.LBB4_14
# %bb.15:                               # %._crit_edge.i73
                                        #   in Loop: Header=BB4_13 Depth=2
	movss	%xmm0, (%r9,%r11,4)
	incq	%r11
	addq	$4, %r10
	cmpq	%r15, %r11
	jne	.LBB4_13
# %bb.16:                               # %._crit_edge30.i77
                                        #   in Loop: Header=BB4_12 Depth=1
	incq	%rdi
	addl	%ebx, %esi
	cmpq	%r15, %rdi
	jne	.LBB4_12
.LBB4_17:                               # %_Z13matmul_openmpiPfS_S_i.exit
	movzwl	%cx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	movsd	.LCPI4_0(%rip), %xmm1           # xmm1 = mem[0],zero
	movsd	%xmm1, 56(%rsp)                 # 8-byte Spill
	divsd	%xmm1, %xmm0
	cvtsi2sdq	32(%rsp), %xmm2         # 8-byte Folded Reload
	addsd	%xmm0, %xmm2
	movsd	%xmm2, 32(%rsp)                 # 8-byte Spill
	movzwl	14(%rsp), %eax                  # 2-byte Folded Reload
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	%xmm1, %xmm0
	xorps	%xmm2, %xmm2
	cvtsi2sdq	40(%rsp), %xmm2         # 8-byte Folded Reload
	addsd	%xmm0, %xmm2
	movzwl	16(%rsp), %eax                  # 2-byte Folded Reload
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	%xmm1, %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	48(%rsp), %xmm1         # 8-byte Folded Reload
	addsd	%xmm0, %xmm1
	subsd	%xmm1, %xmm2
	movsd	%xmm2, 16(%rsp)                 # 8-byte Spill
	leaq	72(%rsp), %r13
	movq	%r13, %rdi
	callq	ftime
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r13), %xmm0
	movzwl	8(%r13), %eax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%eax, %xmm1
	divsd	.LCPI4_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	subsd	32(%rsp), %xmm1                 # 8-byte Folded Reload
	movsd	%xmm1, 48(%rsp)                 # 8-byte Spill
	xorl	%edi, %edi
	callq	hipSetDevice
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$.L.str.2, %edi
	movl	%ebx, %esi
	movl	24(%rsp), %edx                  # 4-byte Reload
	xorl	%eax, %eax
	callq	printf
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movsd	16(%rsp), %xmm1                 # 8-byte Reload
                                        # xmm1 = mem[0],zero
	movapd	%xmm1, %xmm0
	mulsd	.LCPI4_0(%rip), %xmm0
	movsd	%xmm0, 40(%rsp)                 # 8-byte Spill
	xorps	%xmm0, %xmm0
	cvtsi2sd	%ebx, %xmm0
	movapd	%xmm0, %xmm2
	addsd	%xmm0, %xmm2
	mulsd	%xmm0, %xmm2
	mulsd	%xmm0, %xmm2
	movsd	%xmm2, 24(%rsp)                 # 8-byte Spill
	mulsd	.LCPI4_1(%rip), %xmm1
	divsd	%xmm1, %xmm2
	movsd	%xmm2, 16(%rsp)                 # 8-byte Spill
	movl	%ebx, %edi
	movl	%ebx, %esi
	movq	64(%rsp), %r15                  # 8-byte Reload
	movq	%r15, %rdx
	movq	%r15, %rcx
	callq	_Z8maxerroriiPfS_
	movapd	%xmm0, %xmm2
	movl	$.L.str.5, %edi
	movsd	40(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movsd	16(%rsp), %xmm1                 # 8-byte Reload
                                        # xmm1 = mem[0],zero
	movb	$3, %al
	callq	printf
	movsd	56(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movsd	48(%rsp), %xmm1                 # 8-byte Reload
                                        # xmm1 = mem[0],zero
	mulsd	%xmm1, %xmm0
	movsd	%xmm0, 56(%rsp)                 # 8-byte Spill
	mulsd	.LCPI4_1(%rip), %xmm1
	movsd	24(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	divsd	%xmm1, %xmm0
	movsd	%xmm0, 24(%rsp)                 # 8-byte Spill
	movl	%ebx, %edi
	movl	%ebx, %esi
	movq	%r15, %rdx
	movq	%r12, %rcx
	callq	_Z8maxerroriiPfS_
	movapd	%xmm0, %xmm2
	movl	$.L.str.6, %edi
	movsd	56(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movsd	24(%rsp), %xmm1                 # 8-byte Reload
                                        # xmm1 = mem[0],zero
	movb	$3, %al
	callq	printf
	movq	%r14, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB4_18:
	.cfi_def_cfa_offset 144
	movq	stderr(%rip), %rdi
	movl	$.L.str, %esi
	movl	$5, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end4:
	.size	main, .Lfunc_end4-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z11matmul_baseiPfS_S_          # -- Begin function _Z11matmul_baseiPfS_S_
	.type	_Z11matmul_baseiPfS_S_,@function
_Z11matmul_baseiPfS_S_:                 # @_Z11matmul_baseiPfS_S_
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jle	.LBB5_8
# %bb.1:                                # %.preheader26.lr.ph
	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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	%edi, %eax
	leaq	(,%rax,4), %r8
	xorl	%r9d, %r9d
	xorl	%r10d, %r10d
.LBB5_2:                                # %.preheader.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB5_3 Depth 2
                                        #       Child Loop BB5_4 Depth 3
	movl	%r9d, %r11d
	leaq	(%rsi,%r11,4), %r11
	movq	%r10, %rbx
	imulq	%rax, %rbx
	leaq	(%rcx,%rbx,4), %rbx
	movq	%rdx, %r14
	xorl	%r15d, %r15d
.LBB5_3:                                # %.preheader
                                        #   Parent Loop BB5_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB5_4 Depth 3
	xorps	%xmm0, %xmm0
	movq	%r14, %r12
	xorl	%r13d, %r13d
.LBB5_4:                                #   Parent Loop BB5_2 Depth=1
                                        #     Parent Loop BB5_3 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%r11,%r13,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	mulss	(%r12), %xmm1
	addss	%xmm1, %xmm0
	incq	%r13
	addq	%r8, %r12
	cmpq	%r13, %rax
	jne	.LBB5_4
# %bb.5:                                # %._crit_edge
                                        #   in Loop: Header=BB5_3 Depth=2
	movss	%xmm0, (%rbx,%r15,4)
	incq	%r15
	addq	$4, %r14
	cmpq	%rax, %r15
	jne	.LBB5_3
# %bb.6:                                # %._crit_edge30
                                        #   in Loop: Header=BB5_2 Depth=1
	incq	%r10
	addl	%edi, %r9d
	cmpq	%rax, %r10
	jne	.LBB5_2
# %bb.7:
	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
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r13
	.cfi_restore %r14
	.cfi_restore %r15
.LBB5_8:                                # %._crit_edge32
	retq
.Lfunc_end5:
	.size	_Z11matmul_baseiPfS_S_, .Lfunc_end5-_Z11matmul_baseiPfS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z13matmul_openmpiPfS_S_i       # -- Begin function _Z13matmul_openmpiPfS_S_i
	.type	_Z13matmul_openmpiPfS_S_i,@function
_Z13matmul_openmpiPfS_S_i:              # @_Z13matmul_openmpiPfS_S_i
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jle	.LBB6_8
# %bb.1:                                # %.preheader26.lr.ph
	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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	%edi, %eax
	leaq	(,%rax,4), %r8
	xorl	%r9d, %r9d
	xorl	%r10d, %r10d
.LBB6_2:                                # %.preheader.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB6_3 Depth 2
                                        #       Child Loop BB6_4 Depth 3
	movl	%r9d, %r11d
	leaq	(%rsi,%r11,4), %r11
	movq	%r10, %rbx
	imulq	%rax, %rbx
	leaq	(%rcx,%rbx,4), %rbx
	movq	%rdx, %r14
	xorl	%r15d, %r15d
.LBB6_3:                                # %.preheader
                                        #   Parent Loop BB6_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB6_4 Depth 3
	xorps	%xmm0, %xmm0
	movq	%r14, %r12
	xorl	%r13d, %r13d
.LBB6_4:                                #   Parent Loop BB6_2 Depth=1
                                        #     Parent Loop BB6_3 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%r11,%r13,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	mulss	(%r12), %xmm1
	addss	%xmm1, %xmm0
	incq	%r13
	addq	%r8, %r12
	cmpq	%r13, %rax
	jne	.LBB6_4
# %bb.5:                                # %._crit_edge
                                        #   in Loop: Header=BB6_3 Depth=2
	movss	%xmm0, (%rbx,%r15,4)
	incq	%r15
	addq	$4, %r14
	cmpq	%rax, %r15
	jne	.LBB6_3
# %bb.6:                                # %._crit_edge30
                                        #   in Loop: Header=BB6_2 Depth=1
	incq	%r10
	addl	%edi, %r9d
	cmpq	%rax, %r10
	jne	.LBB6_2
# %bb.7:
	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
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r13
	.cfi_restore %r14
	.cfi_restore %r15
.LBB6_8:                                # %._crit_edge32
	retq
.Lfunc_end6:
	.size	_Z13matmul_openmpiPfS_S_i, .Lfunc_end6-_Z13matmul_openmpiPfS_S_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z22matmul_cuda_v1_vanillaiPfS_S_ # -- Begin function _Z22matmul_cuda_v1_vanillaiPfS_S_
	.type	_Z22matmul_cuda_v1_vanillaiPfS_S_,@function
_Z22matmul_cuda_v1_vanillaiPfS_S_:      # @_Z22matmul_cuda_v1_vanillaiPfS_S_
	.cfi_startproc
# %bb.0:
	retq
.Lfunc_end7:
	.size	_Z22matmul_cuda_v1_vanillaiPfS_S_, .Lfunc_end7-_Z22matmul_cuda_v1_vanillaiPfS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z20matmul_cuda_v1_shmemiPfS_S_ # -- Begin function _Z20matmul_cuda_v1_shmemiPfS_S_
	.type	_Z20matmul_cuda_v1_shmemiPfS_S_,@function
_Z20matmul_cuda_v1_shmemiPfS_S_:        # @_Z20matmul_cuda_v1_shmemiPfS_S_
	.cfi_startproc
# %bb.0:
	retq
.Lfunc_end8:
	.size	_Z20matmul_cuda_v1_shmemiPfS_S_, .Lfunc_end8-_Z20matmul_cuda_v1_shmemiPfS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z21matmul_cuda_v1_cublasiPfS_S_ # -- Begin function _Z21matmul_cuda_v1_cublasiPfS_S_
	.type	_Z21matmul_cuda_v1_cublasiPfS_S_,@function
_Z21matmul_cuda_v1_cublasiPfS_S_:       # @_Z21matmul_cuda_v1_cublasiPfS_S_
	.cfi_startproc
# %bb.0:
	retq
.Lfunc_end9:
	.size	_Z21matmul_cuda_v1_cublasiPfS_S_, .Lfunc_end9-_Z21matmul_cuda_v1_cublasiPfS_S_
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Usage: matmul <n> [<#tasks(%d)>]\n"
	.size	.L.str, 34

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Matrix Multiplication: A[M][K] * B[k][N] = C[M][N], M=K=N=%d, %d threads/tasks\n"
	.size	.L.str.2, 80

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"matmul_base:\t\t%4f\t%4f \t\t%g\n"
	.size	.L.str.5, 28

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"matmul_openmp:\t\t%4f\t%4f \t\t%g\n"
	.size	.L.str.6, 30

	.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	"======================================================================================================"
	.size	.Lstr, 103

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Performance:\t\tRuntime (ms)\t MFLOPS \t\tError (compared to base)"
	.size	.Lstr.2, 62

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"------------------------------------------------------------------------------------------------------"
	.size	.Lstr.3, 103

	.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_

	.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_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
