-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsum.cu
71 lines (51 loc) · 1.51 KB
/
sum.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 <cstdio>
#include <bulk/bulk.hpp>
#include <thrust/device_vector.h>
#include <cassert>
struct sum
{
__device__
void operator()(bulk::concurrent_group<> &g, thrust::device_ptr<int> data, thrust::device_ptr<int> result)
{
unsigned int n = g.size();
// allocate some special memory that the group can use for fast communication
int *s_data = static_cast<int*>(bulk::malloc(g, n * sizeof(int)));
// the whole group cooperatively copies the data
bulk::copy_n(g, data, n, s_data);
while(n > 1)
{
unsigned int half_n = n / 2;
if(g.this_exec.index() < half_n)
{
s_data[g.this_exec.index()] += s_data[n - g.this_exec.index() - 1];
}
// the group synchronizes after each update
g.wait();
n -= half_n;
}
if(g.this_exec.index() == 0)
{
*result = s_data[0];
}
// wait for agent 0 to store the result
g.wait();
// free the memory cooperatively
bulk::free(g, s_data);
}
};
int main()
{
size_t group_size = 512;
size_t n = group_size;
// [1, 1, 1, ... 1] - 512 of them
thrust::device_vector<int> vec(n, 1);
thrust::device_vector<int> result(1);
using bulk::con;
// let the runtime size the heap
bulk::async(con(group_size), sum(), bulk::root, vec.data(), result.data());
assert(512 == result[0]);
// size the heap ourself
size_t heap_size = group_size * sizeof(int);
bulk::async(con(group_size, heap_size), sum(), bulk::root, vec.data(), result.data());
assert(512 == result[0]);
}