-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnon_recursive.cu
348 lines (267 loc) · 8.55 KB
/
non_recursive.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//#include "scale.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h> /* fabs */
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <unordered_map>
using namespace std;
#define THREADS_PER_BLOCK 256
#define STREAM_COUNT 4
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
int flag;
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
void printArray(int *arr, int nov){
for (int j=0;j<nov;j++)
{
cout << j << " " << arr[j] << endl;
}
}
__global__ void kernel3(int* adj, int* xadj, int* output, int nov){
int index = threadIdx.x + (blockIdx.x * blockDim.x);
if(index < nov){
//int *marked = new int[n];
//memset(marked, -1, n * sizeof(int)); // bu belki silinebilir
int localcount = 0;
// int round = 0;
// 0-->
int s0 = xadj[index];
int e0 = xadj[index+1];
for(int i=s0; i < e0; i++){
// 0 --> 1
int neighbour_1 = adj[i];
int s1 = xadj[neighbour_1];
int e1 = xadj[neighbour_1+1];
for(int j=s1;j < e1; j++){
// 0 --> 1 --> 2
int neighbour_2 = adj[j];
if (neighbour_2 == index) continue;
int s2 = xadj[neighbour_2];
int e2 = xadj[neighbour_2+1];
for(int k=s2; k < e2; k++){
// 0 --> 1 --> 2 --> 3
int neighbour_3 = adj[k];
if (neighbour_3 == index){
localcount+=1;
break;
}
}
}
}
output[index] = localcount;
}
}
__global__ void kernel4(int* adj, int* xadj, int* output, int nov){
int index = threadIdx.x + (blockIdx.x * blockDim.x);
if(index < nov){
//int *marked = new int[n];
//memset(marked, -1, n * sizeof(int)); // bu belki silinebilir
int localcount = 0;
// int round = 0;
// 0-->
int s0 = xadj[index];
int e0 = xadj[index+1];
for(int i=s0; i < e0; i++){
// 0 --> 1
int neighbour_1 = adj[i];
int s1 = xadj[neighbour_1];
int e1 = xadj[neighbour_1+1];
for(int j=s1;j < e1; j++){
// 0 --> 1 --> 2
int neighbour_2 = adj[j];
//eliminate 0 == 2
if (neighbour_2 == index) continue;
int s2 = xadj[neighbour_2];
int e2 = xadj[neighbour_2+1];
for(int k=s2; k < e2; k++){
// 0 --> 1 --> 2 --> 3
int neighbour_3 = adj[k];
//eliminate 3 == 0
if (neighbour_3 == index) continue;
// eliminate 3 ==1
if (neighbour_3 == neighbour_1) continue;
int s3 = xadj[neighbour_3];
int e3 = xadj[neighbour_3+1];
for(int n=s3; n < e3; n++){
//0 -->1 -->2 -->3 -->4
int neighbour_4 = adj[n];
if (neighbour_4 == index){
localcount+=1;
break;
}
}
}
}
}
output[index] = localcount;
}
}
__global__ void kernel5(int* adj, int* xadj, int* output, int nov){
int index = threadIdx.x + (blockIdx.x * blockDim.x);
if(index < nov){
//int *marked = new int[n];
//memset(marked, -1, n * sizeof(int)); // bu belki silinebilir
int localcount = 0;
// int round = 0;
// 0-->
int s0 = xadj[index];
int e0 = xadj[index+1];
for(int i=s0; i < e0; i++){
// 0 --> 1
int neighbour_1 = adj[i];
int s1 = xadj[neighbour_1];
int e1 = xadj[neighbour_1+1];
for(int j=s1;j < e1; j++){
// 0 --> 1 --> 2
int neighbour_2 = adj[j];
//eliminate 0 == 2
if (neighbour_2 == index) continue;
int s2 = xadj[neighbour_2];
int e2 = xadj[neighbour_2+1];
for(int k=s2; k < e2; k++){
// 0 --> 1 --> 2 --> 3
int neighbour_3 = adj[k];
//eliminate 3 == 0
if (neighbour_3 == index) continue;
// eliminate 3 ==1
if (neighbour_3 == neighbour_1) continue;
int s3 = xadj[neighbour_3];
int e3 = xadj[neighbour_3+1];
for(int n=s3; n < e3; n++){
//0 -->1 -->2 -->3 -->4
int neighbour_4 = adj[n];
//eliminate 4 == 0
if (neighbour_4 == index) continue;
// eliminate 4 ==1
if (neighbour_4 == neighbour_1) continue;
// eliminate 4 ==2
if (neighbour_4 == neighbour_2) continue;
int s4 = xadj[neighbour_4];
int e4 = xadj[neighbour_4+1];
for(int o=s4; o < e4; o++){
//0 -->1 -->2 -->3 -->4--> 5
int neighbour_5 = adj[o];
if (neighbour_5 == index){
localcount+=1;
break;
}
}
}
}
}
}
output[index] = localcount;
}
}
void wrapper(int *xadj, int *adj, int n, int nov, int nnz){
// int X = nov;
// int Y = maxSize;
// int Z = maxSize;
//
// dim3 threadsPerBlock(8, 8, 8);
// dim3 numBlocks(X/threadsPerBlock.x, /* for instance 512/8 = 64
// Y /threadsPerBlock.y,
// Z/threadsPerBlock.z);
//
cudaSetDevice(0);
int *adj_d;
int *xadj_d;
int *output_d;
int *output_h = new int[nov];
int numBlock = (nov + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK;
cudaEvent_t start, stop;
float elapsedTime;
/*
int novForThread = (nov+STREAM_COUNT-1)/STREAM_COUNT;
int novStart = novForThread * threadId;
int novEnd = novForThread * (threadId+1);
if (novEnd> nov) novEnd = nov;
int numBlock = (novEnd-novStart + THREADS_PER_BLOCK-1) / THREADS_PER_BLOCK;
*/
gpuErrchk(cudaMalloc((void**)&adj_d, (nnz) * sizeof(int)));
gpuErrchk(cudaMalloc((void**)&xadj_d, (nov + 1) * sizeof(int)));
gpuErrchk(cudaMalloc((void**)&output_d, (nov) * sizeof(int)));
//gpuErrchk(cudaMallocHost((void **)&output_h, (nov) * sizeof(int)));
gpuErrchk(cudaMemcpy(adj_d, adj, (nnz) * sizeof(int), cudaMemcpyHostToDevice));
gpuErrchk(cudaMemcpy(xadj_d, xadj, (nov + 1) * sizeof(int), cudaMemcpyHostToDevice));
cudaEventCreate(&start);
cudaEventRecord(start, 0);
if (n==3) kernel3<<<numBlock, THREADS_PER_BLOCK>>>(adj_d, xadj_d, output_d, nov);
else if (n==4) kernel4<<<numBlock, THREADS_PER_BLOCK>>>(adj_d, xadj_d, output_d, nov);
else if (n==5) kernel5<<<numBlock, THREADS_PER_BLOCK>>>(adj_d, xadj_d, output_d, nov);
//combination<<<numBlocks, threadsPerBlock>>>(adj_d, xadj_d, output_d, n, nov);
gpuErrchk(cudaDeviceSynchronize());
gpuErrchk(cudaMemcpy(output_h, output_d, (nov) * sizeof(int), cudaMemcpyDeviceToHost));
cudaEventCreate(&stop);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
if(flag == 0) printArray(output_h,nov);
cudaFree(adj_d);
cudaFree(xadj_d);
if(flag == 1) printf("GPU scale took: %f s\n", elapsedTime/1000);
}
void read_mtxbin(string fname, int k){
//cout << "fname: " << fname << endl;
ifstream infile(fname);
int a, b;
int nnv = 0;
unordered_map<int, vector<int> > hashmap;
int maxElement = -1;
while (infile >> a >> b)
{
nnv+=2;
hashmap[a].push_back(b);
hashmap[b].push_back(a);
if(b > maxElement){
maxElement = b;
}
}
//cout << end1-start1 << " -- ILK OKUMA SU (s).\n";
int nov = maxElement +1;
//cout <<"nov " << nov << endl;
//cout <<"nnv " << nnv << endl;
int * adj = new int[nnv];
int * xadj = new int[nov+1];
xadj[0]=0;
int j = 0;
int maxSize = -1;
for(int i=0; i < nov ; i++ ){
auto current = hashmap.find(i);
if (current == hashmap.end()){
xadj[i+1] = xadj[i];
}
else{
int size = current->second.size();
maxSize = max(size,maxSize);
xadj[i+1] = xadj[i] + size;
for(auto val : current->second) {
adj[j] = val;
j++;
}
}
}
// cout << "maxSize: "<<maxSize<<endl;
// cout << end-start << " -- OKUMA SURE (s).\n";
wrapper(xadj,adj,k,nov,nnv);
//cout<<"CYCLES: --> "<<countCycles_sparse(xadj, adj,k,nov)<<endl;
/*double end2 = omp_get_wtime();
cout << end2-start << " -- TOTAL SURE (s).\n";*/
}
int main(int argc, char *argv[]){
char* fname = argv[1];
int k = atoi(argv[2]);
flag = atoi(argv[3]);
read_mtxbin(fname,k);
return 0;
}