-
Notifications
You must be signed in to change notification settings - Fork 0
/
floyd-warshall.cu
97 lines (81 loc) · 2.26 KB
/
floyd-warshall.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
%%cu
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
__global__ void calculate(long *d_adj, int k, int n){
int x = threadIdx.x;
int y = threadIdx.y;
int idx = (x * n) + y;
int idx1 = (x * n) + k;
int idx2 = (k * n) + y;
long s = d_adj[idx1] + d_adj[idx2];
__syncthreads();
if(s < d_adj[idx])
d_adj[idx] = s;
__syncthreads();
}
int main(int argc, char** argv){
int i, j, k, n = 10;
long h_adj[n * n], h_ans[n * n];
printf("Adjaceny Matrix\n\n");
for (i = 0;i < n; i++){
for (j = 0;j < n; j++){
int pos = (i * n) + j;
if (i == j) {
h_adj[pos] = 0;
printf("%ld ", h_adj[pos]);
continue;
}
h_adj[pos] = rand()%n;
h_adj[pos] += 1;
printf("%ld ", h_adj[pos]);
}
printf("\n");
}
long* d_adj;
cudaMalloc((void**)&d_adj, n * n * sizeof(long*));
cudaMemcpy(d_adj, h_adj, n * n * sizeof(long), cudaMemcpyHostToDevice);
//GPU method
for(k = 0; k < n; k++)
calculate<<<1, dim3(n, n, 1)>>>(d_adj, k, n);
cudaMemcpy(h_ans, d_adj, n * n * sizeof(long), cudaMemcpyDeviceToHost);
cudaFree(d_adj);
printf("\nShortest Pairwise Distances\n\n");
for (i = 0;i < n; i++){
for (j = 0;j < n; j++){
int pos = (i * n) + j;
printf("%ld ", h_ans[pos]);
}
printf("\n");
}
//CPU method
for (k = 0; k < n; k++) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
int idx1 = (i * n) + k;
int idx2 = (k * n) + j;
int idx = (i * n) + j;
long temp = h_adj[idx1] + h_adj[idx2];
if (temp < h_adj[idx])
h_adj[idx] = temp;
}
}
}
printf("\nCPU answer\n\n");
int ans_check = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
int idx = (i * n) + j;
printf("%ld ", h_adj[idx]);
if (h_adj[idx] != h_ans[idx])
ans_check = 1;
}
printf("\n");
}
if (ans_check)
printf("\nWrong Answer");
else
printf("\nCorrect Answer");
return 0;
}