Question
I need help with the function void convert_coo_to_csr(int* row_ind, int* col_ind, double* val, int m, int n, int nnz, unsigned int** csr_row_ptr, unsigned int** csr_col_ind,
I need help with the function void convert_coo_to_csr(int* row_ind, int* col_ind, double* val, int m, int n, int nnz, unsigned int** csr_row_ptr, unsigned int** csr_col_ind, double** csr_vals). The input files are .mtx
#include #include #include #include #include "main.h"
#define MAX_FILENAME_SIZE 256 #define MAX_NUM_LENGTH 100
/* This function checks the number of input parameters to the program to make sure it is correct. If the number of input parameters is incorrect, it prints out a message on how to properly use the program. input parameters: int argc char** argv return parameters: none */ void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s ", argv[0]); exit(EXIT_FAILURE); } }
/* This function prints out information about a sparse matrix input parameters: char* fileName name of the sparse matrix file MM_typecode matcode matrix information int m # of rows int n # of columns int nnz # of non-zeros return paramters: none */ void print_matrix_info(char* fileName, MM_typecode matcode, int m, int n, int nnz) { fprintf(stdout, "----------------------------------------------------- "); fprintf(stdout, "Matrix name: %s ", fileName); fprintf(stdout, "Matrix size: %d x %d => %d ", m, n, nnz); fprintf(stdout, "----------------------------------------------------- "); fprintf(stdout, "Is matrix: %d ", mm_is_matrix(matcode)); fprintf(stdout, "Is sparse: %d ", mm_is_sparse(matcode)); fprintf(stdout, "----------------------------------------------------- "); fprintf(stdout, "Is complex: %d ", mm_is_complex(matcode)); fprintf(stdout, "Is real: %d ", mm_is_real(matcode)); fprintf(stdout, "Is integer: %d ", mm_is_integer(matcode)); fprintf(stdout, "Is pattern only: %d ", mm_is_pattern(matcode)); fprintf(stdout, "----------------------------------------------------- "); fprintf(stdout, "Is general: %d ", mm_is_general(matcode)); fprintf(stdout, "Is symmetric: %d ", mm_is_symmetric(matcode)); fprintf(stdout, "Is skewed: %d ", mm_is_skew(matcode)); fprintf(stdout, "Is hermitian: %d ", mm_is_hermitian(matcode)); fprintf(stdout, "----------------------------------------------------- ");
}
/* This function checks the return value from the matrix read function, mm_read_mtx_crd(), and provides descriptive information. input parameters: int ret return value from the mm_read_mtx_crd() function return paramters: none */ void check_mm_ret(int ret) { switch(ret) { case MM_COULD_NOT_READ_FILE: fprintf(stderr, "Error reading file. "); exit(EXIT_FAILURE); break; case MM_PREMATURE_EOF: fprintf(stderr, "Premature EOF (not enough values in a line). "); exit(EXIT_FAILURE); break; case MM_NOT_MTX: fprintf(stderr, "Not Matrix Market format. "); exit(EXIT_FAILURE); break; case MM_NO_HEADER: fprintf(stderr, "No header information. "); exit(EXIT_FAILURE); break; case MM_UNSUPPORTED_TYPE: fprintf(stderr, "Unsupported type (not a matrix). "); exit(EXIT_FAILURE); break; case MM_LINE_TOO_LONG: fprintf(stderr, "Too many values in a line. "); exit(EXIT_FAILURE); break; case MM_COULD_NOT_WRITE_FILE: fprintf(stderr, "Error writing to a file. "); exit(EXIT_FAILURE); break; case 0: fprintf(stdout, "file loaded. "); break; default: fprintf(stdout, "Error - should not be here. "); exit(EXIT_FAILURE); break;
} }
/* This function reads information about a sparse matrix using the mm_read_banner() function and prints out information using the print_matrix_info() function. input parameters: char* fileName name of the sparse matrix file return paramters: none */ void read_info(char* fileName) { FILE* fp; MM_typecode matcode; int m; int n; int nnz;
if((fp = fopen(fileName, "r")) == NULL) { fprintf(stderr, "Error opening file: %s ", fileName); exit(EXIT_FAILURE); }
if(mm_read_banner(fp, &matcode) != 0) { fprintf(stderr, "Error processing Matrix Market banner. "); exit(EXIT_FAILURE); }
if(mm_read_mtx_crd_size(fp, &m, &n, &nnz) != 0) { fprintf(stderr, "Error reading size. "); exit(EXIT_FAILURE); }
print_matrix_info(fileName, matcode, m, n, nnz);
fclose(fp); }
/* This function coverts a sparse matrix stored in COO format to CSR. input parameters: these are 'consumed' by this function int* row_ind row index for the non-zeros in COO int* col_ind column index for the non-zeros in COO double* val values for the non-zeros in COO int m # of rows in the matrix int n # of columns in the matrix int nnz # of non-zeros in the matrix
these are 'produced' by this function unsigned int** csr_row_ptr row pointers to csr_col_ind and csr_vals in CSR unsigned int** csr_col_ind column index for the non-zeros in CSR double** csr_vals values for the non-zeros in CSR return parameters: none */ void convert_coo_to_csr(int* row_ind, int* col_ind, double* val, int m, int n, int nnz, unsigned int** csr_row_ptr, unsigned int** csr_col_ind, double** csr_vals)
{ }
/* This program first reads in a sparse matrix stored in matrix market format (mtx). It generates a set of arrays - row_ind, col_ind, and val, which stores the row/column index and the value for the non-zero elements in the matrix, respectively. This is also known as the co-ordinate format.
Then, it should convert this matrix stored in co-ordinate format to the compressed sparse row (CSR) format. */
int main(int argc, char** argv) { usage(argc, argv);
// Read the sparse matrix file name char matrixName[MAX_FILENAME_SIZE]; strcpy(matrixName, argv[1]); read_info(matrixName);
// Read the sparse matrix and store it in row_ind, col_ind, and val, // also known as co-ordinate format. int ret; MM_typecode matcode; int m; int n; int nnz; int *row_ind; int *col_ind; double *val;
fprintf(stdout, "Matrix file name: %s ... ", matrixName); /* mm_read_mtx_crs is a fucntion provided in mmio.c that reads in a sparse matrix in Matrix Market format and stores the matrix in COO format. m - # of rows n - # of columns nnz - number of non-zeroes row_ind - array of row indices for the non-zeros col_ind - array of column indices for the non-zeros val - array of values for the non-nzeros matcode - return value for the function
The first non-zero's row and column indices are stored in row_ind[0], and col_ind[0], respectively, and the value of the non-zero is stored in va[0]. Therefore, the size of these arrays are equal to nnz. */ ret = mm_read_mtx_crd(matrixName, &m, &n, &nnz, &row_ind, &col_ind, &val, &matcode); check_mm_ret(ret);
// Convert co-ordinate format to CSR format fprintf(stdout, "Converting COO to CSR..."); unsigned int* csr_row_ptr = NULL; unsigned int* csr_col_ind = NULL; double* csr_vals = NULL; convert_coo_to_csr(row_ind, col_ind, val, m, n, nnz, &csr_row_ptr, &csr_col_ind, &csr_vals); fprintf(stdout, "done ");
// Free memory free(csr_row_ptr); free(csr_col_ind); free(csr_vals);
free(vector_x); free(res);
free(row_ind); free(col_ind); free(val);
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started