-
Notifications
You must be signed in to change notification settings - Fork 0
/
matio.h
182 lines (154 loc) · 5.77 KB
/
matio.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* (c) 2017 Virginia Polytechnic Institute & State University (Virginia Tech)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License Version 2.1.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* LICENSE in the root of the repository for details.
*
*/
#ifndef _MAT_IO_H
#define _MAT_IO_H
#include "mmio.h"
/* A customized MTX matrix loader to handle values in different sparse matrices */
template<class valT>
int read_mtx_mat(int &m, int &n, int &nnzA, int *&csrRowPtrA, int *&csrColIdxA, valT *&csrValA, char *filename)
{
int ret_code;
MM_typecode matcode;
FILE *f;
int nnzA_mtx_report;
int isInteger = 0, isReal = 0, isPattern = 0, isSymmetric = 0;
// load matrix
if ((f = fopen(filename, "r")) == NULL)
return -1;
if (mm_read_banner(f, &matcode) != 0)
{
std::cout << "Could not process Matrix Market banner.\n";
return -2;
}
if ( mm_is_complex( matcode ) )
{
std::cout <<"Sorry, data type 'COMPLEX' is not supported.\n";
return -3;
}
if ( mm_is_pattern( matcode ) ) { isPattern = 1; /*cout << "type = Pattern" << endl;*/ }
if ( mm_is_real ( matcode) ) { isReal = 1; /*cout << "type = real" << endl;*/ }
if ( mm_is_integer ( matcode ) ) { isInteger = 1; /*cout << "type = integer" << endl;*/ }
/* find out size of sparse matrix .... */
ret_code = mm_read_mtx_crd_size(f, &m, &n, &nnzA_mtx_report);
if (ret_code != 0)
return -4;
if ( mm_is_symmetric( matcode ) || mm_is_hermitian( matcode ) )
{
isSymmetric = 1;
//cout << "symmetric = true" << endl;
}
else
{
//cout << "symmetric = false" << endl;
}
int *csrRowPtrA_counter = (int *)malloc((m+1) * sizeof(int));
memset(csrRowPtrA_counter, 0, (m+1) * sizeof(int));
int *csrRowIdxA_tmp = (int *)malloc(nnzA_mtx_report * sizeof(int));
int *csrColIdxA_tmp = (int *)malloc(nnzA_mtx_report * sizeof(int));
valT *csrValA_tmp = (valT *)malloc(nnzA_mtx_report * sizeof(valT));
/* 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) */
for (int i = 0; i < nnzA_mtx_report; i++)
{
int idxi, idxj;
double fval;
int ival;
if (isReal)
fscanf(f, "%d %d %lg\n", &idxi, &idxj, &fval);
else if (isInteger)
{
fscanf(f, "%d %d %d\n", &idxi, &idxj, &ival);
fval = ival;
}
else if (isPattern)
{
fscanf(f, "%d %d\n", &idxi, &idxj);
fval = 1.0;
}
// adjust from 1-based to 0-based
idxi--;
idxj--;
csrRowPtrA_counter[idxi]++;
csrRowIdxA_tmp[i] = idxi;
csrColIdxA_tmp[i] = idxj;
csrValA_tmp[i] = fval;
}
if (f != stdin)
fclose(f);
if (isSymmetric)
{
for (int i = 0; i < nnzA_mtx_report; i++)
{
if (csrRowIdxA_tmp[i] != csrColIdxA_tmp[i])
csrRowPtrA_counter[csrColIdxA_tmp[i]]++;
}
}
// exclusive scan for csrRowPtrA_counter
int old_val, new_val;
old_val = csrRowPtrA_counter[0];
csrRowPtrA_counter[0] = 0;
for (int i = 1; i <= m; i++)
{
new_val = csrRowPtrA_counter[i];
csrRowPtrA_counter[i] = old_val + csrRowPtrA_counter[i-1];
old_val = new_val;
}
nnzA = csrRowPtrA_counter[m];
csrRowPtrA = (int *)malloc((m+1) * sizeof(int));
memcpy(csrRowPtrA, csrRowPtrA_counter, (m+1) * sizeof(int));
memset(csrRowPtrA_counter, 0, (m+1) * sizeof(int));
csrColIdxA = (int *)malloc(nnzA * sizeof(int));
csrValA = (valT *)malloc(nnzA * sizeof(valT));
if (isSymmetric)
{
for (int i = 0; i < nnzA_mtx_report; i++)
{
if (csrRowIdxA_tmp[i] != csrColIdxA_tmp[i])
{
int offset = csrRowPtrA[csrRowIdxA_tmp[i]] + csrRowPtrA_counter[csrRowIdxA_tmp[i]];
csrColIdxA[offset] = csrColIdxA_tmp[i];
csrValA[offset] = csrValA_tmp[i];
csrRowPtrA_counter[csrRowIdxA_tmp[i]]++;
offset = csrRowPtrA[csrColIdxA_tmp[i]] + csrRowPtrA_counter[csrColIdxA_tmp[i]];
csrColIdxA[offset] = csrRowIdxA_tmp[i];
csrValA[offset] = csrValA_tmp[i];
csrRowPtrA_counter[csrColIdxA_tmp[i]]++;
}
else
{
int offset = csrRowPtrA[csrRowIdxA_tmp[i]] + csrRowPtrA_counter[csrRowIdxA_tmp[i]];
csrColIdxA[offset] = csrColIdxA_tmp[i];
csrValA[offset] = csrValA_tmp[i];
csrRowPtrA_counter[csrRowIdxA_tmp[i]]++;
}
}
}
else
{
for (int i = 0; i < nnzA_mtx_report; i++)
{
int offset = csrRowPtrA[csrRowIdxA_tmp[i]] + csrRowPtrA_counter[csrRowIdxA_tmp[i]];
csrColIdxA[offset] = csrColIdxA_tmp[i];
csrValA[offset] = csrValA_tmp[i];
csrRowPtrA_counter[csrRowIdxA_tmp[i]]++;
}
}
// free tmp space
free(csrColIdxA_tmp);
free(csrValA_tmp);
free(csrRowIdxA_tmp);
free(csrRowPtrA_counter);
return 0;
}
#endif