#include <stdio.h>
#include <time.h>

void onCPU(float* A, float* B, float* xNow, float* xNext, int Ni) {
    int i,j;
    float sum;

    for (i=0; i<Ni; i++) {
        sum = 0.0;
        for (j=0; j<Ni; j++) {
            if (i != j) {
                sum += A[i*Ni + j] * xNow[j];
            }
        }
        xNext[i] = (B[i] - sum) / A[i*Ni + i];
    }
}

__global__ void onGPU(float* A, float* B,  float* xNow, float* xNext, int Ni) {
    int idx = blockIdx.x*blockDim.x + threadIdx.x;
    if (idx < Ni) {
        float sum = 0.0;
        int idx_Ai = idx*Ni;   
        for (int j=0; j<Ni; j++) {
            if (idx != j) {
                sum += A[idx_Ai + j] * xNow[j];
            }
        }
        xNext[idx] = (B[idx] - sum) / A[idx_Ai + idx];
    }
}

int main( int argc, char *argv[] ) { 
    time_t start_h, end_h, start_d, end_d;
    float timeCPU, timeGPU;
    float *xNow, *xNext, *A, *B, *xCPU, *xGPU;
    float *xNowDevice, *xNextDevice, *deviceA, *deviceB;
    int N, Ni, iter, tileSize, i , k;   

    Ni=2048 , iter=10;
    N = Ni * Ni;

    if( argc == 2 ) {
        tileSize = atoi(argv[1]);
    }
    else if( argc > 2 ) {
        printf("Too many arguments supplied.\n");
        exit(0);
    }
    else {
        printf("Usage: ./jacobi argument(int).\n");
        exit(0);
    }
    
    printf("Jacobi method:\n\n");
    printf("N = %d, Ni = %d, ", N, Ni);
    printf("thread = %d\n", tileSize);

    A = (float *) malloc(N*sizeof(float));
    B = (float *) malloc(Ni*sizeof(float));
    xNext = (float *) malloc(Ni*sizeof(float));
    xNow = (float *) malloc(Ni*sizeof(float));
    xCPU = (float *) malloc(Ni*sizeof(float));
    xGPU = (float *) malloc(Ni*sizeof(float));

    for (i=0; i<Ni; i++) {
        xNow[i] = 0;
        xNext[i] = 0;
    }

    for (i = 0; i < N; i ++){
        A[i] = rand()/(float)RAND_MAX;
    }
    for (i = 0; i < Ni; i++){
        B[i] = rand()/(float)RAND_MAX;
    } 

    /*
    A[0] = 4;
    A[1] = 0.24;
    A[2] = -0.08;
    A[3] = 0.09;
    A[4] = 3;
    A[5] = -0.15;
    A[6] = 0.04;
    A[7] = -0.08;
    A[8] = 4; 

    B[0] = 8;
    B[1] = 9;
    B[2] = 20;
    */

    //============================================= CPU =============================================//
    start_h = clock();

    for (k=0; k<iter; k++) {
        if (k%2) {
            onCPU( A, B, xNow, xNext, Ni);
        }
        else {
            onCPU( A, B, xNext, xNow, Ni);
        }
    }
    
    end_h = clock();

    if (iter%2 != 0) {
        for (i=0; i<Ni; i++) {
            xCPU[i] = xNext[i];
        }
    }
    else {
        for (i=0; i<Ni; i++) {
            xCPU[i] = xNow[i];
        }
    }

    for (i=0; i<Ni; i++) {
        xNow[i] = 0;
        xNext[i] = 0;
    }

    cudaMalloc((void **) &deviceA, N*sizeof(float));
    cudaMalloc((void **) &deviceB, Ni*sizeof(float));
    cudaMalloc((void **) &xNowDevice, Ni*sizeof(float));
    cudaMalloc((void **) &xNextDevice, Ni*sizeof(float));

    cudaMemcpy(deviceA, A, sizeof(float)*N, cudaMemcpyHostToDevice);
    cudaMemcpy(deviceB, B, sizeof(float)*Ni, cudaMemcpyHostToDevice);
    cudaMemcpy(xNowDevice, xNow, sizeof(float)*Ni, cudaMemcpyHostToDevice);
    cudaMemcpy(xNextDevice, xNext, sizeof(float)*Ni, cudaMemcpyHostToDevice);
   
    int nTiles = Ni/tileSize + (Ni%tileSize == 0?0:1);
    int gridHeight = Ni/tileSize + (Ni%tileSize == 0?0:1);
    int gridWidth = Ni/tileSize + (Ni%tileSize == 0?0:1);
    printf("w=%d, h=%d\n",gridWidth,gridHeight);
    dim3 dGrid(gridHeight, gridWidth), dBlock(tileSize, tileSize);

    //============================================= GPU =============================================//
    start_d = clock();
     
    for (k=0; k<iter; k++) {
        if (k%2) {
            onGPU <<< nTiles, tileSize >>> (deviceA, deviceB, xNowDevice, xNextDevice, Ni);
        }
        else {
            onGPU <<< nTiles, tileSize >>> (deviceA, deviceB, xNextDevice, xNowDevice, Ni);
        }
    }
        
    end_d = clock();

    if (iter%2 != 0 ){
        cudaMemcpy(xGPU, xNextDevice, sizeof(float)*Ni, cudaMemcpyDeviceToHost);
    }
    else {
        cudaMemcpy(xGPU, xNowDevice, sizeof(float)*Ni, cudaMemcpyDeviceToHost);
    }

    free(xNext); free(A); free(xNow); free(B);
    cudaFree(xNextDevice); cudaFree(deviceA); cudaFree(xNowDevice); cudaFree(deviceB);

    /*
    for (i =0 ; i < Ni; i ++) {
        //printf("xCPU[%d]=%f\n",i,xCPU[i]);
        //printf("xGPU[%d]=%f\n",i,xGPU[i]);
    }
    */
    
    timeCPU = ((float)end_h - (float)start_h) / CLOCKS_PER_SEC;
    timeGPU = ((float)end_d - (float)start_d) / CLOCKS_PER_SEC;
    printf("\nTiming:\nCPU: %f\nGPU: %f\n\n", timeCPU, timeGPU);

    return 0;
}
	.file	"tmpxft_0022b634_00000000-6_jacobi.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2031:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2031:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z5onCPUPfS_S_S_i
	.type	_Z5onCPUPfS_S_S_i, @function
