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

double cpu_time();

#define M 500
#define N 500

void calculate_solution_gold(double w[M][N], double epsilon)
{
    double diff;
    double ctime;
    double ctime1;
    double ctime2;
    int i;
    int j;
    int iterations;
    int iterations_print;

    double u[M][N];
    diff = epsilon;

    //  iterate until the  new solution W differs from the old solution U
    //  by no more than EPSILON.

    iterations = 0;
    iterations_print = 1;
    printf("\n");
    printf(" Iteration  Change\n");
    printf("\n");

    ctime1 = cpu_time();

    while (epsilon <= diff)
    {
        //  Save the old solution in U.

        for (i = 0; i < M; i++)
            for (j = 0; j < N; j++)
                u[i][j] = w[i][j];

        //  Determine the new estimate of the solution at the interior points.
        //  The new solution W is the average of north, south, east and west neighbors.

        diff = 0.0;
        for (i = 1; i < M - 1; i++)
        {
            for (j = 1; j < N - 1; j++)
            {
                w[i][j] = (u[i - 1][j] + u[i + 1][j] + u[i][j - 1] + u[i][j + 1]) / 4.0;

                if (diff < fabs(w[i][j] - u[i][j]))
                    diff = fabs(w[i][j] - u[i][j]);
            }
        }

        iterations++;
        if (iterations == iterations_print)
        {
            printf("  %8d  %lg\n", iterations, diff);
            iterations_print = 2 * iterations_print;
        }
    } 

    ctime2 = cpu_time();
    ctime = ctime2 - ctime1;

    printf("\n");
    printf("  %8d  %lg\n", iterations, diff);
    printf("\n");
    printf("  Error tolerance achieved.\n");
    printf("  CPU time = %f\n", ctime);
}

double cpu_time() 
{
    double value;

    value = (double)clock() / (double)CLOCKS_PER_SEC;

    return value;
}

#undef M
#undef N
	.file	"tmpxft_00249398_00000000-6_calculate_solution_gold.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	_Z8cpu_timev
	.type	_Z8cpu_timev, @function
_Z8cpu_timev:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	clock@PLT
	cvtsi2sdq	%rax, %xmm0
	divsd	.LC0(%rip), %xmm0
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z8cpu_timev, .-_Z8cpu_timev
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC2:
	.string	"\n"
.LC3:
	.string	" Iteration  Change\n"
.LC6:
	.string	"  %8d  %lg\n"
.LC7:
	.string	"  Error tolerance achieved.\n"
.LC8:
	.string	"  CPU time = %f\n"
	.text
	.globl	_Z23calculate_solution_goldPA500_dd
	.type	_Z23calculate_solution_goldPA500_dd, @function
_Z23calculate_solution_goldPA500_dd:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	leaq	-1998848(%rsp), %r11
	.cfi_def_cfa 11, 1998888
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	.cfi_def_cfa_register 7
	subq	$1208, %rsp
	.cfi_def_cfa_offset 2000096
	leaq	.LC2(%rip), %rbp
	movl	$1, %r13d
	xorl	%r12d, %r12d
	movq	%rbp, %rsi
	movsd	%xmm0, 8(%rsp)
	movq	%rdi, %rbx
	movl	$2, %edi
	movq	%fs:40, %rax
	movq	%rax, 2000040(%rsp)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbp, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	call	_Z8cpu_timev
	movsd	8(%rsp), %xmm1
	movsd	%xmm0, 16(%rsp)
.L5:
	comisd	8(%rsp), %xmm1
	jb	.L18
	leaq	40(%rsp), %rdx
	movl	$500000, %ecx
	xorps	%xmm1, %xmm1
	movq	%rbx, %rsi
	movsd	.LC4(%rip), %xmm2
	movq	%rdx, %rdi
	rep movsl
	movl	$4000, %ecx
.L6:
	leaq	(%rbx,%rcx), %rdi
	leaq	(%rdx,%rcx), %rax
	movl	$1, %esi
.L8:
	movsd	-3992(%rax), %xmm0
	addsd	4008(%rax), %xmm0
	addq	$8, %rax
	addsd	-8(%rax), %xmm0
	addsd	8(%rax), %xmm0
	mulsd	%xmm2, %xmm0
	movsd	%xmm0, (%rdi,%rsi,8)
	subsd	(%rax), %xmm0
	andps	.LC5(%rip), %xmm0
	incq	%rsi
	maxsd	%xmm1, %xmm0
	movaps	%xmm0, %xmm1
	cmpq	$499, %rsi
	jne	.L8
	addq	$4000, %rcx
	cmpq	$1996000, %rcx
	jne	.L6
	incl	%r12d
	cmpl	%r12d, %r13d
	jne	.L5
	movl	%r13d, %edx
	leaq	.LC6(%rip), %rsi
	movb	$1, %al
	addl	%r13d, %r13d
	movl	$2, %edi
	movsd	%xmm0, 24(%rsp)
	call	__printf_chk@PLT
	movsd	24(%rsp), %xmm1
	jmp	.L5
