/*
 * Created by Harshavardhan Patil on 9/30/16.
 *
 *
 *  Matrix Normalization using CUDA :
 *  - The generated input values are stored by inverting the matrix. i.e All the attributes of a column which needs to be normalized
 *    are stored as elements of a row. So that while normalizing the threads in a block will access nearby elements which will
 *    optimize the code
 *  - The number of threads is hard coded to 16. This value is finalized after multiple iterations with values (32, 64, 128, 256, 512).
 *    This value is used along with the size of the matrix to calculate number of blocks.
 *  - Each block is assigned to calculate sum and squares of one row.
 *    In a block each thread will read one element and put it in a shared memory. Once this is done, partial sum is calculated and
 *    each block stores it's partial sum in the global memory area using it's block Id as index.
 *  - Once partial sum calculation is done by all the blocks, another kernal function is launched with this partial
 *    sum as the input in a single block. This block calculates the final sum and squares.
 *  - Partial sum using reduction works only if the number of elements passed to the block is a power of 2.
 *    So to avoid wrong calculation when number of blocks is not power of 2 (in calculateFinalSum method).
 *    The input array argument lentgh is set to the nearest power of 2 for the number of blocks and value 0 is set to those indices
 *    which are greater than number of blocks.
 *  - Then the population standard deviation is calculated for that row using formula (sumOfSquares + N * powf(mean, 2.0) - 2 * mean * sumOfTheElements)/N;
 *    Where,
 *        N - Size of the Matrix
 *  - The above values are used to calculate standard score of each element in that row.
 *  - The computed values are stored in the output matrix at their inverse position. This operation is done for all the elements
 *
 *  Steps to compile and execute
    -----------------------------
    1) Go to folder "/home/hpatil2/hw4"
    2) run : qlogin -q interactive.q
    3) run : cd /home/hpatil2/hw4
    4) run : nvcc MatrixNormalizationCuda.cu -o MatrixNormalizationCuda
    5) run : ./MatrixNormalizationCuda 15000 4

    In step 5 : [argument1 (15000) is MATRIX_SIZE, this is mandatory to pass. and maximum value it can take is 15000]
                [argument2 (4) is seed value, this is an optional field]
 *
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/time.h>
#include <time.h>
#include <cuda_runtime.h>

/* Program Parameters */
#define MAXN 15000  /* Max value of N */
int N;  /* Matrix size */

/* Matrices */
volatile float A[MAXN][MAXN], B[MAXN][MAXN];

/* junk */
#define randm() 4|2[uid]&3

/* Prototype */
void matrixNorm();

/* returns a seed for srand based on the time */
unsigned int time_seed() {
    struct timeval t;
    struct timezone tzdummy;

    gettimeofday(&t, &tzdummy);
    return (unsigned int)(t.tv_usec);
}

/* Set the program parameters from the command-line arguments */
void parameters(int argc, char **argv) {
    int seed = 0;  /* Random seed */
    char uid[32]; /*User name */

    /* Read command-line arguments */
    srand(time_seed());  /* Randomize */

    if (argc == 3) {
        seed = atoi(argv[2]);
        srand(seed);
        printf("Random seed = %i\n", seed);
    }
    if (argc >= 2) {
        N = atoi(argv[1]);
        if (N < 1 || N > MAXN) {
            printf("N = %i is out of range.\n", N);
            exit(0);
        }
    }
    else {
        printf("Usage: %s <matrix_dimension> [random seed]\n",
               argv[0]);
        exit(0);
    }

    /* Print parameters */
    printf("\nMatrix dimension N = %i.\n", N);
}

/* Initialize A and B*/
void initialize_inputs() {
    int row, col;

    printf("\nInitializing...\n");
    for (row = 0; row < N; row++) {
        for (col = 0; col < N; col++) {
            A[row][col] = (float)rand() / 32768.0;
            B[row][col] = 0.0;
        }
    }

}

/* Print input matrices */
void print_inputs() {
    int row, col;

    if (N < 10) {
        printf("\nA =\n\t");
        for (col = 0; col < N; col++) {
            for (row = 0; row < N; row++) {
                printf("%5.2f%s", A[row][col], (row < N-1) ? ", " : ";\n\t");
            }
        }
    }
}

void print_B() {
    int row, col;

    if (N < 10) {
        printf("\nB =\n\t");
        for (row = 0; row < N; row++) {
            for (col = 0; col < N; col++) {
                printf("%1.10f%s", B[row][col], (col < N-1) ? ", " : ";\n\t");
            }
        }
    }
}

/*
 *  This method calculates sum and square of given block
 */
__global__ void calculateBlockSum(const float *input, float *sumResults, float *squareResults, const size_t n)
{
    __shared__ float smSum[512];
    __shared__ float smSquare[512];
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    int tx = threadIdx.x;
    float x = 0;

    if(i < n) {
        x = input[i];
    }
    smSum[tx] = x;
    smSquare[tx] = x*x;
    __syncthreads();

    int j;
    for(j = blockDim.x / 2; j > 0; j >>= 1) {

        if(tx < j) {
            smSum[tx] = smSum[tx]+ smSum[tx + j];
            smSquare[tx] = smSquare[tx] + smSquare[tx + j];
        }
        __syncthreads();
    }

    if(threadIdx.x == 0) {

        sumResults[blockIdx.x] = smSum[0];
        squareResults[blockIdx.x] = smSquare[0];
    }
}

/*
 *  This method calculates final sum and square
 */
__global__ void calculateFinalSum(float *sumResults, float *squareResults, const size_t size, float *finalSumResult, float *finalSigmaResult)  {

    __shared__ float smSum[512];
    __shared__ float smSquare[512];
    int tx = threadIdx.x;

    if(tx < size) {
        smSum[tx] = sumResults[tx];
        smSquare[tx] = squareResults[tx];
    }
    __syncthreads();

    int i;
    for(i = size/2; i > 0; i >>= 1) {

        if(tx < i) {
            smSum[tx] = smSum[tx] + smSum[tx + i];
            smSquare[tx] = smSquare[tx] + smSquare[tx + i];
        }
        __syncthreads();
    }

    if(threadIdx.x == 0) {

        finalSumResult[0] = smSum[0];
        finalSigmaResult[0] = smSquare[0];
    }
}

float* calculateSum (float *input, size_t n, float *dMatrixA, float *sumValue, float *sigmaValue, size_t blockSize, size_t totalBlocks, int nextNearestPowerOf2, float *finalSumResult, float *finalSigmaResult) {

    float *results = (float *)malloc(sizeof(float) * 2);

    cudaMemcpy(dMatrixA, input, sizeof(float) * N, cudaMemcpyHostToDevice);
    calculateBlockSum<<<totalBlocks, blockSize>>> (dMatrixA, sumValue, sigmaValue, n);
    calculateFinalSum<<<1,totalBlocks>>>(sumValue, sigmaValue, nextNearestPowerOf2, finalSumResult, finalSigmaResult);

    cudaMemcpy(&results[0], &finalSumResult[0], sizeof(float), cudaMemcpyDeviceToHost);
    cudaMemcpy(&results[1], &finalSigmaResult[0], sizeof(float), cudaMemcpyDeviceToHost);

    return results;
}

void matrixNorm() {

    int row, column;
    float mu, sigma;
    float *sumValue = 0, *sigmaValue = 0, *dMatrixA = 0;
    float *finalSumResult = 0, *finalSigmaResult = 0;


    printf("Parallel Computing.\n");

    size_t blockSize = 16;
    size_t totalBlocks;

    if(N%blockSize == 0){
        totalBlocks = (N/blockSize);
    } else {
        totalBlocks = (N/blockSize) + 1;
    }

    int nextNearestPowerOf2 = pow(2, ceil(log(totalBlocks)/log(2)));

    cudaMalloc((void**)&sumValue, sizeof(float) * (nextNearestPowerOf2));
    cudaMemset(sumValue, 0.0, sizeof(float) * nextNearestPowerOf2);
    cudaMalloc((void**)&sigmaValue, sizeof(float) * (nextNearestPowerOf2));
    cudaMemset(sigmaValue, 0.0, sizeof(float) * nextNearestPowerOf2);
    cudaMalloc((void**)&dMatrixA, sizeof(float) * N);
    cudaMalloc((void**)&finalSumResult, sizeof(float));
    cudaMalloc((void**)&finalSigmaResult, sizeof(float));

    for (column=0; column < N; column++) {

        mu = 0.0;
        float *result;
        result = calculateSum ((float *)A[column], N, dMatrixA, sumValue, sigmaValue, blockSize, totalBlocks, nextNearestPowerOf2,finalSumResult,finalSigmaResult);
        mu = result[0] / (float) N;
        sigma = (result[1] + N * powf(mu, 2.0) - 2 * mu * result[0])/(float)N;

        for (row=0; row < N; row++) {
            if (sigma == 0.0) {
                B[row][column] = 0.0;
            } else {
                B[row][column] = (A[column][row] - mu) / sigma;
            }
        }
    }
    cudaFree(sumValue);
    cudaFree(sigmaValue);
    cudaFree(dMatrixA);
}


