//
//  main.cu
//  CS 426 - Project 4
//
//  Created by Muhammed Cavusoglu on 19.05.2019.
//  Copyright © 2019 Muhammed Cavusoglu. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void read_matrix(int **row_ptr, int **col_ind, float **values, const char *filename, int *num_rows, int *num_cols, int *num_vals);

// Parallel SpMV using CSR format
__global__ void spmv_csr(const int *row_ptr, const int *col_ind, const float *values, const int num_rows, const float *x, float *y) {
    // Uses a grid-stride loop to perform dot product
    for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < num_rows; i += blockDim.x * gridDim.x) {
        float dotProduct = 0;
        const int row_start = row_ptr[i];
        const int row_end = row_ptr[i + 1];
        
        for (int j = row_start; j < row_end; j++) {
            dotProduct += values[j] * x[col_ind[j]];
        }
        
        y[i] = dotProduct;
    }
 }

int main(int argc, const char * argv[]) {
    if (argc != 5) {
        fprintf(stdout, "Invalid command, enter:\n1. number of threads, 2. number of repetitions, 3. print mode (1 or 2), 4. test filename\n");
        exit(0);
    }
    
    int *row_ptr, *col_ind, num_rows, num_cols, num_vals, numSMs;;
    float *values;
    
    int num_thread = atoi(argv[1]);
    int num_repeat = atoi(argv[2]);
    int print_mode = atoi(argv[3]);
    const char *filename = argv[4];
    
    read_matrix(&row_ptr, &col_ind, &values, filename, &num_rows, &num_cols, &num_vals);
    
    float *x = (float *) malloc(num_rows * sizeof(float));
    float *y = (float *) malloc(num_rows * sizeof(float));
    for (int i = 0; i < num_rows; i++) {
        x[i] = 1.0;
        y[i] = 0.0;
    }
    
    if (print_mode == 1) {
        // Values Array
        fprintf(stdout, "Values Array:\n");
        for (int i = 0; i < num_vals; i++) {
            fprintf(stdout, "%.6f ", values[i]);
        }
        
        // Column Indices Array
        fprintf(stdout, "\n\nColumn Indices Array:\n");
        for (int i = 0; i < num_vals; i++) {
            fprintf(stdout, "%d ", col_ind[i]);
        }
        
        // Row Pointer Array
        fprintf(stdout, "\n\nRow Pointer Array:\n");
        for (int i = 0; i < (num_rows + 1); i++) {
            fprintf(stdout, "%d ", row_ptr[i]);
        }
        
        fprintf(stdout, "\n\nInitial Vector:\n");
        for (int i = 0; i < num_rows; i++) {
            fprintf(stdout, "%.1f ", x[i]);
        }
        
        fprintf(stdout, "\n\nResulting Vector:\n");
    }
    
    // Allocate on device
    int *d_row_ptr, *d_col_ind;
    float *d_values, *d_x, *d_y;
    cudaMalloc((void**)&d_row_ptr, (num_rows + 1) * sizeof(int));
    cudaMalloc((void**)&d_col_ind, num_vals * sizeof(int));
    cudaMalloc((void**)&d_values, num_vals * sizeof(float));
    cudaMalloc((void**)&d_x, num_rows * sizeof(float));
    cudaMalloc((void**)&d_y, num_rows * sizeof(float));
    
    // Get number of SMs
    cudaDeviceGetAttribute(&numSMs, cudaDevAttrMultiProcessorCount, 0);
    
    // Copy from host to device
    cudaMemcpy(d_row_ptr, row_ptr, (num_rows + 1) * sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(d_col_ind, col_ind, num_vals * sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(d_values, values, num_vals * sizeof(float), cudaMemcpyHostToDevice);
    
    // Time the iterations
    float elapsed_time;
    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    
    cudaEventRecord(start);
    
    for (int i = 0; i < num_repeat; i++) {
        cudaMemcpy(d_x, x, num_rows * sizeof(float), cudaMemcpyHostToDevice);
        cudaMemcpy(d_y, y, num_rows * sizeof(float), cudaMemcpyHostToDevice);
        
        // Call kernel function
        spmv_csr<<<32 * numSMs, num_thread>>>(d_row_ptr, d_col_ind, d_values, num_rows, d_x, d_y);
        
        // Copy the result to x_{i} at the end of each iteration, and use it in iteration x_{i+1}
        cudaMemcpy(y, d_y, num_rows * sizeof(float), cudaMemcpyDeviceToHost);
        for (int i = 0; i < num_rows; i++) {
            x[i] = y[i];
            y[i] = 0.0;
        }
    }
    
    cudaEventRecord(stop);
    
    cudaEventSynchronize(stop);
    cudaEventElapsedTime(&elapsed_time, start, stop);
    
    // Print resulting vector
    if (print_mode == 1 || print_mode == 2) {
        for (int i = 0; i < num_rows; i++) {
            fprintf(stdout, "%.6f ", x[i]);
        }
        fprintf(stdout, "\n");
    }
    
    // Print elapsed time
    // printf("\nParallel Running time:  %.4f ms\n", elapsed_time);
    // printf("Num SMs: %d\n", numSMs);
    
    // Free
    cudaFree(d_row_ptr);
    cudaFree(d_col_ind);
    cudaFree(d_values);
    cudaFree(d_x);
    cudaFree(d_y);
    
    free(row_ptr);
    free(col_ind);
    free(values);
    
    return 0;
}

// Reads a sparse matrix and represents it using CSR (Compressed Sparse Row) format
void read_matrix(int **row_ptr, int **col_ind, float **values, const char *filename, int *num_rows, int *num_cols, int *num_vals) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        fprintf(stdout, "File cannot be opened!\n");
        exit(0);
    }
    
    // Get number of rows, columns, and non-zero values
    fscanf(file, "%d %d %d\n", num_rows, num_cols, num_vals);
    
    int *row_ptr_t = (int *) malloc((*num_rows + 1) * sizeof(int));
    int *col_ind_t = (int *) malloc(*num_vals * sizeof(int));
    float *values_t = (float *) malloc(*num_vals * sizeof(float));
    
    // Collect occurances of each row for determining the indices of row_ptr
    int *row_occurances = (int *) malloc(*num_rows * sizeof(int));
    for (int i = 0; i < *num_rows; i++) {
        row_occurances[i] = 0;
    }
    
    int row, column;
    float value;
    while (fscanf(file, "%d %d %f\n", &row, &column, &value) != EOF) {
        // Subtract 1 from row and column indices to match C format
        row--;
        column--;
        
        row_occurances[row]++;
    }
    
    // Set row_ptr
    int index = 0;
    for (int i = 0; i < *num_rows; i++) {
        row_ptr_t[i] = index;
        index += row_occurances[i];
    }
    row_ptr_t[*num_rows] = *num_vals;
    free(row_occurances);
    
    // Set the file position to the beginning of the file
    rewind(file);
    
    // Read the file again, save column indices and values
    for (int i = 0; i < *num_vals; i++) {
        col_ind_t[i] = -1;
    }
    
    fscanf(file, "%d %d %d\n", num_rows, num_cols, num_vals);
    int i = 0;
    while (fscanf(file, "%d %d %f\n", &row, &column, &value) != EOF) {
        row--;
        column--;
        
        // Find the correct index (i + row_ptr_t[row]) using both row information and an index i
        while (col_ind_t[i + row_ptr_t[row]] != -1) {
            i++;
        }
        col_ind_t[i + row_ptr_t[row]] = column;
        values_t[i + row_ptr_t[row]] = value;
        i = 0;
    }
    
    fclose(file);
    
    *row_ptr = row_ptr_t;
    *col_ind = col_ind_t;
    *values = values_t;
}

	.file	"tmpxft_0028a15e_00000000-6_main.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	"r"
