#include "utils.cuh"
__host__
void readMatrixFromFile(char* input_filename,
                        // First row of file
                        int* rows, int* columns, int* num_of_non_zero_entries,
                        // Return variables
                        int** row_ptr_array, int** row_ptr_array_init,
                        int** col_ind_array,                        
                        double** values_array) {
    FILE *fptr;

    fptr = fopen(input_filename, "r");
    if (fptr == NULL) {
        printf("Error reading file");
        return;
    } else {
        int index = 0;
        int row = 0, column = 0, tmp = 0;
        double non_zero_val = 0.0;
        // Read first row from matrix file
        fscanf(fptr, "%d %d %d", &row, &column, &tmp);
        *rows = row;
        *columns = column;
        *num_of_non_zero_entries = tmp;
        int* elem_ptr_arr = (int*)malloc(sizeof(int) * (*rows + 1));
        *row_ptr_array = (int*)malloc(sizeof(int) * (*rows + 1));
        *row_ptr_array_init = (int*)malloc(sizeof(int) * *num_of_non_zero_entries);
        *col_ind_array = (int*)malloc(sizeof(int) * *num_of_non_zero_entries);
        *values_array  = (double*)malloc(sizeof(double) * *num_of_non_zero_entries);
        
        
        // read lines into 3 variables line by line
        while (index < *num_of_non_zero_entries)
        {
            fscanf(fptr, "%d", &row);
            fscanf(fptr, "%d", &column);
            fscanf(fptr, "%lf", &non_zero_val);
        
            // -1 to make indices start from 0
            elem_ptr_arr[row - 1]++;
//            (*row_ptr_array)[index] = row - 1;
            (*row_ptr_array_init)[index] = row - 1;
            (*col_ind_array)[index] = column - 1;
            (*values_array)[index]  = non_zero_val;
            index++;
        }
        // Prefix sum
        for (int k = 1; k < *rows; k++)
        {
            elem_ptr_arr[k] += elem_ptr_arr[k - 1];
            (*row_ptr_array)[k] = elem_ptr_arr[k];
        }
//        for (int k = *rows; k > 0; k--)
//            (*row_ptr_array)[k] = *num_of_non_zero_entries - elem_ptr_arr[k - 1];
        (*row_ptr_array)[0] = 0;
        free(elem_ptr_arr);
    }
}
__host__
void printMatrix(int rows, int columns, int num_of_non_zero_entries,
                int* row_ptr_array, int* col_ind_array,
                double * values_array) {
    printf("%d\t%d\t%d\n", rows, columns, num_of_non_zero_entries);
    // For each row
    for (int i = 0; i < num_of_non_zero_entries; i++)
    {
        printf("%d\t%d\t%lf\n", row_ptr_array[i] + 1,
            col_ind_array[i] + 1, values_array[i]);
    }
}
__host__
void printVector(int rows, double* x_array)
{
    for (int i = 0; i < rows; i++)
    {
        printf("%lf\n", x_array[i]);
    }
    printf("\n");
}
__host__
void CUDAErrorCheck(const char* msg) {
cudaError_t err = cudaGetLastError();
  if (cudaSuccess != err) {
    fprintf(stderr, "Cuda error: %s: %d: %s.\n", msg, (int)err, cudaGetErrorName(err));
//    exit(EXIT_FAILURE);
  }
}
__host__
void mmult_serial(// First row of file
                       int rows, int columns, int num_of_non_zero_entries,
                       int num_repetitions,
                        // Return variables
                       int* row_ptr_array, int* col_ind_array,
                       double* values_array, double** x_array, double** x_array_old)
{
    for ( int row = 0; row < rows; row++)
    {
        double tmp_product = 0;
        int row_start = row_ptr_array[row];
        int row_end   = row_ptr_array[row + 1];
        
        // Iterate over the sparse row
        for (int j = row_start; j < row_end; j++)
            tmp_product += values_array[j] * (*x_array_old)[col_ind_array[j]];
        (*x_array)[row] = tmp_product;
    }
}
/*__host__
void init_vector_to_1(// First row of file
                       int rows, int columns, int num_of_non_zero_entries,
                       int num_repetitions,
                        // Return variables
                       int* row_ptr_array, int* col_ind_array,
                       double* values_array, double** x_array)
{
    for ( int row = 0; row < rows; row++)
    {
        int row_start = row_ptr_array[row];
        int row_end   = row_ptr_array[row + 1];
        
        // Iterate over the sparse row
        for (int j = row_start; j < row_end; j++)
            (*x_array)[col_ind_array[j]] = 1.0f;
    }
}*/

	.file	"tmpxft_002752b5_00000000-6_utils.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"r"
