#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

extern "C" void JacobiHost( float* a, int n, int m, float w0, float w1, float w2, float tol );
extern "C" void JacobiGPU( float* a, int n, int m, float w0, float w1, float w2, float tol );

static void init( float* a, int n, int m )
{
    int i, j;
    memset( a, 0, sizeof(float) * n * m );
    /* boundary conditions */
    for( j = 0; j < n; ++j ){
	a[j*m+n-1] = j;
    }
    for( i = 0; i < m; ++i ){
	a[(n-1)*m+i] = i;
    }
    a[(n-1)*m+m-1] = m+n;
}

int
main( int argc, char* argv[] )
{
    printf("\nStart\n");
    int n, m;
    float *a;

    if( argc <= 1 ){
	fprintf( stderr, "%s sizen [sizem]\n", argv[0] );
	return 1;
    }

    n = atoi( argv[1] );
    if( n <= 0 ) n = 100;
    m = n;
    if( argc > 2 ){
	m = atoi( argv[2] );
	if( m <= 0 ) m = 100;
    }

    printf( "Jacobi %d x %d\n", n, m );

    a = (float*)malloc( sizeof(float) * n * m );

    // init( a, n, m );

    // JacobiHost( a, n, m, .2, .1, .1, .1 );

    init( a, n, m );

    JacobiGPU( a, n, m, .2, .1, .1, .1 );
}

	.file	"tmpxft_0031d362_00000000-6_jdriver.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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"\nStart\n"
.LC1:
	.string	"%s sizen [sizem]\n"
