forked from jaredhoberock/bulk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce.cu
162 lines (117 loc) · 4.6 KB
/
reduce.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
#include <cstdio>
#include <bulk/bulk.hpp>
#include <thrust/device_vector.h>
#include <thrust/sequence.h>
#include <thrust/reduce.h>
#include <thrust/extrema.h>
#include <cassert>
#include <iostream>
#include "time_invocation_cuda.hpp"
#include "decomposition.hpp"
struct reduce_partitions
{
template<typename ConcurrentGroup, typename Iterator1, typename Iterator2, typename T, typename BinaryOperation>
__device__
void operator()(ConcurrentGroup &this_group, Iterator1 first, Iterator1 last, Iterator2 result, T init, BinaryOperation binary_op)
{
T sum = bulk::reduce(this_group, first, last, init, binary_op);
if(this_group.this_exec.index() == 0)
{
*result = sum;
}
}
template<typename ConcurrentGroup, typename Iterator1, typename Iterator2, typename BinaryOperation>
__device__
void operator()(ConcurrentGroup &this_group, Iterator1 first, Iterator1 last, Iterator2 result, BinaryOperation binary_op)
{
// noticeably faster to pass the last element as the init
typename thrust::iterator_value<Iterator2>::type init = last[-1];
(*this)(this_group, first, last - 1, result, init, binary_op);
}
template<typename ConcurrentGroup, typename Iterator1, typename Decomposition, typename Iterator2, typename T, typename BinaryFunction>
__device__
void operator()(ConcurrentGroup &this_group, Iterator1 first, Decomposition decomp, Iterator2 result, T init, BinaryFunction binary_op)
{
typename Decomposition::range range = decomp[this_group.index()];
Iterator1 last = first + range.second;
first += range.first;
if(this_group.index() != 0)
{
// noticeably faster to pass the last element as the init
init = last[-1];
--last;
} // end if
(*this)(this_group, first, last, result + this_group.index(), init, binary_op);
}
};
template<typename RandomAccessIterator,
typename T,
typename BinaryOperation>
T my_reduce(RandomAccessIterator first, RandomAccessIterator last, T init, BinaryOperation binary_op)
{
typedef typename thrust::iterator_difference<RandomAccessIterator>::type size_type;
const size_type n = last - first;
if(n <= 0) return init;
const size_type groupsize = 128;
const size_type grainsize = 7;
const size_type tile_size = groupsize * grainsize;
const size_type num_tiles = (n + tile_size - 1) / tile_size;
const size_type subscription = 10;
bulk::concurrent_group<
bulk::agent<grainsize>,
groupsize
> g;
const size_type num_groups = thrust::min<size_type>(subscription * g.hardware_concurrency(), num_tiles);
aligned_decomposition<size_type> decomp(n, num_groups, tile_size);
thrust::cuda::tag t;
thrust::detail::temporary_array<T,thrust::cuda::tag> partial_sums(t, decomp.size());
// reduce into partial sums
bulk::async(bulk::par(g, decomp.size()), reduce_partitions(), bulk::root.this_exec, first, decomp, partial_sums.begin(), init, binary_op);
if(partial_sums.size() > 1)
{
// reduce the partial sums
bulk::async(g, reduce_partitions(), bulk::root, partial_sums.begin(), partial_sums.end(), partial_sums.begin(), binary_op);
} // end while
return partial_sums[0];
} // end my_reduce()
template<typename T>
T my_reduce(const thrust::device_vector<T> *vec)
{
return my_reduce(vec->begin(), vec->end(), T(0), thrust::plus<T>());
}
template<typename T>
T thrust_reduce(const thrust::device_vector<T> *vec)
{
return thrust::reduce(vec->begin(), vec->end(), T(0), thrust::plus<T>());
}
template<typename T>
void compare()
{
thrust::device_vector<T> vec(1 << 28);
thrust_reduce(&vec);
double thrust_msecs = time_invocation_cuda(50, thrust_reduce<T>, &vec);
my_reduce(&vec);
double my_msecs = time_invocation_cuda(50, my_reduce<T>, &vec);
std::cout << "Thrust's time: " << thrust_msecs << " ms" << std::endl;
std::cout << "My time: " << my_msecs << " ms" << std::endl;
std::cout << "Performance relative to Thrust: " << thrust_msecs / my_msecs << std::endl;
}
int main()
{
size_t n = 123456789;
thrust::device_vector<int> vec(n);
thrust::sequence(vec.begin(), vec.end());
int my_result = my_reduce(vec.begin(), vec.end(), 13, thrust::plus<int>());
std::cout << "my_result: " << my_result << std::endl;
int thrust_result = thrust::reduce(vec.begin(), vec.end(), 13, thrust::plus<int>());
std::cout << "thrust_result: " << thrust_result << std::endl;
assert(thrust_result == my_result);
std::cout << "int: " << std::endl;
compare<int>();
std::cout << "long int: " << std::endl;
compare<long int>();
std::cout << "float: " << std::endl;
compare<float>();
std::cout << "double: " << std::endl;
compare<double>();
}