_Z5onCPUPfS_S_S_i:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movslq	%r8d, %rax
	movq	%rsi, %r9
	movq	%rcx, %r11
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rdx, %r10
	leaq	4(,%rax,4), %rbp
	movq	%rdi, %rcx
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	xorl	%esi, %esi
	xorl	%eax, %eax
.L3:
	movl	%eax, %r12d
	cmpl	%eax, %r8d
	jle	.L10
	xorl	%edx, %edx
	xorps	%xmm1, %xmm1
.L5:
	cmpl	%edx, %r12d
	je	.L4
	leal	(%rsi,%rdx), %ebx
	movslq	%ebx, %rbx
	movss	(%rdi,%rbx,4), %xmm0
	mulss	(%r10,%rdx,4), %xmm0
	addss	%xmm0, %xmm1
.L4:
	incq	%rdx
	cmpl	%edx, %r8d
	jg	.L5
	movss	(%r9,%rax,4), %xmm0
	addl	%r8d, %esi
	subss	%xmm1, %xmm0
	divss	(%rcx), %xmm0
	addq	%rbp, %rcx
	movss	%xmm0, (%r11,%rax,4)
	incq	%rax
	jmp	.L3
.L10:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z5onCPUPfS_S_S_i, .-_Z5onCPUPfS_S_S_i
	.globl	_Z31__device_stub__Z5onGPUPfS_S_S_iPfS_S_S_i
	.type	_Z31__device_stub__Z5onGPUPfS_S_S_iPfS_S_S_i, @function
