-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathping_pong.cu
71 lines (56 loc) · 1.21 KB
/
ping_pong.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
#include <thrust/device_vector.h>
#include <bulk/bulk.hpp>
#include <cstdio>
struct ping
{
__device__
void operator()(volatile int *ball)
{
*ball = 1;
for(unsigned int next_state = 2;
next_state < 25;
next_state += 2)
{
while(*ball != next_state)
{
printf("ping waiting for return\n");
}
*ball += 1;
printf("ping! ball is now %d\n", next_state + 1);
}
}
};
struct pong
{
__device__
void operator()(volatile int *ball)
{
for(unsigned int next_state = 1;
next_state < 25;
next_state += 2)
{
while(*ball != next_state)
{
printf("pong waiting for return\n");
}
*ball += 1;
printf("pong! ball is now %d\n", next_state + 1);
}
}
};
int main()
{
cudaStream_t s1, s2;
cudaStreamCreate(&s1);
cudaStreamCreate(&s2);
using bulk::par;
using bulk::async;
thrust::device_vector<int> ball(1);
bulk::future<void> t1 = async(par(s1, 1), ping(), thrust::raw_pointer_cast(&*ball.data()));
bulk::future<void> t2 = async(par(s2, 1), pong(), thrust::raw_pointer_cast(&*ball.data()));
t1.wait();
t2.wait();
cudaStreamDestroy(s1);
cudaStreamDestroy(s2);
return 0;
}