.LC1:
	.string	"File cannot be opened!\n"
.LC2:
	.string	"%d %d %d\n"
.LC3:
	.string	"%d %d %f\n"
	.text
	.globl	_Z11read_matrixPPiS0_PPfPKcS_S_S_
	.type	_Z11read_matrixPPiS0_PPfPKcS_S_S_, @function
_Z11read_matrixPPiS0_PPfPKcS_S_S_:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdx, %r15
	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
	movq	%r8, %rbx
	subq	$104, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 40(%rsp)
	movq	%rcx, %rdi
	movq	160(%rsp), %r13
	movq	%rsi, 48(%rsp)
	leaq	.LC0(%rip), %rsi
	movq	%r9, 16(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movq	%rax, 8(%rsp)
	testq	%rax, %rax
	jne	.L3
	movq	stdout(%rip), %rdi
	leaq	.LC1(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L3:
	movq	16(%rsp), %rcx
	movq	8(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r13, %r8
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movl	(%rbx), %eax
	leal	1(%rax), %edi
	movslq	%edi, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movslq	0(%r13), %rdi
	movq	%rax, %r12
	salq	$2, %rdi
	call	malloc@PLT
	movslq	0(%r13), %rdi
	movq	%rax, %rbp
	salq	$2, %rdi
	call	malloc@PLT
	movslq	(%rbx), %rdi
	movq	%rax, %r14
	salq	$2, %rdi
	call	malloc@PLT
	movl	(%rbx), %edx
	movq	%rax, %r9
	xorl	%eax, %eax
.L4:
	cmpl	%eax, %edx
	jle	.L6
	xorl	%ecx, %ecx
	movl	%ecx, (%r9,%rax,4)
	incq	%rax
	jmp	.L4
.L6:
	leaq	84(%rsp), %rax
	movq	8(%rsp), %rdi
	leaq	76(%rsp), %rdx
	movq	%r9, 56(%rsp)
	movq	%rax, 24(%rsp)
	leaq	80(%rsp), %rax
	movq	24(%rsp), %r8
	leaq	.LC3(%rip), %rsi
	movq	%rax, 32(%rsp)
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movq	56(%rsp), %r9
	incl	%eax
	je	.L19
	movl	76(%rsp), %eax
	decl	80(%rsp)
	decl	%eax
	movl	%eax, 76(%rsp)
	cltq
	incl	(%r9,%rax,4)
	jmp	.L6
.L19:
	movslq	(%rbx), %rdx
	xorl	%eax, %eax
	xorl	%ecx, %ecx
.L8:
	cmpl	%eax, %edx
	jle	.L20
	movl	%ecx, (%r12,%rax,4)
	addl	(%r9,%rax,4), %ecx
	incq	%rax
	jmp	.L8
.L20:
	movl	0(%r13), %eax
	movq	%r9, %rdi
	movl	%eax, (%r12,%rdx,4)
	call	free@PLT
	movq	8(%rsp), %rdi
	call	rewind@PLT
	movl	0(%r13), %edx
	xorl	%eax, %eax
.L10:
	cmpl	%eax, %edx
	jle	.L21
	movl	$-1, 0(%rbp,%rax,4)
	incq	%rax
	jmp	.L10
.L21:
	movq	16(%rsp), %rcx
	movq	8(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r13, %r8
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	leaq	.LC3(%rip), %rbx
	call	__isoc23_fscanf@PLT
.L12:
	movq	24(%rsp), %r8
	movq	32(%rsp), %rcx
	xorl	%eax, %eax
	movq	%rbx, %rsi
	movq	8(%rsp), %rdi
	leaq	76(%rsp), %rdx
	call	__isoc23_fscanf@PLT
	incl	%eax
	je	.L22
	movl	76(%rsp), %eax
	movl	80(%rsp), %ecx
	decl	%eax
	leal	-1(%rcx), %edx
	movl	%eax, 76(%rsp)
	cltq
	movslq	(%r12,%rax,4), %rax
	movl	%edx, 80(%rsp)
	salq	$2, %rax
.L13:
	movq	%rax, %rcx
	addq	$4, %rax
	cmpl	$-1, -4(%rbp,%rax)
	jne	.L13
	movss	84(%rsp), %xmm0
	movl	%edx, 0(%rbp,%rcx)
	movss	%xmm0, (%r14,%rcx)
	jmp	.L12
.L22:
	movq	8(%rsp), %rdi
	call	fclose@PLT
	movq	40(%rsp), %rax
	movq	%r12, (%rax)
	movq	48(%rsp), %rax
	movq	%rbp, (%rax)
	movq	%r14, (%r15)
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	addq	$104, %rsp
	.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	_Z11read_matrixPPiS0_PPfPKcS_S_S_, .-_Z11read_matrixPPiS0_PPfPKcS_S_S_
	.globl	_Z40__device_stub__Z8spmv_csrPKiS0_PKfiS2_PfPKiS0_PKfiS2_Pf
	.type	_Z40__device_stub__Z8spmv_csrPKiS0_PKfiS2_PfPKiS0_PKfiS2_Pf, @function
_Z40__device_stub__Z8spmv_csrPKiS0_PKfiS2_PfPKiS0_PKfiS2_Pf:
.LFB2053:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	84(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movl	%ecx, 20(%rsp)
	leaq	64(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%r9, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	movq	%rsp, %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L23
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z8spmv_csrPKiS0_PKfiS2_Pf(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 208
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L23:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2053:
	.size	_Z40__device_stub__Z8spmv_csrPKiS0_PKfiS2_PfPKiS0_PKfiS2_Pf, .-_Z40__device_stub__Z8spmv_csrPKiS0_PKfiS2_PfPKiS0_PKfiS2_Pf
	.globl	_Z8spmv_csrPKiS0_PKfiS2_Pf
	.type	_Z8spmv_csrPKiS0_PKfiS2_Pf, @function
_Z8spmv_csrPKiS0_PKfiS2_Pf:
.LFB2054:
	.cfi_startproc
	endbr64
	jmp	_Z40__device_stub__Z8spmv_csrPKiS0_PKfiS2_PfPKiS0_PKfiS2_Pf
	.cfi_endproc
.LFE2054:
	.size	_Z8spmv_csrPKiS0_PKfiS2_Pf, .-_Z8spmv_csrPKiS0_PKfiS2_Pf
	.section	.rodata.str1.1
.LC4:
	.string	"Invalid command, enter:\n1. number of threads, 2. number of repetitions, 3. print mode (1 or 2), 4. test filename\n"
.LC7:
	.string	"Values Array:\n"
.LC8:
	.string	"%.6f "
.LC9:
	.string	"\n\nColumn Indices Array:\n"
.LC10:
	.string	"%d "
.LC11:
	.string	"\n\nRow Pointer Array:\n"
.LC12:
	.string	"\n\nInitial Vector:\n"
.LC13:
	.string	"%.1f "
.LC14:
	.string	"\n\nResulting Vector:\n"
.LC15:
	.string	"\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.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
	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	$168, %rsp
	.cfi_def_cfa_offset 224
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	cmpl	$5, %edi
	je	.L29
	movq	stdout(%rip), %rdi
	leaq	.LC4(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L29:
	movq	8(%rsi), %rdi
	movq	%rsi, %rbx
	call	atoi@PLT
	movq	16(%rbx), %rdi
	movl	%eax, %r14d
	call	atoi@PLT
	movq	24(%rbx), %rdi
	movl	%eax, %r12d
	call	atoi@PLT
	movq	32(%rbx), %rcx
	leaq	64(%rsp), %rdx
	leaq	56(%rsp), %rsi
	movl	%eax, 28(%rsp)
	leaq	48(%rsp), %rdi
	pushq	%rax
	.cfi_def_cfa_offset 232
	leaq	48(%rsp), %rax
	pushq	%rax
	.cfi_def_cfa_offset 240
	leaq	52(%rsp), %r9
	leaq	48(%rsp), %r8
	call	_Z11read_matrixPPiS0_PPfPKcS_S_S_
	movslq	48(%rsp), %rbp
	movq	%rbp, %r13
	salq	$2, %rbp
	movq	%rbp, %rdi
	call	malloc@PLT
	movq	%rbp, %rdi
	movq	%rax, %rbx
	call	malloc@PLT
	movss	.LC5(%rip), %xmm0
	popq	%rdx
	.cfi_def_cfa_offset 232
	movq	%rax, %rbp
	popq	%rcx
	.cfi_def_cfa_offset 224
	xorl	%eax, %eax
.L30:
	cmpl	%eax, %r13d
	jle	.L52
	movl	$0x00000000, 0(%rbp,%rax,4)
	movss	%xmm0, (%rbx,%rax,4)
	incq	%rax
	jmp	.L30
.L52:
	cmpl	$1, 28(%rsp)
	jne	.L32
	movq	stdout(%rip), %rdi
	leaq	.LC7(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	xorl	%r15d, %r15d
	leaq	.LC8(%rip), %r13
	call	__fprintf_chk@PLT
.L33:
	movq	64(%rsp), %rax
	movq	stdout(%rip), %rdi
	cmpl	%r15d, 40(%rsp)
	jle	.L53
	cvtss2sd	(%rax,%r15,4), %xmm0
	movq	%r13, %rdx
	movl	$2, %esi
	movb	$1, %al
	call	__fprintf_chk@PLT
	incq	%r15
	jmp	.L33
.L53:
	leaq	.LC9(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	xorl	%r15d, %r15d
	call	__fprintf_chk@PLT
	leaq	.LC10(%rip), %r13
.L35:
	movq	56(%rsp), %rax
	movq	stdout(%rip), %rdi
	cmpl	%r15d, 40(%rsp)
	jle	.L54
	movl	(%rax,%r15,4), %ecx
	movq	%r13, %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	incq	%r15
	call	__fprintf_chk@PLT
	jmp	.L35
.L54:
	leaq	.LC11(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	xorl	%r15d, %r15d
	call	__fprintf_chk@PLT
	leaq	.LC10(%rip), %r13
.L37:
	movq	48(%rsp), %rax
	movq	stdout(%rip), %rdi
	cmpl	%r15d, 32(%rsp)
	jl	.L55
	movl	(%rax,%r15,4), %ecx
	movq	%r13, %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	incq	%r15
	call	__fprintf_chk@PLT
	jmp	.L37
.L55:
	leaq	.LC12(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	xorl	%r15d, %r15d
	call	__fprintf_chk@PLT
	leaq	.LC13(%rip), %r13
.L39:
	movq	stdout(%rip), %rdi
	cmpl	%r15d, 32(%rsp)
	jle	.L56
	movq	%r13, %rdx
	movl	$2, %esi
	movb	$1, %al
	cvtss2sd	(%rbx,%r15,4), %xmm0
	call	__fprintf_chk@PLT
	incq	%r15
	jmp	.L39
.L56:
	leaq	.LC14(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L32:
	movl	32(%rsp), %eax
	leaq	72(%rsp), %rdi
	xorl	%r13d, %r13d
	movabsq	$4294967297, %r15
	leal	1(%rax), %esi
	movslq	%esi, %rsi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	40(%rsp), %rsi
	leaq	80(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	40(%rsp), %rsi
	leaq	88(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	32(%rsp), %rsi
	leaq	96(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	32(%rsp), %rsi
	leaq	104(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	leaq	44(%rsp), %rdi
	xorl	%edx, %edx
	movl	$16, %esi
	call	cudaDeviceGetAttribute@PLT
	movq	48(%rsp), %rax
	movq	72(%rsp), %rdi
	movl	$1, %ecx
	movq	%rax, (%rsp)
	movl	32(%rsp), %eax
	movq	(%rsp), %rsi
	leal	1(%rax), %edx
	movslq	%edx, %rdx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movq	56(%rsp), %rax
	movslq	40(%rsp), %rdx
	movl	$1, %ecx
	movq	80(%rsp), %rdi
	salq	$2, %rdx
	movq	%rax, %rsi
	movq	%rax, 8(%rsp)
	call	cudaMemcpy@PLT
	movq	64(%rsp), %rax
	movslq	40(%rsp), %rdx
	movl	$1, %ecx
	movq	88(%rsp), %rdi
	salq	$2, %rdx
	movq	%rax, %rsi
	movq	%rax, 16(%rsp)
	call	cudaMemcpy@PLT
	leaq	112(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	120(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	112(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
.L41:
	cmpl	%r12d, %r13d
	jge	.L57
	movslq	32(%rsp), %rdx
	movq	96(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	32(%rsp), %rdx
	movq	104(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movl	44(%rsp), %eax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%r15, 144(%rsp)
	movl	148(%rsp), %ecx
	sall	$5, %eax
	movq	%r15, 132(%rsp)
	movl	136(%rsp), %esi
	movl	%r14d, 140(%rsp)
	movq	140(%rsp), %rdx
	movl	%eax, 128(%rsp)
	movq	128(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L42
	movq	104(%rsp), %r9
	movq	96(%rsp), %r8
	movl	32(%rsp), %ecx
	movq	88(%rsp), %rdx
	movq	80(%rsp), %rsi
	movq	72(%rsp), %rdi
	call	_Z40__device_stub__Z8spmv_csrPKiS0_PKfiS2_PfPKiS0_PKfiS2_Pf
.L42:
	movslq	32(%rsp), %rdx
	movq	104(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbp, %rdi
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movl	32(%rsp), %edx
	xorl	%eax, %eax
.L43:
	cmpl	%eax, %edx
	jle	.L58
	movss	0(%rbp,%rax,4), %xmm0
	movl	$0x00000000, 0(%rbp,%rax,4)
	movss	%xmm0, (%rbx,%rax,4)
	incq	%rax
	jmp	.L43
.L58:
	incl	%r13d
	jmp	.L41
.L57:
	movq	120(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	120(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	120(%rsp), %rdx
	movq	112(%rsp), %rsi
	leaq	140(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movl	28(%rsp), %eax
	decl	%eax
	cmpl	$1, %eax
	ja	.L47
	xorl	%ebp, %ebp
	leaq	.LC8(%rip), %r12
.L46:
	movq	stdout(%rip), %rdi
	cmpl	%ebp, 32(%rsp)
	jle	.L59
	movq	%r12, %rdx
	movl	$2, %esi
	movb	$1, %al
	cvtss2sd	(%rbx,%rbp,4), %xmm0
	call	__fprintf_chk@PLT
	incq	%rbp
	jmp	.L46
.L59:
	leaq	.LC15(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L47:
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	movq	96(%rsp), %rdi
	call	cudaFree@PLT
	movq	104(%rsp), %rdi
	call	cudaFree@PLT
	movq	(%rsp), %rdi
	call	free@PLT
	movq	8(%rsp), %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	free@PLT
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L49
	call	__stack_chk_fail@PLT
.L49:
	addq	$168, %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
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC16:
	.string	"_Z8spmv_csrPKiS0_PKfiS2_Pf"
	.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	.LC16(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z8spmv_csrPKiS0_PKfiS2_Pf(%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
.LC5:
	.long	1065353216
	.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:

//
//  main.cu
//  CS 426 - Project 4
//
//  Created by Muhammed Cavusoglu on 19.05.2019.
//  Copyright © 2019 Muhammed Cavusoglu. All rights reserved.
//


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void read_matrix(int **row_ptr, int **col_ind, float **values, const char *filename, int *num_rows, int *num_cols, int *num_vals);

// Parallel SpMV using CSR format
__global__ void spmv_csr(const int *row_ptr, const int *col_ind, const float *values, const int num_rows, const float *x, float *y) {
    // Uses a grid-stride loop to perform dot product
    for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < num_rows; i += blockDim.x * gridDim.x) {
        float dotProduct = 0;
        const int row_start = row_ptr[i];
        const int row_end = row_ptr[i + 1];
        
        for (int j = row_start; j < row_end; j++) {
            dotProduct += values[j] * x[col_ind[j]];
        }
        
        y[i] = dotProduct;
    }
 }

int main(int argc, const char * argv[]) {
    if (argc != 5) {
        fprintf(stdout, "Invalid command, enter:\n1. number of threads, 2. number of repetitions, 3. print mode (1 or 2), 4. test filename\n");
        exit(0);
    }
    
    int *row_ptr, *col_ind, num_rows, num_cols, num_vals, numSMs;;
    float *values;
    
    int num_thread = atoi(argv[1]);
    int num_repeat = atoi(argv[2]);
    int print_mode = atoi(argv[3]);
    const char *filename = argv[4];
    
    read_matrix(&row_ptr, &col_ind, &values, filename, &num_rows, &num_cols, &num_vals);
    
    float *x = (float *) malloc(num_rows * sizeof(float));
    float *y = (float *) malloc(num_rows * sizeof(float));
    for (int i = 0; i < num_rows; i++) {
        x[i] = 1.0;
        y[i] = 0.0;
    }
    
    if (print_mode == 1) {
        // Values Array
        fprintf(stdout, "Values Array:\n");
        for (int i = 0; i < num_vals; i++) {
            fprintf(stdout, "%.6f ", values[i]);
        }
        
        // Column Indices Array
        fprintf(stdout, "\n\nColumn Indices Array:\n");
        for (int i = 0; i < num_vals; i++) {
            fprintf(stdout, "%d ", col_ind[i]);
        }
        
        // Row Pointer Array
        fprintf(stdout, "\n\nRow Pointer Array:\n");
        for (int i = 0; i < (num_rows + 1); i++) {
            fprintf(stdout, "%d ", row_ptr[i]);
        }
        
        fprintf(stdout, "\n\nInitial Vector:\n");
        for (int i = 0; i < num_rows; i++) {
            fprintf(stdout, "%.1f ", x[i]);
        }
        
        fprintf(stdout, "\n\nResulting Vector:\n");
    }
    
    // Allocate on device
    int *d_row_ptr, *d_col_ind;
    float *d_values, *d_x, *d_y;
    hipMalloc((void**)&d_row_ptr, (num_rows + 1) * sizeof(int));
    hipMalloc((void**)&d_col_ind, num_vals * sizeof(int));
    hipMalloc((void**)&d_values, num_vals * sizeof(float));
    hipMalloc((void**)&d_x, num_rows * sizeof(float));
    hipMalloc((void**)&d_y, num_rows * sizeof(float));
    
    // Get number of SMs
    hipDeviceGetAttribute(&numSMs, hipDeviceAttributeMultiprocessorCount, 0);
    
    // Copy from host to device
    hipMemcpy(d_row_ptr, row_ptr, (num_rows + 1) * sizeof(int), hipMemcpyHostToDevice);
    hipMemcpy(d_col_ind, col_ind, num_vals * sizeof(int), hipMemcpyHostToDevice);
    hipMemcpy(d_values, values, num_vals * sizeof(float), hipMemcpyHostToDevice);
    
    // Time the iterations
    float elapsed_time;
    hipEvent_t start, stop;
    hipEventCreate(&start);
    hipEventCreate(&stop);
    
    hipEventRecord(start);
    
    for (int i = 0; i < num_repeat; i++) {
        hipMemcpy(d_x, x, num_rows * sizeof(float), hipMemcpyHostToDevice);
        hipMemcpy(d_y, y, num_rows * sizeof(float), hipMemcpyHostToDevice);
        
        // Call kernel function
        spmv_csr<<<32 * numSMs, num_thread>>>(d_row_ptr, d_col_ind, d_values, num_rows, d_x, d_y);
        
        // Copy the result to x_{i} at the end of each iteration, and use it in iteration x_{i+1}
        hipMemcpy(y, d_y, num_rows * sizeof(float), hipMemcpyDeviceToHost);
        for (int i = 0; i < num_rows; i++) {
            x[i] = y[i];
            y[i] = 0.0;
        }
    }
    
    hipEventRecord(stop);
    
    hipEventSynchronize(stop);
    hipEventElapsedTime(&elapsed_time, start, stop);
    
    // Print resulting vector
    if (print_mode == 1 || print_mode == 2) {
        for (int i = 0; i < num_rows; i++) {
            fprintf(stdout, "%.6f ", x[i]);
        }
        fprintf(stdout, "\n");
    }
    
    // Print elapsed time
    // printf("\nParallel Running time:  %.4f ms\n", elapsed_time);
    // printf("Num SMs: %d\n", numSMs);
    
    // Free
    hipFree(d_row_ptr);
    hipFree(d_col_ind);
    hipFree(d_values);
    hipFree(d_x);
    hipFree(d_y);
    
    free(row_ptr);
    free(col_ind);
    free(values);
    
    return 0;
}

// Reads a sparse matrix and represents it using CSR (Compressed Sparse Row) format
void read_matrix(int **row_ptr, int **col_ind, float **values, const char *filename, int *num_rows, int *num_cols, int *num_vals) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        fprintf(stdout, "File cannot be opened!\n");
        exit(0);
    }
    
    // Get number of rows, columns, and non-zero values
    fscanf(file, "%d %d %d\n", num_rows, num_cols, num_vals);
    
    int *row_ptr_t = (int *) malloc((*num_rows + 1) * sizeof(int));
    int *col_ind_t = (int *) malloc(*num_vals * sizeof(int));
    float *values_t = (float *) malloc(*num_vals * sizeof(float));
    
    // Collect occurances of each row for determining the indices of row_ptr
    int *row_occurances = (int *) malloc(*num_rows * sizeof(int));
    for (int i = 0; i < *num_rows; i++) {
        row_occurances[i] = 0;
    }
    
    int row, column;
    float value;
    while (fscanf(file, "%d %d %f\n", &row, &column, &value) != EOF) {
        // Subtract 1 from row and column indices to match C format
        row--;
        column--;
        
        row_occurances[row]++;
    }
    
    // Set row_ptr
    int index = 0;
    for (int i = 0; i < *num_rows; i++) {
        row_ptr_t[i] = index;
        index += row_occurances[i];
    }
    row_ptr_t[*num_rows] = *num_vals;
    free(row_occurances);
    
    // Set the file position to the beginning of the file
    rewind(file);
    
    // Read the file again, save column indices and values
    for (int i = 0; i < *num_vals; i++) {
        col_ind_t[i] = -1;
    }
    
    fscanf(file, "%d %d %d\n", num_rows, num_cols, num_vals);
    int i = 0;
    while (fscanf(file, "%d %d %f\n", &row, &column, &value) != EOF) {
        row--;
        column--;
        
        // Find the correct index (i + row_ptr_t[row]) using both row information and an index i
        while (col_ind_t[i + row_ptr_t[row]] != -1) {
            i++;
        }
        col_ind_t[i + row_ptr_t[row]] = column;
        values_t[i + row_ptr_t[row]] = value;
        i = 0;
    }
    
    fclose(file);
    
    *row_ptr = row_ptr_t;
    *col_ind = col_ind_t;
    *values = values_t;
}

	.text
	.file	"main.hip"
	.globl	_Z23__device_stub__spmv_csrPKiS0_PKfiS2_Pf # -- Begin function _Z23__device_stub__spmv_csrPKiS0_PKfiS2_Pf
	.type	_Z23__device_stub__spmv_csrPKiS0_PKfiS2_Pf,@function
_Z23__device_stub__spmv_csrPKiS0_PKfiS2_Pf: # @_Z23__device_stub__spmv_csrPKiS0_PKfiS2_Pf
	.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	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	32(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	24(%rsp), %r8
	movq	%r9, (%r8)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%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	$_Z8spmv_csrPKiS0_PKfiS2_Pf, %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_end0:
	.size	_Z23__device_stub__spmv_csrPKiS0_PKfiS2_Pf, .Lfunc_end0-_Z23__device_stub__spmv_csrPKiS0_PKfiS2_Pf
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function 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	$152, %rsp
	.cfi_def_cfa_offset 208
	.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	$5, %edi
	jne	.LBB1_32
# %bb.1:
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, 64(%rsp)                  # 4-byte Spill
	movq	16(%rbx), %rdi
	callq	atoi
	movl	%eax, %r14d
	movq	24(%rbx), %rdi
	callq	atoi
	movl	%eax, 28(%rsp)                  # 4-byte Spill
	movq	32(%rbx), %rcx
	leaq	24(%rsp), %rax
	movq	%rax, (%rsp)
	leaq	104(%rsp), %rdi
	leaq	96(%rsp), %rsi
	leaq	88(%rsp), %rdx
	leaq	20(%rsp), %rbx
	leaq	148(%rsp), %r9
	movq	%rbx, %r8
	callq	_Z11read_matrixPPiS0_PPfPKcS_S_S_
	movslq	(%rbx), %r12
	leaq	(,%r12,4), %r15
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %rbx
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r15
	testq	%r12, %r12
	jle	.LBB1_4
# %bb.2:                                # %.lr.ph.preheader
	movl	%r12d, %r13d
	leaq	(,%r13,4), %rdx
	xorl	%ebp, %ebp
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
.LBB1_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$1065353216, (%rbx,%rbp,4)      # imm = 0x3F800000
	incq	%rbp
	cmpq	%rbp, %r13
	jne	.LBB1_3
.LBB1_4:                                # %._crit_edge
	cmpl	$1, 28(%rsp)                    # 4-byte Folded Reload
	jne	.LBB1_18
# %bb.5:
	movq	stdout(%rip), %rcx
	movl	$.L.str.1, %edi
	movl	$14, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	cmpl	$0, 24(%rsp)
	jle	.LBB1_8
# %bb.6:                                # %.lr.ph65
	movq	88(%rsp), %r12
	xorl	%r13d, %r13d
.LBB1_7:                                # =>This Inner Loop Header: Depth=1
	movq	stdout(%rip), %rdi
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r12,%r13,4), %xmm0
	movl	$.L.str.2, %esi
	movb	$1, %al
	callq	fprintf
	incq	%r13
	movslq	24(%rsp), %rax
	cmpq	%rax, %r13
	jl	.LBB1_7
.LBB1_8:                                # %._crit_edge66
	movq	stdout(%rip), %rcx
	movl	$.L.str.3, %edi
	movl	$24, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	cmpl	$0, 24(%rsp)
	jle	.LBB1_11
# %bb.9:                                # %.lr.ph69
	movq	96(%rsp), %r12
	xorl	%r13d, %r13d
.LBB1_10:                               # =>This Inner Loop Header: Depth=1
	movq	stdout(%rip), %rdi
	movl	(%r12,%r13,4), %edx
	movl	$.L.str.4, %esi
	xorl	%eax, %eax
	callq	fprintf
	incq	%r13
	movslq	24(%rsp), %rax
	cmpq	%rax, %r13
	jl	.LBB1_10
.LBB1_11:                               # %._crit_edge70
	movq	stdout(%rip), %rcx
	movl	$.L.str.5, %edi
	movl	$21, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	cmpl	$0, 20(%rsp)
	js	.LBB1_14
# %bb.12:                               # %.lr.ph74
	movq	$-1, %r12
	movq	104(%rsp), %r13
.LBB1_13:                               # =>This Inner Loop Header: Depth=1
	movq	stdout(%rip), %rdi
	movl	4(%r13,%r12,4), %edx
	movl	$.L.str.4, %esi
	xorl	%eax, %eax
	callq	fprintf
	movslq	20(%rsp), %rax
	incq	%r12
	cmpq	%rax, %r12
	jl	.LBB1_13
.LBB1_14:                               # %._crit_edge75
	movq	stdout(%rip), %rcx
	movl	$.L.str.6, %edi
	movl	$18, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	cmpl	$0, 20(%rsp)
	jle	.LBB1_17
# %bb.15:                               # %.lr.ph78.preheader
	xorl	%r12d, %r12d
.LBB1_16:                               # %.lr.ph78
                                        # =>This Inner Loop Header: Depth=1
	movq	stdout(%rip), %rdi
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx,%r12,4), %xmm0
	movl	$.L.str.7, %esi
	movb	$1, %al
	callq	fprintf
	incq	%r12
	movslq	20(%rsp), %rax
	cmpq	%rax, %r12
	jl	.LBB1_16
.LBB1_17:                               # %._crit_edge79
	movq	stdout(%rip), %rcx
	movl	$.L.str.8, %edi
	movl	$20, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	20(%rsp), %r12d
.LBB1_18:
	movslq	%r12d, %rax
	leaq	4(,%rax,4), %rsi
	leaq	80(%rsp), %rbp
	movq	%rbp, %rdi
	callq	hipMalloc
	movslq	24(%rsp), %rsi
	shlq	$2, %rsi
	leaq	72(%rsp), %r13
	movq	%r13, %rdi
	callq	hipMalloc
	movslq	24(%rsp), %rsi
	shlq	$2, %rsi
	leaq	56(%rsp), %rdi
	callq	hipMalloc
	movslq	20(%rsp), %rsi
	shlq	$2, %rsi
	leaq	48(%rsp), %rdi
	callq	hipMalloc
	movslq	20(%rsp), %rsi
	shlq	$2, %rsi
	leaq	32(%rsp), %rdi
	callq	hipMalloc
	leaq	68(%rsp), %rdi
	xorl	%r12d, %r12d
	movl	$63, %esi
	xorl	%edx, %edx
	callq	hipDeviceGetAttribute
	movq	(%rbp), %rdi
	movq	104(%rsp), %rsi
	movslq	20(%rsp), %rax
	leaq	4(,%rax,4), %rdx
	movq	%rsi, 128(%rsp)                 # 8-byte Spill
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r13), %rdi
	movq	96(%rsp), %rsi
	movslq	24(%rsp), %rdx
	shlq	$2, %rdx
	movq	%rsi, 120(%rsp)                 # 8-byte Spill
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	56(%rsp), %rax
	movq	(%rax), %rdi
	movq	88(%rsp), %rsi
	movslq	24(%rsp), %rdx
	shlq	$2, %rdx
	movq	%rsi, 112(%rsp)                 # 8-byte Spill
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	136(%rsp), %r13
	movq	%r13, %rdi
	callq	hipEventCreate
	leaq	40(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r13), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	testl	%r14d, %r14d
	jle	.LBB1_26
# %bb.19:                               # %.lr.ph86
	movabsq	$4294967296, %rbp               # imm = 0x100000000
	movl	64(%rsp), %r13d                 # 4-byte Reload
	btsq	$32, %r13
	movl	20(%rsp), %eax
.LBB1_20:                               # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_24 Depth 2
	movq	48(%rsp), %rdi
	movslq	%eax, %rdx
	shlq	$2, %rdx
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	32(%rsp), %rdi
	movslq	20(%rsp), %rdx
	shlq	$2, %rdx
	movq	%r15, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	68(%rsp), %edi
	shll	$5, %edi
	orq	%rbp, %rdi
	movl	$1, %esi
	movq	%r13, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_22
# %bb.21:                               #   in Loop: Header=BB1_20 Depth=1
	movq	80(%rsp), %rdi
	movq	72(%rsp), %rsi
	movq	56(%rsp), %rdx
	movl	20(%rsp), %ecx
	movq	48(%rsp), %r8
	movq	32(%rsp), %r9
	callq	_Z23__device_stub__spmv_csrPKiS0_PKfiS2_Pf
.LBB1_22:                               #   in Loop: Header=BB1_20 Depth=1
	movq	32(%rsp), %rsi
	movslq	20(%rsp), %rdx
	shlq	$2, %rdx
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movl	20(%rsp), %eax
	testl	%eax, %eax
	jle	.LBB1_25
# %bb.23:                               # %.lr.ph82.preheader
                                        #   in Loop: Header=BB1_20 Depth=1
	xorl	%ecx, %ecx
.LBB1_24:                               # %.lr.ph82
                                        #   Parent Loop BB1_20 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%r15,%rcx,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%rbx,%rcx,4)
	movl	$0, (%r15,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB1_24
.LBB1_25:                               # %._crit_edge83
                                        #   in Loop: Header=BB1_20 Depth=1
	incl	%r12d
	cmpl	%r14d, %r12d
	jne	.LBB1_20
.LBB1_26:                               # %._crit_edge87
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	40(%rsp), %rdi
	callq	hipEventSynchronize
	movq	136(%rsp), %rsi
	movq	40(%rsp), %rdx
	leaq	144(%rsp), %rdi
	callq	hipEventElapsedTime
	movl	28(%rsp), %eax                  # 4-byte Reload
	decl	%eax
	cmpl	$1, %eax
	ja	.LBB1_31
# %bb.27:                               # %.preheader
	cmpl	$0, 20(%rsp)
	jle	.LBB1_30
# %bb.28:                               # %.lr.ph89.preheader
	xorl	%r14d, %r14d
.LBB1_29:                               # %.lr.ph89
                                        # =>This Inner Loop Header: Depth=1
	movq	stdout(%rip), %rdi
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx,%r14,4), %xmm0
	movl	$.L.str.2, %esi
	movb	$1, %al
	callq	fprintf
	incq	%r14
	movslq	20(%rsp), %rax
	cmpq	%rax, %r14
	jl	.LBB1_29
.LBB1_30:                               # %._crit_edge90
	movq	stdout(%rip), %rsi
	movl	$10, %edi
	callq	fputc@PLT
.LBB1_31:
	movq	80(%rsp), %rdi
	callq	hipFree
	movq	72(%rsp), %rdi
	callq	hipFree
	movq	56(%rsp), %rdi
	callq	hipFree
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	128(%rsp), %rdi                 # 8-byte Reload
	callq	free
	movq	120(%rsp), %rdi                 # 8-byte Reload
	callq	free
	movq	112(%rsp), %rdi                 # 8-byte Reload
	callq	free
	xorl	%eax, %eax
	addq	$152, %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
.LBB1_32:
	.cfi_def_cfa_offset 208
	movq	stdout(%rip), %rcx
	movl	$.L.str, %edi
	movl	$113, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	xorl	%edi, %edi
	callq	exit
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z11read_matrixPPiS0_PPfPKcS_S_S_ # -- Begin function _Z11read_matrixPPiS0_PPfPKcS_S_S_
	.type	_Z11read_matrixPPiS0_PPfPKcS_S_S_,@function
_Z11read_matrixPPiS0_PPfPKcS_S_S_:      # @_Z11read_matrixPPiS0_PPfPKcS_S_S_
	.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, %r13
	movq	%r8, %rbp
	movq	%rdx, %rbx
	movq	%rsi, %r14
	movq	%rdi, %r15
	movl	$.L.str.10, %esi
	movq	%rcx, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB2_17
# %bb.1:
	movq	%rax, %r12
	movq	%r15, 48(%rsp)                  # 8-byte Spill
	movq	%r14, 56(%rsp)                  # 8-byte Spill
	movq	%rbx, 64(%rsp)                  # 8-byte Spill
	movq	128(%rsp), %rbx
	movl	$.L.str.12, %esi
	movq	%rax, %rdi
	movq	%rbp, %rdx
	movq	%r13, 40(%rsp)                  # 8-byte Spill
	movq	%r13, %rcx
	movq	%rbx, %r8
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movq	%rbp, 24(%rsp)                  # 8-byte Spill
	movslq	(%rbp), %r15
	leaq	4(,%r15,4), %rdi
	callq	malloc
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movslq	(%rbx), %rbx
	shlq	$2, %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %rbp
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	leaq	(,%r15,4), %rdi
	callq	malloc
	movq	%rax, %r14
	testq	%r15, %r15
	jle	.LBB2_3
# %bb.2:                                # %.lr.ph.preheader
	movl	%r15d, %edx
	shlq	$2, %rdx
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
.LBB2_3:                                # %._crit_edge
	leaq	4(%rsp), %rdx
	leaq	8(%rsp), %rcx
	leaq	12(%rsp), %r8
	movl	$.L.str.13, %esi
	movq	%r12, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$-1, %eax
	je	.LBB2_6
# %bb.4:
	leaq	4(%rsp), %rbx
	leaq	8(%rsp), %r15
	leaq	12(%rsp), %r13
.LBB2_5:                                # %.lr.ph69
                                        # =>This Inner Loop Header: Depth=1
	movslq	4(%rsp), %rax
	leaq	-1(%rax), %rcx
	decl	8(%rsp)
	movl	%ecx, 4(%rsp)
	incl	-4(%r14,%rax,4)
	movl	$.L.str.13, %esi
	movq	%r12, %rdi
	movq	%rbx, %rdx
	movq	%r15, %rcx
	movq	%r13, %r8
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$-1, %eax
	jne	.LBB2_5
.LBB2_6:                                # %.preheader
	movq	24(%rsp), %rax                  # 8-byte Reload
	movl	(%rax), %eax
	testl	%eax, %eax
	movq	16(%rsp), %rsi                  # 8-byte Reload
	jle	.LBB2_9
# %bb.7:                                # %.lr.ph72.preheader
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.LBB2_8:                                # %.lr.ph72
                                        # =>This Inner Loop Header: Depth=1
	movl	%edx, (%rsi,%rcx,4)
	addl	(%r14,%rcx,4), %edx
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB2_8
.LBB2_9:                                # %._crit_edge73
	movq	128(%rsp), %rbx
	movl	(%rbx), %ecx
	cltq
	movl	%ecx, (%rsi,%rax,4)
	movq	%r14, %rdi
	callq	free
	movq	%r12, %rdi
	callq	rewind
	movl	(%rbx), %edx
	testl	%edx, %edx
	movq	64(%rsp), %r13                  # 8-byte Reload
	jle	.LBB2_11
# %bb.10:                               # %.lr.ph76.preheader
	shlq	$2, %rdx
	movq	%rbp, %rdi
	movl	$255, %esi
	callq	memset@PLT
.LBB2_11:                               # %._crit_edge77
	movl	$.L.str.12, %esi
	movq	%r12, %rdi
	movq	24(%rsp), %rdx                  # 8-byte Reload
	movq	40(%rsp), %rcx                  # 8-byte Reload
	movq	%rbx, %r8
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	leaq	4(%rsp), %rdx
	leaq	8(%rsp), %rcx
	leaq	12(%rsp), %r8
	movl	$.L.str.13, %esi
	movq	%r12, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$-1, %eax
	je	.LBB2_16
# %bb.12:                               # %.lr.ph81.preheader
	leaq	4(%rsp), %r14
	leaq	8(%rsp), %r15
	leaq	12(%rsp), %rbx
.LBB2_13:                               # %.lr.ph81
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_14 Depth 2
	movslq	4(%rsp), %rcx
	leaq	-1(%rcx), %rax
	movl	%eax, 4(%rsp)
	movl	8(%rsp), %eax
	decl	%eax
	movl	%eax, 8(%rsp)
	movq	16(%rsp), %rdx                  # 8-byte Reload
	movslq	-4(%rdx,%rcx,4), %rcx
	shlq	$2, %rcx
.LBB2_14:                               #   Parent Loop BB2_13 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpl	$-1, (%rbp,%rcx)
	leaq	4(%rcx), %rcx
	jne	.LBB2_14
# %bb.15:                               #   in Loop: Header=BB2_13 Depth=1
	movl	%eax, -4(%rbp,%rcx)
	movss	12(%rsp), %xmm0                 # xmm0 = mem[0],zero,zero,zero
	movq	32(%rsp), %rax                  # 8-byte Reload
	movss	%xmm0, -4(%rax,%rcx)
	movl	$.L.str.13, %esi
	movq	%r12, %rdi
	movq	%r14, %rdx
	movq	%r15, %rcx
	movq	%rbx, %r8
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$-1, %eax
	jne	.LBB2_13
.LBB2_16:                               # %._crit_edge82
	movq	%r12, %rdi
	callq	fclose
	movq	48(%rsp), %rax                  # 8-byte Reload
	movq	16(%rsp), %rcx                  # 8-byte Reload
	movq	%rcx, (%rax)
	movq	56(%rsp), %rax                  # 8-byte Reload
	movq	%rbp, (%rax)
	movq	32(%rsp), %rax                  # 8-byte Reload
	movq	%rax, (%r13)
	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
.LBB2_17:
	.cfi_def_cfa_offset 128
	movq	stdout(%rip), %rcx
	movl	$.L.str.11, %edi
	movl	$23, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	xorl	%edi, %edi
	callq	exit
.Lfunc_end2:
	.size	_Z11read_matrixPPiS0_PPfPKcS_S_S_, .Lfunc_end2-_Z11read_matrixPPiS0_PPfPKcS_S_S_
	.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	$_Z8spmv_csrPKiS0_PKfiS2_Pf, %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	_Z8spmv_csrPKiS0_PKfiS2_Pf,@object # @_Z8spmv_csrPKiS0_PKfiS2_Pf
	.section	.rodata,"a",@progbits
	.globl	_Z8spmv_csrPKiS0_PKfiS2_Pf
	.p2align	3, 0x0
_Z8spmv_csrPKiS0_PKfiS2_Pf:
	.quad	_Z23__device_stub__spmv_csrPKiS0_PKfiS2_Pf
	.size	_Z8spmv_csrPKiS0_PKfiS2_Pf, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Invalid command, enter:\n1. number of threads, 2. number of repetitions, 3. print mode (1 or 2), 4. test filename\n"
	.size	.L.str, 114

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Values Array:\n"
	.size	.L.str.1, 15

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%.6f "
	.size	.L.str.2, 6

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"\n\nColumn Indices Array:\n"
	.size	.L.str.3, 25

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"\n\nRow Pointer Array:\n"
	.size	.L.str.5, 22

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"\n\nInitial Vector:\n"
	.size	.L.str.6, 19

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"%.1f "
	.size	.L.str.7, 6

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"\n\nResulting Vector:\n"
	.size	.L.str.8, 21

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"r"
	.size	.L.str.10, 2

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"File cannot be opened!\n"
	.size	.L.str.11, 24

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

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

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z8spmv_csrPKiS0_PKfiS2_Pf"
	.size	.L__unnamed_1, 27

	.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

	.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 _Z23__device_stub__spmv_csrPKiS0_PKfiS2_Pf
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z8spmv_csrPKiS0_PKfiS2_Pf
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z8spmv_csrPKiS0_PKfiS2_Pf ; -- Begin function _Z8spmv_csrPKiS0_PKfiS2_Pf
	.globl	_Z8spmv_csrPKiS0_PKfiS2_Pf
	.p2align	8
	.type	_Z8spmv_csrPKiS0_PKfiS2_Pf,@function
_Z8spmv_csrPKiS0_PKfiS2_Pf:             ; @_Z8spmv_csrPKiS0_PKfiS2_Pf
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x3c
	s_load_b32 s12, s[0:1], 0x18
	s_add_u32 s2, s0, 48
	s_addc_u32 s3, s1, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s13, s4, 0xffff
	s_mov_b32 s4, exec_lo
	v_mad_u64_u32 v[1:2], null, s15, s13, v[0:1]
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e64 s12, v1
	s_cbranch_execz .LBB0_7
; %bb.1:                                ; %.lr.ph30
	s_load_b32 s14, s[2:3], 0x0
	s_clause 0x2
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[2:3], s[0:1], 0x10
	s_load_b128 s[8:11], s[0:1], 0x20
	s_waitcnt lgkmcnt(0)
	s_mul_i32 s1, s14, s13
	s_mov_b32 s13, 0
.LBB0_2:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_4 Depth 2
	v_ashrrev_i32_e32 v2, 31, v1
	v_mov_b32_e32 v0, 0
	s_mov_b32 s14, exec_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[1:2]
	v_add_co_u32 v4, vcc_lo, s4, v2
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v5, vcc_lo, s5, v3, vcc_lo
	global_load_b64 v[4:5], v[4:5], off
	s_waitcnt vmcnt(0)
	v_cmpx_lt_i32_e64 v4, v5
	s_cbranch_execz .LBB0_6
; %bb.3:                                ; %.lr.ph.preheader
                                        ;   in Loop: Header=BB0_2 Depth=1
	v_ashrrev_i32_e32 v7, 31, v4
	v_mov_b32_e32 v6, v4
	v_mov_b32_e32 v0, 0
	s_mov_b32 s15, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[8:9], 2, v[6:7]
	v_add_co_u32 v6, vcc_lo, s2, v8
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v7, vcc_lo, s3, v9, vcc_lo
	v_add_co_u32 v8, vcc_lo, s6, v8
	v_add_co_ci_u32_e32 v9, vcc_lo, s7, v9, vcc_lo
.LBB0_4:                                ; %.lr.ph
                                        ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	global_load_b32 v10, v[8:9], off
	v_add_nc_u32_e32 v4, 1, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ge_i32_e64 s0, v4, v5
	s_or_b32 s15, s0, s15
	s_waitcnt vmcnt(0)
	v_ashrrev_i32_e32 v11, 31, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[10:11], 2, v[10:11]
	v_add_co_u32 v10, vcc_lo, s8, v10
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v11, vcc_lo, s9, v11, vcc_lo
	global_load_b32 v12, v[6:7], off
	global_load_b32 v10, v[10:11], off
	v_add_co_u32 v6, vcc_lo, v6, 4
	v_add_co_ci_u32_e32 v7, vcc_lo, 0, v7, vcc_lo
	v_add_co_u32 v8, vcc_lo, v8, 4
	v_add_co_ci_u32_e32 v9, vcc_lo, 0, v9, vcc_lo
	s_waitcnt vmcnt(0)
	v_fmac_f32_e32 v0, v12, v10
	s_and_not1_b32 exec_lo, exec_lo, s15
	s_cbranch_execnz .LBB0_4
; %bb.5:                                ; %Flow
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s15
.LBB0_6:                                ; %Flow52
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	s_or_b32 exec_lo, exec_lo, s14
	v_add_nc_u32_e32 v1, s1, v1
	v_add_co_u32 v2, s0, s10, v2
	v_add_co_ci_u32_e64 v3, s0, s11, v3, s0
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	v_cmp_le_i32_e32 vcc_lo, s12, v1
	global_store_b32 v[2:3], v0, off
	s_or_b32 s13, vcc_lo, s13
	s_and_not1_b32 exec_lo, exec_lo, s13
	s_cbranch_execnz .LBB0_2
.LBB0_7:                                ; %Flow54
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z8spmv_csrPKiS0_PKfiS2_Pf
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 304
		.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 13
		.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	_Z8spmv_csrPKiS0_PKfiS2_Pf, .Lfunc_end0-_Z8spmv_csrPKiS0_PKfiS2_Pf
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 428
; NumSgprs: 18
; NumVgprs: 13
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 13
; Occupancy: 16
; WaveLimiterHint : 1
; 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
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         32
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         40
        .size:           8
        .value_kind:     global_buffer
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         52
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         60
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         62
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         64
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         66
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         68
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         70
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         112
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 304
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z8spmv_csrPKiS0_PKfiS2_Pf
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z8spmv_csrPKiS0_PKfiS2_Pf.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     13
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