.L18:
	movsd	%xmm1, 8(%rsp)
	call	_Z8cpu_timev
	subsd	16(%rsp), %xmm0
	movq	%rbp, %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	movq	%xmm0, %rbx
	call	__printf_chk@PLT
	movsd	8(%rsp), %xmm1
	movl	%r12d, %edx
	movb	$1, %al
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	movaps	%xmm1, %xmm0
	call	__printf_chk@PLT
	movq	%rbp, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
	movl	$2, %edi
	leaq	.LC7(%rip), %rsi
	call	__printf_chk@PLT
	movq	2000040(%rsp), %rax
	subq	%fs:40, %rax
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	addq	$2000056, %rsp
	.cfi_def_cfa_offset 40
	movq	%rbx, %xmm0
	leaq	.LC8(%rip), %rsi
	movb	$1, %al
	popq	%rbx
	.cfi_def_cfa_offset 32
	movl	$2, %edi
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z23calculate_solution_goldPA500_dd, .-_Z23calculate_solution_goldPA500_dd
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2054:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2054:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	0
	.long	1093567616
	.align 8
.LC4:
	.long	0
	.long	1070596096
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC5:
	.long	-1
	.long	2147483647
	.long	0
	.long	0
	.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 <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

double cpu_time();

#define M 500
#define N 500

void calculate_solution_gold(double w[M][N], double epsilon)
{
    double diff;
    double ctime;
    double ctime1;
    double ctime2;
    int i;
    int j;
    int iterations;
    int iterations_print;

    double u[M][N];
    diff = epsilon;

    //  iterate until the  new solution W differs from the old solution U
    //  by no more than EPSILON.

    iterations = 0;
    iterations_print = 1;
    printf("\n");
    printf(" Iteration  Change\n");
    printf("\n");

    ctime1 = cpu_time();

    while (epsilon <= diff)
    {
        //  Save the old solution in U.

        for (i = 0; i < M; i++)
            for (j = 0; j < N; j++)
                u[i][j] = w[i][j];

        //  Determine the new estimate of the solution at the interior points.
        //  The new solution W is the average of north, south, east and west neighbors.

        diff = 0.0;
        for (i = 1; i < M - 1; i++)
        {
            for (j = 1; j < N - 1; j++)
            {
                w[i][j] = (u[i - 1][j] + u[i + 1][j] + u[i][j - 1] + u[i][j + 1]) / 4.0;

                if (diff < fabs(w[i][j] - u[i][j]))
                    diff = fabs(w[i][j] - u[i][j]);
            }
        }

        iterations++;
        if (iterations == iterations_print)
        {
            printf("  %8d  %lg\n", iterations, diff);
            iterations_print = 2 * iterations_print;
        }
    } 

    ctime2 = cpu_time();
    ctime = ctime2 - ctime1;

    printf("\n");
    printf("  %8d  %lg\n", iterations, diff);
    printf("\n");
    printf("  Error tolerance achieved.\n");
    printf("  CPU time = %f\n", ctime);
}

double cpu_time() 
{
    double value;

    value = (double)clock() / (double)CLOCKS_PER_SEC;

    return value;
}

#undef M
#undef N
	.text
	.file	"calculate_solution_gold.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z23calculate_solution_goldPA500_dd
.LCPI0_0:
	.quad	0x3fd0000000000000              # double 0.25
.LCPI0_2:
	.quad	0x412e848000000000              # double 1.0E+6
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI0_1:
	.quad	0x7fffffffffffffff              # double NaN
	.quad	0x7fffffffffffffff              # double NaN
	.text
	.globl	_Z23calculate_solution_goldPA500_dd
	.type	_Z23calculate_solution_goldPA500_dd,@function
_Z23calculate_solution_goldPA500_dd:    # @_Z23calculate_solution_goldPA500_dd
	.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	$2000056, %rsp                  # imm = 0x1E84B8
	.cfi_def_cfa_offset 2000112
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movq	%rdi, %r14
	movl	$10, %edi
	callq	putchar@PLT
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$10, %edi
	callq	putchar@PLT
	callq	clock
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	ucomisd	%xmm0, %xmm0
	jp	.LBB0_1
# %bb.2:                                # %.preheader63.preheader
	leaq	4008(%r14), %r13
	xorl	%ebx, %ebx
	movl	$1, %ebp
	leaq	48(%rsp), %r12
	movl	$4000, %r15d                    # imm = 0xFA0