int main(int argc, char **argv) {

    struct timeval etstart, etstop;
    struct timezone tzdummy;
    clock_t etstart2, etstop2;
    unsigned long long usecstart, usecstop;
    struct tms cputstart, cputstop;

    parameters(argc, argv);
    initialize_inputs();
    print_inputs();

    printf("\nStarting clock.\n");
    gettimeofday(&etstart, &tzdummy);
    etstart2 = times(&cputstart);

    matrixNorm();

    gettimeofday(&etstop, &tzdummy);
    etstop2 = times(&cputstop);
    printf("Stopped clock.\n");
    usecstart = (unsigned long long)etstart.tv_sec * 1000000 + etstart.tv_usec;
    usecstop = (unsigned long long)etstop.tv_sec * 1000000 + etstop.tv_usec;

    print_B();

    printf("\nElapsed time = %g ms.\n",
           (float)(usecstop - usecstart)/(float)1000);

    printf("(CPU times are accurate to the nearest %g ms)\n",
           1.0/(float)CLOCKS_PER_SEC * 1000.0);
    printf("My total CPU time for parent = %g ms.\n",
           (float)( (cputstop.tms_utime + cputstop.tms_stime) -
                    (cputstart.tms_utime + cputstart.tms_stime) ) /
           (float)CLOCKS_PER_SEC * 1000);
    printf("My system CPU time for parent = %g ms.\n",
           (float)(cputstop.tms_stime - cputstart.tms_stime) /
           (float)CLOCKS_PER_SEC * 1000);
    printf("My total CPU time for child processes = %g ms.\n",
           (float)( (cputstop.tms_cutime + cputstop.tms_cstime) -
                    (cputstart.tms_cutime + cputstart.tms_cstime) ) /
           (float)CLOCKS_PER_SEC * 1000);
    printf("--------------------------------------------\n");

    exit(0);
}
	.file	"tmpxft_003b9a67_00000000-6_MatrixNormalizationCuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2051:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2051:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z9time_seedv
	.type	_Z9time_seedv, @function
