(Solved) : Instructions 1 Assignment Implementing Sparse Matrix Vector Multiply 2 Provided Following Q44027086 . . .
Instructions:
1. In this assignment, you will be implementing a sparsematrix-vector multiply.
2. We have provided the following functionality:
a) reading a sparse matrix stored in Matrix Market format (i.e.,mm_read_mtx_crd() function).
b) reading and printing out information about the matrix (i.e.,read_info() and print_matrix_info() functions).
c) reading in a vector in a format similar to Matrix Marketformat (i.e., read_vector() function).
d) writing out the vector in a format similar to Matrix Marketformat (i.e., store_result() function).
Read these functions to understand what they are doing.
You do NOT have to read the functions in mmio.c (although youare welcome to, if you wish).
3. Read the descriptions and implement the followingfunctions:
a) convert_coo_to_csr(row_ind, col_ind, val, m, n, nnz,&csr_row_ptr, &csr_col_ind, &csr_vals);
b) spmv(csr_row_ptr, csr_col_ind, csr_vals, m, n, nnz, vector_x,res);
4. Test the functions on the two given sets of input files,stored in test1 and test2 directories.
a) A.mtx is the sparse matrix.
b) x.mtx is the vector that you are multiplying the matrix with(i.e., A * x)
c) ans.mtx is the answer to A*x. Your results should beidentical to ans.mtx.
5. Thing to note:
a) Do not change ANY of the provided skeletoncode, including the header of the functions that you are requiredto implement.
b) Every function in the file (listed in 3. above) must beimplemented to provide the described functionality.
c) Do not ADD any new functions..
d) Do NOT hard-code the file names. This willresult in an automatic 0 for the four test files that will be usedfor grading (see the rubric for more detail).
e) Make sure your code compiles and runs on ix-dev with -std=c11flag.
Please implement the functions
1.convert_coo_to_csr(row_ind, col_ind, val, m, n, nnz,&csr_row_ptr, &csr_col_ind, &csr_vals);
2. spmv(csr_row_ptr, csr_col_ind, csr_vals, m, n, nnz,vector_x, res);
Please do not change any given code or add morefunctions.
mmio.c can be found in:http://math.nist.gov/MatrixMarket
Code to copy:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#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 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 n”, argv[0]);
exit(EXIT_FAILURE);
}
}
/* 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 for thenon-zeros in COO
int* col_ind column index for thenon-zeros in COO
double* val values for the non-zerosin COO
int m # of rowsin the matrix
int n # ofcolumns in the matrix
int nnz # of non-zeros in thematrix
these are ‘produced’ by this function
unsigned int** csr_row_ptr row pointers to csr_col_indand
csr_vals in CSR
unsigned int** csr_col_ind column index for thenon-zeros in CSR
double** csr_vals values for thenon-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 the vector.
The rest of the file contains the values in the vector, one elementper row.
input parameters:
char* fileName Name of the file containing thevector
double** vector Array that willcontain 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 another array(i.e.,
res)
input parameters:
these are ‘consumed’ by this function
unsigned int** csr_row_ptr row pointers to csr_col_indand
csr_vals in CSR
unsigned int** csr_col_ind column index for thenon-zeros in CSR
double** csr_vals values for thenon-zeros in CSR
int m # of rowsin the matrix
int n # ofcolumns in the matrix
int nnz # of non-zeros in thematrix
double vector_x input vector
these are ‘produced’ by this function
double* res Result of SpMV. 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 in formatto
the Matrix Market format.
The first line contains the number of elements in the vector.
The rest of the file contains the values in the vector, one elementper row.
input parameters:
char* fileName Name of the file that will contain thevector
double** res Array that containsthe vector
int* m Integer variable thatcontains 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 in thematrix,
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 is storedin
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;
}
Expert Answer
Answer to Instructions: 1. In this assignment, you will be implementing a sparse matrix-vector multiply. 2. We have provided the f…
OR