.LBB0_3:                                # %.preheader63
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
                                        #       Child Loop BB0_5 Depth 3
	movl	$2000000, %edx                  # imm = 0x1E8480
	movq	%r12, %rdi
	movq	%r14, %rsi
	callq	memcpy@PLT
	movapd	.LCPI0_1(%rip), %xmm5           # xmm5 = [NaN,NaN]
	movsd	.LCPI0_0(%rip), %xmm4           # xmm4 = mem[0],zero
	xorpd	%xmm0, %xmm0
	movl	$1, %eax
	leaq	8056(%rsp), %rcx
	movq	%r13, %rdx
.LBB0_4:                                # %.preheader
                                        #   Parent Loop BB0_3 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB0_5 Depth 3
	imulq	$4000, %rax, %rsi               # imm = 0xFA0
	incq	%rax
	movsd	48(%rsp,%rsi), %xmm1            # xmm1 = mem[0],zero
	movsd	56(%rsp,%rsi), %xmm3            # xmm3 = mem[0],zero
	xorl	%esi, %esi
.LBB0_5:                                #   Parent Loop BB0_3 Depth=1
                                        #     Parent Loop BB0_4 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movapd	%xmm0, %xmm2
	movsd	-8000(%rcx,%rsi,8), %xmm0       # xmm0 = mem[0],zero
	addsd	(%rcx,%rsi,8), %xmm0
	addsd	%xmm1, %xmm0
	movapd	%xmm3, %xmm1
	movsd	-3992(%rcx,%rsi,8), %xmm3       # xmm3 = mem[0],zero
	addsd	%xmm3, %xmm0
	mulsd	%xmm4, %xmm0
	movsd	%xmm0, (%rdx,%rsi,8)
	subsd	%xmm1, %xmm0
	andpd	%xmm5, %xmm0
	maxsd	%xmm2, %xmm0
	incq	%rsi
	cmpq	$498, %rsi                      # imm = 0x1F2
	jne	.LBB0_5
# %bb.6:                                #   in Loop: Header=BB0_4 Depth=2
	addq	%r15, %rdx
	addq	%r15, %rcx
	cmpq	$499, %rax                      # imm = 0x1F3
	jne	.LBB0_4
# %bb.7:                                #   in Loop: Header=BB0_3 Depth=1
	incl	%ebx
	cmpl	%ebp, %ebx
	jne	.LBB0_9
# %bb.8:                                #   in Loop: Header=BB0_3 Depth=1
	movl	$.L.str.2, %edi
	movl	%ebp, %esi
	movb	$1, %al
	movapd	%xmm0, 16(%rsp)                 # 16-byte Spill
	callq	printf
	movapd	16(%rsp), %xmm0                 # 16-byte Reload
	addl	%ebp, %ebp
.LBB0_9:                                #   in Loop: Header=BB0_3 Depth=1
	ucomisd	8(%rsp), %xmm0                  # 8-byte Folded Reload
	jae	.LBB0_3
.LBB0_10:                               # %._crit_edge
	movapd	%xmm0, 16(%rsp)                 # 16-byte Spill
	xorps	%xmm0, %xmm0
	cvtsi2sdq	40(%rsp), %xmm0         # 8-byte Folded Reload
	divsd	.LCPI0_2(%rip), %xmm0
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	callq	clock
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI0_2(%rip), %xmm0
	subsd	8(%rsp), %xmm0                  # 8-byte Folded Reload
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movl	$10, %edi
	callq	putchar@PLT
	movl	$.L.str.2, %edi
	movl	%ebx, %esi
	movaps	16(%rsp), %xmm0                 # 16-byte Reload
	movb	$1, %al
	callq	printf
	movl	$10, %edi
	callq	putchar@PLT
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	$.L.str.4, %edi
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	addq	$2000056, %rsp                  # imm = 0x1E84B8
	.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
	jmp	printf                          # TAILCALL
.LBB0_1:
	.cfi_def_cfa_offset 2000112
	xorl	%ebx, %ebx
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	jmp	.LBB0_10
.Lfunc_end0:
	.size	_Z23calculate_solution_goldPA500_dd, .Lfunc_end0-_Z23calculate_solution_goldPA500_dd
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z8cpu_timev
.LCPI1_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	_Z8cpu_timev
	.type	_Z8cpu_timev,@function
_Z8cpu_timev:                           # @_Z8cpu_timev
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	clock
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI1_0(%rip), %xmm0
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z8cpu_timev, .Lfunc_end1-_Z8cpu_timev
	.cfi_endproc
                                        # -- End function
	.type	.L.str.2,@object                # @.str.2
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.2:
	.asciz	"  %8d  %lg\n"
	.size	.L.str.2, 12

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

	.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	" Iteration  Change"
	.size	.Lstr, 19

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"  Error tolerance achieved."
	.size	.Lstr.1, 28

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

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
