-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cu
274 lines (246 loc) · 7.62 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
#include <omp.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <cuda_runtime.h>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <algorithm>
using namespace std;
typedef unsigned int uint;
typedef unsigned long long ull;
#define BLOCK_SIZE 64
const double ratio = 0.9;
__global__ void TC_kernal(ull *d_cnt, int *d_col, int *d_off, int *d_row, int *d_map, int map_stride, int row_idx)
{
ull cnt = 0;
for (int i = blockIdx.x; i < row_idx; i += gridDim.x) {
int st = d_off[d_row[i]];
int ed = d_off[d_row[i]+1];
for (int j = st; j < ed; j += blockDim.x) {
int u;
if (j + threadIdx.x < ed) {
u = d_col[j+threadIdx.x];
atomicOr(d_map + blockIdx.x*map_stride + (u >> 5), (1 << (u & 0x1f)));
}
__syncthreads();
int ked = min(ed-j, blockDim.x);
for (int k = 0; k < ked; k++) {
int t = d_col[j+k];
int l = d_off[t] + threadIdx.x;
int r = d_off[t+1];
while (l < r) {
int v = d_col[l];
if (d_map[blockIdx.x*map_stride + (v >> 5)] & (1 << (v & 0x1f))) cnt++;
l += blockDim.x;
}
}
__syncthreads();
}
for (int j = st; j < ed; j += blockDim.x) {
if (j + threadIdx.x < ed) {
int u = d_col[j+threadIdx.x];
d_map[blockIdx.x*map_stride + (u >> 5)] = 0;
}
}
__syncthreads();
}
__shared__ ull tot[BLOCK_SIZE];
tot[threadIdx.x] = cnt;
__syncthreads();
for (int k = blockDim.x >> 1; k; k >>= 1) {
if (threadIdx.x < k) {
tot[threadIdx.x] += tot[threadIdx.x + k];
}
__syncthreads();
}
if (threadIdx.x == 0)
d_cnt[blockIdx.x] = tot[0];
}
union edge
{
uint a[2];
ull l;
edge(){}
edge(uint _u, uint _v){
a[0] = _u;
a[1] = _v;
}
bool operator <(const edge r) const {
return l < r.l;
}
bool operator ==(const edge r) const {
return l == r.l;
}
};
int *col, *off, *row, *hash, *bitset;
int *d_col, *d_off, *d_row, *d_map;
ull *h_cnt, *d_cnt, *h_sort, *d_sort;
int col_idx, row_idx, off_idx, edge_idx, point_idx;
edge *edges;
ull ans = 0;
int cri(int u)
{
#pragma omp critical(a)
{
if (hash[u] == 0) u = hash[u] = point_idx++;
else u = hash[u];
}
return u;
}
int main(int argc, char const *argv[])
{
cudaDeviceReset();
char filename[100];
if (argc > 1) {
int i = 1;
while (argv[i][0] == '-') i++;
strcpy(filename, argv[i]);
} else {
// strcpy(filename, "/data/soc-LiveJournal1.bin");
// strcpy(filename, "/data/s24.kron.edgelist");
// strcpy(filename, "/data/twitter_rv.bin");
// strcpy(filename, "/data/s26.kron.edgelist");
strcpy(filename, "/data/s27.kron.edgelist");
}
struct stat statbuf;
stat(filename,&statbuf);
size_t size = statbuf.st_size;
cudaHostAlloc((void **)&edges, size, cudaHostAllocMapped);
edge_idx = 0;
FILE *fin = fopen(filename, "rb");
int wid = 256;
int fid = 256;
while (fid == wid) {
fid = fread(edges + edge_idx, 4, wid, fin);
int ed = edge_idx + (fid >> 1);
for (int i = edge_idx; i < ed; i++)
if (edges[i].a[0] != edges[i].a[1]) {
if (i == edge_idx) {
edge_idx++;
} else {
edges[edge_idx++].l = edges[i].l;
}
}
}
fclose(fin);
hash = (int *)malloc(sizeof(int)*(1lu<<32));
memset(hash, 0, sizeof(int)*(1lu<<32));
point_idx = 1;
#pragma omp parallel for
for (int i = 0; i < edge_idx; i++) {
int u = edges[i].a[0];
int v = edges[i].a[1];
int t = hash[u];
if (t != 0) {
u = t;
} else if (u != 0) {
u = cri(u);
}
t = hash[v];
if (t != 0) {
v = t;
} else if (v != 0) {
v = cri(v);
}
if (u > v) {
t = u;
u = v;
v = t;
}
edges[i].a[0] = u;
edges[i].a[1] = v;
}
cudaHostGetDevicePointer((void **)&d_sort, (void *)edges, 0);
thrust::sort((ull *)d_sort, (ull *)(d_sort + edge_idx));
edge_idx = unique(edges, edges + edge_idx) - edges;
col = (int *)malloc(sizeof(int)*edge_idx);
off = (int *)malloc(sizeof(int)*(point_idx+2));
row = (int *)malloc(sizeof(int)*point_idx);
int last = -1;
off_idx = row_idx = 0;
for (int i = 0; i < edge_idx; i++) {
int u = edges[i].a[0];
int v = edges[i].a[1];
col[i] = u;
while (off_idx <= v)
off[off_idx++] = i;
if (v != last)
row[row_idx++] = v;
last = v;
}
off[off_idx++] = edge_idx;
off[off_idx++] = edge_idx;
int row_gpu_en;
ull prefix_sum = 0;
for (int i = 0; i < row_idx; i++) {
ull m = off[row[i]+1] - off[row[i]];
prefix_sum += m;
if (prefix_sum > edge_idx * ratio) {
row_gpu_en = i;
break;
}
}
cudaMalloc((void **) &d_col, sizeof(int)*edge_idx);
cudaMalloc((void **) &d_off, sizeof(int)*off_idx);
cudaMalloc((void **) &d_row, sizeof(int)*row_idx);
// Rest 1G for GPU
size_t mem_tot;
size_t mem_free;
cudaMemGetInfo(&mem_free, &mem_tot);
int map_stride = (off_idx >> 5) + 1;
int grid_size = (mem_free - (1<<30)) / (sizeof(int)*map_stride + sizeof(ull));
h_cnt = (ull *)malloc(sizeof(ull)*grid_size);
cudaMalloc((void **) &d_map, sizeof(int)*map_stride*grid_size);
cudaMalloc((void **) &d_cnt, sizeof(ull)*grid_size);
cudaMemcpy(d_col, col, sizeof(int)*edge_idx, cudaMemcpyHostToDevice);
cudaMemcpy(d_off, off, sizeof(int)*off_idx, cudaMemcpyHostToDevice);
cudaMemcpy(d_row, row, sizeof(int)*row_idx, cudaMemcpyHostToDevice);
cudaMemset(d_map, 0, sizeof(int)*map_stride*grid_size);
TC_kernal<<< grid_size, BLOCK_SIZE >>>(d_cnt, d_col, d_off, d_row, d_map, map_stride, row_gpu_en);
bitset = (int *)malloc(sizeof(int)*map_stride*omp_get_num_procs());
memset(bitset, 0, sizeof(int)*map_stride*omp_get_num_procs());
#pragma omp parallel for reduction(+:ans) shared(bitset) schedule(dynamic)
for (int i = row_gpu_en; i < row_idx; i++) {
int thread_id = omp_get_thread_num();
int st = off[row[i]];
int ed = off[row[i]+1];
for (int j = st; j < ed; j++) {
int to = col[j];
bitset[thread_id*map_stride + (to >> 5)] |= (1 << (to & 0x1f));
}
for (int j = st; j < ed; j++) {
int t1 = col[j];
int kst = off[t1];
int ked = off[t1+1];
for (int k = kst; k < ked; k++) {
int t2 = col[k];
if ( bitset[thread_id*map_stride + (t2 >> 5)] & (1 << (t2 & 0x1f)) ) ans++;
}
}
for (int j = st; j < ed; j++) {
bitset[thread_id*map_stride + (col[j] >> 5)] = 0;
}
}
cudaDeviceSynchronize();
cudaMemcpy(h_cnt, d_cnt, sizeof(ull)*grid_size, cudaMemcpyDeviceToHost);
for (int i = 0; i < grid_size; i++)
ans += h_cnt[i];
printf("There are %llu triangles in the input graph.\n", ans);
free(col);
free(off);
free(row);
free(hash);
free(h_cnt);
free(bitset);
cudaFree(d_col);
cudaFree(d_off);
cudaFree(d_row);
cudaFree(d_cnt);
cudaFree(d_map);
cudaFreeHost(edges);
return 0;
}