#include <math.h>

void getRowsNnzPerProc(int *rowsPP, int *nnzPP, const int *global_n, const int *global_nnz,  const int *row_Ptr) 
{
    int worldSize=1;

    double nnzIncre = (double) *global_nnz/ (double) worldSize;
    double lookingFor=nnzIncre;
    int startRow=0, endRow;
    int partition=0;

    for (int row=0; row<*global_n; ++row) {    
        if ( (double) row_Ptr[row+1] >=  lookingFor ) { 
            // search for smallest difference
            if (fabs ( lookingFor - row_Ptr[row+1])  <= fabs ( lookingFor - row_Ptr[row])   ) {
                endRow = row;
            } else {
                endRow = row-1;
            } // end if //
            
            rowsPP[partition] = endRow-startRow+1;
            nnzPP[partition]  = row_Ptr[endRow+1] - row_Ptr[startRow];
             
            startRow = endRow+1;
            ++partition;
            if (partition < worldSize-1) {
               lookingFor += nnzIncre;
            } else {
                lookingFor=*global_nnz;
            } // end if //   
        } // end if // 
    } // end for //
} // end of getRowsPerProc //

	.file	"tmpxft_00250be0_00000000-6_getRowsNnzPerProc.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2011:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2011:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z17getRowsNnzPerProcPiS_PKiS1_S1_
	.type	_Z17getRowsNnzPerProcPiS_PKiS1_S1_, @function
_Z17getRowsNnzPerProcPiS_PKiS1_S1_:
.LFB2008:
	.cfi_startproc
	endbr64
	cvtsi2sdl	(%rcx), %xmm3
	movq	%rdi, %r10
	movq	%rsi, %r11
	movq	%rcx, %rdi
	movq	.LC0(%rip), %xmm4
	movq	%rdx, %r9
	xorl	%eax, %eax
	xorl	%esi, %esi
	xorl	%ecx, %ecx
	movaps	%xmm3, %xmm0
.L14:
	cmpl	%eax, (%r9)
	jle	.L21
	cvtsi2sdl	4(%r8,%rax,4), %xmm2
	comisd	%xmm0, %xmm2
	jnb	.L18
	incq	%rax
	jmp	.L14
.L21:
	ret
.L18:
	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
.L15:
	cvtsi2sdl	(%r8,%rax,4), %xmm5
	movaps	%xmm0, %xmm1
	movl	%eax, %edx
	subsd	%xmm2, %xmm1
	movaps	%xmm0, %xmm2
	subsd	%xmm5, %xmm2
	andps	%xmm4, %xmm1
	andps	%xmm4, %xmm2
	comisd	%xmm1, %xmm2
	jnb	.L8
	leal	-1(%rax), %edx
.L8:
	movl	%edx, %ebx
	movslq	%esi, %rbp
	incl	%edx
	subl	%ecx, %ebx
	movslq	%edx, %r12
	incl	%ebx
	movl	%ebx, (%r10,%rbp,4)
	movslq	%ecx, %rbx
	movl	(%r8,%r12,4), %ecx
	subl	(%r8,%rbx,4), %ecx
	movl	%ecx, (%r11,%rbp,4)
	incl	%esi
	jns	.L9
	addsd	%xmm3, %xmm0
	jmp	.L19
.L9:
	cvtsi2sdl	(%rdi), %xmm0
.L19:
	movl	%edx, %ecx
.L4:
	incq	%rax
	cmpl	%eax, (%r9)
	jle	.L22
	cvtsi2sdl	4(%r8,%rax,4), %xmm2
	comisd	%xmm0, %xmm2
	jb	.L4
	jmp	.L15
.L22:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2008:
	.size	_Z17getRowsNnzPerProcPiS_PKiS1_S1_, .-_Z17getRowsNnzPerProcPiS_PKiS1_S1_
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2034:
	.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
.LFE2034:
	.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.cst16,"aM",@progbits,16
	.align 16
.LC0:
	.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 <math.h>