.LC1:
	.string	"Error reading file"
.LC3:
	.string	"%d %d %d"
.LC4:
	.string	"%d"
.LC5:
	.string	"%lf"
	.text
	.globl	_Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd
	.type	_Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd, @function
_Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd:
.LFB2027:
	.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
	movq	%r9, %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	%rsi, %rbp
	leaq	.LC0(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rcx, %rbx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%r8, 8(%rsp)
	movq	128(%rsp), %r14
	movq	136(%rsp), %r15
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movq	%rax, (%rsp)
	testq	%rax, %rax
	jne	.L3
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L12
	addq	$72, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	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
	jmp	__printf_chk@PLT
.L3:
	.cfi_restore_state
	xorl	%edx, %edx
	leaq	40(%rsp), %rax
	movq	(%rsp), %rdi
	leaq	44(%rsp), %r8
	leaq	.LC3(%rip), %rsi
	movq	%rax, %rcx
	movl	%edx, 36(%rsp)
	movl	%edx, 40(%rsp)
	movl	%edx, 44(%rsp)
	leaq	36(%rsp), %rdx
	movq	%rax, 16(%rsp)
	xorl	%eax, %eax
	movq	$0x000000000, 48(%rsp)
	call	__isoc23_fscanf@PLT
	movl	36(%rsp), %eax
	movl	%eax, 0(%rbp)
	movl	40(%rsp), %eax
	movl	%eax, (%r12)
	movl	44(%rsp), %eax
	movl	%eax, (%rbx)
	movl	0(%rbp), %eax
	leal	1(%rax), %edi
	movslq	%edi, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rax, %r12
	movl	0(%rbp), %eax
	leal	1(%rax), %edi
	movslq	%edi, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movq	8(%rsp), %rcx
	movslq	(%rbx), %rdi
	movq	%rax, (%rcx)
	salq	$2, %rdi
	call	malloc@PLT
	movslq	(%rbx), %rdi
	movq	%rax, 0(%r13)
	salq	$2, %rdi
	call	malloc@PLT
	movslq	(%rbx), %rdi
	movq	%rax, (%r14)
	salq	$3, %rdi
	call	malloc@PLT
	xorl	%r8d, %r8d
	movq	%rax, (%r15)
.L5:
	cmpl	%r8d, (%rbx)
	jle	.L14
	movq	(%rsp), %rdi
	leaq	36(%rsp), %rdx
	xorl	%eax, %eax
	movq	%r8, 24(%rsp)
	leaq	.LC4(%rip), %rsi
	call	__isoc23_fscanf@PLT
	movq	16(%rsp), %rdx
	movq	(%rsp), %rdi
	xorl	%eax, %eax
	leaq	.LC4(%rip), %rsi
	call	__isoc23_fscanf@PLT
	movq	(%rsp), %rdi
	leaq	48(%rsp), %rdx
	xorl	%eax, %eax
	leaq	.LC5(%rip), %rsi
	call	__isoc23_fscanf@PLT
	movslq	36(%rsp), %rdx
	movq	24(%rsp), %r8
	movsd	48(%rsp), %xmm0
	movq	%rdx, %rax
	incl	-4(%r12,%rdx,4)
	movq	0(%r13), %rdx
	decl	%eax
	movl	%eax, (%rdx,%r8,4)
	movl	40(%rsp), %eax
	movq	(%r14), %rdx
	decl	%eax
	movl	%eax, (%rdx,%r8,4)
	movq	(%r15), %rax
	movsd	%xmm0, (%rax,%r8,8)
	incq	%r8
	jmp	.L5
.L14:
	movq	8(%rsp), %rax
	movq	(%rax), %rsi
	xorl	%eax, %eax
.L7:
	incq	%rax
	cmpl	%eax, 0(%rbp)
	jle	.L15
	movl	-4(%r12,%rax,4), %edx
	addl	(%r12,%rax,4), %edx
	movl	%edx, (%r12,%rax,4)
	movl	%edx, (%rsi,%rax,4)
	jmp	.L7
.L15:
	xorl	%eax, %eax
	movl	%eax, (%rsi)
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L9
.L12:
	call	__stack_chk_fail@PLT
.L9:
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	movq	%r12, %rdi
	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
	jmp	free@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd, .-_Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd
	.section	.rodata.str1.1
.LC6:
	.string	"%d\t%d\t%d\n"
.LC7:
	.string	"%d\t%d\t%lf\n"
	.text
	.globl	_Z11printMatrixiiiPiS_Pd
	.type	_Z11printMatrixiiiPiS_Pd, @function
_Z11printMatrixiiiPiS_Pd:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%eax, %eax
	leaq	.LC7(%rip), %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%r9, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%r8, %r13
	movl	%edx, %r8d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rcx, %r12
	movl	%esi, %ecx
	leaq	.LC6(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%edx, %ebp
	movl	%edi, %edx
	movl	$2, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	pushq	%r10
	.cfi_def_cfa_offset 64
	call	__printf_chk@PLT
.L17:
	cmpl	%ebx, %ebp
	jle	.L20
	movl	0(%r13,%rbx,4), %eax
	movsd	(%r14,%rbx,8), %xmm0
	movq	%r15, %rsi
	movl	$2, %edi
	leal	1(%rax), %ecx
	movl	(%r12,%rbx,4), %eax
	incq	%rbx
	leal	1(%rax), %edx
	movb	$1, %al
	call	__printf_chk@PLT
	jmp	.L17
.L20:
	popq	%rax
	.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
.LFE2028:
	.size	_Z11printMatrixiiiPiS_Pd, .-_Z11printMatrixiiiPiS_Pd
	.section	.rodata.str1.1
.LC8:
	.string	"%lf\n"
.LC9:
	.string	"\n"
	.text
	.globl	_Z11printVectoriPd
	.type	_Z11printVectoriPd, @function
_Z11printVectoriPd:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC8(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%edi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rcx
	.cfi_def_cfa_offset 48
.L22:
	cmpl	%ebx, %ebp
	jle	.L25
	movsd	(%r12,%rbx,8), %xmm0
	movq	%r13, %rsi
	movl	$2, %edi
	movb	$1, %al
	incq	%rbx
	call	__printf_chk@PLT
	jmp	.L22
.L25:
	popq	%rdx
	.cfi_def_cfa_offset 40
	leaq	.LC9(%rip), %rsi
	popq	%rbx
	.cfi_def_cfa_offset 32
	movl	$2, %edi
	popq	%rbp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z11printVectoriPd, .-_Z11printVectoriPd
	.section	.rodata.str1.1
.LC10:
	.string	"Cuda error: %s: %d: %s.\n"
	.text
	.globl	_Z14CUDAErrorCheckPKc
	.type	_Z14CUDAErrorCheckPKc, @function
_Z14CUDAErrorCheckPKc:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	call	cudaGetLastError@PLT
	testl	%eax, %eax
	je	.L26
	movl	%eax, %edi
	movl	%eax, 12(%rsp)
	call	cudaGetErrorName@PLT
	movl	12(%rsp), %r8d
	movq	%rbx, %rcx
	movq	stderr(%rip), %rdi
	addq	$16, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 16
	movq	%rax, %r9
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC10(%rip), %rdx
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	__fprintf_chk@PLT
.L26:
	.cfi_restore_state
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z14CUDAErrorCheckPKc, .-_Z14CUDAErrorCheckPKc
	.globl	_Z12mmult_serialiiiiPiS_PdPS0_S1_
	.type	_Z12mmult_serialiiiiPiS_PdPS0_S1_, @function
_Z12mmult_serialiiiiPiS_PdPS0_S1_:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	%edi, %ecx
	xorl	%eax, %eax
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	24(%rsp), %rsi
	movq	32(%rsp), %rdi
	movq	40(%rsp), %r10
.L30:
	cmpl	%eax, %ecx
	jle	.L35
	movl	4(%r8,%rax,4), %r11d
	movslq	(%r8,%rax,4), %rdx
	xorps	%xmm0, %xmm0
.L31:
	cmpl	%edx, %r11d
	jle	.L36
	movslq	(%r9,%rdx,4), %rbx
	movq	(%r10), %r14
	movsd	(%r14,%rbx,8), %xmm1
	mulsd	(%rsi,%rdx,8), %xmm1
	incq	%rdx
	addsd	%xmm1, %xmm0
	jmp	.L31
.L36:
	movq	(%rdi), %rdx
	movsd	%xmm0, (%rdx,%rax,8)
	incq	%rax
	jmp	.L30
.L35:
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z12mmult_serialiiiiPiS_PdPS0_S1_, .-_Z12mmult_serialiiiiPiS_PdPS0_S1_
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.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
.LFE2057:
	.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
	.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 "utils.cuh"
__host__
void readMatrixFromFile(char* input_filename,
                        // First row of file
                        int* rows, int* columns, int* num_of_non_zero_entries,
                        // Return variables
                        int** row_ptr_array, int** row_ptr_array_init,
                        int** col_ind_array,                        
                        double** values_array) {
    FILE *fptr;

    fptr = fopen(input_filename, "r");
    if (fptr == NULL) {
        printf("Error reading file");
        return;
    } else {
        int index = 0;
        int row = 0, column = 0, tmp = 0;
        double non_zero_val = 0.0;
        // Read first row from matrix file
        fscanf(fptr, "%d %d %d", &row, &column, &tmp);
        *rows = row;
        *columns = column;
        *num_of_non_zero_entries = tmp;
        int* elem_ptr_arr = (int*)malloc(sizeof(int) * (*rows + 1));
        *row_ptr_array = (int*)malloc(sizeof(int) * (*rows + 1));
        *row_ptr_array_init = (int*)malloc(sizeof(int) * *num_of_non_zero_entries);
        *col_ind_array = (int*)malloc(sizeof(int) * *num_of_non_zero_entries);
        *values_array  = (double*)malloc(sizeof(double) * *num_of_non_zero_entries);
        
        
        // read lines into 3 variables line by line
        while (index < *num_of_non_zero_entries)
        {
            fscanf(fptr, "%d", &row);
            fscanf(fptr, "%d", &column);
            fscanf(fptr, "%lf", &non_zero_val);
        
            // -1 to make indices start from 0
            elem_ptr_arr[row - 1]++;
//            (*row_ptr_array)[index] = row - 1;
            (*row_ptr_array_init)[index] = row - 1;
            (*col_ind_array)[index] = column - 1;
            (*values_array)[index]  = non_zero_val;
            index++;
        }
        // Prefix sum
        for (int k = 1; k < *rows; k++)
        {
            elem_ptr_arr[k] += elem_ptr_arr[k - 1];
            (*row_ptr_array)[k] = elem_ptr_arr[k];
        }
//        for (int k = *rows; k > 0; k--)
//            (*row_ptr_array)[k] = *num_of_non_zero_entries - elem_ptr_arr[k - 1];
        (*row_ptr_array)[0] = 0;
        free(elem_ptr_arr);
    }
}
__host__
void printMatrix(int rows, int columns, int num_of_non_zero_entries,
                int* row_ptr_array, int* col_ind_array,
                double * values_array) {
    printf("%d\t%d\t%d\n", rows, columns, num_of_non_zero_entries);
    // For each row
    for (int i = 0; i < num_of_non_zero_entries; i++)
    {
        printf("%d\t%d\t%lf\n", row_ptr_array[i] + 1,
            col_ind_array[i] + 1, values_array[i]);
    }
}
__host__
void printVector(int rows, double* x_array)
{
    for (int i = 0; i < rows; i++)
    {
        printf("%lf\n", x_array[i]);
    }
    printf("\n");
}
__host__
void CUDAErrorCheck(const char* msg) {
hipError_t err = hipGetLastError();
  if (hipSuccess != err) {
    fprintf(stderr, "Cuda error: %s: %d: %s.\n", msg, (int)err, hipGetErrorName(err));
//    exit(EXIT_FAILURE);
  }
}
__host__
void mmult_serial(// First row of file
                       int rows, int columns, int num_of_non_zero_entries,
                       int num_repetitions,
                        // Return variables
                       int* row_ptr_array, int* col_ind_array,
                       double* values_array, double** x_array, double** x_array_old)
{
    for ( int row = 0; row < rows; row++)
    {
        double tmp_product = 0;
        int row_start = row_ptr_array[row];
        int row_end   = row_ptr_array[row + 1];
        
        // Iterate over the sparse row
        for (int j = row_start; j < row_end; j++)
            tmp_product += values_array[j] * (*x_array_old)[col_ind_array[j]];
        (*x_array)[row] = tmp_product;
    }
}
/*__host__
void init_vector_to_1(// First row of file
                       int rows, int columns, int num_of_non_zero_entries,
                       int num_repetitions,
                        // Return variables
                       int* row_ptr_array, int* col_ind_array,
                       double* values_array, double** x_array)
{
    for ( int row = 0; row < rows; row++)
    {
        int row_start = row_ptr_array[row];
        int row_end   = row_ptr_array[row + 1];
        
        // Iterate over the sparse row
        for (int j = row_start; j < row_end; j++)
            (*x_array)[col_ind_array[j]] = 1.0f;
    }
}*/

	.text
	.file	"utils.hip"
	.globl	_Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd # -- Begin function _Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd
	.type	_Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd,@function
_Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd: # @_Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd
	.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	$72, %rsp
	.cfi_def_cfa_offset 128
	.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, %rbp
	movq	%r8, %r14
	movq	%rcx, %rbx
	movq	%rdx, 8(%rsp)                   # 8-byte Spill
	movq	%rsi, %r12
	movq	136(%rsp), %rax
	movq	%rax, 56(%rsp)                  # 8-byte Spill
	movq	128(%rsp), %r15
	movl	$.L.str, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB0_10
# %bb.1:
	movq	%rax, %r13
	xorl	%eax, %eax
	movq	%r14, 24(%rsp)                  # 8-byte Spill
	leaq	4(%rsp), %r14
	movl	%eax, (%r14)
	movq	%r15, 32(%rsp)                  # 8-byte Spill
	movq	%rbp, 16(%rsp)                  # 8-byte Spill
	movq	%rsp, %rbp
	movl	%eax, (%rbp)
	leaq	68(%rsp), %r15
	movl	%eax, (%r15)
	movq	$0, 40(%rsp)
	movl	$.L.str.2, %esi
	movq	%r13, %rdi
	movq	%r14, %rdx
	movq	%rbp, %rcx
	movq	%r15, %r8
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	(%r14), %eax
	movl	%eax, (%r12)
	movl	(%rbp), %eax
	movq	8(%rsp), %rcx                   # 8-byte Reload
	movl	%eax, (%rcx)
	movq	%r12, %rax
	movslq	(%r15), %r12
	movq	%rbx, 48(%rsp)                  # 8-byte Spill
	movl	%r12d, (%rbx)
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	movslq	(%rax), %rbx
	leaq	4(,%rbx,4), %r14
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %rbp
	movq	%r14, %rdi
	callq	malloc
	movq	24(%rsp), %rcx                  # 8-byte Reload
	movq	%rax, (%rcx)
	leaq	(,%r12,4), %r14
	movq	%r14, %rdi
	callq	malloc
	movq	16(%rsp), %rcx                  # 8-byte Reload
	movq	%rax, (%rcx)
	movq	32(%rsp), %r15                  # 8-byte Reload
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, (%r15)
	leaq	(,%r12,8), %rdi
	callq	malloc
	movq	56(%rsp), %r15                  # 8-byte Reload
	movq	%rax, (%r15)
	testq	%r12, %r12
	jle	.LBB0_2
# %bb.8:                                # %.lr.ph.preheader
	xorl	%r12d, %r12d
	movq	16(%rsp), %r14                  # 8-byte Reload
	movq	48(%rsp), %rbx                  # 8-byte Reload
.LBB0_9:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.3, %esi
	movq	%r13, %rdi
	leaq	4(%rsp), %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	$.L.str.3, %esi
	movq	%r13, %rdi
	movq	%rsp, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	$.L.str.4, %esi
	movq	%r13, %rdi
	leaq	40(%rsp), %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movslq	4(%rsp), %rax
	incl	-4(%rbp,%rax,4)
	decl	%eax
	movq	(%r14), %rcx
	movl	%eax, (%rcx,%r12,4)
	movl	(%rsp), %eax
	decl	%eax
	movq	32(%rsp), %rcx                  # 8-byte Reload
	movq	(%rcx), %rcx
	movl	%eax, (%rcx,%r12,4)
	movsd	40(%rsp), %xmm0                 # xmm0 = mem[0],zero
	movq	(%r15), %rax
	movsd	%xmm0, (%rax,%r12,8)
	incq	%r12
	movslq	(%rbx), %rax
	cmpq	%rax, %r12
	jl	.LBB0_9
# %bb.3:                                # %.preheader.loopexit
	movq	8(%rsp), %rsi                   # 8-byte Reload
	movl	(%rsi), %ebx
	jmp	.LBB0_4
.LBB0_10:
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	addq	$72, %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
	jmp	printf                          # TAILCALL
.LBB0_2:
	.cfi_def_cfa_offset 128
	movq	8(%rsp), %rsi                   # 8-byte Reload
.LBB0_4:                                # %.preheader
	movq	24(%rsp), %rax                  # 8-byte Reload
	movq	(%rax), %rax
	cmpl	$2, %ebx
	jl	.LBB0_7
# %bb.5:                                # %.lr.ph44.preheader
	movl	$1, %ecx
.LBB0_6:                                # %.lr.ph44
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rbp,%rcx,4), %edx
	addl	-4(%rbp,%rcx,4), %edx
	movl	%edx, (%rbp,%rcx,4)
	movl	%edx, (%rax,%rcx,4)
	incq	%rcx
	movslq	(%rsi), %rdx
	cmpq	%rdx, %rcx
	jl	.LBB0_6
.LBB0_7:                                # %._crit_edge
	movl	$0, (%rax)
	movq	%rbp, %rdi
	callq	free
	addq	$72, %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_end0:
	.size	_Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd, .Lfunc_end0-_Z18readMatrixFromFilePcPiS0_S0_PS0_S1_S1_PPd
	.cfi_endproc
                                        # -- End function
	.globl	_Z11printMatrixiiiPiS_Pd        # -- Begin function _Z11printMatrixiiiPiS_Pd
	.type	_Z11printMatrixiiiPiS_Pd,@function
_Z11printMatrixiiiPiS_Pd:               # @_Z11printMatrixiiiPiS_Pd
	.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	%r9, %rbx
	movq	%r8, %r14
	movq	%rcx, %r15
	movl	%edx, %ebp
	movl	%esi, %edx
	movl	%edi, %esi
	movl	$.L.str.5, %edi
	movl	%ebp, %ecx
	xorl	%eax, %eax
	callq	printf
	testl	%ebp, %ebp
	jle	.LBB1_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%ebp, %r12d
	xorl	%r13d, %r13d
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%r15,%r13,4), %esi
	incl	%esi
	movl	(%r14,%r13,4), %edx
	incl	%edx
	movsd	(%rbx,%r13,8), %xmm0            # xmm0 = mem[0],zero
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	incq	%r13
	cmpq	%r13, %r12
	jne	.LBB1_2
.LBB1_3:                                # %._crit_edge
	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
.Lfunc_end1:
	.size	_Z11printMatrixiiiPiS_Pd, .Lfunc_end1-_Z11printMatrixiiiPiS_Pd
	.cfi_endproc
                                        # -- End function
	.globl	_Z11printVectoriPd              # -- Begin function _Z11printVectoriPd
	.type	_Z11printVectoriPd,@function
_Z11printVectoriPd:                     # @_Z11printVectoriPd
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jle	.LBB2_4
# %bb.1:                                # %.lr.ph.preheader
	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
	movq	%rsi, %rbx
	movl	%edi, %r14d
	xorl	%r15d, %r15d
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movsd	(%rbx,%r15,8), %xmm0            # xmm0 = mem[0],zero
	movl	$.L.str.7, %edi
	movb	$1, %al
	callq	printf
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB2_2
# %bb.3:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
.LBB2_4:                                # %._crit_edge
	movl	$10, %edi
	jmp	putchar@PLT                     # TAILCALL
.Lfunc_end2:
	.size	_Z11printVectoriPd, .Lfunc_end2-_Z11printVectoriPd
	.cfi_endproc
                                        # -- End function
	.globl	_Z14CUDAErrorCheckPKc           # -- Begin function _Z14CUDAErrorCheckPKc
	.type	_Z14CUDAErrorCheckPKc,@function
_Z14CUDAErrorCheckPKc:                  # @_Z14CUDAErrorCheckPKc
	.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
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %rbp, -16
	movq	%rdi, %rbx
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB3_2
# %bb.1:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB3_2:
	.cfi_def_cfa_offset 32
	movl	%eax, %ebp
	movq	stderr(%rip), %r14
	movl	%eax, %edi
	callq	hipGetErrorName
	movl	$.L.str.9, %esi
	movq	%r14, %rdi
	movq	%rbx, %rdx
	movl	%ebp, %ecx
	movq	%rax, %r8
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	fprintf                         # TAILCALL
.Lfunc_end3:
	.size	_Z14CUDAErrorCheckPKc, .Lfunc_end3-_Z14CUDAErrorCheckPKc
	.cfi_endproc
                                        # -- End function
	.globl	_Z12mmult_serialiiiiPiS_PdPS0_S1_ # -- Begin function _Z12mmult_serialiiiiPiS_PdPS0_S1_
	.type	_Z12mmult_serialiiiiPiS_PdPS0_S1_,@function
_Z12mmult_serialiiiiPiS_PdPS0_S1_:      # @_Z12mmult_serialiiiiPiS_PdPS0_S1_
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jle	.LBB4_7
# %bb.1:                                # %.lr.ph25
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	56(%rsp), %rax
	movq	48(%rsp), %rdx
	movq	40(%rsp), %rcx
	movq	(%rdx), %rdx
	movl	%edi, %esi
	movl	(%r8), %ebp
	xorl	%edi, %edi
.LBB4_2:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_4 Depth 2
	movl	4(%r8,%rdi,4), %r10d
	xorpd	%xmm0, %xmm0
	cmpl	%r10d, %ebp
	jge	.LBB4_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB4_2 Depth=1
	movslq	%r10d, %r11
	movq	(%rax), %rbx
	movslq	%ebp, %r14
.LBB4_4:                                #   Parent Loop BB4_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movsd	(%rcx,%r14,8), %xmm1            # xmm1 = mem[0],zero
	movslq	(%r9,%r14,4), %r15
	mulsd	(%rbx,%r15,8), %xmm1
	addsd	%xmm1, %xmm0
	incq	%r14
	cmpq	%r14, %r11
	jne	.LBB4_4
.LBB4_5:                                # %._crit_edge
                                        #   in Loop: Header=BB4_2 Depth=1
	movsd	%xmm0, (%rdx,%rdi,8)
	incq	%rdi
	movl	%r10d, %ebp
	cmpq	%rsi, %rdi
	jne	.LBB4_2
# %bb.6:
	popq	%rbx
	.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
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
	.cfi_restore %rbp
.LBB4_7:                                # %._crit_edge26
	retq
.Lfunc_end4:
	.size	_Z12mmult_serialiiiiPiS_PdPS0_S1_, .Lfunc_end4-_Z12mmult_serialiiiiPiS_PdPS0_S1_
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"r"
	.size	.L.str, 2

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Error reading file"
	.size	.L.str.1, 19

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%d %d %d"
	.size	.L.str.2, 9

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"%lf"
	.size	.L.str.4, 4

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%d\t%d\t%d\n"
	.size	.L.str.5, 10

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"%d\t%d\t%lf\n"
	.size	.L.str.6, 11

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"%lf\n"
	.size	.L.str.7, 5

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Cuda error: %s: %d: %s.\n"
	.size	.L.str.9, 25

	.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
