#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <fstream>
#include <cstdlib>
#include <math.h>

#define BLOCK_SIZE 16
#define FEATURE_LEN 128
using namespace std;

void calMeanVar(double* v, double& mean, double& var){
    double sum = 0;
    for(int i=0;i<FEATURE_LEN;i++)
        sum+=v[i];
    mean =  sum / FEATURE_LEN;
    double sq_sum = 0;
    for(int i=0;i<FEATURE_LEN;i++)
        sq_sum+=v[i]*v[i];
    var = sqrt(sq_sum / FEATURE_LEN - mean * mean);
    //printf("%f,%f\n",mean,var);
    return;
}

void normalize(double* v){
    double mean = 0;
    double var = 0;
    calMeanVar(v, mean, var);
    for(int i = 0; i < FEATURE_LEN; i++){
        v[i] = (v[i] - mean) / var;
    }
    return;
}

cudaError_t MulWithCuda(double* A, double* B, double* C, int matrixSize);
__global__ void matrixMulCUDA(double *A, double *B, double *C, int blockRow, int blockCol,int featureNum)
{
    double ans=0;
    //Need to copy A[bx*BLOCK_SIZE:(bx+1)*BLOCK_SIZE][:]
    //Need to copy B[by*BLOCK_SIZE:(by+1)*BLOCK_SIZE][:]

    __shared__ double AC[BLOCK_SIZE][FEATURE_LEN];//BLOCK_SIZE*blockCol
    __shared__ double BC[BLOCK_SIZE][FEATURE_LEN];

    //Each Thread response for #blockCol copy

    for(int k=0;k<blockCol;k++)
    {
        if((blockIdx.x*BLOCK_SIZE+threadIdx.x)<featureNum&&(k*BLOCK_SIZE+threadIdx.y)<FEATURE_LEN)
            AC[threadIdx.x][k*BLOCK_SIZE+threadIdx.y]=A[(blockIdx.x*BLOCK_SIZE+threadIdx.x)*FEATURE_LEN+(k*BLOCK_SIZE+threadIdx.y)];
        //else
            //printf("blockIdx.x blockIdx.y threadIdx.x threadIdx.y %d %d %d %d %d %d\n",blockIdx.x,blockIdx.y,threadIdx.x,threadIdx.y,blockIdx.x*BLOCK_SIZE+threadIdx.x,k*BLOCK_SIZE+threadIdx.y);
        if((blockIdx.y*BLOCK_SIZE+threadIdx.x)<featureNum&&(k*BLOCK_SIZE+threadIdx.y)<FEATURE_LEN)
            BC[threadIdx.x][k*BLOCK_SIZE+threadIdx.y]=B[(blockIdx.y*BLOCK_SIZE+threadIdx.x)*FEATURE_LEN+(k*BLOCK_SIZE+threadIdx.y)];
        //else
            //printf("blockIdx.x blockIdx.y threadIdx.x threadIdx.y %d %d %d %d %d %d\n",blockIdx.x,blockIdx.y,threadIdx.x,threadIdx.y,blockIdx.x*BLOCK_SIZE+threadIdx.x,k*BLOCK_SIZE+threadIdx.y);
    }
    __syncthreads();

    /*
    for(int i=0;i<16;i++)
    {
        for(int j=0;j<128;j++)
            printf("%f ",BC[i][j]);
        printf("\n");
    }
*/
    //if(blockIdx.x==8&&blockIdx.y==8)
    //    printf("blockIdx.x blockIdx.y threadIdx.x threadIdx.y %d %d %d %d\n",blockIdx.x,blockIdx.y,threadIdx.x,threadIdx.y);
    //if(blockIdx.x==8&&blockIdx.y==8&&threadIdx.x==0&&threadIdx.y==0)
    //        printf("%f",ans);
    for(int k=0;k<FEATURE_LEN;k++)
    {
        ans+=(AC[threadIdx.x][k]-BC[threadIdx.y][k])*(AC[threadIdx.x][k]-BC[threadIdx.y][k]);
    }

    //if(threadIdx.x==0&&threadIdx.y==0&&blockIdx.x==0&&blockIdx.y==0)
    //    printf("%f",ans);


    if((blockIdx.x*BLOCK_SIZE+threadIdx.x)<featureNum&&(blockIdx.y*BLOCK_SIZE+threadIdx.y)<featureNum)
        C[(blockIdx.x*BLOCK_SIZE+threadIdx.x)*featureNum+(blockIdx.y*BLOCK_SIZE+threadIdx.y)]=ans;
}

extern "C" int mulMatrix()
{
    int featureNum = 400;
    fstream fa("img1_400.txt",fstream::in);
    fstream fb("img2_400.txt",fstream::in);
    fstream fc("400_distance.txt",fstream::in);


    // if (argc == 1){
    //     printf("Please input your feature number!");
    //     return 1;
    // }else if (argc == 2){
    //     featureNum = atoi(argv[1]);
    //     printf("Input size: %d\n", featureNum);
    // }else{
    //     printf("Too many arguments!");
    //     return 1;
    // }

    double* A = (double*)malloc(featureNum * FEATURE_LEN * sizeof(double)); // We represent a 2D matrix in the form of 1D array.
    double* B = (double*)malloc(featureNum * FEATURE_LEN * sizeof(double)); // We represent a 2D matrix in the form of 1D array.
    double* C = (double*)malloc(featureNum * featureNum * sizeof(double)); // We represent a 2D matrix in the form of 1D array.

    // Initiate matrix A.
    for(int i = 0; i < featureNum; i++){
        for(int j = 0; j < FEATURE_LEN; j++){
            fa>>A[i * FEATURE_LEN + j];
            fb>>B[i * FEATURE_LEN + j];
        }
        normalize(A+i*FEATURE_LEN);
        normalize(B+i*FEATURE_LEN);
    }

    // Parallel.
    cudaError_t cudaStatus = MulWithCuda(A,B,C,featureNum);
    if (cudaStatus != cudaSuccess) {fprintf(stderr, "mulWithCuda failed!");return 1;}



    double err_max=0;
    printf("%f\n",C[0]);
    for(int i = 0; i < featureNum; i++){
        for(int j = 0; j < featureNum; j++){
            double tmp;
            fc>>tmp;
            err_max=err_max<abs(C[i*featureNum+j]-tmp*tmp)?C[i*featureNum+j]-tmp*tmp:err_max;

        }
    }
    printf("%f\n",err_max);

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess) {fprintf(stderr, "cudaDeviceReset failed!");return 1;}

    free(A);free(B);free(C);
    fa.close();fb.close();fc.close();
    return 0;
}