_Z9time_seedv:
.LFB2040:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rsi
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	movl	16(%rsp), %eax
	movq	24(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L3
	call	__stack_chk_fail@PLT
.L3:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2040:
	.size	_Z9time_seedv, .-_Z9time_seedv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Random seed = %i\n"
.LC1:
	.string	"N = %i is out of range.\n"
.LC2:
	.string	"Usage: %s <matrix_dimension> [random seed]\n"
.LC3:
	.string	"\nMatrix dimension N = %i.\n"
	.text
	.globl	_Z10parametersiPPc
	.type	_Z10parametersiPPc, @function
_Z10parametersiPPc:
.LFB2041:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%edi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rsi, %rbx
	pushq	%r8
	.cfi_def_cfa_offset 32
	call	_Z9time_seedv
	movl	%eax, %edi
	call	srand@PLT
	cmpl	$3, %ebp
	jne	.L7
	movq	16(%rbx), %rdi
	call	atoi@PLT
	movl	%eax, %ebp
	movl	%eax, %edi
	call	srand@PLT
	movl	%ebp, %edx
	leaq	.LC0(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L8
.L7:
	decl	%ebp
	jle	.L9
.L8:
	movq	8(%rbx), %rdi
	call	atoi@PLT
	movl	%eax, N(%rip)
	movl	%eax, %edx
	leal	-1(%rax), %eax
	cmpl	$14999, %eax
	jbe	.L10
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L12
.L9:
	movq	(%rbx), %rdx
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L12:
	xorl	%edi, %edi
	call	exit@PLT
.L10:
	popq	%rcx
	.cfi_def_cfa_offset 24
	leaq	.LC3(%rip), %rsi
	popq	%rbx
	.cfi_def_cfa_offset 16
	movl	$2, %edi
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2041:
	.size	_Z10parametersiPPc, .-_Z10parametersiPPc
	.section	.rodata.str1.1
.LC4:
	.string	"\nInitializing...\n"
	.text
	.globl	_Z17initialize_inputsv
	.type	_Z17initialize_inputsv, @function
_Z17initialize_inputsv:
.LFB2042:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	xorl	%ebx, %ebx
	call	__printf_chk@PLT
.L14:
	cmpl	%ebx, N(%rip)
	jle	.L19
	xorl	%ebp, %ebp
	movslq	%ebx, %r12
.L16:
	cmpl	%ebp, N(%rip)
	jle	.L20
	call	rand@PLT
	movslq	%ebp, %rdx
	incl	%ebp
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC5(%rip), %xmm0
	imulq	$15000, %r12, %rax
	addq	%rdx, %rax
	leaq	A(%rip), %rdx
	movss	%xmm0, (%rdx,%rax,4)
	leaq	B(%rip), %rdx
	movl	$0x00000000, (%rdx,%rax,4)
	jmp	.L16
.L20:
	incl	%ebx
	jmp	.L14
.L19:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2042:
	.size	_Z17initialize_inputsv, .-_Z17initialize_inputsv
	.section	.rodata.str1.1
.LC7:
	.string	", "
.LC8:
	.string	";\n\t"
.LC9:
	.string	"\nA =\n\t"
.LC10:
	.string	"%5.2f%s"
	.text
	.globl	_Z12print_inputsv
	.type	_Z12print_inputsv, @function
_Z12print_inputsv:
.LFB2043:
	.cfi_startproc
	endbr64
	cmpl	$9, N(%rip)
	jg	.L30
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	leaq	.LC8(%rip), %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	pushq	%rdx
	.cfi_def_cfa_offset 48
	call	__printf_chk@PLT
.L23:
	cmpl	N(%rip), %ebp
	jge	.L33
	xorl	%ebx, %ebx
	movslq	%ebp, %r13
.L26:
	movl	N(%rip), %eax
	cmpl	%ebx, %eax
	jle	.L34
	decl	%eax
	leaq	.LC7(%rip), %rdx
	leaq	A(%rip), %rcx
	movl	$2, %edi
	cmpl	%ebx, %eax
	movslq	%ebx, %rax
	leaq	.LC10(%rip), %rsi
	cmovle	%r12, %rdx
	imulq	$15000, %rax, %rax
	incl	%ebx
	addq	%r13, %rax
	movss	(%rcx,%rax,4), %xmm0
	movb	$1, %al
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	jmp	.L26
.L34:
	incl	%ebp
	jmp	.L23
.L33:
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
.L30:
	.cfi_restore 3
	.cfi_restore 6
	.cfi_restore 12
	.cfi_restore 13
	ret
	.cfi_endproc
.LFE2043:
	.size	_Z12print_inputsv, .-_Z12print_inputsv
	.section	.rodata.str1.1
.LC11:
	.string	"\nB =\n\t"
.LC12:
	.string	"%1.10f%s"
	.text
	.globl	_Z7print_Bv
	.type	_Z7print_Bv, @function
_Z7print_Bv:
.LFB2044:
	.cfi_startproc
	endbr64
	cmpl	$9, N(%rip)
	jg	.L44
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	leaq	.LC8(%rip), %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	pushq	%rdx
	.cfi_def_cfa_offset 48
	call	__printf_chk@PLT
.L37:
	cmpl	N(%rip), %ebp
	jge	.L47
	xorl	%ebx, %ebx
	movslq	%ebp, %r13
.L40:
	movl	N(%rip), %eax
	cmpl	%ebx, %eax
	jle	.L48
	decl	%eax
	movslq	%ebx, %rcx
	leaq	.LC7(%rip), %rdx
	movl	$2, %edi
	cmpl	%ebx, %eax
	leaq	.LC12(%rip), %rsi
	cmovle	%r12, %rdx
	imulq	$15000, %r13, %rax
	incl	%ebx
	addq	%rcx, %rax
	leaq	B(%rip), %rcx
	movss	(%rcx,%rax,4), %xmm0
	movb	$1, %al
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	jmp	.L40
.L48:
	incl	%ebp
	jmp	.L37
.L47:
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
.L44:
	.cfi_restore 3
	.cfi_restore 6
	.cfi_restore 12
	.cfi_restore 13
	ret
	.cfi_endproc
.LFE2044:
	.size	_Z7print_Bv, .-_Z7print_Bv
	.globl	_Z44__device_stub__Z17calculateBlockSumPKfPfS1_mPKfPfS1_m
	.type	_Z44__device_stub__Z17calculateBlockSumPKfPfS1_mPKfPfS1_m, @function
_Z44__device_stub__Z17calculateBlockSumPKfPfS1_mPKfPfS1_m:
.LFB2073:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movq	%rcx, (%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	movq	%rsp, %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L49
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z17calculateBlockSumPKfPfS1_m(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L49:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L51
	call	__stack_chk_fail@PLT
.L51:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2073:
	.size	_Z44__device_stub__Z17calculateBlockSumPKfPfS1_mPKfPfS1_m, .-_Z44__device_stub__Z17calculateBlockSumPKfPfS1_mPKfPfS1_m
	.globl	_Z17calculateBlockSumPKfPfS1_m
	.type	_Z17calculateBlockSumPKfPfS1_m, @function
_Z17calculateBlockSumPKfPfS1_m:
.LFB2074:
	.cfi_startproc
	endbr64
	jmp	_Z44__device_stub__Z17calculateBlockSumPKfPfS1_mPKfPfS1_m
	.cfi_endproc
.LFE2074:
	.size	_Z17calculateBlockSumPKfPfS1_m, .-_Z17calculateBlockSumPKfPfS1_m
	.globl	_Z44__device_stub__Z17calculateFinalSumPfS_mS_S_PfS_mS_S_
	.type	_Z44__device_stub__Z17calculateFinalSumPfS_mS_S_PfS_mS_S_, @function
_Z44__device_stub__Z17calculateFinalSumPfS_mS_S_PfS_mS_S_:
.LFB2075:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L54
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z17calculateFinalSumPfS_mS_S_(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 192
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L54:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L56
	call	__stack_chk_fail@PLT
.L56:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2075:
	.size	_Z44__device_stub__Z17calculateFinalSumPfS_mS_S_PfS_mS_S_, .-_Z44__device_stub__Z17calculateFinalSumPfS_mS_S_PfS_mS_S_
	.globl	_Z17calculateFinalSumPfS_mS_S_
	.type	_Z17calculateFinalSumPfS_mS_S_, @function
_Z17calculateFinalSumPfS_mS_S_:
.LFB2076:
	.cfi_startproc
	endbr64
	jmp	_Z44__device_stub__Z17calculateFinalSumPfS_mS_S_PfS_mS_S_
	.cfi_endproc
.LFE2076:
	.size	_Z17calculateFinalSumPfS_mS_S_, .-_Z17calculateFinalSumPfS_mS_S_
	.globl	_Z12calculateSumPfmS_S_S_mmiS_S_
	.type	_Z12calculateSumPfmS_S_S_mmiS_S_, @function
_Z12calculateSumPfmS_S_S_mmiS_S_:
.LFB2045:
	.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
	movq	%rdi, %r14
	movl	$8, %edi
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rsi, %r13
	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
	movq	%r8, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%r9, 8(%rsp)
	movq	112(%rsp), %r15
	movq	%rcx, (%rsp)
	call	malloc@PLT
	movl	$1, %ecx
	movq	%r14, %rsi
	movq	%r12, %rdi
	movslq	N(%rip), %rdx
	movq	%rax, %rbx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %r9
	movl	%r15d, 24(%rsp)
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	movl	%r9d, 36(%rsp)
	xorl	%r9d, %r9d
	movq	%rax, 40(%rsp)
	movl	44(%rsp), %ecx
	movq	%rax, 28(%rsp)
	movq	36(%rsp), %rdx
	movq	24(%rsp), %rdi
	movl	32(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L60
	movq	(%rsp), %rsi
	movq	%r13, %rcx
	movq	%rbp, %rdx
	movq	%r12, %rdi
	call	_Z44__device_stub__Z17calculateBlockSumPKfPfS1_mPKfPfS1_m
.L60:
	movl	%r15d, 36(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1, %esi
	movabsq	$4294967297, %rdi
	movl	$1, 32(%rsp)
	movq	%rdi, 40(%rsp)
	movl	44(%rsp), %ecx
	movq	36(%rsp), %rdx
	movq	%rdi, 24(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L61
	movslq	120(%rsp), %rdx
	movq	136(%rsp), %r8
	movq	%rbp, %rsi
	movq	128(%rsp), %rcx
	movq	(%rsp), %rdi
	call	_Z44__device_stub__Z17calculateFinalSumPfS_mS_S_PfS_mS_S_
.L61:
	movq	128(%rsp), %rsi
	movq	%rbx, %rdi
	movl	$2, %ecx
	movl	$4, %edx
	call	cudaMemcpy@PLT
	leaq	4(%rbx), %rdi
	movl	$2, %ecx
	movq	136(%rsp), %rsi
	movl	$4, %edx
	call	cudaMemcpy@PLT
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	movq	%rbx, %rax
	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
.LFE2045:
	.size	_Z12calculateSumPfmS_S_S_mmiS_S_, .-_Z12calculateSumPfmS_S_S_mmiS_S_
	.section	.rodata.str1.1
.LC13:
	.string	"Parallel Computing.\n"
	.text
	.globl	_Z10matrixNormv
	.type	_Z10matrixNormv, @function
_Z10matrixNormv:
.LFB2046:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%edx, %edx
	leaq	.LC13(%rip), %rsi
	movl	$2, %edi
	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	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	movq	%rdx, (%rsp)
	movq	%rdx, 8(%rsp)
	movq	%rdx, 16(%rsp)
	movq	%rdx, 24(%rsp)
	movq	%rdx, 32(%rsp)
	call	__printf_chk@PLT
	movslq	N(%rip), %rbx
	testb	$15, %bl
	jne	.L64
	shrq	$4, %rbx
	jmp	.L65
.L64:
	shrq	$4, %rbx
	incq	%rbx
.L65:
	cvtsi2sdq	%rbx, %xmm0
	leaq	A(%rip), %r15
	movq	%r15, %r14
	leaq	B(%rip), %r13
	call	log@PLT
	divsd	.LC14(%rip), %xmm0
	call	ceil@PLT
	movaps	%xmm0, %xmm1
	movsd	.LC15(%rip), %xmm0
	call	pow@PLT
	movq	%rsp, %rdi
	cvttsd2sil	%xmm0, %r12d
	movslq	%r12d, %rbp
	salq	$2, %rbp
	movq	%rbp, %rsi
	call	cudaMalloc@PLT
	movq	(%rsp), %rdi
	movq	%rbp, %rdx
	xorl	%esi, %esi
	call	cudaMemset@PLT
	movq	%rbp, %rsi
	leaq	8(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movq	%rbp, %rdx
	xorl	%esi, %esi
	xorl	%ebp, %ebp
	call	cudaMemset@PLT
	movslq	N(%rip), %rsi
	leaq	16(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
	leaq	32(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
.L66:
	movslq	N(%rip), %rsi
	movq	(%rsp), %rcx
	cmpl	%ebp, %esi
	jle	.L76
	pushq	32(%rsp)
	.cfi_def_cfa_offset 120
	movq	%r15, %rdi
	movl	$16, %r9d
	pushq	32(%rsp)
	.cfi_def_cfa_offset 128
	pushq	%r12
	.cfi_def_cfa_offset 136
	pushq	%rbx
	.cfi_def_cfa_offset 144
	movq	48(%rsp), %rdx
	movq	40(%rsp), %r8
	call	_Z12calculateSumPfmS_S_S_mmiS_S_
	movl	N(%rip), %edx
	movslq	%ebp, %rsi
	xorl	%ecx, %ecx
	movss	(%rax), %xmm4
	imulq	$15000, %rsi, %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 112
	cvtsi2ssl	%edx, %xmm2
	movaps	%xmm4, %xmm1
	divss	%xmm2, %xmm1
	movaps	%xmm1, %xmm0
	movaps	%xmm1, %xmm3
	mulss	%xmm1, %xmm0
	addss	%xmm1, %xmm3
	mulss	%xmm4, %xmm3
	mulss	%xmm2, %xmm0
	addss	4(%rax), %xmm0
	subss	%xmm3, %xmm0
	xorps	%xmm3, %xmm3
	divss	%xmm2, %xmm0
.L67:
	cmpl	%ecx, %edx
	jle	.L77
	ucomiss	%xmm3, %xmm0
	movslq	%ecx, %rax
	jp	.L68
	jne	.L68
	imulq	$15000, %rax, %rax
	addq	%rsi, %rax
	movl	$0x00000000, 0(%r13,%rax,4)
	jmp	.L70
.L68:
	leaq	(%rdi,%rax), %r8
	movss	(%r14,%r8,4), %xmm2
	imulq	$15000, %rax, %rax
	subss	%xmm1, %xmm2
	addq	%rsi, %rax
	divss	%xmm0, %xmm2
	movss	%xmm2, 0(%r13,%rax,4)
.L70:
	incl	%ecx
	jmp	.L67
.L77:
	incl	%ebp
	addq	$60000, %r15
	jmp	.L66
.L76:
	movq	%rcx, %rdi
	call	cudaFree@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L73
	call	__stack_chk_fail@PLT
.L73:
	addq	$56, %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
.LFE2046:
	.size	_Z10matrixNormv, .-_Z10matrixNormv
	.section	.rodata.str1.1
.LC16:
	.string	"\nStarting clock.\n"
.LC17:
	.string	"Stopped clock.\n"
.LC19:
	.string	"\nElapsed time = %g ms.\n"
.LC21:
	.string	"(CPU times are accurate to the nearest %g ms)\n"
.LC23:
	.string	"My total CPU time for parent = %g ms.\n"
.LC24:
	.string	"My system CPU time for parent = %g ms.\n"
.LC25:
	.string	"My total CPU time for child processes = %g ms.\n"
.LC26:
	.string	"--------------------------------------------\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2048:
	.cfi_startproc
	endbr64
	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
	subq	$112, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rbx
	call	_Z10parametersiPPc
	call	_Z17initialize_inputsv
	call	_Z12print_inputsv
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbx, %rsi
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	leaq	40(%rsp), %rdi
	call	times@PLT
	call	_Z10matrixNormv
	movq	%rbx, %rsi
	leaq	24(%rsp), %rdi
	call	gettimeofday@PLT
	leaq	72(%rsp), %rdi
	call	times@PLT
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	32(%rsp), %rbx
	imulq	$1000000, 8(%rsp), %r12
	imulq	$1000000, 24(%rsp), %rbp
	subq	16(%rsp), %rbp
	call	_Z7print_Bv
	leaq	(%rbx,%rbp), %rax
	subq	%r12, %rax
	js	.L79
	cvtsi2ssq	%rax, %xmm0
	jmp	.L80
.L79:
	movq	%rax, %rdx
	andl	$1, %eax
	shrq	%rdx
	orq	%rax, %rdx
	cvtsi2ssq	%rdx, %xmm0
	addss	%xmm0, %xmm0
.L80:
	leaq	.LC19(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	divss	.LC18(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movl	$2, %edi
	movb	$1, %al
	movsd	.LC20(%rip), %xmm0
	leaq	.LC21(%rip), %rsi
	call	__printf_chk@PLT
	movq	80(%rsp), %rax
	addq	72(%rsp), %rax
	leaq	.LC23(%rip), %rsi
	subq	48(%rsp), %rax
	subq	40(%rsp), %rax
	movl	$2, %edi
	cvtsi2ssq	%rax, %xmm0
	divss	.LC22(%rip), %xmm0
	movb	$1, %al
	mulss	.LC18(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	80(%rsp), %rax
	subq	48(%rsp), %rax
	cvtsi2ssq	%rax, %xmm0
	divss	.LC22(%rip), %xmm0
	leaq	.LC24(%rip), %rsi
	movl	$2, %edi
	mulss	.LC18(%rip), %xmm0
	movb	$1, %al
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	96(%rsp), %rax
	addq	88(%rsp), %rax
	subq	64(%rsp), %rax
	subq	56(%rsp), %rax
	movl	$2, %edi
	cvtsi2ssq	%rax, %xmm0
	divss	.LC22(%rip), %xmm0
	leaq	.LC25(%rip), %rsi
	movb	$1, %al
	mulss	.LC18(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC26(%rip), %rsi
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
	.cfi_endproc
.LFE2048:
	.size	main, .-main
	.section	.rodata.str1.1
.LC27:
	.string	"_Z17calculateFinalSumPfS_mS_S_"
.LC28:
	.string	"_Z17calculateBlockSumPKfPfS1_m"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2078:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -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	.LC27(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z17calculateFinalSumPfS_mS_S_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC28(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z17calculateBlockSumPKfPfS1_m(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	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	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2078:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.globl	B
	.bss
	.align 32
	.type	B, @object
	.size	B, 900000000
B:
	.zero	900000000
	.globl	A
	.align 32
	.type	A, @object
	.size	A, 900000000
A:
	.zero	900000000
	.globl	N
	.align 4
	.type	N, @object
	.size	N, 4
N:
	.zero	4
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC5:
	.long	939524096
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC14:
	.long	-17155601
	.long	1072049730
	.align 8
.LC15:
	.long	0
	.long	1073741824
	.section	.rodata.cst4
	.align 4
.LC18:
	.long	1148846080
	.section	.rodata.cst8
	.align 8
.LC20:
	.long	-755914244
	.long	1062232653
	.section	.rodata.cst4
	.align 4
.LC22:
	.long	1232348160
	.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:

/*
 * Created by Harshavardhan Patil on 9/30/16.
 *
 *
 *  Matrix Normalization using CUDA :
 *  - The generated input values are stored by inverting the matrix. i.e All the attributes of a column which needs to be normalized
 *    are stored as elements of a row. So that while normalizing the threads in a block will access nearby elements which will
 *    optimize the code
 *  - The number of threads is hard coded to 16. This value is finalized after multiple iterations with values (32, 64, 128, 256, 512).
 *    This value is used along with the size of the matrix to calculate number of blocks.
 *  - Each block is assigned to calculate sum and squares of one row.
 *    In a block each thread will read one element and put it in a shared memory. Once this is done, partial sum is calculated and
 *    each block stores it's partial sum in the global memory area using it's block Id as index.
 *  - Once partial sum calculation is done by all the blocks, another kernal function is launched with this partial
 *    sum as the input in a single block. This block calculates the final sum and squares.
 *  - Partial sum using reduction works only if the number of elements passed to the block is a power of 2.
 *    So to avoid wrong calculation when number of blocks is not power of 2 (in calculateFinalSum method).
 *    The input array argument lentgh is set to the nearest power of 2 for the number of blocks and value 0 is set to those indices
 *    which are greater than number of blocks.
 *  - Then the population standard deviation is calculated for that row using formula (sumOfSquares + N * powf(mean, 2.0) - 2 * mean * sumOfTheElements)/N;
 *    Where,
 *        N - Size of the Matrix
 *  - The above values are used to calculate standard score of each element in that row.
 *  - The computed values are stored in the output matrix at their inverse position. This operation is done for all the elements
 *
 *  Steps to compile and execute
    -----------------------------
    1) Go to folder "/home/hpatil2/hw4"
    2) run : qlogin -q interactive.q
    3) run : cd /home/hpatil2/hw4
    4) run : nvcc MatrixNormalizationCuda.cu -o MatrixNormalizationCuda
    5) run : ./MatrixNormalizationCuda 15000 4

    In step 5 : [argument1 (15000) is MATRIX_SIZE, this is mandatory to pass. and maximum value it can take is 15000]
                [argument2 (4) is seed value, this is an optional field]
 *
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/time.h>
#include <time.h>
#include <hip/hip_runtime.h>

/* Program Parameters */
#define MAXN 15000  /* Max value of N */
int N;  /* Matrix size */

/* Matrices */
volatile float A[MAXN][MAXN], B[MAXN][MAXN];

/* junk */
#define randm() 4|2[uid]&3

/* Prototype */
void matrixNorm();

/* returns a seed for srand based on the time */
unsigned int time_seed() {
    struct timeval t;
    struct timezone tzdummy;

    gettimeofday(&t, &tzdummy);
    return (unsigned int)(t.tv_usec);
}

/* Set the program parameters from the command-line arguments */
void parameters(int argc, char **argv) {
    int seed = 0;  /* Random seed */
    char uid[32]; /*User name */

    /* Read command-line arguments */
    srand(time_seed());  /* Randomize */

    if (argc == 3) {
        seed = atoi(argv[2]);
        srand(seed);
        printf("Random seed = %i\n", seed);
    }
    if (argc >= 2) {
        N = atoi(argv[1]);
        if (N < 1 || N > MAXN) {
            printf("N = %i is out of range.\n", N);
            exit(0);
        }
    }
    else {
        printf("Usage: %s <matrix_dimension> [random seed]\n",
               argv[0]);
        exit(0);
    }

    /* Print parameters */
    printf("\nMatrix dimension N = %i.\n", N);
}

/* Initialize A and B*/
void initialize_inputs() {
    int row, col;

    printf("\nInitializing...\n");
    for (row = 0; row < N; row++) {
        for (col = 0; col < N; col++) {
            A[row][col] = (float)rand() / 32768.0;
            B[row][col] = 0.0;
        }
    }

}

/* Print input matrices */
void print_inputs() {
    int row, col;

    if (N < 10) {
        printf("\nA =\n\t");
        for (col = 0; col < N; col++) {
            for (row = 0; row < N; row++) {
                printf("%5.2f%s", A[row][col], (row < N-1) ? ", " : ";\n\t");
            }
        }
    }
}

void print_B() {
    int row, col;

    if (N < 10) {
        printf("\nB =\n\t");
        for (row = 0; row < N; row++) {
            for (col = 0; col < N; col++) {
                printf("%1.10f%s", B[row][col], (col < N-1) ? ", " : ";\n\t");
            }
        }
    }
}

/*
 *  This method calculates sum and square of given block
 */
__global__ void calculateBlockSum(const float *input, float *sumResults, float *squareResults, const size_t n)
{
    __shared__ float smSum[512];
    __shared__ float smSquare[512];
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    int tx = threadIdx.x;
    float x = 0;

    if(i < n) {
        x = input[i];
    }
    smSum[tx] = x;
    smSquare[tx] = x*x;
    __syncthreads();

    int j;
    for(j = blockDim.x / 2; j > 0; j >>= 1) {

        if(tx < j) {
            smSum[tx] = smSum[tx]+ smSum[tx + j];
            smSquare[tx] = smSquare[tx] + smSquare[tx + j];
        }
        __syncthreads();
    }

    if(threadIdx.x == 0) {

        sumResults[blockIdx.x] = smSum[0];
        squareResults[blockIdx.x] = smSquare[0];
    }
}

/*
 *  This method calculates final sum and square
 */
__global__ void calculateFinalSum(float *sumResults, float *squareResults, const size_t size, float *finalSumResult, float *finalSigmaResult)  {

    __shared__ float smSum[512];
    __shared__ float smSquare[512];
    int tx = threadIdx.x;

    if(tx < size) {
        smSum[tx] = sumResults[tx];
        smSquare[tx] = squareResults[tx];
    }
    __syncthreads();

    int i;
    for(i = size/2; i > 0; i >>= 1) {

        if(tx < i) {
            smSum[tx] = smSum[tx] + smSum[tx + i];
            smSquare[tx] = smSquare[tx] + smSquare[tx + i];
        }
        __syncthreads();
    }

    if(threadIdx.x == 0) {

        finalSumResult[0] = smSum[0];
        finalSigmaResult[0] = smSquare[0];
    }
}

float* calculateSum (float *input, size_t n, float *dMatrixA, float *sumValue, float *sigmaValue, size_t blockSize, size_t totalBlocks, int nextNearestPowerOf2, float *finalSumResult, float *finalSigmaResult) {

    float *results = (float *)malloc(sizeof(float) * 2);

    hipMemcpy(dMatrixA, input, sizeof(float) * N, hipMemcpyHostToDevice);
    calculateBlockSum<<<totalBlocks, blockSize>>> (dMatrixA, sumValue, sigmaValue, n);
    calculateFinalSum<<<1,totalBlocks>>>(sumValue, sigmaValue, nextNearestPowerOf2, finalSumResult, finalSigmaResult);

    hipMemcpy(&results[0], &finalSumResult[0], sizeof(float), hipMemcpyDeviceToHost);
    hipMemcpy(&results[1], &finalSigmaResult[0], sizeof(float), hipMemcpyDeviceToHost);

    return results;
}

void matrixNorm() {

    int row, column;
    float mu, sigma;
    float *sumValue = 0, *sigmaValue = 0, *dMatrixA = 0;
    float *finalSumResult = 0, *finalSigmaResult = 0;


    printf("Parallel Computing.\n");

    size_t blockSize = 16;
    size_t totalBlocks;

    if(N%blockSize == 0){
        totalBlocks = (N/blockSize);
    } else {
        totalBlocks = (N/blockSize) + 1;
    }

    int nextNearestPowerOf2 = pow(2, ceil(log(totalBlocks)/log(2)));

    hipMalloc((void**)&sumValue, sizeof(float) * (nextNearestPowerOf2));
    hipMemset(sumValue, 0.0, sizeof(float) * nextNearestPowerOf2);
    hipMalloc((void**)&sigmaValue, sizeof(float) * (nextNearestPowerOf2));
    hipMemset(sigmaValue, 0.0, sizeof(float) * nextNearestPowerOf2);
    hipMalloc((void**)&dMatrixA, sizeof(float) * N);
    hipMalloc((void**)&finalSumResult, sizeof(float));
    hipMalloc((void**)&finalSigmaResult, sizeof(float));

    for (column=0; column < N; column++) {

        mu = 0.0;
        float *result;
        result = calculateSum ((float *)A[column], N, dMatrixA, sumValue, sigmaValue, blockSize, totalBlocks, nextNearestPowerOf2,finalSumResult,finalSigmaResult);
        mu = result[0] / (float) N;
        sigma = (result[1] + N * powf(mu, 2.0) - 2 * mu * result[0])/(float)N;

        for (row=0; row < N; row++) {
            if (sigma == 0.0) {
                B[row][column] = 0.0;
            } else {
                B[row][column] = (A[column][row] - mu) / sigma;
            }
        }
    }
    hipFree(sumValue);
    hipFree(sigmaValue);
    hipFree(dMatrixA);
}


int main(int argc, char **argv) {

    struct timeval etstart, etstop;
    struct timezone tzdummy;
    clock_t etstart2, etstop2;
    unsigned long long usecstart, usecstop;
    struct tms cputstart, cputstop;

    parameters(argc, argv);
    initialize_inputs();
    print_inputs();

    printf("\nStarting clock.\n");
    gettimeofday(&etstart, &tzdummy);
    etstart2 = times(&cputstart);

    matrixNorm();

    gettimeofday(&etstop, &tzdummy);
    etstop2 = times(&cputstop);
    printf("Stopped clock.\n");
    usecstart = (unsigned long long)etstart.tv_sec * 1000000 + etstart.tv_usec;
    usecstop = (unsigned long long)etstop.tv_sec * 1000000 + etstop.tv_usec;

    print_B();

    printf("\nElapsed time = %g ms.\n",
           (float)(usecstop - usecstart)/(float)1000);

    printf("(CPU times are accurate to the nearest %g ms)\n",
           1.0/(float)CLOCKS_PER_SEC * 1000.0);
    printf("My total CPU time for parent = %g ms.\n",
           (float)( (cputstop.tms_utime + cputstop.tms_stime) -
                    (cputstart.tms_utime + cputstart.tms_stime) ) /
           (float)CLOCKS_PER_SEC * 1000);
    printf("My system CPU time for parent = %g ms.\n",
           (float)(cputstop.tms_stime - cputstart.tms_stime) /
           (float)CLOCKS_PER_SEC * 1000);
    printf("My total CPU time for child processes = %g ms.\n",
           (float)( (cputstop.tms_cutime + cputstop.tms_cstime) -
                    (cputstart.tms_cutime + cputstart.tms_cstime) ) /
           (float)CLOCKS_PER_SEC * 1000);
    printf("--------------------------------------------\n");

    exit(0);
}
	.text
	.file	"MatrixNormalizationCuda.hip"
	.globl	_Z9time_seedv                   # -- Begin function _Z9time_seedv
	.type	_Z9time_seedv,@function
_Z9time_seedv:                          # @_Z9time_seedv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	leaq	16(%rsp), %rbx
	leaq	8(%rsp), %rsi
	movq	%rbx, %rdi
	callq	gettimeofday
	movl	8(%rbx), %eax
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z9time_seedv, .Lfunc_end0-_Z9time_seedv
	.cfi_endproc
                                        # -- End function
	.globl	_Z10parametersiPPc              # -- Begin function _Z10parametersiPPc
	.type	_Z10parametersiPPc,@function
_Z10parametersiPPc:                     # @_Z10parametersiPPc
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movl	%edi, %ebp
	leaq	16(%rsp), %r14
	leaq	8(%rsp), %rsi
	movq	%r14, %rdi
	callq	gettimeofday
	movl	8(%r14), %edi
	callq	srand
	cmpl	$3, %ebp
	jne	.LBB1_2
# %bb.1:                                # %.thread
	movq	16(%rbx), %rdi
	callq	atoi
	movl	%eax, %ebp
	movl	%eax, %edi
	callq	srand
	movl	$.L.str, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB1_3
.LBB1_2:
	cmpl	$2, %ebp
	jl	.LBB1_7
.LBB1_3:
	movq	8(%rbx), %rdi
	callq	atoi
	movl	%eax, N(%rip)
	movl	$-15001, %ecx                   # imm = 0xC567
	movl	%eax, %edx
	addl	%ecx, %edx
	cmpl	%ecx, %edx
	jbe	.LBB1_5
# %bb.4:
	movl	$.L.str.3, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	printf                          # TAILCALL
.LBB1_5:
	.cfi_def_cfa_offset 64
	movl	$.L.str.1, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB1_6
.LBB1_7:
	movq	(%rbx), %rsi
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
.LBB1_6:
	xorl	%edi, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z10parametersiPPc, .Lfunc_end1-_Z10parametersiPPc
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z17initialize_inputsv
.LCPI2_0:
	.long	0x38000000                      # float 3.05175781E-5
	.text
	.globl	_Z17initialize_inputsv
	.type	_Z17initialize_inputsv,@function
_Z17initialize_inputsv:                 # @_Z17initialize_inputsv
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	N(%rip), %eax
	testl	%eax, %eax
	jle	.LBB2_6
# %bb.1:                                # %.preheader.preheader
	xorl	%ebx, %ebx
	xorl	%r14d, %r14d
.LBB2_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_4 Depth 2
	testl	%eax, %eax
	jle	.LBB2_7
# %bb.3:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB2_2 Depth=1
	xorl	%r15d, %r15d
.LBB2_4:                                # %.lr.ph
                                        #   Parent Loop BB2_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	movss	.LCPI2_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	%xmm1, %xmm0
	movss	%xmm0, A(%rbx,%r15,4)
	movl	$0, B(%rbx,%r15,4)
	incq	%r15
	movl	N(%rip), %eax
	movslq	%eax, %rcx
	cmpq	%rcx, %r15
	jl	.LBB2_4
	jmp	.LBB2_5
.LBB2_7:                                # %.preheader.._crit_edge_crit_edge
                                        #   in Loop: Header=BB2_2 Depth=1
	movslq	%eax, %rcx
.LBB2_5:                                # %._crit_edge
                                        #   in Loop: Header=BB2_2 Depth=1
	incq	%r14
	addq	$60000, %rbx                    # imm = 0xEA60
	cmpq	%rcx, %r14
	jl	.LBB2_2
.LBB2_6:                                # %._crit_edge11
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z17initialize_inputsv, .Lfunc_end2-_Z17initialize_inputsv
	.cfi_endproc
                                        # -- End function
	.globl	_Z12print_inputsv               # -- Begin function _Z12print_inputsv
	.type	_Z12print_inputsv,@function
_Z12print_inputsv:                      # @_Z12print_inputsv
	.cfi_startproc
# %bb.0:
	cmpl	$9, N(%rip)
	jg	.LBB3_8
# %bb.1:
	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	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	movl	N(%rip), %eax
	testl	%eax, %eax
	jle	.LBB3_7
# %bb.2:                                # %.preheader.preheader
	movl	$A, %ebx
	xorl	%r14d, %r14d
	movl	$.L.str.7, %r15d
.LBB3_3:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_5 Depth 2
	testl	%eax, %eax
	jle	.LBB3_9
# %bb.4:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB3_3 Depth=1
	movq	%rbx, %r12
	xorl	%r13d, %r13d
.LBB3_5:                                # %.lr.ph
                                        #   Parent Loop BB3_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r12), %xmm0
	decl	%eax
	cltq
	cmpq	%rax, %r13
	movl	$.L.str.8, %esi
	cmovlq	%r15, %rsi
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	incq	%r13
	movl	N(%rip), %eax
	movslq	%eax, %rcx
	addq	$60000, %r12                    # imm = 0xEA60
	cmpq	%rcx, %r13
	jl	.LBB3_5
	jmp	.LBB3_6
.LBB3_9:                                # %.preheader.._crit_edge_crit_edge
                                        #   in Loop: Header=BB3_3 Depth=1
	movslq	%eax, %rcx
.LBB3_6:                                # %._crit_edge
                                        #   in Loop: Header=BB3_3 Depth=1
	incq	%r14
	addq	$4, %rbx
	cmpq	%rcx, %r14
	jl	.LBB3_3
.LBB3_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
.LBB3_8:                                # %.loopexit
	retq
.Lfunc_end3:
	.size	_Z12print_inputsv, .Lfunc_end3-_Z12print_inputsv
	.cfi_endproc
                                        # -- End function
	.globl	_Z7print_Bv                     # -- Begin function _Z7print_Bv
	.type	_Z7print_Bv,@function
_Z7print_Bv:                            # @_Z7print_Bv
	.cfi_startproc
# %bb.0:
	cmpl	$9, N(%rip)
	jg	.LBB4_8
# %bb.1:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$.L.str.9, %edi
	xorl	%eax, %eax
	callq	printf
	movl	N(%rip), %eax
	testl	%eax, %eax
	jle	.LBB4_7
# %bb.2:                                # %.preheader.preheader
	movl	$B, %ebx
	xorl	%r14d, %r14d
	movl	$.L.str.7, %r15d
.LBB4_3:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_5 Depth 2
	testl	%eax, %eax
	jle	.LBB4_9
# %bb.4:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB4_3 Depth=1
	xorl	%r12d, %r12d
.LBB4_5:                                # %.lr.ph
                                        #   Parent Loop BB4_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx,%r12,4), %xmm0
	decl	%eax
	cltq
	cmpq	%rax, %r12
	movl	$.L.str.8, %esi
	cmovlq	%r15, %rsi
	movl	$.L.str.10, %edi
	movb	$1, %al
	callq	printf
	incq	%r12
	movl	N(%rip), %eax
	movslq	%eax, %rcx
	cmpq	%rcx, %r12
	jl	.LBB4_5
	jmp	.LBB4_6
.LBB4_9:                                # %.preheader.._crit_edge_crit_edge
                                        #   in Loop: Header=BB4_3 Depth=1
	movslq	%eax, %rcx
.LBB4_6:                                # %._crit_edge
                                        #   in Loop: Header=BB4_3 Depth=1
	incq	%r14
	addq	$60000, %rbx                    # imm = 0xEA60
	cmpq	%rcx, %r14
	jl	.LBB4_3
.LBB4_7:
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r14
	.cfi_restore %r15
.LBB4_8:                                # %.loopexit
	retq
.Lfunc_end4:
	.size	_Z7print_Bv, .Lfunc_end4-_Z7print_Bv
	.cfi_endproc
                                        # -- End function
	.globl	_Z32__device_stub__calculateBlockSumPKfPfS1_m # -- Begin function _Z32__device_stub__calculateBlockSumPKfPfS1_m
	.type	_Z32__device_stub__calculateBlockSumPKfPfS1_m,@function
_Z32__device_stub__calculateBlockSumPKfPfS1_m: # @_Z32__device_stub__calculateBlockSumPKfPfS1_m
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	16(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%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	$_Z17calculateBlockSumPKfPfS1_m, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end5:
	.size	_Z32__device_stub__calculateBlockSumPKfPfS1_m, .Lfunc_end5-_Z32__device_stub__calculateBlockSumPKfPfS1_m
	.cfi_endproc
                                        # -- End function
	.globl	_Z32__device_stub__calculateFinalSumPfS_mS_S_ # -- Begin function _Z32__device_stub__calculateFinalSumPfS_mS_S_
	.type	_Z32__device_stub__calculateFinalSumPfS_mS_S_,@function
_Z32__device_stub__calculateFinalSumPfS_mS_S_: # @_Z32__device_stub__calculateFinalSumPfS_mS_S_
	.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	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	24(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z17calculateFinalSumPfS_mS_S_, %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_end6:
	.size	_Z32__device_stub__calculateFinalSumPfS_mS_S_, .Lfunc_end6-_Z32__device_stub__calculateFinalSumPfS_mS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z12calculateSumPfmS_S_S_mmiS_S_ # -- Begin function _Z12calculateSumPfmS_S_S_mmiS_S_
	.type	_Z12calculateSumPfmS_S_S_mmiS_S_,@function
_Z12calculateSumPfmS_S_S_mmiS_S_:       # @_Z12calculateSumPfmS_S_S_mmiS_S_
	.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	%r9, %r15
	movq	%r8, 8(%rsp)                    # 8-byte Spill
	movq	%rcx, (%rsp)                    # 8-byte Spill
	movq	%rdx, %r14
	movq	%rsi, 16(%rsp)                  # 8-byte Spill
	movq	%rdi, %rbp
	movl	80(%rsp), %r12d
	movabsq	$4294967296, %r13               # imm = 0x100000000
	movl	$8, %edi
	callq	malloc
	movq	%rax, %rbx
	movslq	N(%rip), %rdx
	shlq	$2, %rdx
	movq	%r14, %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	btsq	$32, %r12
	movl	%r15d, %edx
	btsq	$32, %rdx
	movq	%r12, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB7_2
# %bb.1:
	movq	%r14, %rdi
	movq	(%rsp), %rsi                    # 8-byte Reload
	movq	8(%rsp), %rdx                   # 8-byte Reload
	movq	16(%rsp), %rcx                  # 8-byte Reload
	callq	_Z32__device_stub__calculateBlockSumPKfPfS1_m
.LBB7_2:
	movq	104(%rsp), %rbp
	movq	96(%rsp), %r14
	incq	%r13
	movq	%r13, %rdi
	movl	$1, %esi
	movq	%r12, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB7_4
# %bb.3:
	movslq	88(%rsp), %rdx
	movq	(%rsp), %rdi                    # 8-byte Reload
	movq	8(%rsp), %rsi                   # 8-byte Reload
	movq	%r14, %rcx
	movq	%rbp, %r8
	callq	_Z32__device_stub__calculateFinalSumPfS_mS_S_
.LBB7_4:
	movl	$4, %edx
	movq	%rbx, %rdi
	movq	%r14, %rsi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%rbx, %rdi
	addq	$4, %rdi
	movl	$4, %edx
	movq	%rbp, %rsi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%rbx, %rax
	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_end7:
	.size	_Z12calculateSumPfmS_S_S_mmiS_S_, .Lfunc_end7-_Z12calculateSumPfmS_S_S_mmiS_S_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z10matrixNormv
.LCPI8_0:
	.quad	0x3fe62e42fefa39ef              # double 0.69314718055994529
	.text
	.globl	_Z10matrixNormv
	.type	_Z10matrixNormv,@function
_Z10matrixNormv:                        # @_Z10matrixNormv
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	xorl	%eax, %eax
	leaq	32(%rsp), %r13
	movq	%rax, (%r13)
	leaq	24(%rsp), %r12
	movq	%rax, (%r12)
	leaq	16(%rsp), %r15
	movq	%rax, (%r15)
	leaq	8(%rsp), %rcx
	movq	%rax, (%rcx)
	movq	%rsp, %rcx
	movq	%rax, (%rcx)
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movslq	N(%rip), %rbx
	movl	%ebx, %eax
	andl	$15, %eax
	shrq	$4, %rbx
	cmpq	$1, %rax
	sbbq	$-1, %rbx
	cvtsi2sd	%rbx, %xmm0
	callq	log
	divsd	.LCPI8_0(%rip), %xmm0
	callq	ceil@PLT
	callq	exp2@PLT
	cvttsd2si	%xmm0, %r14d
	movslq	%r14d, %rbp
	shlq	$2, %rbp
	movq	%r13, %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	movq	(%r13), %rdi
	xorl	%esi, %esi
	movq	%rbp, %rdx
	callq	hipMemset
	movq	%r12, %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	movq	(%r12), %rdi
	xorl	%esi, %esi
	movq	%rbp, %rdx
	callq	hipMemset
	movslq	N(%rip), %rsi
	shlq	$2, %rsi
	movq	%r15, %rdi
	callq	hipMalloc
	movl	$4, %esi
	leaq	8(%rsp), %rdi
	callq	hipMalloc
	movl	$4, %esi
	movq	%rsp, %rdi
	callq	hipMalloc
	movl	N(%rip), %ecx
	testl	%ecx, %ecx
	jle	.LBB8_8
# %bb.1:                                # %.lr.ph35.preheader
	movl	$B, %r15d
	movl	$A, %r12d
	xorl	%r13d, %r13d
.LBB8_2:                                # %.lr.ph35
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB8_4 Depth 2
	imulq	$60000, %r13, %rax              # imm = 0xEA60
	leaq	A(%rax), %rdi
	movslq	%ecx, %rsi
	movq	16(%rsp), %rdx
	movq	32(%rsp), %rcx
	movq	24(%rsp), %r8
	movl	$16, %r9d
	pushq	(%rsp)
	.cfi_adjust_cfa_offset 8
	pushq	16(%rsp)
	.cfi_adjust_cfa_offset 8
	pushq	%r14
	.cfi_adjust_cfa_offset 8
	pushq	%rbx
	.cfi_adjust_cfa_offset 8
	callq	_Z12calculateSumPfmS_S_S_mmiS_S_
	addq	$32, %rsp
	.cfi_adjust_cfa_offset -32
	movl	N(%rip), %ecx
	testl	%ecx, %ecx
	jle	.LBB8_7
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB8_2 Depth=1
	xorps	%xmm2, %xmm2
	cvtsi2ss	%ecx, %xmm2
	movss	(%rax), %xmm3                   # xmm3 = mem[0],zero,zero,zero
	movaps	%xmm3, %xmm0
	divss	%xmm2, %xmm0
	movaps	%xmm0, %xmm1
	mulss	%xmm0, %xmm1
	mulss	%xmm2, %xmm1
	addss	4(%rax), %xmm1
	movaps	%xmm0, %xmm4
	addss	%xmm0, %xmm4
	mulss	%xmm3, %xmm4
	subss	%xmm4, %xmm1
	divss	%xmm2, %xmm1
	movq	%r15, %rax
	xorl	%edx, %edx
.LBB8_4:                                #   Parent Loop BB8_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm2, %xmm2
	ucomiss	%xmm2, %xmm1
	jne	.LBB8_5
	jnp	.LBB8_6
.LBB8_5:                                #   in Loop: Header=BB8_4 Depth=2
	movss	(%r12,%rdx,4), %xmm2            # xmm2 = mem[0],zero,zero,zero
	subss	%xmm0, %xmm2
	divss	%xmm1, %xmm2
.LBB8_6:                                #   in Loop: Header=BB8_4 Depth=2
	movss	%xmm2, (%rax)
	incq	%rdx
	addq	$60000, %rax                    # imm = 0xEA60
	cmpq	%rdx, %rcx
	jne	.LBB8_4
.LBB8_7:                                # %._crit_edge
                                        #   in Loop: Header=BB8_2 Depth=1
	incq	%r13
	movslq	%ecx, %rax
	addq	$4, %r15
	addq	$60000, %r12                    # imm = 0xEA60
	cmpq	%rax, %r13
	jl	.LBB8_2
.LBB8_8:                                # %._crit_edge36
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	addq	$40, %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_end8:
	.size	_Z10matrixNormv, .Lfunc_end8-_Z10matrixNormv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI9_0:
	.long	0x447a0000                      # float 1000
.LCPI9_2:
	.long	0x49742400                      # float 1.0E+6
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI9_1:
	.quad	0x3f50624dd2f1a9fc              # double 0.001
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$112, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	callq	_Z10parametersiPPc
	callq	_Z17initialize_inputsv
	callq	_Z12print_inputsv
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	leaq	96(%rsp), %rbx
	leaq	72(%rsp), %r14
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	gettimeofday
	leaq	40(%rsp), %rdi
	callq	times
	callq	_Z10matrixNormv
	leaq	80(%rsp), %r15
	movq	%r15, %rdi
	movq	%r14, %rsi
	callq	gettimeofday
	leaq	8(%rsp), %rdi
	callq	times
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movq	(%r15), %r14
	movq	8(%r15), %r15
	subq	(%rbx), %r14
	subq	8(%rbx), %r15
	callq	_Z7print_Bv
	imulq	$1000000, %r14, %rax            # imm = 0xF4240
	addq	%r15, %rax
	js	.LBB9_1
# %bb.2:
	cvtsi2ss	%rax, %xmm0
	jmp	.LBB9_3
.LBB9_1:
	movq	%rax, %rcx
	shrq	%rcx
	andl	$1, %eax
	orq	%rcx, %rax
	cvtsi2ss	%rax, %xmm0
	addss	%xmm0, %xmm0
.LBB9_3:
	divss	.LCPI9_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.14, %edi
	movb	$1, %al
	callq	printf
	movsd	.LCPI9_1(%rip), %xmm0           # xmm0 = mem[0],zero
	movl	$.L.str.15, %edi
	movb	$1, %al
	callq	printf
	movq	16(%rsp), %rbx
	movq	48(%rsp), %r14
	movq	8(%rsp), %rax
	addq	%rbx, %rax
	movq	40(%rsp), %rcx
	addq	%r14, %rcx
	subq	%rcx, %rax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
	divss	.LCPI9_2(%rip), %xmm0
	mulss	.LCPI9_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.16, %edi
	movb	$1, %al
	callq	printf
	subq	%r14, %rbx
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rbx, %xmm0
	divss	.LCPI9_2(%rip), %xmm0
	mulss	.LCPI9_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.17, %edi
	movb	$1, %al
	callq	printf
	movq	32(%rsp), %rax
	movq	56(%rsp), %rcx
	addq	24(%rsp), %rax
	addq	64(%rsp), %rcx
	subq	%rcx, %rax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
	divss	.LCPI9_2(%rip), %xmm0
	mulss	.LCPI9_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.18, %edi
	movb	$1, %al
	callq	printf
	movl	$.Lstr.4, %edi
	callq	puts@PLT
	xorl	%edi, %edi
	callq	exit
.Lfunc_end9:
	.size	main, .Lfunc_end9-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB10_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB10_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z17calculateBlockSumPKfPfS1_m, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z17calculateFinalSumPfS_mS_S_, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end10:
	.size	__hip_module_ctor, .Lfunc_end10-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB11_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB11_2:
	retq
.Lfunc_end11:
	.size	__hip_module_dtor, .Lfunc_end11-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	N,@object                       # @N
	.bss
	.globl	N
	.p2align	2, 0x0
N:
	.long	0                               # 0x0
	.size	N, 4

	.type	A,@object                       # @A
	.globl	A
	.p2align	4, 0x0
A:
	.zero	900000000
	.size	A, 900000000

	.type	B,@object                       # @B
	.globl	B
	.p2align	4, 0x0
B:
	.zero	900000000
	.size	B, 900000000

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Random seed = %i\n"
	.size	.L.str, 18

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"N = %i is out of range.\n"
	.size	.L.str.1, 25

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Usage: %s <matrix_dimension> [random seed]\n"
	.size	.L.str.2, 44

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"\nMatrix dimension N = %i.\n"
	.size	.L.str.3, 27

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"\nA =\n\t"
	.size	.L.str.5, 7

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"%5.2f%s"
	.size	.L.str.6, 8

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	", "
	.size	.L.str.7, 3

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	";\n\t"
	.size	.L.str.8, 4

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"\nB =\n\t"
	.size	.L.str.9, 7

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"%1.10f%s"
	.size	.L.str.10, 9

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

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

	.type	.L.str.14,@object               # @.str.14
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.14:
	.asciz	"\nElapsed time = %g ms.\n"
	.size	.L.str.14, 24

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"(CPU times are accurate to the nearest %g ms)\n"
	.size	.L.str.15, 47

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"My total CPU time for parent = %g ms.\n"
	.size	.L.str.16, 39

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"My system CPU time for parent = %g ms.\n"
	.size	.L.str.17, 40

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"My total CPU time for child processes = %g ms.\n"
	.size	.L.str.18, 48

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z17calculateBlockSumPKfPfS1_m"
	.size	.L__unnamed_1, 31

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z17calculateFinalSumPfS_mS_S_"
	.size	.L__unnamed_2, 31

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

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

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"\nInitializing..."
	.size	.Lstr, 17

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Parallel Computing."
	.size	.Lstr.1, 20

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"\nStarting clock."
	.size	.Lstr.2, 17

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"Stopped clock."
	.size	.Lstr.3, 15

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"--------------------------------------------"
	.size	.Lstr.4, 45

	.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 _Z32__device_stub__calculateBlockSumPKfPfS1_m
	.addrsig_sym _Z32__device_stub__calculateFinalSumPfS_mS_S_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym A
	.addrsig_sym B
	.addrsig_sym _Z17calculateBlockSumPKfPfS1_m
	.addrsig_sym _Z17calculateFinalSumPfS_mS_S_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z17calculateBlockSumPKfPfS1_m ; -- Begin function _Z17calculateBlockSumPKfPfS1_m
	.globl	_Z17calculateBlockSumPKfPfS1_m
	.p2align	8
	.type	_Z17calculateBlockSumPKfPfS1_m,@function
_Z17calculateBlockSumPKfPfS1_m:         ; @_Z17calculateBlockSumPKfPfS1_m
; %bb.0:
	s_clause 0x1
	s_load_b32 s9, s[0:1], 0x2c
	s_load_b256 s[0:7], s[0:1], 0x0
	s_mov_b32 s8, s15
	v_mov_b32_e32 v3, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s9, s9, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s8, s9, v[0:1]
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_gt_u64_e32 vcc_lo, s[6:7], v[1:2]
	s_and_saveexec_b32 s6, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	v_lshlrev_b64 v[1:2], 2, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v1, vcc_lo, s0, v1
	v_add_co_ci_u32_e32 v2, vcc_lo, s1, v2, vcc_lo
	global_load_b32 v3, v[1:2], off
.LBB0_2:
	s_or_b32 exec_lo, exec_lo, s6
	s_waitcnt vmcnt(0)
	v_dual_mul_f32 v2, v3, v3 :: v_dual_lshlrev_b32 v1, 2, v0
	s_cmp_lt_u32 s9, 2
	ds_store_2addr_stride64_b32 v1, v3, v2 offset1:8
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_7
; %bb.3:
	v_add_nc_u32_e32 v2, 0x800, v1
.LBB0_4:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_lshr_b32 s0, s9, 1
	s_mov_b32 s1, exec_lo
	v_cmpx_gt_u32_e64 s0, v0
	s_cbranch_execz .LBB0_6
; %bb.5:                                ;   in Loop: Header=BB0_4 Depth=1
	s_lshl_b32 s6, s0, 2
	s_delay_alu instid0(SALU_CYCLE_1)
	v_add_nc_u32_e32 v3, s6, v2
	v_add_nc_u32_e32 v4, s6, v1
	ds_load_b32 v5, v1
	ds_load_b32 v3, v3
	ds_load_b32 v4, v4
	ds_load_b32 v6, v2
	s_waitcnt lgkmcnt(0)
	v_dual_add_f32 v4, v5, v4 :: v_dual_add_f32 v3, v6, v3
	ds_store_b32 v1, v4
	ds_store_b32 v2, v3
.LBB0_6:                                ;   in Loop: Header=BB0_4 Depth=1
	s_or_b32 exec_lo, exec_lo, s1
	s_cmp_gt_u32 s9, 3
	s_mov_b32 s9, s0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_4
.LBB0_7:                                ; %._crit_edge
	s_mov_b32 s9, 0
	s_mov_b32 s0, exec_lo
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB0_9
; %bb.8:
	v_mov_b32_e32 v2, 0
	s_lshl_b64 s[0:1], s[8:9], 2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_u32 s4, s4, s0
	s_addc_u32 s5, s5, s1
	ds_load_2addr_stride64_b32 v[0:1], v2 offset1:8
	s_add_u32 s0, s2, s0
	s_addc_u32 s1, s3, s1
	s_waitcnt lgkmcnt(0)
	s_clause 0x1
	global_store_b32 v2, v0, s[0:1]
	global_store_b32 v2, v1, s[4:5]
.LBB0_9:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z17calculateBlockSumPKfPfS1_m
		.amdhsa_group_segment_fixed_size 4096
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.amdhsa_user_sgpr_count 15
		.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 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 7
		.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	_Z17calculateBlockSumPKfPfS1_m, .Lfunc_end0-_Z17calculateBlockSumPKfPfS1_m
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 376
; NumSgprs: 18
; NumVgprs: 7
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 4096 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 7
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.protected	_Z17calculateFinalSumPfS_mS_S_ ; -- Begin function _Z17calculateFinalSumPfS_mS_S_
	.globl	_Z17calculateFinalSumPfS_mS_S_
	.p2align	8
	.type	_Z17calculateFinalSumPfS_mS_S_,@function
_Z17calculateFinalSumPfS_mS_S_:         ; @_Z17calculateFinalSumPfS_mS_S_
; %bb.0:
	s_clause 0x1
	s_load_b256 s[4:11], s[0:1], 0x0
	s_load_b64 s[0:1], s[0:1], 0x20
	v_mov_b32_e32 v1, 0
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_gt_u64_e32 vcc_lo, s[8:9], v[0:1]
	v_lshlrev_b32_e32 v1, 2, v0
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB1_2
; %bb.1:
	s_clause 0x1
	global_load_b32 v2, v1, s[4:5]
	global_load_b32 v3, v1, s[6:7]
	s_waitcnt vmcnt(0)
	ds_store_2addr_stride64_b32 v1, v2, v3 offset1:8
.LBB1_2:
	s_or_b32 exec_lo, exec_lo, s2
	v_alignbit_b32 v2, s9, s8, 1
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_cmp_gt_i32_e32 vcc_lo, 1, v2
	v_readfirstlane_b32 s2, v2
	s_cbranch_vccnz .LBB1_7
; %bb.3:                                ; %.lr.ph
	v_add_nc_u32_e32 v2, 0x800, v1
.LBB1_4:                                ; =>This Inner Loop Header: Depth=1
	s_mov_b32 s3, exec_lo
	s_delay_alu instid0(VALU_DEP_2)
	v_cmpx_gt_i32_e64 s2, v0
	s_cbranch_execz .LBB1_6
; %bb.5:                                ;   in Loop: Header=BB1_4 Depth=1
	s_lshl_b32 s4, s2, 2
	s_delay_alu instid0(SALU_CYCLE_1)
	v_add_nc_u32_e32 v3, s4, v2
	v_add_nc_u32_e32 v4, s4, v1
	ds_load_b32 v5, v1
	ds_load_b32 v3, v3
	ds_load_b32 v4, v4
	ds_load_b32 v6, v2
	s_waitcnt lgkmcnt(0)
	v_dual_add_f32 v4, v5, v4 :: v_dual_add_f32 v3, v6, v3
	ds_store_b32 v1, v4
	ds_store_b32 v2, v3
.LBB1_6:                                ;   in Loop: Header=BB1_4 Depth=1
	s_or_b32 exec_lo, exec_lo, s3
	s_lshr_b32 s3, s2, 1
	s_cmp_gt_u32 s2, 1
	s_mov_b32 s2, s3
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB1_4
.LBB1_7:                                ; %._crit_edge
	s_mov_b32 s2, exec_lo
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB1_9
; %bb.8:
	v_mov_b32_e32 v2, 0
	ds_load_2addr_stride64_b32 v[0:1], v2 offset1:8
	s_waitcnt lgkmcnt(0)
	s_clause 0x1
	global_store_b32 v2, v0, s[10:11]
	global_store_b32 v2, v1, s[0:1]
.LBB1_9:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z17calculateFinalSumPfS_mS_S_
		.amdhsa_group_segment_fixed_size 4096
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 40
		.amdhsa_user_sgpr_count 15
		.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 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 7
		.amdhsa_next_free_sgpr 12
		.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_end1:
	.size	_Z17calculateFinalSumPfS_mS_S_, .Lfunc_end1-_Z17calculateFinalSumPfS_mS_S_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 320
; NumSgprs: 14
; NumVgprs: 7
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 4096 bytes/workgroup (compile time only)
; SGPRBlocks: 1
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 14
; NumVGPRsForWavesPerEU: 7
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.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:           8
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 4096
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z17calculateBlockSumPKfPfS1_m
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z17calculateBlockSumPKfPfS1_m.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     7
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           8
        .value_kind:     by_value
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         32
        .size:           8
        .value_kind:     global_buffer
    .group_segment_fixed_size: 4096
    .kernarg_segment_align: 8
    .kernarg_segment_size: 40
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z17calculateFinalSumPfS_mS_S_
    .private_segment_fixed_size: 0
    .sgpr_count:     14
    .sgpr_spill_count: 0
    .symbol:         _Z17calculateFinalSumPfS_mS_S_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     7
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
