-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIO.h
More file actions
191 lines (173 loc) · 5.94 KB
/
IO.h
File metadata and controls
191 lines (173 loc) · 5.94 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
#ifndef _IO_SPGEMM_H
#define _IO_SPGEMM_H
#include "Triple.h"
#include "CSC.h"
#define READBUFFER (512 * 1024 * 1024) // in MB
template <typename IT, typename NT>
int ReadBinary(string filename, CSC<IT,NT> & csc)
{
FILE * f = fopen(filename.c_str(), "r");
if(!f)
{
cerr << "Problem reading binary input file" << filename << endl;
return -1;
}
IT m,n,nnz;
fread(&m, sizeof(IT), 1, f);
fread(&n, sizeof(IT), 1, f);
fread(&nnz, sizeof(IT), 1, f);
if (m <= 0 || n <= 0 || nnz <= 0)
{
cerr << "Problem with matrix size in binary input file" << filename << endl;
return -1;
}
double start = omp_get_wtime( );
cout << "Reading matrix with dimensions: "<< m << "-by-" << n <<" having "<< nnz << " nonzeros" << endl;
IT * rowindices = new IT[nnz];
IT * colindices = new IT[nnz];
NT * vals = new NT[nnz];
size_t rows = fread(rowindices, sizeof(IT), nnz, f);
size_t cols = fread(colindices, sizeof(IT), nnz, f);
size_t nums = fread(vals, sizeof(NT), nnz, f);
if(rows != nnz || cols != nnz || nums != nnz)
{
cerr << "Problem with FREAD, aborting... " << endl;
return -1;
}
double end = omp_get_wtime( );
// printf("start = %.16g\nend = %.16g\ndiff = %.16g\n", start, end, end - start);
printf("Converting matrix data from binary to COO fromat takes %.16g seconds.\n", end - start);
fclose(f);
csc = *(new CSC<IT,NT>(rowindices, colindices, vals , nnz, m, n));
delete [] rowindices;
delete [] colindices;
delete [] vals;
return 1;
}
template <typename IT, typename NT>
int ReadASCII(string filename, CSC<IT,NT> & csc, IT cluster_sz = 0)
{
bool isSymmetric = false;
double start = omp_get_wtime( );
ifstream infile(filename.c_str());
char line[256];
char c = infile.get();
while(c == '%')
{
infile.getline(line,256);
if (strstr(line, "symmetric")) {
isSymmetric = true;
cout << "Matrix is symmetric" << endl;
}
c = infile.get();
}
infile.unget();
infile.getline(line,256);
IT m,n,nnz;
if (typeid(IT) == typeid(int)) {
sscanf(line, "%d %d %d", &m, &n, &nnz);
}
else if (typeid(IT) == typeid(long long int)) {
sscanf(line, "%ld %ld %ld", &m, &n, &nnz);
}
else {
sscanf(line, "%ld %ld %ld", &m, &n, &nnz);
}
// cout << "Reading matrix with dimensions: "<< m << "-by-" << n <<" having "<< nnz << " nonzeros" << endl;
if (isSymmetric) {
nnz *= 2;
}
// cout << "come so far..." << endl;
Triple<IT,NT> * triples = new Triple<IT,NT>[nnz];
// cout << "mem allocation dome..." << endl;
if (infile.is_open())
{
IT cnz = 0; // current number of nonzeros
while (! infile.eof() && cnz < nnz)
{
infile.getline(line,256);
char *ch = line;
// cout << "line: " << line << endl;
triples[cnz].row = (IT)(atoi(ch));
// ch = strchr(ch, ' ');
// ch++;
ch = strpbrk(ch, " \t");
while (*ch == ' ' || *ch == '\t') ++ch;
triples[cnz].col = (IT)(atoi(ch));
// ch = strchr(ch, ' ');
ch = strpbrk(ch, " \t");
if (ch != NULL) {
// ch++;
while (*ch == ' ' || *ch == '\t') ++ch;
/* Read third word (value data)*/
// triples[cnz].val = (NT)(atoi(ch));
// triples[cnz].val = (NT)(atof(ch));
triples[cnz].val = (NT)(atof(ch) + 1.0);
// ch = strchr(ch, ' ');
}
else {
triples[cnz].val = 1.0;
}
// infile >> triples[cnz].row >> triples[cnz].col >> triples[cnz].val; // row-col-value
triples[cnz].row--;
triples[cnz].col--;
if (isSymmetric) {
if (triples[cnz].col != triples[cnz].row) {
cnz++;
triples[cnz].col = triples[cnz - 1].row;
triples[cnz].row = triples[cnz - 1].col;
triples[cnz].val = triples[cnz - 1].val;
}
else {
nnz--;
}
}
++cnz;
// if(cnz % 1000000 == 0) cout << "read " << (cnz / 1000000) << "M entries" << endl;
}
// IT rid, cid;
// NT v;
// while (infile >> rid >> cid >> v)
// {
// triples[cnz].row = rid;
// triples[cnz].col = cid;
// triples[cnz].val = v;
//
// triples[cnz].row--;
// triples[cnz].col--;
//
// if (isSymmetric) {
// if (triples[cnz].col != triples[cnz].row) {
// cnz++;
// triples[cnz].col = triples[cnz - 1].row;
// triples[cnz].row = triples[cnz - 1].col;
// triples[cnz].val = triples[cnz - 1].val;
// }
// else {
// nnz--;
// }
// }
// ++cnz;
//// if(cnz % 1000000 == 0) cout << "read " << (cnz / 1000000) << "M entries" << endl;
// }
// if(cnz != nnz) cout << "nnz: " << nnz << ", cnz: " << cnz << endl;
assert(cnz == nnz);
}
double end = omp_get_wtime( );
// printf("start = %.16g\nend = %.16g\ndiff = %.16g\n", start, end, end - start);
printf("Converting matrix data from ASCII to COO format: %.16g seconds\n", end - start);
printf("Input Matrix: Rows = %d, Columns= %d, nnz = %d, cluster_sz = %d\n", m, n, nnz, cluster_sz);
// cout << "cluster_sz: " << cluster_sz << endl;
cout << "Converting to csc ... " << endl << endl;
if(cluster_sz != 0) {
if(m%cluster_sz != 0) {
m = ((m / cluster_sz) + 1) * cluster_sz;
n = m;
}
}
csc= *(new CSC<IT,NT>(triples, nnz, m, n));
csc.totalcols = n;
delete [] triples;
return 1;
}
#endif