void getRowsNnzPerProc(int *rowsPP, int *nnzPP, const int *global_n, const int *global_nnz,  const int *row_Ptr) 
{
    int worldSize=1;

    double nnzIncre = (double) *global_nnz/ (double) worldSize;
    double lookingFor=nnzIncre;
    int startRow=0, endRow;
    int partition=0;

    for (int row=0; row<*global_n; ++row) {    
        if ( (double) row_Ptr[row+1] >=  lookingFor ) { 
            // search for smallest difference
            if (fabs ( lookingFor - row_Ptr[row+1])  <= fabs ( lookingFor - row_Ptr[row])   ) {
                endRow = row;
            } else {
                endRow = row-1;
            } // end if //
            
            rowsPP[partition] = endRow-startRow+1;
            nnzPP[partition]  = row_Ptr[endRow+1] - row_Ptr[startRow];
             
            startRow = endRow+1;
            ++partition;
            if (partition < worldSize-1) {
               lookingFor += nnzIncre;
            } else {
                lookingFor=*global_nnz;
            } // end if //   
        } // end if // 
    } // end for //
} // end of getRowsPerProc //

	.text
	.file	"getRowsNnzPerProc.hip"
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z17getRowsNnzPerProcPiS_PKiS1_S1_
.LCPI0_0:
	.quad	0x7fffffffffffffff              # double NaN
	.quad	0x7fffffffffffffff              # double NaN
	.text
	.globl	_Z17getRowsNnzPerProcPiS_PKiS1_S1_
	.type	_Z17getRowsNnzPerProcPiS_PKiS1_S1_,@function
_Z17getRowsNnzPerProcPiS_PKiS1_S1_:     # @_Z17getRowsNnzPerProcPiS_PKiS1_S1_
	.cfi_startproc
# %bb.0:
	cmpl	$0, (%rdx)
	jle	.LBB0_9
# %bb.1:                                # %.lr.ph.preheader
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	cvtsi2sdl	(%rcx), %xmm0
	xorl	%r9d, %r9d
	movl	$1, %eax
	movapd	.LCPI0_0(%rip), %xmm1           # xmm1 = [NaN,NaN]
	xorl	%r10d, %r10d
	movapd	%xmm0, %xmm2
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	xorps	%xmm3, %xmm3
	cvtsi2sdl	(%r8,%rax,4), %xmm3
	ucomisd	%xmm2, %xmm3
	jb	.LBB0_7
# %bb.3:                                #   in Loop: Header=BB0_2 Depth=1
	movapd	%xmm2, %xmm4
	subsd	%xmm3, %xmm4
	andpd	%xmm1, %xmm4
	xorps	%xmm3, %xmm3
	cvtsi2sdl	-4(%r8,%rax,4), %xmm3
	movapd	%xmm2, %xmm5
	subsd	%xmm3, %xmm5
	andpd	%xmm1, %xmm5
	ucomisd	%xmm4, %xmm5
	movl	$0, %r11d
	sbbl	%r11d, %r11d
	movl	%r11d, %ebx
	subl	%r10d, %ebx
	addl	%eax, %ebx
	movslq	%r9d, %r14
	movl	%ebx, (%rdi,%r14,4)
	addq	%rax, %r11
	movslq	%r11d, %r9
	movl	(%r8,%r9,4), %r9d
	movslq	%r10d, %r10
	subl	(%r8,%r10,4), %r9d
	movl	%r9d, (%rsi,%r14,4)
	leal	1(%r14), %r9d
	cmpl	$-2, %r14d
	jg	.LBB0_5
# %bb.4:                                #   in Loop: Header=BB0_2 Depth=1
	addsd	%xmm0, %xmm2
	jmp	.LBB0_6
.LBB0_5:                                #   in Loop: Header=BB0_2 Depth=1
	xorps	%xmm2, %xmm2
	cvtsi2sdl	(%rcx), %xmm2
.LBB0_6:                                #   in Loop: Header=BB0_2 Depth=1
	movl	%r11d, %r10d
.LBB0_7:                                #   in Loop: Header=BB0_2 Depth=1
	movslq	(%rdx), %r11
	leaq	1(%rax), %rbx
	cmpq	%r11, %rax
	movq	%rbx, %rax
	jl	.LBB0_2
# %bb.8:
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
.LBB0_9:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z17getRowsNnzPerProcPiS_PKiS1_S1_, .Lfunc_end0-_Z17getRowsNnzPerProcPiS_PKiS1_S1_
	.cfi_endproc
                                        # -- End function
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __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
