forked from khaled-rahman/BatchTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.h
157 lines (138 loc) · 4.48 KB
/
IO.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
#ifndef _IO_SPGEMM_H
#define _IO_SPGEMM_H
#include "Triple.h"
#include "CSC.h"
#include <fstream>
#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)
{
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);
}
if (isSymmetric) {
nnz *= 2;
}
Triple<IT,NT> * triples = new Triple<IT,NT>[nnz];
if (infile.is_open())
{
IT cnz = 0; // current number of nonzeros
while (! infile.eof() && cnz < nnz)
{
infile.getline(line,256);
char *ch = line;
triples[cnz].row = (IT)(atoi(ch));
ch = strchr(ch, ' ');
ch++;
triples[cnz].col = (IT)(atoi(ch));
ch = strchr(ch, ' ');
//added by khaled to avoid self loop
if (ch != NULL) {
ch++;
/* Read third word (value data)*/
triples[cnz].val = (NT)(atoi(ch));
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) {
//printf("%d, %d\n", triples[cnz].col, triples[cnz].row);
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;
}
//added by khaled to avoid self-loop
else if(triples[cnz].col == triples[cnz].row){
nnz -= 2;
continue;
}
else {
nnz--;
}
}
++cnz;
}
//printf("cnz = %d, nnz = %d\n", cnz, nnz);
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\n", m, n, nnz);
cout << "Converting to csc ... " << endl << endl;
csc= *(new CSC<IT,NT>(triples, nnz, m, n));
csc.totalcols = n;
delete [] triples;
return 1;
}
#endif