-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparsemat.cpp
More file actions
268 lines (218 loc) · 5.18 KB
/
sparsemat.cpp
File metadata and controls
268 lines (218 loc) · 5.18 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "sparsemat.h"
#include <iostream>
#include <fstream>
#include <sstream>
//==========================================================
// class SparseMat
SparseMat & SparseMat::operator= (const SparseMat & spamat)
{
if(this == &spamat)
return *this;
row = spamat.row;
col = spamat.col;
nnz = spamat.nnz;
val = spamat.val;
return *this;
}
SparseMat & SparseMat::operator= (SparseMat && spamat)
{
if(this == &spamat)
return *this;
row = spamat.row;
col = spamat.col;
nnz = spamat.nnz;
val.swap(spamat.val);
return *this;
}
INT SparseMat::GetRow() const
{
return row;
}
INT SparseMat::GetCol() const
{
return col;
}
INT SparseMat::GetNnz() const
{
return nnz;
}
//==========================================================
// class SpaCOO
SpaCOO & SpaCOO::operator=(const SpaCOO & coo)
{
if(this == &coo)
return *this;
SparseMat::operator=(coo);
row_vec = coo.row_vec;
col_vec = coo.col_vec;
return *this;
}
SpaCOO & SpaCOO::operator=(SpaCOO && coo)
{
if(this == &coo)
return *this;
SparseMat::operator=(std::move(coo));
row_vec.swap(coo.row_vec);
col_vec.swap(coo.col_vec);
return *this;
}
void SpaCOO::PrintPartialMat(INT num)
{
for(INT i=0;i<num;i++)
std::cout<<row_vec[i]<<" "<<col_vec[i]<<" "<<val[i]<<std::endl;
}
void SpaCOO::ReadMat0(std::string file_name)
{
std::ifstream myfile(file_name);
if(!myfile.is_open())
{
std::cout<<"can't open the matrix file!"<<std::endl;
exit(1);
}
std::string line;
getline(myfile,line);
std::istringstream split_line(line);
split_line >> row >> col >> nnz;
std::vector<INT> tmp_row_vec(nnz);
std::vector<INT> tmp_col_vec(nnz);
std::vector<DOUBLE> tmp_val(nnz);
for(INT i=0;i<nnz;i++)
{
getline(myfile,line);
std::istringstream tmp_line(line);
tmp_line >> tmp_row_vec[i] >> tmp_col_vec[i] >> tmp_val[i];
}
getline(myfile,line);
if(!myfile.eof())
std::cout<<"Reading file is not finished!"<< std::endl;
myfile.close();
row_vec.swap(tmp_row_vec);
col_vec.swap(tmp_col_vec);
val.swap(tmp_val);
}
void SpaCOO::ReadMat1(std::string file_name)
{
std::ifstream myfile(file_name);
if(!myfile.is_open())
{
std::cout<<"can't open the matrix file!"<<std::endl;
exit(1);
}
std::string line;
getline(myfile,line);
std::istringstream split_line(line);
split_line >> row >> col >> nnz;
std::vector<INT> tmp_row_vec(nnz);
std::vector<INT> tmp_col_vec(nnz);
std::vector<DOUBLE> tmp_val(nnz);
for(INT i=0;i<nnz;i++)
{
getline(myfile,line);
std::istringstream tmp_line(line);
INT tmp_i,tmp_j;
tmp_line >> tmp_i >> tmp_j >> tmp_val[i];
tmp_row_vec[i] = tmp_i - 1;
tmp_col_vec[i] = tmp_j - 1;
}
getline(myfile,line);
if(!myfile.eof())
std::cout<<"Reading file is not finished!"<< std::endl;
myfile.close();
row_vec.swap(tmp_row_vec);
col_vec.swap(tmp_col_vec);
val.swap(tmp_val);
}
//==========================================================
// class SpaCSR
SpaCSR::SpaCSR(const SpaCOO & coo):SparseMat( coo.GetRow(), coo.GetCol(), coo.GetNnz() ),
row_vec(coo.GetRow()+1),
col_vec(coo.GetNnz())
{
row_vec[0] = 0;
for(INT i=0;i<nnz;i++)
row_vec[coo.row_vec[i] + 1] += 1;
std::vector<INT> num_per_row = row_vec;
for(INT i=2;i<row+1;i++)
row_vec[i] = row_vec[i] + row_vec[i-1];
for(INT i=0;i<nnz;i++)
{
INT r_idx = coo.row_vec[i];
INT begin_idx = row_vec[r_idx];
INT end_idx = row_vec[r_idx + 1];
INT offset = end_idx - begin_idx - num_per_row[1 + r_idx];
num_per_row[1 + r_idx] -= 1;
if(offset == end_idx - begin_idx)
{
std::cout<<"Converting COO to CSR(assignment function): the index is wrong!"<<std::endl;
exit(1);
}
else
{
col_vec[begin_idx + offset] = coo.col_vec[i];
val[begin_idx + offset] = coo.val[i];
}
}
}
SpaCSR & SpaCSR::operator=(const SpaCSR & csr)
{
if(this == &csr)
return *this;
SparseMat::operator=(csr);
row_vec = csr.row_vec;
col_vec = csr.col_vec;
return *this;
}
SpaCSR & SpaCSR::operator=(const SpaCOO & coo)
{
/* SparseMat::operator=(coo); */
row = coo.GetRow();
col = coo.GetCol();
nnz = coo.GetNnz();
row_vec.resize(row+1) ;
col_vec.resize(nnz);
val.resize(nnz);
row_vec[0] = 0;
for(INT i=0;i<nnz;i++)
row_vec[coo.row_vec[i] + 1] += 1;
std::vector<INT> num_per_row = row_vec;
for(INT i=2;i<row+1;i++)
row_vec[i] = row_vec[i] + row_vec[i-1];
for(INT i=0;i<nnz;i++)
{
INT r_idx = coo.row_vec[i];
INT begin_idx = row_vec[r_idx];
INT end_idx = row_vec[r_idx + 1];
INT offset = end_idx - begin_idx - num_per_row[1 + r_idx];
num_per_row[1 + r_idx] -= 1;
if(offset == end_idx - begin_idx)
{
std::cout<<"Converting COO to CSR(assignment function): the index is wrong!"<<std::endl;
exit(1);
}
else
{
col_vec[begin_idx + offset] = coo.col_vec[i];
val[begin_idx + offset] = coo.val[i];
}
}
return *this;
}
SpaCSR & SpaCSR::operator=(SpaCSR && csr)
{
if(this == &csr)
return *this;
SparseMat::operator=(std::move(csr));
row_vec.swap(csr.row_vec);
col_vec.swap(csr.col_vec);
return *this;
}
void SpaCSR::PrintPartialMat(INT num)
{
for(INT i=0;i<num;i++)
{
INT begin_idx = row_vec[i];
INT num_entry = row_vec[i+1] - begin_idx;
for(INT j=0;j<num_entry;j++)
std::cout<<i<<" "<<col_vec[begin_idx+j]<<" "<<val[begin_idx+j]<<std::endl;
}
}