-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreadMatrix.h
More file actions
194 lines (183 loc) · 5.03 KB
/
readMatrix.h
File metadata and controls
194 lines (183 loc) · 5.03 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef READ_MATRIX_H
#define READ_MATRIX_H
#include <fstream>
#include <iostream>
#include <sstream>
// Both read matrix functions partially taken from sympiler repository. However, both have been modified
/*
* reading a CSC matrix from a coordinate file, stored col-ordered
*/
bool readSparseMatrix(std::string fName, int &n, int &NNZ, int* &col, int* &row, double* &val){
/*This function reads the input matrix from "fName" file and
* allocate memory for matrix A, L and U.
* - The input file is a coordinate version and e
* ach row of the file shows (col, row, nnz)
* - The matrices are zero-indexed
*/
std::ifstream inFile;
inFile.open(fName);
std::string line,banner, mtx, crd, arith, sym;
/* File format:
* %%MatrixMarket matrix coordinate real general/symmetric/...
* % ...
* % (optional comments)
* % ...
* #rows #non-zero
* Triplet in the rest of lines: row col value
*/
std::getline(inFile,line);
for (unsigned i=0; i<line.length(); line[i]=tolower(line[i]),i++);
std::istringstream iss(line);
if (!(iss >> banner >> mtx >> crd >> arith >> sym)){
std::cout<<"Invalid header (first line does not contain 5 tokens)" << std::endl;
return false;
}
if(banner.compare("%%matrixmarket")) {
std::cout<<"Invalid header (first token is not \"%%%%MatrixMarket\")" << std::endl;
return false;
}
if(mtx.compare("matrix")) {
std::cout<<"Not a matrix; this driver cannot handle that." << std::endl;
return false;
}
if(crd.compare("coordinate")) {
std::cout<<"Not in coordinate format; this driver cannot handle that." << std::endl;
return false;
}
if(arith.compare("real")) {
if(!arith.compare("complex")) {
std::cout<<"Complex matrix; use zreadMM instead!" << std::endl;
return false;
}
else if(!arith.compare("pattern")) {
std::cout<<"Pattern matrix; values are needed!" << std::endl;
return false;
}
else {
std::cout<<"Unknown arithmetic" << std::endl;
return false;
}
}
while (!line.compare(0,1,"%"))
{
std::getline(inFile, line);
}
std::istringstream issDim(line);
if (!(issDim >> n >> n >> NNZ)){
std::cout<<"The matrix dimension is missing" << std::endl;
return false;
}
if(n <= 0 || NNZ <= 0)
return false;
col = new int[n + 1]();
row = new int[NNZ];
val = new double[NNZ];
if(!val || !col || !row)
return false;
int y, x, colCnt=0, nnzCnt=0;
double value;
col[0]=0;
for (int i = 0; i < NNZ; ++i) {
inFile>>x;
inFile>>y;
inFile>>value;
if(y > n || y > x || x > n) {
std::cout << x << " " << y << " " << value ;
return false;
}
val[i] = value;
row[i] = x - 1;
++col[y];
}
for (int i = 2; i <= n; ++i)
col[i] += col[i-1];
return true;
}
/*
* reading a dense vector from a coordinate file, stored col-ordered
*/
bool readRHSMatrix(std::string fName, double* &val, const int size) {
std::ifstream inFile;
inFile.open(fName);
std::string line,banner, mtx, crd, arith, sym;
/* File format:
* %%MatrixMarket matrix coordinate real general/symmetric/...
* % ...
* % (optional comments)
* % ...
* #rows #non-zero
* Triplet in the rest of lines: row col value
*/
std::getline(inFile,line);
for (unsigned i=0; i<line.length(); line[i]=tolower(line[i]),i++);
std::istringstream iss(line);
if (!(iss >> banner >> mtx >> crd >> arith >> sym)){
std::cout<<"Invalid header (first line does not contain 5 tokens)" << std::endl;
return false;
}
if(banner.compare("%%matrixmarket")) {
std::cout<<"Invalid header (first token is not \"%%%%MatrixMarket\")" << std::endl;
return false;
}
if(mtx.compare("matrix")) {
std::cout<<"Not a matrix; this driver cannot handle that." << std::endl;
return false;
}
if(crd.compare("coordinate")) {
std::cout<<"Not in coordinate format; this driver cannot handle that." << std::endl;
return false;
}
if(arith.compare("real")) {
if(!arith.compare("complex")) {
std::cout<<"Complex matrix; use zreadMM instead!" << std::endl;
return false;
}
else if(!arith.compare("pattern")) {
std::cout<<"Pattern matrix; values are needed!" << std::endl;
return false;
}
else {
std::cout<<"Unknown arithmetic" << std::endl;
return false;
}
}
while (!line.compare(0,1,"%"))
{
std::getline(inFile, line);
}
int n, cols, NNZ;
std::istringstream issDim(line);
if (!(issDim >> n >> cols >> NNZ)){
std::cout<<"The matrix dimension is missing" << std::endl;
return false;
}
if(n <= 0 || NNZ <= 0)
return false;
if (n != size) {
std::cout << "Dimensions of L and RHS do not match" << std::endl;
std::cout << size << " " << n << std::endl;
return false;
}
if (cols != 1) {
std::cout << "RHS matrix has incorrect dimensions" << std::endl;
return false;
}
val = new double[n];
if(!val)
return false;
//Initializing the result vector
int y, x;
double value;
for (int i = 0; i < NNZ; ++i) {
inFile>>x;
inFile>>y;
inFile>>value;
if(y > 1 || x > n) {
std::cout << x << " " << y << " " << value ;
return false;
}
val[x-1] = value;
}
return true;
}
#endif