| 1 | /*
|
|---|
| 2 | * Matrix Market I/O library for ANSI C
|
|---|
| 3 | *
|
|---|
| 4 | * See http://math.nist.gov/MatrixMarket for details.
|
|---|
| 5 | *
|
|---|
| 6 | *
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | #ifndef MM_IO_H
|
|---|
| 10 | #define MM_IO_H
|
|---|
| 11 |
|
|---|
| 12 | #define MM_MAX_LINE_LENGTH 1025
|
|---|
| 13 | #define MatrixMarketBanner "%%MatrixMarket"
|
|---|
| 14 | #define MM_MAX_TOKEN_LENGTH 64
|
|---|
| 15 |
|
|---|
| 16 | typedef char MM_typecode[4];
|
|---|
| 17 |
|
|---|
| 18 | char *mm_typecode_to_str(MM_typecode matcode);
|
|---|
| 19 |
|
|---|
| 20 | /** @brief reads banner of a file in MM fomat and sets the matcode
|
|---|
| 21 | * @param f filename of file containing the matrix.
|
|---|
| 22 | * @param matcode output matcode of the matrix.
|
|---|
| 23 | * @return 0 on success -1 on failure.
|
|---|
| 24 | */
|
|---|
| 25 | int mm_read_banner(FILE *f, MM_typecode *matcode);
|
|---|
| 26 | int mm_read_mtx_crd_size(FILE *f, int *M, int *N, int *nz);
|
|---|
| 27 | int mm_read_mtx_array_size(FILE *f, int *M, int *N);
|
|---|
| 28 |
|
|---|
| 29 | int mm_write_banner(FILE *f, MM_typecode matcode);
|
|---|
| 30 | int mm_write_mtx_crd_size(FILE *f, int M, int N, int nz);
|
|---|
| 31 | int mm_write_mtx_array_size(FILE *f, int M, int N);
|
|---|
| 32 | /** @brief reads size of RHS matrix in MM format from a file
|
|---|
| 33 | * @param fname filename of file containing the matrix.
|
|---|
| 34 | * @param M_ output #rows.
|
|---|
| 35 | * @param N_ output #columns.
|
|---|
| 36 | * @return 0 on success -1 on failure.
|
|---|
| 37 | */
|
|---|
| 38 | int mm_read_rhs_crd_size(FILE *f, int *M, int *N);
|
|---|
| 39 | /** @brief reads an RHS matrix in MM format from a file
|
|---|
| 40 | * @param fname filename of fiel containing the matrix.
|
|---|
| 41 | * @param M_ output #rows.
|
|---|
| 42 | * @param N_ output #columns.
|
|---|
| 43 | * @param val_ output values of MM matrix.
|
|---|
| 44 | * @return 0 on success -1 on failure.
|
|---|
| 45 | */
|
|---|
| 46 | int mm_read_rhs(const char *fname, int *M_, int *N_, double **val_);
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | /********************* MM_typecode query fucntions ***************************/
|
|---|
| 50 |
|
|---|
| 51 | #define mm_is_matrix(typecode) ((typecode)[0]=='M')
|
|---|
| 52 |
|
|---|
| 53 | #define mm_is_sparse(typecode) ( ((typecode)[1]=='C') || ((typecode)[1]=='S') )
|
|---|
| 54 | #define mm_is_sparserow(typecode) ((typecode)[1]=='S')
|
|---|
| 55 | //#define mm_is_sparse(typecode) ((typecode)[1]=='C')
|
|---|
| 56 | #define mm_is_coordinate(typecode)((typecode)[1]=='C')
|
|---|
| 57 | #define mm_is_dense(typecode) ((typecode)[1]=='A')
|
|---|
| 58 | #define mm_is_array(typecode) ((typecode)[1]=='A')
|
|---|
| 59 |
|
|---|
| 60 | #define mm_is_complex(typecode) ((typecode)[2]=='C')
|
|---|
| 61 | #define mm_is_real(typecode) ((typecode)[2]=='R')
|
|---|
| 62 | #define mm_is_pattern(typecode) ((typecode)[2]=='P')
|
|---|
| 63 | #define mm_is_integer(typecode) ((typecode)[2]=='I')
|
|---|
| 64 |
|
|---|
| 65 | #define mm_is_symmetric(typecode)((typecode)[3]=='S')
|
|---|
| 66 | #define mm_is_general(typecode) ((typecode)[3]=='G')
|
|---|
| 67 | #define mm_is_skew(typecode) ((typecode)[3]=='K')
|
|---|
| 68 | #define mm_is_hermitian(typecode)((typecode)[3]=='H')
|
|---|
| 69 |
|
|---|
| 70 | int mm_is_valid(MM_typecode matcode); /* too complex for a macro */
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | /********************* MM_typecode modify fucntions ***************************/
|
|---|
| 74 |
|
|---|
| 75 | #define mm_set_matrix(typecode) ((*typecode)[0]='M')
|
|---|
| 76 | #define mm_set_coordinate(typecode) ((*typecode)[1]='C')
|
|---|
| 77 | #define mm_set_sparserow(typecode) ((*typecode)[1]='S')
|
|---|
| 78 | #define mm_set_array(typecode) ((*typecode)[1]='A')
|
|---|
| 79 | #define mm_set_dense(typecode) mm_set_array(typecode)
|
|---|
| 80 | #define mm_set_sparse(typecode) mm_set_coordinate(typecode)
|
|---|
| 81 |
|
|---|
| 82 | #define mm_set_complex(typecode)((*typecode)[2]='C')
|
|---|
| 83 | #define mm_set_real(typecode) ((*typecode)[2]='R')
|
|---|
| 84 | #define mm_set_pattern(typecode)((*typecode)[2]='P')
|
|---|
| 85 | #define mm_set_integer(typecode)((*typecode)[2]='I')
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | #define mm_set_symmetric(typecode)((*typecode)[3]='S')
|
|---|
| 89 | #define mm_set_general(typecode)((*typecode)[3]='G')
|
|---|
| 90 | #define mm_set_skew(typecode) ((*typecode)[3]='K')
|
|---|
| 91 | #define mm_set_hermitian(typecode)((*typecode)[3]='H')
|
|---|
| 92 |
|
|---|
| 93 | #define mm_clear_typecode(typecode) ((*typecode)[0]=(*typecode)[1]= \
|
|---|
| 94 | (*typecode)[2]=' ',(*typecode)[3]='G')
|
|---|
| 95 |
|
|---|
| 96 | #define mm_initialize_typecode(typecode) mm_clear_typecode(typecode)
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | /********************* Matrix Market error codes ***************************/
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | #define MM_COULD_NOT_READ_FILE 11
|
|---|
| 103 | #define MM_PREMATURE_EOF 12
|
|---|
| 104 | #define MM_NOT_MTX 13
|
|---|
| 105 | #define MM_NO_HEADER 14
|
|---|
| 106 | #define MM_UNSUPPORTED_TYPE 15
|
|---|
| 107 | #define MM_LINE_TOO_LONG 16
|
|---|
| 108 | #define MM_COULD_NOT_WRITE_FILE 17
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 | /******************** Matrix Market internal definitions ********************
|
|---|
| 112 |
|
|---|
| 113 | MM_matrix_typecode: 4-character sequence
|
|---|
| 114 |
|
|---|
| 115 | ojbect sparse/ data storage
|
|---|
| 116 | dense type scheme
|
|---|
| 117 |
|
|---|
| 118 | string position: [0] [1] [2] [3]
|
|---|
| 119 |
|
|---|
| 120 | Matrix typecode: M(atrix) C(oord) R(eal) G(eneral)
|
|---|
| 121 | A(array) C(omplex) H(ermitian)
|
|---|
| 122 | P(attern) S(ymmetric)
|
|---|
| 123 | I(nteger) K(kew)
|
|---|
| 124 |
|
|---|
| 125 | ***********************************************************************/
|
|---|
| 126 |
|
|---|
| 127 | #define MM_MTX_STR "matrix"
|
|---|
| 128 | #define MM_ARRAY_STR "array"
|
|---|
| 129 | #define MM_DENSE_STR "array"
|
|---|
| 130 | #define MM_COORDINATE_STR "coordinate"
|
|---|
| 131 | #define MM_SPARSEROW_STR "sparserow"
|
|---|
| 132 | #define MM_SPARSE_STR "coordinate"
|
|---|
| 133 | #define MM_COMPLEX_STR "complex"
|
|---|
| 134 | #define MM_REAL_STR "real"
|
|---|
| 135 | #define MM_INT_STR "integer"
|
|---|
| 136 | #define MM_GENERAL_STR "general"
|
|---|
| 137 | #define MM_SYMM_STR "symmetric"
|
|---|
| 138 | #define MM_HERM_STR "hermitian"
|
|---|
| 139 | #define MM_SKEW_STR "skew-symmetric"
|
|---|
| 140 | #define MM_PATTERN_STR "pattern"
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | /* high level routines */
|
|---|
| 144 | int mm_write_mtx_crd(char fname[], int M, int N, int nz, int I[], int J[],
|
|---|
| 145 | double val[], MM_typecode matcode);
|
|---|
| 146 | int mm_read_mtx_crd_data(FILE *f, int M, int N, int nz, int I[], int J[],
|
|---|
| 147 | double val[], MM_typecode matcode);
|
|---|
| 148 | int mm_read_mtx_crd_entry(FILE *f, int *I, int *J, double *real, double *img,
|
|---|
| 149 | MM_typecode matcode);
|
|---|
| 150 |
|
|---|
| 151 | /** @brief reads an unsymmetric sparse matrix in MM format from a file
|
|---|
| 152 | * @param fname filename of fiel containing the matrix.
|
|---|
| 153 | * @param M_ output #rows.
|
|---|
| 154 | * @param N_ output #columns.
|
|---|
| 155 | * @param nz_ output #non zeros.
|
|---|
| 156 | * @param val_ output values of MM matrix.
|
|---|
| 157 | * @param I_ row values of MM matrix.
|
|---|
| 158 | * @param J_ column values of MM matrix.
|
|---|
| 159 | * @return 0 on success -1 on failure.
|
|---|
| 160 | */
|
|---|
| 161 | int mm_read_unsymmetric_sparse(const char *fname, int *M_, int *N_, int *nz_,
|
|---|
| 162 | double **val_, int **I_, int **J_);
|
|---|
| 163 | /** @brief reads an symmetric sparse matrix in MM format from a file
|
|---|
| 164 | * @param fname filename of fiel containing the matrix.
|
|---|
| 165 | * @param M_ output #rows.
|
|---|
| 166 | * @param N_ output #columns.
|
|---|
| 167 | * @param nz_ output #non zeros.
|
|---|
| 168 | * @param val_ output values of MM matrix.
|
|---|
| 169 | * @param I_ output row values of MM matrix.
|
|---|
| 170 | * @param J_ output column values of MM matrix.
|
|---|
| 171 | * @return 0 on success -1 on failure.
|
|---|
| 172 | */
|
|---|
| 173 | int mm_read_symmetric_sparse(const char *fname, int *M_, int *N_, int *nz_,
|
|---|
| 174 | double **val_, int **I_, int **J_);
|
|---|
| 175 |
|
|---|
| 176 | /** @brief reads matrix market format (coordinate) and returns CSR format
|
|---|
| 177 | * @param fn filename of file containing the matrix.
|
|---|
| 178 | * @param m output #rows.
|
|---|
| 179 | * @param n output #columns.
|
|---|
| 180 | * @param nz output #non zeros.
|
|---|
| 181 | * @param i output row values of CSR matrix.
|
|---|
| 182 | * @param j output column values of CSR matrix.
|
|---|
| 183 | * @param a output values of CSR matrix.
|
|---|
| 184 | * @return 0 on success -1 on failure.
|
|---|
| 185 | */
|
|---|
| 186 | int read_mm_matrix (const char *fn, int *m, int *n, int *nz,
|
|---|
| 187 | int **i_idx, int **j_idx, double **a);
|
|---|
| 188 |
|
|---|
| 189 | /* write CSR format */
|
|---|
| 190 | /* 1st line : % number_of_rows number_of_columns number_of_nonzeros
|
|---|
| 191 | 2nd line : % base of index
|
|---|
| 192 | 3rd line : row_number nz_r(=number_of_nonzeros_in_the_row)
|
|---|
| 193 | next nz_r lines : column_index value(when a != NULL)
|
|---|
| 194 | next line : row_number nz_r(=number_of_nonzeros_in_the_row)
|
|---|
| 195 | next nz_r lines : column_index value(when a != NULL)
|
|---|
| 196 | ...
|
|---|
| 197 | * @return 0 on success -1 on failure.
|
|---|
| 198 | */
|
|---|
| 199 | int write_csr (const char *fn, int m, int n, int nz,
|
|---|
| 200 | int *row_start, int *col_idx, double *a);
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 | /** @brief converts COO format to CSR format, not in-place,
|
|---|
| 204 | * if SORT_IN_ROW is defined, each row is sorted in column index
|
|---|
| 205 | * @param n #rows.
|
|---|
| 206 | * @param nz #non zeros.
|
|---|
| 207 | * @param a values of MM matrix.
|
|---|
| 208 | * @param i_idx row values of MM matrix.
|
|---|
| 209 | * @param j_idx column values of MM matrix.
|
|---|
| 210 | * @param csr_a output values of CSR matrix.
|
|---|
| 211 | * @param col_idx output column values of CSR matrix.
|
|---|
| 212 | * @param row_start output row values of CSR matrix.
|
|---|
| 213 | * @return Void.
|
|---|
| 214 | */
|
|---|
| 215 | void coo2csr(int n, int nz, double *a, int *i_idx, int *j_idx,
|
|---|
| 216 | double *csr_a, int *col_idx, int *row_start);
|
|---|
| 217 |
|
|---|
| 218 | #endif
|
|---|