Menu

(Solved) : Include Include Include Include Include Mainh Define Maxfilenamesize 256 Define Maxnumleng Q43956586 . . .

#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 theprogram to make

sure it is correct. If the number of input parameters isincorrect, 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 n”, argv[0]);

exit(EXIT_FAILURE);

}

}

/* This function prints out information about a sparsematrix

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,”—————————————————–n”);

fprintf(stdout, “Matrix name: %sn”, fileName);

fprintf(stdout, “Matrix size: %d x %d => %dn”, m, n,nnz);

fprintf(stdout,”—————————————————–n”);

fprintf(stdout, “Is matrix: %dn”, mm_is_matrix(matcode));

fprintf(stdout, “Is sparse: %dn”, mm_is_sparse(matcode));

fprintf(stdout,”—————————————————–n”);

fprintf(stdout, “Is complex: %dn”, mm_is_complex(matcode));

fprintf(stdout, “Is real: %dn”, mm_is_real(matcode));

fprintf(stdout, “Is integer: %dn”, mm_is_integer(matcode));

fprintf(stdout, “Is pattern only: %dn”,mm_is_pattern(matcode));

fprintf(stdout,”—————————————————–n”);

fprintf(stdout, “Is general: %dn”, mm_is_general(matcode));

fprintf(stdout, “Is symmetric: %dn”,mm_is_symmetric(matcode));

fprintf(stdout, “Is skewed: %dn”, mm_is_skew(matcode));

fprintf(stdout, “Is hermitian: %dn”,mm_is_hermitian(matcode));

fprintf(stdout,”—————————————————–n”);

}

/* This function checks the return value from the matrix readfunction,

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.n”);

exit(EXIT_FAILURE);

break;

case MM_PREMATURE_EOF:

fprintf(stderr, “Premature EOF (not enough values in aline).n”);

exit(EXIT_FAILURE);

break;

case MM_NOT_MTX:

fprintf(stderr, “Not Matrix Market format.n”);

exit(EXIT_FAILURE);

break;

case MM_NO_HEADER:

fprintf(stderr, “No header information.n”);

exit(EXIT_FAILURE);

break;

case MM_UNSUPPORTED_TYPE:

fprintf(stderr, “Unsupported type (not a matrix).n”);

exit(EXIT_FAILURE);

break;

case MM_LINE_TOO_LONG:

fprintf(stderr, “Too many values in a line.n”);

exit(EXIT_FAILURE);

break;

case MM_COULD_NOT_WRITE_FILE:

fprintf(stderr, “Error writing to a file.n”);

exit(EXIT_FAILURE);

break;

case 0:

fprintf(stdout, “file loaded.n”);

break;

default:

fprintf(stdout, “Error – should not be here.n”);

exit(EXIT_FAILURE);

break;

}

}

/* This function reads information about a sparse matrix usingthe

mm_read_banner() function and prints out information usingthe

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: %sn”, fileName);

exit(EXIT_FAILURE);

}

if(mm_read_banner(fp, &matcode) != 0)

{

fprintf(stderr, “Error processing Matrix Market banner.n”);

exit(EXIT_FAILURE);

}

if(mm_read_mtx_crd_size(fp, &m, &n, &nnz) != 0){

fprintf(stderr, “Error reading size.n”);

exit(EXIT_FAILURE);

}

print_matrix_info(fileName, matcode, m, n, nnz);

fclose(fp);

}

/* This function coverts a sparse matrix stored in COO format toCSR.

input parameters:

these are ‘consumed’ by this function

int* row_ind       row index forthe non-zeros in COO

int* col_ind       columnindex for the non-zeros in COO

double* val       values forthe non-zeros in COO

int m         # ofrows in the matrix

int n         # ofcolumns in the matrix

int nnz       # of non-zerosin the matrix

these are ‘produced’ by this function

unsigned int** csr_row_ptr   row pointers tocsr_col_ind and

csr_vals in CSR

unsigned int** csr_col_ind   column index for thenon-zeros in CSR

double** csr_vals      values forthe 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 function reads in a vector from a text file, similar informat to

the Matrix Market format.

The first line contains the number of elements in thevector.

The rest of the file contains the values in the vector, oneelement per row.

input parameters:

char* fileName    Name of the filecontaining the vector

double** vector      Array thatwill contain the vector

int* vecSize     Integer variable thatwill contain the size of

the vector

return parameters:

none

*/