// Helper function for using CUDA to add vectors in parallel.
cudaError_t MulWithCuda(double* A, double* B, double* C, int featureNum)
{
    double *dev_A;
    double *dev_B;
    double *dev_C;
    cudaError_t cudaStatus;
	cudaEvent_t start, stop;
    float gpu_time = 0.0f;

    // Launch a kernel on the GPU with one thread for each element.
    int blockRowNum=featureNum%BLOCK_SIZE?featureNum/BLOCK_SIZE+1:featureNum/BLOCK_SIZE;
    int blockColNum=FEATURE_LEN%BLOCK_SIZE?FEATURE_LEN/BLOCK_SIZE+1:FEATURE_LEN/BLOCK_SIZE;
    dim3 gridDime(blockRowNum,blockRowNum);
    dim3 blockDime(BLOCK_SIZE,BLOCK_SIZE);

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        goto Error;
    }

    // Allocate GPU buffers for two matrix
    cudaStatus = cudaMalloc((void**)&dev_A, featureNum * FEATURE_LEN * sizeof(double));
    if (cudaStatus != cudaSuccess) {fprintf(stderr, "cudaMalloc A failed!");goto Error;}

    cudaStatus = cudaMalloc((void**)&dev_B, featureNum * FEATURE_LEN * sizeof(double));
    if (cudaStatus != cudaSuccess) {fprintf(stderr, "cudaMalloc B failed!");goto Error;}

    cudaStatus = cudaMalloc((void**)&dev_C, featureNum * featureNum * sizeof(double));
    if (cudaStatus != cudaSuccess) {fprintf(stderr, "cudaMalloc C failed!");goto Error;}

    // Copy input vectors from host memory to GPU buffers.
    cudaStatus = cudaMemcpy(dev_A, A, featureNum * FEATURE_LEN * sizeof(double), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {fprintf(stderr, "cudaMemcpy A failed!");goto Error;}

    cudaStatus = cudaMemcpy(dev_B, B, featureNum * FEATURE_LEN * sizeof(double), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {fprintf(stderr, "cudaMemcpy B failed!");goto Error;}

	// Set up timing
	cudaEventCreate(&start);
	cudaEventCreate(&stop);
	cudaEventRecord(start, 0);
	printf("Row,Col %d %d\n",blockRowNum,blockColNum);

    matrixMulCUDA<<<gridDime,blockDime>>>(dev_A,dev_B,dev_C,blockRowNum,blockColNum,featureNum);

    // copy result back
    cudaStatus = cudaMemcpy(C, dev_C, featureNum * featureNum * sizeof(double), cudaMemcpyDeviceToHost);

    if (cudaStatus != cudaSuccess) {fprintf(stderr, "cudaMemcpy C back failed!");goto Error;}



    cudaStatus = cudaDeviceSynchronize();
    if (cudaStatus != cudaSuccess) {fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching Kernel!\n", cudaStatus);goto Error;}

	// Close timing
	cudaEventRecord(stop, 0);
	cudaEventSynchronize(stop);
	cudaEventElapsedTime(&gpu_time, start, stop);
	printf("Time spent: %.5f\n", gpu_time);
	cudaEventDestroy(start);
    cudaEventDestroy(stop);

Error:
    cudaFree(dev_A);cudaFree(dev_B);cudaFree(dev_C);
    return cudaStatus;
}

	.file	"tmpxft_003891c2_00000000-6_testopencuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3772:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3772:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z10calMeanVarPdRdS0_
	.type	_Z10calMeanVarPdRdS0_, @function
_Z10calMeanVarPdRdS0_:
.LFB3766:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	xorl	%eax, %eax
	movq	%rdx, %rbx
	xorps	%xmm1, %xmm1
.L3:
	addsd	(%rdi,%rax,8), %xmm1
	incq	%rax
	cmpq	$128, %rax
	jne	.L3
	movsd	.LC1(%rip), %xmm3
	xorl	%eax, %eax
	xorps	%xmm0, %xmm0
	mulsd	%xmm3, %xmm1
	movsd	%xmm1, (%rsi)
.L4:
	movsd	(%rdi,%rax,8), %xmm2
	incq	%rax
	mulsd	%xmm2, %xmm2
	addsd	%xmm2, %xmm0
	cmpq	$128, %rax
	jne	.L4
	mulsd	%xmm3, %xmm0
	mulsd	%xmm1, %xmm1
	subsd	%xmm1, %xmm0
	call	sqrt@PLT
	movsd	%xmm0, (%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3766:
	.size	_Z10calMeanVarPdRdS0_, .-_Z10calMeanVarPdRdS0_
	.globl	_Z9normalizePd
	.type	_Z9normalizePd, @function
_Z9normalizePd:
.LFB3767:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	16(%rsp), %rdx
	leaq	8(%rsp), %rsi
	call	_Z10calMeanVarPdRdS0_
	movsd	8(%rsp), %xmm2
	movsd	16(%rsp), %xmm1
	xorl	%eax, %eax
.L10:
	movsd	(%rbx,%rax,8), %xmm0
	subsd	%xmm2, %xmm0
	divsd	%xmm1, %xmm0
	movsd	%xmm0, (%rbx,%rax,8)
	incq	%rax
	cmpq	$128, %rax
	jne	.L10
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3767:
	.size	_Z9normalizePd, .-_Z9normalizePd
	.globl	_Z40__device_stub__Z13matrixMulCUDAPdS_S_iiiPdS_S_iii
	.type	_Z40__device_stub__Z13matrixMulCUDAPdS_S_iiiPdS_S_iii, @function
_Z40__device_stub__Z13matrixMulCUDAPdS_S_iiiPdS_S_iii:
.LFB3794:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	84(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movl	%ecx, 20(%rsp)
	leaq	64(%rsp), %rcx
	movl	%r8d, 16(%rsp)
	movl	%r9d, 12(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L14
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z13matrixMulCUDAPdS_S_iii(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 208
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L14:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L16
	call	__stack_chk_fail@PLT
.L16:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3794:
	.size	_Z40__device_stub__Z13matrixMulCUDAPdS_S_iiiPdS_S_iii, .-_Z40__device_stub__Z13matrixMulCUDAPdS_S_iiiPdS_S_iii
	.globl	_Z13matrixMulCUDAPdS_S_iii
	.type	_Z13matrixMulCUDAPdS_S_iii, @function
_Z13matrixMulCUDAPdS_S_iii:
.LFB3795:
	.cfi_startproc
	endbr64
	jmp	_Z40__device_stub__Z13matrixMulCUDAPdS_S_iiiPdS_S_iii
	.cfi_endproc
.LFE3795:
	.size	_Z13matrixMulCUDAPdS_S_iii, .-_Z13matrixMulCUDAPdS_S_iii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?"
.LC4:
	.string	"cudaMalloc A failed!"
.LC5:
	.string	"cudaMalloc B failed!"
.LC6:
	.string	"cudaMalloc C failed!"
.LC7:
	.string	"cudaMemcpy A failed!"
.LC8:
	.string	"cudaMemcpy B failed!"
.LC9:
	.string	"Row,Col %d %d\n"
.LC10:
	.string	"cudaMemcpy C back failed!"
.LC11:
	.string	"cudaDeviceSynchronize returned error code %d after launching Kernel!\n"
.LC12:
	.string	"Time spent: %.5f\n"
	.text
	.globl	_Z11MulWithCudaPdS_S_i
	.type	_Z11MulWithCudaPdS_S_i, @function
_Z11MulWithCudaPdS_S_i:
.LFB3769:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rsi, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%ecx, %ebp
	movl	$16, %ecx
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$104, %rsp
	.cfi_def_cfa_offset 160
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	movl	%ebp, %eax
	movl	$0x00000000, 20(%rsp)
	cltd
	movl	$1, 72(%rsp)
	idivl	%ecx
	movl	$1, 84(%rsp)
	movl	%ebp, %edx
	andl	$15, %edx
	cmpl	$1, %edx
	sbbl	$-1, %eax
	xorl	%edi, %edi
	movl	%eax, 64(%rsp)
	movl	%eax, %r12d
	movl	%eax, 68(%rsp)
	movabsq	$68719476752, %rax
	movq	%rax, 76(%rsp)
	call	cudaSetDevice@PLT
	leaq	.LC3(%rip), %rdx
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.L35
	movl	%ebp, %eax
	leaq	24(%rsp), %rdi
	sall	$7, %eax
	cltq
	salq	$3, %rax
	movq	%rax, %rsi
	movq	%rax, (%rsp)
	call	cudaMalloc@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L23
	leaq	.LC4(%rip), %rdx
.L35:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L22
.L23:
	movq	(%rsp), %rsi
	leaq	32(%rsp), %rdi
	call	cudaMalloc@PLT
	leaq	.LC5(%rip), %rdx
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.L35
	movl	%ebp, %eax
	leaq	40(%rsp), %rdi
	imull	%ebp, %eax
	cltq
	salq	$3, %rax
	movq	%rax, %rsi
	movq	%rax, 8(%rsp)
	call	cudaMalloc@PLT
	leaq	.LC6(%rip), %rdx
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.L35
	movq	(%rsp), %rdx
	movq	24(%rsp), %rdi
	movl	$1, %ecx
	movq	%r15, %rsi
	call	cudaMemcpy@PLT
	leaq	.LC7(%rip), %rdx
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.L35
	movq	(%rsp), %rdx
	movq	32(%rsp), %rdi
	movl	$1, %ecx
	movq	%r14, %rsi
	call	cudaMemcpy@PLT
	leaq	.LC8(%rip), %rdx
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.L35
	leaq	48(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	56(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	$8, %ecx
	movl	%r12d, %edx
	xorl	%eax, %eax
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	84(%rsp), %ecx
	movl	72(%rsp), %esi
	xorl	%r9d, %r9d
	movq	76(%rsp), %rdx
	movq	64(%rsp), %rdi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L28
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	movl	%ebp, %r9d
	movl	%r12d, %ecx
	movq	24(%rsp), %rdi
	movl	$8, %r8d
	call	_Z40__device_stub__Z13matrixMulCUDAPdS_S_iiiPdS_S_iii
.L28:
	movq	8(%rsp), %rdx
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	movq	%r13, %rdi
	call	cudaMemcpy@PLT
	leaq	.LC10(%rip), %rdx
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.L35
	call	cudaDeviceSynchronize@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L30
	movq	stderr(%rip), %rdi
	movl	%eax, %ecx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC11(%rip), %rdx
	call	__fprintf_chk@PLT
	jmp	.L22
.L30:
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	56(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	leaq	20(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	20(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	48(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	56(%rsp), %rdi
	call	cudaEventDestroy@PLT
.L22:
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L31
	call	__stack_chk_fail@PLT
.L31:
	addq	$104, %rsp
	.cfi_def_cfa_offset 56
	movl	%ebx, %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
.LFE3769:
	.size	_Z11MulWithCudaPdS_S_i, .-_Z11MulWithCudaPdS_S_i
	.section	.rodata.str1.1
.LC13:
	.string	"_Z13matrixMulCUDAPdS_S_iii"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3797:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC13(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z13matrixMulCUDAPdS_S_iii(%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
.LFE3797:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.rodata.str1.1
.LC14:
	.string	"img1_400.txt"
.LC15:
	.string	"img2_400.txt"
.LC16:
	.string	"400_distance.txt"
.LC17:
	.string	"mulWithCuda failed!"
.LC18:
	.string	"%f\n"
.LC20:
	.string	"cudaDeviceReset failed!"
	.text
	.globl	mulMatrix
	.type	mulMatrix, @function
mulMatrix:
.LFB3768:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA3768
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$8, %edx
	leaq	.LC14(%rip), %rsi
	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	$1656, %rsp
	.cfi_def_cfa_offset 1712
	movq	%fs:40, %rax
	movq	%rax, 1640(%rsp)
	xorl	%eax, %eax
	leaq	56(%rsp), %rax
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
.LEHB0:
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode@PLT
.LEHE0:
	leaq	584(%rsp), %rax
	movl	$8, %edx
	leaq	.LC15(%rip), %rsi
	movq	%rax, %rdi
	movq	%rax, 16(%rsp)
.LEHB1:
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode@PLT
.LEHE1:
	leaq	1112(%rsp), %rax
	movl	$8, %edx
	leaq	.LC16(%rip), %rsi
	movq	%rax, %rdi
	movq	%rax, 24(%rsp)
.LEHB2:
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode@PLT
.LEHE2:
	movl	$409600, %edi
	xorl	%r15d, %r15d
	call	malloc@PLT
	movl	$409600, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movl	$1280000, %edi
	movq	%rbp, %r13
	movq	%rax, 40(%rsp)
	call	malloc@PLT
	movq	40(%rsp), %r14
	movq	%rax, %rbx
.L39:
	xorl	%r12d, %r12d
.L40:
	movq	8(%rsp), %rdi
	leaq	(%r12,%r13), %rsi
.LEHB3:
	call	_ZNSi10_M_extractIdEERSiRT_@PLT
	movq	16(%rsp), %rdi
	leaq	(%r12,%r14), %rsi
	call	_ZNSi10_M_extractIdEERSiRT_@PLT
	addq	$8, %r12
	cmpq	$1024, %r12
	jne	.L40
	movq	%r13, %rdi
	subl	$-128, %r15d
	addq	$1024, %r13
	call	_Z9normalizePd
	movq	%r14, %rdi
	addq	$1024, %r14
	call	_Z9normalizePd
	cmpl	$51200, %r15d
	jne	.L39
	movq	40(%rsp), %rsi
	movl	$400, %ecx
	movq	%rbx, %rdx
	movq	%rbp, %rdi
	call	_Z11MulWithCudaPdS_S_i
	testl	%eax, %eax
	je	.L42
	leaq	.LC17(%rip), %rdx
.L66:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %ebx
	jmp	.L43
.L42:
	leaq	.LC18(%rip), %r15
	movsd	(%rbx), %xmm0
	movl	$2, %edi
	movb	$1, %al
	movq	%r15, %rsi
	call	__printf_chk@PLT
	movq	$0x000000000, 32(%rsp)
	xorl	%r13d, %r13d
.L44:
	imulq	$3200, %r13, %r14
	xorl	%r12d, %r12d
	addq	%rbx, %r14
.L46:
	movq	24(%rsp), %rdi
	leaq	48(%rsp), %rsi
	call	_ZNSi10_M_extractIdEERSiRT_@PLT
	movsd	48(%rsp), %xmm0
	movaps	%xmm0, %xmm1
	mulsd	%xmm0, %xmm1
	movsd	(%r14,%r12,8), %xmm0
	subsd	%xmm1, %xmm0
	movaps	%xmm0, %xmm1
	andps	.LC19(%rip), %xmm1
	comisd	32(%rsp), %xmm1
	jbe	.L45
	movsd	%xmm0, 32(%rsp)
.L45:
	incq	%r12
	cmpq	$400, %r12
	jne	.L46
	incq	%r13
	cmpq	$400, %r13
	jne	.L44
	movsd	32(%rsp), %xmm0
	movq	%r15, %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	call	cudaDeviceReset@PLT
	leaq	.LC20(%rip), %rdx
	testl	%eax, %eax
	jne	.L66
	movq	%rbp, %rdi
	call	free@PLT
	movq	40(%rsp), %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	8(%rsp), %rdi
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEE5closeEv@PLT
	movq	16(%rsp), %rdi
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEE5closeEv@PLT
	movq	24(%rsp), %rdi
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEE5closeEv@PLT
.LEHE3:
	xorl	%ebx, %ebx
.L43:
	movq	24(%rsp), %rdi
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	16(%rsp), %rdi
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	8(%rsp), %rdi
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	1640(%rsp), %rax
	subq	%fs:40, %rax
	je	.L54
	jmp	.L63
.L59:
	endbr64
	movq	%rax, %rbx
.L50:
	movq	24(%rsp), %rdi
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev@PLT
	jmp	.L51
.L58:
	endbr64
	movq	%rax, %rbx
.L51:
	movq	16(%rsp), %rdi
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev@PLT
	jmp	.L52
.L57:
	endbr64
	movq	%rax, %rbx
.L52:
	movq	8(%rsp), %rdi
	call	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	1640(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L63
	movq	%rbx, %rdi
.LEHB4:
	call	_Unwind_Resume@PLT
.LEHE4:
.L63:
	call	__stack_chk_fail@PLT
.L54:
	addq	$1656, %rsp
	.cfi_def_cfa_offset 56
	movl	%ebx, %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
.LFE3768:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA3768:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE3768-.LLSDACSB3768
.LLSDACSB3768:
	.uleb128 .LEHB0-.LFB3768
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB3768
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L57-.LFB3768
	.uleb128 0
	.uleb128 .LEHB2-.LFB3768
	.uleb128 .LEHE2-.LEHB2
	.uleb128 .L58-.LFB3768
	.uleb128 0
	.uleb128 .LEHB3-.LFB3768
	.uleb128 .LEHE3-.LEHB3
	.uleb128 .L59-.LFB3768
	.uleb128 0
	.uleb128 .LEHB4-.LFB3768
	.uleb128 .LEHE4-.LEHB4
	.uleb128 0
	.uleb128 0
.LLSDACSE3768:
	.text
	.size	mulMatrix, .-mulMatrix
	.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
.LC1:
	.long	0
	.long	1065353216
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC19:
	.long	-1
	.long	2147483647
	.long	0
	.long	0
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include "hip/hip_runtime.h"

#include <stdio.h>
#include <fstream>
#include <cstdlib>
#include <math.h>

#define BLOCK_SIZE 16
#define FEATURE_LEN 128
using namespace std;

void calMeanVar(double* v, double& mean, double& var){
    double sum = 0;
    for(int i=0;i<FEATURE_LEN;i++)
        sum+=v[i];
    mean =  sum / FEATURE_LEN;
    double sq_sum = 0;
    for(int i=0;i<FEATURE_LEN;i++)
        sq_sum+=v[i]*v[i];
    var = sqrt(sq_sum / FEATURE_LEN - mean * mean);
    //printf("%f,%f\n",mean,var);
    return;
}

void normalize(double* v){
    double mean = 0;
    double var = 0;
    calMeanVar(v, mean, var);
    for(int i = 0; i < FEATURE_LEN; i++){
        v[i] = (v[i] - mean) / var;
    }
    return;
}

hipError_t MulWithCuda(double* A, double* B, double* C, int matrixSize);
__global__ void matrixMulCUDA(double *A, double *B, double *C, int blockRow, int blockCol,int featureNum)
{
    double ans=0;
    //Need to copy A[bx*BLOCK_SIZE:(bx+1)*BLOCK_SIZE][:]
    //Need to copy B[by*BLOCK_SIZE:(by+1)*BLOCK_SIZE][:]

    __shared__ double AC[BLOCK_SIZE][FEATURE_LEN];//BLOCK_SIZE*blockCol
    __shared__ double BC[BLOCK_SIZE][FEATURE_LEN];

    //Each Thread response for #blockCol copy

    for(int k=0;k<blockCol;k++)
    {
        if((blockIdx.x*BLOCK_SIZE+threadIdx.x)<featureNum&&(k*BLOCK_SIZE+threadIdx.y)<FEATURE_LEN)
            AC[threadIdx.x][k*BLOCK_SIZE+threadIdx.y]=A[(blockIdx.x*BLOCK_SIZE+threadIdx.x)*FEATURE_LEN+(k*BLOCK_SIZE+threadIdx.y)];
        //else
            //printf("blockIdx.x blockIdx.y threadIdx.x threadIdx.y %d %d %d %d %d %d\n",blockIdx.x,blockIdx.y,threadIdx.x,threadIdx.y,blockIdx.x*BLOCK_SIZE+threadIdx.x,k*BLOCK_SIZE+threadIdx.y);
        if((blockIdx.y*BLOCK_SIZE+threadIdx.x)<featureNum&&(k*BLOCK_SIZE+threadIdx.y)<FEATURE_LEN)
            BC[threadIdx.x][k*BLOCK_SIZE+threadIdx.y]=B[(blockIdx.y*BLOCK_SIZE+threadIdx.x)*FEATURE_LEN+(k*BLOCK_SIZE+threadIdx.y)];
        //else
            //printf("blockIdx.x blockIdx.y threadIdx.x threadIdx.y %d %d %d %d %d %d\n",blockIdx.x,blockIdx.y,threadIdx.x,threadIdx.y,blockIdx.x*BLOCK_SIZE+threadIdx.x,k*BLOCK_SIZE+threadIdx.y);
    }
    __syncthreads();

    /*
    for(int i=0;i<16;i++)
    {
        for(int j=0;j<128;j++)
            printf("%f ",BC[i][j]);
        printf("\n");
    }
*/
    //if(blockIdx.x==8&&blockIdx.y==8)
    //    printf("blockIdx.x blockIdx.y threadIdx.x threadIdx.y %d %d %d %d\n",blockIdx.x,blockIdx.y,threadIdx.x,threadIdx.y);
    //if(blockIdx.x==8&&blockIdx.y==8&&threadIdx.x==0&&threadIdx.y==0)
    //        printf("%f",ans);
    for(int k=0;k<FEATURE_LEN;k++)
    {
        ans+=(AC[threadIdx.x][k]-BC[threadIdx.y][k])*(AC[threadIdx.x][k]-BC[threadIdx.y][k]);
    }

    //if(threadIdx.x==0&&threadIdx.y==0&&blockIdx.x==0&&blockIdx.y==0)
    //    printf("%f",ans);


    if((blockIdx.x*BLOCK_SIZE+threadIdx.x)<featureNum&&(blockIdx.y*BLOCK_SIZE+threadIdx.y)<featureNum)
        C[(blockIdx.x*BLOCK_SIZE+threadIdx.x)*featureNum+(blockIdx.y*BLOCK_SIZE+threadIdx.y)]=ans;
}

extern "C" int mulMatrix()
{
    int featureNum = 400;
    fstream fa("img1_400.txt",fstream::in);
    fstream fb("img2_400.txt",fstream::in);
    fstream fc("400_distance.txt",fstream::in);


    // if (argc == 1){
    //     printf("Please input your feature number!");
    //     return 1;
    // }else if (argc == 2){
    //     featureNum = atoi(argv[1]);
    //     printf("Input size: %d\n", featureNum);
    // }else{
    //     printf("Too many arguments!");
    //     return 1;
    // }

    double* A = (double*)malloc(featureNum * FEATURE_LEN * sizeof(double)); // We represent a 2D matrix in the form of 1D array.
    double* B = (double*)malloc(featureNum * FEATURE_LEN * sizeof(double)); // We represent a 2D matrix in the form of 1D array.
    double* C = (double*)malloc(featureNum * featureNum * sizeof(double)); // We represent a 2D matrix in the form of 1D array.

    // Initiate matrix A.
    for(int i = 0; i < featureNum; i++){
        for(int j = 0; j < FEATURE_LEN; j++){
            fa>>A[i * FEATURE_LEN + j];
            fb>>B[i * FEATURE_LEN + j];
        }
        normalize(A+i*FEATURE_LEN);
        normalize(B+i*FEATURE_LEN);
    }

    // Parallel.
    hipError_t cudaStatus = MulWithCuda(A,B,C,featureNum);
    if (cudaStatus != hipSuccess) {fprintf(stderr, "mulWithCuda failed!");return 1;}



    double err_max=0;
    printf("%f\n",C[0]);
    for(int i = 0; i < featureNum; i++){
        for(int j = 0; j < featureNum; j++){
            double tmp;
            fc>>tmp;
            err_max=err_max<abs(C[i*featureNum+j]-tmp*tmp)?C[i*featureNum+j]-tmp*tmp:err_max;

        }
    }
    printf("%f\n",err_max);

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = hipDeviceReset();
    if (cudaStatus != hipSuccess) {fprintf(stderr, "hipDeviceReset failed!");return 1;}

    free(A);free(B);free(C);
    fa.close();fb.close();fc.close();
    return 0;
}

// Helper function for using CUDA to add vectors in parallel.
hipError_t MulWithCuda(double* A, double* B, double* C, int featureNum)
{
    double *dev_A;
    double *dev_B;
    double *dev_C;
    hipError_t cudaStatus;
	hipEvent_t start, stop;
    float gpu_time = 0.0f;

    // Launch a kernel on the GPU with one thread for each element.
    int blockRowNum=featureNum%BLOCK_SIZE?featureNum/BLOCK_SIZE+1:featureNum/BLOCK_SIZE;
    int blockColNum=FEATURE_LEN%BLOCK_SIZE?FEATURE_LEN/BLOCK_SIZE+1:FEATURE_LEN/BLOCK_SIZE;
    dim3 gridDime(blockRowNum,blockRowNum);
    dim3 blockDime(BLOCK_SIZE,BLOCK_SIZE);

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = hipSetDevice(0);
    if (cudaStatus != hipSuccess) {
        fprintf(stderr, "hipSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        goto Error;
    }

    // Allocate GPU buffers for two matrix
    cudaStatus = hipMalloc((void**)&dev_A, featureNum * FEATURE_LEN * sizeof(double));
    if (cudaStatus != hipSuccess) {fprintf(stderr, "hipMalloc A failed!");goto Error;}

    cudaStatus = hipMalloc((void**)&dev_B, featureNum * FEATURE_LEN * sizeof(double));
    if (cudaStatus != hipSuccess) {fprintf(stderr, "hipMalloc B failed!");goto Error;}

    cudaStatus = hipMalloc((void**)&dev_C, featureNum * featureNum * sizeof(double));
    if (cudaStatus != hipSuccess) {fprintf(stderr, "hipMalloc C failed!");goto Error;}

    // Copy input vectors from host memory to GPU buffers.
    cudaStatus = hipMemcpy(dev_A, A, featureNum * FEATURE_LEN * sizeof(double), hipMemcpyHostToDevice);
    if (cudaStatus != hipSuccess) {fprintf(stderr, "hipMemcpy A failed!");goto Error;}

    cudaStatus = hipMemcpy(dev_B, B, featureNum * FEATURE_LEN * sizeof(double), hipMemcpyHostToDevice);
    if (cudaStatus != hipSuccess) {fprintf(stderr, "hipMemcpy B failed!");goto Error;}

	// Set up timing
	hipEventCreate(&start);
	hipEventCreate(&stop);
	hipEventRecord(start, 0);
	printf("Row,Col %d %d\n",blockRowNum,blockColNum);

    matrixMulCUDA<<<gridDime,blockDime>>>(dev_A,dev_B,dev_C,blockRowNum,blockColNum,featureNum);

    // copy result back
    cudaStatus = hipMemcpy(C, dev_C, featureNum * featureNum * sizeof(double), hipMemcpyDeviceToHost);

    if (cudaStatus != hipSuccess) {fprintf(stderr, "hipMemcpy C back failed!");goto Error;}



    cudaStatus = hipDeviceSynchronize();
    if (cudaStatus != hipSuccess) {fprintf(stderr, "hipDeviceSynchronize returned error code %d after launching Kernel!\n", cudaStatus);goto Error;}

	// Close timing
	hipEventRecord(stop, 0);
	hipEventSynchronize(stop);
	hipEventElapsedTime(&gpu_time, start, stop);
	printf("Time spent: %.5f\n", gpu_time);
	hipEventDestroy(start);
    hipEventDestroy(stop);

Error:
    hipFree(dev_A);hipFree(dev_B);hipFree(dev_C);
    return cudaStatus;
}

	.text
	.file	"testopencuda.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z10calMeanVarPdRdS0_
.LCPI0_0:
	.quad	0x3f80000000000000              # double 0.0078125
	.text
	.globl	_Z10calMeanVarPdRdS0_
	.type	_Z10calMeanVarPdRdS0_,@function
_Z10calMeanVarPdRdS0_:                  # @_Z10calMeanVarPdRdS0_
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdx, %rbx
	xorpd	%xmm1, %xmm1
	xorl	%eax, %eax
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	addsd	(%rdi,%rax,8), %xmm1
	incq	%rax
	cmpq	$128, %rax
	jne	.LBB0_1
# %bb.2:
	mulsd	.LCPI0_0(%rip), %xmm1
	movsd	%xmm1, (%rsi)
	xorpd	%xmm0, %xmm0
	xorl	%eax, %eax
.LBB0_3:                                # =>This Inner Loop Header: Depth=1
	movsd	(%rdi,%rax,8), %xmm2            # xmm2 = mem[0],zero
	mulsd	%xmm2, %xmm2
	addsd	%xmm2, %xmm0
	incq	%rax
	cmpq	$128, %rax
	jne	.LBB0_3
# %bb.4:
	mulsd	.LCPI0_0(%rip), %xmm0
	mulsd	%xmm1, %xmm1
	subsd	%xmm1, %xmm0
	xorpd	%xmm1, %xmm1
	ucomisd	%xmm1, %xmm0
	jb	.LBB0_6
# %bb.5:
	sqrtsd	%xmm0, %xmm0
	jmp	.LBB0_7
.LBB0_6:                                # %call.sqrt
	callq	sqrt
.LBB0_7:                                # %.split
	movsd	%xmm0, (%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z10calMeanVarPdRdS0_, .Lfunc_end0-_Z10calMeanVarPdRdS0_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z9normalizePd
.LCPI1_0:
	.quad	0x3f80000000000000              # double 0.0078125
	.text
	.globl	_Z9normalizePd
	.type	_Z9normalizePd,@function
_Z9normalizePd:                         # @_Z9normalizePd
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	xorpd	%xmm2, %xmm2
	xorl	%eax, %eax
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	addsd	(%rbx,%rax,8), %xmm2
	incq	%rax
	cmpq	$128, %rax
	jne	.LBB1_1
# %bb.2:                                # %.preheader.preheader
	xorpd	%xmm0, %xmm0
	xorl	%eax, %eax
.LBB1_3:                                # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	movsd	(%rbx,%rax,8), %xmm1            # xmm1 = mem[0],zero
	mulsd	%xmm1, %xmm1
	addsd	%xmm1, %xmm0
	incq	%rax
	cmpq	$128, %rax
	jne	.LBB1_3
# %bb.4:                                # %_Z10calMeanVarPdRdS0_.exit
	movsd	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero
	mulsd	%xmm1, %xmm2
	mulsd	%xmm1, %xmm0
	movapd	%xmm2, %xmm1
	mulsd	%xmm2, %xmm1
	subsd	%xmm1, %xmm0
	xorpd	%xmm1, %xmm1
	ucomisd	%xmm1, %xmm0
	jb	.LBB1_6
# %bb.5:
	sqrtsd	%xmm0, %xmm0
	jmp	.LBB1_7
.LBB1_6:                                # %call.sqrt
	movsd	%xmm2, 8(%rsp)                  # 8-byte Spill
	callq	sqrt
	movsd	8(%rsp), %xmm2                  # 8-byte Reload
                                        # xmm2 = mem[0],zero
.LBB1_7:                                # %_Z10calMeanVarPdRdS0_.exit.split
	xorl	%eax, %eax
.LBB1_8:                                # =>This Inner Loop Header: Depth=1
	movsd	(%rbx,%rax,8), %xmm1            # xmm1 = mem[0],zero
	subsd	%xmm2, %xmm1
	divsd	%xmm0, %xmm1
	movsd	%xmm1, (%rbx,%rax,8)
	incq	%rax
	cmpq	$128, %rax
	jne	.LBB1_8
# %bb.9:
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z9normalizePd, .Lfunc_end1-_Z9normalizePd
	.cfi_endproc
                                        # -- End function
	.globl	_Z28__device_stub__matrixMulCUDAPdS_S_iii # -- Begin function _Z28__device_stub__matrixMulCUDAPdS_S_iii
	.type	_Z28__device_stub__matrixMulCUDAPdS_S_iii,@function
_Z28__device_stub__matrixMulCUDAPdS_S_iii: # @_Z28__device_stub__matrixMulCUDAPdS_S_iii
	.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	$144, %rsp
	.cfi_def_cfa_offset 192
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	20(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	16(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	12(%rsp), %r8
	movl	%r9d, (%r8)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	32(%rsp), %r12
	leaq	24(%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	$_Z13matrixMulCUDAPdS_S_iii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$160, %rsp
	.cfi_adjust_cfa_offset -160
	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_end2:
	.size	_Z28__device_stub__matrixMulCUDAPdS_S_iii, .Lfunc_end2-_Z28__device_stub__matrixMulCUDAPdS_S_iii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function mulMatrix
.LCPI3_0:
	.quad	0x7fffffffffffffff              # double NaN
	.quad	0x7fffffffffffffff              # double NaN
	.text
	.globl	mulMatrix
	.type	mulMatrix,@function
mulMatrix:                              # @mulMatrix
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %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	$1640, %rsp                     # imm = 0x668
	.cfi_def_cfa_offset 1696
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	leaq	1112(%rsp), %rdi
	movl	$.L.str, %esi
	movl	$8, %edx
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode
.Ltmp0:
	leaq	584(%rsp), %rdi
	movl	$.L.str.1, %esi
	movl	$8, %edx
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode
.Ltmp1:
# %bb.1:
.Ltmp3:
	leaq	56(%rsp), %rdi
	movl	$.L.str.2, %esi
	movl	$8, %edx
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode
.Ltmp4:
# %bb.2:
	movl	$409600, %edi                   # imm = 0x64000
	callq	malloc
	movq	%rax, %rbx
	movl	$409600, %edi                   # imm = 0x64000
	callq	malloc
	movq	%rax, %r14
	movl	$1280000, %edi                  # imm = 0x138800
	callq	malloc
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	xorl	%ebp, %ebp
	leaq	1112(%rsp), %r12
	leaq	584(%rsp), %r13
	movq	%rbx, 8(%rsp)                   # 8-byte Spill
	movq	%r14, (%rsp)                    # 8-byte Spill
.LBB3_3:                                # %.preheader70
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_4 Depth 2
	movq	%rbp, %rax
	shlq	$7, %rax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	xorl	%r15d, %r15d
.LBB3_4:                                #   Parent Loop BB3_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leaq	(%rbx,%r15), %rsi
.Ltmp6:
	movq	%r12, %rdi
	callq	_ZNSi10_M_extractIdEERSiRT_
.Ltmp7:
# %bb.5:                                # %_ZNSirsERd.exit
                                        #   in Loop: Header=BB3_4 Depth=2
	leaq	(%r14,%r15), %rsi
.Ltmp8:
	movq	%r13, %rdi
	callq	_ZNSi10_M_extractIdEERSiRT_
.Ltmp9:
# %bb.6:                                # %_ZNSirsERd.exit68
                                        #   in Loop: Header=BB3_4 Depth=2
	addq	$8, %r15
	cmpq	$1024, %r15                     # imm = 0x400
	jne	.LBB3_4
# %bb.7:                                #   in Loop: Header=BB3_3 Depth=1
	movq	8(%rsp), %rax                   # 8-byte Reload
	movq	16(%rsp), %r15                  # 8-byte Reload
	leaq	(%rax,%r15,8), %rdi
	callq	_Z9normalizePd
	movq	(%rsp), %rax                    # 8-byte Reload
	leaq	(%rax,%r15,8), %rdi
	callq	_Z9normalizePd
	incq	%rbp
	movl	$1024, %eax                     # imm = 0x400
	addq	%rax, %r14
	addq	%rax, %rbx
	cmpq	$400, %rbp                      # imm = 0x190
	jne	.LBB3_3
# %bb.8:
.Ltmp11:
	movq	8(%rsp), %rdi                   # 8-byte Reload
	movq	(%rsp), %rsi                    # 8-byte Reload
	movq	40(%rsp), %r15                  # 8-byte Reload
	movq	%r15, %rdx
	movl	$400, %ecx                      # imm = 0x190
	callq	_Z11MulWithCudaPdS_S_i
.Ltmp12:
# %bb.9:
	testl	%eax, %eax
	jne	.LBB3_10
# %bb.15:
	movsd	(%r15), %xmm0                   # xmm0 = mem[0],zero
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	xorpd	%xmm0, %xmm0
	movapd	%xmm0, 16(%rsp)                 # 16-byte Spill
	xorl	%ebp, %ebp
	leaq	56(%rsp), %r12
	leaq	48(%rsp), %r13
	movq	%r15, %rbx
.LBB3_16:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_17 Depth 2
	xorl	%r14d, %r14d
.LBB3_17:                               #   Parent Loop BB3_16 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
.Ltmp14:
	movq	%r12, %rdi
	movq	%r13, %rsi
	callq	_ZNSi10_M_extractIdEERSiRT_
.Ltmp15:
# %bb.18:                               # %_ZNSirsERd.exit69
                                        #   in Loop: Header=BB3_17 Depth=2
	movsd	(%rbx,%r14,8), %xmm0            # xmm0 = mem[0],zero
	movsd	48(%rsp), %xmm1                 # xmm1 = mem[0],zero
	mulsd	%xmm1, %xmm1
	subsd	%xmm1, %xmm0
	movapd	%xmm0, %xmm1
	andpd	.LCPI3_0(%rip), %xmm1
	movapd	16(%rsp), %xmm3                 # 16-byte Reload
	movapd	%xmm3, %xmm2
	cmpltsd	%xmm1, %xmm2
	andpd	%xmm2, %xmm0
	andnpd	%xmm3, %xmm2
	orpd	%xmm0, %xmm2
	movapd	%xmm2, 16(%rsp)                 # 16-byte Spill
	incq	%r14
	cmpq	$400, %r14                      # imm = 0x190
	jne	.LBB3_17
# %bb.19:                               #   in Loop: Header=BB3_16 Depth=1
	incq	%rbp
	addq	$3200, %rbx                     # imm = 0xC80
	cmpq	$400, %rbp                      # imm = 0x190
	jne	.LBB3_16
# %bb.20:
	movl	$.L.str.4, %edi
	movapd	16(%rsp), %xmm0                 # 16-byte Reload
	movb	$1, %al
	callq	printf
.Ltmp17:
	callq	hipDeviceReset
.Ltmp18:
# %bb.21:
	testl	%eax, %eax
	jne	.LBB3_22
# %bb.23:
	movq	8(%rsp), %rdi                   # 8-byte Reload
	callq	free
	movq	(%rsp), %rdi                    # 8-byte Reload
	callq	free
	movq	%r15, %rdi
	callq	free
.Ltmp19:
	leaq	1112(%rsp), %rdi
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEE5closeEv
.Ltmp20:
# %bb.24:
.Ltmp21:
	leaq	584(%rsp), %rdi
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEE5closeEv
.Ltmp22:
# %bb.25:
	xorl	%ebx, %ebx
.Ltmp23:
	leaq	56(%rsp), %rdi
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEE5closeEv
.Ltmp24:
.LBB3_26:
	leaq	56(%rsp), %rdi
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev
	leaq	584(%rsp), %rdi
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev
	leaq	1112(%rsp), %rdi
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev
	movl	%ebx, %eax
	addq	$1640, %rsp                     # imm = 0x668
	.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
.LBB3_10:
	.cfi_def_cfa_offset 1696
	movq	stderr(%rip), %rcx
	movl	$.L.str.3, %edi
	movl	$19, %esi
	jmp	.LBB3_11
.LBB3_22:
	movq	stderr(%rip), %rcx
	movl	$.L.str.5, %edi
	movl	$22, %esi
.LBB3_11:
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$1, %ebx
	jmp	.LBB3_26
.LBB3_14:
.Ltmp13:
	jmp	.LBB3_28
.LBB3_13:
.Ltmp5:
	movq	%rax, %rbx
	jmp	.LBB3_29
.LBB3_12:
.Ltmp2:
	movq	%rax, %rbx
	jmp	.LBB3_30
.LBB3_32:
.Ltmp25:
	jmp	.LBB3_28
.LBB3_31:
.Ltmp16:
	jmp	.LBB3_28
.LBB3_27:
.Ltmp10:
.LBB3_28:
	movq	%rax, %rbx
	leaq	56(%rsp), %rdi
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev
.LBB3_29:
	leaq	584(%rsp), %rdi
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev
.LBB3_30:
	leaq	1112(%rsp), %rdi
	callq	_ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev
	movq	%rbx, %rdi
	callq	_Unwind_Resume@PLT
.Lfunc_end3:
	.size	mulMatrix, .Lfunc_end3-mulMatrix
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table3:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	255                             # @TType Encoding = omit
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Lfunc_begin0-.Lfunc_begin0    # >> Call Site 1 <<
	.uleb128 .Ltmp0-.Lfunc_begin0           #   Call between .Lfunc_begin0 and .Ltmp0
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 2 <<
	.uleb128 .Ltmp1-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp1
	.uleb128 .Ltmp2-.Lfunc_begin0           #     jumps to .Ltmp2
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp3-.Lfunc_begin0           # >> Call Site 3 <<
	.uleb128 .Ltmp4-.Ltmp3                  #   Call between .Ltmp3 and .Ltmp4
	.uleb128 .Ltmp5-.Lfunc_begin0           #     jumps to .Ltmp5
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp6-.Lfunc_begin0           # >> Call Site 4 <<
	.uleb128 .Ltmp9-.Ltmp6                  #   Call between .Ltmp6 and .Ltmp9
	.uleb128 .Ltmp10-.Lfunc_begin0          #     jumps to .Ltmp10
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp11-.Lfunc_begin0          # >> Call Site 5 <<
	.uleb128 .Ltmp12-.Ltmp11                #   Call between .Ltmp11 and .Ltmp12
	.uleb128 .Ltmp13-.Lfunc_begin0          #     jumps to .Ltmp13
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp14-.Lfunc_begin0          # >> Call Site 6 <<
	.uleb128 .Ltmp15-.Ltmp14                #   Call between .Ltmp14 and .Ltmp15
	.uleb128 .Ltmp16-.Lfunc_begin0          #     jumps to .Ltmp16
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp17-.Lfunc_begin0          # >> Call Site 7 <<
	.uleb128 .Ltmp24-.Ltmp17                #   Call between .Ltmp17 and .Ltmp24
	.uleb128 .Ltmp25-.Lfunc_begin0          #     jumps to .Ltmp25
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp24-.Lfunc_begin0          # >> Call Site 8 <<
	.uleb128 .Lfunc_end3-.Ltmp24            #   Call between .Ltmp24 and .Lfunc_end3
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
.Lcst_end0:
	.p2align	2, 0x0
                                        # -- End function
	.text
	.globl	_Z11MulWithCudaPdS_S_i          # -- Begin function _Z11MulWithCudaPdS_S_i
	.type	_Z11MulWithCudaPdS_S_i,@function
_Z11MulWithCudaPdS_S_i:                 # @_Z11MulWithCudaPdS_S_i
	.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	$72, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%ecx, %r15d
	movq	%rdx, %r12
	movq	%rsi, %r14
	movq	%rdi, %r13
	movl	$0, 44(%rsp)
	testb	$15, %r15b
	je	.LBB4_2
# %bb.1:
	leal	15(%r15), %ebp
	testl	%r15d, %r15d
	cmovnsl	%r15d, %ebp
	sarl	$4, %ebp
	incl	%ebp
	jmp	.LBB4_3
.LBB4_2:
	movl	%r15d, %ebp
	sarl	$4, %ebp
.LBB4_3:
	xorl	%edi, %edi
	callq	hipSetDevice
	testl	%eax, %eax
	jne	.LBB4_4
# %bb.5:
	movq	%r13, 64(%rsp)                  # 8-byte Spill
	movl	%r15d, %eax
	shll	$7, %eax
	movslq	%eax, %r13
	shlq	$3, %r13
	leaq	32(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB4_6
# %bb.9:
	leaq	24(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB4_10
# %bb.11:
	movq	%r12, 56(%rsp)                  # 8-byte Spill
	movl	%r15d, %r12d
	imull	%r12d, %r12d
	shlq	$3, %r12
	leaq	16(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB4_12
# %bb.13:
	movq	32(%rsp), %rdi
	movq	64(%rsp), %rsi                  # 8-byte Reload
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB4_14
# %bb.15:
	movq	24(%rsp), %rdi
	movq	%r14, %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB4_16
# %bb.17:
	movl	%ebp, %eax
	movq	%rax, %rbx
	shlq	$32, %rbx
	orq	%rax, %rbx
	leaq	48(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r14), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	$.L.str.12, %edi
	movl	%ebp, %esi
	movl	$8, %edx
	xorl	%eax, %eax
	callq	printf
	movabsq	$68719476752, %rdx              # imm = 0x1000000010
	movq	%rbx, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_19
# %bb.18:
	movq	32(%rsp), %rdi
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdx
	movl	%ebp, %ecx
	movl	$8, %r8d
	movl	%r15d, %r9d
	callq	_Z28__device_stub__matrixMulCUDAPdS_S_iii
.LBB4_19:
	movq	16(%rsp), %rsi
	movq	56(%rsp), %rdi                  # 8-byte Reload
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB4_20
# %bb.21:
	callq	hipDeviceSynchronize
	testl	%eax, %eax
	jne	.LBB4_22
# %bb.23:
	movq	8(%rsp), %rdi
	xorl	%ebx, %ebx
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	48(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	44(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventElapsedTime
	cvtss2sd	(%r14), %xmm0
	movl	$.L.str.15, %edi
	movb	$1, %al
	callq	printf
	movq	48(%rsp), %rdi
	callq	hipEventDestroy
	movq	8(%rsp), %rdi
	callq	hipEventDestroy
	jmp	.LBB4_24
.LBB4_4:
	movl	%eax, %ebx
	movq	stderr(%rip), %rcx
	movl	$.L.str.6, %edi
	movl	$63, %esi
	jmp	.LBB4_8
.LBB4_6:
	movl	%eax, %ebx
	movq	stderr(%rip), %rcx
	movl	$.L.str.7, %edi
	jmp	.LBB4_7
.LBB4_10:
	movl	%eax, %ebx
	movq	stderr(%rip), %rcx
	movl	$.L.str.8, %edi
	jmp	.LBB4_7
.LBB4_12:
	movl	%eax, %ebx
	movq	stderr(%rip), %rcx
	movl	$.L.str.9, %edi
	jmp	.LBB4_7
.LBB4_14:
	movl	%eax, %ebx
	movq	stderr(%rip), %rcx
	movl	$.L.str.10, %edi
	jmp	.LBB4_7
.LBB4_16:
	movl	%eax, %ebx
	movq	stderr(%rip), %rcx
	movl	$.L.str.11, %edi
.LBB4_7:
	movl	$19, %esi
.LBB4_8:
	movl	$1, %edx
	callq	fwrite@PLT
.LBB4_24:
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movl	%ebx, %eax
	addq	$72, %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_20:
	.cfi_def_cfa_offset 128
	movl	%eax, %ebx
	movq	stderr(%rip), %rcx
	movl	$.L.str.13, %edi
	movl	$24, %esi
	jmp	.LBB4_8
.LBB4_22:
	movl	%eax, %ebx
	movq	stderr(%rip), %rdi
	movl	$.L.str.14, %esi
	movl	%eax, %edx
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB4_24
.Lfunc_end4:
	.size	_Z11MulWithCudaPdS_S_i, .Lfunc_end4-_Z11MulWithCudaPdS_S_i
	.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	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z13matrixMulCUDAPdS_S_iii, %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_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__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	.LBB6_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
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z13matrixMulCUDAPdS_S_iii,@object # @_Z13matrixMulCUDAPdS_S_iii
	.section	.rodata,"a",@progbits
	.globl	_Z13matrixMulCUDAPdS_S_iii
	.p2align	3, 0x0
_Z13matrixMulCUDAPdS_S_iii:
	.quad	_Z28__device_stub__matrixMulCUDAPdS_S_iii
	.size	_Z13matrixMulCUDAPdS_S_iii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"img1_400.txt"
	.size	.L.str, 13

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"img2_400.txt"
	.size	.L.str.1, 13

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"400_distance.txt"
	.size	.L.str.2, 17

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"mulWithCuda failed!"
	.size	.L.str.3, 20

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"hipDeviceReset failed!"
	.size	.L.str.5, 23

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"hipSetDevice failed!  Do you have a CUDA-capable GPU installed?"
	.size	.L.str.6, 64

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"hipMalloc A failed!"
	.size	.L.str.7, 20

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"hipMalloc B failed!"
	.size	.L.str.8, 20

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"hipMalloc C failed!"
	.size	.L.str.9, 20

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"hipMemcpy A failed!"
	.size	.L.str.10, 20

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"hipMemcpy B failed!"
	.size	.L.str.11, 20

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Row,Col %d %d\n"
	.size	.L.str.12, 15

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"hipMemcpy C back failed!"
	.size	.L.str.13, 25

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"hipDeviceSynchronize returned error code %d after launching Kernel!\n"
	.size	.L.str.14, 69

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"Time spent: %.5f\n"
	.size	.L.str.15, 18

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z13matrixMulCUDAPdS_S_iii"
	.size	.L__unnamed_1, 27

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

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

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

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z13matrixMulCUDAPdS_S_iii ; -- Begin function _Z13matrixMulCUDAPdS_S_iii
	.globl	_Z13matrixMulCUDAPdS_S_iii
	.p2align	8
	.type	_Z13matrixMulCUDAPdS_S_iii,@function
_Z13matrixMulCUDAPdS_S_iii:             ; @_Z13matrixMulCUDAPdS_S_iii
; %bb.0:
	s_clause 0x2
	s_load_b64 s[8:9], s[0:1], 0x1c
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[2:3], s[0:1], 0x10
	v_and_b32_e32 v2, 0x3ff, v0
	v_bfe_u32 v3, v0, 10, 10
	s_mov_b32 s0, 0
	s_waitcnt lgkmcnt(0)
	s_cmp_gt_i32 s8, 0
	s_cbranch_scc1 .LBB0_2
; %bb.1:                                ; %.._crit_edge_crit_edge
	v_and_b32_e32 v4, 0x3ff, v0
	v_bfe_u32 v5, v0, 10, 10
	s_branch .LBB0_3
.LBB0_2:
	s_mov_b32 s0, -1
                                        ; implicit-def: $vgpr4
                                        ; implicit-def: $vgpr5
.LBB0_3:                                ; %Flow61
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 vcc_lo, exec_lo, s0
	s_cbranch_vccnz .LBB0_11
; %bb.4:                                ; %.lr.ph
	v_lshl_add_u32 v0, s14, 4, v2
	v_lshl_add_u32 v1, s15, 4, v2
	v_lshlrev_b32_e32 v5, 7, v2
	v_dual_mov_b32 v7, v3 :: v_dual_lshlrev_b32 v6, 3, v3
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cmp_gt_u32_e32 vcc_lo, s9, v0
	v_cmp_gt_u32_e64 s0, s9, v1
	v_mov_b32_e32 v1, 0
	v_lshl_add_u32 v4, s15, 11, v5
	v_lshl_add_u32 v5, s14, 11, v5
	v_lshl_add_u32 v6, v2, 10, v6
.LBB0_5:                                ; =>This Inner Loop Header: Depth=1
	v_cmp_gt_u32_e64 s1, 0x80, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s1, vcc_lo, s1
	s_and_saveexec_b32 s10, s1
	s_cbranch_execz .LBB0_7
; %bb.6:                                ;   in Loop: Header=BB0_5 Depth=1
	v_add_nc_u32_e32 v0, v5, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[8:9], 3, v[0:1]
	v_add_co_u32 v8, s1, s4, v8
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v9, s1, s5, v9, s1
	global_load_b64 v[8:9], v[8:9], off
	s_waitcnt vmcnt(0)
	ds_store_b64 v6, v[8:9]
.LBB0_7:                                ;   in Loop: Header=BB0_5 Depth=1
	s_or_b32 exec_lo, exec_lo, s10
	v_cmp_gt_u32_e64 s1, 0x80, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s1, s0, s1
	s_and_saveexec_b32 s10, s1
	s_cbranch_execz .LBB0_9
; %bb.8:                                ;   in Loop: Header=BB0_5 Depth=1
	v_add_nc_u32_e32 v0, v4, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[8:9], 3, v[0:1]
	v_add_co_u32 v8, s1, s6, v8
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v9, s1, s7, v9, s1
	global_load_b64 v[8:9], v[8:9], off
	s_waitcnt vmcnt(0)
	ds_store_b64 v6, v[8:9] offset:16384
.LBB0_9:                                ;   in Loop: Header=BB0_5 Depth=1
	s_or_b32 exec_lo, exec_lo, s10
	v_add_nc_u32_e32 v7, 16, v7
	v_add_nc_u32_e32 v6, 0x80, v6
	s_add_i32 s8, s8, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s8, 0
	s_cbranch_scc0 .LBB0_5
; %bb.10:
	v_dual_mov_b32 v4, v2 :: v_dual_mov_b32 v5, v3
.LBB0_11:                               ; %Flow62
	v_mov_b32_e32 v0, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_dual_mov_b32 v1, 0 :: v_dual_lshlrev_b32 v2, 10, v4
	v_lshl_add_u32 v3, v5, 10, 0x4000
	s_mov_b32 s0, 0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
.LBB0_12:                               ; =>This Inner Loop Header: Depth=1
	v_add_nc_u32_e32 v6, s0, v2
	v_add_nc_u32_e32 v8, s0, v3
	s_add_i32 s0, s0, 8
	ds_load_b64 v[6:7], v6
	ds_load_b64 v[8:9], v8
	s_cmpk_eq_i32 s0, 0x400
	s_waitcnt lgkmcnt(0)
	v_add_f64 v[6:7], v[6:7], -v[8:9]
	s_delay_alu instid0(VALU_DEP_1)
	v_fma_f64 v[0:1], v[6:7], v[6:7], v[0:1]
	s_cbranch_scc0 .LBB0_12
; %bb.13:
	v_lshl_add_u32 v3, s14, 4, v4
	v_lshl_add_u32 v2, s15, 4, v5
	s_mov_b32 s0, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_max_u32_e32 v4, v3, v2
	v_cmpx_gt_u32_e64 s9, v4
	s_cbranch_execz .LBB0_15
; %bb.14:
	v_mad_u64_u32 v[4:5], null, v3, s9, v[2:3]
	v_mov_b32_e32 v5, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 3, v[4:5]
	v_add_co_u32 v2, vcc_lo, s2, v2
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s3, v3, vcc_lo
	global_store_b64 v[2:3], v[0:1], off
.LBB0_15:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z13matrixMulCUDAPdS_S_iii
		.amdhsa_group_segment_fixed_size 32768
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 36
		.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 10
		.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	_Z13matrixMulCUDAPdS_S_iii, .Lfunc_end0-_Z13matrixMulCUDAPdS_S_iii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 588
; NumSgprs: 18
; NumVgprs: 10
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 32768 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 10
; 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:         28
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 32768
    .kernarg_segment_align: 8
    .kernarg_segment_size: 36
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z13matrixMulCUDAPdS_S_iii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z13matrixMulCUDAPdS_S_iii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     10
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