.LC2:
	.string	"Jacobi %d x %d\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	xorl	%eax, %eax
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movl	%edi, %r13d
	movl	$2, %edi
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rsi, %r12
	leaq	.LC0(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	call	__printf_chk@PLT
	cmpl	$1, %r13d
	jg	.L3
	movq	(%r12), %rcx
	movq	stderr(%rip), %rdi
	leaq	.LC1(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %eax
	jmp	.L2
.L3:
	movq	8(%r12), %rdi
	call	atoi@PLT
	movl	%eax, %ebp
	testl	%eax, %eax
	jg	.L5
	movl	$100, %ebp
.L5:
	movl	%ebp, %ebx
	cmpl	$2, %r13d
	je	.L6
	movq	16(%r12), %rdi
	call	atoi@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	jg	.L6
	movl	$100, %ebx
.L6:
	movl	%ebx, %ecx
	movl	%ebp, %edx
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	movslq	%ebx, %r14
	call	__printf_chk@PLT
	movslq	%ebp, %rax
	movq	%r14, %r13
	salq	$2, %r14
	leaq	0(,%rax,4), %r12
	imulq	%r12, %r13
	movq	%r13, %rdi
	call	malloc@PLT
	xorl	%esi, %esi
	movq	%r13, %rcx
	movq	%r13, %rdx
	movq	%rax, %rdi
	call	__memset_chk@PLT
	xorl	%esi, %esi
	movq	%rax, %rdi
	leaq	(%rax,%r12), %rax
.L7:
	cvtsi2ssl	%esi, %xmm0
	movl	%esi, %edx
	incl	%esi
	movss	%xmm0, -4(%rax)
	addq	%r14, %rax
	cmpl	%esi, %ebp
	jne	.L7
	movl	%edx, %eax
	imull	%ebx, %eax
	cltq
	leaq	(%rdi,%rax,4), %rdx
	xorl	%eax, %eax
.L8:
	cvtsi2ssl	%eax, %xmm0
	movss	%xmm0, (%rdx,%rax,4)
	incq	%rax
	cmpl	%eax, %ebx
	jg	.L8
	leal	(%rbx,%rsi), %eax
	movss	.LC3(%rip), %xmm3
	movl	%ebx, %edx
	cvtsi2ssl	%eax, %xmm0
	movaps	%xmm3, %xmm2
	movaps	%xmm3, %xmm1
	movss	%xmm0, -4(%rdi,%r13)
	movss	.LC4(%rip), %xmm0
	call	JacobiGPU@PLT
	xorl	%eax, %eax
.L2:
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	main, .-main
	.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.cst4,"aM",@progbits,4
	.align 4
.LC3:
	.long	1036831949
	.align 4
.LC4:
	.long	1045220557
	.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 <stdlib.h>
#include <sys/time.h>

extern "C" void JacobiHost( float* a, int n, int m, float w0, float w1, float w2, float tol );
extern "C" void JacobiGPU( float* a, int n, int m, float w0, float w1, float w2, float tol );

static void init( float* a, int n, int m )
{
    int i, j;
    memset( a, 0, sizeof(float) * n * m );
    /* boundary conditions */
    for( j = 0; j < n; ++j ){
	a[j*m+n-1] = j;
    }
    for( i = 0; i < m; ++i ){
	a[(n-1)*m+i] = i;
    }
    a[(n-1)*m+m-1] = m+n;
}

int
main( int argc, char* argv[] )
{
    printf("\nStart\n");
    int n, m;
    float *a;

    if( argc <= 1 ){
	fprintf( stderr, "%s sizen [sizem]\n", argv[0] );
	return 1;
    }

    n = atoi( argv[1] );
    if( n <= 0 ) n = 100;
    m = n;
    if( argc > 2 ){
	m = atoi( argv[2] );
	if( m <= 0 ) m = 100;
    }

    printf( "Jacobi %d x %d\n", n, m );

    a = (float*)malloc( sizeof(float) * n * m );

    // init( a, n, m );

    // JacobiHost( a, n, m, .2, .1, .1, .1 );

    init( a, n, m );

    JacobiGPU( a, n, m, .2, .1, .1, .1 );
}

	.text
	.file	"jdriver.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI0_0:
	.long	0x3e4ccccd                      # float 0.200000003
.LCPI0_1:
	.long	0x3dcccccd                      # float 0.100000001
	.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
	pushq	%rax
	.cfi_def_cfa_offset 64
	.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	%rsi, %r15
	movl	%edi, %ebp
	movl	$.Lstr, %edi
	callq	puts@PLT
	cmpl	$1, %ebp
	jle	.LBB0_1
# %bb.2:
	movq	8(%r15), %rdi
	callq	atoi
	movl	%eax, %ebx
	testl	%eax, %eax
	movl	$100, %r12d
	cmovlel	%r12d, %ebx
	movl	%ebx, %r14d
	cmpl	$2, %ebp
	je	.LBB0_4
# %bb.3:
	movq	16(%r15), %rdi
	callq	atoi
	testl	%eax, %eax
	cmovgl	%eax, %r12d
	movl	%r12d, %r14d
.LBB0_4:                                # %.lr.ph.preheader.i
	xorl	%r15d, %r15d
	movl	$.L.str.2, %edi
	movl	%ebx, %esi
	movl	%r14d, %edx
	xorl	%eax, %eax
	callq	printf
	movslq	%ebx, %r13
	movslq	%r14d, %r12
	movq	%r13, %rsi
	imulq	%r12, %rsi
	shlq	$2, %rsi
	movl	$1, %edi
	callq	calloc@PLT
	movl	%r13d, %ecx
	leaq	(%rax,%r13,4), %rdx
	addq	$-4, %rdx
	shlq	$2, %r12
.LBB0_5:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtsi2ss	%r15d, %xmm0
	movss	%xmm0, (%rdx)
	incq	%r15
	addq	%r12, %rdx
	cmpq	%r15, %rcx
	jne	.LBB0_5
# %bb.6:                                # %.preheader.i
	testl	%r14d, %r14d
	jle	.LBB0_9
# %bb.7:                                # %.lr.ph30.i
	leal	-1(%rbx), %ecx
	imull	%r14d, %ecx
	movslq	%ecx, %rdx
	movl	%r14d, %ecx
	leaq	(%rax,%rdx,4), %rdx
	xorl	%esi, %esi
.LBB0_8:                                # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtsi2ss	%esi, %xmm0
	movss	%xmm0, (%rdx,%rsi,4)
	incq	%rsi
	cmpq	%rsi, %rcx
	jne	.LBB0_8
.LBB0_9:                                # %_ZL4initPfii.exit
	leal	(%r14,%rbx), %ecx
	xorps	%xmm0, %xmm0
	cvtsi2ss	%ecx, %xmm0
	movl	%ebx, %ecx
	imull	%r14d, %ecx
	movslq	%ecx, %rcx
	movss	%xmm0, -4(%rax,%rcx,4)
	movss	.LCPI0_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movss	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	movq	%rax, %rdi
	movl	%ebx, %esi
	movl	%r14d, %edx
	movaps	%xmm1, %xmm2
	movaps	%xmm1, %xmm3
	callq	JacobiGPU
	xorl	%eax, %eax
.LBB0_10:
	addq	$8, %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
.LBB0_1:
	.cfi_def_cfa_offset 64
	movq	stderr(%rip), %rdi
	movq	(%r15), %rdx
	movl	$.L.str.1, %esi
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %eax
	jmp	.LBB0_10
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"%s sizen [sizem]\n"
	.size	.L.str.1, 18

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Jacobi %d x %d\n"
	.size	.L.str.2, 16

	.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	"\nStart"
	.size	.Lstr, 7

	.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
