forked from akyrola/shotgun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_matrix_market.cpp
155 lines (126 loc) · 4.34 KB
/
read_matrix_market.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Matrix Market I/O example program
*
* Read a real (non-complex) sparse matrix from a Matrix Market (v. 2.0) file.
* and copies it to stdout. This porgram does nothing useful, but
* illustrates common usage of the Matrix Matrix I/O routines.
* (See http://math.nist.gov/MatrixMarket for details.)
*
* Usage: a.out [filename] > output
*
*
* NOTES:
*
* 1) Matrix Market files are always 1-based, i.e. the index of the first
* element of a matrix is (1,1), not (0,0) as in C. ADJUST THESE
* OFFSETS ACCORDINGLY offsets accordingly when reading and writing
* to files.
*
* 2) ANSI C requires one to use the "l" format modifier when reading
* double precision floating point numbers in scanf() and
* its variants. For example, use "%lf", "%lg", or "%le"
* when reading doubles, otherwise errors will occur.
*/
#include <stdio.h>
#include <stdlib.h>
#include "mmio.h"
#include "common.h"
#include <assert.h>
void convert_2_mat(const char * filename, shotgun_data * prob )
{
int ret_code;
MM_typecode matcode;
FILE *f;
int M, N, nz;
int i;
if ((f = fopen(filename, "r")) == NULL){
printf("Could not find Matrix Market input file %s.\n", filename);
exit(1);
}
if (mm_read_banner(f, &matcode) != 0)
{
printf("Could not process Matrix Market banner in inputs file %s.\n", filename);
exit(1);
}
/* This is how one can screen matrix types if their application */
/* only supports a subset of the Matrix Market data types. */
if (mm_is_complex(matcode) && mm_is_matrix(matcode) &&
mm_is_sparse(matcode) )
{
printf("Sorry, this application does not support ");
printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
exit(1);
}
/* find out size of sparse matrix .... */
if ((ret_code = mm_read_mtx_crd_size(f, &M, &N, &nz)) !=0){
printf("Could not process Matrix Market size in input file: %s.\n", filename);
exit(1);
}
/* reseve memory for matrices */
/* NOTE: when reading in doubles, ANSI C requires the use of the "l" */
/* specifier as in "%lg", "%lf", "%le", otherwise errors will occur */
/* (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15) */
int I,J;
double val;
prob->A_cols = new std::vector<sparse_array>();
prob->A_rows = new std::vector<sparse_array>();
(*prob->A_cols).resize(N);
(*prob->A_rows).resize(M);
for (i=0; i<nz; i++)
{
fscanf(f, "%d %d %lg\n", &I, &J, &val);
I--; /* adjust from 1-based to 0-based */
J--;
prob->A_cols[J].add(I, val);
prob->A_rows[I].add(J, val);
}
prob->nx = N;
prob->ny = M;
if (f !=stdin) fclose(f);
}
void convert_2_vec(const char * filename, shotgun_data * prob )
{
int ret_code;
MM_typecode matcode;
FILE *f;
int M, N, nz;
int i;
if ((f = fopen(filename, "r")) == NULL) {
printf("Could not file vector input file : %s.\n", filename);
exit(1);
}
if (mm_read_banner(f, &matcode) != 0)
{
printf("Could not process Matrix Market banner in input file %s.\n", filename);
exit(1);
}
/* This is how one can screen matrix types if their application */
/* only supports a subset of the Matrix Market data types. */
if (mm_is_complex(matcode) && mm_is_matrix(matcode) &&
mm_is_sparse(matcode) )
{
printf("Sorry, this application does not support ");
printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
exit(1);
}
/* find out size of sparse matrix .... */
if ((ret_code = mm_read_mtx_crd_size(f, &M, &N, &nz)) !=0){
printf("Could not process Matrix Market size in input file: %s.\n", filename);
exit(1);
}
/* NOTE: when reading in doubles, ANSI C requires the use of the "l" */
/* specifier as in "%lg", "%lf", "%le", otherwise errors will occur */
/* (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15) */
prob->y.reserve(nz);
int I,J;
double val;
for (i=0; i<nz; i++)
{
fscanf(f, "%d %d %lg\n", &I, &J, &val);
I--; /* adjust from 1-based to 0-based */
J--;
assert(J==0);
prob->y.push_back(val);
}
if (f !=stdin) fclose(f);
}