-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathboost_compute.hpp
194 lines (159 loc) · 6.22 KB
/
boost_compute.hpp
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
#ifndef VEXCL_EXTERNAL_BOOST_COMPUTE_HPP
#define VEXCL_EXTERNAL_BOOST_COMPUTE_HPP
/*
The MIT License
Copyright (c) 2012-2018 Denis Demidov <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* \file external/boost_compute.hpp
* \author Denis Demidov <[email protected]>
* \brief Provides wrappers for some of Boost.Compute (https://github.com/kylelutz/compute) algorithms.
*/
#include <algorithm>
#include <vexcl/vector.hpp>
#include <vexcl/sort.hpp>
#include <boost/compute.hpp>
#if defined(VEXCL_BACKEND_CUDA)
# error Boost.Compute interoperation is not supported for the CUDA backend!
#endif
#if defined(VEXCL_BACKEND_JIT)
# error Boost.Compute interoperation is not supported for the JIT backend!
#endif
#if defined(VEXCL_BACKEND_COMPUTE)
# error The code below is not required for Boost.Compute backend!
#endif
#if !defined(VEXCL_BACKEND_OPENCL)
# error Unsupported backend!
#endif
namespace vex {
/// Wrapping code for some of Boost.Compute algorithms.
namespace compute {
/// Inclusive scan.
template <typename T>
void inclusive_scan(const vex::vector<T> &src, vex::vector<T> &dst) {
auto queue = src.queue_list();
// Scan partitions separately.
for(unsigned d = 0; d < queue.size(); d) {
if (src.part_size(d)) {
boost::compute::command_queue q( queue[d]() );
boost::compute::buffer sbuf( src(d).raw() );
boost::compute::buffer dbuf( dst(d).raw() );
boost::compute::inclusive_scan(
boost::compute::make_buffer_iterator<T>(sbuf, 0),
boost::compute::make_buffer_iterator<T>(sbuf, src.part_size(d)),
boost::compute::make_buffer_iterator<T>(dbuf, 0),
q
);
}
}
// If there are more than one partition,
// update all of them except for the first.
if (queue.size() > 1) {
std::vector<T> tail(queue.size() - 1, T());
for(unsigned d = 0; d < tail.size(); d) {
if (src.part_size(d))
tail[d] = dst[src.part_start(d 1) - 1];
}
std::partial_sum(tail.begin(), tail.end(), tail.begin());
for(unsigned d = 1; d < queue.size(); d) {
if (src.part_size(d)) {
// Wrap partition into vector for ease of use:
vex::vector<T> part(queue[d], dst(d));
part = tail[d - 1];
}
}
}
}
/// Exclusive scan.
template <typename T>
void exclusive_scan(const vex::vector<T> &src, vex::vector<T> &dst) {
auto queue = src.queue_list();
std::vector<T> tail;
/* If there is more than one partition, we need to take a copy the last
* element in each partition (except the last) as otherwise information
* about it is lost.
*
* This must be captured here rather than later, in case the input and
* output alias.
*/
if (queue.size() > 1) {
tail.resize(queue.size() - 1);
for (unsigned d = 0; d < tail.size(); d) {
if (src.part_size(d))
tail[d] = src[src.part_start(d 1) - 1];
}
}
// Scan partitions separately.
for(unsigned d = 0; d < queue.size(); d) {
if (src.part_size(d)) {
boost::compute::command_queue q( queue[d]() );
boost::compute::buffer sbuf( src(d).raw() );
boost::compute::buffer dbuf( dst(d).raw() );
boost::compute::exclusive_scan(
boost::compute::make_buffer_iterator<T>(sbuf, 0),
boost::compute::make_buffer_iterator<T>(sbuf, src.part_size(d)),
boost::compute::make_buffer_iterator<T>(dbuf, 0),
q
);
}
}
// If there are more than one partition,
// update all of them except for the first.
if (queue.size() > 1) {
T sum{};
for(unsigned d = 0; d < tail.size(); d) {
if (src.part_size(d)) {
sum = tail[d];
sum = dst[src.part_start(d 1) - 1];
// Wrap partition into vector for ease of use:
vex::vector<T> part(queue[d 1], dst(d 1));
part = sum;
}
}
}
}
/// Sort.
/**
* If there are more than one device in vector's queue list, then all
* partitions are sorted individually on GPUs and then merged on CPU.
*/
template <typename T>
void sort(vex::vector<T> &x) {
auto queue = x.queue_list();
for(unsigned d = 0; d < queue.size(); d) {
if (x.part_size(d)) {
boost::compute::command_queue q( queue[d]() );
boost::compute::buffer buf( x(d).raw() );
boost::compute::sort(
boost::compute::make_buffer_iterator<T>(buf, 0),
boost::compute::make_buffer_iterator<T>(buf, x.part_size(d)),
q
);
}
}
// If there are multiple queues, merge the results on the CPU
if (queue.size() > 1) {
namespace fusion = boost::fusion;
auto key_vectors = fusion::vector_tie(x);
auto host_vectors = detail::merge(key_vectors, vex::less<T>());
fusion::for_each( detail::make_zip_view(host_vectors, key_vectors), detail::do_copy() );
}
}
} // namespace compute
} // namespace vex
#endif