-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtriangles.cpp
90 lines (82 loc) · 1.91 KB
/
triangles.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
#include <frovedis/matrix/crs_matrix.hpp>
#include <frovedis/matrix/spgemm.hpp>
#include <chrono>
#include <string>
using namespace std;
using namespace frovedis;
int main(int argc, char* argv[]) {
string path = argv[1];
// Input graph must be a sparse upper triangular matrix.
auto U = make_crs_matrix_local_loadbinary<long,int>(path);
cout << "Start calculation \n";
auto start = chrono::high_resolution_clock::now();
auto L = U.transpose();
cout << "After transposing...";
auto B = spgemm(L,U);
cout << "After spgemm...";
// U = U .* B; sparse element-wise product
int indexU = 0;
int indexB = 0;
int rowU = 0;
int rowB = 0;
int FLAG = 0; //Record whether the current element has been changed
int sizeU = U.val.size();
int sizeB = B.val.size();
int size_offU = U.off.size();
int size_offB = B.off.size();
auto offU = U.off.data();
auto offB = B.off.data();
auto valU = U.val.data();
auto valB = B.val.data();
auto idxU = U.idx.data();
auto idxB = B.idx.data();
for ( ; indexU < sizeU; indexU++)
{
FLAG = 0;
// Update rowU
for ( ; rowU < size_offU; rowU++)
{
if ((offU[rowU] <= indexU) && (offU[rowU + 1] > indexU))
break;
}
for ( ; indexB < sizeB; indexB++)
{
// Update rowB
for ( ; rowB < size_offB; rowB++)
{
if ((offB[rowB] <= indexB) && (offB[rowB + 1] > indexB))
break;
}
if (rowB > rowU)
{
break;
}
if (rowB < rowU)
{
continue;
}
if (idxB[indexB] > idxU[indexU])
{
break;
}
if ((rowB == rowU) && (idxB[indexB] == idxU[indexU]))
{
valU[indexU] = valB[indexB];
FLAG = 1;
}
}
if (FLAG == 0)
{
valU[indexU] = 0;
}
}
long sum = 0;
for(size_t i = 0; i < sizeU; i++)
{
sum += valU[i];
}
cout << sum << '\n';
auto finish = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = finish - start;
cout << "Time: " << elapsed.count() << " s\n";
}