void read_vector(char* fileName, double** vector, int*vecSize)

{

FILE* fp = fopen(fileName, “r”);

assert(fp);

char line[MAX_NUM_LENGTH];

fgets(line, MAX_NUM_LENGTH, fp);

fclose(fp);

unsigned int vector_size = atoi(line);

double* vector_ = (double*) malloc(sizeof(double) *vector_size);

fp = fopen(fileName, “r”);

assert(fp);

// first read the first line to get the # elements

fgets(line, MAX_NUM_LENGTH, fp);

unsigned int index = 0;

while(fgets(line, MAX_NUM_LENGTH, fp) != NULL) {

vector_[index] = atof(line);

index++;

}

fclose(fp);

assert(index == vector_size);

*vector = vector_;

*vecSize = vector_size;

}

/* This function calculates the sparse matrix-vector multiplyfrom the matrix

in CSR format (i.e., csr_row_ptr, csr_col_ind, and csr_vals) andthe vector

in an array (i.e., vector_x). It stores the result in anotherarray (i.e.,

res)

input parameters:

these are ‘consumed’ by this function

unsigned int** csr_row_ptr   row pointers tocsr_col_ind and

csr_vals in CSR

unsigned int** csr_col_ind   column index for thenon-zeros in CSR

double** csr_vals      values forthe non-zeros in CSR

int m         # ofrows in the matrix

int n         # ofcolumns in the matrix

int nnz       # of non-zerosin the matrix

double vector_x      inputvector

these are ‘produced’ by this function

double* res       Result ofSpMV. res = A * x, where

A is stored in CSR format and x is

stored in vector_x

return parameters:

none

*/

void spmv(unsigned int* csr_row_ptr, unsigned int*csr_col_ind,

double* csr_vals, int m, int n, int nnz,

double* vector_x, double* res)

{

}

/* This function stores a vector in a text file, similar informat to

the Matrix Market format.

The first line contains the number of elements in thevector.

The rest of the file contains the values in the vector, oneelement per row.

input parameters:

char* fileName    Name of the file that willcontain the vector

double** res        Array that containsthe vector

int* m       Integer variablethat contains the size of

the vector

return parameters:

none

*/

void store_result(char *fileName, double* res, int m)

{

FILE* fp = fopen(fileName, “w”);

assert(fp);

fprintf(fp, “%dn”, m);

for(int i = 0; i < m; i++) {

fprintf(fp, “%0.20fn”, res[i]);

}

fclose(fp);

}

/* This program first reads in a sparse matrix stored in matrixmarket 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 inthe matrix,

respectively. This is also known as the co-ordinate format.

Then, it should convert this matrix stored in co-ordinate formatto the

compressed sparse row (CSR) format.

Then, finally it should use the CSR format to multiply thematrix with a

vector (i.e., calculate the sparse matrix vector multiply, orSpMV).

The resulting vector should then be stored in a file, one valueper line,

whose name was specified as an input to the program.

*/

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, andval,

// 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 asparse

matrix in Matrix Market format and stores the matrix in COOformat.

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 inrow_ind[0], and

col_ind[0], respectively, and the value of the non-zero isstored 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, “donen”);

// Load the vector file

char vectorName[MAX_FILENAME_SIZE];

strcpy(vectorName, argv[2]);

fprintf(stdout, “Vector file name: %s … “, vectorName);

double* vector_x;

unsigned int vector_size;

read_vector(vectorName, &vector_x, &vector_size);

assert(n == vector_size);

fprintf(stdout, “file loadedn”);

// Calculate SpMV

double *res = (double*) malloc(sizeof(double) * m);;

assert(res);

fprintf(stdout, “Calculating SpMV … “);

spmv(csr_row_ptr, csr_col_ind, csr_vals, m, n, nnz, vector_x,res);

fprintf(stdout, “donen”);

// Store the calculated vector in a file, one element perline.

char resName[MAX_FILENAME_SIZE];

strcpy(resName, argv[3]);

fprintf(stdout, “Result file name: %s … “, resName);

store_result(resName, res, m);

fprintf(stdout, “file savedn”);

// 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;

}

Implement convert_coo_to_csr and spmv, please do not addany functions or change any given code.

Expert Answer


Answer to #include #include #include #include #include “main.h” #define MAX_FILENAME_SIZE 256 #define MAX_NUM_LENGTH 100 /* This f…

OR