-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
161 lines (124 loc) · 4.82 KB
/
main.cpp
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
#include <iostream>
#include <sys/time.h>
#include <string.h>
#include <fstream>
#include <math.h>
#include <iomanip>
#include <algorithm>
#include "writeMM.hpp"
#include "readMM.hpp"
#include "GMsequential.hpp"
#include "GMopenMP.hpp"
#include "GMpthreads.hpp"
#define NUM_THREADS 4
bool areCSRVectorsEqual(CSR &csrSeq, CSR &csrPar)
{
if (csrSeq.row != csrPar.row || csrSeq.col.size() != csrPar.col.size() || csrSeq.val.size() != csrPar.val.size() || csrSeq.col.size() != csrSeq.val.size())
return false;
for (size_t i = 0; i < csrSeq.row.size(); i++)
{
std::vector<size_t>::iterator startRange = csrSeq.col.begin() + csrSeq.row[i];
for (size_t j = csrSeq.row[i]; j < csrSeq.row[i + 1]; j++)
{
std::vector<size_t>::iterator colIndex = std::find(startRange, startRange + csrSeq.row[i + 1], csrPar.col[j]); // find this column in the range of columns of the same row in Seq
// (each column appears at most once in each row)
// If col[j] is not found, vectors are not equal
if (colIndex == csrSeq.col.end())
{
printf("colIndex == colSeq.end()\n");
return false;
}
// Get the index of col[j] in colSeq and thus valSeq
size_t valIndex = std::distance(csrSeq.col.begin(), colIndex);
// Check if these values are in the same column cluster
if (csrPar.val[j] != csrSeq.val[valIndex])
{
printf("col[i] != colSeq[colIndex]\n");
printf(" j = %ld, colIndex = %ld\n", j, valIndex);
return false;
}
}
}
// Vectors are equal
return true;
}
int main(int argc, char *argv[])
{
uint32_t numThreads = NUM_THREADS;
if (argc < 3)
{
fprintf(stderr, "Usage: %s ${martix-market-filename} ${c-matrix-filename} ${number-of-threads}\n", argv[0]);
exit(1);
}
else if (argc == 4)
if (atoi(argv[3]) > 0)
numThreads = atoi(argv[3]);
std::cout << "Running with numThreads: " << numThreads << std::endl;
char *filename = (char *)argv[1];
int Nread;
int nz;
bool symmetric = false;
verifyMMfile(&Nread, &nz, symmetric, filename);
size_t nzread = nz;
std::vector<size_t> conf(Nread, 1);
char *cfilename = (char *)argv[2];
std::ifstream cfile(cfilename);
size_t c, index = 0;
while (cfile >> c)
{
conf[index++] = c;
}
/* Parsing the exponential data of the roads_usa cluster ids
// std::ofstream output;
// output.open("parsing.txt");
// size_t sc[10];
// for (size_t i = 0; i < 5; i++)
// {
// cfile >> std::scientific >> sc[i];
// output << std::fixed << std::setprecision(0) << sc[i] << std::endl;
// }
// output.close();
*/
printf("start\n");
std::vector<size_t> I(nzread, 0);
std::vector<size_t> J(nzread, 0);
std::vector<uint64_t> V(nzread, 0);
std::vector<size_t> row(Nread + 1, 0);
std::vector<size_t> col(nzread, 0);
std::vector<uint64_t> val(nzread, 0);
COO coo = {I, J, V};
CSR csr = {row, col, val};
readMM(I, J, V, filename, nzread);
coo_to_csr(csr, nzread, coo, Nread, symmetric);
std::vector<size_t> rowM(Nread + 1, 0);
std::vector<size_t> colM(nzread, 0);
std::vector<uint64_t> valM(nzread, 0);
CSR csrM = {rowM, colM, valM};
struct timeval start, end;
gettimeofday(&start, NULL);
seq(csrM, csr, conf);
gettimeofday(&end, NULL);
printf("seq time: %ld\n", ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)));
printf("row size: %ld\n", csrM.row.size());
printf("col size: %ld\n", csrM.col.size());
printf("val size: %ld\n", csrM.val.size());
std::vector<size_t> rowMP(Nread + 1, 0);
std::vector<size_t> colMP(nzread, 0);
std::vector<uint64_t> valMP(nzread, 0);
CSR csrMP = {rowMP, colMP, valMP};
gettimeofday(&start, NULL);
GMopenMP(csrMP, csr, conf, numThreads);
gettimeofday(&end, NULL);
printf("parallel time: %ld\n", ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)));
printf("Are seq and openmp vectors equal? %d\n", areCSRVectorsEqual(csrM, csrMP));
std::vector<size_t> rowPt(Nread + 1, 0);
std::vector<size_t> colPt(nzread, 0);
std::vector<uint64_t> valPt(nzread, 0);
CSR csrPt = {rowPt, colPt, valPt};
gettimeofday(&start, NULL);
GMpthreads(csrPt, csr, conf, numThreads);
gettimeofday(&end, NULL);
printf("pthread time: %ld\n", ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)));
printf("Are seq and pthreads vectors equal? %d\n", areCSRVectorsEqual(csrM, csrPt));
return 0;
}