_Z31__device_stub__Z5onGPUPfS_S_S_iPfS_S_S_i:
.LFB2053:
	.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
	movl	%r8d, 12(%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	12(%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	.L11
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z5onGPUPfS_S_S_i(%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
.L11:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2053:
	.size	_Z31__device_stub__Z5onGPUPfS_S_S_iPfS_S_S_i, .-_Z31__device_stub__Z5onGPUPfS_S_S_iPfS_S_S_i
	.globl	_Z5onGPUPfS_S_S_i
	.type	_Z5onGPUPfS_S_S_i, @function
_Z5onGPUPfS_S_S_i:
.LFB2054:
	.cfi_startproc
	endbr64
	jmp	_Z31__device_stub__Z5onGPUPfS_S_S_iPfS_S_S_i
	.cfi_endproc
.LFE2054:
	.size	_Z5onGPUPfS_S_S_i, .-_Z5onGPUPfS_S_S_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"Jacobi method:\n\n"
.LC2:
	.string	"N = %d, Ni = %d, "
.LC3:
	.string	"thread = %d\n"
.LC4:
	.string	"Too many arguments supplied.\n"
.LC5:
	.string	"Usage: ./jacobi argument(int).\n"
.LC7:
	.string	"w=%d, h=%d\n"
.LC9:
	.string	"\nTiming:\nCPU: %f\nGPU: %f\n\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	cmpl	$2, %edi
	jne	.L17
	movq	8(%rsi), %rdi
	xorl	%r14d, %r14d
	call	atoi@PLT
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	movl	%eax, %r13d
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$2048, %ecx
	movl	$4194304, %edx
	xorl	%eax, %eax
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	%r13d, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC3(%rip), %rsi
	call	__printf_chk@PLT
	movl	$16777216, %edi
	call	malloc@PLT
	movl	$8192, %edi
	movq	%rax, %r12
	call	malloc@PLT
	movl	$8192, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movl	$8192, %edi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	movl	$8192, %edi
	movq	%rax, (%rsp)
	call	malloc@PLT
	movl	$8192, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	movq	(%rsp), %rdi
	movl	$2048, %ecx
	movq	%rax, 16(%rsp)
	xorl	%eax, %eax
	rep stosl
	movq	8(%rsp), %rdi
	movl	$2048, %ecx
	rep stosl
	jmp	.L18
.L17:
	leaq	.LC4(%rip), %rsi
	jg	.L37
	leaq	.LC5(%rip), %rsi
.L37:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L18:
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC6(%rip), %xmm0
	movss	%xmm0, (%r12,%r14,4)
	incq	%r14
	cmpq	$4194304, %r14
	jne	.L18
	xorl	%r14d, %r14d
.L20:
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC6(%rip), %xmm0
	movss	%xmm0, 0(%rbp,%r14,4)
	incq	%r14
	cmpq	$2048, %r14
	jne	.L20
	call	clock@PLT
	xorl	%r14d, %r14d
	movq	%rax, 24(%rsp)
.L23:
	movl	$2048, %r8d
	testl	$1, %r14d
	je	.L21
	movq	8(%rsp), %rcx
	movq	(%rsp), %rdx
	jmp	.L35
.L21:
	movq	(%rsp), %rcx
	movq	8(%rsp), %rdx
.L35:
	movq	%rbp, %rsi
	movq	%r12, %rdi
	incl	%r14d
	call	_Z5onCPUPfS_S_S_i
	cmpl	$10, %r14d
	jne	.L23
	call	clock@PLT
	movq	(%rsp), %rsi
	movq	%rbx, %rdi
	movl	$2048, %ecx
	rep movsl
	movq	(%rsp), %rdi
	movq	%rax, 32(%rsp)
	movl	$2048, %ecx
	xorl	%eax, %eax
	movl	$16777216, %esi
	movabsq	$4294967297, %r15
	rep stosl
	movq	8(%rsp), %rdi
	movl	$2048, %ecx
	rep stosl
	leaq	64(%rsp), %rdi
	call	cudaMalloc@PLT
	leaq	72(%rsp), %rdi
	movl	$8192, %esi
	call	cudaMalloc@PLT
	leaq	48(%rsp), %rdi
	movl	$8192, %esi
	call	cudaMalloc@PLT
	leaq	56(%rsp), %rdi
	movl	$8192, %esi
	call	cudaMalloc@PLT
	movq	64(%rsp), %rdi
	movl	$1, %ecx
	movq	%r12, %rsi
	movl	$16777216, %edx
	call	cudaMemcpy@PLT
	movq	72(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	movl	$8192, %edx
	call	cudaMemcpy@PLT
	movq	(%rsp), %rsi
	movq	48(%rsp), %rdi
	movl	$1, %ecx
	movl	$8192, %edx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rsi
	movq	56(%rsp), %rdi
	movl	$1, %ecx
	movl	$8192, %edx
	call	cudaMemcpy@PLT
	movl	$2048, %eax
	movl	$2, %edi
	leaq	.LC7(%rip), %rsi
	cltd
	idivl	%r13d
	cmpl	$1, %edx
	sbbl	$-1, %eax
	xorl	%r14d, %r14d
	movl	%eax, %ecx
	movl	%eax, %edx
	movl	%eax, %ebx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	call	clock@PLT
	movq	%rax, 40(%rsp)
.L28:
	movq	%r15, 96(%rsp)
	xorl	%r9d, %r9d
	movl	100(%rsp), %ecx
	xorl	%r8d, %r8d
	movq	%r15, 84(%rsp)
	movl	88(%rsp), %esi
	movl	%r13d, 92(%rsp)
	movq	92(%rsp), %rdx
	movl	%ebx, 80(%rsp)
	movq	80(%rsp), %rdi
	testl	$1, %r14d
	je	.L24
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L26
	movq	56(%rsp), %rcx
	movq	48(%rsp), %rdx
	movl	$2048, %r8d
	jmp	.L36
.L24:
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L26
	movq	48(%rsp), %rcx
	movq	56(%rsp), %rdx
	movl	$2048, %r8d
.L36:
	movq	72(%rsp), %rsi
	movq	64(%rsp), %rdi
	call	_Z31__device_stub__Z5onGPUPfS_S_S_iPfS_S_S_i
.L26:
	incl	%r14d
	cmpl	$10, %r14d
	jne	.L28
	call	clock@PLT
	movq	48(%rsp), %rsi
	movq	16(%rsp), %rdi
	movl	$2, %ecx
	movl	$8192, %edx
	movq	%rax, %rbx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	(%rsp), %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
	cvtsi2ssq	40(%rsp), %xmm0
	movb	$2, %al
	movss	.LC8(%rip), %xmm2
	cvtsi2ssq	%rbx, %xmm1
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	cvtsi2ssq	24(%rsp), %xmm3
	subss	%xmm0, %xmm1
	cvtsi2ssq	32(%rsp), %xmm0
	divss	%xmm2, %xmm1
	subss	%xmm3, %xmm0
	divss	%xmm2, %xmm0
	cvtss2sd	%xmm1, %xmm1
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L29
	call	__stack_chk_fail@PLT
.L29:
	addq	$120, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	main, .-main
	.section	.rodata.str1.1
.LC10:
	.string	"_Z5onGPUPfS_S_S_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2056:
	.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	.LC10(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z5onGPUPfS_S_S_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2056:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC6:
	.long	805306368
	.align 4
.LC8:
	.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:


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <time.h>

void onCPU(float* A, float* B, float* xNow, float* xNext, int Ni) {
    int i,j;
    float sum;

    for (i=0; i<Ni; i++) {
        sum = 0.0;
        for (j=0; j<Ni; j++) {
            if (i != j) {
                sum += A[i*Ni + j] * xNow[j];
            }
        }
        xNext[i] = (B[i] - sum) / A[i*Ni + i];
    }
}

__global__ void onGPU(float* A, float* B,  float* xNow, float* xNext, int Ni) {
    int idx = blockIdx.x*blockDim.x + threadIdx.x;
    if (idx < Ni) {
        float sum = 0.0;
        int idx_Ai = idx*Ni;   
        for (int j=0; j<Ni; j++) {
            if (idx != j) {
                sum += A[idx_Ai + j] * xNow[j];
            }
        }
        xNext[idx] = (B[idx] - sum) / A[idx_Ai + idx];
    }
}

int main( int argc, char *argv[] ) { 
    time_t start_h, end_h, start_d, end_d;
    float timeCPU, timeGPU;
    float *xNow, *xNext, *A, *B, *xCPU, *xGPU;
    float *xNowDevice, *xNextDevice, *deviceA, *deviceB;
    int N, Ni, iter, tileSize, i , k;   

    Ni=2048 , iter=10;
    N = Ni * Ni;

    if( argc == 2 ) {
        tileSize = atoi(argv[1]);
    }
    else if( argc > 2 ) {
        printf("Too many arguments supplied.\n");
        exit(0);
    }
    else {
        printf("Usage: ./jacobi argument(int).\n");
        exit(0);
    }
    
    printf("Jacobi method:\n\n");
    printf("N = %d, Ni = %d, ", N, Ni);
    printf("thread = %d\n", tileSize);

    A = (float *) malloc(N*sizeof(float));
    B = (float *) malloc(Ni*sizeof(float));
    xNext = (float *) malloc(Ni*sizeof(float));
    xNow = (float *) malloc(Ni*sizeof(float));
    xCPU = (float *) malloc(Ni*sizeof(float));
    xGPU = (float *) malloc(Ni*sizeof(float));

    for (i=0; i<Ni; i++) {
        xNow[i] = 0;
        xNext[i] = 0;
    }

    for (i = 0; i < N; i ++){
        A[i] = rand()/(float)RAND_MAX;
    }
    for (i = 0; i < Ni; i++){
        B[i] = rand()/(float)RAND_MAX;
    } 

    /*
    A[0] = 4;
    A[1] = 0.24;
    A[2] = -0.08;
    A[3] = 0.09;
    A[4] = 3;
    A[5] = -0.15;
    A[6] = 0.04;
    A[7] = -0.08;
    A[8] = 4; 

    B[0] = 8;
    B[1] = 9;
    B[2] = 20;
    */

    //============================================= CPU =============================================//
    start_h = clock();

    for (k=0; k<iter; k++) {
        if (k%2) {
            onCPU( A, B, xNow, xNext, Ni);
        }
        else {
            onCPU( A, B, xNext, xNow, Ni);
        }
    }
    
    end_h = clock();

    if (iter%2 != 0) {
        for (i=0; i<Ni; i++) {
            xCPU[i] = xNext[i];
        }
    }
    else {
        for (i=0; i<Ni; i++) {
            xCPU[i] = xNow[i];
        }
    }

    for (i=0; i<Ni; i++) {
        xNow[i] = 0;
        xNext[i] = 0;
    }

    hipMalloc((void **) &deviceA, N*sizeof(float));
    hipMalloc((void **) &deviceB, Ni*sizeof(float));
    hipMalloc((void **) &xNowDevice, Ni*sizeof(float));
    hipMalloc((void **) &xNextDevice, Ni*sizeof(float));

    hipMemcpy(deviceA, A, sizeof(float)*N, hipMemcpyHostToDevice);
    hipMemcpy(deviceB, B, sizeof(float)*Ni, hipMemcpyHostToDevice);
    hipMemcpy(xNowDevice, xNow, sizeof(float)*Ni, hipMemcpyHostToDevice);
    hipMemcpy(xNextDevice, xNext, sizeof(float)*Ni, hipMemcpyHostToDevice);
   
    int nTiles = Ni/tileSize + (Ni%tileSize == 0?0:1);
    int gridHeight = Ni/tileSize + (Ni%tileSize == 0?0:1);
    int gridWidth = Ni/tileSize + (Ni%tileSize == 0?0:1);
    printf("w=%d, h=%d\n",gridWidth,gridHeight);
    dim3 dGrid(gridHeight, gridWidth), dBlock(tileSize, tileSize);

    //============================================= GPU =============================================//
    start_d = clock();
     
    for (k=0; k<iter; k++) {
        if (k%2) {
            onGPU <<< nTiles, tileSize >>> (deviceA, deviceB, xNowDevice, xNextDevice, Ni);
        }
        else {
            onGPU <<< nTiles, tileSize >>> (deviceA, deviceB, xNextDevice, xNowDevice, Ni);
        }
    }
        
    end_d = clock();

    if (iter%2 != 0 ){
        hipMemcpy(xGPU, xNextDevice, sizeof(float)*Ni, hipMemcpyDeviceToHost);
    }
    else {
        hipMemcpy(xGPU, xNowDevice, sizeof(float)*Ni, hipMemcpyDeviceToHost);
    }

    free(xNext); free(A); free(xNow); free(B);
    hipFree(xNextDevice); hipFree(deviceA); hipFree(xNowDevice); hipFree(deviceB);

    /*
    for (i =0 ; i < Ni; i ++) {
        //printf("xCPU[%d]=%f\n",i,xCPU[i]);
        //printf("xGPU[%d]=%f\n",i,xGPU[i]);
    }
    */
    
    timeCPU = ((float)end_h - (float)start_h) / CLOCKS_PER_SEC;
    timeGPU = ((float)end_d - (float)start_d) / CLOCKS_PER_SEC;
    printf("\nTiming:\nCPU: %f\nGPU: %f\n\n", timeCPU, timeGPU);

    return 0;
}
	.text
	.file	"jacobi.hip"
	.globl	_Z5onCPUPfS_S_S_i               # -- Begin function _Z5onCPUPfS_S_S_i
	.type	_Z5onCPUPfS_S_S_i,@function
_Z5onCPUPfS_S_S_i:                      # @_Z5onCPUPfS_S_S_i
	.cfi_startproc
# %bb.0:
                                        # kill: def $r8d killed $r8d def $r8
	testl	%r8d, %r8d
	jle	.LBB0_8
# %bb.1:                                # %.preheader.lr.ph
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	leal	1(%r8), %eax
	movl	%r8d, %r9d
	xorl	%r10d, %r10d
	xorl	%r11d, %r11d
.LBB0_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_3 Depth 2
	movl	%r10d, %ebx
	leaq	(%rdi,%rbx,4), %rbx
	xorps	%xmm0, %xmm0
	xorl	%r14d, %r14d
.LBB0_3:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpq	%r14, %r11
	je	.LBB0_5
# %bb.4:                                #   in Loop: Header=BB0_3 Depth=2
	movss	(%rbx,%r14,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	mulss	(%rdx,%r14,4), %xmm1
	addss	%xmm1, %xmm0
.LBB0_5:                                #   in Loop: Header=BB0_3 Depth=2
	incq	%r14
	cmpq	%r14, %r9
	jne	.LBB0_3
# %bb.6:                                # %._crit_edge
                                        #   in Loop: Header=BB0_2 Depth=1
	movss	(%rsi,%r11,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	subss	%xmm0, %xmm1
	movl	%eax, %ebx
	imull	%r11d, %ebx
	movslq	%ebx, %rbx
	divss	(%rdi,%rbx,4), %xmm1
	movss	%xmm1, (%rcx,%r11,4)
	incq	%r11
	addl	%r8d, %r10d
	cmpq	%r9, %r11
	jne	.LBB0_2
# %bb.7:
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
.LBB0_8:                                # %._crit_edge30
	retq
.Lfunc_end0:
	.size	_Z5onCPUPfS_S_S_i, .Lfunc_end0-_Z5onCPUPfS_S_S_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z20__device_stub__onGPUPfS_S_S_i # -- Begin function _Z20__device_stub__onGPUPfS_S_S_i
	.type	_Z20__device_stub__onGPUPfS_S_S_i,@function
_Z20__device_stub__onGPUPfS_S_S_i:      # @_Z20__device_stub__onGPUPfS_S_S_i
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$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	12(%rsp), %rcx
	movl	%r8d, (%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	24(%rsp), %r12
	leaq	16(%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	$_Z5onGPUPfS_S_S_i, %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_end1:
	.size	_Z20__device_stub__onGPUPfS_S_S_i, .Lfunc_end1-_Z20__device_stub__onGPUPfS_S_S_i
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI2_0:
	.long	0x30000000                      # float 4.65661287E-10
.LCPI2_1:
	.long	0x49742400                      # float 1.0E+6
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$2, %edi
	jne	.LBB2_12
# %bb.1:                                # %.preheader166.preheader
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %ebx
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	xorl	%r13d, %r13d
	movl	$.L.str.3, %edi
	movl	$4194304, %esi                  # imm = 0x400000
	movl	$2048, %edx                     # imm = 0x800
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.4, %edi
	movl	%ebx, 20(%rsp)                  # 4-byte Spill
	movl	%ebx, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$16777216, %edi                 # imm = 0x1000000
	callq	malloc
	movq	%rax, %r14
	movl	$8192, %edi                     # imm = 0x2000
	callq	malloc
	movq	%rax, %rbx
	movl	$1, %edi
	movl	$8192, %esi                     # imm = 0x2000
	callq	calloc@PLT
	movq	%rax, %r12
	movl	$1, %edi
	movl	$8192, %esi                     # imm = 0x2000
	callq	calloc@PLT
	movq	%rax, %r15
	movl	$8192, %edi                     # imm = 0x2000
	callq	malloc
	movq	%rax, 72(%rsp)                  # 8-byte Spill
.LBB2_2:                                # %.preheader166
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	movss	.LCPI2_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	%xmm1, %xmm0
	movss	%xmm0, (%r14,%r13,4)
	incq	%r13
	cmpq	$4194304, %r13                  # imm = 0x400000
	jne	.LBB2_2
# %bb.3:                                # %.preheader165.preheader
	xorl	%r13d, %r13d
.LBB2_4:                                # %.preheader165
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI2_0(%rip), %xmm0
	movss	%xmm0, (%rbx,%r13,4)
	incq	%r13
	cmpq	$2048, %r13                     # imm = 0x800
	jne	.LBB2_4
# %bb.5:
	xorl	%r13d, %r13d
	callq	clock
	movq	%rax, 64(%rsp)                  # 8-byte Spill
.LBB2_6:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_7 Depth 2
                                        #       Child Loop BB2_8 Depth 3
                                        #     Child Loop BB2_16 Depth 2
                                        #       Child Loop BB2_17 Depth 3
	movq	%r14, %rax
	xorl	%ecx, %ecx
	testb	$1, %r13b
	jne	.LBB2_7
.LBB2_16:                               # %.lr.ph.i135
                                        #   Parent Loop BB2_6 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB2_17 Depth 3
	xorps	%xmm0, %xmm0
	xorl	%edx, %edx
.LBB2_17:                               #   Parent Loop BB2_6 Depth=1
                                        #     Parent Loop BB2_16 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	cmpq	%rdx, %rcx
	je	.LBB2_19
# %bb.18:                               #   in Loop: Header=BB2_17 Depth=3
	movss	(%rax,%rdx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	mulss	(%r12,%rdx,4), %xmm1
	addss	%xmm1, %xmm0
.LBB2_19:                               #   in Loop: Header=BB2_17 Depth=3
	incq	%rdx
	cmpq	$2048, %rdx                     # imm = 0x800
	jne	.LBB2_17
# %bb.20:                               # %._crit_edge.i145
                                        #   in Loop: Header=BB2_16 Depth=2
	movss	(%rbx,%rcx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	subss	%xmm0, %xmm1
	movq	%rcx, %rdx
	shlq	$13, %rdx
	leaq	(%rdx,%rcx,4), %rdx
	divss	(%r14,%rdx), %xmm1
	movss	%xmm1, (%r15,%rcx,4)
	incq	%rcx
	addq	$8192, %rax                     # imm = 0x2000
	cmpq	$2048, %rcx                     # imm = 0x800
	jne	.LBB2_16
	jmp	.LBB2_21
.LBB2_7:                                # %.lr.ph.i
                                        #   Parent Loop BB2_6 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB2_8 Depth 3
	xorps	%xmm0, %xmm0
	xorl	%edx, %edx
.LBB2_8:                                #   Parent Loop BB2_6 Depth=1
                                        #     Parent Loop BB2_7 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	cmpq	%rdx, %rcx
	je	.LBB2_10
# %bb.9:                                #   in Loop: Header=BB2_8 Depth=3
	movss	(%rax,%rdx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	mulss	(%r15,%rdx,4), %xmm1
	addss	%xmm1, %xmm0
.LBB2_10:                               #   in Loop: Header=BB2_8 Depth=3
	incq	%rdx
	cmpq	$2048, %rdx                     # imm = 0x800
	jne	.LBB2_8
# %bb.11:                               # %._crit_edge.i
                                        #   in Loop: Header=BB2_7 Depth=2
	movss	(%rbx,%rcx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	subss	%xmm0, %xmm1
	movq	%rcx, %rdx
	shlq	$13, %rdx
	leaq	(%rdx,%rcx,4), %rdx
	divss	(%r14,%rdx), %xmm1
	movss	%xmm1, (%r12,%rcx,4)
	incq	%rcx
	addq	$8192, %rax                     # imm = 0x2000
	cmpq	$2048, %rcx                     # imm = 0x800
	jne	.LBB2_7
.LBB2_21:                               # %_Z5onCPUPfS_S_S_i.exit
                                        #   in Loop: Header=BB2_6 Depth=1
	incl	%r13d
	cmpl	$10, %r13d
	jne	.LBB2_6
# %bb.22:                               # %.preheader.preheader
	callq	clock
	movq	%rax, 56(%rsp)                  # 8-byte Spill
	xorl	%r13d, %r13d
	movl	$8192, %edx                     # imm = 0x2000
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
	movl	$8192, %edx                     # imm = 0x2000
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
	leaq	40(%rsp), %rbp
	movl	$16777216, %esi                 # imm = 0x1000000
	movq	%rbp, %rdi
	callq	hipMalloc
	leaq	24(%rsp), %rdi
	movl	$8192, %esi                     # imm = 0x2000
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movl	$8192, %esi                     # imm = 0x2000
	callq	hipMalloc
	leaq	32(%rsp), %rdi
	movl	$8192, %esi                     # imm = 0x2000
	callq	hipMalloc
	movq	(%rbp), %rdi
	movl	$16777216, %edx                 # imm = 0x1000000
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	24(%rsp), %rax
	movq	(%rax), %rdi
	movl	$8192, %edx                     # imm = 0x2000
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	8(%rsp), %rax
	movq	(%rax), %rdi
	movl	$8192, %edx                     # imm = 0x2000
	movq	%r15, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	32(%rsp), %rax
	movq	(%rax), %rdi
	movl	$8192, %edx                     # imm = 0x2000
	movq	%r12, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$2048, %eax                     # imm = 0x800
	xorl	%edx, %edx
	idivl	20(%rsp)                        # 4-byte Folded Reload
	movl	%eax, %ebp
	cmpl	$1, %edx
	sbbl	$-1, %ebp
	movl	$.L.str.5, %edi
	movl	%ebp, %esi
	movl	%ebp, %edx
	xorl	%eax, %eax
	callq	printf
	callq	clock
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	btsq	$32, %rbp
	movq	%rbp, 80(%rsp)                  # 8-byte Spill
	movl	20(%rsp), %ebp                  # 4-byte Reload
	btsq	$32, %rbp
.LBB2_23:                               # =>This Inner Loop Header: Depth=1
	movq	80(%rsp), %rdi                  # 8-byte Reload
	movl	$1, %esi
	movq	%rbp, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testb	$1, %r13b
	jne	.LBB2_24
# %bb.26:                               #   in Loop: Header=BB2_23 Depth=1
	testl	%eax, %eax
	jne	.LBB2_29
# %bb.27:                               #   in Loop: Header=BB2_23 Depth=1
	movq	40(%rsp), %rdi
	movq	24(%rsp), %rsi
	movq	32(%rsp), %rdx
	movq	8(%rsp), %rcx
	jmp	.LBB2_28
.LBB2_24:                               #   in Loop: Header=BB2_23 Depth=1
	testl	%eax, %eax
	jne	.LBB2_29
# %bb.25:                               #   in Loop: Header=BB2_23 Depth=1
	movq	40(%rsp), %rdi
	movq	24(%rsp), %rsi
	movq	8(%rsp), %rdx
	movq	32(%rsp), %rcx
.LBB2_28:                               #   in Loop: Header=BB2_23 Depth=1
	movl	$2048, %r8d                     # imm = 0x800
	callq	_Z20__device_stub__onGPUPfS_S_S_i
.LBB2_29:                               #   in Loop: Header=BB2_23 Depth=1
	incl	%r13d
	cmpl	$10, %r13d
	jne	.LBB2_23
# %bb.30:
	callq	clock
	movq	%rax, %r13
	movq	8(%rsp), %rsi
	movl	$8192, %edx                     # imm = 0x2000
	movq	72(%rsp), %rdi                  # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%r12, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	%rbx, %rdi
	callq	free
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	cvtsi2ssq	56(%rsp), %xmm0         # 8-byte Folded Reload
	cvtsi2ssq	64(%rsp), %xmm1         # 8-byte Folded Reload
	subss	%xmm1, %xmm0
	movss	.LCPI2_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	cvtsi2ss	%r13, %xmm2
	divss	%xmm1, %xmm0
	cvtsi2ssq	48(%rsp), %xmm3         # 8-byte Folded Reload
	subss	%xmm3, %xmm2
	divss	%xmm1, %xmm2
	cvtss2sd	%xmm0, %xmm0
	xorps	%xmm1, %xmm1
	cvtss2sd	%xmm2, %xmm1
	movl	$.L.str.6, %edi
	movb	$2, %al
	callq	printf
	xorl	%eax, %eax
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB2_12:
	.cfi_def_cfa_offset 144
	jle	.LBB2_15
# %bb.13:
	movl	$.Lstr.1, %edi
	jmp	.LBB2_14
.LBB2_15:
	movl	$.Lstr, %edi
.LBB2_14:
	callq	puts@PLT
	xorl	%edi, %edi
	callq	exit
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB3_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB3_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z5onGPUPfS_S_S_i, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end3:
	.size	__hip_module_ctor, .Lfunc_end3-__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	.LBB4_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
.LBB4_2:
	retq
.Lfunc_end4:
	.size	__hip_module_dtor, .Lfunc_end4-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z5onGPUPfS_S_S_i,@object       # @_Z5onGPUPfS_S_S_i
	.section	.rodata,"a",@progbits
	.globl	_Z5onGPUPfS_S_S_i
	.p2align	3, 0x0
_Z5onGPUPfS_S_S_i:
	.quad	_Z20__device_stub__onGPUPfS_S_S_i
	.size	_Z5onGPUPfS_S_S_i, 8

	.type	.L.str.3,@object                # @.str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.3:
	.asciz	"N = %d, Ni = %d, "
	.size	.L.str.3, 18

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"thread = %d\n"
	.size	.L.str.4, 13

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"w=%d, h=%d\n"
	.size	.L.str.5, 12

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"\nTiming:\nCPU: %f\nGPU: %f\n\n"
	.size	.L.str.6, 27

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z5onGPUPfS_S_S_i"
	.size	.L__unnamed_1, 18

	.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	"Usage: ./jacobi argument(int)."
	.size	.Lstr, 31

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Too many arguments supplied."
	.size	.Lstr.1, 29

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Jacobi method:\n"
	.size	.Lstr.2, 16

	.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 _Z20__device_stub__onGPUPfS_S_S_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z5onGPUPfS_S_S_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z5onGPUPfS_S_S_i       ; -- Begin function _Z5onGPUPfS_S_S_i
	.globl	_Z5onGPUPfS_S_S_i
	.p2align	8
	.type	_Z5onGPUPfS_S_S_i,@function
_Z5onGPUPfS_S_S_i:                      ; @_Z5onGPUPfS_S_S_i
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x34
	s_load_b32 s8, s[0:1], 0x20
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_i32_e64 s8, v1
	s_cbranch_execz .LBB0_7
; %bb.1:
	s_load_b256 s[0:7], s[0:1], 0x0
	v_mul_lo_u32 v4, v1, s8
	v_mov_b32_e32 v0, 0
	s_cmp_lt_i32 s8, 1
	s_cbranch_scc1 .LBB0_6
; %bb.2:                                ; %.lr.ph.preheader
	s_delay_alu instid0(VALU_DEP_2)
	v_dual_mov_b32 v2, v4 :: v_dual_mov_b32 v5, v1
.LBB0_3:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_mov_b32 s9, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_ne_u32_e32 0, v5
	s_cbranch_execz .LBB0_5
; %bb.4:                                ;   in Loop: Header=BB0_3 Depth=1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v3, 31, v2
	s_waitcnt lgkmcnt(0)
	s_load_b32 s10, s[4:5], 0x0
	v_lshlrev_b64 v[6:7], 2, v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v6, vcc_lo, s0, v6
	v_add_co_ci_u32_e32 v7, vcc_lo, s1, v7, vcc_lo
	global_load_b32 v3, v[6:7], off
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_fmac_f32_e32 v0, s10, v3
.LBB0_5:                                ;   in Loop: Header=BB0_3 Depth=1
	s_or_b32 exec_lo, exec_lo, s9
	s_add_i32 s8, s8, -1
	v_add_nc_u32_e32 v5, -1, v5
	v_add_nc_u32_e32 v2, 1, v2
	s_waitcnt lgkmcnt(0)
	s_add_u32 s4, s4, 4
	s_addc_u32 s5, s5, 0
	s_cmp_eq_u32 s8, 0
	s_cbranch_scc0 .LBB0_3
.LBB0_6:                                ; %Flow49
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v3, v4, v1
	v_lshlrev_b64 v[1:2], 2, v[1:2]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v5, vcc_lo, s2, v1
	v_add_co_ci_u32_e32 v6, vcc_lo, s3, v2, vcc_lo
	global_load_b32 v5, v[5:6], off
	s_waitcnt vmcnt(0)
	v_sub_f32_e32 v0, v5, v0
	v_ashrrev_i32_e32 v4, 31, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[3:4], 2, v[3:4]
	v_add_co_u32 v3, vcc_lo, s0, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_4) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v4, vcc_lo, s1, v4, vcc_lo
	global_load_b32 v3, v[3:4], off
	s_waitcnt vmcnt(0)
	v_div_scale_f32 v4, null, v3, v3, v0
	v_div_scale_f32 v7, vcc_lo, v0, v3, v0
	v_rcp_f32_e32 v5, v4
	s_waitcnt_depctr 0xfff
	v_fma_f32 v6, -v4, v5, 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v5, v6, v5
	v_mul_f32_e32 v6, v7, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v8, -v4, v6, v7
	v_fmac_f32_e32 v6, v8, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v4, -v4, v6, v7
	v_div_fmas_f32 v4, v4, v5, v6
	s_delay_alu instid0(VALU_DEP_1)
	v_div_fixup_f32 v3, v4, v3, v0
	v_add_co_u32 v0, vcc_lo, s6, v1
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v2, vcc_lo
	global_store_b32 v[0:1], v3, off
.LBB0_7:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z5onGPUPfS_S_S_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 296
		.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 9
		.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	_Z5onGPUPfS_S_S_i, .Lfunc_end0-_Z5onGPUPfS_S_S_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 436
; NumSgprs: 18
; NumVgprs: 9
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 9
; 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
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           4
        .value_kind:     by_value
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         44
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         52
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         54
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         56
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         58
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         60
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         62
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         104
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 296
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z5onGPUPfS_S_S_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z5onGPUPfS_S_S_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     9
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
