-
Notifications
You must be signed in to change notification settings - Fork 3
/
mmio-wrapper.c
109 lines (88 loc) · 2.96 KB
/
mmio-wrapper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* High-level wrapper for Matrix Market I/O library
*
* It is used to read sparse, real & square matrices from files
*/
#include <stdio.h>
#include <stdlib.h>
#include "mmio-wrapper.h"
#include "mmio.h"
/* Reads a matrix from a Matrix Market file, stored in COO format */
int read_matrix (const char * filename, int **i_idx, int **j_idx, double **values, int *N, int *NZ)
{
FILE *f;
MM_typecode matcode;
int errorcode, nrows, ncols, nz_elements;
/* open the file */
if ( (f = fopen(filename, "r")) == NULL ) {
fprintf(stderr, "Cannot open '%s'\n", filename);
return 1;
}
/* process first line */
if ( (errorcode = mm_read_banner(f, &matcode)) != 0 ) {
fprintf(stderr, "Error while processing banner (file:'%s') (code=%d)\n",
filename, errorcode);
return 1;
}
/* matrix should be sparse and real */
if ( !mm_is_matrix(matcode) ||
!mm_is_real(matcode) ||
!mm_is_sparse(matcode) ) {
fprintf(stderr, "Not supported matrix type: %s\n", mm_typecode_to_str(matcode));
return 1;
}
/* read info */
if ( (errorcode = mm_read_mtx_crd_size(f, &nrows, &ncols, &nz_elements)) != 0) {
fprintf(stderr, "Error while processing array (file:'%s') (code:%d)\n",
filename, errorcode);
return 1;
}
/* matrix should be square */
if (nrows != ncols) {
fprintf(stderr, "Matrix is NOT square (rows=%d, cols=%d)\n", nrows, ncols);
return 1;
}
*N = nrows;
*NZ = nz_elements;
/* reserve memory for vector */
*i_idx = (int *)malloc( nz_elements * sizeof(int) );
*j_idx = (int *)malloc( nz_elements * sizeof(int) );
*values = (double *)malloc( nz_elements * sizeof(double));
/* read actual matrix */
for (int i = 0; i < *NZ; i++) {
fscanf(f, "%d %d %lf", &(*i_idx)[i], &(*j_idx)[i], &(*values)[i]);
(*i_idx)[i]--; (*j_idx)[i]--;
}
/* close the file */
if ( fclose(f) != 0 ) {
fprintf(stderr, "Cannot close file (fil:'%s')\n", filename);
}
return 0;
}
int write_matrix (const char *filename, const int *i_idx, const int *j_idx, const double *values, int N, int NZ)
{
FILE* f;
MM_typecode matcode;
/* open the file */
if ( (f = fopen(filename, "w")) == NULL ) {
fprintf(stderr, "Cannot open '%s'\n", filename);
return 1;
}
/* init and set proper flags for matrix */
mm_initialize_typecode(&matcode);
mm_set_matrix(&matcode);
mm_set_real(&matcode);
mm_set_sparse(&matcode);
/* write banner and matrix size info */
mm_write_banner(f, matcode);
mm_write_mtx_crd_size(f, N, N, NZ);
/* write matrix elements */
for (int i = 0; i < NZ; i++) {
fprintf(f, "%d %d %.9lf\n", i_idx[i] + 1, j_idx[i] + 1, values[i]);
}
/* close the file */
if ( fclose(f) != 0 ) {
fprintf(stderr, "Cannot close file (fil:'%s')\n", filename);
}
return 0;
}