-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.cu
318 lines (271 loc) · 8.99 KB
/
main.cu
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* vim: set ft=cpp: */
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <ctime>
#include <iomanip>
#include <sys/stat.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include "util/data_input_stream.h"
#include "util/data_output_stream.h"
#include "util/timer.h"
#include "util/parameters.h"
#include "matrix/matrix_input.h"
#include "matrix/factory.h"
#define DEFAULT_VECTOR_VALUE 1
using namespace std;
Parameter app;
uint32_t times = 1;
string outfile;
vector<string> formats;
vector<uint32_t> start_rows, perm, revperm;
uint32_t num_rows, num_cols, cur=0, nnz;
bool rebuild = false, writeMatrixParts = false, DEBUG = false;
template <typename T>
void readVector(const char *file, thrust::host_vector<T> &v, uint32_t len){
ifstream in;
in.exceptions ( ifstream::failbit | ifstream::badbit );
try {
in.open(file);
for (int i=0;i<len;i++) {
T t; in >> t;
v.push_back(t);
}
}catch(ifstream::failure e) {
cout << "Exception opening/reading: " << file << endl;
throw e;
}
in.close();
}
template <typename T>
struct MatrixFormatsHolder{
vector<Matrix<T> *> vec;
~MatrixFormatsHolder(){
for(uint32_t i=0; i<vec.size(); i++){
if(vec[i] != NULL){
delete vec[i];
}
}
}
};
void skipLines(MMFormatInput &in, uint32_t lines){
for(uint32_t i=0; i<lines; i++){
uint32_t nc, temp;
in >> nc;
for(uint32_t j=0; j<nc; j++){
in >> temp;
}
}
}
template <typename T>
void print_result( T* v , thrust::host_vector<T> &hv) {
thrust::device_ptr<T> d_ptr = thrust::device_pointer_cast(v);
thrust::copy(d_ptr, d_ptr+num_rows, hv.begin());
ofstream out(outfile.c_str());
for(uint32_t i=0; i<num_rows; i++){
out << hv[revperm[i]] << endl;
}
out.close();
}
template <typename T>
void execute(){
cudaSetDevice(app.gpu);
ifstream f(app.matrixFile.c_str());
MMFormatInput matrix_input(f, perm);
num_rows = matrix_input.getNumRows();
num_cols = matrix_input.getNumCols();
nnz = matrix_input.getNNz();
cout << num_rows << " " << num_cols << " " << nnz << " " << static_cast<double>(nnz)/num_rows << endl;
thrust::host_vector<T> hv;
//read vector
if(!app.vectorFile.empty()){
hv.reserve(max(num_rows,num_cols));
readVector(app.vectorFile.c_str(), hv, num_cols);
}else{
hv.resize(max(num_rows,num_cols), DEFAULT_VECTOR_VALUE);
}
if (DEBUG) cout << "Vector storage: " << hv.size() * sizeof(T) / 1024.0 / 1024.0 << " MB" << endl;
MatrixFormatsHolder<T> mfh;
for(uint32_t i=0; i<formats.size(); i++){
Matrix<T> *matrix = matrix_factory::getMatrixObject<T>(formats[i]);
if(i>0){
uint32_t nr = start_rows[i]-start_rows[i-1];
cur += nr;
mfh.vec.back()->set_num_rows(nr);
}
matrix->set_num_cols(num_cols);
mfh.vec.push_back(matrix);
}
if (cur >= num_rows)
throw ios::failure("Sum of rows > number of rows!");
mfh.vec.back()->set_num_rows(num_rows - cur);
uint32_t skip = 0;
for(uint32_t i=0; i<mfh.vec.size(); i++){
struct stat st_file_info;
stringstream ss;
ss << getFileName(app.matrixFile) << "-" << start_rows[i] << mfh.vec[i]->getCacheName();
string s(ss.str());
if(!rebuild && !stat(s.c_str(), &st_file_info)){
ifstream inCache(s.c_str());
if (DEBUG) cout<<"Reading cache from row " << start_rows[i] << endl;
mfh.vec[i]->readCache(inCache);
inCache.close();
skip += mfh.vec[i]->get_num_rows();
}else{
skipLines(matrix_input, skip);
skip = 0;
if (DEBUG) cout << "Reading matrix from row " << start_rows[i] << endl;
mfh.vec[i]->readMatrix(matrix_input, perm);
ofstream outCache(s.c_str());
if (DEBUG) cout << "Building cache... " << endl;
mfh.vec[i]->buildCache(outCache);
outCache.close();
}
mfh.vec[i]->transferToDevice();
}
f.close();
double total = 0;
for(uint32_t j=0; j<mfh.vec.size(); j++){
double cur = mfh.vec[j]->memoryUsage()/1024.0/1024.0;
total += cur;
}
thrust::device_vector<T> dv[2];
dv[0].assign(hv.begin(), hv.end());
dv[1].resize(hv.size(), 0);
size_t free, dvTotal;
cudaMemGetInfo (&free,&dvTotal);
if (DEBUG){
cout << "Used device memory: " << (dvTotal-free)/1024.0/1024.0 << "MB" << endl;
cout << "Free device memory: " << free/1024.0/1024.0 << "MB" << endl;
cout << "Total device memory: " << dvTotal/1024.0/1024.0 << "MB" << endl;
}
double parts_time[mfh.vec.size()];
nnz = 0;
for(uint32_t i=0; i<mfh.vec.size(); i++){
parts_time[i] = 0;
nnz += mfh.vec[i]->nonZeroes();
}
T* v = p(dv[0]);
T* r = p(dv[1]);
cudaStream_t streams[mfh.vec.size()];
for (int j=0; j<mfh.vec.size(); j++) {
cudaStreamCreate(&streams[j]);
}
Timer overall, part;
overall.start();
for(uint32_t i=0; i<times; i++){
uint32_t cur_row = 0;
for(uint32_t j=0; j<mfh.vec.size(); j++){
if (DEBUG) part.start();
mfh.vec[j]->multiply(v, r+cur_row, streams[j]);
cur_row += mfh.vec[j]->get_num_rows();
if (DEBUG) {
cudaThreadSynchronize();
parts_time[j] += part.stop();
checkCUDAError("kernel call");
}
}
cudaDeviceSynchronize();
checkCUDAError("kernel call");
swap(v, r);
}
double totalTime = overall.stop();
cout << "Expected/Actual memory usage: " << (total + 2 * hv.size() * sizeof(T) / 1024.0 / 1024.0) << " / " << (dvTotal-free)/1024.0/1024.0 << " MB" << endl;
stringstream stmp;
stmp << sizeof(T)*8 << "b," << times <<"iter";
cout << setw(15) << stmp.str()
<< setw(15) << "seconds"
<< setw(15) << "nnz"
<< setw(15) << "GFL/s"
<< setw(15) << "MB"
<< setw(15) << "B/nnz"
<< endl;
if (DEBUG)
for(uint32_t j=0; j<mfh.vec.size(); j++){
cout << setw(15) << (formats[j] + ":")
<< setw(15) << parts_time[j]
<< setw(15) << mfh.vec[j]->nonZeroes()
<< setw(15) << 2*mfh.vec[j]->nonZeroes()/(parts_time[j]*1000000000)*times
<< setw(15) << mfh.vec[j]->memoryUsage()/1024.0/1024.0
<< setw(15) << mfh.vec[j]->memoryUsage()/(double)mfh.vec[j]->nonZeroes()
<< endl;
}
cout << setw(15) << "Total:"
<< setw(15) << totalTime
<< setw(15) << nnz
<< setw(15) << nnz*2/(totalTime*1000000000)*times
<< setw(15) << total
<< setw(15) << total*1024*1024/nnz
<< endl;
if (outfile.length() > 0)
print_result(v, hv);
}
void readInfo() {
char infopath[255];
sprintf(infopath, FORMAT_PERM, getFileName(app.matrixFile).c_str());
ifstream ifs;
ifs.exceptions ( ifstream::failbit | ifstream::badbit );
try {
ifs.open(infopath);
DataInputStream permif( ifs );
permif.readVector( perm );
ifs.close();
} catch (ifstream::failure e) {
cerr << "Exception while reading permutation file (prep?): " << infopath << endl;
throw e;
}
uint32_t maxdim = perm.size();
revperm.resize(maxdim);
for (uint32_t i=0;i<maxdim;i++)
revperm[perm[i]] = i;
sprintf(infopath , FORMAT_OUTPUT, getFileName(app.matrixFile).c_str());
if (DEBUG) cout << infopath << endl;
ifs.open(infopath);
try {
int nformats;
ifs >> nformats;
if (DEBUG) cout << "Read " << nformats << endl;
formats.resize(nformats);
start_rows.resize(nformats);
for (int i=0;i<nformats;i++) {
ifs >> formats[i];
cout << formats[i] << " ";
}
cout << "|| " ;
for (int i=0;i<nformats;i++) {
ifs >> start_rows[i];
cout << start_rows[i] << " ";
}
cout << endl;
ifs.close();
} catch (ifstream::failure e) {
cerr << "Exception while reading info file (prep?): " << infopath << endl;
throw e;
}
}
int main(int argc, char* argv[]){
try {
Params param(argc, argv);
app.init(param);
param.getParamOptional("times", ×);
param.getParamOptional("outfile", &outfile);
if(!app.wdir.empty()){
cout << "Changing working directory to " << app.wdir << endl;
int r = chdir(app.wdir.c_str());
}
cout << "Input data .. " << endl;
app.vectorFile = app.vectorFile;
readInfo();
if (app.datatype == "float")
execute<float>();
else
execute<double>();
} catch(std::exception& e) {
cerr << e.what() << endl;
return 1;
}
return 0;
}